From 521f1356c85db788c25f7704906c2a752cf4a14b Mon Sep 17 00:00:00 2001 From: Daniel Collins Date: Thu, 22 Sep 2022 23:48:14 +0100 Subject: [PATCH] [HTML5] Don't load text files as binary data (fixes GM-5814). --- scripts/yyBuffer.js | 10 ++++----- scripts/yyIniFile.js | 52 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/scripts/yyBuffer.js b/scripts/yyBuffer.js index 7e7f75e3..cbac6f56 100644 --- a/scripts/yyBuffer.js +++ b/scripts/yyBuffer.js @@ -2393,7 +2393,7 @@ function buffer_load_async(_buffer, _fname, _offset, _size) { if (!pBuff) return -1; // Try and load from local storage first - var pTextFile = LoadTextFile_Block(_fname, true); + var pTextFile = LoadBinaryFile_Block(_fname, true); if (pTextFile) { if (_size >= 0 && pTextFile.length > _size) @@ -2447,10 +2447,10 @@ function buffer_load(_fname) { _fname = yyGetString(_fname); var shouldDecode = true; - var pTextFile = LoadTextFile_Block(_fname, true); + var pTextFile = LoadBinaryFile_Block(_fname, true); if (pTextFile == null) { - pTextFile = LoadTextFile_Block(_fname, false); + pTextFile = LoadBinaryFile_Block(_fname, false); shouldDecode = false; } if (pTextFile == null) return -1; @@ -2528,10 +2528,10 @@ function buffer_load_partial(buffer, filename, src_offset, len, dst_offset) { var shouldDecode = true; - var pTextFile = LoadTextFile_Block(filename, true); + var pTextFile = LoadBinaryFile_Block(filename, true); if (pTextFile == null) { - pTextFile = LoadTextFile_Block(filename, false); + pTextFile = LoadBinaryFile_Block(filename, false); shouldDecode = false; } if (pTextFile == null) return -1; diff --git a/scripts/yyIniFile.js b/scripts/yyIniFile.js index 8e54fb29..7e369578 100644 --- a/scripts/yyIniFile.js +++ b/scripts/yyIniFile.js @@ -722,7 +722,7 @@ function RawFileExists(_url) { // ############################################################################################# /// Function: -/// Read/Write a file from a server +/// Read/Write a BINARY file from a server /// /// /// In: file or URL to access @@ -747,6 +747,30 @@ function RawServerReadWrite(U, V) } } +// ############################################################################################# +/// Function: +/// Read/Write a TEXT file from a server +/// +/// +/// In: file or URL to access +/// false for GET, true for POST +/// Out: +/// the file, or null if an error occured. +/// +// ############################################################################################# +function TextServerReadWrite(U, V) +{ + try{ + var X = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest(); + X.open(V ? 'PUT' : 'GET', U, false ); + X.send(V ? V : ''); + g_LastErrorStatus = X.status; + return X.responseText; + }catch(e){ + return null; + } +} + // ############################################################################################# /// Function: @@ -815,7 +839,28 @@ function SaveTextFile_Block(_filename, _pFile) /// // ############################################################################################# function LoadTextFile_Block( _FileName, _fLocal ) -{ +{ + return LoadXXXFile_Block( _FileName, _fLocal, TextServerReadWrite ); +} + +// ############################################################################################# +/// Function: +/// Load a BINARY file from local, OR remote (blocking) +/// +/// +/// In: +/// +/// Out: +/// +/// +// ############################################################################################# +function LoadBinaryFile_Block( _FileName, _fLocal ) +{ + return LoadXXXFile_Block( _FileName, _fLocal, RawServerReadWrite ); +} + +function LoadXXXFile_Block( _FileName, _fLocal, _ServerReadWriteFunc ) +{ var pFile = null; if (_FileName.substring(0, 5) == "file:") return null; @@ -846,14 +891,13 @@ function LoadTextFile_Block( _FileName, _fLocal ) _FileName = CheckWorkingDirectory(_FileName); - pFile = RawServerReadWrite(_FileName, false); + pFile = _ServerReadWriteFunc(_FileName, false); if( ( pFile ==null ) || (pFile==undefined ) ) return null; if (g_LastErrorStatus == 404) return null; } return pFile; } - // ############################################################################################# /// Function: /// Load a TEXT file from local, OR remote (blocking)