aboutsummaryrefslogtreecommitdiff
path: root/lib/io/src
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-04 20:26:14 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-05 09:16:39 +0200
commit1370963723917eed6c93e28c9970a2b27be57ea4 (patch)
tree14c6e45fc1a4ccd1721823d65edd8a892135e23d /lib/io/src
parent8af4ee6f415fe316894e4423235dfc4ee70d8cbb (diff)
libio: remove buffer_offset from istream_t
Instead, make the buffer const, let the user adjust the pointer and size. The offset can then be inferred in precache. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/io/src')
-rw-r--r--lib/io/src/get_line.c22
-rw-r--r--lib/io/src/internal.h1
-rw-r--r--lib/io/src/istream.c25
-rw-r--r--lib/io/src/mem.c20
-rw-r--r--lib/io/src/unix/istream.c30
-rw-r--r--lib/io/src/win32/istream.c24
-rw-r--r--lib/io/src/xfrm/istream.c37
7 files changed, 80 insertions, 79 deletions
diff --git a/lib/io/src/get_line.c b/lib/io/src/get_line.c
index f7e0b59..19af208 100644
--- a/lib/io/src/get_line.c
+++ b/lib/io/src/get_line.c
@@ -41,8 +41,8 @@ static size_t trim(char *buffer, int flags)
int istream_get_line(istream_t *strm, char **out,
size_t *line_num, int flags)
{
+ size_t i, count, line_len = 0;
char *line = NULL, *new;
- size_t i, line_len = 0;
bool have_line = false;
*out = NULL;
@@ -70,24 +70,28 @@ int istream_get_line(istream_t *strm, char **out,
}
if (i < strm->buffer_used) {
- have_line = true;
- strm->buffer_offset = i + 1;
+ count = i++;
+
+ if (count > 0 && strm->buffer[count - 1] == '\r')
+ --count;
- if (i > 0 && strm->buffer[i - 1] == '\r')
- --i;
+ have_line = true;
} else {
- strm->buffer_offset = i;
+ count = i;
}
- new = realloc(line, line_len + i + 1);
+ new = realloc(line, line_len + count + 1);
if (new == NULL)
goto fail_errno;
line = new;
- memcpy(line + line_len, strm->buffer, i);
- line_len += i;
+ memcpy(line + line_len, strm->buffer, count);
+ line_len += count;
line[line_len] = '\0';
+ strm->buffer += i;
+ strm->buffer_used -= i;
+
if (have_line) {
line_len = trim(line, flags);
diff --git a/lib/io/src/internal.h b/lib/io/src/internal.h
index 25a0196..1e51012 100644
--- a/lib/io/src/internal.h
+++ b/lib/io/src/internal.h
@@ -21,6 +21,7 @@
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
+#include <assert.h>
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
diff --git a/lib/io/src/istream.c b/lib/io/src/istream.c
index 2b89a00..0739fd6 100644
--- a/lib/io/src/istream.c
+++ b/lib/io/src/istream.c
@@ -16,7 +16,7 @@ sqfs_s32 istream_read(istream_t *strm, void *data, size_t size)
size = 0x7FFFFFFF;
while (size > 0) {
- if (strm->buffer_offset >= strm->buffer_used) {
+ if (strm->buffer_used == 0) {
if (istream_precache(strm))
return -1;
@@ -24,13 +24,14 @@ sqfs_s32 istream_read(istream_t *strm, void *data, size_t size)
break;
}
- diff = strm->buffer_used - strm->buffer_offset;
+ diff = strm->buffer_used;
if (diff > size)
diff = size;
- memcpy(data, strm->buffer + strm->buffer_offset, diff);
+ memcpy(data, strm->buffer, diff);
data = (char *)data + diff;
- strm->buffer_offset += diff;
+ strm->buffer += diff;
+ strm->buffer_used -= diff;
size -= diff;
total += diff;
}
@@ -43,7 +44,7 @@ int istream_skip(istream_t *strm, sqfs_u64 size)
size_t diff;
while (size > 0) {
- if (strm->buffer_offset >= strm->buffer_used) {
+ if (strm->buffer_used == 0) {
if (istream_precache(strm))
return -1;
@@ -54,11 +55,12 @@ int istream_skip(istream_t *strm, sqfs_u64 size)
}
}
- diff = strm->buffer_used - strm->buffer_offset;
+ diff = strm->buffer_used;
if ((sqfs_u64)diff > size)
diff = size;
- strm->buffer_offset += diff;
+ strm->buffer += diff;
+ strm->buffer_used -= diff;
size -= diff;
}
@@ -74,7 +76,7 @@ sqfs_s32 istream_splice(istream_t *in, ostream_t *out, sqfs_u32 size)
size = 0x7FFFFFFF;
while (size > 0) {
- if (in->buffer_offset >= in->buffer_used) {
+ if (in->buffer_used == 0) {
if (istream_precache(in))
return -1;
@@ -82,14 +84,15 @@ sqfs_s32 istream_splice(istream_t *in, ostream_t *out, sqfs_u32 size)
break;
}
- diff = in->buffer_used - in->buffer_offset;
+ diff = in->buffer_used;
if (diff > size)
diff = size;
- if (ostream_append(out, in->buffer + in->buffer_offset, diff))
+ if (ostream_append(out, in->buffer, diff))
return -1;
- in->buffer_offset += diff;
+ in->buffer += diff;
+ in->buffer_used -= diff;
size -= diff;
total += diff;
}
diff --git a/lib/io/src/mem.c b/lib/io/src/mem.c
index d305a83..b1abbdb 100644
--- a/lib/io/src/mem.c
+++ b/lib/io/src/mem.c
@@ -10,6 +10,7 @@
#include <stdlib.h>
#include <string.h>
+#include <assert.h>
typedef struct {
istream_t base;
@@ -28,17 +29,16 @@ static int mem_in_precache(istream_t *strm)
mem_istream_t *mem = (mem_istream_t *)strm;
size_t diff;
- if (strm->buffer_offset >= strm->buffer_used) {
- strm->buffer_offset = 0;
- strm->buffer_used = 0;
- } else if (strm->buffer_offset > 0) {
- memmove(strm->buffer,
- strm->buffer + strm->buffer_offset,
- strm->buffer_used - strm->buffer_offset);
+ assert(strm->buffer >= mem->buffer);
+ assert(strm->buffer <= (mem->buffer + mem->bufsz));
+ assert(strm->buffer_used <= mem->bufsz);
+ assert((size_t)(strm->buffer - mem->buffer) <=
+ (mem->bufsz - strm->buffer_used));
- strm->buffer_used -= strm->buffer_offset;
- strm->buffer_offset = 0;
- }
+ if (strm->buffer_used > 0)
+ memmove(mem->buffer, strm->buffer, strm->buffer_used);
+
+ strm->buffer = mem->buffer;
diff = mem->bufsz - strm->buffer_used;
if (diff > mem->size)
diff --git a/lib/io/src/unix/istream.c b/lib/io/src/unix/istream.c
index 24b18d6..db870d9 100644
--- a/lib/io/src/unix/istream.c
+++ b/lib/io/src/unix/istream.c
@@ -17,25 +17,21 @@ typedef struct {
static int file_precache(istream_t *strm)
{
file_istream_t *file = (file_istream_t *)strm;
- ssize_t ret;
- size_t diff;
-
- if (strm->buffer_offset >= strm->buffer_used) {
- strm->buffer_offset = 0;
- strm->buffer_used = 0;
- } else if (strm->buffer_offset > 0) {
- memmove(strm->buffer,
- strm->buffer + strm->buffer_offset,
- strm->buffer_used - strm->buffer_offset);
-
- strm->buffer_used -= strm->buffer_offset;
- strm->buffer_offset = 0;
- }
- while (!strm->eof && strm->buffer_used < sizeof(file->buffer)) {
- diff = sizeof(file->buffer) - strm->buffer_used;
+ assert(strm->buffer >= file->buffer);
+ assert(strm->buffer <= (file->buffer + BUFSZ));
+ assert(strm->buffer_used <= BUFSZ);
+ assert((size_t)(strm->buffer - file->buffer) <=
+ (BUFSZ - strm->buffer_used));
+
+ if (strm->buffer_used > 0)
+ memmove(file->buffer, strm->buffer, strm->buffer_used);
+
+ strm->buffer = file->buffer;
- ret = read(file->fd, strm->buffer + strm->buffer_used, diff);
+ while (!strm->eof && strm->buffer_used < BUFSZ) {
+ ssize_t ret = read(file->fd, file->buffer + strm->buffer_used,
+ BUFSZ - strm->buffer_used);
if (ret == 0)
strm->eof = true;
diff --git a/lib/io/src/win32/istream.c b/lib/io/src/win32/istream.c
index 918b868..94c8584 100644
--- a/lib/io/src/win32/istream.c
+++ b/lib/io/src/win32/istream.c
@@ -23,27 +23,25 @@ static int file_precache(istream_t *strm)
DWORD diff, actual;
HANDLE hnd;
- if (strm->buffer_offset >= strm->buffer_used) {
- strm->buffer_offset = 0;
- strm->buffer_used = 0;
- } else if (strm->buffer_offset > 0) {
- memmove(strm->buffer,
- strm->buffer + strm->buffer_offset,
- strm->buffer_used - strm->buffer_offset);
-
- strm->buffer_used -= strm->buffer_offset;
- strm->buffer_offset = 0;
- }
-
if (strm->eof)
return 0;
+ assert(strm->buffer >= file->buffer);
+ assert(strm->buffer <= (file->buffer + BUFSZ));
+ assert(strm->buffer_used <= BUFSZ);
+ assert((size_t)(strm->buffer - file->buffer) <=
+ (BUFSZ - strm->buffer_used));
+
+ if (strm->buffer_used > 0)
+ memmove(file->buffer, strm->buffer, strm->buffer_used);
+
+ strm->buffer = file->buffer;
hnd = file->path == NULL ? GetStdHandle(STD_INPUT_HANDLE) : file->hnd;
while (strm->buffer_used < sizeof(file->buffer)) {
diff = sizeof(file->buffer) - strm->buffer_used;
- if (!ReadFile(hnd, strm->buffer + strm->buffer_used,
+ if (!ReadFile(hnd, file->buffer + strm->buffer_used,
diff, &actual, NULL)) {
DWORD error = GetLastError();
diff --git a/lib/io/src/xfrm/istream.c b/lib/io/src/xfrm/istream.c
index b301209..152563f 100644
--- a/lib/io/src/xfrm/istream.c
+++ b/lib/io/src/xfrm/istream.c
@@ -20,28 +20,25 @@ static int xfrm_precache(istream_t *base)
istream_xfrm_t *xfrm = (istream_xfrm_t *)base;
int ret;
- if (base->buffer_offset >= base->buffer_used) {
- base->buffer_offset = 0;
- base->buffer_used = 0;
- } else if (base->buffer_offset > 0) {
- memmove(base->buffer,
- base->buffer + base->buffer_offset,
- base->buffer_used - base->buffer_offset);
-
- base->buffer_used -= base->buffer_offset;
- base->buffer_offset = 0;
- }
-
if (base->eof)
return 0;
+ assert(base->buffer >= xfrm->uncompressed);
+ assert(base->buffer <= (xfrm->uncompressed + BUFSZ));
+ assert(base->buffer_used <= BUFSZ);
+ assert((size_t)(base->buffer - xfrm->uncompressed) <=
+ (BUFSZ - base->buffer_used));
+
+ if (base->buffer_used > 0)
+ memmove(xfrm->uncompressed, base->buffer, base->buffer_used);
+
+ base->buffer = xfrm->uncompressed;
+
ret = istream_precache(xfrm->wrapped);
if (ret != 0)
return ret;
for (;;) {
- const sqfs_u32 in_sz = xfrm->wrapped->buffer_used;
- const sqfs_u32 out_sz = sizeof(xfrm->uncompressed);
sqfs_u32 in_off = 0, out_off = base->buffer_used;
int mode = XFRM_STREAM_FLUSH_NONE;
@@ -49,9 +46,10 @@ static int xfrm_precache(istream_t *base)
mode = XFRM_STREAM_FLUSH_FULL;
ret = xfrm->xfrm->process_data(xfrm->xfrm,
- xfrm->wrapped->buffer, in_sz,
- base->buffer + out_off,
- out_sz - out_off,
+ xfrm->wrapped->buffer,
+ xfrm->wrapped->buffer_used,
+ xfrm->uncompressed + out_off,
+ BUFSZ - out_off,
&in_off, &out_off, mode);
if (ret == XFRM_STREAM_ERROR) {
@@ -61,9 +59,10 @@ static int xfrm_precache(istream_t *base)
}
base->buffer_used = out_off;
- xfrm->wrapped->buffer_offset = in_off;
+ xfrm->wrapped->buffer += in_off;
+ xfrm->wrapped->buffer_used -= in_off;
- if (ret == XFRM_STREAM_BUFFER_FULL || out_off >= out_sz)
+ if (ret == XFRM_STREAM_BUFFER_FULL || out_off >= BUFSZ)
break;
ret = istream_precache(xfrm->wrapped);