Created
August 28, 2013 10:36
-
-
Save dpq/6364653 to your computer and use it in GitHub Desktop.
Fourier transformation of binaries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from scipy.fftpack import rfft, irfft | |
from scipy import array | |
import struct | |
from sys import argv | |
if len(argv) < 2: | |
print "Usage: ./encode.py SOURCEFILE" | |
exit() | |
try: | |
filename = argv[1] | |
code = open(filename, 'rb').read() | |
except: | |
print "Couldn't open the target file, aborting" | |
exit() | |
# Acquire data discrepancies | |
misses = struct.unpack('I', code[:4])[0] | |
fix = code[4:4+5*misses] | |
# Interpret the binary as an array of floats | |
code = code[4+5*misses:] | |
coefs = [] | |
for i in range(0, len(code), 4): | |
coefs.append(struct.unpack('f', code[i:i+4])) | |
coefs=array(coefs) | |
y2 = irfft(coefs) | |
code2 = "" | |
for i in range(len(y2)): | |
code2 += struct.pack('f', y2[i]) | |
# Extract fixes | |
fixmap = {} | |
for i in range(0, len(fix), 5): | |
addr, val = struct.unpack('I', fix[i:i+4])[0], fix[i+4:i+5] | |
fixmap[addr] = val | |
# Build the reverse FFT-transformed binary and apply the fixes | |
f = open(argv[1].replace(".fft", ""), 'wb') | |
for i in range(len(code2)): | |
if not i in fixmap.keys(): | |
f.write(code2[i]) | |
else: | |
f.write(fixmap[i]) | |
f.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from scipy.fftpack import rfft, irfft | |
from scipy import array | |
import struct | |
from sys import argv | |
if len(argv) < 2: | |
print "Usage: ./encode.py SOURCEFILE" | |
exit() | |
try: | |
filename = argv[1] | |
code = open(filename, 'rb').read() | |
except: | |
print "Couldn't open the target file, aborting" | |
exit() | |
# Interpret the binary as an array of floats | |
y = [] | |
for i in range(0, len(code), 4): | |
y.append(struct.unpack('f', code[i:i+4])) | |
y = array(y) | |
# FFT/BFFT | |
coefs = rfft(y) | |
# Decode for verification | |
y2 = irfft(coefs) | |
code2 = "" | |
for v in range(len(y2)): | |
code2 += struct.pack('f', y2[v]) | |
# Check for discrepancies between the original and the encoded-decoded copy | |
misses, fix = 0, "" | |
for i in range(len(code)): | |
if code[i] != code2[i]: | |
misses += 1 | |
fix += struct.pack("I", i) + code[i] | |
print i | |
# Build the FFT-transformed binary | |
compressed = struct.pack("I", misses) + fix | |
for i in range(len(coefs)): | |
compressed += struct.pack('f', coefs[i]) | |
f = open(argv[1] + ".fft", 'wb') | |
f.write(compressed) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment