Skip to content
This repository was archived by the owner on Mar 29, 2026. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/urh/signalprocessing/Encoding.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,31 @@ def code_edge(self, decoding, inpt):
return output, errors, self.ErrorState.SUCCESS

def code_substitution(self, decoding, inpt):
byte_data = inpt.tobytes()
src = self.src
dst = self.dst

if len(src) < 1 or len(dst) < 1:
return [], 1, self.ErrorState.WRONG_INPUT
if not decoding:
src, dst = dst, src

mapping = sorted(zip(src, dst), key=lambda x: len(x[0]), reverse=True)
final_replacements = []
current_token_val = 255

for s, d in mapping:
token = bytes([current_token_val])
final_replacements.append((token, d.tobytes()))
byte_data = byte_data.replace(s.tobytes(), token)
current_token_val -= 1

for token, dst_bytes in final_replacements:
byte_data = byte_data.replace(token, dst_bytes)

return array.array("B", byte_data), 0, self.ErrorState.SUCCESS

def code_substitution_old(self, decoding, inpt):
padded_inpt = copy.copy(inpt)
output = array.array("B", [])

Expand Down
10 changes: 4 additions & 6 deletions tests/test_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,13 @@ def test_substitution(self):

# encoded-string with 3 missing trailing zeroes
encoded = e.str2bit(
"1000111010001110111011101110111011101110100011101110111011101110111011101000100010001000100010001"
)
compare = e.str2bit(
"1000111010001110111011101110111011101110100011101110111011101110111011101000100010001000100010001000"
)
decoded, err, _ = e.code_substitution(decoding=True, inpt=encoded)

decoded, _, _ = e.code_substitution(decoding=True, inpt=encoded)
reencoded, _, _ = e.code_substitution(decoding=False, inpt=decoded)
self.assertEqual(err, 3)
self.assertEqual(reencoded, compare)

self.assertEqual(reencoded, encoded)

def test_external(self):
encoder = get_path_for_data_file("encode.py")
Expand Down
Loading