diff options
author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-12 22:00:02 +0200 |
---|---|---|
committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2023-06-15 13:38:25 +0200 |
commit | 043495538ebaf02adba6d40764fb3e6def65cb09 (patch) | |
tree | ff05ba2a1ebc5093ec266114e309af9991187ae8 /lib/sqfs | |
parent | ba99ef34e7b073c03519ef74f017091de6c9bee8 (diff) |
libsquashfs: Add a get_filename function to sqfs_file_t
Similar to the sqfs_istream_t & sqfs_ostream_t interfaces.
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Diffstat (limited to 'lib/sqfs')
-rw-r--r-- | lib/sqfs/src/unix/io_file.c | 26 | ||||
-rw-r--r-- | lib/sqfs/src/win32/io_file.c | 26 | ||||
-rw-r--r-- | lib/sqfs/test/table.c | 1 | ||||
-rw-r--r-- | lib/sqfs/test/xattr_writer.c | 1 |
4 files changed, 48 insertions, 6 deletions
diff --git a/lib/sqfs/src/unix/io_file.c b/lib/sqfs/src/unix/io_file.c index e1fb9db..886f752 100644 --- a/lib/sqfs/src/unix/io_file.c +++ b/lib/sqfs/src/unix/io_file.c @@ -9,6 +9,7 @@ #include "sqfs/io.h" #include "sqfs/error.h" +#include "compat.h" #include <sys/stat.h> #include <stdlib.h> @@ -24,6 +25,8 @@ typedef struct { bool readonly; sqfs_u64 size; int fd; + + char name[]; } sqfs_file_stdio_t; @@ -39,6 +42,7 @@ static sqfs_object_t *stdio_copy(const sqfs_object_t *base) { const sqfs_file_stdio_t *file = (const sqfs_file_stdio_t *)base; sqfs_file_stdio_t *copy; + size_t size; int err; if (!file->readonly) { @@ -46,11 +50,12 @@ static sqfs_object_t *stdio_copy(const sqfs_object_t *base) return NULL; } - copy = calloc(1, sizeof(*copy)); + size = sizeof(*file) + strlen(file->name) + 1; + copy = calloc(1, size); if (copy == NULL) return NULL; - memcpy(copy, file, sizeof(*file)); + memcpy(copy, file, size); copy->fd = dup(file->fd); if (copy->fd < 0) { @@ -136,10 +141,15 @@ static int stdio_truncate(sqfs_file_t *base, sqfs_u64 size) return 0; } +static const char *stdio_get_filename(sqfs_file_t *file) +{ + return ((sqfs_file_stdio_t *)file)->name; +} sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) { sqfs_file_stdio_t *file; + size_t size, namelen; int open_mode, temp; sqfs_file_t *base; struct stat sb; @@ -149,12 +159,21 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) return NULL; } - file = calloc(1, sizeof(*file)); + namelen = strlen(filename); + size = sizeof(*file) + 1; + + if (SZ_ADD_OV(size, namelen, &size)) { + errno = EOVERFLOW; + return NULL; + } + + file = calloc(1, size); base = (sqfs_file_t *)file; if (file == NULL) return NULL; sqfs_object_init(file, stdio_destroy, stdio_copy); + memcpy(file->name, filename, namelen); if (flags & SQFS_FILE_OPEN_READ_ONLY) { file->readonly = true; @@ -192,5 +211,6 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) base->write_at = stdio_write_at; base->get_size = stdio_get_size; base->truncate = stdio_truncate; + base->get_filename = stdio_get_filename; return base; } diff --git a/lib/sqfs/src/win32/io_file.c b/lib/sqfs/src/win32/io_file.c index 548a246..ec98900 100644 --- a/lib/sqfs/src/win32/io_file.c +++ b/lib/sqfs/src/win32/io_file.c @@ -9,6 +9,7 @@ #include "sqfs/io.h" #include "sqfs/error.h" +#include "compat.h" #include <stdlib.h> @@ -22,6 +23,8 @@ typedef struct { bool readonly; sqfs_u64 size; HANDLE fd; + + char name[]; } sqfs_file_stdio_t; @@ -37,6 +40,7 @@ static sqfs_object_t *stdio_copy(const sqfs_object_t *base) { const sqfs_file_stdio_t *file = (const sqfs_file_stdio_t *)base; sqfs_file_stdio_t *copy; + size_t size; BOOL ret; if (!file->readonly) { @@ -44,11 +48,12 @@ static sqfs_object_t *stdio_copy(const sqfs_object_t *base) return NULL; } - copy = calloc(1, sizeof(*copy)); + size = sizeof(*file) + strlen(file->name) + 1; + copy = calloc(1, size); if (copy == NULL) return NULL; - memcpy(copy, file, sizeof(*file)); + memcpy(copy, file, size); ret = DuplicateHandle(GetCurrentProcess(), file->fd, GetCurrentProcess(), ©->fd, @@ -148,11 +153,16 @@ static int stdio_truncate(sqfs_file_t *base, sqfs_u64 size) return 0; } +static const char *stdio_get_filename(sqfs_file_t *file) +{ + return ((sqfs_file_stdio_t *)file)->name; +} sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) { int access_flags, creation_mode, share_mode; sqfs_file_stdio_t *file; + size_t namelen, total; LARGE_INTEGER size; sqfs_file_t *base; WCHAR *wpath = NULL; @@ -163,6 +173,14 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) return NULL; } + namelen = strlen(filename); + total = sizeof(*file) + 1; + + if (SZ_ADD_OV(total, namelen, &total)) { + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + return NULL; + } + if (!(flags & SQFS_FILE_OPEN_NO_CHARSET_XFRM)) { length = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0); if (length <= 0) @@ -179,7 +197,7 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) wpath[length] = '\0'; } - file = calloc(1, sizeof(*file)); + file = calloc(1, total); base = (sqfs_file_t *)file; if (file == NULL) { free(wpath); @@ -188,6 +206,7 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) } sqfs_object_init(file, stdio_destroy, stdio_copy); + memcpy(file->name, filename, namelen); if (flags & SQFS_FILE_OPEN_READ_ONLY) { file->readonly = true; @@ -233,5 +252,6 @@ sqfs_file_t *sqfs_open_file(const char *filename, sqfs_u32 flags) base->write_at = stdio_write_at; base->get_size = stdio_get_size; base->truncate = stdio_truncate; + base->get_filename = stdio_get_filename; return base; } diff --git a/lib/sqfs/test/table.c b/lib/sqfs/test/table.c index 3e44fa3..324dc13 100644 --- a/lib/sqfs/test/table.c +++ b/lib/sqfs/test/table.c @@ -89,6 +89,7 @@ static sqfs_file_t dummy_file = { dummy_write_at, dummy_get_size, NULL, + NULL, }; static sqfs_compressor_t dummy_compressor = { diff --git a/lib/sqfs/test/xattr_writer.c b/lib/sqfs/test/xattr_writer.c index 55d2030..f5db9d3 100644 --- a/lib/sqfs/test/xattr_writer.c +++ b/lib/sqfs/test/xattr_writer.c @@ -59,6 +59,7 @@ static sqfs_file_t dummy_file = { dummy_write_at, dummy_get_size, NULL, + NULL, }; static sqfs_compressor_t dummy_compressor = { |