| 1528 | // |
| 1529 | |
| 1530 | static int rleCompress(int inLength, const char in[], signed char out[]) { |
| 1531 | const char *inEnd = in + inLength; |
| 1532 | const char *runStart = in; |
| 1533 | const char *runEnd = in + 1; |
| 1534 | signed char *outWrite = out; |
| 1535 | |
| 1536 | while (runStart < inEnd) { |
| 1537 | while (runEnd < inEnd && *runStart == *runEnd && |
| 1538 | runEnd - runStart - 1 < MAX_RUN_LENGTH) { |
| 1539 | ++runEnd; |
| 1540 | } |
| 1541 | |
| 1542 | if (runEnd - runStart >= MIN_RUN_LENGTH) { |
| 1543 | // |
| 1544 | // Compressible run |
| 1545 | // |
| 1546 | |
| 1547 | *outWrite++ = static_cast<char>(runEnd - runStart) - 1; |
| 1548 | *outWrite++ = *(reinterpret_cast<const signed char *>(runStart)); |
| 1549 | runStart = runEnd; |
| 1550 | } else { |
| 1551 | // |
| 1552 | // Uncompressable run |
| 1553 | // |
| 1554 | |
| 1555 | while (runEnd < inEnd && |
| 1556 | ((runEnd + 1 >= inEnd || *runEnd != *(runEnd + 1)) || |
| 1557 | (runEnd + 2 >= inEnd || *(runEnd + 1) != *(runEnd + 2))) && |
| 1558 | runEnd - runStart < MAX_RUN_LENGTH) { |
| 1559 | ++runEnd; |
| 1560 | } |
| 1561 | |
| 1562 | *outWrite++ = static_cast<char>(runStart - runEnd); |
| 1563 | |
| 1564 | while (runStart < runEnd) { |
| 1565 | *outWrite++ = *(reinterpret_cast<const signed char *>(runStart++)); |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | ++runEnd; |
| 1570 | } |
| 1571 | |
| 1572 | return static_cast<int>(outWrite - out); |
| 1573 | } |
| 1574 | |
| 1575 | // |
| 1576 | // Uncompress an array of bytes compressed with rleCompress(). |