ngtcp2_conv.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include "ngtcp2_conv.h"
  26. #include <string.h>
  27. #include <assert.h>
  28. #include "ngtcp2_str.h"
  29. #include "ngtcp2_pkt.h"
  30. #include "ngtcp2_net.h"
  31. #include "ngtcp2_unreachable.h"
  32. const uint8_t *ngtcp2_get_uint64be(uint64_t *dest, const uint8_t *p) {
  33. memcpy(dest, p, sizeof(*dest));
  34. *dest = ngtcp2_ntohl64(*dest);
  35. return p + sizeof(*dest);
  36. }
  37. const uint8_t *ngtcp2_get_uint32be(uint32_t *dest, const uint8_t *p) {
  38. memcpy(dest, p, sizeof(*dest));
  39. *dest = ngtcp2_ntohl(*dest);
  40. return p + sizeof(*dest);
  41. }
  42. const uint8_t *ngtcp2_get_uint24be(uint32_t *dest, const uint8_t *p) {
  43. *dest = 0;
  44. memcpy(((uint8_t *)dest) + 1, p, 3);
  45. *dest = ngtcp2_ntohl(*dest);
  46. return p + 3;
  47. }
  48. const uint8_t *ngtcp2_get_uint16be(uint16_t *dest, const uint8_t *p) {
  49. memcpy(dest, p, sizeof(*dest));
  50. *dest = ngtcp2_ntohs(*dest);
  51. return p + sizeof(*dest);
  52. }
  53. const uint8_t *ngtcp2_get_uint16(uint16_t *dest, const uint8_t *p) {
  54. memcpy(dest, p, sizeof(*dest));
  55. return p + sizeof(*dest);
  56. }
  57. static const uint8_t *get_uvarint(uint64_t *dest, const uint8_t *p) {
  58. union {
  59. uint8_t n8;
  60. uint16_t n16;
  61. uint32_t n32;
  62. uint64_t n64;
  63. } n;
  64. switch (*p >> 6) {
  65. case 0:
  66. *dest = *p++;
  67. return p;
  68. case 1:
  69. memcpy(&n, p, 2);
  70. n.n8 &= 0x3f;
  71. *dest = ngtcp2_ntohs(n.n16);
  72. return p + 2;
  73. case 2:
  74. memcpy(&n, p, 4);
  75. n.n8 &= 0x3f;
  76. *dest = ngtcp2_ntohl(n.n32);
  77. return p + 4;
  78. case 3:
  79. memcpy(&n, p, 8);
  80. n.n8 &= 0x3f;
  81. *dest = ngtcp2_ntohl64(n.n64);
  82. return p + 8;
  83. default:
  84. ngtcp2_unreachable();
  85. }
  86. }
  87. const uint8_t *ngtcp2_get_uvarint(uint64_t *dest, const uint8_t *p) {
  88. return get_uvarint(dest, p);
  89. }
  90. const uint8_t *ngtcp2_get_varint(int64_t *dest, const uint8_t *p) {
  91. return get_uvarint((uint64_t *)dest, p);
  92. }
  93. int64_t ngtcp2_get_pkt_num(const uint8_t *p, size_t pkt_numlen) {
  94. uint32_t l;
  95. uint16_t s;
  96. switch (pkt_numlen) {
  97. case 1:
  98. return *p;
  99. case 2:
  100. ngtcp2_get_uint16be(&s, p);
  101. return (int64_t)s;
  102. case 3:
  103. ngtcp2_get_uint24be(&l, p);
  104. return (int64_t)l;
  105. case 4:
  106. ngtcp2_get_uint32be(&l, p);
  107. return (int64_t)l;
  108. default:
  109. ngtcp2_unreachable();
  110. }
  111. }
  112. uint8_t *ngtcp2_put_uint64be(uint8_t *p, uint64_t n) {
  113. n = ngtcp2_htonl64(n);
  114. return ngtcp2_cpymem(p, (const uint8_t *)&n, sizeof(n));
  115. }
  116. uint8_t *ngtcp2_put_uint32be(uint8_t *p, uint32_t n) {
  117. n = ngtcp2_htonl(n);
  118. return ngtcp2_cpymem(p, (const uint8_t *)&n, sizeof(n));
  119. }
  120. uint8_t *ngtcp2_put_uint24be(uint8_t *p, uint32_t n) {
  121. n = ngtcp2_htonl(n);
  122. return ngtcp2_cpymem(p, ((const uint8_t *)&n) + 1, 3);
  123. }
  124. uint8_t *ngtcp2_put_uint16be(uint8_t *p, uint16_t n) {
  125. n = ngtcp2_htons(n);
  126. return ngtcp2_cpymem(p, (const uint8_t *)&n, sizeof(n));
  127. }
  128. uint8_t *ngtcp2_put_uint16(uint8_t *p, uint16_t n) {
  129. return ngtcp2_cpymem(p, (const uint8_t *)&n, sizeof(n));
  130. }
  131. uint8_t *ngtcp2_put_uvarint(uint8_t *p, uint64_t n) {
  132. uint8_t *rv;
  133. if (n < 64) {
  134. *p++ = (uint8_t)n;
  135. return p;
  136. }
  137. if (n < 16384) {
  138. rv = ngtcp2_put_uint16be(p, (uint16_t)n);
  139. *p |= 0x40;
  140. return rv;
  141. }
  142. if (n < 1073741824) {
  143. rv = ngtcp2_put_uint32be(p, (uint32_t)n);
  144. *p |= 0x80;
  145. return rv;
  146. }
  147. assert(n < 4611686018427387904ULL);
  148. rv = ngtcp2_put_uint64be(p, n);
  149. *p |= 0xc0;
  150. return rv;
  151. }
  152. uint8_t *ngtcp2_put_uvarint30(uint8_t *p, uint32_t n) {
  153. uint8_t *rv;
  154. assert(n < 1073741824);
  155. rv = ngtcp2_put_uint32be(p, n);
  156. *p |= 0x80;
  157. return rv;
  158. }
  159. uint8_t *ngtcp2_put_pkt_num(uint8_t *p, int64_t pkt_num, size_t len) {
  160. switch (len) {
  161. case 1:
  162. *p++ = (uint8_t)pkt_num;
  163. return p;
  164. case 2:
  165. return ngtcp2_put_uint16be(p, (uint16_t)pkt_num);
  166. case 3:
  167. return ngtcp2_put_uint24be(p, (uint32_t)pkt_num);
  168. case 4:
  169. return ngtcp2_put_uint32be(p, (uint32_t)pkt_num);
  170. default:
  171. ngtcp2_unreachable();
  172. }
  173. }
  174. size_t ngtcp2_get_uvarintlen(const uint8_t *p) {
  175. return (size_t)(1u << (*p >> 6));
  176. }
  177. size_t ngtcp2_put_uvarintlen(uint64_t n) {
  178. if (n < 64) {
  179. return 1;
  180. }
  181. if (n < 16384) {
  182. return 2;
  183. }
  184. if (n < 1073741824) {
  185. return 4;
  186. }
  187. assert(n < 4611686018427387904ULL);
  188. return 8;
  189. }
  190. uint64_t ngtcp2_ord_stream_id(int64_t stream_id) {
  191. return (uint64_t)(stream_id >> 2) + 1;
  192. }