Coverage Report

Created: 2023-03-26 06:11

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