nghttp2_buf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2014 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. #include "nghttp2_buf.h"
  26. #include <stdio.h>
  27. #include "nghttp2_helper.h"
  28. #include "nghttp2_debug.h"
  29. void nghttp2_buf_init(nghttp2_buf *buf) {
  30. buf->begin = NULL;
  31. buf->end = NULL;
  32. buf->pos = NULL;
  33. buf->last = NULL;
  34. buf->mark = NULL;
  35. }
  36. int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial, nghttp2_mem *mem) {
  37. nghttp2_buf_init(buf);
  38. return nghttp2_buf_reserve(buf, initial, mem);
  39. }
  40. void nghttp2_buf_free(nghttp2_buf *buf, nghttp2_mem *mem) {
  41. if (buf == NULL) {
  42. return;
  43. }
  44. nghttp2_mem_free(mem, buf->begin);
  45. buf->begin = NULL;
  46. }
  47. int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap, nghttp2_mem *mem) {
  48. uint8_t *ptr;
  49. size_t cap;
  50. cap = nghttp2_buf_cap(buf);
  51. if (cap >= new_cap) {
  52. return 0;
  53. }
  54. new_cap = nghttp2_max(new_cap, cap * 2);
  55. ptr = nghttp2_mem_realloc(mem, buf->begin, new_cap);
  56. if (ptr == NULL) {
  57. return NGHTTP2_ERR_NOMEM;
  58. }
  59. buf->pos = ptr + (buf->pos - buf->begin);
  60. buf->last = ptr + (buf->last - buf->begin);
  61. buf->mark = ptr + (buf->mark - buf->begin);
  62. buf->begin = ptr;
  63. buf->end = ptr + new_cap;
  64. return 0;
  65. }
  66. void nghttp2_buf_reset(nghttp2_buf *buf) {
  67. buf->pos = buf->last = buf->mark = buf->begin;
  68. }
  69. void nghttp2_buf_wrap_init(nghttp2_buf *buf, uint8_t *begin, size_t len) {
  70. buf->begin = buf->pos = buf->last = buf->mark = begin;
  71. buf->end = begin + len;
  72. }
  73. static int buf_chain_new(nghttp2_buf_chain **chain, size_t chunk_length,
  74. nghttp2_mem *mem) {
  75. int rv;
  76. *chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
  77. if (*chain == NULL) {
  78. return NGHTTP2_ERR_NOMEM;
  79. }
  80. (*chain)->next = NULL;
  81. rv = nghttp2_buf_init2(&(*chain)->buf, chunk_length, mem);
  82. if (rv != 0) {
  83. nghttp2_mem_free(mem, *chain);
  84. return NGHTTP2_ERR_NOMEM;
  85. }
  86. return 0;
  87. }
  88. static void buf_chain_del(nghttp2_buf_chain *chain, nghttp2_mem *mem) {
  89. nghttp2_buf_free(&chain->buf, mem);
  90. nghttp2_mem_free(mem, chain);
  91. }
  92. int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk,
  93. nghttp2_mem *mem) {
  94. return nghttp2_bufs_init2(bufs, chunk_length, max_chunk, 0, mem);
  95. }
  96. int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length,
  97. size_t max_chunk, size_t offset, nghttp2_mem *mem) {
  98. return nghttp2_bufs_init3(bufs, chunk_length, max_chunk, max_chunk, offset,
  99. mem);
  100. }
  101. int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length,
  102. size_t max_chunk, size_t chunk_keep, size_t offset,
  103. nghttp2_mem *mem) {
  104. int rv;
  105. nghttp2_buf_chain *chain;
  106. if (chunk_keep == 0 || max_chunk < chunk_keep || chunk_length < offset) {
  107. return NGHTTP2_ERR_INVALID_ARGUMENT;
  108. }
  109. rv = buf_chain_new(&chain, chunk_length, mem);
  110. if (rv != 0) {
  111. return rv;
  112. }
  113. bufs->mem = mem;
  114. bufs->offset = offset;
  115. bufs->head = chain;
  116. bufs->cur = bufs->head;
  117. nghttp2_buf_shift_right(&bufs->cur->buf, offset);
  118. bufs->chunk_length = chunk_length;
  119. bufs->chunk_used = 1;
  120. bufs->max_chunk = max_chunk;
  121. bufs->chunk_keep = chunk_keep;
  122. return 0;
  123. }
  124. int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length) {
  125. int rv;
  126. nghttp2_buf_chain *chain;
  127. if (chunk_length < bufs->offset) {
  128. return NGHTTP2_ERR_INVALID_ARGUMENT;
  129. }
  130. rv = buf_chain_new(&chain, chunk_length, bufs->mem);
  131. if (rv != 0) {
  132. return rv;
  133. }
  134. nghttp2_bufs_free(bufs);
  135. bufs->head = chain;
  136. bufs->cur = bufs->head;
  137. nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
  138. bufs->chunk_length = chunk_length;
  139. bufs->chunk_used = 1;
  140. return 0;
  141. }
  142. void nghttp2_bufs_free(nghttp2_bufs *bufs) {
  143. nghttp2_buf_chain *chain, *next_chain;
  144. if (bufs == NULL) {
  145. return;
  146. }
  147. for (chain = bufs->head; chain;) {
  148. next_chain = chain->next;
  149. buf_chain_del(chain, bufs->mem);
  150. chain = next_chain;
  151. }
  152. bufs->head = NULL;
  153. }
  154. int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len,
  155. nghttp2_mem *mem) {
  156. nghttp2_buf_chain *chain;
  157. chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
  158. if (chain == NULL) {
  159. return NGHTTP2_ERR_NOMEM;
  160. }
  161. chain->next = NULL;
  162. nghttp2_buf_wrap_init(&chain->buf, begin, len);
  163. bufs->mem = mem;
  164. bufs->offset = 0;
  165. bufs->head = chain;
  166. bufs->cur = bufs->head;
  167. bufs->chunk_length = len;
  168. bufs->chunk_used = 1;
  169. bufs->max_chunk = 1;
  170. bufs->chunk_keep = 1;
  171. return 0;
  172. }
  173. int nghttp2_bufs_wrap_init2(nghttp2_bufs *bufs, const nghttp2_vec *vec,
  174. size_t veclen, nghttp2_mem *mem) {
  175. size_t i = 0;
  176. nghttp2_buf_chain *cur_chain;
  177. nghttp2_buf_chain *head_chain;
  178. nghttp2_buf_chain **dst_chain = &head_chain;
  179. if (veclen == 0) {
  180. return nghttp2_bufs_wrap_init(bufs, NULL, 0, mem);
  181. }
  182. head_chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain) * veclen);
  183. if (head_chain == NULL) {
  184. return NGHTTP2_ERR_NOMEM;
  185. }
  186. for (i = 0; i < veclen; ++i) {
  187. cur_chain = &head_chain[i];
  188. cur_chain->next = NULL;
  189. nghttp2_buf_wrap_init(&cur_chain->buf, vec[i].base, vec[i].len);
  190. *dst_chain = cur_chain;
  191. dst_chain = &cur_chain->next;
  192. }
  193. bufs->mem = mem;
  194. bufs->offset = 0;
  195. bufs->head = head_chain;
  196. bufs->cur = bufs->head;
  197. /* We don't use chunk_length since no allocation is expected. */
  198. bufs->chunk_length = 0;
  199. bufs->chunk_used = veclen;
  200. bufs->max_chunk = veclen;
  201. bufs->chunk_keep = veclen;
  202. return 0;
  203. }
  204. void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs) {
  205. if (bufs == NULL) {
  206. return;
  207. }
  208. if (bufs->head) {
  209. nghttp2_mem_free(bufs->mem, bufs->head);
  210. }
  211. }
  212. void nghttp2_bufs_seek_last_present(nghttp2_bufs *bufs) {
  213. nghttp2_buf_chain *ci;
  214. for (ci = bufs->cur; ci; ci = ci->next) {
  215. if (nghttp2_buf_len(&ci->buf) == 0) {
  216. return;
  217. } else {
  218. bufs->cur = ci;
  219. }
  220. }
  221. }
  222. size_t nghttp2_bufs_len(nghttp2_bufs *bufs) {
  223. nghttp2_buf_chain *ci;
  224. size_t len;
  225. len = 0;
  226. for (ci = bufs->head; ci; ci = ci->next) {
  227. len += nghttp2_buf_len(&ci->buf);
  228. }
  229. return len;
  230. }
  231. static int bufs_alloc_chain(nghttp2_bufs *bufs) {
  232. int rv;
  233. nghttp2_buf_chain *chain;
  234. if (bufs->cur->next) {
  235. bufs->cur = bufs->cur->next;
  236. return 0;
  237. }
  238. if (bufs->max_chunk == bufs->chunk_used) {
  239. return NGHTTP2_ERR_BUFFER_ERROR;
  240. }
  241. rv = buf_chain_new(&chain, bufs->chunk_length, bufs->mem);
  242. if (rv != 0) {
  243. return rv;
  244. }
  245. DEBUGF("new buffer %zu bytes allocated for bufs %p, used %zu\n",
  246. bufs->chunk_length, bufs, bufs->chunk_used);
  247. ++bufs->chunk_used;
  248. bufs->cur->next = chain;
  249. bufs->cur = chain;
  250. nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
  251. return 0;
  252. }
  253. int nghttp2_bufs_add(nghttp2_bufs *bufs, const void *data, size_t len) {
  254. int rv;
  255. size_t nwrite;
  256. nghttp2_buf *buf;
  257. const uint8_t *p;
  258. p = data;
  259. while (len) {
  260. buf = &bufs->cur->buf;
  261. nwrite = nghttp2_min(nghttp2_buf_avail(buf), len);
  262. if (nwrite == 0) {
  263. rv = bufs_alloc_chain(bufs);
  264. if (rv != 0) {
  265. return rv;
  266. }
  267. continue;
  268. }
  269. buf->last = nghttp2_cpymem(buf->last, p, nwrite);
  270. p += nwrite;
  271. len -= nwrite;
  272. }
  273. return 0;
  274. }
  275. static int bufs_ensure_addb(nghttp2_bufs *bufs) {
  276. int rv;
  277. nghttp2_buf *buf;
  278. buf = &bufs->cur->buf;
  279. if (nghttp2_buf_avail(buf) > 0) {
  280. return 0;
  281. }
  282. rv = bufs_alloc_chain(bufs);
  283. if (rv != 0) {
  284. return rv;
  285. }
  286. return 0;
  287. }
  288. int nghttp2_bufs_addb(nghttp2_bufs *bufs, uint8_t b) {
  289. int rv;
  290. rv = bufs_ensure_addb(bufs);
  291. if (rv != 0) {
  292. return rv;
  293. }
  294. *bufs->cur->buf.last++ = b;
  295. return 0;
  296. }
  297. int nghttp2_bufs_addb_hold(nghttp2_bufs *bufs, uint8_t b) {
  298. int rv;
  299. rv = bufs_ensure_addb(bufs);
  300. if (rv != 0) {
  301. return rv;
  302. }
  303. *bufs->cur->buf.last = b;
  304. return 0;
  305. }
  306. int nghttp2_bufs_orb(nghttp2_bufs *bufs, uint8_t b) {
  307. int rv;
  308. rv = bufs_ensure_addb(bufs);
  309. if (rv != 0) {
  310. return rv;
  311. }
  312. *bufs->cur->buf.last++ |= b;
  313. return 0;
  314. }
  315. int nghttp2_bufs_orb_hold(nghttp2_bufs *bufs, uint8_t b) {
  316. int rv;
  317. rv = bufs_ensure_addb(bufs);
  318. if (rv != 0) {
  319. return rv;
  320. }
  321. *bufs->cur->buf.last |= b;
  322. return 0;
  323. }
  324. ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out) {
  325. size_t len;
  326. nghttp2_buf_chain *chain;
  327. nghttp2_buf *buf;
  328. uint8_t *res;
  329. nghttp2_buf resbuf;
  330. len = 0;
  331. for (chain = bufs->head; chain; chain = chain->next) {
  332. len += nghttp2_buf_len(&chain->buf);
  333. }
  334. if (len == 0) {
  335. res = NULL;
  336. return 0;
  337. }
  338. res = nghttp2_mem_malloc(bufs->mem, len);
  339. if (res == NULL) {
  340. return NGHTTP2_ERR_NOMEM;
  341. }
  342. nghttp2_buf_wrap_init(&resbuf, res, len);
  343. for (chain = bufs->head; chain; chain = chain->next) {
  344. buf = &chain->buf;
  345. resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf));
  346. }
  347. *out = res;
  348. return (ssize_t)len;
  349. }
  350. size_t nghttp2_bufs_remove_copy(nghttp2_bufs *bufs, uint8_t *out) {
  351. size_t len;
  352. nghttp2_buf_chain *chain;
  353. nghttp2_buf *buf;
  354. nghttp2_buf resbuf;
  355. len = nghttp2_bufs_len(bufs);
  356. nghttp2_buf_wrap_init(&resbuf, out, len);
  357. for (chain = bufs->head; chain; chain = chain->next) {
  358. buf = &chain->buf;
  359. resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf));
  360. }
  361. return len;
  362. }
  363. void nghttp2_bufs_reset(nghttp2_bufs *bufs) {
  364. nghttp2_buf_chain *chain, *ci;
  365. size_t k;
  366. k = bufs->chunk_keep;
  367. for (ci = bufs->head; ci; ci = ci->next) {
  368. nghttp2_buf_reset(&ci->buf);
  369. nghttp2_buf_shift_right(&ci->buf, bufs->offset);
  370. if (--k == 0) {
  371. break;
  372. }
  373. }
  374. if (ci) {
  375. chain = ci->next;
  376. ci->next = NULL;
  377. for (ci = chain; ci;) {
  378. chain = ci->next;
  379. buf_chain_del(ci, bufs->mem);
  380. ci = chain;
  381. }
  382. bufs->chunk_used = bufs->chunk_keep;
  383. }
  384. bufs->cur = bufs->head;
  385. }
  386. int nghttp2_bufs_advance(nghttp2_bufs *bufs) { return bufs_alloc_chain(bufs); }
  387. int nghttp2_bufs_next_present(nghttp2_bufs *bufs) {
  388. nghttp2_buf_chain *chain;
  389. chain = bufs->cur->next;
  390. return chain && nghttp2_buf_len(&chain->buf);
  391. }