Advertisement
Gfy

missing_RAR2.py

Gfy
Mar 23rd, 2012
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.85 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import zlib
  5. import struct
  6.  
  7. # !Referenced file not found:
  8. #   \Modern.Family.S02E02.1080p.BluRay.X264-7SinS\
  9. #   7sins-modern.family.s02e02.1080p.x264.r00
  10.  
  11. path = "C:\\Users\\Me\\Desktop\\rebuild\\"
  12. full = path + "7sins-modern.family.s02e02.1080p.x264.mkv"
  13. part = path + "r27data.bin"
  14. todo = path + "modern.family.s02e11.1080p.bluray-bia.r27"
  15. filenb = 28 # r27
  16.  
  17. amount = 49999894 # amount of data stored in one volume
  18. sfv_crc = 0xfa6f96b5
  19. start = amount * filenb
  20. end = start + amount
  21. MARKER = "526172211a0700"
  22. archive = "f1fb7301000d00000000000000"
  23. fileh = ("5fbc740301560016f0fa020000605d038dc5d081f192273f14302e00a401"
  24.         "000000000000000000006d6f6465726e2e66616d696c792e733032653131"
  25.         "2e31303830702e626c757261792e783236342d6269612e6d6b76")
  26. arend = "75577b0f40140039e39c7a000000000000000000"
  27.  
  28. def extract_part(stored_file, output_file):
  29.     with open(stored_file, "rb") as af:
  30.         af.seek(start)
  31.         with open(output_file, "wb") as data:
  32.             data.write(af.read(amount))
  33.  
  34. def calc_crc(new_data, previous_crc=None):  
  35.     """calculate the crc needed in the header from the file/RR
  36.     previous_volume: not used: not a running crc!"""
  37.     # we only look at the stored data in the current file
  38.     if previous_crc:
  39.         crc = zlib.crc32(new_data, previous_crc) & 0xFFFFFFFF
  40.     else:
  41.         crc = zlib.crc32(new_data) & 0xFFFFFFFF
  42.     return crc
  43.  
  44. def fix_archive_flags(header, first_volume=False):
  45.     """ necessary because we start from .rar or need .rar
  46.     first_volume is true if we need to create the .rar file """
  47.     (flags, ) = struct.unpack("<H", header[3:5])
  48.     if first_volume and not flags & 0x0100:
  49.         flags += 0x0100 # fist volume flag
  50.     elif not first_volume and flags & 0x0100: # the flag is actually set
  51.         flags -= 0x0100
  52.     fixed = header[:3] + struct.pack("<H", flags) + header[5:]
  53.     header_crc = zlib.crc32(fixed[2:]) & 0xFFFF
  54.     return struct.pack("<H", header_crc) + fixed[2:]
  55.  
  56. def fix_file_flags(header, first_volume=False):
  57.     """ necessary because we start from .rar or need .rar
  58.     first_volume is true if we need to create the .rar file """
  59.     (flags, ) = struct.unpack("<H", header[3:5])
  60.     if first_volume and flags & 0x0001:
  61.         flags -= 0x0001 # file continued from previous volume
  62.     elif not first_volume and not flags & 0x0001:
  63.         flags += 0x0001
  64.     fixed = header[:3] + struct.pack("<H", flags) + header[5:]
  65.     header_crc = zlib.crc32(fixed[2:]) & 0xFFFF
  66.     return struct.pack("<H", header_crc) + fixed[2:]
  67.  
  68. def fix_file_header(file_header, part_crc, timedate_32bit):
  69.     """ fix the headers before the data """
  70.     lenb = len(file_header)
  71.     # fix crc of the RAR part
  72.     before = file_header[:7+9]
  73.     after = file_header[7+9+4:]
  74.     crc_fixed = before + struct.pack("<I", part_crc) + after
  75.     assert lenb == len(crc_fixed) == 86
  76.    
  77.     # fix the time if necessary
  78.     before = crc_fixed[:7+9+4]
  79.     after = crc_fixed[7+9+4+4:]
  80.     fixed_crc_header = before + struct.pack("<I", timedate_32bit) + after
  81.     assert lenb == len(fixed_crc_header)
  82.    
  83.     header_crc = zlib.crc32(fixed_crc_header[2:]) & 0xFFFF
  84.     fixed_crc_header = struct.pack("<H", header_crc) + fixed_crc_header[2:]
  85.     assert lenb == len(fixed_crc_header)
  86.     return fixed_crc_header
  87.        
  88. def fix_end_header(end_header, crc_all, file_number):
  89.     if end_header == "":
  90.         return ""
  91.     before = end_header[:7]
  92.     after = end_header[7+4+2:]
  93.    
  94.     # change 12 to the next volume
  95.     fixedh = (before + struct.pack("<I", crc_all) +
  96.               struct.pack("<H", file_number) + after)
  97.     header_crc = zlib.crc32(fixedh[2:]) & 0xFFFF
  98.     return struct.pack("<H", header_crc) + fixedh[2:]
  99.  
  100. MARKER = MARKER.decode('hex')
  101. archive = archive.decode('hex')
  102. fileh = fileh.decode('hex')
  103. arend = arend.decode('hex')
  104.  
  105. #print("Grabbing data from extracted file.")
  106. #extract_part(full, part)
  107.  
  108.  
  109. print("Calculating CRC file data.")
  110. with open(part, "rb") as data:
  111.     part_crc = calc_crc(data.read())
  112.  
  113. print("Fixing flags.")
  114. archive = fix_archive_flags(archive)
  115. fileh = fix_file_flags(fileh)
  116.  
  117. with open(part, "rb") as fh:
  118.     pdata = fh.read()
  119.    
  120. # f192 to 3793
  121. # 0x92F1 to 0x9337
  122. # to find: fa92273f 0x3F2792FA
  123.  
  124. # fixing time
  125. for time_index in range(0x3F2792F1, 0x3F279337):
  126.     print("Fixing file header.")
  127.     fileh = fix_file_header(fileh, part_crc, time_index)
  128.    
  129.     print("Calculating CRC complete RAR volume.")
  130.     # everything except the last 20 bytes
  131.     start = MARKER + archive + fileh
  132.  
  133.     crc_all = calc_crc(pdata, calc_crc(start))
  134. #   crc_all2 = calc_crc(start + pdata)
  135. #   assert crc_all == crc_all2
  136.    
  137. #   print("Fixing archive end header.")
  138. #   arend = fix_end_header(arend, crc_all, filenb)
  139. #  
  140. #   if zlib.crc32(arend, crc_all) & 0xffffffff == 0xfa6f96b5:
  141. #       print("found it: %d" % i)
  142. #       break
  143.     if crc_all & 0xffffffff == sfv_crc:
  144.         print("found it: %d" % time_index)
  145.         break
  146.  
  147. print("Creating %s." % todo)
  148. with open(todo, "wb") as fh:
  149.     fh.write(start)
  150.     with open(part, "rb") as fpart:
  151.         fh.write(fpart.read())
  152. #   fh.write(arend)
  153.    
  154. print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement