Coverage Report

Created: 2026-03-12 06:35

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