My track length code seems to have a bug, though I currently do not see what the issue is. Am I not using correct values somewhere? The code is GML (Game Maker Language), which is C/C++ like.
old_len4 through old_len1 are the 4 bytes of the track length as read from the midi file left to right (bytes are read individually).
old_len is the total of those values as a single combined sum.
I read the 4 individual bytes, combine them, add a certain number of bytes to the track length (new_length_add), and then write the new bytes back to the file.
It's worked so far, but I have just come across a file where my final values are off, thus corrupting the file.
My final track length should be
00 01 0C 05
but instead I end up with
00 02 0B 05
for a difference of
FF 00
This code has been presumably run many many times before the file is saved as I add in SYSEX resets and bank selects. I haven't had an issue thus far because this file must have a track length value that ends up right on the threshold between working and breaking, where all other files up to this point were far enough away from whatever the issue is that they did not trigger it. I had very lazy track length code prior to December that also worked for a very long time before it finally caused a problem, and this updated code was meant to fix my laziness, which it has up until now.
old_len4 = buffer_peek(new_buffer, buffer_tell(new_buffer), buffer_u8) * 0x1000000;
old_len3 = buffer_peek(new_buffer, buffer_tell(new_buffer) + 1, buffer_u8) * 0x10000;
old_len2 = buffer_peek(new_buffer, buffer_tell(new_buffer) + 2, buffer_u8) * 0x100;
old_len1 = buffer_peek(new_buffer, buffer_tell(new_buffer) + 3, buffer_u8);
old_len = old_len4 + old_len3 + old_len2 + old_len1;
old_len += global.new_length_add;
if (old_len > 0xFFFFFF)
{
buffer_write(new_buffer, buffer_u8, old_len / 0x1000000);
old_len -= 0x1000000;
}
else
buffer_seek(new_buffer, buffer_seek_relative, 1);
if (old_len > 0xFFFF)
{
buffer_write(new_buffer, buffer_u8, old_len / 0x10000);
old_len -= 0x10000;
}
else
buffer_seek(new_buffer, buffer_seek_relative, 1);
if (old_len > 0xFF)
{
buffer_write(new_buffer, buffer_u8, old_len / 0x100);
old_len -= 0x100;
}
else
buffer_seek(new_buffer, buffer_seek_relative, 1);
buffer_write(new_buffer, buffer_u8, old_len);