Skip to content

Commit d42f369

Browse files
committed
realloc-buffer: add three new helpers
realloc_buffer_append_byte() does what the name suggests: appends one byte to the realloc buffer. realloc_buffer_read_full() reads data off an fd until EOF is reached. realloc_buffer_memchr() searchs the buffer for a character.
1 parent 543f884 commit d42f369

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/realloc-buffer.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,23 @@ int realloc_buffer_read_size(ReallocBuffer *b, int fd, size_t add) {
243243
return l > 0;
244244
}
245245

246+
int realloc_buffer_read_full(ReallocBuffer *b, int fd, size_t limit) {
247+
int r;
248+
249+
/* Reads from "fd" until EOF is hit, but never more than "limit" */
250+
251+
for (;;) {
252+
if (limit != (size_t) -1 && realloc_buffer_size(b) > limit)
253+
return -E2BIG;
254+
255+
r = realloc_buffer_read(b, fd);
256+
if (r < 0)
257+
return r;
258+
if (r == 0)
259+
return 0;
260+
}
261+
}
262+
246263
int realloc_buffer_read_target(ReallocBuffer *b, int fd, size_t target_size) {
247264
int r;
248265

@@ -392,3 +409,31 @@ int realloc_buffer_printf(ReallocBuffer *b, const char *fmt, ...) {
392409
}
393410

394411
}
412+
413+
int realloc_buffer_memchr(ReallocBuffer *buffer, uint8_t c) {
414+
const uint8_t *p, *q;
415+
size_t l, d;
416+
int ret;
417+
418+
/* Looks for byte 'c' in the buffer. Returns -ENXIO when we can't find the byte. Returns an index >= 0 if
419+
* found. Returns -EOVERFLOW if found but the index doesn't fit in "int" */
420+
421+
l = realloc_buffer_size(buffer);
422+
if (l <= 0)
423+
return -ENXIO;
424+
425+
p = realloc_buffer_data(buffer);
426+
q = memchr(p, c, l);
427+
if (!q)
428+
return -ENXIO;
429+
430+
d = (size_t) (q - p);
431+
ret = (int) d;
432+
433+
if (_unlikely_(ret < 0))
434+
return -EOVERFLOW;
435+
if (_unlikely_((size_t) ret != d))
436+
return -EOVERFLOW;
437+
438+
return ret;
439+
}

src/realloc-buffer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ void* realloc_buffer_extend(ReallocBuffer *b, size_t size);
5656
void* realloc_buffer_extend0(ReallocBuffer *b, size_t size);
5757
void* realloc_buffer_append(ReallocBuffer *b, const void *p, size_t size);
5858

59+
static inline void* realloc_buffer_append_byte(ReallocBuffer *b, uint8_t q) {
60+
return realloc_buffer_append(b, &q, sizeof(q));
61+
}
62+
5963
void realloc_buffer_free(ReallocBuffer *b);
6064

6165
static inline void realloc_buffer_empty(ReallocBuffer *b) {
@@ -72,6 +76,8 @@ static inline int realloc_buffer_read(ReallocBuffer *b, int fd) {
7276
return realloc_buffer_read_size(b, fd, (size_t) -1);
7377
}
7478

79+
int realloc_buffer_read_full(ReallocBuffer *b, int fd, size_t limit);
80+
7581
int realloc_buffer_write(ReallocBuffer *b, int fd);
7682
int realloc_buffer_write_maybe(ReallocBuffer *b, int fd);
7783

@@ -83,4 +89,6 @@ void* realloc_buffer_donate(ReallocBuffer *b, void *p, size_t size);
8389

8490
int realloc_buffer_printf(ReallocBuffer *b, const char *fmt, ...) _printf_(2,3);
8591

92+
int realloc_buffer_memchr(ReallocBuffer *buffer, uint8_t c);
93+
8694
#endif

0 commit comments

Comments
 (0)