2 # vim: set ts=4 sts=4 sw=4 textwidth=80:
4 # This breaks my convention, but python requires that it be
5 # at the top of the file ...
6 from __future__ import with_statement
9 Module which holds PAR2 file parsing functions.
11 Much of this code was borrowed from the excellent cfv project.
12 See http://cfv.sourceforge.net/ for a copy.
15 __author__ = "Ira W. Snyder (devel@irasnyder.com)"
16 __copyright__ = "Copyright (c) 2006-2008 Ira W. Snyder (devel@irasnyder.com)"
17 __license__ = "GNU GPL v2 (or, at your option, any later version)"
19 # par2parser.py -- PAR2 file parsing utility
21 # Copyright (C) 2006-2008 Ira W. Snyder (devel@irasnyder.com)
23 # This program is free software; you can redistribute it and/or modify
24 # it under the terms of the GNU General Public License as published by
25 # the Free Software Foundation; either version 2 of the License, or
26 # (at your option) any later version.
28 # This program is distributed in the hope that it will be useful,
29 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 # GNU General Public License for more details.
33 # You should have received a copy of the GNU General Public License
34 # along with this program; if not, write to the Free Software
35 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 import struct, errno, os, md5
40 ################################################################################
50 ################################################################################
52 # Get all of the filenames protected by the parityFile
54 # This code was almost copied from the CFV project
55 # See the header of this file for more information
56 def getProtectedFiles(directory, parityFile):
58 assert os.path.isdir(directory)
59 assert os.path.isfile(os.path.join(directory, parityFile))
61 with open(os.path.join(directory, parityFile), 'rb') as file:
63 # We always want to do crc checks
66 pkt_header_fmt = '< 8s Q 16s 16s 16s'
67 pkt_header_size = struct.calcsize(pkt_header_fmt)
68 file_pkt_fmt = '< 16s 16s 16s Q'
69 file_pkt_size = struct.calcsize(file_pkt_fmt)
70 main_pkt_fmt = '< Q I'
71 main_pkt_size = struct.calcsize(main_pkt_fmt)
74 expected_file_ids = None
78 d = file.read(pkt_header_size)
82 magic, pkt_len, pkt_md5, set_id, pkt_type = struct.unpack(pkt_header_fmt, d)
85 control_md5 = md5.new()
86 control_md5.update(d[0x20:])
87 d = file.read(pkt_len - pkt_header_size)
90 if control_md5.digest() != pkt_md5:
91 raise EnvironmentError, (errno.EINVAL, \
92 "corrupt par2 file - bad packet hash")
94 if pkt_type == 'PAR 2.0\0FileDesc':
96 d = file.read(pkt_len - pkt_header_size)
98 file_id, file_md5, file_md5_16k, file_size = \
99 struct.unpack(file_pkt_fmt, d[:file_pkt_size])
101 if seen_file_ids.get(file_id) is None:
102 seen_file_ids[file_id] = 1
103 filename = chompnulls(d[file_pkt_size:])
104 filenames.append(filename)
106 elif pkt_type == "PAR 2.0\0Main\0\0\0\0":
108 d = file.read(pkt_len - pkt_header_size)
110 if expected_file_ids is None:
111 expected_file_ids = []
112 slice_size, num_files = struct.unpack(main_pkt_fmt, d[:main_pkt_size])
113 num_nonrecovery = (len(d)-main_pkt_size)/16 - num_files
115 for i in range(main_pkt_size,main_pkt_size+(num_files+num_nonrecovery)*16,16):
116 expected_file_ids.append(d[i:i+16])
120 file.seek(pkt_len - pkt_header_size, 1)
122 if expected_file_ids is None:
123 raise EnvironmentError, (errno.EINVAL, \
124 "corrupt or unsupported par2 file - no main packet found")
126 for id in expected_file_ids:
127 if not seen_file_ids.has_key(id):
128 raise EnvironmentError, (errno.EINVAL, \
129 "corrupt or unsupported par2 file - " \
130 "expected file description packet not found")
134 ################################################################################
139 ################################################################################
141 if __name__ == '__main__':