I'm reverse-engineering custom binary file format. I'm using 010 Editor to check this file. I found data structure to represent UTF-8 strings, which has header section of one or two bytes in length then the actual UTF-8 string data. The header section seems to store total number of bytes needed for the string data.
╔════════════╦═══════════════╦═══════════════════╗
║ first byte ║ optional byte ║ UTF-8 string data ║
╚════════════╩═══════════════╩═══════════════════╝
The second byte in header is optional and it's only there when UTF-8 string needs more than 128 bytes to be stored. When string length is less or equal 128 bytes, decoding it's length is easy. However, when string length > 128, I'm failing to calculate string length from header section. So I did experiments and generated many binary files with different string length and below is the result. String length is the total number of bytes needed to store the string. First and second columns are first and second optional byte in header section.
╔════╦════╦═══════════════╗
║ 01 ║ 02 ║ String length ║
╠════╬════╬═══════════════╣
║ 7D ║N/A ║ 126 ║
║ 7E ║N/A ║ 127 ║
║ 7F ║N/A ║ 128 ║
║ 80 ║ 01 ║ 129 ║
║ 81 ║ 01 ║ 130 ║
║ C7 ║ 01 ║ 200 ║
║ C8 ║ 01 ║ 201 ║
║ F9 ║ 01 ║ 250 ║
║ FE ║ 01 ║ 255 ║
║ FF ║ 01 ║ 256 ║
║ 80 ║ 02 ║ 257 ║
║ 81 ║ 02 ║ 258 ║
║ 82 ║ 02 ║ 259 ║
║ F3 ║ 03 ║ 500 ║
║ F4 ║ 03 ║ 501 ║
║ F5 ║ 03 ║ 502 ║
║ F6 ║ 03 ║ 503 ║
║ 80 ║ 04 ║ 513 ║
╚════╩════╩═══════════════╝
I read somewhere that Pascal\Delphi is using string format where it has header to save string length instead of null terminated strings like in C, which look similar to my case.
My question is how can I calculate string length when it's greater than 128 bytes in length using values in header section?