nghttp2_buf.c 11 KB

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