From b70b9c626746fb3cb6622d14c474312b22a2fdf5 Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Mon, 23 Nov 2026 21:37:03 +0000 Subject: [PATCH 1/4] Add the buffer_utf8 data type and an optional size for buffer_read and buffer_peek (GameMaker-Bugs#16485) buffer_utf8 reads and peeks one UTF-8 character as its code point (U+FFFD and one byte for a malformed sequence) and writes and pokes a code point as UTF-8. A size for buffer_text / buffer_string reads exactly that many bytes, up to the end of the buffer. The text decode is shared through yyDecodeUTF8, which also stops it reading past the end of the data. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/yyBuffer.js | 212 ++++++++++++++++++++++++++------------------ 1 file changed, 125 insertions(+), 87 deletions(-) diff --git a/scripts/yyBuffer.js b/scripts/yyBuffer.js index cb0fdc23..b332d948 100644 --- a/scripts/yyBuffer.js +++ b/scripts/yyBuffer.js @@ -40,7 +40,8 @@ var eBuffer_None = 0, eBuffer_Bool = 10, eBuffer_String = 11, eBuffer_U64 = 12, - eBuffer_Text = 13; + eBuffer_Text = 13, + eBuffer_UTF8 = 14; var eBuffer_Start = 0, @@ -500,6 +501,71 @@ function UnicodeToUTF8(_str) { } +// ############################################################################################# +/// UTF-8 helpers for buffer_utf8 and the buffer_text / buffer_string reads (GameMaker-Bugs#16485) +// ############################################################################################# +var g_UTF8DecodeLength = 1; // bytes taken by the last yyDecodeUTF8 +var g_UTF8DecodeStop = 0; // where the last yyb_decodestring stopped (after its zero byte, or the end) + +// Decodes one UTF-8 character at _offset without reading at or past _end. Returns the code point, or U+FFFD +// for a malformed sequence (a continuation byte or a byte that can't start a character, a missing or bad +// continuation byte, an overlong encoding, a surrogate, a value above U+10FFFF, or a sequence cut off by +// _end). g_UTF8DecodeLength is set to the bytes the character takes, which is 1 for a malformed sequence. +function yyDecodeUTF8(_dataView, _offset, _end) { + var b0 = _dataView.getUint8(_offset); + g_UTF8DecodeLength = 1; + if (b0 < 0x80) return b0; + + var len, cp, min; + if ((b0 >= 0xC2) && (b0 <= 0xDF)) { len = 2; cp = b0 & 0x1F; min = 0x80; } + else if ((b0 >= 0xE0) && (b0 <= 0xEF)) { len = 3; cp = b0 & 0x0F; min = 0x800; } + else if ((b0 >= 0xF0) && (b0 <= 0xF4)) { len = 4; cp = b0 & 0x07; min = 0x10000; } + else return 0xFFFD; + + if ((_offset + len) > _end) return 0xFFFD; + for (var i = 1; i < len; i++) { + var b = _dataView.getUint8(_offset + i); + if ((b & 0xC0) != 0x80) return 0xFFFD; + cp = (cp << 6) | (b & 0x3F); + } + if ((cp < min) || (cp > 0x10FFFF) || ((cp >= 0xD800) && (cp <= 0xDFFF))) return 0xFFFD; + + g_UTF8DecodeLength = len; + return cp; +} + +// The UTF-8 bytes of a code point; a value that isn't a code point (negative, a surrogate or above +// U+10FFFF) gives the bytes of U+FFFD +function yyEncodeUTF8(_value) { + var cp = Math.trunc(yyGetReal(_value)); + if (!(cp >= 0) || (cp > 0x10FFFF) || ((cp >= 0xD800) && (cp <= 0xDFFF))) cp = 0xFFFD; + + if (cp < 0x80) return [cp]; + if (cp < 0x800) return [0xC0 | (cp >> 6), 0x80 | (cp & 0x3F)]; + if (cp < 0x10000) return [0xE0 | (cp >> 12), 0x80 | ((cp >> 6) & 0x3F), 0x80 | (cp & 0x3F)]; + return [0xF0 | (cp >> 18), 0x80 | ((cp >> 12) & 0x3F), 0x80 | ((cp >> 6) & 0x3F), 0x80 | (cp & 0x3F)]; +} + +// Decodes the UTF-8 bytes from _start up to the first zero byte or _end into a string; +// g_UTF8DecodeStop is set to the index after the zero byte, or _end +yyBuffer.prototype.yyb_decodestring = function(_start, _end) { + var res = ""; + var idx = _start; + while (idx < _end) { + var cp = yyDecodeUTF8(this.m_DataView, idx, _end); + idx += g_UTF8DecodeLength; + if (cp == 0) break; + if (cp >= 0x10000) { + res += String.fromCharCode((cp >> 10) + 0xD7C0) + String.fromCharCode((cp & 0x3FF) | 0xDC00); + } else { + res += String.fromCharCode(cp); + } + } + g_UTF8DecodeStop = idx; + return res; +}; + + // ############################################################################################# /// Function: /// Return the size of a "type" @@ -510,7 +576,7 @@ function UnicodeToUTF8(_str) { /// size or -1 for error /// // ############################################################################################# -yyBuffer.prototype.yyb_read = function(_type) { +yyBuffer.prototype.yyb_read = function(_type, _size) { // Deal with basic alignment first @@ -524,11 +590,18 @@ yyBuffer.prototype.yyb_read = function(_type) { } } + // A size reads that many bytes of a buffer_text / buffer_string (GameMaker-Bugs#16485) + var sized = ((_type == eBuffer_String) || (_type == eBuffer_Text)) && (_size !== undefined) && (_size >= 0); + // Out of space? if (this.m_BufferIndex >= this.m_Size) return (_type==eBuffer_String) ? "" : eBuffer_OutOfBounds; var res; switch (_type) { + case eBuffer_UTF8: + res = yyDecodeUTF8(this.m_DataView, this.m_BufferIndex, this.m_Size); + this.m_BufferIndex += g_UTF8DecodeLength; + break; case eBuffer_Bool: res = this.m_DataView.getUint8(this.m_BufferIndex++); if (res == 1) { @@ -544,45 +617,14 @@ yyBuffer.prototype.yyb_read = function(_type) { break; case eBuffer_String: case eBuffer_Text: - { - res = ""; - var chr; - var chrCode = 0; - - while(this.m_BufferIndex> 10) + 0xD7C0) + String.fromCharCode((v & 0x3FF) | 0xDC00); - } - if (v == 0x00) break; - if(chr < 0) chr = String.fromCharCode(v); - res += chr; - } + if (sized) { + // exactly _size bytes (up to the end of the buffer), the string stopping at a zero byte + var spanEnd = Math.min(this.m_BufferIndex + _size, this.m_Size); + res = this.yyb_decodestring(this.m_BufferIndex, spanEnd); + this.m_BufferIndex = spanEnd; + } else { + res = this.yyb_decodestring(this.m_BufferIndex, this.m_UsedSize); + this.m_BufferIndex = g_UTF8DecodeStop; } break; case eBuffer_S8: @@ -1333,6 +1375,15 @@ yyBuffer.prototype.yyb_write = function(_type, _value) { sizeneeded = UTF8_String.length; if( _type === eBuffer_String ) sizeneeded++; // null at the end of a string (not text) } + var UTF8_Bytes; + if (_type === eBuffer_UTF8) { + UTF8_Bytes = yyEncodeUTF8(_value); + sizeneeded = UTF8_Bytes.length; + // a character isn't written across the end of a wrap buffer + if ((this.m_Type == eBuffer_Format_Wrap) && ((this.m_BufferIndex + sizeneeded) > this.m_Size)) { + return eBuffer_OutOfSpace; + } + } if ((this.m_BufferIndex + sizeneeded) > this.m_Size) { // Resize buffer... @@ -1371,6 +1422,12 @@ yyBuffer.prototype.yyb_write = function(_type, _value) { } break; + case eBuffer_UTF8: + for (var u = 0; u < UTF8_Bytes.length; u++) { + this.m_DataView.setUint8(this.m_BufferIndex++, UTF8_Bytes[u]); + } + break; + case eBuffer_String: case eBuffer_Text: { @@ -1439,7 +1496,7 @@ yyBuffer.prototype.yyb_write = function(_type, _value) { /// Function: /// // ############################################################################################# -yyBuffer.prototype.yyb_peek = function(_type, _offset) { +yyBuffer.prototype.yyb_peek = function(_type, _offset, _size) { var size = BufferSizeOf(_type); @@ -1458,6 +1515,11 @@ yyBuffer.prototype.yyb_peek = function(_type, _offset) { var res; switch (_type) { + case eBuffer_UTF8: + if (_offset >= this.m_Size) + return undefined; // can't read off the end of the buffer + res = yyDecodeUTF8(this.m_DataView, _offset, this.m_Size); + break; case eBuffer_Bool: res = this.m_DataView.getUint8(_offset); if (res == 1) { @@ -1506,47 +1568,11 @@ yyBuffer.prototype.yyb_peek = function(_type, _offset) { break; case eBuffer_String: case eBuffer_Text: - { - res = ""; - var chr; - var chrCode = 0; - - var currOffset = _offset; - - while(currOffset> 10) + 0xD7C0) + String.fromCharCode((v & 0x3FF) | 0xDC00); - } - if (v == 0x00) break; - if(chr < 0) chr = String.fromCharCode(v); - res += chr; - } + if ((_size !== undefined) && (_size >= 0)) { + // exactly _size bytes (up to the end of the buffer), the string stopping at a zero byte (GameMaker-Bugs#16485) + res = this.yyb_decodestring(_offset, Math.min(_offset + _size, this.m_Size)); + } else { + res = this.yyb_decodestring(_offset, this.m_UsedSize); } break; @@ -1744,6 +1770,18 @@ yyBuffer.prototype.yyb_poke = function(_type, _offset, _value) { switch (_type) { + case eBuffer_UTF8: + { + var UTF8_Bytes = yyEncodeUTF8(_value); + if ((_offset + UTF8_Bytes.length) > this.m_Size) + return 0; // can't write off the end of the buffer + for (var u = 0; u < UTF8_Bytes.length; u++) { + this.m_DataView.setUint8(_offset + u, UTF8_Bytes[u]); + } + this.yyb_UpdateUsedSize(_offset + UTF8_Bytes.length); + } + return; + case eBuffer_Bool: if (_value == true) { _value = 1; @@ -1852,22 +1890,22 @@ function buffer_seek(buffer, base, offset) { /// Function: /// // ############################################################################################# -function buffer_read(buffer, type) { +function buffer_read(buffer, type, size) { var pBuff = g_BufferStorage.Get(yyGetInt32(buffer)); if (!pBuff) return 0; - return pBuff.yyb_read(yyGetInt32(type)); + return pBuff.yyb_read(yyGetInt32(type), (size === undefined) ? undefined : yyGetInt32(size)); } // ############################################################################################# /// Function: /// // ############################################################################################# -function buffer_peek(buffer, offset, type) { +function buffer_peek(buffer, offset, type, size) { var pBuff = g_BufferStorage.Get(yyGetInt32(buffer)); if (!pBuff) return 0; - return pBuff.yyb_peek(yyGetInt32(type), yyGetInt32(offset)); + return pBuff.yyb_peek(yyGetInt32(type), yyGetInt32(offset), (size === undefined) ? undefined : yyGetInt32(size)); } // ############################################################################################# From 0bfa27c07ae6ec89d40b787b96e98f3c79548550 Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Mon, 23 Nov 2026 22:01:26 +0000 Subject: [PATCH 2/4] buffer_utf8 follow-ups: no buffer_fill, writes across the end of a wrap buffer, argument count checks, size 0 at the end buffer_fill with buffer_utf8 does nothing (as on the C++ runner and GMRT), and a type with no size (an empty buffer_text) no longer loops forever. buffer_utf8 writes carry on at the start of a wrap buffer like the other writes. buffer_read and buffer_peek raise an error outside 2-3 and 3-4 arguments. A size of 0 reads an empty string without moving, even at the end of the buffer. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/yyBuffer.js | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/scripts/yyBuffer.js b/scripts/yyBuffer.js index b332d948..36172973 100644 --- a/scripts/yyBuffer.js +++ b/scripts/yyBuffer.js @@ -578,6 +578,10 @@ yyBuffer.prototype.yyb_decodestring = function(_start, _end) { // ############################################################################################# yyBuffer.prototype.yyb_read = function(_type, _size) { + // A size reads that many bytes of a buffer_text / buffer_string (GameMaker-Bugs#16485) + var sized = ((_type == eBuffer_String) || (_type == eBuffer_Text)) && (_size !== undefined) && (_size >= 0); + // a size of 0 reads an empty string without moving, even at the end of the buffer + if (sized && (_size == 0)) return ""; // Deal with basic alignment first //m_BufferIndex = (m_BufferIndex + (m_Alignment-1)) & ~(m_Alignment-1); // align @@ -590,9 +594,6 @@ yyBuffer.prototype.yyb_read = function(_type, _size) { } } - // A size reads that many bytes of a buffer_text / buffer_string (GameMaker-Bugs#16485) - var sized = ((_type == eBuffer_String) || (_type == eBuffer_Text)) && (_size !== undefined) && (_size >= 0); - // Out of space? if (this.m_BufferIndex >= this.m_Size) return (_type==eBuffer_String) ? "" : eBuffer_OutOfBounds; @@ -1379,10 +1380,6 @@ yyBuffer.prototype.yyb_write = function(_type, _value) { if (_type === eBuffer_UTF8) { UTF8_Bytes = yyEncodeUTF8(_value); sizeneeded = UTF8_Bytes.length; - // a character isn't written across the end of a wrap buffer - if ((this.m_Type == eBuffer_Format_Wrap) && ((this.m_BufferIndex + sizeneeded) > this.m_Size)) { - return eBuffer_OutOfSpace; - } } if ((this.m_BufferIndex + sizeneeded) > this.m_Size) { @@ -1423,8 +1420,13 @@ yyBuffer.prototype.yyb_write = function(_type, _value) { break; case eBuffer_UTF8: + // in a wrap buffer the bytes carry on from the start, as for the other runners (reads never decode across the end) for (var u = 0; u < UTF8_Bytes.length; u++) { this.m_DataView.setUint8(this.m_BufferIndex++, UTF8_Bytes[u]); + if ((this.m_Type == eBuffer_Format_Wrap) && (this.m_BufferIndex >= this.m_Size)) { + this.yyb_calcnextalignmentoffset(); + this.m_BufferIndex = 0; + } } break; @@ -1626,7 +1628,10 @@ yyBuffer.prototype.yyb_fill = function(_offset, _size, _type, _value, _stride, _ if( _type === eBuffer_String ) typesize++; // null at the end of a string (not text) } - + // buffer_utf8 isn't filled (as on the other runners), and a type with no size (e.g. an empty buffer_text) + // would never advance the loop below (GameMaker-Bugs#16485) + if ((_type === eBuffer_UTF8) || (typesize <= 0)) + return; var bAlign; if (_stride <= 0) @@ -1892,6 +1897,10 @@ function buffer_seek(buffer, base, offset) { // ############################################################################################# function buffer_read(buffer, type, size) { + if ((arguments.length < 2) || (arguments.length > 3)) { + yyError("buffer_read: expected 2 or 3 arguments, got " + arguments.length); + return 0; + } var pBuff = g_BufferStorage.Get(yyGetInt32(buffer)); if (!pBuff) return 0; return pBuff.yyb_read(yyGetInt32(type), (size === undefined) ? undefined : yyGetInt32(size)); @@ -1903,6 +1912,10 @@ function buffer_read(buffer, type, size) { // ############################################################################################# function buffer_peek(buffer, offset, type, size) { + if ((arguments.length < 3) || (arguments.length > 4)) { + yyError("buffer_peek: expected 3 or 4 arguments, got " + arguments.length); + return undefined; + } var pBuff = g_BufferStorage.Get(yyGetInt32(buffer)); if (!pBuff) return 0; return pBuff.yyb_peek(yyGetInt32(type), yyGetInt32(offset), (size === undefined) ? undefined : yyGetInt32(size)); From e0ad0db37bbc57ab5da585c16b164b2cd6ddfc9b Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Mon, 23 Nov 2026 22:03:12 +0000 Subject: [PATCH 3/4] buffer_poke with buffer_utf8 writes across the end of a wrap buffer As on the C++ runner, a poke into a wrap buffer carries on at offset 0; on other buffers a character that doesn't fit isn't written. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/yyBuffer.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/scripts/yyBuffer.js b/scripts/yyBuffer.js index 36172973..906b6317 100644 --- a/scripts/yyBuffer.js +++ b/scripts/yyBuffer.js @@ -1778,12 +1778,18 @@ yyBuffer.prototype.yyb_poke = function(_type, _offset, _value) { case eBuffer_UTF8: { var UTF8_Bytes = yyEncodeUTF8(_value); - if ((_offset + UTF8_Bytes.length) > this.m_Size) + if ((this.m_Type != eBuffer_Format_Wrap) && ((_offset + UTF8_Bytes.length) > this.m_Size)) return 0; // can't write off the end of the buffer + // in a wrap buffer the bytes carry on at offset 0, as for the other runners + var pokeOffset = _offset; for (var u = 0; u < UTF8_Bytes.length; u++) { - this.m_DataView.setUint8(_offset + u, UTF8_Bytes[u]); + this.m_DataView.setUint8(pokeOffset++, UTF8_Bytes[u]); + if (pokeOffset >= this.m_Size) { + this.yyb_UpdateUsedSize(pokeOffset); + pokeOffset = 0; + } } - this.yyb_UpdateUsedSize(_offset + UTF8_Bytes.length); + this.yyb_UpdateUsedSize(pokeOffset); } return; From 895e59b6d9f2b13f4fca136b081e2e033207e98d Mon Sep 17 00:00:00 2001 From: Russell Kay Date: Mon, 23 Nov 2026 22:19:10 +0000 Subject: [PATCH 4/4] buffer_utf8 wrap writes count the tail toward the used size; size 0 only at or before the end A buffer_utf8 write that wraps now updates the used size to the end of the buffer before going back to 0, and only wraps when bytes are still to be written, so a character that ends exactly at the end leaves buffer_tell at the buffer size like the other types. A size of 0 reads an empty string only at or before the end of the buffer; past it the read is out of bounds like any other. Claude-Session: https://claude.ai/code/session_01EgTASfpMoBvX7TBDy119t5 --- scripts/yyBuffer.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/scripts/yyBuffer.js b/scripts/yyBuffer.js index 906b6317..6f8ce2f7 100644 --- a/scripts/yyBuffer.js +++ b/scripts/yyBuffer.js @@ -580,8 +580,8 @@ yyBuffer.prototype.yyb_read = function(_type, _size) { // A size reads that many bytes of a buffer_text / buffer_string (GameMaker-Bugs#16485) var sized = ((_type == eBuffer_String) || (_type == eBuffer_Text)) && (_size !== undefined) && (_size >= 0); - // a size of 0 reads an empty string without moving, even at the end of the buffer - if (sized && (_size == 0)) return ""; + // a size of 0 reads an empty string without moving, at or before the end of the buffer (past it, it's out of bounds like any other read) + if (sized && (_size == 0) && ((this.m_Type == eBuffer_Format_Wrap) || (this.m_BufferIndex <= this.m_Size))) return ""; // Deal with basic alignment first //m_BufferIndex = (m_BufferIndex + (m_Alignment-1)) & ~(m_Alignment-1); // align @@ -1423,7 +1423,10 @@ yyBuffer.prototype.yyb_write = function(_type, _value) { // in a wrap buffer the bytes carry on from the start, as for the other runners (reads never decode across the end) for (var u = 0; u < UTF8_Bytes.length; u++) { this.m_DataView.setUint8(this.m_BufferIndex++, UTF8_Bytes[u]); - if ((this.m_Type == eBuffer_Format_Wrap) && (this.m_BufferIndex >= this.m_Size)) { + // only wrap here when more bytes follow; a character that ends exactly at the end leaves the position + // there, as the other types do, and the next write wraps it + if ((this.m_Type == eBuffer_Format_Wrap) && (this.m_BufferIndex >= this.m_Size) && ((u + 1) < UTF8_Bytes.length)) { + this.yyb_UpdateUsedSize(this.m_Size); // the bytes at the tail count before the position goes back to 0 this.yyb_calcnextalignmentoffset(); this.m_BufferIndex = 0; }