freemyipod r860 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r859‎ | r860 | r861 >
Date:00:19, 3 January 2012
Author:farthen
Status:new
Tags:
Comment:
emcore/tools/libipodcrypto.py: Clean up the functions that deal with file operations, add some extra exception handling to file operations and properly remove the outfile if an error occurred during the function call.
Modified paths:
  • /emcore/trunk/tools/libipodcrypto.py (modified) (history)

Diff [purge]

Index: emcore/trunk/tools/libipodcrypto.py
@@ -144,74 +144,42 @@
145145 emcore.free(addr)
146146 return data
147147
 148+def fileoperation(infilepath, outfilepath, function):
 149+ with open(infilepath, "rb") as infile:
 150+ infiledata = infile.read()
 151+
 152+ try:
 153+ outfiledata = function(infiledata)
 154+ except:
 155+ os.remove(outfilepath)
 156+ raise
 157+
 158+ with open(outfilepath, "wb") as outfile:
 159+ outfile.write(outfiledata)
148160
149161 def s5l8701cryptdfufile(infile, outfile):
150 - infile = open(infile, "rb")
151 - outfile = open(outfile, "wb")
152 - outfile.write(s5l8701cryptdfu(infile.read()))
153 - infile.close()
154 - outfile.close()
 162+ fileoperation(infile, outfile, s5l8701cryptdfu)
155163
156 -
157164 def s5l8701decryptdfufile(infile, outfile):
158 - infile = open(infile, "rb")
159 - outfile = open(outfile, "wb")
160 - outfile.write(s5l8701decryptdfu(infile.read()))
161 - infile.close()
162 - outfile.close()
 165+ fileoperation(infile, outfile, s5l8701decryptdfu)
163166
164 -
165167 def s5l8701cryptfirmwarefile(infile, outfile):
166 - infile = open(infile, "rb")
167 - outfile = open(outfile, "wb")
168 - outfile.write(s5l8701cryptfirmware(infile.read()))
169 - infile.close()
170 - outfile.close()
 168+ fileoperation(infile, outfile, s5l8701cryptfirmware)
171169
172 -
173170 def s5l8701decryptfirmwarefile(infile, outfile):
174 - infile = open(infile, "rb")
175 - outfile = open(outfile, "wb")
176 - outfile.write(s5l8701decryptfirmware(infile.read()))
177 - infile.close()
178 - outfile.close()
 171+ fileoperation(infile, outfile, s5l8701decryptfirmware)
179172
180 -
181173 def s5l8702cryptnorfile(infile, outfile):
182 - infile = open(infile, "rb")
183 - outfile = open(outfile, "wb")
184 - outfile.write(s5l8702cryptnor(infile.read()))
185 - infile.close()
186 - outfile.close()
 174+ fileoperation(infile, outfile, s5l8702cryptnor)
187175
188 -
189176 def s5l8702decryptnorfile(infile, outfile):
190 - infile = open(infile, "rb")
191 - outfile = open(outfile, "wb")
192 - outfile.write(s5l8702decryptnor(infile.read()))
193 - infile.close()
194 - outfile.close()
 177+ fileoperation(infile, outfile, s5l8702decryptnor)
195178
196 -
197179 def s5l8702genpwnagefile(infile, outfile):
198 - infile = open(infile, "rb")
199 - outfile = open(outfile, "wb")
200 - outfile.write(s5l8702genpwnage(infile.read()))
201 - infile.close()
202 - outfile.close()
 180+ fileoperation(infile, outfile, s5l8702genpwnage)
203181
204 -
205182 def s5l8702genpwnagefile800(infile, outfile):
206 - infile = open(infile, "rb")
207 - outfile = open(outfile, "wb")
208 - outfile.write(s5l8702genpwnage800(infile.read()))
209 - infile.close()
210 - outfile.close()
 183+ fileoperation(infile, outfile, s5l8702genpwnage800)
211184
212 -
213185 def s5l8720genpwnagefile(infile, outfile):
214 - infile = open(infile, "rb")
215 - outfile = open(outfile, "wb")
216 - outfile.write(s5l8720genpwnage(infile.read()))
217 - infile.close()
218 - outfile.close()
 186+ fileoperation(infile, outfile, s5l8720genpwnage)