Skip to content

Instantly share code, notes, and snippets.

@socram8888
Created December 11, 2024 23:00
Show Gist options
  • Save socram8888/bef3740d639e8e7fdf148c65e9d72ee9 to your computer and use it in GitHub Desktop.
Save socram8888/bef3740d639e8e7fdf148c65e9d72ee9 to your computer and use it in GitHub Desktop.
Decoder and encoder for Samsung CSC XML files
# Decoder and encoder for Samsung CSC XML files
# (c) Marcos Del Sol Vives <[email protected]>
# License: WTFPL
import io
import gzip
class OmcEncoding:
SHIFT = [1, 1, 0, 2, 2, 4, 5, 0, 4, 7, 1, 6, 5, 3, 3, 1, 2, 5, 0, 6, 2, 2, 4, 2, 2, 3, 0, 2, 1, 2, 4, 3, 4, 0, 0, 0, 3, 5, 3, 1, 6, 5, 6, 1, 1, 1, 0, 0, 3, 2, 7, 7, 5, 6, 7, 3, 5, 1, 0, 7, 6, 3, 6, 5, 4, 5, 3, 5, 1, 3, 3, 1, 5, 4, 1, 0, 0, 2, 6, 6, 6, 6, 4, 0, 1, 1, 0, 5, 5, 4, 2, 4, 6, 1, 7, 1, 2, 1, 1, 6, 5, 4, 7, 6, 5, 1, 6, 7, 0, 2, 6, 3, 1, 7, 1, 1, 7, 4, 0, 4, 2, 5, 3, 1, 1, 5, 6, 0, 3, 5, 3, 6, 5, 7, 2, 5, 6, 6, 2, 2, 3, 6, 0, 4, 3, 2, 0, 2, 2, 3, 5, 3, 3, 2, 5, 5, 5, 1, 3, 1, 1, 1, 4, 5, 1, 6, 2, 4, 7, 1, 4, 6, 0, 6, 4, 3, 2, 6, 1, 6, 3, 2, 1, 6, 7, 3, 2, 1, 1, 5, 6, 7, 2, 2, 2, 7, 4, 6, 7, 5, 3, 1, 4, 2, 7, 1, 6, 2, 4, 1, 5, 6, 5, 4, 5, 0, 1, 1, 6, 3, 7, 2, 0, 2, 5, 0, 1, 3, 3, 2, 6, 7, 7, 2, 5, 6, 0, 4, 1, 2, 5, 3, 7, 6, 5, 2, 5, 2, 0, 1, 3, 1, 4, 3, 4, 2]
SALT = bytes([65, 197, 33, 222, 107, 28, 149, 55, 78, 17, 175, 6, 176, 135, 221, 233, 72, 122, 193, 213, 68, 119, 178, 145, 196, 31, 60, 57, 92, 168, 156, 187, 150, 91, 69, 93, 110, 23, 93, 53, 212, 205, 64, 176, 46, 2, 252, 12, 211, 80, 212, 221, 145, 228, 190, 140, 39, 2, 229, 211, 204, 125, 39, 66, 166, 63, 151, 189, 84, 199, 252, 252, 101, 166, 81, 10, 223, 1, 67, 199, 185, 18, 182, 102, 96, 167, 64, 239, 54, 162, 172, 190, 14, 119, 121, 2, 178, 177, 89, 63, 93, 109, 178, 205, 66, 220, 32, 86, 3, 198, 241, 92, 58, 2, 167, 176, 243, 255, 122, 252, 48, 63, 212, 59, 100, 214, 211, 59, 249, 239, 202, 34, 202, 71, 192, 230, 169, 176, 239, 212, 218, 144, 70, 10, 150, 95, 232, 252, 138, 45, 171, 243, 85, 25, 154, 137, 13, 219, 116, 46, 187, 59, 42, 166, 218, 151, 101, 137, 220, 97, 253, 194, 165, 159, 131, 17, 14, 106, 184, 137, 99, 111, 20, 18, 229, 113, 64, 232, 74, 196, 156, 26, 56, 212, 186, 12, 205, 156, 224, 245, 26, 48, 139, 98, 163, 51, 231, 177, 225, 97, 87, 151, 192, 7, 243, 155, 33, 134, 5, 152, 89, 212, 139, 63, 176, 250, 185, 146, 227, 151, 116, 107, 163, 91, 215, 243, 20, 141, 178, 43, 79, 134, 6, 102, 224, 52, 138, 205, 72, 152, 41, 218, 124, 72, 130, 221])
@staticmethod
def pack(input, output=None):
data = OmcEncoding._read_input(input)
data = gzip.compress(data)
data = OmcEncoding._scramble(data)
OmcEncoding._write_output(data, output)
return data
@staticmethod
def unpack(input, output=None):
data = OmcEncoding._read_input(input)
data = OmcEncoding._descramble(data)
data = gzip.decompress(data)
OmcEncoding._write_output(data, output)
return data
@staticmethod
def _descramble(source):
results = bytearray(len(source))
for i in range(len(source)):
byte_value = source[i]
shift = OmcEncoding.SHIFT[i % 256]
results[i] = ((byte_value << shift) | (byte_value >> (8 - shift))) & 0xFF
results[i] ^= OmcEncoding.SALT[i % 256]
return results
@staticmethod
def _scramble(source):
results = bytearray(len(source))
for i in range(len(source)):
byte_value = source[i] ^ OmcEncoding.SALT[i % 256]
shift = OmcEncoding.SHIFT[i % 256]
results[i] = ((byte_value >> shift) | (byte_value << (8 - shift))) & 0xFF
return results
@staticmethod
def _read_input(input):
if isinstance(input, (bytes, bytearray)):
data = input
elif isinstance(input, str):
with open(input, 'rb') as f:
data = f.read()
else:
data = input.read()
return data
@staticmethod
def _write_output(data, output):
if output is None:
return
elif isinstance(output, str):
with open(output, 'wb') as f:
f.write(data)
else:
output.write(data)
if __name__ == '__main__':
import sys
if len(sys.argv) != 4:
print(f"Usage: python {sys.argv[0]} <e|d> <input_file> <output_file>")
sys.exit(1)
operation = sys.argv[1]
input_file = sys.argv[2]
output_file = sys.argv[3]
if operation == 'e':
OmcEncoding.pack(input_file, output_file)
print(f"Packed {input_file} to {output_file}")
elif operation == 'd':
OmcEncoding.unpack(input_file, output_file)
print(f"Unpacked {input_file} to {output_file}")
else:
print("Invalid operation. Use 'e' for packing or 'd' for unpacking.")
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment