nghttp3_conv.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. #ifndef NGHTTP3_CONV_H
  27. #define NGHTTP3_CONV_H
  28. #ifdef HAVE_CONFIG_H
  29. # include <config.h>
  30. #endif /* defined(HAVE_CONFIG_H) */
  31. #ifdef HAVE_ARPA_INET_H
  32. # include <arpa/inet.h>
  33. #endif /* defined(HAVE_ARPA_INET_H) */
  34. #ifdef HAVE_NETINET_IN_H
  35. # include <netinet/in.h>
  36. #endif /* defined(HAVE_NETINET_IN_H) */
  37. #ifdef HAVE_BYTESWAP_H
  38. # include <byteswap.h>
  39. #endif /* defined(HAVE_BYTESWAP_H) */
  40. #ifdef HAVE_ENDIAN_H
  41. # include <endian.h>
  42. #endif /* defined(HAVE_ENDIAN_H) */
  43. #ifdef HAVE_SYS_ENDIAN_H
  44. # include <sys/endian.h>
  45. #endif /* defined(HAVE_SYS_ENDIAN_H) */
  46. #ifdef __APPLE__
  47. # include <libkern/OSByteOrder.h>
  48. #endif /* defined(__APPLE__) */
  49. #include <nghttp3/nghttp3.h>
  50. #if HAVE_DECL_BE64TOH
  51. # define nghttp3_ntohl64(N) be64toh(N)
  52. # define nghttp3_htonl64(N) htobe64(N)
  53. #else /* !HAVE_DECL_BE64TOH */
  54. # ifdef WORDS_BIGENDIAN
  55. # define nghttp3_ntohl64(N) (N)
  56. # define nghttp3_htonl64(N) (N)
  57. # else /* !defined(WORDS_BIGENDIAN) */
  58. # if HAVE_DECL_BSWAP_64
  59. # define nghttp3_bswap64 bswap_64
  60. # elif defined(WIN32)
  61. # define nghttp3_bswap64 _byteswap_uint64
  62. # elif defined(__APPLE__)
  63. # define nghttp3_bswap64 OSSwapInt64
  64. # else /* !(HAVE_DECL_BSWAP_64 || defined(WIN32) || defined(__APPLE__)) */
  65. # define nghttp3_bswap64(N) \
  66. ((uint64_t)(ntohl((uint32_t)(N))) << 32 | ntohl((uint32_t)((N) >> 32)))
  67. # endif /* !(HAVE_DECL_BSWAP_64 || defined(WIN32) || defined(__APPLE__)) */
  68. # define nghttp3_ntohl64(N) nghttp3_bswap64(N)
  69. # define nghttp3_htonl64(N) nghttp3_bswap64(N)
  70. # endif /* !defined(WORDS_BIGENDIAN) */
  71. #endif /* !HAVE_DECL_BE64TOH */
  72. #ifdef WIN32
  73. /* Windows requires ws2_32 library for ntonl family of functions. We
  74. define inline functions for those functions so that we don't have
  75. dependency on that lib. */
  76. # ifdef _MSC_VER
  77. # define STIN static __inline
  78. # else /* !defined(_MSC_VER) */
  79. # define STIN static inline
  80. # endif /* !defined(_MSC_VER) */
  81. STIN uint32_t htonl(uint32_t hostlong) {
  82. uint32_t res;
  83. unsigned char *p = (unsigned char *)&res;
  84. *p++ = (unsigned char)(hostlong >> 24);
  85. *p++ = (hostlong >> 16) & 0xffu;
  86. *p++ = (hostlong >> 8) & 0xffu;
  87. *p = hostlong & 0xffu;
  88. return res;
  89. }
  90. STIN uint16_t htons(uint16_t hostshort) {
  91. uint16_t res;
  92. unsigned char *p = (unsigned char *)&res;
  93. *p++ = (unsigned char)(hostshort >> 8);
  94. *p = hostshort & 0xffu;
  95. return res;
  96. }
  97. STIN uint32_t ntohl(uint32_t netlong) {
  98. uint32_t res;
  99. unsigned char *p = (unsigned char *)&netlong;
  100. res = (uint32_t)(*p++ << 24);
  101. res += (uint32_t)(*p++ << 16);
  102. res += (uint32_t)(*p++ << 8);
  103. res += *p;
  104. return res;
  105. }
  106. STIN uint16_t ntohs(uint16_t netshort) {
  107. uint16_t res;
  108. unsigned char *p = (unsigned char *)&netshort;
  109. res = (uint16_t)(*p++ << 8);
  110. res += *p;
  111. return res;
  112. }
  113. #endif /* defined(WIN32) */
  114. /*
  115. * nghttp3_get_varint reads variable-length unsigned integer from |p|,
  116. * and stores it in the buffer pointed by |dest| in host byte order.
  117. * It returns |p| plus the number of bytes read from |p|.
  118. */
  119. const uint8_t *nghttp3_get_varint(int64_t *dest, const uint8_t *p);
  120. /*
  121. * nghttp3_get_varint_fb reads first byte of encoded variable-length
  122. * integer from |p|.
  123. */
  124. int64_t nghttp3_get_varint_fb(const uint8_t *p);
  125. /*
  126. * nghttp3_get_varintlen returns the required number of bytes to read
  127. * variable-length integer starting at |p|.
  128. */
  129. size_t nghttp3_get_varintlen(const uint8_t *p);
  130. /*
  131. * nghttp3_put_uint64be writes |n| in host byte order in |p| in
  132. * network byte order. It returns the one beyond of the last written
  133. * position.
  134. */
  135. uint8_t *nghttp3_put_uint64be(uint8_t *p, uint64_t n);
  136. /*
  137. * nghttp3_put_uint32be writes |n| in host byte order in |p| in
  138. * network byte order. It returns the one beyond of the last written
  139. * position.
  140. */
  141. uint8_t *nghttp3_put_uint32be(uint8_t *p, uint32_t n);
  142. /*
  143. * nghttp3_put_uint16be writes |n| in host byte order in |p| in
  144. * network byte order. It returns the one beyond of the last written
  145. * position.
  146. */
  147. uint8_t *nghttp3_put_uint16be(uint8_t *p, uint16_t n);
  148. /*
  149. * nghttp3_put_varint writes |n| in |p| using variable-length integer
  150. * encoding. It returns the one beyond of the last written position.
  151. */
  152. uint8_t *nghttp3_put_varint(uint8_t *p, int64_t n);
  153. /*
  154. * nghttp3_put_varintlen returns the required number of bytes to
  155. * encode |n|.
  156. */
  157. size_t nghttp3_put_varintlen(int64_t n);
  158. /*
  159. * nghttp3_ord_stream_id returns the ordinal number of |stream_id|.
  160. */
  161. uint64_t nghttp3_ord_stream_id(int64_t stream_id);
  162. /*
  163. * NGHTTP3_PRI_INC_MASK is a bit mask to retrieve incremental bit from
  164. * a value produced by nghttp3_pri_to_uint8.
  165. */
  166. #define NGHTTP3_PRI_INC_MASK (1 << 7)
  167. #endif /* !defined(NGHTTP3_CONV_H) */