From f5b6a9db5c318135ed5dec04aaa92252bcba5584 Mon Sep 17 00:00:00 2001 From: yexo Date: Sun, 15 Jan 2012 11:31:34 +0000 Subject: (svn r23801) -Fix: reading the utf-8 BOM from AI/GS files on big-endian machines failed --- src/3rdparty/squirrel/include/squirrel.h | 2 +- src/3rdparty/squirrel/squirrel/sqlexer.h | 6 +----- src/script/squirrel.cpp | 14 ++++++++++---- 3 files changed, 12 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/3rdparty/squirrel/include/squirrel.h b/src/3rdparty/squirrel/include/squirrel.h index 3be425304..1fe023dff 100644 --- a/src/3rdparty/squirrel/include/squirrel.h +++ b/src/3rdparty/squirrel/include/squirrel.h @@ -183,7 +183,7 @@ typedef char SQChar; #define scstrdup strdup #define scstrrchr strrchr #define scstrcat strcat -#define MAX_CHAR 0xFF +#define MAX_CHAR 0xFFFF #endif #define SQUIRREL_VERSION _SC("Squirrel 2.2.5 stable - With custom OpenTTD modifications") diff --git a/src/3rdparty/squirrel/squirrel/sqlexer.h b/src/3rdparty/squirrel/squirrel/sqlexer.h index 4b5ac203c..1d81ab3c8 100644 --- a/src/3rdparty/squirrel/squirrel/sqlexer.h +++ b/src/3rdparty/squirrel/squirrel/sqlexer.h @@ -2,11 +2,7 @@ #ifndef _SQLEXER_H_ #define _SQLEXER_H_ -#ifdef SQUNICODE -typedef SQChar LexChar; -#else -typedef unsigned char LexChar; -#endif +typedef unsigned short LexChar; struct SQLexer { 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")); -- cgit v1.2.3-70-g09d2