ngtcp2_vec.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2018 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_VEC_H
  26. #define NGTCP2_VEC_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <ngtcp2/ngtcp2.h>
  31. #include "ngtcp2_mem.h"
  32. /*
  33. * ngtcp2_vec_lit is a convenient macro to fill the object pointed by
  34. * |DEST| with the literal string |LIT|.
  35. */
  36. #define ngtcp2_vec_lit(DEST, LIT) \
  37. ((DEST)->base = (uint8_t *)(LIT), (DEST)->len = sizeof(LIT) - 1, (DEST))
  38. /*
  39. * ngtcp2_vec_init initializes |vec| with the given parameters. It
  40. * returns |vec|.
  41. */
  42. ngtcp2_vec *ngtcp2_vec_init(ngtcp2_vec *vec, const uint8_t *base, size_t len);
  43. /*
  44. * ngtcp2_vec_new allocates and initializes |*pvec| with given |data|
  45. * of length |datalen|. This function allocates memory for |*pvec|
  46. * and the given data with a single allocation, and the contents
  47. * pointed by |data| is copied into the allocated memory space. To
  48. * free the allocated memory, call ngtcp2_vec_del.
  49. */
  50. int ngtcp2_vec_new(ngtcp2_vec **pvec, const uint8_t *data, size_t datalen,
  51. const ngtcp2_mem *mem);
  52. /*
  53. * ngtcp2_vec_del frees the memory allocated by |vec| which is
  54. * allocated and initialized by ngtcp2_vec_new.
  55. */
  56. void ngtcp2_vec_del(ngtcp2_vec *vec, const ngtcp2_mem *mem);
  57. /*
  58. * ngtcp2_vec_len returns the sum of length in |vec| of |n| elements.
  59. */
  60. uint64_t ngtcp2_vec_len(const ngtcp2_vec *vec, size_t n);
  61. /*
  62. * ngtcp2_vec_len_varint is similar to ngtcp2_vec_len, but it returns
  63. * -1 if the sum of the length exceeds NGTCP2_MAX_VARINT.
  64. */
  65. int64_t ngtcp2_vec_len_varint(const ngtcp2_vec *vec, size_t n);
  66. /*
  67. * ngtcp2_vec_split splits |src| at the data position where
  68. * ngtcp2_vec_len(|src|) does not exceed |left| bytes. The removed
  69. * vectors are moved to |dst|. The existing elements in |dst| are
  70. * moved forward to make up a space. The |maxcnt| is the maximum
  71. * number of elements which |dst| array can contain. The caller must
  72. * set |*psrccnt| to the number of elements of |src|. Similarly, the
  73. * caller must set |*pdstcnt| to the number of elements of |dst|. The
  74. * split does not necessarily occur at the boundary of ngtcp2_vec
  75. * object. After split has done, this function updates |*psrccnt| and
  76. * |*pdstcnt|. This function returns the number of bytes moved from
  77. * |src| to |dst|. If split cannot be made because doing so exceeds
  78. * |maxcnt|, this function returns -1.
  79. */
  80. ngtcp2_ssize ngtcp2_vec_split(ngtcp2_vec *dst, size_t *pdstcnt, ngtcp2_vec *src,
  81. size_t *psrccnt, size_t left, size_t maxcnt);
  82. /*
  83. * ngtcp2_vec_merge merges |src| into |dst| by moving at most |left|
  84. * bytes from |src|. The |maxcnt| is the maximum number of elements
  85. * which |dst| array can contain. The caller must set |*pdstcnt| to
  86. * the number of elements of |dst|. Similarly, the caller must set
  87. * |*psrccnt| to the number of elements of |src|. After merge has
  88. * done, this function updates |*psrccnt| and |*pdstcnt|. This
  89. * function returns the number of bytes moved from |src| to |dst|.
  90. */
  91. size_t ngtcp2_vec_merge(ngtcp2_vec *dst, size_t *pdstcnt, ngtcp2_vec *src,
  92. size_t *psrccnt, size_t left, size_t maxcnt);
  93. /*
  94. * ngtcp2_vec_copy_at_most copies |src| of length |srccnt| to |dst| of
  95. * length |dstcnt|. The total number of bytes which the copied
  96. * ngtcp2_vec refers to is at most |left|. The empty elements in
  97. * |src| are ignored. This function returns the number of elements
  98. * copied.
  99. */
  100. size_t ngtcp2_vec_copy_at_most(ngtcp2_vec *dst, size_t dstcnt,
  101. const ngtcp2_vec *src, size_t srccnt,
  102. size_t left);
  103. /*
  104. * ngtcp2_vec_copy copies |src| of length |cnt| to |dst|. |dst| must
  105. * have sufficient capacity.
  106. */
  107. void ngtcp2_vec_copy(ngtcp2_vec *dst, const ngtcp2_vec *src, size_t cnt);
  108. #endif /* !defined(NGTCP2_VEC_H) */