Browse Source

new extract_lsbs

Taruu 4 months ago
parent
commit
eac71c7c4f
1 changed files with 16 additions and 13 deletions
  1. 16 13
      main.py

+ 16 - 13
main.py

@@ -34,23 +34,26 @@ def chunks(in_bytes: bytes, take_n: int) -> bytes:
 
 
 def extract_lsbs(data):
-    logger.info("Extract LSBs.")
+    deduced_data = bytearray()
+    previous_byte = None
 
-    buffer = []
+    for byte in data:
+        if byte != previous_byte:
+            deduced_data.append(byte)
+            previous_byte = byte
 
-    data = [k for k, _ in groupby(data)]
-    data = bytes(data)
-    data = chunks(data, 8)
+    buffer = bytearray()
+    chunk_size = 8
+    num_full_chunks = len(deduced_data) // chunk_size
 
-    for chunk in data:
-        if len(chunk) != 8:
-            break
+    for i in range(num_full_chunks):
+        result_byte = 0
+        start_index = i * chunk_size
 
-        ba = bitstring.BitArray(8)
-        for i, byte in zip(range(len(chunk)), chunk):
-            ba[i] = byte & 1
+        for j in range(chunk_size):
+            result_byte |= ((deduced_data[start_index + j] & 1) << (7 - j))
 
-        buffer.append(ba.u)
+        buffer.append(result_byte)
 
     return bytes(buffer)
 
@@ -131,5 +134,5 @@ class FrameTakeWorker:
 
 
 if __name__ == "__main__":
-    ftw = FrameTakeWorker(1)
+    ftw = FrameTakeWorker(0, 3)
     asyncio.run(ftw.run_workers())