ngtcp2_conv.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <ngtcp2/ngtcp2.h>
  31. /*
  32. * ngtcp2_get_uint64be reads 8 bytes from |p| as 64 bits unsigned
  33. * integer encoded as network byte order, and stores it in the buffer
  34. * pointed by |dest| in host byte order. It returns |p| + 8.
  35. */
  36. const uint8_t *ngtcp2_get_uint64be(uint64_t *dest, const uint8_t *p);
  37. /*
  38. * ngtcp2_get_uint32be reads 4 bytes from |p| as 32 bits unsigned
  39. * integer encoded as network byte order, and stores it in the buffer
  40. * pointed by |dest| in host byte order. It returns |p| + 4.
  41. */
  42. const uint8_t *ngtcp2_get_uint32be(uint32_t *dest, const uint8_t *p);
  43. /*
  44. * ngtcp2_get_uint24be reads 3 bytes from |p| as 24 bits unsigned
  45. * integer encoded as network byte order, and stores it in the buffer
  46. * pointed by |dest| in host byte order. It returns |p| + 3.
  47. */
  48. const uint8_t *ngtcp2_get_uint24be(uint32_t *dest, const uint8_t *p);
  49. /*
  50. * ngtcp2_get_uint16be reads 2 bytes from |p| as 16 bits unsigned
  51. * integer encoded as network byte order, and stores it in the buffer
  52. * pointed by |dest| in host byte order. It returns |p| + 2.
  53. */
  54. const uint8_t *ngtcp2_get_uint16be(uint16_t *dest, const uint8_t *p);
  55. /*
  56. * ngtcp2_get_uint16 reads 2 bytes from |p| as 16 bits unsigned
  57. * integer encoded as network byte order, and stores it in the buffer
  58. * pointed by |dest| as is. It returns |p| + 2.
  59. */
  60. const uint8_t *ngtcp2_get_uint16(uint16_t *dest, const uint8_t *p);
  61. /*
  62. * ngtcp2_get_uvarint reads variable-length unsigned integer from |p|,
  63. * and stores it in the buffer pointed by |dest| in host byte order.
  64. * It returns |p| plus the number of bytes read from |p|.
  65. */
  66. const uint8_t *ngtcp2_get_uvarint(uint64_t *dest, const uint8_t *p);
  67. /*
  68. * ngtcp2_get_varint reads variable-length unsigned integer from |p|,
  69. * and casts it to the signed integer, and stores it in the buffer
  70. * pointed by |dest| in host byte order. No information should be
  71. * lost in this cast, because the variable-length integer is 62
  72. * bits. It returns |p| plus the number of bytes read from |p|.
  73. */
  74. const uint8_t *ngtcp2_get_varint(int64_t *dest, const uint8_t *p);
  75. /*
  76. * ngtcp2_get_pkt_num reads encoded packet number from |p|. The
  77. * packet number is encoed in |pkt_numlen| bytes.
  78. */
  79. int64_t ngtcp2_get_pkt_num(const uint8_t *p, size_t pkt_numlen);
  80. /*
  81. * ngtcp2_put_uint64be writes |n| in host byte order in |p| in network
  82. * byte order. It returns the one beyond of the last written
  83. * position.
  84. */
  85. uint8_t *ngtcp2_put_uint64be(uint8_t *p, uint64_t n);
  86. /*
  87. * ngtcp2_put_uint32be writes |n| in host byte order in |p| in network
  88. * byte order. It returns the one beyond of the last written
  89. * position.
  90. */
  91. uint8_t *ngtcp2_put_uint32be(uint8_t *p, uint32_t n);
  92. /*
  93. * ngtcp2_put_uint24be writes |n| in host byte order in |p| in network
  94. * byte order. It writes only least significant 24 bits. It returns
  95. * the one beyond of the last written position.
  96. */
  97. uint8_t *ngtcp2_put_uint24be(uint8_t *p, uint32_t n);
  98. /*
  99. * ngtcp2_put_uint16be writes |n| in host byte order in |p| in network
  100. * byte order. It returns the one beyond of the last written
  101. * position.
  102. */
  103. uint8_t *ngtcp2_put_uint16be(uint8_t *p, uint16_t n);
  104. /*
  105. * ngtcp2_put_uint16 writes |n| as is in |p|. It returns the one
  106. * beyond of the last written position.
  107. */
  108. uint8_t *ngtcp2_put_uint16(uint8_t *p, uint16_t n);
  109. /*
  110. * ngtcp2_put_uvarint writes |n| in |p| using variable-length integer
  111. * encoding. It returns the one beyond of the last written position.
  112. */
  113. uint8_t *ngtcp2_put_uvarint(uint8_t *p, uint64_t n);
  114. /*
  115. * ngtcp2_put_uvarint30 writes |n| in |p| using variable-length
  116. * integer encoding. |n| must be strictly less than 1073741824. The
  117. * function always encodes |n| in 4 bytes. It returns the one beyond
  118. * of the last written position.
  119. */
  120. uint8_t *ngtcp2_put_uvarint30(uint8_t *p, uint32_t n);
  121. /*
  122. * ngtcp2_put_pkt_num encodes |pkt_num| using |len| bytes. It
  123. * returns the one beyond of the last written position.
  124. */
  125. uint8_t *ngtcp2_put_pkt_num(uint8_t *p, int64_t pkt_num, size_t len);
  126. /*
  127. * ngtcp2_get_uvarintlen returns the required number of bytes to read
  128. * variable-length integer starting at |p|.
  129. */
  130. size_t ngtcp2_get_uvarintlen(const uint8_t *p);
  131. /*
  132. * ngtcp2_put_uvarintlen returns the required number of bytes to
  133. * encode |n|.
  134. */
  135. size_t ngtcp2_put_uvarintlen(uint64_t n);
  136. /*
  137. * ngtcp2_ord_stream_id returns the ordinal number of |stream_id|.
  138. */
  139. uint64_t ngtcp2_ord_stream_id(int64_t stream_id);
  140. #endif /* !defined(NGTCP2_CONV_H) */