| 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_internal.h" | 5 #include "third_party/zlib/google/zip_internal.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 CloseHandle(file); | 72 CloseHandle(file); |
| 73 else | 73 else |
| 74 *(static_cast<WIN32FILE_IOWIN*>(ret)) = file_ret; | 74 *(static_cast<WIN32FILE_IOWIN*>(ret)) = file_ret; |
| 75 } | 75 } |
| 76 return ret; | 76 return ret; |
| 77 } | 77 } |
| 78 #endif | 78 #endif |
| 79 | 79 |
| 80 #if defined(OS_POSIX) | 80 #if defined(OS_POSIX) |
| 81 // Callback function for zlib that opens a file stream from a file descriptor. | 81 // Callback function for zlib that opens a file stream from a file descriptor. |
| 82 // Since we do not own the file descriptor, dup it so that we can fdopen/fclose |
| 83 // a file stream. |
| 82 void* FdOpenFileFunc(void* opaque, const char* filename, int mode) { | 84 void* FdOpenFileFunc(void* opaque, const char* filename, int mode) { |
| 83 FILE* file = NULL; | 85 FILE* file = NULL; |
| 84 const char* mode_fopen = NULL; | 86 const char* mode_fopen = NULL; |
| 85 | 87 |
| 86 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ) | 88 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ) |
| 87 mode_fopen = "rb"; | 89 mode_fopen = "rb"; |
| 88 else if (mode & ZLIB_FILEFUNC_MODE_EXISTING) | 90 else if (mode & ZLIB_FILEFUNC_MODE_EXISTING) |
| 89 mode_fopen = "r+b"; | 91 mode_fopen = "r+b"; |
| 90 else if (mode & ZLIB_FILEFUNC_MODE_CREATE) | 92 else if (mode & ZLIB_FILEFUNC_MODE_CREATE) |
| 91 mode_fopen = "wb"; | 93 mode_fopen = "wb"; |
| 92 | 94 |
| 93 if ((filename != NULL) && (mode_fopen != NULL)) | 95 if ((filename != NULL) && (mode_fopen != NULL)) { |
| 94 file = fdopen(*static_cast<int*>(opaque), mode_fopen); | 96 int fd = dup(*static_cast<int*>(opaque)); |
| 97 if (fd != -1) |
| 98 file = fdopen(fd, mode_fopen); |
| 99 } |
| 95 | 100 |
| 96 return file; | 101 return file; |
| 97 } | 102 } |
| 98 | 103 |
| 99 // We don't actually close the file stream since that would close | 104 int FdCloseFileFunc(void* opaque, void* stream) { |
| 100 // the underlying file descriptor, and we don't own it. However we do need to | 105 fclose(static_cast<FILE*>(stream)); |
| 101 // flush buffers and free |opaque| since we malloc'ed it in FillFdOpenFileFunc. | 106 free(opaque); // malloc'ed in FillFdOpenFileFunc() |
| 102 int CloseFileFunc(void* opaque, void* stream) { | |
| 103 fflush(static_cast<FILE*>(stream)); | |
| 104 free(opaque); | |
| 105 return 0; | 107 return 0; |
| 106 } | 108 } |
| 107 | 109 |
| 108 // Fills |pzlib_filecunc_def| appropriately to handle the zip file | 110 // Fills |pzlib_filecunc_def| appropriately to handle the zip file |
| 109 // referred to by |fd|. | 111 // referred to by |fd|. |
| 110 void FillFdOpenFileFunc(zlib_filefunc_def* pzlib_filefunc_def, int fd) { | 112 void FillFdOpenFileFunc(zlib_filefunc_def* pzlib_filefunc_def, int fd) { |
| 111 fill_fopen_filefunc(pzlib_filefunc_def); | 113 fill_fopen_filefunc(pzlib_filefunc_def); |
| 112 pzlib_filefunc_def->zopen_file = FdOpenFileFunc; | 114 pzlib_filefunc_def->zopen_file = FdOpenFileFunc; |
| 113 pzlib_filefunc_def->zclose_file = CloseFileFunc; | 115 pzlib_filefunc_def->zclose_file = FdCloseFileFunc; |
| 114 int* ptr_fd = static_cast<int*>(malloc(sizeof(fd))); | 116 int* ptr_fd = static_cast<int*>(malloc(sizeof(fd))); |
| 115 *ptr_fd = fd; | 117 *ptr_fd = fd; |
| 116 pzlib_filefunc_def->opaque = ptr_fd; | 118 pzlib_filefunc_def->opaque = ptr_fd; |
| 117 } | 119 } |
| 118 #endif // defined(OS_POSIX) | 120 #endif // defined(OS_POSIX) |
| 119 | 121 |
| 120 #if defined(OS_WIN) | 122 #if defined(OS_WIN) |
| 121 // Callback function for zlib that opens a file stream from a Windows handle. | 123 // Callback function for zlib that opens a file stream from a Windows handle. |
| 124 // Does not take ownership of the handle. |
| 122 void* HandleOpenFileFunc(void* opaque, const char* filename, int mode) { | 125 void* HandleOpenFileFunc(void* opaque, const char* filename, int mode) { |
| 123 WIN32FILE_IOWIN file_ret; | 126 WIN32FILE_IOWIN file_ret; |
| 124 file_ret.hf = static_cast<HANDLE>(opaque); | 127 file_ret.hf = static_cast<HANDLE>(opaque); |
| 125 file_ret.error = 0; | 128 file_ret.error = 0; |
| 126 if (file_ret.hf == INVALID_HANDLE_VALUE) | 129 if (file_ret.hf == INVALID_HANDLE_VALUE) |
| 127 return NULL; | 130 return NULL; |
| 128 | 131 |
| 129 void* ret = malloc(sizeof(WIN32FILE_IOWIN)); | 132 void* ret = malloc(sizeof(WIN32FILE_IOWIN)); |
| 130 if (ret != NULL) | 133 if (ret != NULL) |
| 131 *(static_cast<WIN32FILE_IOWIN*>(ret)) = file_ret; | 134 *(static_cast<WIN32FILE_IOWIN*>(ret)) = file_ret; |
| 132 return ret; | 135 return ret; |
| 133 } | 136 } |
| 137 |
| 138 int HandleCloseFileFunc(void* opaque, void* stream) { |
| 139 free(stream); // malloc'ed in HandleOpenFileFunc() |
| 140 return 0; |
| 141 } |
| 134 #endif | 142 #endif |
| 135 | 143 |
| 136 // A struct that contains data required for zlib functions to extract files from | 144 // A struct that contains data required for zlib functions to extract files from |
| 137 // a zip archive stored in memory directly. The following I/O API functions | 145 // a zip archive stored in memory directly. The following I/O API functions |
| 138 // expect their opaque parameters refer to this struct. | 146 // expect their opaque parameters refer to this struct. |
| 139 struct ZipBuffer { | 147 struct ZipBuffer { |
| 140 const char* data; // weak | 148 const char* data; // weak |
| 141 size_t length; | 149 size_t length; |
| 142 size_t offset; | 150 size_t offset; |
| 143 }; | 151 }; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 // Passing dummy "fd" filename to zlib. | 283 // Passing dummy "fd" filename to zlib. |
| 276 return unzOpen2("fd", &zip_funcs); | 284 return unzOpen2("fd", &zip_funcs); |
| 277 } | 285 } |
| 278 #endif | 286 #endif |
| 279 | 287 |
| 280 #if defined(OS_WIN) | 288 #if defined(OS_WIN) |
| 281 unzFile OpenHandleForUnzipping(HANDLE zip_handle) { | 289 unzFile OpenHandleForUnzipping(HANDLE zip_handle) { |
| 282 zlib_filefunc_def zip_funcs; | 290 zlib_filefunc_def zip_funcs; |
| 283 fill_win32_filefunc(&zip_funcs); | 291 fill_win32_filefunc(&zip_funcs); |
| 284 zip_funcs.zopen_file = HandleOpenFileFunc; | 292 zip_funcs.zopen_file = HandleOpenFileFunc; |
| 293 zip_funcs.zclose_file = HandleCloseFileFunc; |
| 285 zip_funcs.opaque = zip_handle; | 294 zip_funcs.opaque = zip_handle; |
| 286 return unzOpen2("fd", &zip_funcs); | 295 return unzOpen2("fd", &zip_funcs); |
| 287 } | 296 } |
| 288 #endif | 297 #endif |
| 289 | 298 |
| 290 // static | 299 // static |
| 291 unzFile PrepareMemoryForUnzipping(const std::string& data) { | 300 unzFile PrepareMemoryForUnzipping(const std::string& data) { |
| 292 if (data.empty()) | 301 if (data.empty()) |
| 293 return NULL; | 302 return NULL; |
| 294 | 303 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 0, // versionMadeBy | 378 0, // versionMadeBy |
| 370 LANGUAGE_ENCODING_FLAG)) { // flagBase | 379 LANGUAGE_ENCODING_FLAG)) { // flagBase |
| 371 DLOG(ERROR) << "Could not open zip file entry " << str_path; | 380 DLOG(ERROR) << "Could not open zip file entry " << str_path; |
| 372 return false; | 381 return false; |
| 373 } | 382 } |
| 374 return true; | 383 return true; |
| 375 } | 384 } |
| 376 | 385 |
| 377 } // namespace internal | 386 } // namespace internal |
| 378 } // namespace zip | 387 } // namespace zip |
| OLD | NEW |