1 /* 2 * ngtcp2 3 * 4 * Copyright (c) 2017 ngtcp2 contributors 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 NGTCP2_CONV_H 26 #define NGTCP2_CONV_H 27 28 #ifdef HAVE_CONFIG_H 29 # include <config.h> 30 #endif /* HAVE_CONFIG_H */ 31 32 #include <ngtcp2/ngtcp2.h> 33 34 /* 35 * ngtcp2_get_uint64 reads 8 bytes from |p| as 64 bits unsigned 36 * integer encoded as network byte order, and returns it in host byte 37 * order. 38 */ 39 uint64_t ngtcp2_get_uint64(const uint8_t *p); 40 41 /* 42 * ngtcp2_get_uint48 reads 6 bytes from |p| as 48 bits unsigned 43 * integer encoded as network byte order, and returns it in host byte 44 * order. 45 */ 46 uint64_t ngtcp2_get_uint48(const uint8_t *p); 47 48 /* 49 * ngtcp2_get_uint32 reads 4 bytes from |p| as 32 bits unsigned 50 * integer encoded as network byte order, and returns it in host byte 51 * order. 52 */ 53 uint32_t ngtcp2_get_uint32(const uint8_t *p); 54 55 /* 56 * ngtcp2_get_uint24 reads 3 bytes from |p| as 24 bits unsigned 57 * integer encoded as network byte order, and returns it in host byte 58 * order. 59 */ 60 uint32_t ngtcp2_get_uint24(const uint8_t *p); 61 62 /* 63 * ngtcp2_get_uint16 reads 2 bytes from |p| as 16 bits unsigned 64 * integer encoded as network byte order, and returns it in host byte 65 * order. 66 */ 67 uint16_t ngtcp2_get_uint16(const uint8_t *p); 68 69 /* 70 * ngtcp2_get_varint reads variable-length integer from |p|, and 71 * returns it in host byte order. The number of bytes read is stored 72 * in |*plen|. 73 */ 74 uint64_t ngtcp2_get_varint(size_t *plen, const uint8_t *p); 75 76 /* 77 * ngtcp2_get_pkt_num reads encoded packet number from |p|. The 78 * packet number is encoed in |pkt_numlen| bytes. 79 */ 80 int64_t ngtcp2_get_pkt_num(const uint8_t *p, size_t pkt_numlen); 81 82 /* 83 * ngtcp2_put_uint64be writes |n| in host byte order in |p| in network 84 * byte order. It returns the one beyond of the last written 85 * position. 86 */ 87 uint8_t *ngtcp2_put_uint64be(uint8_t *p, uint64_t n); 88 89 /* 90 * ngtcp2_put_uint48be writes |n| in host byte order in |p| in network 91 * byte order. It writes only least significant 48 bits. It returns 92 * the one beyond of the last written position. 93 */ 94 uint8_t *ngtcp2_put_uint48be(uint8_t *p, uint64_t n); 95 96 /* 97 * ngtcp2_put_uint32be writes |n| in host byte order in |p| in network 98 * byte order. It returns the one beyond of the last written 99 * position. 100 */ 101 uint8_t *ngtcp2_put_uint32be(uint8_t *p, uint32_t n); 102 103 /* 104 * ngtcp2_put_uint24be writes |n| in host byte order in |p| in network 105 * byte order. It writes only least significant 24 bits. It returns 106 * the one beyond of the last written position. 107 */ 108 uint8_t *ngtcp2_put_uint24be(uint8_t *p, uint32_t n); 109 110 /* 111 * ngtcp2_put_uint16be writes |n| in host byte order in |p| in network 112 * byte order. It returns the one beyond of the last written 113 * position. 114 */ 115 uint8_t *ngtcp2_put_uint16be(uint8_t *p, uint16_t n); 116 117 /* 118 * ngtcp2_put_varint writes |n| in |p| using variable-length integer 119 * encoding. It returns the one beyond of the last written position. 120 */ 121 uint8_t *ngtcp2_put_varint(uint8_t *p, uint64_t n); 122 123 /* 124 * ngtcp2_put_varint30 writes |n| in |p| using variable-length integer 125 * encoding. |n| must be strictly less than 1073741824. The function 126 * always encodes |n| in 4 bytes. It returns the one beyond of the 127 * last written position. 128 */ 129 uint8_t *ngtcp2_put_varint30(uint8_t *p, uint32_t n); 130 131 /* 132 * ngtcp2_put_pkt_num encodes |pkt_num| using |len| bytes. It 133 * returns the one beyond of the last written position. 134 */ 135 uint8_t *ngtcp2_put_pkt_num(uint8_t *p, int64_t pkt_num, size_t len); 136 137 /* 138 * ngtcp2_get_varint_len returns the required number of bytes to read 139 * variable-length integer starting at |p|. 140 */ 141 size_t ngtcp2_get_varint_len(const uint8_t *p); 142 143 /* 144 * ngtcp2_put_varint_len returns the required number of bytes to 145 * encode |n|. 146 */ 147 size_t ngtcp2_put_varint_len(uint64_t n); 148 149 /* 150 * ngtcp2_nth_server_bidi_id returns |n|-th server bidirectional 151 * stream ID. If |n| is 0, it returns 0. If the |n|-th stream ID is 152 * larger than NGTCP2_MAX_SERVER_STREAM_ID_BIDI, this function returns 153 * NGTCP2_MAX_SERVER_STREAM_ID_BIDI. 154 */ 155 int64_t ngtcp2_nth_server_bidi_id(uint64_t n); 156 157 /* 158 * ngtcp2_nth_client_bidi_id returns |n|-th client bidirectional 159 * stream ID. If |n| is 0, it returns 0. If the |n|-th stream ID is 160 * larger than NGTCP2_MAX_CLIENT_STREAM_ID_BIDI, this function returns 161 * NGTCP2_MAX_CLIENT_STREAM_ID_BIDI. 162 */ 163 int64_t ngtcp2_nth_client_bidi_id(uint64_t n); 164 165 /* 166 * ngtcp2_nth_server_uni_id returns |n|-th server unidirectional 167 * stream ID. If |n| is 0, it returns 0. If the |n|-th stream ID is 168 * larger than NGTCP2_MAX_SERVER_STREAM_ID_UNI, this function returns 169 * NGTCP2_MAX_SERVER_STREAM_ID_UNI. 170 */ 171 int64_t ngtcp2_nth_server_uni_id(uint64_t n); 172 173 /* 174 * ngtcp2_nth_client_uni_id returns |n|-th client unidirectional 175 * stream ID. If |n| is 0, it returns 0. If the |n|-th stream ID is 176 * larger than NGTCP2_MAX_CLIENT_STREAM_ID_UNI, this function returns 177 * NGTCP2_MAX_CLIENT_STREAM_ID_UNI. 178 */ 179 int64_t ngtcp2_nth_client_uni_id(uint64_t n); 180 181 /* 182 * ngtcp2_ord_stream_id returns the ordinal number of |stream_id|. 183 */ 184 uint64_t ngtcp2_ord_stream_id(int64_t stream_id); 185 186 #endif /* NGTCP2_CONV_H */ 187