nghttp3_buf.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_buf.h"
  27. void nghttp3_buf_init(nghttp3_buf *buf) {
  28. buf->begin = buf->end = buf->pos = buf->last = NULL;
  29. }
  30. void nghttp3_buf_wrap_init(nghttp3_buf *buf, uint8_t *src, size_t len) {
  31. buf->begin = buf->pos = buf->last = src;
  32. buf->end = buf->begin + len;
  33. }
  34. void nghttp3_buf_free(nghttp3_buf *buf, const nghttp3_mem *mem) {
  35. nghttp3_mem_free(mem, buf->begin);
  36. }
  37. size_t nghttp3_buf_left(const nghttp3_buf *buf) {
  38. return (size_t)(buf->end - buf->last);
  39. }
  40. size_t nghttp3_buf_len(const nghttp3_buf *buf) {
  41. return (size_t)(buf->last - buf->pos);
  42. }
  43. size_t nghttp3_buf_cap(const nghttp3_buf *buf) {
  44. return (size_t)(buf->end - buf->begin);
  45. }
  46. void nghttp3_buf_reset(nghttp3_buf *buf) { buf->pos = buf->last = buf->begin; }
  47. int nghttp3_buf_reserve(nghttp3_buf *buf, size_t size, const nghttp3_mem *mem) {
  48. uint8_t *p;
  49. nghttp3_ssize pos_offset, last_offset;
  50. if ((size_t)(buf->end - buf->begin) >= size) {
  51. return 0;
  52. }
  53. pos_offset = buf->pos - buf->begin;
  54. last_offset = buf->last - buf->begin;
  55. p = nghttp3_mem_realloc(mem, buf->begin, size);
  56. if (p == NULL) {
  57. return NGHTTP3_ERR_NOMEM;
  58. }
  59. buf->begin = p;
  60. buf->end = p + size;
  61. buf->pos = p + pos_offset;
  62. buf->last = p + last_offset;
  63. return 0;
  64. }
  65. void nghttp3_buf_swap(nghttp3_buf *a, nghttp3_buf *b) {
  66. nghttp3_buf c = *a;
  67. *a = *b;
  68. *b = c;
  69. }
  70. void nghttp3_typed_buf_init(nghttp3_typed_buf *tbuf, const nghttp3_buf *buf,
  71. nghttp3_buf_type type) {
  72. tbuf->buf = *buf;
  73. tbuf->type = type;
  74. }