# HG changeset patch # User Matti Hamalainen # Date 1525994769 -10800 # Node ID f0232684857a61d92348f7592da1d751797d9ddb # Parent 7729fefe8e57139d8260b159e8ad40655f7ffc39 Cleanup the RLE decoder. diff -r 7729fefe8e57 -r f0232684857a tools/lib64gfx.c --- a/tools/lib64gfx.c Fri May 11 02:25:40 2018 +0300 +++ b/tools/lib64gfx.c Fri May 11 02:26:09 2018 +0300 @@ -231,35 +231,34 @@ int res; if ((res = dmGrowBufAlloc(dst, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK) - { - dmError(res, - "Could not allocate RLE decoding buffer.\n"); goto err; - } // Perform RLE decode while (src < srcEnd) { - Uint8 c = *src++; - int cnt = 1; + Uint8 data = *src++; + int count = 1; switch (rleType) { case DM_RLE_MARKER: - if (c == rleMarker) + // A simple marker byte RLE variant: [Marker] [count] [data] + if (data == rleMarker) { if (srcEnd - src < 2) { res = DMERR_INVALID_DATA; goto err; } - cnt = *src++; - c = *src++; + count = *src++; + data = *src++; } break; case DM_RLE_MASK: - if ((c & rleMask1) == rleMarker) + // Mask marker RLE: usually high bit(s) of byte mark RLE sequence + // and the lower bits contain the count: [Mask + count] [data] + if ((data & rleMask1) == rleMarker) { if (srcEnd - src < 1) { @@ -267,16 +266,15 @@ goto err; } - // XXX TODO actually we probably want another mask here - cnt = c & rleMask2; - c = *src++; + count = data & rleMask2; + data = *src++; } break; } - while (cnt--) + while (count--) { - if (!dmGrowBufPutU8(dst, c)) + if (!dmGrowBufPutU8(dst, data)) { res = DMERR_MALLOC; goto err; @@ -284,10 +282,6 @@ } } - // Reallocate the memory - if ((res = dmGrowBufResize(dst)) != DMERR_OK) - goto err; - res = DMERR_OK; err: