nghttp2_buf.c 11 KB

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