nghttp2_helper.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_max_def(SUFFIX, T) \
  35. static inline T nghttp2_max_##SUFFIX(T a, T b) { return a < b ? b : a; }
  36. nghttp2_max_def(int8, int8_t)
  37. nghttp2_max_def(int16, int16_t)
  38. nghttp2_max_def(int32, int32_t)
  39. nghttp2_max_def(int64, int64_t)
  40. nghttp2_max_def(uint8, uint8_t)
  41. nghttp2_max_def(uint16, uint16_t)
  42. nghttp2_max_def(uint32, uint32_t)
  43. nghttp2_max_def(uint64, uint64_t)
  44. nghttp2_max_def(size, size_t)
  45. #define nghttp2_min_def(SUFFIX, T) \
  46. static inline T nghttp2_min_##SUFFIX(T a, T b) { return a < b ? a : b; }
  47. nghttp2_min_def(int8, int8_t)
  48. nghttp2_min_def(int16, int16_t)
  49. nghttp2_min_def(int32, int32_t)
  50. nghttp2_min_def(int64, int64_t)
  51. nghttp2_min_def(uint8, uint8_t)
  52. nghttp2_min_def(uint16, uint16_t)
  53. nghttp2_min_def(uint32, uint32_t)
  54. nghttp2_min_def(uint64, uint64_t)
  55. nghttp2_min_def(size, size_t)
  56. #define lstreq(A, B, N) ((sizeof((A)) - 1) == (N) && memcmp((A), (B), (N)) == 0)
  57. #define nghttp2_struct_of(ptr, type, member) \
  58. ((type *)(void *)((char *)(ptr) - offsetof(type, member)))
  59. /*
  60. * Copies 2 byte unsigned integer |n| in host byte order to |buf| in
  61. * network byte order.
  62. */
  63. void nghttp2_put_uint16be(uint8_t *buf, uint16_t n);
  64. /*
  65. * Copies 4 byte unsigned integer |n| in host byte order to |buf| in
  66. * network byte order.
  67. */
  68. void nghttp2_put_uint32be(uint8_t *buf, uint32_t n);
  69. /*
  70. * Retrieves 2 byte unsigned integer stored in |data| in network byte
  71. * order and returns it in host byte order.
  72. */
  73. uint16_t nghttp2_get_uint16(const uint8_t *data);
  74. /*
  75. * Retrieves 4 byte unsigned integer stored in |data| in network byte
  76. * order and returns it in host byte order.
  77. */
  78. uint32_t nghttp2_get_uint32(const uint8_t *data);
  79. void nghttp2_downcase(uint8_t *s, size_t len);
  80. /*
  81. * Adjusts |*local_window_size_ptr|, |*recv_window_size_ptr|,
  82. * |*recv_reduction_ptr| with |*delta_ptr| which is the
  83. * WINDOW_UPDATE's window_size_increment sent from local side. If
  84. * |delta| is strictly larger than |*recv_window_size_ptr|,
  85. * |*local_window_size_ptr| is increased by delta -
  86. * *recv_window_size_ptr. If |delta| is negative,
  87. * |*local_window_size_ptr| is decreased by delta.
  88. *
  89. * This function returns 0 if it succeeds, or one of the following
  90. * negative error codes:
  91. *
  92. * NGHTTP2_ERR_FLOW_CONTROL
  93. * local_window_size overflow or gets negative.
  94. */
  95. int nghttp2_adjust_local_window_size(int32_t *local_window_size_ptr,
  96. int32_t *recv_window_size_ptr,
  97. int32_t *recv_reduction_ptr,
  98. int32_t *delta_ptr);
  99. /*
  100. * This function works like nghttp2_adjust_local_window_size(). The
  101. * difference is that this function assumes *delta_ptr >= 0, and
  102. * *recv_window_size_ptr is not decreased by *delta_ptr.
  103. *
  104. * This function returns 0 if it succeeds, or one of the following
  105. * negative error codes:
  106. *
  107. * NGHTTP2_ERR_FLOW_CONTROL
  108. * local_window_size overflow or gets negative.
  109. */
  110. int nghttp2_increase_local_window_size(int32_t *local_window_size_ptr,
  111. int32_t *recv_window_size_ptr,
  112. int32_t *recv_reduction_ptr,
  113. int32_t *delta_ptr);
  114. /*
  115. * Returns non-zero if the function decided that WINDOW_UPDATE should
  116. * be sent.
  117. */
  118. int nghttp2_should_send_window_update(int32_t local_window_size,
  119. int32_t recv_window_size);
  120. /*
  121. * Copies the buffer |src| of length |len| to the destination pointed
  122. * by the |dest|, assuming that the |dest| is at lest |len| bytes long
  123. * . Returns dest + len.
  124. */
  125. uint8_t *nghttp2_cpymem(uint8_t *dest, const void *src, size_t len);
  126. #endif /* NGHTTP2_HELPER_H */