ngtcp2_str.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_str.h"
  26. #include <string.h>
  27. #include "ngtcp2_macro.h"
  28. void *ngtcp2_cpymem(void *dest, const void *src, size_t n) {
  29. memcpy(dest, src, n);
  30. return (uint8_t *)dest + n;
  31. }
  32. uint8_t *ngtcp2_setmem(uint8_t *dest, uint8_t b, size_t n) {
  33. memset(dest, b, n);
  34. return dest + n;
  35. }
  36. const void *ngtcp2_get_bytes(void *dest, const void *src, size_t n) {
  37. memcpy(dest, src, n);
  38. return (uint8_t *)src + n;
  39. }
  40. #define LOWER_XDIGITS "0123456789abcdef"
  41. uint8_t *ngtcp2_encode_hex(uint8_t *dest, const uint8_t *data, size_t len) {
  42. size_t i;
  43. uint8_t *p = dest;
  44. for (i = 0; i < len; ++i) {
  45. *p++ = (uint8_t)LOWER_XDIGITS[data[i] >> 4];
  46. *p++ = (uint8_t)LOWER_XDIGITS[data[i] & 0xf];
  47. }
  48. *p = '\0';
  49. return dest;
  50. }
  51. char *ngtcp2_encode_printable_ascii(char *dest, const uint8_t *data,
  52. size_t len) {
  53. size_t i;
  54. char *p = dest;
  55. uint8_t c;
  56. for (i = 0; i < len; ++i) {
  57. c = data[i];
  58. if (0x20 <= c && c <= 0x7e) {
  59. *p++ = (char)c;
  60. } else {
  61. *p++ = '.';
  62. }
  63. }
  64. *p = '\0';
  65. return dest;
  66. }
  67. /*
  68. * write_uint writes |n| to the buffer pointed by |p| in decimal
  69. * representation. It returns |p| plus the number of bytes written.
  70. * The function assumes that the buffer has enough capacity to contain
  71. * a string.
  72. */
  73. static uint8_t *write_uint(uint8_t *p, uint64_t n) {
  74. size_t nlen = 0;
  75. uint64_t t;
  76. uint8_t *res;
  77. if (n == 0) {
  78. *p++ = '0';
  79. return p;
  80. }
  81. for (t = n; t; t /= 10, ++nlen)
  82. ;
  83. p += nlen;
  84. res = p;
  85. for (; n; n /= 10) {
  86. *--p = (uint8_t)((n % 10) + '0');
  87. }
  88. return res;
  89. }
  90. uint8_t *ngtcp2_encode_ipv4(uint8_t *dest, const uint8_t *addr) {
  91. size_t i;
  92. uint8_t *p = dest;
  93. p = write_uint(p, addr[0]);
  94. for (i = 1; i < 4; ++i) {
  95. *p++ = '.';
  96. p = write_uint(p, addr[i]);
  97. }
  98. *p = '\0';
  99. return dest;
  100. }
  101. /*
  102. * write_hex_zsup writes the content of buffer pointed by |data| of
  103. * length |len| to |dest| in hex string. Any leading zeros are
  104. * suppressed. It returns |dest| plus the number of bytes written.
  105. */
  106. static uint8_t *write_hex_zsup(uint8_t *dest, const uint8_t *data, size_t len) {
  107. size_t i;
  108. uint8_t *p = dest;
  109. uint8_t d;
  110. for (i = 0; i < len; ++i) {
  111. d = data[i];
  112. if (d >> 4) {
  113. break;
  114. }
  115. d &= 0xf;
  116. if (d) {
  117. *p++ = (uint8_t)LOWER_XDIGITS[d];
  118. ++i;
  119. break;
  120. }
  121. }
  122. if (p == dest && i == len) {
  123. *p++ = '0';
  124. return p;
  125. }
  126. for (; i < len; ++i) {
  127. d = data[i];
  128. *p++ = (uint8_t)LOWER_XDIGITS[d >> 4];
  129. *p++ = (uint8_t)LOWER_XDIGITS[d & 0xf];
  130. }
  131. return p;
  132. }
  133. uint8_t *ngtcp2_encode_ipv6(uint8_t *dest, const uint8_t *addr) {
  134. uint16_t blks[8];
  135. size_t i;
  136. size_t zlen, zoff;
  137. size_t max_zlen = 0, max_zoff = 8;
  138. uint8_t *p = dest;
  139. for (i = 0; i < 16; i += sizeof(uint16_t)) {
  140. /* Copy in network byte order. */
  141. memcpy(&blks[i / sizeof(uint16_t)], addr + i, sizeof(uint16_t));
  142. }
  143. for (i = 0; i < 8;) {
  144. if (blks[i]) {
  145. ++i;
  146. continue;
  147. }
  148. zlen = 1;
  149. zoff = i;
  150. ++i;
  151. for (; i < 8 && blks[i] == 0; ++i, ++zlen)
  152. ;
  153. if (zlen > max_zlen) {
  154. max_zlen = zlen;
  155. max_zoff = zoff;
  156. }
  157. }
  158. /* Do not suppress a single '0' block */
  159. if (max_zlen == 1) {
  160. max_zoff = 8;
  161. }
  162. if (max_zoff != 0) {
  163. p = write_hex_zsup(p, (const uint8_t *)blks, sizeof(uint16_t));
  164. for (i = 1; i < max_zoff; ++i) {
  165. *p++ = ':';
  166. p = write_hex_zsup(p, (const uint8_t *)(blks + i), sizeof(uint16_t));
  167. }
  168. }
  169. if (max_zoff != 8) {
  170. *p++ = ':';
  171. if (max_zoff + max_zlen == 8) {
  172. *p++ = ':';
  173. } else {
  174. for (i = max_zoff + max_zlen; i < 8; ++i) {
  175. *p++ = ':';
  176. p = write_hex_zsup(p, (const uint8_t *)(blks + i), sizeof(uint16_t));
  177. }
  178. }
  179. }
  180. *p = '\0';
  181. return dest;
  182. }
  183. int ngtcp2_cmemeq(const uint8_t *a, const uint8_t *b, size_t n) {
  184. size_t i;
  185. int rv = 0;
  186. for (i = 0; i < n; ++i) {
  187. rv |= a[i] ^ b[i];
  188. }
  189. return rv == 0;
  190. }