summaryrefslogtreecommitdiff
path: root/src/script/squirrel.cpp
diff options
context:
space:
mode:
authoryexo <yexo@openttd.org>2012-01-15 11:31:34 +0000
committeryexo <yexo@openttd.org>2012-01-15 11:31:34 +0000
commitf5b6a9db5c318135ed5dec04aaa92252bcba5584 (patch)
tree572e2ef1bd60e8bf7ec8886ac5bede5d48700dd5 /src/script/squirrel.cpp
parent48717ade9cc1e59d07d8b8d6fe4c552b6bf09043 (diff)
downloadopenttd-f5b6a9db5c318135ed5dec04aaa92252bcba5584.tar.xz
(svn r23801) -Fix: reading the utf-8 BOM from AI/GS files on big-endian machines failed
Diffstat (limited to 'src/script/squirrel.cpp')
-rw-r--r--src/script/squirrel.cpp14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/script/squirrel.cpp b/src/script/squirrel.cpp
index da5ed47c2..1f4ce7ae9 100644
--- a/src/script/squirrel.cpp
+++ b/src/script/squirrel.cpp
@@ -413,14 +413,14 @@ static SQInteger _io_file_lexfeed_UTF8(SQUserPointer file)
return c;
}
-static SQInteger _io_file_lexfeed_UCS2_LE(SQUserPointer file)
+static SQInteger _io_file_lexfeed_UCS2_no_swap(SQUserPointer file)
{
wchar_t c;
if (((SQFile *)file)->Read(&c, sizeof(c), 1) > 0) return (SQChar)c;
return 0;
}
-static SQInteger _io_file_lexfeed_UCS2_BE(SQUserPointer file)
+static SQInteger _io_file_lexfeed_UCS2_swap(SQUserPointer file)
{
unsigned short c;
if (((SQFile *)file)->Read(&c, sizeof(c), 1) > 0) {
@@ -472,9 +472,15 @@ SQRESULT Squirrel::LoadFile(HSQUIRRELVM vm, const char *filename, SQBool printer
FioFCloseFile(file);
return sq_throwerror(vm, _SC("Couldn't read bytecode"));
}
- case 0xFFFE: func = _io_file_lexfeed_UCS2_BE; break; // UTF-16 little endian
- case 0xFEFF: func = _io_file_lexfeed_UCS2_LE; break; // UTF-16 big endian
+ case 0xFFFE:
+ /* Either this file is encoded as big-endian and we're on a little-endian
+ * machine, or this file is encoded as little-endian and we're on a big-endian
+ * machine. Either way, swap the bytes of every word we read. */
+ func = _io_file_lexfeed_UCS2_swap;
+ break;
+ case 0xFEFF: func = _io_file_lexfeed_UCS2_no_swap; break;
case 0xBBEF: // UTF-8
+ case 0xEFBB: // UTF-8 on big-endian machine
if (fread(&uc, 1, sizeof(uc), file) == 0) {
FioFCloseFile(file);
return sq_throwerror(vm, _SC("I/O error"));