aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bin/tar2sqfs/src/tar2sqfs.c9
-rw-r--r--include/io/istream.h3
-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
-rw-r--r--lib/io/test/istream_read.c2
-rw-r--r--lib/io/test/istream_skip.c2
-rw-r--r--lib/io/test/stream_splice.c2
-rw-r--r--lib/io/test/xfrm.c1
-rw-r--r--lib/tar/src/iterator.c37
14 files changed, 99 insertions, 116 deletions
diff --git a/bin/tar2sqfs/src/tar2sqfs.c b/bin/tar2sqfs/src/tar2sqfs.c
index 9257fed..1145675 100644
--- a/bin/tar2sqfs/src/tar2sqfs.c
+++ b/bin/tar2sqfs/src/tar2sqfs.c
@@ -36,22 +36,17 @@ static istream_t *magic_autowrap(istream_t *strm)
{
xfrm_stream_t *xfrm = NULL;
istream_t *wrapper = NULL;
- const sqfs_u8 *data;
- size_t avail;
int ret;
ret = istream_precache(strm);
if (ret != 0)
goto out;
- data = strm->buffer + strm->buffer_offset;
- avail = strm->buffer_used - strm->buffer_offset;
-
- ret = tar_probe(data, avail);
+ ret = tar_probe(strm->buffer, strm->buffer_used);
if (ret > 0)
return strm;
- ret = xfrm_compressor_id_from_magic(data, avail);
+ ret = xfrm_compressor_id_from_magic(strm->buffer, strm->buffer_used);
if (ret <= 0)
return strm;
diff --git a/include/io/istream.h b/include/io/istream.h
index 7adb204..9a8257f 100644
--- a/include/io/istream.h
+++ b/include/io/istream.h
@@ -21,10 +21,9 @@ typedef struct istream_t {
sqfs_object_t base;
size_t buffer_used;
- size_t buffer_offset;
bool eof;
- sqfs_u8 *buffer;
+ const sqfs_u8 *buffer;
int (*precache)(struct istream_t *strm);
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);
diff --git a/lib/io/test/istream_read.c b/lib/io/test/istream_read.c
index 87e52c5..7029c38 100644
--- a/lib/io/test/istream_read.c
+++ b/lib/io/test/istream_read.c
@@ -101,7 +101,7 @@ int main(int argc, char **argv)
}
TEST_ASSERT(dummy->eof);
- TEST_ASSERT(dummy->buffer_offset >= dummy->buffer_used);
+ TEST_ASSERT(dummy->buffer_used == 0);
sqfs_drop(dummy);
return EXIT_SUCCESS;
}
diff --git a/lib/io/test/istream_skip.c b/lib/io/test/istream_skip.c
index 40d4ea4..c54a9cd 100644
--- a/lib/io/test/istream_skip.c
+++ b/lib/io/test/istream_skip.c
@@ -90,7 +90,7 @@ int main(int argc, char **argv)
}
TEST_ASSERT(dummy->eof);
- TEST_ASSERT(dummy->buffer_offset >= dummy->buffer_used);
+ TEST_ASSERT(dummy->buffer_used == 0);
sqfs_drop(dummy);
return EXIT_SUCCESS;
}
diff --git a/lib/io/test/stream_splice.c b/lib/io/test/stream_splice.c
index 9008d83..ab8a0e4 100644
--- a/lib/io/test/stream_splice.c
+++ b/lib/io/test/stream_splice.c
@@ -92,7 +92,7 @@ int main(int argc, char **argv)
}
TEST_ASSERT(in->eof);
- TEST_ASSERT(in->buffer_offset >= in->buffer_used);
+ TEST_ASSERT(in->buffer_used == 0);
TEST_EQUAL_UI(total, end2);
TEST_EQUAL_UI(out_offset, end2);
sqfs_drop(in);
diff --git a/lib/io/test/xfrm.c b/lib/io/test/xfrm.c
index d06c558..ffb1f37 100644
--- a/lib/io/test/xfrm.c
+++ b/lib/io/test/xfrm.c
@@ -440,7 +440,6 @@ static void run_unpack_test(const void *blob, size_t size)
TEST_EQUAL_I(ret, 0);
TEST_EQUAL_UI(mem_istream->buffer_used, 0);
- TEST_EQUAL_UI(mem_istream->buffer_offset, 0);
TEST_ASSERT(mem_istream->eof);
sqfs_drop(istream);
diff --git a/lib/tar/src/iterator.c b/lib/tar/src/iterator.c
index 532eb8c..b1be42d 100644
--- a/lib/tar/src/iterator.c
+++ b/lib/tar/src/iterator.c
@@ -71,10 +71,7 @@ static bool is_sparse_region(const tar_iterator_t *tar, sqfs_u64 *count)
static int data_available(tar_iterator_t *tar, sqfs_u64 want, sqfs_u64 *out)
{
- sqfs_u64 avail = tar->stream->buffer_used - tar->stream->buffer_offset;
-
- if ((want > avail) &&
- ((tar->stream->buffer_offset > 0) || avail == 0)) {
+ if (want > tar->stream->buffer_used) {
if (istream_precache(tar->stream)) {
tar->state = SQFS_ERROR_IO;
return -1;
@@ -84,11 +81,10 @@ static int data_available(tar_iterator_t *tar, sqfs_u64 want, sqfs_u64 *out)
tar->state = SQFS_ERROR_CORRUPTED;
return -1;
}
-
- avail = tar->stream->buffer_used;
}
- *out = avail <= want ? avail : want;
+ *out = tar->stream->buffer_used <= want ?
+ tar->stream->buffer_used : want;
return 0;
}
@@ -104,43 +100,36 @@ static int strm_precache(istream_t *strm)
tar_istream_t *tar = (tar_istream_t *)strm;
sqfs_u64 diff;
- strm->buffer_used -= strm->buffer_offset;
- strm->buffer_offset = 0;
-
if (strm->eof)
- return 0;
-
- tar->parent->offset += tar->parent->last_chunk;
+ goto out_eof;
if (!tar->parent->last_sparse) {
- tar->parent->stream->buffer_offset += tar->parent->last_chunk;
+ tar->parent->stream->buffer += tar->parent->last_chunk;
+ tar->parent->stream->buffer_used -= tar->parent->last_chunk;
tar->parent->record_size -= tar->parent->last_chunk;
}
+ tar->parent->offset += tar->parent->last_chunk;
if (tar->parent->offset >= tar->parent->file_size)
goto out_eof;
- if (is_sparse_region(tar->parent, &diff)) {
+ tar->parent->last_sparse = is_sparse_region(tar->parent, &diff);
+
+ if (tar->parent->last_sparse) {
if (diff > sizeof(tar->buffer))
diff = sizeof(tar->buffer);
strm->buffer = tar->buffer;
- strm->buffer_used = diff;
- tar->parent->last_chunk = diff;
- tar->parent->last_sparse = true;
-
memset(tar->buffer, 0, diff);
} else {
if (data_available(tar->parent, diff, &diff))
goto out_eof;
- strm->buffer = tar->parent->stream->buffer +
- tar->parent->stream->buffer_offset;
- strm->buffer_used = diff;
- tar->parent->last_chunk = diff;
- tar->parent->last_sparse = false;
+ strm->buffer = tar->parent->stream->buffer;
}
+ strm->buffer_used = diff;
+ tar->parent->last_chunk = diff;
return 0;
out_eof:
strm->eof = true;