Coverage Report

Created: 2026-04-29 06:03

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