summaryrefslogtreecommitdiff
path: root/tools/xor_compress.c
diff options
context:
space:
mode:
authorRangi <remy.oukaour+rangi42@gmail.com>2020-09-17 21:41:29 -0400
committerRangi <remy.oukaour+rangi42@gmail.com>2020-09-17 21:41:29 -0400
commita54b3d0a34e2082f9a9ad0645f5e0a20ec4116f8 (patch)
treee277d0c0a4ed6e184411a661bcaa93849ef253b4 /tools/xor_compress.c
parent2d3329a82ba85812c21c58b27af43eb97269b1cd (diff)
Fix xor_compress algorithm bug for single-byte input
Do use read_to_end for Rust in combination with map_err
Diffstat (limited to 'tools/xor_compress.c')
-rw-r--r--tools/xor_compress.c18
1 files changed, 9 insertions, 9 deletions
diff --git a/tools/xor_compress.c b/tools/xor_compress.c
index 2e1f4fe..0e1c5e2 100644
--- a/tools/xor_compress.c
+++ b/tools/xor_compress.c
@@ -67,17 +67,10 @@ int write_compressed(const char *filename, unsigned char *data, size_t n, bool v
for (size_t i = 0; i < n; runs++) {
unsigned char byte = data[i++];
unsigned char size = 0;
- if (data[i] == v) {
- // Alternating (>= 0x80)
- // Run stops at 0x80 bytes or when the values stop alternating
- for (; i < n && size < 0x80 && data[i] == ((size % 2) ? byte : v); size++, i++);
- fputc(size + 0x7f, f);
- fputc(v ^ byte, f);
- if (size % 2 == 0) v = byte;
- } else {
+ if (i == n || data[i] != v) {
// Sequential (< 0x80)
// Run stops at 0x80 bytes or when the value two ahead is equal to v
- unsigned char buffer[256];
+ unsigned char buffer[0x80];
buffer[size++] = v ^ byte;
for (; i < n; i++) {
v = byte;
@@ -87,6 +80,13 @@ int write_compressed(const char *filename, unsigned char *data, size_t n, bool v
}
fputc(size - 1, f);
fwrite(buffer, 1, size, f);
+ } else {
+ // Alternating (>= 0x80)
+ // Run stops at 0x80 bytes or when the values stop alternating
+ for (; i < n && size < 0x80 && data[i] == ((size % 2) ? byte : v); size++, i++);
+ fputc(size + 0x7f, f);
+ fputc(v ^ byte, f);
+ if (size % 2 == 0) v = byte;
}
}