[PAR2PARSER] Fix Par2Parser class
[rarslave2.git] / Par2Parser.py
1 #!/usr/bin/env python
2 # vim: set ts=4 sts=4 sw=4 textwidth=92:
3
4 ################################################################################
5 # The PAR2 Parser
6 #
7 # This was stolen from cfv (see http://cfv.sourceforge.net/ for a copy)
8 ################################################################################
9
10 import struct, errno, os, md5
11
12 def chompnulls(line):
13         p = line.find('\0')
14         if p < 0: return line
15         else:     return line[:p]
16
17 def get_protected_files (dir, filename):
18         """Get all of the filenames that are protected by the par2
19         file given as the filename"""
20
21         assert os.path.isdir (dir) # MUST be a valid directory
22         assert os.path.isfile (os.path.join (dir, filename))
23
24         full_filename = os.path.join (dir, filename)
25
26         try:
27                 file = open(full_filename, 'rb')
28         except:
29                 print 'Could not open %s' % (full_filename, )
30                 return []
31
32         # We always want to do crc checks
33         docrcchecks = True
34
35         pkt_header_fmt = '< 8s Q 16s 16s 16s'
36         pkt_header_size = struct.calcsize(pkt_header_fmt)
37         file_pkt_fmt = '< 16s 16s 16s Q'
38         file_pkt_size = struct.calcsize(file_pkt_fmt)
39         main_pkt_fmt = '< Q I'
40         main_pkt_size = struct.calcsize(main_pkt_fmt)
41
42         seen_file_ids = {}
43         expected_file_ids = None
44         filenames = []
45
46         while 1:
47                 d = file.read(pkt_header_size)
48                 if not d:
49                         break
50
51                 magic, pkt_len, pkt_md5, set_id, pkt_type = struct.unpack(pkt_header_fmt, d)
52
53                 if docrcchecks:
54                         control_md5 = md5.new()
55                         control_md5.update(d[0x20:])
56                         d = file.read(pkt_len - pkt_header_size)
57                         control_md5.update(d)
58
59                         if control_md5.digest() != pkt_md5:
60                                 raise EnvironmentError, (errno.EINVAL, \
61                                         "corrupt par2 file - bad packet hash")
62
63                 if pkt_type == 'PAR 2.0\0FileDesc':
64                         if not docrcchecks:
65                                 d = file.read(pkt_len - pkt_header_size)
66
67                         file_id, file_md5, file_md5_16k, file_size = \
68                                 struct.unpack(file_pkt_fmt, d[:file_pkt_size])
69
70                         if seen_file_ids.get(file_id) is None:
71                                 seen_file_ids[file_id] = 1
72                                 filename = chompnulls(d[file_pkt_size:])
73                                 filenames.append(filename)
74
75                 elif pkt_type == "PAR 2.0\0Main\0\0\0\0":
76                         if not docrcchecks:
77                                 d = file.read(pkt_len - pkt_header_size)
78
79                         if expected_file_ids is None:
80                                 expected_file_ids = []
81                                 slice_size, num_files = struct.unpack(main_pkt_fmt, d[:main_pkt_size])
82                                 num_nonrecovery = (len(d)-main_pkt_size)/16 - num_files
83
84                                 for i in range(main_pkt_size,main_pkt_size+(num_files+num_nonrecovery)*16,16):
85                                         expected_file_ids.append(d[i:i+16])
86
87                 else:
88                         if not docrcchecks:
89                                 file.seek(pkt_len - pkt_header_size, 1)
90
91         if expected_file_ids is None:
92                 raise EnvironmentError, (errno.EINVAL, \
93                         "corrupt or unsupported par2 file - no main packet found")
94
95         for id in expected_file_ids:
96                 if not seen_file_ids.has_key(id):
97                         raise EnvironmentError, (errno.EINVAL, \
98                                 "corrupt or unsupported par2 file - " \
99                                 "expected file description packet not found")
100
101         return filenames
102
103 def main ():
104         pass
105
106 if __name__ == '__main__':
107         main ()
108