RLE bug squashed, now the modules that couldn't load can load now

This commit is contained in:
techmetx11 2024-04-12 13:09:18 +00:00
parent 99729a4157
commit 7a051b4486
No known key found for this signature in database
GPG key ID: 20E0C88A0E7E5AF2

View file

@ -38,20 +38,27 @@ class TFMRLEReader {
int tagLenLeft; int tagLenLeft;
signed char tagChar; signed char tagChar;
void decodeRLE(signed char prevChar) { void decodeRLE(unsigned char prevChar) {
int lenShift=0; int lenShift=0;
tagLenLeft=0; tagLenLeft=0;
signed char rleTag=0; unsigned char rleTag=0;
do { do {
rleTag=readC(); rleTag=readCNoRLE();
tagLenLeft|=(rleTag&0x7F)<<lenShift; tagLenLeft|=(rleTag&0x7F)<<lenShift;
lenShift+=7; lenShift+=7;
logD("RLE tag: %X, len shift: %d, len left: %d",rleTag,lenShift,tagLenLeft); logD("offset: %x, RLE tag: %X, len shift: %d, len left: %d",curSeek,rleTag,lenShift,tagLenLeft);
} while (!(rleTag&0x80)); } while (!(rleTag&0x80));
if (tagLenLeft) {
// sync back since we've already read one character // sync back since we've already read one character
tagLenLeft--;
inTag=true; inTag=true;
tagLenLeft--;
tagChar=prevChar; tagChar=prevChar;
} else {
tagChar=0x80;
}
logD("tag finished: len left: %d, char: %X",tagLenLeft,tagChar);
} }
public: public:
@ -64,9 +71,9 @@ public:
tagChar(0) {} tagChar(0) {}
// these functions may throw TFMEndOfFileException // these functions may throw TFMEndOfFileException
signed char readC() { unsigned char readC() {
if (inTag) { if (inTag) {
if (!tagLenLeft) { if (tagLenLeft<=0) {
inTag=false; inTag=false;
return readC(); return readC();
} }
@ -86,22 +93,16 @@ public:
// in certain parts of the header and footer) // in certain parts of the header and footer)
// TFM music maker actually uses double 0x80 to escape the 0x80 // TFM music maker actually uses double 0x80 to escape the 0x80
// for example: 0xDA 0x80 0x80 0x00 0x23 = 0xDA 0x80 0x00 0x23) // for example: 0xDA 0x80 0x80 0x00 0x23 = 0xDA 0x80 0x00 0x23)
if (ret==0x80 && curSeek+1<=len) { if (ret==0x80) {
if (buf[curSeek+1]!=0x80) {
decodeRLE(buf[curSeek-2]); decodeRLE(buf[curSeek-2]);
tagLenLeft--; tagLenLeft--;
return tagChar; return tagChar;
} else {
// to avoid outputting the extra 0x80
curSeek++;
return ret;
}
} }
return ret; return ret;
} }
signed char readCNoRLE() { signed char readCNoRLE() {
if (curSeek+1>len) throw TFMEndOfFileException(this,len); if (curSeek>len) throw TFMEndOfFileException(this,len);
return buf[curSeek++]; return buf[curSeek++];
} }