From 54c150653dbaf4bee545051427223fbd2e7be46a Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Tue, 10 Oct 2017 17:10:03 +0200 Subject: [PATCH] MINOR: h2: add a few functions to retrieve contents from a wrapping buffer Functions h2_get_buf_n{16,32,64}() and h2_get_buf_bytes() respectively extract a network-ordered 16/32/64 bit value from a possibly wrapping buffer, or any arbitrary size. They're convenient to retrieve a PING payload or to parse SETTINGS frames. Since they copy one byte at a time, they will be less efficient than a memcpy-based implementation on large blocks. --- src/mux_h2.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/mux_h2.c b/src/mux_h2.c index 422fca227..629d2758d 100644 --- a/src/mux_h2.c +++ b/src/mux_h2.c @@ -401,6 +401,33 @@ static inline void h2_set_frame_size(void *frame, uint32_t len) write_n16(out + 1, len); } +/* reads bytes from buffer starting at relative offset from the + * current pointer, dealing with wrapping, and stores the result in . It's + * the caller's responsibility to verify that there are at least bytes + * available in the buffer's input prior to calling this function. + */ +static inline void h2_get_buf_bytes(void *dst, size_t bytes, + const struct buffer *b, int o) +{ + readv_bytes(dst, bytes, b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); +} + +static inline uint16_t h2_get_n16(const struct buffer *b, int o) +{ + return readv_n16(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); +} + +static inline uint32_t h2_get_n32(const struct buffer *b, int o) +{ + return readv_n32(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); +} + +static inline uint64_t h2_get_n64(const struct buffer *b, int o) +{ + return readv_n64(b_ptr(b, o), b_end(b) - b_ptr(b, o), b->data); +} + + /* Peeks an H2 frame header from buffer into descriptor . The algorithm * is not obvious. It turns out that H2 headers are neither aligned nor do they * use regular sizes. And to add to the trouble, the buffer may wrap so each