1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 Tatsuhiro Tsujikawa
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #ifndef NGHTTP2_HELPER_H
26 #define NGHTTP2_HELPER_H
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif /* HAVE_CONFIG_H */
31 
32 #include <string.h>
33 #include <stddef.h>
34 
35 #include "nghttp2.h"
36 #include "nghttp2_mem.h"
37 
38 #define nghttp2_min(A, B) ((A) < (B) ? (A) : (B))
39 #define nghttp2_max(A, B) ((A) > (B) ? (A) : (B))
40 
41 #define lstreq(A, B, N)   ((sizeof((A)) - 1) == (N) && memcmp((A), (B), (N)) == 0)
42 
43 #define nghttp2_struct_of(ptr, type, member) \
44     ((type *)(void *)((char *)(ptr) - offsetof(type, member)))
45 
46 /*
47  * Copies 2 byte unsigned integer |n| in host byte order to |buf| in
48  * network byte order.
49  */
50 void nghttp2_put_uint16be(uint8_t *buf, uint16_t n);
51 
52 /*
53  * Copies 4 byte unsigned integer |n| in host byte order to |buf| in
54  * network byte order.
55  */
56 void nghttp2_put_uint32be(uint8_t *buf, uint32_t n);
57 
58 /*
59  * Retrieves 2 byte unsigned integer stored in |data| in network byte
60  * order and returns it in host byte order.
61  */
62 uint16_t nghttp2_get_uint16(const uint8_t *data);
63 
64 /*
65  * Retrieves 4 byte unsigned integer stored in |data| in network byte
66  * order and returns it in host byte order.
67  */
68 uint32_t nghttp2_get_uint32(const uint8_t *data);
69 
70 void nghttp2_downcase(uint8_t *s, size_t len);
71 
72 /*
73  * Adjusts |*local_window_size_ptr|, |*recv_window_size_ptr|,
74  * |*recv_reduction_ptr| with |*delta_ptr| which is the
75  * WINDOW_UPDATE's window_size_increment sent from local side. If
76  * |delta| is strictly larger than |*recv_window_size_ptr|,
77  * |*local_window_size_ptr| is increased by delta -
78  * *recv_window_size_ptr. If |delta| is negative,
79  * |*local_window_size_ptr| is decreased by delta.
80  *
81  * This function returns 0 if it succeeds, or one of the following
82  * negative error codes:
83  *
84  * NGHTTP2_ERR_FLOW_CONTROL
85  *     local_window_size overflow or gets negative.
86  */
87 int nghttp2_adjust_local_window_size(int32_t *local_window_size_ptr,
88                                      int32_t *recv_window_size_ptr,
89                                      int32_t *recv_reduction_ptr,
90                                      int32_t *delta_ptr);
91 
92 /*
93  * This function works like nghttp2_adjust_local_window_size().  The
94  * difference is that this function assumes *delta_ptr >= 0, and
95  * *recv_window_size_ptr is not decreased by *delta_ptr.
96  *
97  * This function returns 0 if it succeeds, or one of the following
98  * negative error codes:
99  *
100  * NGHTTP2_ERR_FLOW_CONTROL
101  *     local_window_size overflow or gets negative.
102  */
103 int nghttp2_increase_local_window_size(int32_t *local_window_size_ptr,
104                                        int32_t *recv_window_size_ptr,
105                                        int32_t *recv_reduction_ptr,
106                                        int32_t *delta_ptr);
107 
108 /*
109  * Returns non-zero if the function decided that WINDOW_UPDATE should
110  * be sent.
111  */
112 int nghttp2_should_send_window_update(int32_t local_window_size,
113                                       int32_t recv_window_size);
114 
115 /*
116  * Copies the buffer |src| of length |len| to the destination pointed
117  * by the |dest|, assuming that the |dest| is at lest |len| bytes long
118  * . Returns dest + len.
119  */
120 uint8_t *nghttp2_cpymem(uint8_t *dest, const void *src, size_t len);
121 
122 #endif /* NGHTTP2_HELPER_H */
123