summaryrefslogtreecommitdiff
path: root/tools/xor_compress.py
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.py
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.py')
-rw-r--r--tools/xor_compress.py25
1 files changed, 12 insertions, 13 deletions
diff --git a/tools/xor_compress.py b/tools/xor_compress.py
index 1c3a2f1..42d091f 100644
--- a/tools/xor_compress.py
+++ b/tools/xor_compress.py
@@ -32,19 +32,7 @@ while i < n:
i += 1
runs += 1
- if data[i] == v:
- # Alternating (>= 0x80)
- # Run stops at 0x80 bytes or when the values stop alternating
- size = 0
- while i < n and size < 0x80 and data[i] == (byte if size % 2 else v):
- size += 1
- i += 1
- output.append(size + 0x7f)
- output.append(v ^ byte)
- if not size % 2:
- v = byte
-
- else:
+ if i == n or data[i] != v:
# Sequential (< 0x80)
# Run stops at 0x80 bytes or when the value two ahead is equal to v
buffer = [v ^ byte]
@@ -57,6 +45,17 @@ while i < n:
i += 1
output.append(len(buffer) - 1)
output.extend(buffer)
+ else:
+ # Alternating (>= 0x80)
+ # Run stops at 0x80 bytes or when the values stop alternating
+ size = 0
+ while i < n and size < 0x80 and data[i] == (byte if size % 2 else v):
+ size += 1
+ i += 1
+ output.append(size + 0x7f)
+ output.append(v ^ byte)
+ if not size % 2:
+ v = byte
with open(out_filename, 'wb') as f:
f.write(output)