aboutsummaryrefslogtreecommitdiff
path: root/lib/io
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-09 13:43:50 +0200
committerDavid Oberhollenzer <david.oberhollenzer@sigma-star.at>2023-06-09 13:43:50 +0200
commit9a4110f3c205107a3a48a0e48b760abf3cd1f3bc (patch)
tree0d89aec7c431487ebeb1254a36194acd0f837ee3 /lib/io
parent5b1a81160a6d0e63ab1360e8777009b913464d89 (diff)
libio: eliminate direct access of the interal buffer
Instead, go through helper functions, which in a next step can be moved inside the implementation. Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/io')
-rw-r--r--lib/io/src/get_line.c22
-rw-r--r--lib/io/src/istream.c76
-rw-r--r--lib/io/src/xfrm/istream.c34
3 files changed, 70 insertions, 62 deletions
diff --git a/lib/io/src/get_line.c b/lib/io/src/get_line.c
index c764d04..a939a6c 100644
--- a/lib/io/src/get_line.c
+++ b/lib/io/src/get_line.c
@@ -46,12 +46,14 @@ int istream_get_line(istream_t *strm, char **out,
for (;;) {
bool have_line = false;
- size_t i, count;
+ size_t i, count, avail;
+ const sqfs_u8 *ptr;
+ int ret;
- if (istream_precache(strm))
+ ret = istream_get_buffered_data(strm, &ptr, &avail);
+ if (ret < 0)
goto fail_free;
-
- if (strm->buffer_used == 0) {
+ if (ret > 0) {
if (line_len == 0)
goto out_eof;
@@ -62,12 +64,12 @@ int istream_get_line(istream_t *strm, char **out,
goto out_eof;
}
- for (i = 0; i < strm->buffer_used; ++i) {
- if (strm->buffer[i] == '\n')
+ for (i = 0; i < avail; ++i) {
+ if (ptr[i] == '\n')
break;
}
- if (i < strm->buffer_used) {
+ if (i < avail) {
count = i++;
have_line = true;
} else {
@@ -79,12 +81,12 @@ int istream_get_line(istream_t *strm, char **out,
goto fail_errno;
line = new;
- memcpy(line + line_len, strm->buffer, count);
+ memcpy(line + line_len, ptr, count);
line_len += count;
line[line_len] = '\0';
- strm->buffer += i;
- strm->buffer_used -= i;
+ if (istream_advance_buffer(strm, i))
+ goto fail_free;
if (have_line) {
if (line_len > 0 && line[line_len - 1] == '\r')
diff --git a/lib/io/src/istream.c b/lib/io/src/istream.c
index 0739fd6..6171f2c 100644
--- a/lib/io/src/istream.c
+++ b/lib/io/src/istream.c
@@ -10,30 +10,32 @@
sqfs_s32 istream_read(istream_t *strm, void *data, size_t size)
{
sqfs_s32 total = 0;
- size_t diff;
if (size > 0x7FFFFFFF)
size = 0x7FFFFFFF;
while (size > 0) {
- if (strm->buffer_used == 0) {
- if (istream_precache(strm))
- return -1;
+ const sqfs_u8 *ptr;
+ size_t diff;
+ int ret;
- if (strm->buffer_used == 0)
- break;
- }
+ ret = istream_get_buffered_data(strm, &ptr, &diff);
+ if (ret > 0)
+ break;
+ if (ret < 0)
+ return ret;
- diff = strm->buffer_used;
if (diff > size)
diff = size;
- memcpy(data, strm->buffer, diff);
+ memcpy(data, ptr, diff);
data = (char *)data + diff;
- strm->buffer += diff;
- strm->buffer_used -= diff;
size -= diff;
total += diff;
+
+ ret = istream_advance_buffer(strm, diff);
+ if (ret)
+ return -1;
}
return total;
@@ -41,27 +43,27 @@ sqfs_s32 istream_read(istream_t *strm, void *data, size_t size)
int istream_skip(istream_t *strm, sqfs_u64 size)
{
- size_t diff;
-
while (size > 0) {
- if (strm->buffer_used == 0) {
- if (istream_precache(strm))
- return -1;
-
- if (strm->buffer_used == 0) {
- fprintf(stderr, "%s: unexpected end-of-file\n",
- strm->get_filename(strm));
- return -1;
- }
+ const sqfs_u8 *ptr;
+ size_t diff;
+ int ret;
+
+ ret = istream_get_buffered_data(strm, &ptr, &diff);
+ if (ret < 0)
+ return ret;
+ if (ret > 0) {
+ fprintf(stderr, "%s: unexpected end-of-file\n",
+ strm->get_filename(strm));
+ return -1;
}
- diff = strm->buffer_used;
if ((sqfs_u64)diff > size)
diff = size;
- strm->buffer += diff;
- strm->buffer_used -= diff;
size -= diff;
+
+ if (istream_advance_buffer(strm, diff))
+ return -1;
}
return 0;
@@ -70,31 +72,31 @@ int istream_skip(istream_t *strm, sqfs_u64 size)
sqfs_s32 istream_splice(istream_t *in, ostream_t *out, sqfs_u32 size)
{
sqfs_s32 total = 0;
- size_t diff;
if (size > 0x7FFFFFFF)
size = 0x7FFFFFFF;
while (size > 0) {
- if (in->buffer_used == 0) {
- if (istream_precache(in))
- return -1;
+ const sqfs_u8 *ptr;
+ size_t diff;
+ int ret;
- if (in->buffer_used == 0)
- break;
- }
+ ret = istream_get_buffered_data(in, &ptr, &diff);
+ if (ret < 0)
+ return ret;
+ if (ret > 0)
+ break;
- diff = in->buffer_used;
if (diff > size)
diff = size;
- if (ostream_append(out, in->buffer, diff))
+ if (ostream_append(out, ptr, diff))
+ return -1;
+ if (istream_advance_buffer(in, diff))
return -1;
- in->buffer += diff;
- in->buffer_used -= diff;
- size -= diff;
total += diff;
+ size -= diff;
}
return total;
diff --git a/lib/io/src/xfrm/istream.c b/lib/io/src/xfrm/istream.c
index 152563f..a89ac3d 100644
--- a/lib/io/src/xfrm/istream.c
+++ b/lib/io/src/xfrm/istream.c
@@ -18,7 +18,7 @@ typedef struct istream_xfrm_t {
static int xfrm_precache(istream_t *base)
{
istream_xfrm_t *xfrm = (istream_xfrm_t *)base;
- int ret;
+ int ret, sret;
if (base->eof)
return 0;
@@ -34,20 +34,26 @@ static int xfrm_precache(istream_t *base)
base->buffer = xfrm->uncompressed;
- ret = istream_precache(xfrm->wrapped);
- if (ret != 0)
- return ret;
-
for (;;) {
sqfs_u32 in_off = 0, out_off = base->buffer_used;
int mode = XFRM_STREAM_FLUSH_NONE;
+ const sqfs_u8 *ptr;
+ size_t avail;
+
+ ret = istream_precache(xfrm->wrapped);
+ if (ret != 0)
+ return ret;
- if (xfrm->wrapped->eof)
+ ret = istream_get_buffered_data(xfrm->wrapped, &ptr, &avail);
+ if (ret < 0)
+ return ret;
+ if (ret > 0) {
mode = XFRM_STREAM_FLUSH_FULL;
+ avail = 0;
+ }
ret = xfrm->xfrm->process_data(xfrm->xfrm,
- xfrm->wrapped->buffer,
- xfrm->wrapped->buffer_used,
+ ptr, avail,
xfrm->uncompressed + out_off,
BUFSZ - out_off,
&in_off, &out_off, mode);
@@ -59,17 +65,15 @@ static int xfrm_precache(istream_t *base)
}
base->buffer_used = out_off;
- xfrm->wrapped->buffer += in_off;
- xfrm->wrapped->buffer_used -= in_off;
+
+ sret = istream_advance_buffer(xfrm->wrapped, in_off);
+ if (sret != 0)
+ return sret;
if (ret == XFRM_STREAM_BUFFER_FULL || out_off >= BUFSZ)
break;
- ret = istream_precache(xfrm->wrapped);
- if (ret != 0)
- return ret;
-
- if (xfrm->wrapped->eof && xfrm->wrapped->buffer_used == 0) {
+ if (mode == XFRM_STREAM_FLUSH_FULL) {
if (base->buffer_used == 0)
base->eof = true;
break;