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/nghttp2.h> 36 #include "nghttp2_mem.h" 37 38 #define nghttp2_max_def(SUFFIX, T) \ 39 static inline T nghttp2_max_##SUFFIX(T a, T b) { return a < b ? b : a; } 40 41 nghttp2_max_def(int8, int8_t); 42 nghttp2_max_def(int16, int16_t); 43 nghttp2_max_def(int32, int32_t); 44 nghttp2_max_def(int64, int64_t); 45 nghttp2_max_def(uint8, uint8_t); 46 nghttp2_max_def(uint16, uint16_t); 47 nghttp2_max_def(uint32, uint32_t); 48 nghttp2_max_def(uint64, uint64_t); 49 nghttp2_max_def(size, size_t); 50 51 #define nghttp2_min_def(SUFFIX, T) \ 52 static inline T nghttp2_min_##SUFFIX(T a, T b) { return a < b ? a : b; } 53 54 nghttp2_min_def(int8, int8_t); 55 nghttp2_min_def(int16, int16_t); 56 nghttp2_min_def(int32, int32_t); 57 nghttp2_min_def(int64, int64_t); 58 nghttp2_min_def(uint8, uint8_t); 59 nghttp2_min_def(uint16, uint16_t); 60 nghttp2_min_def(uint32, uint32_t); 61 nghttp2_min_def(uint64, uint64_t); 62 nghttp2_min_def(size, size_t); 63 64 #define lstreq(A, B, N) ((sizeof((A)) - 1) == (N) && memcmp((A), (B), (N)) == 0) 65 66 #define nghttp2_struct_of(ptr, type, member) \ 67 ((type *)(void *)((char *)(ptr)-offsetof(type, member))) 68 69 /* 70 * Copies 2 byte unsigned integer |n| in host byte order to |buf| in 71 * network byte order. 72 */ 73 void nghttp2_put_uint16be(uint8_t *buf, uint16_t n); 74 75 /* 76 * Copies 4 byte unsigned integer |n| in host byte order to |buf| in 77 * network byte order. 78 */ 79 void nghttp2_put_uint32be(uint8_t *buf, uint32_t n); 80 81 /* 82 * Retrieves 2 byte unsigned integer stored in |data| in network byte 83 * order and returns it in host byte order. 84 */ 85 uint16_t nghttp2_get_uint16(const uint8_t *data); 86 87 /* 88 * Retrieves 4 byte unsigned integer stored in |data| in network byte 89 * order and returns it in host byte order. 90 */ 91 uint32_t nghttp2_get_uint32(const uint8_t *data); 92 93 void nghttp2_downcase(uint8_t *s, size_t len); 94 95 /* 96 * Adjusts |*local_window_size_ptr|, |*recv_window_size_ptr|, 97 * |*recv_reduction_ptr| with |*delta_ptr| which is the 98 * WINDOW_UPDATE's window_size_increment sent from local side. If 99 * |delta| is strictly larger than |*recv_window_size_ptr|, 100 * |*local_window_size_ptr| is increased by delta - 101 * *recv_window_size_ptr. If |delta| is negative, 102 * |*local_window_size_ptr| is decreased by delta. 103 * 104 * This function returns 0 if it succeeds, or one of the following 105 * negative error codes: 106 * 107 * NGHTTP2_ERR_FLOW_CONTROL 108 * local_window_size overflow or gets negative. 109 */ 110 int nghttp2_adjust_local_window_size(int32_t *local_window_size_ptr, 111 int32_t *recv_window_size_ptr, 112 int32_t *recv_reduction_ptr, 113 int32_t *delta_ptr); 114 115 /* 116 * This function works like nghttp2_adjust_local_window_size(). The 117 * difference is that this function assumes *delta_ptr >= 0, and 118 * *recv_window_size_ptr is not decreased by *delta_ptr. 119 * 120 * This function returns 0 if it succeeds, or one of the following 121 * negative error codes: 122 * 123 * NGHTTP2_ERR_FLOW_CONTROL 124 * local_window_size overflow or gets negative. 125 */ 126 int nghttp2_increase_local_window_size(int32_t *local_window_size_ptr, 127 int32_t *recv_window_size_ptr, 128 int32_t *recv_reduction_ptr, 129 int32_t *delta_ptr); 130 131 /* 132 * Returns non-zero if the function decided that WINDOW_UPDATE should 133 * be sent. 134 */ 135 int nghttp2_should_send_window_update(int32_t local_window_size, 136 int32_t recv_window_size); 137 138 /* 139 * Copies the buffer |src| of length |len| to the destination pointed 140 * by the |dest|, assuming that the |dest| is at lest |len| bytes long 141 * . Returns dest + len. 142 */ 143 uint8_t *nghttp2_cpymem(uint8_t *dest, const void *src, size_t len); 144 145 #endif /* NGHTTP2_HELPER_H */ 146