summaryrefslogtreecommitdiff
path: root/src/io.c
diff options
context:
space:
mode:
authorjakob <jakob@memeware.net>2017-06-03 19:00:07 -0400
committerjakob <jakob@memeware.net>2017-06-03 19:00:07 -0400
commit17b658ceadd08d693f23448c276958e2eea5970e (patch)
tree8d0f62709646de856373a0ffa4fc2513f717f770 /src/io.c
parent6adad1bfad91274aad030cf6167c1f19fb602d62 (diff)
Fully working repacker, no bugs related to the encoded output being larger than the buffer, and filenames are properly encoded.
Diffstat (limited to 'src/io.c')
-rw-r--r--src/io.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/io.c b/src/io.c
index 759902c..fcc5602 100644
--- a/src/io.c
+++ b/src/io.c
@@ -110,20 +110,25 @@ void stream_read(void *dest, struct stream *s, size_t n) {
/* Copies `n` bytes into the given stream from the memory area specified
by `src`. The stream's cursor is advanced appropriately. */
void stream_write(struct stream *s, void *src, size_t n) {
- while (s->_cur + n > s->_start + s->len) {
- ptrdiff_t dist = (uintptr_t) s->_cur - (uintptr_t) s->_start;
- switch(s->_loc) {
- case HEAP:
- s->len *= 2;
- s->_start = realloc(s->_start, s->len);
- s->_cur = s->_start + dist;
- }
- }
+ while (s->_cur + n > s->_start + s->len)
+ stream_expand(s, s->len);
memcpy(s->_cur, src, n);
s->_cur += n;
}
+/* Increases the length of `s` by `n` bytes. */
+void stream_expand(struct stream *s, size_t n) {
+ ptrdiff_t dist = (uintptr_t) s->_cur - (uintptr_t) s->_start;
+ switch(s->_loc) {
+ case HEAP:
+ s->len += n;
+ s->_start = realloc(s->_start, s->len);
+ s->_cur = s->_start + dist;
+ }
+}
+
+
/* Concatenates the contents of `src` onto `dst`. */
void stream_concat(struct stream *dst, struct stream *src, size_t n) {
stream_write(dst, src->_cur, n);