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