# HG changeset patch # User Matti Hamalainen # Date 1528284898 -10800 # Node ID 9731b0bdec64703d7cb5880e811a06ccd427ba8a # Parent c0c6fd8b288aabd74afa4b145961476eea098a5d Improve error handling of dmEncodeGenericRLESequence() and use dmGenericRLEOutputRun() here as well. diff -r c0c6fd8b288a -r 9731b0bdec64 tools/lib64gfx.c --- a/tools/lib64gfx.c Wed Jun 06 14:33:30 2018 +0300 +++ b/tools/lib64gfx.c Wed Jun 06 14:34:58 2018 +0300 @@ -469,9 +469,10 @@ } -static BOOL dmEncodeGenericRLESequence(DMGrowBuf *dst, const Uint8 data, unsigned int count, const DMCompParams *cfg) +int dmEncodeGenericRLESequence(DMGrowBuf *dst, const Uint8 data, const unsigned int count, const DMCompParams *cfg) { BOOL copyOnly = FALSE; + int res; switch (cfg->type) { @@ -481,20 +482,20 @@ { // A simple marker byte RLE variant: [Marker] [count] [data] if (!dmGrowBufPutU8(dst, cfg->rleMarkerW)) - return FALSE; + goto err; switch (cfg->flags & DM_RLE_ORDER_MASK) { case DM_RLE_ORDER_1: if (!dmGrowBufPutU16LE(dst, count) || !dmGrowBufPutU8(dst, data)) - return FALSE; + goto err; break; case DM_RLE_ORDER_2: if (!dmGrowBufPutU8(dst, data) || !dmGrowBufPutU16LE(dst, count)) - return FALSE; + goto err; break; } } @@ -504,20 +505,20 @@ { // A simple marker byte RLE variant: [Marker] [count] [data] if (!dmGrowBufPutU8(dst, cfg->rleMarkerB)) - return FALSE; + goto err; switch (cfg->flags & DM_RLE_ORDER_MASK) { case DM_RLE_ORDER_1: if (!dmGrowBufPutU8(dst, count) || !dmGrowBufPutU8(dst, data)) - return FALSE; + goto err; break; case DM_RLE_ORDER_2: if (!dmGrowBufPutU8(dst, data) || !dmGrowBufPutU8(dst, count)) - return FALSE; + goto err; break; } } @@ -532,23 +533,22 @@ // and the lower bits contain the count: [Mask + count] [data] if (!dmGrowBufPutU8(dst, cfg->rleMarkerBits | count) || !dmGrowBufPutU8(dst, data)) - return FALSE; + goto err; } else copyOnly = TRUE; break; } - if (copyOnly) - { - while (count--) - { - if (!dmGrowBufPutU8(dst, data)) - return FALSE; - } - } + if (copyOnly && (res = dmGenericRLEOutputRun(dst, cfg, data, count)) != DMERR_OK) + return res; + + return DMERR_OK; - return TRUE; +err: + return dmError(DMERR_MALLOC, + "%s: RLE: Could not output RLE sequence %d x 0x%02x.\n", + cfg->func, count, data); } @@ -556,7 +556,7 @@ { DMGrowBuf src; unsigned int count = 0; - int prev = -1; + int prev = -1, res = DMERR_OK; Uint8 data; // As we need to modify the offs, etc. but not the data, @@ -572,7 +572,7 @@ ((cfg->flags & DM_RLE_WORD_RUNS) && count >= cfg->rleMaxCountW) || (((cfg->flags & DM_RLE_RUNS_MASK) == DM_RLE_BYTE_RUNS) && count >= cfg->rleMaxCountB)) { - if (!dmEncodeGenericRLESequence(dst, prev, count, cfg)) + if ((res = dmEncodeGenericRLESequence(dst, prev, count, cfg)) != DMERR_OK) goto err; count = 1; @@ -583,14 +583,14 @@ prev = data; } - if (!dmEncodeGenericRLESequence(dst, prev, count, cfg)) + // If there is anything left in the output queue .. + if ((res = dmEncodeGenericRLESequence(dst, prev, count, cfg)) != DMERR_OK) goto err; - return DMERR_OK; + dmFinishRLEBuffers(dst, &src, cfg); err: - return dmError(DMERR_MALLOC, - "Could not reallocate memory for RLE encoding buffer.\n"); + return res; }