nghttp2_helper.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2012 Tatsuhiro Tsujikawa
  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 NGHTTP2_HELPER_H
  26. #define NGHTTP2_HELPER_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* HAVE_CONFIG_H */
  30. #include <string.h>
  31. #include <stddef.h>
  32. #include <nghttp2/nghttp2.h>
  33. #include "nghttp2_mem.h"
  34. #define nghttp2_min(A, B) ((A) < (B) ? (A) : (B))
  35. #define nghttp2_max(A, B) ((A) > (B) ? (A) : (B))
  36. #define lstreq(A, B, N) ((sizeof((A)) - 1) == (N) && memcmp((A), (B), (N)) == 0)
  37. #define nghttp2_struct_of(ptr, type, member) \
  38. ((type *)(void *)((char *)(ptr)-offsetof(type, member)))
  39. /*
  40. * Copies 2 byte unsigned integer |n| in host byte order to |buf| in
  41. * network byte order.
  42. */
  43. void nghttp2_put_uint16be(uint8_t *buf, uint16_t n);
  44. /*
  45. * Copies 4 byte unsigned integer |n| in host byte order to |buf| in
  46. * network byte order.
  47. */
  48. void nghttp2_put_uint32be(uint8_t *buf, uint32_t n);
  49. /*
  50. * Retrieves 2 byte unsigned integer stored in |data| in network byte
  51. * order and returns it in host byte order.
  52. */
  53. uint16_t nghttp2_get_uint16(const uint8_t *data);
  54. /*
  55. * Retrieves 4 byte unsigned integer stored in |data| in network byte
  56. * order and returns it in host byte order.
  57. */
  58. uint32_t nghttp2_get_uint32(const uint8_t *data);
  59. void nghttp2_downcase(uint8_t *s, size_t len);
  60. /*
  61. * Adjusts |*local_window_size_ptr|, |*recv_window_size_ptr|,
  62. * |*recv_reduction_ptr| with |*delta_ptr| which is the
  63. * WINDOW_UPDATE's window_size_increment sent from local side. If
  64. * |delta| is strictly larger than |*recv_window_size_ptr|,
  65. * |*local_window_size_ptr| is increased by delta -
  66. * *recv_window_size_ptr. If |delta| is negative,
  67. * |*local_window_size_ptr| is decreased by delta.
  68. *
  69. * This function returns 0 if it succeeds, or one of the following
  70. * negative error codes:
  71. *
  72. * NGHTTP2_ERR_FLOW_CONTROL
  73. * local_window_size overflow or gets negative.
  74. */
  75. int nghttp2_adjust_local_window_size(int32_t *local_window_size_ptr,
  76. int32_t *recv_window_size_ptr,
  77. int32_t *recv_reduction_ptr,
  78. int32_t *delta_ptr);
  79. /*
  80. * This function works like nghttp2_adjust_local_window_size(). The
  81. * difference is that this function assumes *delta_ptr >= 0, and
  82. * *recv_window_size_ptr is not decreased by *delta_ptr.
  83. *
  84. * This function returns 0 if it succeeds, or one of the following
  85. * negative error codes:
  86. *
  87. * NGHTTP2_ERR_FLOW_CONTROL
  88. * local_window_size overflow or gets negative.
  89. */
  90. int nghttp2_increase_local_window_size(int32_t *local_window_size_ptr,
  91. int32_t *recv_window_size_ptr,
  92. int32_t *recv_reduction_ptr,
  93. int32_t *delta_ptr);
  94. /*
  95. * Returns non-zero if the function decided that WINDOW_UPDATE should
  96. * be sent.
  97. */
  98. int nghttp2_should_send_window_update(int32_t local_window_size,
  99. int32_t recv_window_size);
  100. /*
  101. * Copies the buffer |src| of length |len| to the destination pointed
  102. * by the |dest|, assuming that the |dest| is at lest |len| bytes long
  103. * . Returns dest + len.
  104. */
  105. uint8_t *nghttp2_cpymem(uint8_t *dest, const void *src, size_t len);
  106. #endif /* NGHTTP2_HELPER_H */