comparison tools/lib64gfx.c @ 1476:f0232684857a

Cleanup the RLE decoder.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 11 May 2018 02:26:09 +0300
parents 7729fefe8e57
children d883b4c1cf48
comparison
equal deleted inserted replaced
1475:7729fefe8e57 1476:f0232684857a
229 const Uint8 rleMarker, const Uint8 rleMask1, const Uint8 rleMask2, const int rleType) 229 const Uint8 rleMarker, const Uint8 rleMask1, const Uint8 rleMask2, const int rleType)
230 { 230 {
231 int res; 231 int res;
232 232
233 if ((res = dmGrowBufAlloc(dst, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK) 233 if ((res = dmGrowBufAlloc(dst, BUF_SIZE_INITIAL, BUF_SIZE_GROW)) != DMERR_OK)
234 {
235 dmError(res,
236 "Could not allocate RLE decoding buffer.\n");
237 goto err; 234 goto err;
238 }
239 235
240 // Perform RLE decode 236 // Perform RLE decode
241 while (src < srcEnd) 237 while (src < srcEnd)
242 { 238 {
243 Uint8 c = *src++; 239 Uint8 data = *src++;
244 int cnt = 1; 240 int count = 1;
245 241
246 switch (rleType) 242 switch (rleType)
247 { 243 {
248 case DM_RLE_MARKER: 244 case DM_RLE_MARKER:
249 if (c == rleMarker) 245 // A simple marker byte RLE variant: [Marker] [count] [data]
246 if (data == rleMarker)
250 { 247 {
251 if (srcEnd - src < 2) 248 if (srcEnd - src < 2)
252 { 249 {
253 res = DMERR_INVALID_DATA; 250 res = DMERR_INVALID_DATA;
254 goto err; 251 goto err;
255 } 252 }
256 cnt = *src++; 253 count = *src++;
257 c = *src++; 254 data = *src++;
258 } 255 }
259 break; 256 break;
260 257
261 case DM_RLE_MASK: 258 case DM_RLE_MASK:
262 if ((c & rleMask1) == rleMarker) 259 // Mask marker RLE: usually high bit(s) of byte mark RLE sequence
260 // and the lower bits contain the count: [Mask + count] [data]
261 if ((data & rleMask1) == rleMarker)
263 { 262 {
264 if (srcEnd - src < 1) 263 if (srcEnd - src < 1)
265 { 264 {
266 res = DMERR_INVALID_DATA; 265 res = DMERR_INVALID_DATA;
267 goto err; 266 goto err;
268 } 267 }
269 268
270 // XXX TODO actually we probably want another mask here 269 count = data & rleMask2;
271 cnt = c & rleMask2; 270 data = *src++;
272 c = *src++;
273 } 271 }
274 break; 272 break;
275 } 273 }
276 274
277 while (cnt--) 275 while (count--)
278 { 276 {
279 if (!dmGrowBufPutU8(dst, c)) 277 if (!dmGrowBufPutU8(dst, data))
280 { 278 {
281 res = DMERR_MALLOC; 279 res = DMERR_MALLOC;
282 goto err; 280 goto err;
283 } 281 }
284 } 282 }
285 } 283 }
286
287 // Reallocate the memory
288 if ((res = dmGrowBufResize(dst)) != DMERR_OK)
289 goto err;
290 284
291 res = DMERR_OK; 285 res = DMERR_OK;
292 286
293 err: 287 err:
294 return res; 288 return res;