aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/src/io
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sqfs/src/io')
-rw-r--r--lib/sqfs/src/io/file.c286
-rw-r--r--lib/sqfs/src/io/istream.c203
-rw-r--r--lib/sqfs/src/io/ostream.c221
-rw-r--r--lib/sqfs/src/io/unix.c87
-rw-r--r--lib/sqfs/src/io/win32.c120
5 files changed, 917 insertions, 0 deletions
diff --git a/lib/sqfs/src/io/file.c b/lib/sqfs/src/io/file.c
new file mode 100644
index 0000000..881730d
--- /dev/null
+++ b/lib/sqfs/src/io/file.c
@@ -0,0 +1,286 @@
+/* SPDX-License-Identifier: LGPL-3.0-or-later */
+/*
+ * file.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#define SQFS_BUILDING_DLL
+#include "config.h"
+
+#include "sqfs/io.h"
+#include "sqfs/error.h"
+#include "compat.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+#if defined(_WIN32) || defined(__WINDOWS__)
+static int get_file_size(HANDLE fd, sqfs_u64 *out)
+{
+ LARGE_INTEGER size;
+ if (!GetFileSizeEx(fd, &size))
+ return -1;
+ *out = size.QuadPart;
+ return 0;
+}
+#else
+#include <sys/stat.h>
+#include <unistd.h>
+#include <errno.h>
+
+static int get_file_size(int fd, sqfs_u64 *out)
+{
+ struct stat sb;
+ if (fstat(fd, &sb))
+ return -1;
+ *out = sb.st_size;
+ return 0;
+}
+#endif
+
+typedef struct {
+ sqfs_file_t base;
+
+ bool readonly;
+ sqfs_u64 size;
+ sqfs_file_handle_t fd;
+
+ char name[];
+} sqfs_file_stdio_t;
+
+
+static void stdio_destroy(sqfs_object_t *base)
+{
+ sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
+
+ sqfs_close_native_file(file->fd);
+ free(file);
+}
+
+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;
+
+ if (!file->readonly) {
+#if defined(_WIN32) || defined(__WINDOWS__)
+ SetLastError(ERROR_NOT_SUPPORTED);
+#else
+ errno = ENOTSUP;
+#endif
+ return NULL;
+ }
+
+ size = sizeof(*file) + strlen(file->name) + 1;
+ copy = calloc(1, size);
+ if (copy == NULL)
+ return NULL;
+
+ memcpy(copy, file, size);
+
+ if (sqfs_duplicate_native_file(file->fd, &copy->fd)) {
+ os_error_t error = get_os_error_state();
+ free(copy);
+ copy = NULL;
+ set_os_error_state(error);
+ }
+
+ return (sqfs_object_t *)copy;
+}
+
+#if defined(_WIN32) || defined(__WINDOWS__)
+static int stdio_read_at(sqfs_file_t *base, sqfs_u64 offset,
+ void *buffer, size_t size)
+{
+ sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
+ DWORD actually_read;
+ int ret;
+
+ if (offset >= file->size)
+ return SQFS_ERROR_OUT_OF_BOUNDS;
+
+ if (size == 0)
+ return 0;
+
+ if ((offset + size - 1) >= file->size)
+ return SQFS_ERROR_OUT_OF_BOUNDS;
+
+ ret = sqfs_seek_native_file(file->fd, offset, SQFS_FILE_SEEK_START);
+ if (ret)
+ return ret;
+
+ while (size > 0) {
+ if (!ReadFile(file->fd, buffer, size, &actually_read, NULL))
+ return SQFS_ERROR_IO;
+
+ size -= actually_read;
+ buffer = (char *)buffer + actually_read;
+ }
+
+ return 0;
+}
+
+static int stdio_write_at(sqfs_file_t *base, sqfs_u64 offset,
+ const void *buffer, size_t size)
+{
+ sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
+ DWORD actually_read;
+ int ret;
+
+ if (size == 0)
+ return 0;
+
+ ret = sqfs_seek_native_file(file->fd, offset, SQFS_FILE_SEEK_START);
+ if (ret)
+ return ret;
+
+ while (size > 0) {
+ if (!WriteFile(file->fd, buffer, size, &actually_read, NULL))
+ return SQFS_ERROR_IO;
+
+ size -= actually_read;
+ buffer = (char *)buffer + actually_read;
+ offset += actually_read;
+
+ if (offset > file->size)
+ file->size = offset;
+ }
+
+ return 0;
+}
+#else
+static int stdio_read_at(sqfs_file_t *base, sqfs_u64 offset,
+ void *buffer, size_t size)
+{
+ sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
+ ssize_t ret;
+
+ while (size > 0) {
+ ret = pread(file->fd, buffer, size, offset);
+
+ if (ret < 0) {
+ if (errno == EINTR)
+ continue;
+ return SQFS_ERROR_IO;
+ }
+
+ if (ret == 0)
+ return SQFS_ERROR_OUT_OF_BOUNDS;
+
+ buffer = (char *)buffer + ret;
+ size -= ret;
+ offset += ret;
+ }
+
+ return 0;
+}
+
+static int stdio_write_at(sqfs_file_t *base, sqfs_u64 offset,
+ const void *buffer, size_t size)
+{
+ sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
+ ssize_t ret;
+
+ while (size > 0) {
+ ret = pwrite(file->fd, buffer, size, offset);
+
+ if (ret < 0) {
+ if (errno == EINTR)
+ continue;
+ return SQFS_ERROR_IO;
+ }
+
+ if (ret == 0)
+ return SQFS_ERROR_OUT_OF_BOUNDS;
+
+ buffer = (const char *)buffer + ret;
+ size -= ret;
+ offset += ret;
+ }
+
+ if (offset >= file->size)
+ file->size = offset;
+
+ return 0;
+}
+#endif
+
+static sqfs_u64 stdio_get_size(const sqfs_file_t *base)
+{
+ const sqfs_file_stdio_t *file = (const sqfs_file_stdio_t *)base;
+
+ return file->size;
+}
+
+static int stdio_truncate(sqfs_file_t *base, sqfs_u64 size)
+{
+ sqfs_file_stdio_t *file = (sqfs_file_stdio_t *)base;
+ int ret;
+
+ ret = sqfs_seek_native_file(file->fd, size, SQFS_FILE_SEEK_START |
+ SQFS_FILE_SEEK_TRUNCATE);
+ if (ret)
+ return ret;
+
+ file->size = 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)
+{
+ bool file_opened = false;
+ sqfs_file_stdio_t *file;
+ size_t size, namelen;
+ sqfs_file_t *base;
+ os_error_t err;
+
+ namelen = strlen(filename);
+ size = sizeof(*file) + 1;
+
+ if (SZ_ADD_OV(size, namelen, &size))
+ goto fail_no_mem;
+
+ file = calloc(1, size);
+ base = (sqfs_file_t *)file;
+ if (file == NULL)
+ goto fail_no_mem;
+
+ sqfs_object_init(file, stdio_destroy, stdio_copy);
+ memcpy(file->name, filename, namelen);
+
+ if (sqfs_open_native_file(&file->fd, filename, flags))
+ goto fail;
+
+ file_opened = true;
+ if (get_file_size(file->fd, &file->size))
+ goto fail;
+
+ file->readonly = (flags & SQFS_FILE_OPEN_READ_ONLY) != 0;
+
+ base->read_at = stdio_read_at;
+ base->write_at = stdio_write_at;
+ base->get_size = stdio_get_size;
+ base->truncate = stdio_truncate;
+ base->get_filename = stdio_get_filename;
+ return base;
+fail:
+ err = get_os_error_state();
+ if (file_opened)
+ sqfs_close_native_file(file->fd);
+ free(file);
+ set_os_error_state(err);
+ return NULL;
+fail_no_mem:
+#if defined(_WIN32) || defined(__WINDOWS__)
+ SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+#else
+ errno = ENOMEM;
+#endif
+ return NULL;
+}
diff --git a/lib/sqfs/src/io/istream.c b/lib/sqfs/src/io/istream.c
new file mode 100644
index 0000000..c3399fe
--- /dev/null
+++ b/lib/sqfs/src/io/istream.c
@@ -0,0 +1,203 @@
+/* SPDX-License-Identifier: LGPL-3.0-or-later */
+/*
+ * istream.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#define SQFS_BUILDING_DLL
+#include "config.h"
+
+#include "sqfs/io.h"
+#include "sqfs/error.h"
+#include "compat.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+
+#if defined(_WIN32) || defined(__WINDOWS__)
+static SSIZE_T read(HANDLE fd, void *ptr, size_t size)
+{
+ DWORD ret;
+
+ if (!ReadFile(fd, ptr, size, &ret, NULL)) {
+ os_error_t error = get_os_error_state();
+ set_os_error_state(error);
+
+ if (error.w32_errno == ERROR_HANDLE_EOF ||
+ error.w32_errno == ERROR_BROKEN_PIPE) {
+ return 0;
+ }
+
+ return -1;
+ }
+
+ return ret;
+}
+#endif
+
+#define BUFSZ (131072)
+
+typedef struct {
+ sqfs_istream_t base;
+ char *path;
+ sqfs_file_handle_t fd;
+
+ bool eof;
+ size_t buffer_offset;
+ size_t buffer_used;
+ sqfs_u8 buffer[BUFSZ];
+} file_istream_t;
+
+static int precache(sqfs_istream_t *strm)
+{
+ file_istream_t *file = (file_istream_t *)strm;
+
+ if (file->eof)
+ return 0;
+
+ if (file->buffer_offset > 0 &&
+ file->buffer_offset < file->buffer_used) {
+ memmove(file->buffer, file->buffer + file->buffer_offset,
+ file->buffer_used - file->buffer_offset);
+ }
+
+ file->buffer_used -= file->buffer_offset;
+ file->buffer_offset = 0;
+
+ while (file->buffer_used < BUFSZ) {
+ ssize_t ret = read(file->fd, file->buffer + file->buffer_used,
+ BUFSZ - file->buffer_used);
+
+ if (ret == 0) {
+ file->eof = true;
+ break;
+ }
+
+ if (ret < 0) {
+#if !defined(_WIN32) && !defined(__WINDOWS__)
+ if (errno == EINTR)
+ continue;
+#endif
+ return SQFS_ERROR_IO;
+ }
+
+ file->buffer_used += ret;
+ }
+
+ return 0;
+}
+
+static int file_get_buffered_data(sqfs_istream_t *strm, const sqfs_u8 **out,
+ size_t *size, size_t want)
+{
+ file_istream_t *file = (file_istream_t *)strm;
+
+ if (want > BUFSZ)
+ want = BUFSZ;
+
+ if (file->buffer_used == 0 ||
+ (file->buffer_used - file->buffer_offset) < want) {
+ int ret = precache(strm);
+ if (ret)
+ return ret;
+ }
+
+ *out = file->buffer + file->buffer_offset;
+ *size = file->buffer_used - file->buffer_offset;
+ return (file->eof && *size == 0) ? 1 : 0;
+}
+
+static void file_advance_buffer(sqfs_istream_t *strm, size_t count)
+{
+ file_istream_t *file = (file_istream_t *)strm;
+
+ assert(count <= file->buffer_used);
+
+ file->buffer_offset += count;
+
+ assert(file->buffer_offset <= file->buffer_used);
+}
+
+static const char *file_get_filename(sqfs_istream_t *strm)
+{
+ return ((file_istream_t *)strm)->path;
+}
+
+static void file_destroy(sqfs_object_t *obj)
+{
+ file_istream_t *file = (file_istream_t *)obj;
+
+ sqfs_close_native_file(file->fd);
+ free(file->path);
+ free(file);
+}
+
+int sqfs_istream_open_handle(sqfs_istream_t **out, const char *path,
+ sqfs_file_handle_t fd, sqfs_u32 flags)
+{
+ file_istream_t *file;
+ sqfs_istream_t *strm;
+ int ret;
+
+ if (flags & ~(SQFS_FILE_OPEN_ALL_FLAGS))
+ return SQFS_ERROR_UNSUPPORTED;
+
+ file = calloc(1, sizeof(*file));
+ strm = (sqfs_istream_t *)file;
+ if (file == NULL)
+ return SQFS_ERROR_ALLOC;
+
+ sqfs_object_init(file, file_destroy, NULL);
+
+ file->path = strdup(path);
+ if (file->path == NULL) {
+ os_error_t err = get_os_error_state();
+ free(file);
+ set_os_error_state(err);
+ return SQFS_ERROR_ALLOC;
+ }
+
+ ret = sqfs_duplicate_native_file(fd, &file->fd);
+ if (ret) {
+ os_error_t err = get_os_error_state();
+ free(file->path);
+ free(file);
+ set_os_error_state(err);
+ return ret;
+ }
+ sqfs_close_native_file(fd);
+
+ strm->get_buffered_data = file_get_buffered_data;
+ strm->advance_buffer = file_advance_buffer;
+ strm->get_filename = file_get_filename;
+
+ *out = strm;
+ return 0;
+}
+
+int sqfs_istream_open_file(sqfs_istream_t **out, const char *path,
+ sqfs_u32 flags)
+{
+ sqfs_file_handle_t fd;
+ int ret;
+
+ flags |= SQFS_FILE_OPEN_READ_ONLY;
+
+ if (flags & (SQFS_FILE_OPEN_OVERWRITE | SQFS_FILE_OPEN_NO_SPARSE))
+ return SQFS_ERROR_ARG_INVALID;
+
+ ret = sqfs_open_native_file(&fd, path, flags);
+ if (ret)
+ return ret;
+
+ ret = sqfs_istream_open_handle(out, path, fd, flags);
+ if (ret != 0) {
+ os_error_t err = get_os_error_state();
+ sqfs_close_native_file(fd);
+ set_os_error_state(err);
+ }
+
+ return ret;
+}
diff --git a/lib/sqfs/src/io/ostream.c b/lib/sqfs/src/io/ostream.c
new file mode 100644
index 0000000..ba8e29a
--- /dev/null
+++ b/lib/sqfs/src/io/ostream.c
@@ -0,0 +1,221 @@
+/* SPDX-License-Identifier: LGPL-3.0-or-later */
+/*
+ * ostream.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#define SQFS_BUILDING_DLL
+#include "config.h"
+
+#include "sqfs/io.h"
+#include "sqfs/error.h"
+#include "compat.h"
+
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+typedef struct {
+ sqfs_ostream_t base;
+ char *path;
+ sqfs_u32 flags;
+ sqfs_file_handle_t fd;
+
+ sqfs_u64 sparse_count;
+ sqfs_u64 size;
+} file_ostream_t;
+
+static int write_all(file_ostream_t *file, const sqfs_u8 *data, size_t size)
+{
+ while (size > 0) {
+#if defined(_WIN32) || defined(__WINDOWS__)
+ DWORD ret;
+ if (!WriteFile(file->fd, data, size, &ret, NULL))
+ return SQFS_ERROR_IO;
+#else
+ ssize_t ret = write(file->fd, data, size);
+
+ if (ret == 0) {
+ errno = EPIPE;
+ return SQFS_ERROR_IO;
+ }
+
+ if (ret < 0) {
+ if (errno == EINTR)
+ continue;
+ return SQFS_ERROR_IO;
+ }
+#endif
+ file->size += ret;
+ size -= ret;
+ data += ret;
+ }
+
+ return 0;
+}
+
+static int realize_sparse(file_ostream_t *file)
+{
+ unsigned char *buffer;
+ size_t diff, bufsz;
+ int ret;
+
+ if (file->sparse_count == 0)
+ return 0;
+
+ if (file->flags & SQFS_FILE_OPEN_NO_SPARSE) {
+ bufsz = file->sparse_count > 1024 ? 1024 : file->sparse_count;
+ buffer = calloc(1, bufsz);
+ if (buffer == NULL)
+ return SQFS_ERROR_ALLOC;
+
+ while (file->sparse_count > 0) {
+ diff = file->sparse_count > bufsz ?
+ bufsz : file->sparse_count;
+
+ ret = write_all(file, buffer, diff);
+ if (ret) {
+ os_error_t err = get_os_error_state();
+ free(buffer);
+ set_os_error_state(err);
+ return ret;
+ }
+
+ file->sparse_count -= diff;
+ }
+
+ free(buffer);
+ } else {
+ ret = sqfs_seek_native_file(file->fd, file->sparse_count,
+ SQFS_FILE_SEEK_CURRENT |
+ SQFS_FILE_SEEK_TRUNCATE);
+ if (ret)
+ return ret;
+
+ file->sparse_count = 0;
+ }
+
+ return 0;
+}
+
+static int file_append(sqfs_ostream_t *strm, const void *data, size_t size)
+{
+ file_ostream_t *file = (file_ostream_t *)strm;
+ int ret;
+
+ if (size == 0 || data == NULL) {
+ file->sparse_count += size;
+ file->size += size;
+ return 0;
+ }
+
+ ret = realize_sparse(file);
+ if (ret)
+ return ret;
+
+ return write_all(file, data, size);
+}
+
+static int file_flush(sqfs_ostream_t *strm)
+{
+ file_ostream_t *file = (file_ostream_t *)strm;
+ int ret;
+
+ ret = realize_sparse(file);
+ if (ret)
+ return ret;
+
+#if defined(_WIN32) || defined(__WINDOWS__)
+ if (!FlushFileBuffers(file->fd))
+ return SQFS_ERROR_IO;
+#else
+ if (fsync(file->fd) != 0 && errno != EINVAL)
+ return SQFS_ERROR_IO;
+#endif
+ return 0;
+}
+
+static void file_destroy(sqfs_object_t *obj)
+{
+ file_ostream_t *file = (file_ostream_t *)obj;
+
+ sqfs_close_native_file(file->fd);
+ free(file->path);
+ free(file);
+}
+
+static const char *file_get_filename(sqfs_ostream_t *strm)
+{
+ return ((file_ostream_t *)strm)->path;
+}
+
+int sqfs_ostream_open_handle(sqfs_ostream_t **out, const char *path,
+ sqfs_file_handle_t fd, sqfs_u32 flags)
+{
+ file_ostream_t *file;
+ sqfs_ostream_t *strm;
+ int ret;
+
+ *out = NULL;
+ if (flags & ~(SQFS_FILE_OPEN_ALL_FLAGS))
+ return SQFS_ERROR_UNSUPPORTED;
+
+ file = calloc(1, sizeof(*file));
+ strm = (sqfs_ostream_t *)file;
+ if (file == NULL)
+ return SQFS_ERROR_ALLOC;
+
+ sqfs_object_init(file, file_destroy, NULL);
+
+ file->path = strdup(path);
+ if (file->path == NULL) {
+ os_error_t err = get_os_error_state();
+ free(file);
+ set_os_error_state(err);
+ return SQFS_ERROR_ALLOC;
+ }
+
+ ret = sqfs_duplicate_native_file(fd, &file->fd);
+ if (ret) {
+ os_error_t err = get_os_error_state();
+ free(file->path);
+ free(file);
+ set_os_error_state(err);
+ return ret;
+ }
+
+ sqfs_close_native_file(fd);
+
+ file->flags = flags;
+ strm->append = file_append;
+ strm->flush = file_flush;
+ strm->get_filename = file_get_filename;
+
+ *out = strm;
+ return 0;
+}
+
+int sqfs_ostream_open_file(sqfs_ostream_t **out, const char *path,
+ sqfs_u32 flags)
+{
+ sqfs_file_handle_t fd;
+ int ret;
+
+ *out = NULL;
+ if (flags & SQFS_FILE_OPEN_READ_ONLY)
+ return SQFS_ERROR_ARG_INVALID;
+
+ ret = sqfs_open_native_file(&fd, path, flags);
+ if (ret)
+ return ret;
+
+ ret = sqfs_ostream_open_handle(out, path, fd, flags);
+ if (ret) {
+ os_error_t err = get_os_error_state();
+ sqfs_close_native_file(fd);
+ set_os_error_state(err);
+ return ret;
+ }
+
+ return 0;
+}
diff --git a/lib/sqfs/src/io/unix.c b/lib/sqfs/src/io/unix.c
new file mode 100644
index 0000000..45af2ce
--- /dev/null
+++ b/lib/sqfs/src/io/unix.c
@@ -0,0 +1,87 @@
+/* SPDX-License-Identifier: LGPL-3.0-or-later */
+/*
+ * unix.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#define SQFS_BUILDING_DLL
+#include "config.h"
+
+#include "sqfs/io.h"
+#include "sqfs/error.h"
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+int sqfs_open_native_file(sqfs_file_handle_t *out, const char *filename,
+ sqfs_u32 flags)
+{
+ int open_mode;
+
+ *out = -1;
+
+ if (flags & ~SQFS_FILE_OPEN_ALL_FLAGS)
+ return SQFS_ERROR_UNSUPPORTED;
+
+ if (flags & SQFS_FILE_OPEN_READ_ONLY) {
+ open_mode = O_RDONLY;
+ } else {
+ open_mode = O_CREAT | O_RDWR;
+
+ if (flags & SQFS_FILE_OPEN_OVERWRITE) {
+ open_mode |= O_TRUNC;
+ } else {
+ open_mode |= O_EXCL;
+ }
+ }
+
+ *out = open(filename, open_mode, 0644);
+
+ return (*out < 0) ? SQFS_ERROR_IO : 0;
+}
+
+void sqfs_close_native_file(sqfs_file_handle_t fd)
+{
+ while (close(fd) != 0 && errno == EINTR)
+ ;
+}
+
+int sqfs_duplicate_native_file(sqfs_file_handle_t in, sqfs_file_handle_t *out)
+{
+ *out = dup(in);
+ return (*out < 0) ? SQFS_ERROR_IO : 0;
+}
+
+int sqfs_seek_native_file(sqfs_file_handle_t fd,
+ sqfs_s64 offset, sqfs_u32 flags)
+{
+ int whence;
+ off_t off;
+
+ switch (flags & SQFS_FILE_SEEK_TYPE_MASK) {
+ case SQFS_FILE_SEEK_START: whence = SEEK_SET; break;
+ case SQFS_FILE_SEEK_CURRENT: whence = SEEK_CUR; break;
+ case SQFS_FILE_SEEK_END: whence = SEEK_END; break;
+ default: return SQFS_ERROR_UNSUPPORTED;
+ }
+
+ if (flags & ~(SQFS_FILE_SEEK_FLAG_MASK | SQFS_FILE_SEEK_TYPE_MASK))
+ return SQFS_ERROR_UNSUPPORTED;
+
+ off = lseek(fd, offset, whence);
+ if (off == ((off_t)-1)) {
+ if (errno == ESPIPE)
+ return SQFS_ERROR_UNSUPPORTED;
+ return SQFS_ERROR_IO;
+ }
+
+ if (flags & SQFS_FILE_SEEK_TRUNCATE) {
+ while (ftruncate(fd, off) != 0) {
+ if (errno != EINTR)
+ return SQFS_ERROR_IO;
+ }
+ }
+
+ return 0;
+}
diff --git a/lib/sqfs/src/io/win32.c b/lib/sqfs/src/io/win32.c
new file mode 100644
index 0000000..decfd23
--- /dev/null
+++ b/lib/sqfs/src/io/win32.c
@@ -0,0 +1,120 @@
+/* SPDX-License-Identifier: LGPL-3.0-or-later */
+/*
+ * win32.c
+ *
+ * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
+ */
+#define SQFS_BUILDING_DLL
+#include "config.h"
+
+#include "sqfs/io.h"
+#include "sqfs/error.h"
+#include "compat.h"
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+int sqfs_open_native_file(sqfs_file_handle_t *out, const char *filename,
+ sqfs_u32 flags)
+{
+ int access_flags, creation_mode, share_mode;
+ WCHAR *wpath = NULL;
+ os_error_t err;
+ DWORD length;
+
+ *out = INVALID_HANDLE_VALUE;
+
+ if (flags & ~SQFS_FILE_OPEN_ALL_FLAGS) {
+ SetLastError(ERROR_INVALID_PARAMETER);
+ return SQFS_ERROR_UNSUPPORTED;
+ }
+
+ if (flags & SQFS_FILE_OPEN_READ_ONLY) {
+ access_flags = GENERIC_READ;
+ creation_mode = OPEN_EXISTING;
+ share_mode = FILE_SHARE_READ;
+ } else {
+ access_flags = GENERIC_READ | GENERIC_WRITE;
+ share_mode = 0;
+
+ if (flags & SQFS_FILE_OPEN_OVERWRITE) {
+ creation_mode = CREATE_ALWAYS;
+ } else {
+ creation_mode = CREATE_NEW;
+ }
+ }
+
+ if (flags & SQFS_FILE_OPEN_NO_CHARSET_XFRM) {
+ *out = CreateFileA(filename, access_flags, share_mode, NULL,
+ creation_mode, FILE_ATTRIBUTE_NORMAL, NULL);
+ } else {
+ length = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);
+ if (length <= 0)
+ return SQFS_ERROR_INTERNAL;
+
+ wpath = calloc(sizeof(wpath[0]), length + 1);
+ if (wpath == NULL) {
+ SetLastError(ERROR_NOT_ENOUGH_MEMORY);
+ return SQFS_ERROR_ALLOC;
+ }
+
+ MultiByteToWideChar(CP_UTF8, 0, filename, -1,
+ wpath, length + 1);
+ wpath[length] = '\0';
+
+ *out = CreateFileW(wpath, access_flags, share_mode, NULL,
+ creation_mode, FILE_ATTRIBUTE_NORMAL,
+ NULL);
+
+ err = get_os_error_state();
+ free(wpath);
+ set_os_error_state(err);
+ }
+
+ return (*out == INVALID_HANDLE_VALUE) ? SQFS_ERROR_IO : 0;
+}
+
+void sqfs_close_native_file(sqfs_file_handle_t hnd)
+{
+ CloseHandle(hnd);
+}
+
+int sqfs_duplicate_native_file(sqfs_file_handle_t in, sqfs_file_handle_t *out)
+{
+ HANDLE hProc = GetCurrentProcess();
+ BOOL ret = DuplicateHandle(hProc, in, hProc, out, 0,
+ FALSE, DUPLICATE_SAME_ACCESS);
+
+ return ret ? 0 : SQFS_ERROR_IO;
+}
+
+int sqfs_seek_native_file(sqfs_file_handle_t fd,
+ sqfs_s64 offset, sqfs_u32 flags)
+{
+ LARGE_INTEGER pos;
+ DWORD whence;
+
+ switch (type & SQFS_FILE_SEEK_TYPE_MASK) {
+ case SQFS_FILE_SEEK_START: whence = FILE_BEGIN; break;
+ case SQFS_FILE_SEEK_CURRENT: whence = FILE_CURRENT; break;
+ case SQFS_FILE_SEEK_END: whence = FILE_END; break;
+ default: return SQFS_ERROR_UNSUPPORTED;
+ }
+
+ if (flags & ~(SQFS_FILE_SEEK_FLAG_MASK | SQFS_FILE_SEEK_TYPE_MASK))
+ return SQFS_ERROR_UNSUPPORTED;
+
+ if (GetFileType(fd) != FILE_TYPE_DISK)
+ return SQFS_ERROR_UNSUPPORTED;
+
+ pos.QuadPart = offset;
+ if (!SetFilePointerEx(fd, pos, NULL, whence))
+ return SQFS_ERROR_IO;
+
+ if (flags & SQFS_FILE_SEEK_TRUNCATE) {
+ if (!SetEndOfFile(fd))
+ return SQFS_ERROR_IO;
+ }
+
+ return 0;
+}