agentirons

pfcoder_v3

Sep 29th, 2016
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. ## Canon Picture Style Decoder/Encoder v3 ##
  4. ## http://www.magiclantern.fm/forum/index.php?topic=16299 ##
  5. ## Thanks to chris_overseas for the original java app, and chmee for the PHP version ##
  6.  
  7. import sys, io, os
  8. from struct import *
  9.  
  10. def decode(b, index1, index2):
  11.     return int(b) ^ KEY_1[int(index1) % len(KEY_1)] ^ KEY_2[int(index2) % len(KEY_2)] ^ 0x37
  12.  
  13. HEADER_SIZE = 0x0C
  14.  
  15. DECODED_SUFFIX = '.decoded'
  16.  
  17. KEY_1 = [
  18.     0xF1, 0x77, 0x9E, 0x97, 0x19, 0x71, 0x7D, 0xEF, 0xD7, 0x2C, 0x58, 0xA4, 0x82, 0x22, 0x37, 0x24,
  19.     0x97, 0xDE, 0x28, 0xD1, 0xC5, 0x21, 0x32, 0xD3, 0x9A, 0xB9, 0x91, 0x0A, 0xD7, 0x34, 0x21, 0x4E,
  20.     0x7E, 0x28, 0x3B, 0x64, 0x7C, 0xA7, 0x31, 0x4B, 0x0D, 0x67, 0x8F, 0x6B, 0x59, 0x8F, 0xCD, 0xE5,
  21.     0xD0, 0x93, 0xD2, 0xCF, 0x46, 0x38, 0x72, 0xA2, 0x6E, 0x12, 0x04, 0xFA, 0xB5, 0x32, 0x2A, 0x0E,
  22.     0x8B, 0xB7, 0x06, 0xBB, 0xE7, 0x8C, 0x98, 0x78, 0xB7, 0x74, 0x98, 0x7C, 0x3C, 0xC1, 0x51, 0x84,
  23.     0x19, 0x57, 0xEC, 0x32, 0xAF, 0x8C, 0x6E, 0x32, 0x48, 0x3B, 0x2F, 0x14, 0x56, 0x2E, 0xA4, 0x6C,
  24.     0x2A, 0x81, 0x74, 0x1B, 0x3B, 0x90, 0xB8, 0x62, 0x35, 0x8C, 0xDC, 0x0B, 0xCE, 0x3C, 0xBD, 0xB7,
  25.     0x7B, 0x9F, 0xCC, 0x8F, 0xC9, 0x8A, 0x88, 0x89, 0x75, 0x4B, 0xAC, 0xAC, 0x62, 0x66, 0x9A, 0x1B,
  26.     0xD5, 0xC2, 0x6E, 0xE6, 0x53, 0x2B, 0x06, 0x5A, 0x26, 0xF1, 0xFB, 0xF7, 0xA2, 0x69, 0x6D, 0x32,
  27.     0x82, 0x69, 0x44, 0x80, 0xDA, 0xB9, 0x8B, 0x7E, 0xBD, 0x74, 0xF0, 0x3E, 0x29, 0x7A, 0xB8, 0xCB,
  28.     0x74, 0xFD, 0xC5, 0xAC, 0xDD, 0x49, 0x9A, 0x8A, 0xE5, 0xC5, 0x28, 0x2C, 0x60, 0x20, 0x79, 0x3D,
  29.     0xF3, 0xA2, 0x09, 0xBF, 0xBA, 0x1F, 0x47, 0x17, 0x44, 0x88, 0x7F, 0xB0, 0x32, 0x2E, 0x05, 0xC9,
  30.     0x26, 0x8A, 0x9A, 0xA4, 0x2A, 0x90, 0xC2, 0xDC, 0xC5, 0xE5, 0x1D, 0xBF, 0x74, 0xC9, 0x90, 0xB4,
  31.     0x50, 0xA1, 0x41, 0xFE, 0xF3, 0x50, 0x4F, 0xF7, 0x4D, 0xCB, 0x61, 0xB3, 0xE7, 0x92, 0x26, 0x9D,
  32.     0xED, 0x94, 0x2C, 0x19, 0xA7, 0xB2, 0xE2, 0x3F, 0x82, 0x59, 0xFB, 0x1D, 0xA7, 0x95, 0xBD, 0x67,
  33.     0x4B, 0x9C, 0x6E, 0xFA, 0x27, 0x18, 0x21, 0x2C, 0x7E, 0x07, 0x45, 0xD3, 0x45, 0x62, 0xE8, 0x49,
  34.     0x86, 0x86, 0xAC, 0xAC, 0xBD, 0xBD, 0xC1, 0x7A, 0x7A, 0xB3, 0xB3, 0xAC, 0x15, 0x5F, 0x3E, 0xAD,
  35.     0x5A, 0x90, 0x91, 0xCA, 0x3B, 0xB8, 0x4E, 0x58, 0x58, 0xFC, 0xFC, 0x24, 0xB8, 0xB8, 0x75, 0x75,
  36.     0xD8, 0xD8, 0x3B, 0x3B, 0xB2, 0xB2, 0x0E, 0x0E, 0x1A, 0x1A, 0xE2, 0xE2, 0x12, 0x19, 0x19, 0x9F,
  37.     0x9F, 0xA7, 0xA7, 0xC2, 0xC2, 0xCB, 0xCB, 0x34, 0x34, 0xB8, 0xB8, 0xC1, 0xC1, 0x5C, 0x2A, 0x2A,
  38.     0x61, 0x61, 0xF5, 0xF5, 0x64, 0x64, 0x8D, 0x8D, 0x40, 0x40, 0x28, 0x28, 0x7D, 0x7D, 0x83, 0xF7,
  39.     0xF7, 0xB6, 0xB6, 0x51, 0x51, 0xD0, 0xD0, 0x21, 0x21, 0x87, 0x87, 0xA2, 0xA2, 0x03, 0x03, 0x62,
  40.     0x62, 0x6F, 0x55, 0x55, 0x27, 0x27, 0xA0, 0xA0, 0x44, 0x44, 0x2F, 0x2F, 0x8C, 0x8C, 0xE1, 0xE1,
  41.     0x20, 0x20, 0x23, 0xB1, 0xB1, 0xAB, 0xAB, 0x12, 0x12, 0x4C, 0xEE, 0xC7, 0xA9, 0x56, 0xAB, 0x6F,
  42.     0x0E, 0x60, 0xDD, 0xA6, 0xB6, 0xA1, 0xB3, 0x7A, 0x8C, 0x69, 0xAA, 0xCA, 0x2B, 0x66, 0x66, 0x7E,
  43.     0x7E, 0x83, 0x83, 0x62, 0x62, 0x1D, 0x1D, 0x08, 0x51, 0x51, 0x82, 0x82, 0x7A, 0xCF, 0x7C, 0xBD,
  44.     0xBE, 0xC6, 0x13, 0x52, 0x52, 0xF8, 0xF8, 0x83, 0x86, 0x5C, 0x1C, 0xE9, 0x67, 0x65, 0x6C, 0x02,
  45.     0x8F, 0xEA, 0x9B, 0x1C, 0x44, 0x68, 0xA9, 0x16, 0xA1, 0x86, 0xD6, 0x53, 0x2E, 0xE5, 0xF6, 0x3A,
  46.     0x90, 0x4F, 0x2C, 0x18, 0xC2, 0xAA, 0xE7, 0x8F, 0x7E, 0xA1, 0xDE, 0x5F, 0x08, 0xC6, 0xFD, 0x37,
  47.     0xBA, 0xEF, 0x88, 0xBE, 0xD3, 0x5A, 0x27, 0xD0, 0xEF, 0xE6, 0xDE, 0xC0, 0x37, 0x50, 0x62, 0x05,
  48.     0x3A, 0xE6, 0x0E, 0xD0, 0x7E, 0xAA, 0x99, 0x51, 0x87, 0x82, 0xE5, 0x1B, 0x62, 0xF0, 0x03, 0x03,
  49.     0x9C, 0x5A, 0x5A, 0x5A, 0x5A, 0xB6, 0xB6, 0x87, 0x87, 0xC4, 0xC4, 0xB5, 0xB5, 0x84, 0x84, 0xFA
  50. ]
  51.  
  52. KEY_2 = [
  53.     0x5B, 0x02, 0xB7, 0x07, 0x5B, 0x5E, 0x8B, 0xB5, 0xE6, 0xF9, 0x7F, 0x98, 0x07, 0xD7, 0xD7, 0x9C,
  54.     0x68, 0xE4, 0x77, 0xEA, 0xD9, 0x3F, 0xE6, 0xB9, 0xB3, 0xCA, 0xEA, 0xD9, 0x15, 0x02, 0x75, 0x52,
  55.     0x78, 0x51, 0xCB, 0x89, 0x71, 0x9A, 0xFB, 0x05, 0x45, 0x0F, 0xA6, 0x91, 0x81, 0x9A, 0x7C, 0x18,
  56.     0xEE, 0xD7, 0x38, 0x34, 0x54, 0xDF, 0x4E, 0xD5, 0xE4, 0x81, 0xAD, 0xC3, 0xA0, 0x13, 0xBD, 0xFE,
  57.     0xB8, 0x43, 0xA9, 0xD8, 0xE2, 0x1A, 0xEC, 0x9F, 0x6B, 0xE2, 0x0E, 0x3C, 0xCD, 0x62, 0x7F, 0xFD,
  58.     0x40, 0xE3, 0xCF, 0x6B, 0xF7, 0x8C, 0xCA, 0x79, 0xCA, 0x2A, 0xBD, 0xA9, 0xB5, 0xB4, 0x99, 0x58,
  59.     0x5B, 0x41, 0x56, 0xFB, 0x6C, 0x36, 0x95, 0x80, 0x69, 0xD0, 0x94, 0xF4, 0x66, 0x18, 0xD5, 0x0E,
  60.     0x36, 0x59, 0xFC, 0x32, 0x96, 0x8C, 0x3D, 0x95, 0x46, 0xC0, 0xD0, 0x4A, 0xA4, 0x6C, 0x5E, 0xCF,
  61.     0x9E, 0x54, 0x1B, 0x3B, 0x41, 0xB3, 0x3E, 0x3E, 0x2D, 0x65, 0x65, 0xAE, 0xAE, 0x3C, 0x3C, 0x3E,
  62.     0x3E, 0x7E, 0x7E, 0x95, 0x95, 0x7B, 0x7B, 0xF6, 0xF6, 0x59, 0xCC, 0xCC, 0x63, 0x63, 0x94, 0x94,
  63.     0xC9, 0xC9, 0xF4, 0xF4, 0x3F, 0x3F, 0x88, 0x88, 0x97, 0x97, 0xBF, 0xBD, 0xAC, 0xAC, 0x62, 0x66,
  64.     0xC0, 0x2B, 0xFE, 0x06, 0x27, 0x9B, 0x9B, 0x53, 0x53, 0x91, 0x91, 0x46, 0x46, 0x06, 0x63, 0xF1,
  65.     0x8C, 0x5A, 0xF7, 0xB5, 0xC3, 0x6D, 0xBF, 0x3C, 0x52, 0x52, 0x44, 0x44, 0xEA, 0xEA, 0xEF, 0xEF,
  66.     0xDA, 0xDA, 0x66, 0x66, 0x88, 0x5C, 0x68, 0xCB, 0xFD, 0x15, 0x15, 0x0D, 0x0D, 0xA3, 0xA3, 0x6C,
  67.     0x6C, 0x1B, 0x1B, 0x49, 0x49, 0x88, 0x88, 0xC4, 0xC4, 0x8A, 0x3D, 0x3D, 0xE5, 0x02, 0xBF, 0xA2,
  68.     0xA2, 0x74, 0x74, 0x98, 0x98, 0x8E, 0x8E, 0xA3, 0xA3, 0x5F, 0x91, 0x91, 0x06, 0x2F, 0x2F, 0xD8,
  69.     0xD8, 0x17, 0x3A, 0x3A, 0x44, 0x44, 0xB0, 0xB0, 0x8D, 0x6C, 0xF2, 0xB9, 0xBC, 0xCC, 0x26, 0x8A,
  70.     0x08, 0x65, 0x6B, 0xD8, 0x26, 0xDC, 0xC5, 0xE5, 0x82, 0x88, 0xAD, 0x90, 0x3C, 0x7F, 0xD6, 0xB4,
  71.     0xB4, 0xFE, 0xFE, 0xE7, 0xE7, 0x0A, 0x0A, 0x50, 0x50, 0x0A, 0x1F, 0x1F, 0xBA, 0xBA, 0x1A, 0x1A,
  72.     0x2C, 0x2C, 0x31, 0x31, 0x61, 0x61, 0x8A, 0x8A, 0xB3, 0xB3, 0xAF, 0xFE, 0xFE, 0x92, 0x92, 0x26,
  73.     0x26, 0x9F, 0x9F, 0xC5, 0xC5, 0x2E, 0x2E, 0x03, 0x03, 0x2C, 0x1D, 0x75, 0xE0, 0xD4, 0xF3, 0xFB,
  74.     0x1D, 0x41, 0x25, 0x5C, 0xC0, 0x59, 0x3F, 0xE0, 0x0E, 0x80, 0x80, 0x2C, 0x2C, 0x6C, 0x6C, 0x92,
  75.     0x92, 0x54, 0x54, 0x81, 0x81, 0xD3, 0xB9, 0xB9, 0x9B, 0x9B, 0xE3, 0xE3, 0xE8, 0xE8, 0x6C, 0x6C,
  76.     0xC9, 0xC9, 0x31, 0xA1, 0xAC, 0xD6, 0x0B, 0xC4, 0x0F, 0x9E, 0xBE, 0x89, 0x03, 0x15, 0x3D, 0x3D,
  77.     0x51, 0x51, 0x3E, 0x3E, 0xAD, 0xAD, 0x5A, 0x5A, 0xD5, 0xD5, 0x88, 0x88, 0x3D, 0x3D, 0xD5, 0x05,
  78.     0x05, 0x9D, 0x9D, 0xC2, 0xC2, 0xCF, 0xCF, 0x57, 0x57, 0xE5, 0xE5, 0x8B, 0x8B, 0x64, 0x64, 0xB8,
  79.     0xA2, 0xA2, 0x59, 0x59, 0x10, 0x47, 0x28, 0x36, 0x19, 0x67, 0xC2, 0x07, 0xB8, 0x90, 0x89, 0x02,
  80.     0x64, 0x04, 0x1B, 0x24, 0x83, 0x28, 0x51, 0x59, 0x84, 0xA2, 0x6D, 0x9C, 0xC9, 0x0A, 0x44, 0x56,
  81.     0x6C, 0x23, 0x23, 0xF3, 0xF3, 0x61, 0x61, 0x45, 0x45, 0x9D, 0xC0, 0xC0, 0xB7, 0xB7, 0x12, 0x12,
  82.     0x34, 0x34, 0x4D, 0x4D, 0x3C, 0x3C, 0xB8, 0xB8, 0x56, 0x56, 0xAB, 0x68, 0x68, 0xF8, 0xF8, 0x6F,
  83.     0x6F, 0x66, 0x66, 0x11, 0x11, 0x0E, 0x0E, 0x9E, 0x9E, 0x2F, 0x2F, 0xDC, 0xDD, 0xDD, 0x69, 0x69,
  84.     0x74, 0x74, 0x8F, 0x8F, 0x06, 0x06, 0x08, 0x08, 0x8C, 0x8C, 0x2A, 0xDF, 0x3B, 0x39, 0x13, 0x1A,
  85.     0x1D
  86. ]
  87.  
  88. if (len(sys.argv) == 1):
  89.     print 'Usage: pfcoder_v3.py input-file [output-file]'
  90.     sys.exit()
  91.  
  92. inputFile = sys.argv[1]
  93. if (len(sys.argv) == 2):
  94.     if (inputFile.endswith(DECODED_SUFFIX)):
  95.         outputFile = inputFile[:-len(DECODED_SUFFIX)]
  96.     else:
  97.         outputFile = inputFile + DECODED_SUFFIX
  98. else:
  99.     outputFile = sys.argv[2]
  100.  
  101. try:
  102.     inputStream = io.open(inputFile, 'rb')
  103.     fsize = os.path.getsize(inputFile)
  104.     data = inputStream.read()
  105.     try:
  106.         outputStream = io.open(outputFile, mode='wb')
  107.         b = 0
  108.         key1Index = HEADER_SIZE
  109.         key2Index = HEADER_SIZE
  110.         index = 0
  111.         byteArray = unpack(str(fsize)+'B',data)
  112.         for b in byteArray:
  113.             if ((index) >= HEADER_SIZE):
  114.                 print 'decoding byte: '+hex(b)+' to '
  115.                 b = decode(b, key1Index, key2Index)
  116.                 print hex(b)
  117.                 key1Index += 1
  118.                 key2Index += 1
  119.             outputStream.write(chr(b))
  120.             index += 1
  121.     except IOError as err:
  122.         print 'Failed to open ' + outputFile + ' for writing. Reason: {0}'.format(err)
  123.         sys.exit(-2)
  124. except IOError as err:
  125.     print 'Failed to open ' + inputFile + ' for reading. Reason: {0}'.format(err)
  126.     sys.exit(-3)
RAW Paste Data