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 
27 #include <stdio.h>
28 
29 #include "nghttp2_helper.h"
30 #include "nghttp2_debug.h"
31 
nghttp2_buf_init(nghttp2_buf * buf)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 
nghttp2_buf_init2(nghttp2_buf * buf,size_t initial,nghttp2_mem * mem)40 int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial, nghttp2_mem *mem) {
41   nghttp2_buf_init(buf);
42   return nghttp2_buf_reserve(buf, initial, mem);
43 }
44 
nghttp2_buf_free(nghttp2_buf * buf,nghttp2_mem * mem)45 void nghttp2_buf_free(nghttp2_buf *buf, nghttp2_mem *mem) {
46   if (buf == NULL) {
47     return;
48   }
49 
50   nghttp2_mem_free(mem, buf->begin);
51   buf->begin = NULL;
52 }
53 
nghttp2_buf_reserve(nghttp2_buf * buf,size_t new_cap,nghttp2_mem * mem)54 int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap, nghttp2_mem *mem) {
55   uint8_t *ptr;
56   size_t cap;
57 
58   cap = nghttp2_buf_cap(buf);
59 
60   if (cap >= new_cap) {
61     return 0;
62   }
63 
64   new_cap = nghttp2_max(new_cap, cap * 2);
65 
66   ptr = nghttp2_mem_realloc(mem, buf->begin, new_cap);
67   if (ptr == NULL) {
68     return NGHTTP2_ERR_NOMEM;
69   }
70 
71   buf->pos = ptr + (buf->pos - buf->begin);
72   buf->last = ptr + (buf->last - buf->begin);
73   buf->mark = ptr + (buf->mark - buf->begin);
74   buf->begin = ptr;
75   buf->end = ptr + new_cap;
76 
77   return 0;
78 }
79 
nghttp2_buf_reset(nghttp2_buf * buf)80 void nghttp2_buf_reset(nghttp2_buf *buf) {
81   buf->pos = buf->last = buf->mark = buf->begin;
82 }
83 
nghttp2_buf_wrap_init(nghttp2_buf * buf,uint8_t * begin,size_t len)84 void nghttp2_buf_wrap_init(nghttp2_buf *buf, uint8_t *begin, size_t len) {
85   buf->begin = buf->pos = buf->last = buf->mark = begin;
86   buf->end = begin + len;
87 }
88 
buf_chain_new(nghttp2_buf_chain ** chain,size_t chunk_length,nghttp2_mem * mem)89 static int buf_chain_new(nghttp2_buf_chain **chain, size_t chunk_length,
90                          nghttp2_mem *mem) {
91   int rv;
92 
93   *chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
94   if (*chain == NULL) {
95     return NGHTTP2_ERR_NOMEM;
96   }
97 
98   (*chain)->next = NULL;
99 
100   rv = nghttp2_buf_init2(&(*chain)->buf, chunk_length, mem);
101   if (rv != 0) {
102     nghttp2_mem_free(mem, *chain);
103     return NGHTTP2_ERR_NOMEM;
104   }
105 
106   return 0;
107 }
108 
buf_chain_del(nghttp2_buf_chain * chain,nghttp2_mem * mem)109 static void buf_chain_del(nghttp2_buf_chain *chain, nghttp2_mem *mem) {
110   nghttp2_buf_free(&chain->buf, mem);
111   nghttp2_mem_free(mem, chain);
112 }
113 
nghttp2_bufs_init(nghttp2_bufs * bufs,size_t chunk_length,size_t max_chunk,nghttp2_mem * mem)114 int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk,
115                       nghttp2_mem *mem) {
116   return nghttp2_bufs_init2(bufs, chunk_length, max_chunk, 0, mem);
117 }
118 
nghttp2_bufs_init2(nghttp2_bufs * bufs,size_t chunk_length,size_t max_chunk,size_t offset,nghttp2_mem * mem)119 int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length,
120                        size_t max_chunk, size_t offset, nghttp2_mem *mem) {
121   return nghttp2_bufs_init3(bufs, chunk_length, max_chunk, max_chunk, offset,
122                             mem);
123 }
124 
nghttp2_bufs_init3(nghttp2_bufs * bufs,size_t chunk_length,size_t max_chunk,size_t chunk_keep,size_t offset,nghttp2_mem * mem)125 int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length,
126                        size_t max_chunk, size_t chunk_keep, size_t offset,
127                        nghttp2_mem *mem) {
128   int rv;
129   nghttp2_buf_chain *chain;
130 
131   if (chunk_keep == 0 || max_chunk < chunk_keep || chunk_length < offset) {
132     return NGHTTP2_ERR_INVALID_ARGUMENT;
133   }
134 
135   rv = buf_chain_new(&chain, chunk_length, mem);
136   if (rv != 0) {
137     return rv;
138   }
139 
140   bufs->mem = mem;
141   bufs->offset = offset;
142 
143   bufs->head = chain;
144   bufs->cur = bufs->head;
145 
146   nghttp2_buf_shift_right(&bufs->cur->buf, offset);
147 
148   bufs->chunk_length = chunk_length;
149   bufs->chunk_used = 1;
150   bufs->max_chunk = max_chunk;
151   bufs->chunk_keep = chunk_keep;
152 
153   return 0;
154 }
155 
nghttp2_bufs_realloc(nghttp2_bufs * bufs,size_t chunk_length)156 int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length) {
157   int rv;
158   nghttp2_buf_chain *chain;
159 
160   if (chunk_length < bufs->offset) {
161     return NGHTTP2_ERR_INVALID_ARGUMENT;
162   }
163 
164   rv = buf_chain_new(&chain, chunk_length, bufs->mem);
165   if (rv != 0) {
166     return rv;
167   }
168 
169   nghttp2_bufs_free(bufs);
170 
171   bufs->head = chain;
172   bufs->cur = bufs->head;
173 
174   nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
175 
176   bufs->chunk_length = chunk_length;
177   bufs->chunk_used = 1;
178 
179   return 0;
180 }
181 
nghttp2_bufs_free(nghttp2_bufs * bufs)182 void nghttp2_bufs_free(nghttp2_bufs *bufs) {
183   nghttp2_buf_chain *chain, *next_chain;
184 
185   if (bufs == NULL) {
186     return;
187   }
188 
189   for (chain = bufs->head; chain;) {
190     next_chain = chain->next;
191 
192     buf_chain_del(chain, bufs->mem);
193 
194     chain = next_chain;
195   }
196 
197   bufs->head = NULL;
198 }
199 
nghttp2_bufs_wrap_init(nghttp2_bufs * bufs,uint8_t * begin,size_t len,nghttp2_mem * mem)200 int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len,
201                            nghttp2_mem *mem) {
202   nghttp2_buf_chain *chain;
203 
204   chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain));
205   if (chain == NULL) {
206     return NGHTTP2_ERR_NOMEM;
207   }
208 
209   chain->next = NULL;
210 
211   nghttp2_buf_wrap_init(&chain->buf, begin, len);
212 
213   bufs->mem = mem;
214   bufs->offset = 0;
215 
216   bufs->head = chain;
217   bufs->cur = bufs->head;
218 
219   bufs->chunk_length = len;
220   bufs->chunk_used = 1;
221   bufs->max_chunk = 1;
222   bufs->chunk_keep = 1;
223 
224   return 0;
225 }
226 
nghttp2_bufs_wrap_init2(nghttp2_bufs * bufs,const nghttp2_vec * vec,size_t veclen,nghttp2_mem * mem)227 int nghttp2_bufs_wrap_init2(nghttp2_bufs *bufs, const nghttp2_vec *vec,
228                             size_t veclen, nghttp2_mem *mem) {
229   size_t i = 0;
230   nghttp2_buf_chain *cur_chain;
231   nghttp2_buf_chain *head_chain;
232   nghttp2_buf_chain **dst_chain = &head_chain;
233 
234   if (veclen == 0) {
235     return nghttp2_bufs_wrap_init(bufs, NULL, 0, mem);
236   }
237 
238   head_chain = nghttp2_mem_malloc(mem, sizeof(nghttp2_buf_chain) * veclen);
239   if (head_chain == NULL) {
240     return NGHTTP2_ERR_NOMEM;
241   }
242 
243   for (i = 0; i < veclen; ++i) {
244     cur_chain = &head_chain[i];
245     cur_chain->next = NULL;
246     nghttp2_buf_wrap_init(&cur_chain->buf, vec[i].base, vec[i].len);
247 
248     *dst_chain = cur_chain;
249     dst_chain = &cur_chain->next;
250   }
251 
252   bufs->mem = mem;
253   bufs->offset = 0;
254 
255   bufs->head = head_chain;
256   bufs->cur = bufs->head;
257 
258   /* We don't use chunk_length since no allocation is expected. */
259   bufs->chunk_length = 0;
260   bufs->chunk_used = veclen;
261   bufs->max_chunk = veclen;
262   bufs->chunk_keep = veclen;
263 
264   return 0;
265 }
266 
nghttp2_bufs_wrap_free(nghttp2_bufs * bufs)267 void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs) {
268   if (bufs == NULL) {
269     return;
270   }
271 
272   if (bufs->head) {
273     nghttp2_mem_free(bufs->mem, bufs->head);
274   }
275 }
276 
nghttp2_bufs_seek_last_present(nghttp2_bufs * bufs)277 void nghttp2_bufs_seek_last_present(nghttp2_bufs *bufs) {
278   nghttp2_buf_chain *ci;
279 
280   for (ci = bufs->cur; ci; ci = ci->next) {
281     if (nghttp2_buf_len(&ci->buf) == 0) {
282       return;
283     } else {
284       bufs->cur = ci;
285     }
286   }
287 }
288 
nghttp2_bufs_len(nghttp2_bufs * bufs)289 size_t nghttp2_bufs_len(nghttp2_bufs *bufs) {
290   nghttp2_buf_chain *ci;
291   size_t len;
292 
293   len = 0;
294   for (ci = bufs->head; ci; ci = ci->next) {
295     len += nghttp2_buf_len(&ci->buf);
296   }
297 
298   return len;
299 }
300 
bufs_alloc_chain(nghttp2_bufs * bufs)301 static int bufs_alloc_chain(nghttp2_bufs *bufs) {
302   int rv;
303   nghttp2_buf_chain *chain;
304 
305   if (bufs->cur->next) {
306     bufs->cur = bufs->cur->next;
307 
308     return 0;
309   }
310 
311   if (bufs->max_chunk == bufs->chunk_used) {
312     return NGHTTP2_ERR_BUFFER_ERROR;
313   }
314 
315   rv = buf_chain_new(&chain, bufs->chunk_length, bufs->mem);
316   if (rv != 0) {
317     return rv;
318   }
319 
320   DEBUGF("new buffer %zu bytes allocated for bufs %p, used %zu\n",
321          bufs->chunk_length, bufs, bufs->chunk_used);
322 
323   ++bufs->chunk_used;
324 
325   bufs->cur->next = chain;
326   bufs->cur = chain;
327 
328   nghttp2_buf_shift_right(&bufs->cur->buf, bufs->offset);
329 
330   return 0;
331 }
332 
nghttp2_bufs_add(nghttp2_bufs * bufs,const void * data,size_t len)333 int nghttp2_bufs_add(nghttp2_bufs *bufs, const void *data, size_t len) {
334   int rv;
335   size_t nwrite;
336   nghttp2_buf *buf;
337   const uint8_t *p;
338 
339   p = data;
340 
341   while (len) {
342     buf = &bufs->cur->buf;
343 
344     nwrite = nghttp2_min(nghttp2_buf_avail(buf), len);
345     if (nwrite == 0) {
346       rv = bufs_alloc_chain(bufs);
347       if (rv != 0) {
348         return rv;
349       }
350       continue;
351     }
352 
353     buf->last = nghttp2_cpymem(buf->last, p, nwrite);
354     p += nwrite;
355     len -= nwrite;
356   }
357 
358   return 0;
359 }
360 
bufs_ensure_addb(nghttp2_bufs * bufs)361 static int bufs_ensure_addb(nghttp2_bufs *bufs) {
362   int rv;
363   nghttp2_buf *buf;
364 
365   buf = &bufs->cur->buf;
366 
367   if (nghttp2_buf_avail(buf) > 0) {
368     return 0;
369   }
370 
371   rv = bufs_alloc_chain(bufs);
372   if (rv != 0) {
373     return rv;
374   }
375 
376   return 0;
377 }
378 
nghttp2_bufs_addb(nghttp2_bufs * bufs,uint8_t b)379 int nghttp2_bufs_addb(nghttp2_bufs *bufs, uint8_t b) {
380   int rv;
381 
382   rv = bufs_ensure_addb(bufs);
383   if (rv != 0) {
384     return rv;
385   }
386 
387   *bufs->cur->buf.last++ = b;
388 
389   return 0;
390 }
391 
nghttp2_bufs_addb_hold(nghttp2_bufs * bufs,uint8_t b)392 int nghttp2_bufs_addb_hold(nghttp2_bufs *bufs, uint8_t b) {
393   int rv;
394 
395   rv = bufs_ensure_addb(bufs);
396   if (rv != 0) {
397     return rv;
398   }
399 
400   *bufs->cur->buf.last = b;
401 
402   return 0;
403 }
404 
nghttp2_bufs_orb(nghttp2_bufs * bufs,uint8_t b)405 int nghttp2_bufs_orb(nghttp2_bufs *bufs, uint8_t b) {
406   int rv;
407 
408   rv = bufs_ensure_addb(bufs);
409   if (rv != 0) {
410     return rv;
411   }
412 
413   *bufs->cur->buf.last++ |= b;
414 
415   return 0;
416 }
417 
nghttp2_bufs_orb_hold(nghttp2_bufs * bufs,uint8_t b)418 int nghttp2_bufs_orb_hold(nghttp2_bufs *bufs, uint8_t b) {
419   int rv;
420 
421   rv = bufs_ensure_addb(bufs);
422   if (rv != 0) {
423     return rv;
424   }
425 
426   *bufs->cur->buf.last |= b;
427 
428   return 0;
429 }
430 
nghttp2_bufs_remove(nghttp2_bufs * bufs,uint8_t ** out)431 ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out) {
432   size_t len;
433   nghttp2_buf_chain *chain;
434   nghttp2_buf *buf;
435   uint8_t *res;
436   nghttp2_buf resbuf;
437 
438   len = 0;
439 
440   for (chain = bufs->head; chain; chain = chain->next) {
441     len += nghttp2_buf_len(&chain->buf);
442   }
443 
444   if (len == 0) {
445     res = NULL;
446     return 0;
447   }
448 
449   res = nghttp2_mem_malloc(bufs->mem, len);
450   if (res == NULL) {
451     return NGHTTP2_ERR_NOMEM;
452   }
453 
454   nghttp2_buf_wrap_init(&resbuf, res, len);
455 
456   for (chain = bufs->head; chain; chain = chain->next) {
457     buf = &chain->buf;
458     resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf));
459   }
460 
461   *out = res;
462 
463   return (ssize_t)len;
464 }
465 
nghttp2_bufs_remove_copy(nghttp2_bufs * bufs,uint8_t * out)466 size_t nghttp2_bufs_remove_copy(nghttp2_bufs *bufs, uint8_t *out) {
467   size_t len;
468   nghttp2_buf_chain *chain;
469   nghttp2_buf *buf;
470   nghttp2_buf resbuf;
471 
472   len = nghttp2_bufs_len(bufs);
473 
474   nghttp2_buf_wrap_init(&resbuf, out, len);
475 
476   for (chain = bufs->head; chain; chain = chain->next) {
477     buf = &chain->buf;
478     resbuf.last = nghttp2_cpymem(resbuf.last, buf->pos, nghttp2_buf_len(buf));
479   }
480 
481   return len;
482 }
483 
nghttp2_bufs_reset(nghttp2_bufs * bufs)484 void nghttp2_bufs_reset(nghttp2_bufs *bufs) {
485   nghttp2_buf_chain *chain, *ci;
486   size_t k;
487 
488   k = bufs->chunk_keep;
489 
490   for (ci = bufs->head; ci; ci = ci->next) {
491     nghttp2_buf_reset(&ci->buf);
492     nghttp2_buf_shift_right(&ci->buf, bufs->offset);
493 
494     if (--k == 0) {
495       break;
496     }
497   }
498 
499   if (ci) {
500     chain = ci->next;
501     ci->next = NULL;
502 
503     for (ci = chain; ci;) {
504       chain = ci->next;
505 
506       buf_chain_del(ci, bufs->mem);
507 
508       ci = chain;
509     }
510 
511     bufs->chunk_used = bufs->chunk_keep;
512   }
513 
514   bufs->cur = bufs->head;
515 }
516 
nghttp2_bufs_advance(nghttp2_bufs * bufs)517 int nghttp2_bufs_advance(nghttp2_bufs *bufs) { return bufs_alloc_chain(bufs); }
518 
nghttp2_bufs_next_present(nghttp2_bufs * bufs)519 int nghttp2_bufs_next_present(nghttp2_bufs *bufs) {
520   nghttp2_buf_chain *chain;
521 
522   chain = bufs->cur->next;
523 
524   return chain && nghttp2_buf_len(&chain->buf);
525 }
526