OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // rezip is a tool which is used to modify zip files. It reads in a | |
6 // zip file and outputs a new zip file after applying various | |
7 // transforms. The tool is used in the Android Chromium build process | |
8 // to modify an APK file (which are zip files). The main application | |
9 // of this is to modify the APK so that the shared library is no | |
10 // longer compressed. Ironically, this saves both transmission and | |
11 // device drive space. It saves transmission space because | |
12 // uncompressed libraries make much smaller deltas with previous | |
13 // versions. It saves device drive space because it is no longer | |
14 // necessary to have both a compressed and uncompressed shared library | |
15 // on the device. To achieve this the uncompressed library is opened | |
16 // directly from within the APK using the "crazy" linker. | |
17 | |
18 #include <assert.h> | |
19 #include <string.h> | |
20 | |
21 #include <iostream> | |
22 #include <sstream> | |
23 #include <string> | |
24 | |
25 #include "third_party/zlib/contrib/minizip/unzip.h" | |
26 #include "third_party/zlib/contrib/minizip/zip.h" | |
27 | |
28 const int kMaxFilenameInZip = 256; | |
29 const int kMaxExtraFieldInZip = 8192; | |
30 const int kBufferSize = 4096; | |
31 // Note do not use sysconf(_SC_PAGESIZE) here as that will give you the | |
32 // page size of the host, this should be the page size of the target. | |
33 const int kPageSize = 4096; | |
bulach
2014/06/17 16:07:08
nit: how about kTargetPageSize?
Anton
2014/06/18 13:30:41
changed to PageSizeOnDevice
| |
34 | |
35 // This is done to avoid having to make a dependency on all of base. | |
36 class LogStream { | |
37 public: | |
38 ~LogStream() { | |
39 stream_.flush(); | |
40 std::cerr << stream_.str() << std::endl; | |
41 } | |
42 std::ostream& stream() { | |
43 return stream_; | |
44 } | |
45 private: | |
46 std::ostringstream stream_; | |
47 }; | |
48 | |
49 #define LOG(tag) (LogStream().stream() << #tag << ":") | |
50 | |
51 // Copy the data from the currently opened file in the zipfile we are unzipping | |
52 // into the currently opened file of the zipfile we are zipping. | |
53 static bool CopySubfile(unzFile in_file, | |
bulach
2014/06/17 16:07:08
nit: I think it's more common to have an anonymous
Anton
2014/06/18 13:30:41
C++ style says either is fine.
| |
54 zipFile out_file, | |
55 const char* in_zip_filename, | |
56 const char* out_zip_filename, | |
57 const char* in_filename, | |
58 const char* out_filename) { | |
59 char buf[kBufferSize]; | |
60 | |
61 int bytes = 0; | |
62 while (true) { | |
63 bytes = unzReadCurrentFile(in_file, buf, sizeof(buf)); | |
64 if (bytes < 0) { | |
65 LOG(ERROR) << "failed to read from " << in_filename << " in zipfile " | |
66 << in_zip_filename; | |
67 return false; | |
68 } | |
69 | |
70 if (bytes == 0) { | |
71 break; | |
72 } | |
73 | |
74 if (ZIP_OK != zipWriteInFileInZip(out_file, buf, bytes)) { | |
75 LOG(ERROR) << "failed to write from " << out_filename << " in zipfile " | |
76 << out_zip_filename; | |
77 return false; | |
78 } | |
79 } | |
80 | |
81 return true; | |
82 } | |
83 | |
84 static zip_fileinfo BuildOutInfo(const unz_file_info& in_info) { | |
85 zip_fileinfo out_info; | |
86 out_info.tmz_date.tm_sec = in_info.tmu_date.tm_sec; | |
87 out_info.tmz_date.tm_min = in_info.tmu_date.tm_min; | |
88 out_info.tmz_date.tm_hour = in_info.tmu_date.tm_hour; | |
89 out_info.tmz_date.tm_mday = in_info.tmu_date.tm_mday; | |
90 out_info.tmz_date.tm_mon = in_info.tmu_date.tm_mon; | |
91 out_info.tmz_date.tm_year = in_info.tmu_date.tm_year; | |
92 | |
93 out_info.dosDate = in_info.dosDate; | |
94 out_info.internal_fa = in_info.internal_fa; | |
95 out_info.external_fa = in_info.external_fa; | |
96 return out_info; | |
97 } | |
98 | |
99 // RAII pattern for closing the unzip file. | |
100 class UnzipCloser { | |
bulach
2014/06/17 16:07:08
nit: I think these classes are normally named "Sco
Anton
2014/06/18 13:30:41
Done.
| |
101 public: | |
102 UnzipCloser(unzFile z_file, const char* z_filename) | |
103 : z_file_(z_file), z_filename_(z_filename) {} | |
104 | |
105 ~UnzipCloser() { | |
106 if (unzClose(z_file_) != UNZ_OK) { | |
107 LOG(ERROR) << "failed to close input zipfile " << z_filename_; | |
108 exit(1); | |
109 } | |
110 } | |
111 | |
112 private: | |
113 const char* z_filename_; | |
114 unzFile z_file_; | |
115 }; | |
116 | |
117 // RAII pattern for closing the out zip file. | |
118 class ZipCloser { | |
119 public: | |
120 ZipCloser(zipFile z_file, const char* z_filename) | |
121 : z_file_(z_file), z_filename_(z_filename) {} | |
122 | |
123 ~ZipCloser() { | |
124 if (zipClose(z_file_, NULL) != ZIP_OK) { | |
125 LOG(ERROR) << "failed to close output zipfile" << z_filename_; | |
126 exit(1); | |
127 } | |
128 } | |
129 | |
130 private: | |
131 const char* z_filename_; | |
132 zipFile z_file_; | |
133 }; | |
134 | |
135 typedef std::string (*RenameFun)(const char* in_filename); | |
136 typedef int (*AlignFun)(const char* in_filename, | |
137 unzFile in_file, | |
138 char* extra_buffer, | |
139 int size); | |
140 typedef bool (*InflateFun)(const char* filename); | |
141 | |
142 static bool IsPrefixLibraryFilename(const char* filename, | |
143 const char* base_prefix) { | |
144 // We are basically matching "lib/[^/]*/<base_prefix>lib.*[.]so". | |
145 // However, we don't have C++11 regex, so we just handroll the test. | |
146 // Also we exclude "libchromium_android_linker.so" as a match. | |
147 const std::string filename_str = filename; | |
148 const std::string prefix = "lib/"; | |
149 const std::string suffix = ".so"; | |
150 | |
151 if (filename_str.length() < suffix.length() + prefix.length()) { | |
152 // too short | |
153 return false; | |
154 } | |
155 | |
156 if (filename_str.compare(0, prefix.size(), prefix) != 0) { | |
157 // does not start with "lib/" | |
158 return false; | |
159 } | |
160 | |
161 if (filename_str.compare(filename_str.length() - suffix.length(), | |
162 suffix.length(), | |
163 suffix) != 0) { | |
164 // does not end with ".so" | |
165 return false; | |
166 } | |
167 | |
168 const size_t last_slash = filename_str.find_last_of('/'); | |
169 if (last_slash < prefix.length()) { | |
170 // Only one slash | |
171 return false; | |
172 } | |
173 | |
174 const size_t second_slash = filename_str.find_first_of('/', prefix.length()); | |
175 if (second_slash != last_slash) { | |
176 // filename_str contains more than two slashes. | |
177 return false; | |
178 } | |
179 | |
180 const std::string libprefix = std::string(base_prefix) + "lib"; | |
181 if (filename_str.compare(last_slash + 1, libprefix.length(), libprefix) != | |
182 0) { | |
183 // basename piece does not start with <base_prefix>"lib" | |
184 return false; | |
185 } | |
186 | |
187 const std::string linker = "libchromium_android_linker.so"; | |
bulach
2014/06/17 16:07:08
nit: it may be nicer to get this as a param passed
Anton
2014/06/18 13:30:41
Actually I would prefer it passed the filename of
| |
188 if (last_slash + 1 + linker.length() == filename_str.length() && | |
189 filename_str.compare(last_slash + 1, linker.length(), linker) == 0) { | |
190 // Do not match the linker. | |
191 return false; | |
192 } | |
193 return true; | |
194 } | |
195 | |
196 static bool IsLibraryFilename(const char* filename) { | |
197 return IsPrefixLibraryFilename(filename, ""); | |
198 } | |
199 | |
200 static bool IsCrazyLibraryFilename(const char* filename) { | |
201 return IsPrefixLibraryFilename(filename, "crazy."); | |
202 } | |
203 | |
204 static std::string RenameLibrary(const char* in_filename) { | |
rmcilroy
2014/06/18 09:17:03
Add a comment to describe what this function does.
Anton
2014/06/18 13:30:41
Done.
| |
205 if (!IsLibraryFilename(in_filename)) { | |
206 // Don't rename | |
207 return in_filename; | |
208 } | |
209 | |
210 std::string filename_str = in_filename; | |
211 size_t last_slash = filename_str.find_last_of('/'); | |
212 if (last_slash == std::string::npos || | |
213 last_slash == filename_str.length() - 1) { | |
214 return in_filename; | |
215 } | |
216 | |
217 // We rename the library, so that the Android Package Manager | |
218 // no longer extracts the library. | |
219 const std::string basename_prefix = "crazy."; | |
220 return filename_str.substr(0, last_slash + 1) + basename_prefix + | |
221 filename_str.substr(last_slash + 1); | |
222 } | |
223 | |
224 static int PageAlignCrazyLibrary(const char* in_filename, | |
rmcilroy
2014/06/18 09:17:02
Comment describing what the function does please.
Anton
2014/06/18 13:30:41
Done.
| |
225 unzFile in_file, | |
226 char* extra_buffer, | |
227 int extra_size) { | |
228 if (!IsCrazyLibraryFilename(in_filename)) { | |
229 return extra_size; | |
230 } | |
231 const ZPOS64_T pos = unzGetCurrentFileZStreamPos64(in_file); | |
232 const int padding = kPageSize - (pos % kPageSize); | |
233 if (padding == kPageSize) { | |
234 return extra_size; | |
235 } | |
236 | |
237 assert(extra_size < kMaxExtraFieldInZip - padding); | |
238 memset(extra_buffer + extra_size, 0, padding); | |
239 return extra_size + padding; | |
240 } | |
241 | |
242 // As only the read side API provides offsets, we check that we added the | |
243 // correct amount of padding by reading the zip file we just generated. | |
244 static bool CheckPageAlign(const char* out_zip_filename) { | |
245 unzFile in_file = unzOpen(out_zip_filename); | |
bulach
2014/06/17 16:07:08
nit: this is repeated 315-320, how about rolling i
Anton
2014/06/18 13:30:41
Done.
| |
246 if (in_file == NULL) { | |
247 LOG(ERROR) << "failed to open zipfile " << out_zip_filename; | |
248 return false; | |
249 } | |
250 UnzipCloser unzipCloser(in_file, out_zip_filename); | |
251 | |
252 int err = 0; | |
253 bool checked = false; | |
254 while (true) { | |
255 char in_filename[kMaxFilenameInZip + 1]; | |
256 // Get info and extra field for current file. | |
257 unz_file_info in_info; | |
258 err = unzGetCurrentFileInfo(in_file, | |
259 &in_info, | |
260 in_filename, | |
261 sizeof(in_filename) - 1, | |
262 NULL, | |
263 0, | |
264 NULL, | |
265 0); | |
266 if (err != UNZ_OK) { | |
267 LOG(ERROR) << "failed to get filename" << out_zip_filename; | |
268 return false; | |
269 } | |
270 assert(in_info.size_filename <= kMaxFilenameInZip); | |
271 in_filename[in_info.size_filename] = '\0'; | |
272 | |
273 if (IsCrazyLibraryFilename(in_filename)) { | |
274 err = unzOpenCurrentFile(in_file); | |
275 if (err != UNZ_OK) { | |
276 LOG(ERROR) << "failed to open subfile" << out_zip_filename << " " | |
277 << in_filename; | |
278 return false; | |
279 } | |
280 | |
281 const ZPOS64_T pos = unzGetCurrentFileZStreamPos64(in_file); | |
282 const int alignment = pos % kPageSize; | |
283 checked = (alignment == 0); | |
284 if (!checked) { | |
285 LOG(ERROR) << "Failed to page align library " << in_filename | |
286 << ", position " << pos << " alignment " << alignment; | |
287 } | |
288 | |
289 err = unzCloseCurrentFile(in_file); | |
290 if (err != UNZ_OK) { | |
291 LOG(ERROR) << "failed to close subfile" << out_zip_filename << " " | |
292 << in_filename; | |
293 return false; | |
294 } | |
295 } | |
296 | |
297 const int next = unzGoToNextFile(in_file); | |
298 if (next == UNZ_END_OF_LIST_OF_FILE) { | |
299 break; | |
300 } | |
301 if (next != UNZ_OK) { | |
302 LOG(ERROR) << "failed to go to next file" << out_zip_filename; | |
303 return false; | |
304 } | |
305 } | |
306 return checked; | |
307 } | |
308 | |
309 // Copy files from one archive to another applying alignment, rename and | |
310 // inflate transformations if given. | |
311 static bool Rezip(const char* in_zip_filename, | |
312 const char* out_zip_filename, | |
313 AlignFun align_fun, | |
314 RenameFun rename_fun, | |
315 InflateFun inflate_fun) { | |
316 unzFile in_file = unzOpen(in_zip_filename); | |
317 if (in_file == NULL) { | |
318 LOG(ERROR) << "failed to open zipfile " << in_zip_filename; | |
319 return false; | |
320 } | |
321 UnzipCloser unzipCloser(in_file, in_zip_filename); | |
322 | |
323 zipFile out_file = zipOpen(out_zip_filename, APPEND_STATUS_CREATE); | |
324 if (unzGoToFirstFile(in_file) != UNZ_OK) { | |
325 LOG(ERROR) << "failed to go to first file in " << in_zip_filename; | |
326 return false; | |
327 } | |
328 ZipCloser zipCloser(out_file, out_zip_filename); | |
329 | |
330 int err = 0; | |
331 while (true) { | |
rmcilroy
2014/06/18 09:17:02
I'm not keen on the while (true) with the break in
Anton
2014/06/18 13:30:41
Earlier reviewer made me remove the do { } while (
| |
332 char in_filename[kMaxFilenameInZip + 1]; | |
333 // Get info and extra field for current file. | |
334 char extra_buffer[kMaxExtraFieldInZip]; | |
335 unz_file_info in_info; | |
336 err = unzGetCurrentFileInfo(in_file, | |
337 &in_info, | |
338 in_filename, | |
339 sizeof(in_filename) - 1, | |
340 &extra_buffer, | |
341 sizeof(extra_buffer), | |
342 NULL, | |
343 0); | |
344 if (err != UNZ_OK) { | |
345 LOG(ERROR) << "failed to get filename " << in_zip_filename; | |
346 return false; | |
347 } | |
348 assert(in_info.size_filename <= kMaxFilenameInZip); | |
349 in_filename[in_info.size_filename] = '\0'; | |
350 | |
351 std::string out_filename = in_filename; | |
352 if (rename_fun != NULL) { | |
353 out_filename = rename_fun(in_filename); | |
354 } | |
355 | |
356 bool inflate = false; | |
357 if (inflate_fun != NULL) { | |
358 inflate = inflate_fun(in_filename); | |
rmcilroy
2014/06/18 09:17:02
rename inflate_fun to inflate_filename_filter_fun
Anton
2014/06/18 13:30:41
It is not a filter, so I changed it to InflatePred
| |
359 } | |
360 | |
361 // Open the current file. | |
362 int method = 0; | |
363 int level = 0; | |
364 int raw = !inflate; | |
365 err = unzOpenCurrentFile2(in_file, &method, &level, raw); | |
366 if (inflate) { | |
367 method = Z_NO_COMPRESSION; | |
368 level = 0; | |
369 } | |
370 | |
371 if (err != UNZ_OK) { | |
372 LOG(ERROR) << "failed to open subfile " << in_zip_filename << " " | |
373 << in_filename; | |
374 return false; | |
375 } | |
376 | |
377 // Get the extra field from the local header. | |
378 char local_extra_buffer[kMaxExtraFieldInZip]; | |
379 int local_extra_size = unzGetLocalExtrafield( | |
380 in_file, &local_extra_buffer, sizeof(local_extra_buffer)); | |
381 | |
382 if (align_fun != NULL) { | |
383 local_extra_size = | |
384 align_fun(in_filename, in_file, local_extra_buffer, local_extra_size); | |
385 } | |
386 | |
387 const char* local_extra = local_extra_size > 0 ? local_extra_buffer : NULL; | |
388 const char* extra = in_info.size_file_extra > 0 ? extra_buffer : NULL; | |
389 | |
390 // Build the output info structure from the input info structure. | |
391 const zip_fileinfo out_info = BuildOutInfo(in_info); | |
392 | |
393 const int ret = zipOpenNewFileInZip4(out_file, | |
394 out_filename.c_str(), | |
395 &out_info, | |
396 local_extra, | |
397 local_extra_size, | |
398 extra, | |
399 in_info.size_file_extra, | |
400 /* comment */ NULL, | |
401 method, | |
402 level, | |
403 /* raw */ 1, | |
404 /* windowBits */ 0, | |
405 /* memLevel */ 0, | |
406 /* strategy */ 0, | |
407 /* password */ NULL, | |
408 /* crcForCrypting */ 0, | |
409 in_info.version, | |
410 /*flagBase */ 0); | |
bulach
2014/06/17 16:07:08
nit: space after /*
Anton
2014/06/18 13:30:41
Done.
| |
411 | |
412 if (ZIP_OK != ret) { | |
413 LOG(ERROR) << "failed to open subfile " << out_zip_filename << " " | |
414 << out_filename; | |
415 return false; | |
416 } | |
417 | |
418 if (!CopySubfile(in_file, | |
419 out_file, | |
420 in_zip_filename, | |
421 out_zip_filename, | |
422 in_filename, | |
423 out_filename.c_str())) { | |
424 return false; | |
425 } | |
426 | |
427 if (ZIP_OK != zipCloseFileInZipRaw( | |
428 out_file, in_info.uncompressed_size, in_info.crc)) { | |
429 LOG(ERROR) << "failed to close subfile " << out_zip_filename << " " | |
430 << out_filename; | |
431 return false; | |
432 } | |
433 | |
434 err = unzCloseCurrentFile(in_file); | |
435 if (err != UNZ_OK) { | |
436 LOG(ERROR) << "failed to close subfile " << in_zip_filename << " " | |
437 << in_filename; | |
438 return false; | |
439 } | |
440 const int next = unzGoToNextFile(in_file); | |
441 if (next == UNZ_END_OF_LIST_OF_FILE) { | |
442 break; | |
443 } | |
444 if (next != UNZ_OK) { | |
445 LOG(ERROR) << "failed to go to next file" << in_zip_filename; | |
446 return false; | |
447 } | |
448 } | |
449 | |
450 return true; | |
451 } | |
452 | |
453 int main(int argc, const char* argv[]) { | |
454 if (argc != 4) { | |
455 LOG(ERROR) << "Usage: <action> <in_zipfile> <out_zipfile>"; | |
456 LOG(ERROR) << " <action> is 'inflatealign', 'dropdescriptors' or 'rename'"; | |
rmcilroy
2014/06/18 09:17:02
Please add some description of what the various ac
Anton
2014/06/18 13:30:41
Done.
| |
457 exit(1); | |
458 } | |
459 | |
460 const char* action = argv[1]; | |
461 const char* in_zip_filename = argv[2]; | |
462 const char* out_zip_filename = argv[3]; | |
463 | |
464 InflateFun inflate_fun = NULL; | |
465 AlignFun align_fun = NULL; | |
466 RenameFun rename_fun = NULL; | |
467 bool checkPageAlign = false; | |
bulach
2014/06/17 16:07:08
nit: check_page_align
Anton
2014/06/18 13:30:41
Done.
| |
468 if (strcmp("inflatealign", action) == 0) { | |
rmcilroy
2014/06/18 09:17:03
The functions themselves are specific to the crazy
Anton
2014/06/18 13:30:41
I prefer my non-ugly names.
rmcilroy
2014/06/18 14:04:20
This is fine with the added usage instructions (al
| |
469 inflate_fun = &IsCrazyLibraryFilename; | |
470 align_fun = &PageAlignCrazyLibrary; | |
471 checkPageAlign = true; | |
472 } else if (strcmp("rename", action) == 0) { | |
473 rename_fun = &RenameLibrary; | |
474 } else if (strcmp("dropdescriptors", action) == 0) { | |
475 // Minizip does not know about data descriptors, so the default | |
476 // copying action will drop the descriptors. This should be fine | |
477 // as data descriptors are redundant information. | |
478 // Note we need to explicitly drop the descriptors before trying to | |
479 // do alignment otherwise we will miscalculate the position because | |
480 // we don't know about the data descriptors. | |
481 } else { | |
482 LOG(ERROR) << "Usage: <action> should be 'inflatealign', " | |
483 "'dropdescriptors' or 'rename'"; | |
484 exit(1); | |
485 } | |
486 | |
487 if (!Rezip(in_zip_filename, | |
488 out_zip_filename, | |
489 align_fun, | |
490 rename_fun, | |
491 inflate_fun)) { | |
492 exit(1); | |
493 } | |
494 if (checkPageAlign && !CheckPageAlign(out_zip_filename)) { | |
495 exit(1); | |
496 } | |
497 return 0; | |
498 } | |
OLD | NEW |