nghttp3_conv.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include <string.h>
  28. #include <assert.h>
  29. #include "nghttp3_str.h"
  30. #include "nghttp3_unreachable.h"
  31. const uint8_t *nghttp3_get_varint(int64_t *dest, const uint8_t *p) {
  32. union {
  33. uint8_t n8;
  34. uint16_t n16;
  35. uint32_t n32;
  36. uint64_t n64;
  37. } n;
  38. switch (*p >> 6) {
  39. case 0:
  40. *dest = *p++;
  41. return p;
  42. case 1:
  43. memcpy(&n, p, 2);
  44. n.n8 &= 0x3f;
  45. *dest = ntohs(n.n16);
  46. return p + 2;
  47. case 2:
  48. memcpy(&n, p, 4);
  49. n.n8 &= 0x3f;
  50. *dest = ntohl(n.n32);
  51. return p + 4;
  52. case 3:
  53. memcpy(&n, p, 8);
  54. n.n8 &= 0x3f;
  55. *dest = (int64_t)nghttp3_ntohl64(n.n64);
  56. return p + 8;
  57. default:
  58. nghttp3_unreachable();
  59. }
  60. }
  61. int64_t nghttp3_get_varint_fb(const uint8_t *p) { return *p & 0x3f; }
  62. size_t nghttp3_get_varintlen(const uint8_t *p) {
  63. return (size_t)(1u << (*p >> 6));
  64. }
  65. uint8_t *nghttp3_put_uint64be(uint8_t *p, uint64_t n) {
  66. n = nghttp3_htonl64(n);
  67. return nghttp3_cpymem(p, (const uint8_t *)&n, sizeof(n));
  68. }
  69. uint8_t *nghttp3_put_uint32be(uint8_t *p, uint32_t n) {
  70. n = htonl(n);
  71. return nghttp3_cpymem(p, (const uint8_t *)&n, sizeof(n));
  72. }
  73. uint8_t *nghttp3_put_uint16be(uint8_t *p, uint16_t n) {
  74. n = htons(n);
  75. return nghttp3_cpymem(p, (const uint8_t *)&n, sizeof(n));
  76. }
  77. uint8_t *nghttp3_put_varint(uint8_t *p, int64_t n) {
  78. uint8_t *rv;
  79. if (n < 64) {
  80. *p++ = (uint8_t)n;
  81. return p;
  82. }
  83. if (n < 16384) {
  84. rv = nghttp3_put_uint16be(p, (uint16_t)n);
  85. *p |= 0x40;
  86. return rv;
  87. }
  88. if (n < 1073741824) {
  89. rv = nghttp3_put_uint32be(p, (uint32_t)n);
  90. *p |= 0x80;
  91. return rv;
  92. }
  93. assert(n < 4611686018427387904LL);
  94. rv = nghttp3_put_uint64be(p, (uint64_t)n);
  95. *p |= 0xc0;
  96. return rv;
  97. }
  98. size_t nghttp3_put_varintlen(int64_t n) {
  99. if (n < 64) {
  100. return 1;
  101. }
  102. if (n < 16384) {
  103. return 2;
  104. }
  105. if (n < 1073741824) {
  106. return 4;
  107. }
  108. assert(n < 4611686018427387904LL);
  109. return 8;
  110. }
  111. uint64_t nghttp3_ord_stream_id(int64_t stream_id) {
  112. return (uint64_t)(stream_id >> 2) + 1;
  113. }