From 2b8eb19a4de81db470f7ff5021a8a4822cc8c80d Mon Sep 17 00:00:00 2001
From: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
Date: Sun, 28 Nov 2021 00:05:24 +0100
Subject: Fix: consistently use the widechar file API on Windows

When opening files on windows, use the widechar versions and convert
from (assumed) UTF-8 to UTF-16 as needed.

Since the broken, code-page-random API may acutall be intended in some
use cases, leave that option in through an additional flag.

Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
---
 include/sqfs/io.h | 27 +++++++++++++++++++++++++--
 1 file changed, 25 insertions(+), 2 deletions(-)

(limited to 'include/sqfs')

diff --git a/include/sqfs/io.h b/include/sqfs/io.h
index 246ac70..4cdb574 100644
--- a/include/sqfs/io.h
+++ b/include/sqfs/io.h
@@ -54,7 +54,29 @@ typedef enum {
 	 */
 	SQFS_FILE_OPEN_OVERWRITE = 0x02,
 
-	SQFS_FILE_OPEN_ALL_FLAGS = 0x03,
+	/**
+	 * @brief If set, do not try to apply any character set transformations
+	 *        to the file path.
+	 *
+	 * This flag instructs the @ref sqfs_open_file API to pass the file
+	 * path to the OS dependent API as-is and not to attempt any character
+	 * set transformation. By default, if the underlying OS has a concept
+	 * of locale dependent file paths, the input path is UTF-8 and it is
+	 * transformed as needed, in order to retain a level of portabillity
+	 * and sanity.
+	 *
+	 * This flag currently only affects the Windows implementation. On
+	 * Unix-like systems, the path is always passed to the OS API as-is
+	 * and this flag has no effect.
+	 *
+	 * On Windows, the input path is converted from UTF-8 to UTF-16 and
+	 * then passed on to the wide-char based file API. If this flag is set,
+	 * the path is used as-is and passed on to the 8-bit codepage-whatever
+	 * API instead.
+	 */
+	SQFS_FILE_OPEN_NO_CHARSET_XFRM = 0x04,
+
+	SQFS_FILE_OPEN_ALL_FLAGS = 0x07,
 } SQFS_FILE_OPEN_FLAGS;
 
 /**
@@ -134,7 +156,8 @@ extern "C" {
  * API for file I/O.
  *
  * On Unix-like systems, if the open call fails, this function makes sure to
- * preserves the value in errno indicating the underlying problem.
+ * preserves the value in errno indicating the underlying problem. Similarly,
+ * on Windows, the implementation tries to preserve the GetLastError value.
  *
  * @param filename The name of the file to open.
  * @param flags A set of @ref SQFS_FILE_OPEN_FLAGS.
-- 
cgit v1.2.3