1/* 2 * nghttp3 3 * 4 * Copyright (c) 2019 nghttp3 contributors 5 * Copyright (c) 2017 ngtcp2 contributors 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining 8 * a copy of this software and associated documentation files (the 9 * "Software"), to deal in the Software without restriction, including 10 * without limitation the rights to use, copy, modify, merge, publish, 11 * distribute, sublicense, and/or sell copies of the Software, and to 12 * permit persons to whom the Software is furnished to do so, subject to 13 * the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be 16 * included in all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25 */ 26#include "nghttp3_conv.h" 27 28#include <string.h> 29#include <assert.h> 30 31#include "nghttp3_str.h" 32 33int64_t nghttp3_get_varint(size_t *plen, const uint8_t *p) { 34 union { 35 char b[8]; 36 uint16_t n16; 37 uint32_t n32; 38 uint64_t n64; 39 } n; 40 41 *plen = 1u << (*p >> 6); 42 43 switch (*plen) { 44 case 1: 45 return (int64_t)*p; 46 case 2: 47 memcpy(&n, p, 2); 48 n.b[0] &= 0x3f; 49 return (int64_t)ntohs(n.n16); 50 case 4: 51 memcpy(&n, p, 4); 52 n.b[0] &= 0x3f; 53 return (int64_t)ntohl(n.n32); 54 case 8: 55 memcpy(&n, p, 8); 56 n.b[0] &= 0x3f; 57 return (int64_t)nghttp3_ntohl64(n.n64); 58 } 59 60 assert(0); 61 abort(); 62} 63 64int64_t nghttp3_get_varint_fb(const uint8_t *p) { return *p & 0x3f; } 65 66size_t nghttp3_get_varint_len(const uint8_t *p) { return 1u << (*p >> 6); } 67 68uint8_t *nghttp3_put_uint64be(uint8_t *p, uint64_t n) { 69 n = nghttp3_htonl64(n); 70 return nghttp3_cpymem(p, (const uint8_t *)&n, sizeof(n)); 71} 72 73uint8_t *nghttp3_put_uint48be(uint8_t *p, uint64_t n) { 74 n = nghttp3_htonl64(n); 75 return nghttp3_cpymem(p, ((const uint8_t *)&n) + 2, 6); 76} 77 78uint8_t *nghttp3_put_uint32be(uint8_t *p, uint32_t n) { 79 n = htonl(n); 80 return nghttp3_cpymem(p, (const uint8_t *)&n, sizeof(n)); 81} 82 83uint8_t *nghttp3_put_uint24be(uint8_t *p, uint32_t n) { 84 n = htonl(n); 85 return nghttp3_cpymem(p, ((const uint8_t *)&n) + 1, 3); 86} 87 88uint8_t *nghttp3_put_uint16be(uint8_t *p, uint16_t n) { 89 n = htons(n); 90 return nghttp3_cpymem(p, (const uint8_t *)&n, sizeof(n)); 91} 92 93uint8_t *nghttp3_put_varint(uint8_t *p, int64_t n) { 94 uint8_t *rv; 95 if (n < 64) { 96 *p++ = (uint8_t)n; 97 return p; 98 } 99 if (n < 16384) { 100 rv = nghttp3_put_uint16be(p, (uint16_t)n); 101 *p |= 0x40; 102 return rv; 103 } 104 if (n < 1073741824) { 105 rv = nghttp3_put_uint32be(p, (uint32_t)n); 106 *p |= 0x80; 107 return rv; 108 } 109 assert(n < 4611686018427387904LL); 110 rv = nghttp3_put_uint64be(p, (uint64_t)n); 111 *p |= 0xc0; 112 return rv; 113} 114 115size_t nghttp3_put_varint_len(int64_t n) { 116 if (n < 64) { 117 return 1; 118 } 119 if (n < 16384) { 120 return 2; 121 } 122 if (n < 1073741824) { 123 return 4; 124 } 125 assert(n < 4611686018427387904LL); 126 return 8; 127} 128 129uint64_t nghttp3_ord_stream_id(int64_t stream_id) { 130 return (uint64_t)(stream_id >> 2) + 1; 131} 132 133uint8_t nghttp3_pri_to_uint8(const nghttp3_pri *pri) { 134 return (uint8_t)((uint32_t)pri->inc << 7 | pri->urgency); 135} 136