OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "third_party/zlib/google/zip.h" | 5 #include "third_party/zlib/google/zip_internal.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
| 9 #include "base/file_util.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/time/time.h" |
11 | 13 |
12 #if defined(USE_SYSTEM_MINIZIP) | 14 #if defined(USE_SYSTEM_MINIZIP) |
13 #include <minizip/ioapi.h> | 15 #include <minizip/ioapi.h> |
14 #include <minizip/unzip.h> | 16 #include <minizip/unzip.h> |
15 #include <minizip/zip.h> | 17 #include <minizip/zip.h> |
16 #else | 18 #else |
17 #include "third_party/zlib/contrib/minizip/unzip.h" | 19 #include "third_party/zlib/contrib/minizip/unzip.h" |
18 #include "third_party/zlib/contrib/minizip/zip.h" | 20 #include "third_party/zlib/contrib/minizip/zip.h" |
19 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
20 #include "third_party/zlib/contrib/minizip/iowin32.h" | 22 #include "third_party/zlib/contrib/minizip/iowin32.h" |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 free(opaque); | 225 free(opaque); |
224 return 0; | 226 return 0; |
225 } | 227 } |
226 | 228 |
227 // Returns the last error happened when reading or writing data. This function | 229 // Returns the last error happened when reading or writing data. This function |
228 // always returns zero, which means there are not any errors. | 230 // always returns zero, which means there are not any errors. |
229 int GetErrorOfZipBuffer(void* /*opaque*/, void* /*stream*/) { | 231 int GetErrorOfZipBuffer(void* /*opaque*/, void* /*stream*/) { |
230 return 0; | 232 return 0; |
231 } | 233 } |
232 | 234 |
| 235 // Returns a zip_fileinfo struct with the time represented by |file_time|. |
| 236 zip_fileinfo TimeToZipFileInfo(const base::Time& file_time) { |
| 237 base::Time::Exploded file_time_parts; |
| 238 file_time.LocalExplode(&file_time_parts); |
| 239 |
| 240 zip_fileinfo zip_info = {}; |
| 241 if (file_time_parts.year >= 1980) { |
| 242 // This if check works around the handling of the year value in |
| 243 // contrib/minizip/zip.c in function zip64local_TmzDateToDosDate |
| 244 // It assumes that dates below 1980 are in the double digit format. |
| 245 // Hence the fail safe option is to leave the date unset. Some programs |
| 246 // might show the unset date as 1980-0-0 which is invalid. |
| 247 zip_info.tmz_date.tm_year = file_time_parts.year; |
| 248 zip_info.tmz_date.tm_mon = file_time_parts.month - 1; |
| 249 zip_info.tmz_date.tm_mday = file_time_parts.day_of_month; |
| 250 zip_info.tmz_date.tm_hour = file_time_parts.hour; |
| 251 zip_info.tmz_date.tm_min = file_time_parts.minute; |
| 252 zip_info.tmz_date.tm_sec = file_time_parts.second; |
| 253 } |
| 254 |
| 255 return zip_info; |
| 256 } |
233 } // namespace | 257 } // namespace |
234 | 258 |
235 namespace zip { | 259 namespace zip { |
236 namespace internal { | 260 namespace internal { |
237 | 261 |
238 unzFile OpenForUnzipping(const std::string& file_name_utf8) { | 262 unzFile OpenForUnzipping(const std::string& file_name_utf8) { |
239 zlib_filefunc_def* zip_func_ptrs = NULL; | 263 zlib_filefunc_def* zip_func_ptrs = NULL; |
240 #if defined(OS_WIN) | 264 #if defined(OS_WIN) |
241 zlib_filefunc_def zip_funcs; | 265 zlib_filefunc_def zip_funcs; |
242 fill_win32_filefunc(&zip_funcs); | 266 fill_win32_filefunc(&zip_funcs); |
(...skipping 16 matching lines...) Expand all Loading... |
259 unzFile OpenHandleForUnzipping(HANDLE zip_handle) { | 283 unzFile OpenHandleForUnzipping(HANDLE zip_handle) { |
260 zlib_filefunc_def zip_funcs; | 284 zlib_filefunc_def zip_funcs; |
261 fill_win32_filefunc(&zip_funcs); | 285 fill_win32_filefunc(&zip_funcs); |
262 zip_funcs.zopen_file = HandleOpenFileFunc; | 286 zip_funcs.zopen_file = HandleOpenFileFunc; |
263 zip_funcs.opaque = zip_handle; | 287 zip_funcs.opaque = zip_handle; |
264 return unzOpen2("fd", &zip_funcs); | 288 return unzOpen2("fd", &zip_funcs); |
265 } | 289 } |
266 #endif | 290 #endif |
267 | 291 |
268 // static | 292 // static |
269 unzFile PreprareMemoryForUnzipping(const std::string& data) { | 293 unzFile PrepareMemoryForUnzipping(const std::string& data) { |
270 if (data.empty()) | 294 if (data.empty()) |
271 return NULL; | 295 return NULL; |
272 | 296 |
273 ZipBuffer* buffer = static_cast<ZipBuffer*>(malloc(sizeof(ZipBuffer))); | 297 ZipBuffer* buffer = static_cast<ZipBuffer*>(malloc(sizeof(ZipBuffer))); |
274 if (!buffer) | 298 if (!buffer) |
275 return NULL; | 299 return NULL; |
276 buffer->data = data.data(); | 300 buffer->data = data.data(); |
277 buffer->length = data.length(); | 301 buffer->length = data.length(); |
278 buffer->offset = 0; | 302 buffer->offset = 0; |
279 | 303 |
(...skipping 25 matching lines...) Expand all Loading... |
305 | 329 |
306 #if defined(OS_POSIX) | 330 #if defined(OS_POSIX) |
307 zipFile OpenFdForZipping(int zip_fd, int append_flag) { | 331 zipFile OpenFdForZipping(int zip_fd, int append_flag) { |
308 zlib_filefunc_def zip_funcs; | 332 zlib_filefunc_def zip_funcs; |
309 FillFdOpenFileFunc(&zip_funcs, zip_fd); | 333 FillFdOpenFileFunc(&zip_funcs, zip_fd); |
310 // Passing dummy "fd" filename to zlib. | 334 // Passing dummy "fd" filename to zlib. |
311 return zipOpen2("fd", append_flag, NULL, &zip_funcs); | 335 return zipOpen2("fd", append_flag, NULL, &zip_funcs); |
312 } | 336 } |
313 #endif | 337 #endif |
314 | 338 |
| 339 zip_fileinfo GetFileInfoForZipping(const base::FilePath& path) { |
| 340 base::Time file_time; |
| 341 base::File::Info file_info; |
| 342 if (base::GetFileInfo(path, &file_info)) |
| 343 file_time = file_info.last_modified; |
| 344 return TimeToZipFileInfo(file_time); |
| 345 } |
| 346 |
| 347 bool ZipOpenNewFileInZipWrapper(zipFile zip_file, |
| 348 const std::string& str_path, |
| 349 const zip_fileinfo* file_info) { |
| 350 // Section 4.4.4 http://www.pkware.com/documents/casestudies/APPNOTE.TXT |
| 351 // Setting the Language encoding flag so the file is told to be in utf-8. |
| 352 const uLong LANGUAGE_ENCODING_FLAG = 0x1 << 11; |
| 353 |
| 354 if (ZIP_OK != zipOpenNewFileInZip4( |
| 355 zip_file, // file |
| 356 str_path.c_str(), // filename |
| 357 file_info, // zipfi |
| 358 NULL, // extrafield_local, |
| 359 0u, // size_extrafield_local |
| 360 NULL, // extrafield_global |
| 361 0u, // size_extrafield_global |
| 362 NULL, // comment |
| 363 Z_DEFLATED, // method |
| 364 Z_DEFAULT_COMPRESSION, // level |
| 365 0, // raw |
| 366 -MAX_WBITS, // windowBits |
| 367 DEF_MEM_LEVEL, // memLevel |
| 368 Z_DEFAULT_STRATEGY, // strategy |
| 369 NULL, // password |
| 370 0, // crcForCrypting |
| 371 0, // versionMadeBy |
| 372 LANGUAGE_ENCODING_FLAG)) { // flagBase |
| 373 DLOG(ERROR) << "Could not open zip file entry " << str_path; |
| 374 return false; |
| 375 } |
| 376 return true; |
| 377 } |
| 378 |
315 } // namespace internal | 379 } // namespace internal |
316 } // namespace zip | 380 } // namespace zip |
OLD | NEW |