diff options
| author | Loki <lokirautio@gmail.com> | 2026-03-09 06:53:08 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-03-09 06:53:08 -0500 |
| commit | bda3b1078ac357b805156a8802a0649f7021716e (patch) | |
| tree | 838cf12ae598a810fb90a6e455f7fc03131f1342 /Minecraft.World/ByteArrayOutputStream.cpp | |
| parent | a358a3caaee2a4781f910cfb440bd822ae73a7e5 (diff) | |
Port over RCE Patches from LCEMP (#1023)
* LCEMP RCE Fixes WIP
Based on https://github.com/LCEMP/LCEMP/commit/d017bfc30a68888bf5c79b23cf5c4f607cf828bf
* Update to LCEMP's ByteArrayIO version
Fixes compilation since ours was missing some revisions from LCEMP
* Add additional safety checks missed in first pass
* Remove duplicate recipe count check
Diffstat (limited to 'Minecraft.World/ByteArrayOutputStream.cpp')
| -rw-r--r-- | Minecraft.World/ByteArrayOutputStream.cpp | 99 |
1 files changed, 59 insertions, 40 deletions
diff --git a/Minecraft.World/ByteArrayOutputStream.cpp b/Minecraft.World/ByteArrayOutputStream.cpp index a9f36e04..9173941f 100644 --- a/Minecraft.World/ByteArrayOutputStream.cpp +++ b/Minecraft.World/ByteArrayOutputStream.cpp @@ -5,76 +5,95 @@ // Creates a new byte array output stream. The buffer capacity is initially 32 bytes, though its size increases if necessary. ByteArrayOutputStream::ByteArrayOutputStream() { - count = 0; - buf = byteArray( 32 ); + count = 0; + buf = byteArray(32); } -//Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes. -//Parameters: -//size - the initial size. +// Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes. +// Parameters: +// size - the initial size. ByteArrayOutputStream::ByteArrayOutputStream(unsigned int size) { - count = 0; - buf = byteArray( size ); + count = 0; + buf = byteArray(size); } ByteArrayOutputStream::~ByteArrayOutputStream() { - if (buf.data != nullptr) - delete[] buf.data; + if (buf.data != NULL) + { + delete[] buf.data; + } } -//Writes the specified byte to this byte array output stream. -//Parameters: -//b - the byte to be written. +// Writes the specified byte to this byte array output stream. +// Parameters: +// b - the byte to be written. void ByteArrayOutputStream::write(unsigned int b) { - // If we will fill the buffer we need to make it bigger - if( count + 1 >= buf.length ) - buf.resize( buf.length * 2 ); + // If we will fill the buffer we need to make it bigger + if (count + 1 >= buf.length) + { + buf.resize(buf.length * 2); + } - buf[count] = static_cast<byte>(b); - count++; + buf[count] = (byte)b; + count++; } // Writes b.length bytes from the specified byte array to this output stream. -//The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length). +// The general contract for write(b) is that it should have exactly the same effect as the call write(b, 0, b.length). void ByteArrayOutputStream::write(byteArray b) { - write(b, 0, b.length); + write(b, 0, b.length); } -//Writes len bytes from the specified byte array starting at offset off to this byte array output stream. -//Parameters: -//b - the data. -//off - the start offset in the data. -//len - the number of bytes to write. +// Writes len bytes from the specified byte array starting at offset off to this byte array output stream. +// Parameters: +// b - the data. +// off - the start offset in the data. +// len - the number of bytes to write. void ByteArrayOutputStream::write(byteArray b, unsigned int offset, unsigned int length) { - assert( b.length >= offset + length ); - // If we will fill the buffer we need to make it bigger - if( count + length >= buf.length ) - buf.resize( max( count + length + 1, buf.length * 2 ) ); + if (offset > b.length || length > b.length - offset) + { + return; + } - XMemCpy( &buf[count], &b[offset], length ); - //std::copy( b->data+offset, b->data+offset+length, buf->data + count ); // Or this instead? + if (length > 0xFFFFFFFF - count) + { + return; + } - count += length; + // If we will fill the buffer we need to make it bigger + if (count + length >= buf.length) + { + unsigned int newSize = (std::max)(count + length + 1, buf.length * 2); + if (newSize <= buf.length) + { + return; + } + buf.resize(newSize); + } + + XMemCpy(&buf[count], &b[offset], length); + + count += length; } -//Closing a ByteArrayOutputStream has no effect. -//The methods in this class can be called after the stream has been closed without generating an IOException. +// Closing a ByteArrayOutputStream has no effect. +// The methods in this class can be called after the stream has been closed without generating an IOException. void ByteArrayOutputStream::close() { } -//Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it. -//Returns: -//the current contents of this output stream, as a byte array. +// Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it. +// Returns: +// the current contents of this output stream, as a byte array. byteArray ByteArrayOutputStream::toByteArray() { - byteArray out(count); - memcpy(out.data,buf.data,count); - return out; -}
\ No newline at end of file + byteArray out(count); + memcpy(out.data, buf.data, count); + return out; +} |
