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 "chrome/common/zip.h" | 5 #include "chrome/common/zip.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "third_party/zlib/contrib/minizip/unzip.h" | 8 #include "third_party/zlib/contrib/minizip/unzip.h" |
9 #include "third_party/zlib/contrib/minizip/zip.h" | 9 #include "third_party/zlib/contrib/minizip/zip.h" |
10 #if defined(OS_WIN) | 10 #if defined(OS_WIN) |
11 #include "third_party/zlib/contrib/minizip/iowin32.h" | 11 #include "third_party/zlib/contrib/minizip/iowin32.h" |
| 12 #elif defined(OS_POSIX) |
| 13 #include "third_party/zlib/contrib/minizip/ioapi.h" |
12 #endif | 14 #endif |
13 | 15 |
14 namespace { | 16 namespace { |
15 | 17 |
16 #if defined(OS_WIN) | 18 #if defined(OS_WIN) |
17 typedef struct { | 19 typedef struct { |
18 HANDLE hf; | 20 HANDLE hf; |
19 int error; | 21 int error; |
20 } WIN32FILE_IOWIN; | 22 } WIN32FILE_IOWIN; |
21 | 23 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 ret = malloc(sizeof(WIN32FILE_IOWIN)); | 60 ret = malloc(sizeof(WIN32FILE_IOWIN)); |
59 if (ret == NULL) | 61 if (ret == NULL) |
60 CloseHandle(file); | 62 CloseHandle(file); |
61 else | 63 else |
62 *(static_cast<WIN32FILE_IOWIN*>(ret)) = file_ret; | 64 *(static_cast<WIN32FILE_IOWIN*>(ret)) = file_ret; |
63 } | 65 } |
64 return ret; | 66 return ret; |
65 } | 67 } |
66 #endif | 68 #endif |
67 | 69 |
| 70 #if defined(OS_POSIX) |
| 71 // Callback function for zlib that opens a file stream from a file descriptor. |
| 72 void* FdOpenFileFunc(void* opaque, const char* filename, int mode) { |
| 73 FILE* file = NULL; |
| 74 const char* mode_fopen = NULL; |
| 75 |
| 76 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ) |
| 77 mode_fopen = "rb"; |
| 78 else if (mode & ZLIB_FILEFUNC_MODE_EXISTING) |
| 79 mode_fopen = "r+b"; |
| 80 else if (mode & ZLIB_FILEFUNC_MODE_CREATE) |
| 81 mode_fopen = "wb"; |
| 82 |
| 83 if ((filename != NULL) && (mode_fopen != NULL)) |
| 84 file = fdopen(*static_cast<int*>(opaque), mode_fopen); |
| 85 |
| 86 return file; |
| 87 } |
| 88 |
| 89 // We don't actually close the file stream since that would close |
| 90 // the underlying file descriptor, and we don't own it. We do free |
| 91 // |opaque| since we malloc'ed it in FillFdOpenFileFunc. |
| 92 int CloseFileFunc(void* opaque, void* stream) { |
| 93 free(opaque); |
| 94 return 0; |
| 95 } |
| 96 |
| 97 // Fills |pzlib_filecunc_def| appropriately to handle the zip file |
| 98 // referred to by |fd|. |
| 99 void FillFdOpenFileFunc(zlib_filefunc_def* pzlib_filefunc_def, int fd) { |
| 100 fill_fopen_filefunc(pzlib_filefunc_def); |
| 101 pzlib_filefunc_def->zopen_file = FdOpenFileFunc; |
| 102 pzlib_filefunc_def->zclose_file = CloseFileFunc; |
| 103 int* ptr_fd = static_cast<int*>(malloc(sizeof(fd))); |
| 104 *ptr_fd = fd; |
| 105 pzlib_filefunc_def->opaque = ptr_fd; |
| 106 } |
| 107 #endif // defined(OS_POSIX) |
| 108 |
68 } // namespace | 109 } // namespace |
69 | 110 |
70 namespace zip { | 111 namespace zip { |
71 namespace internal { | 112 namespace internal { |
72 | 113 |
73 unzFile OpenForUnzipping(const std::string& file_name_utf8) { | 114 unzFile OpenForUnzipping(const std::string& file_name_utf8) { |
74 zlib_filefunc_def* zip_func_ptrs = NULL; | 115 zlib_filefunc_def* zip_func_ptrs = NULL; |
75 #if defined(OS_WIN) | 116 #if defined(OS_WIN) |
76 zlib_filefunc_def zip_funcs; | 117 zlib_filefunc_def zip_funcs; |
77 fill_win32_filefunc(&zip_funcs); | 118 fill_win32_filefunc(&zip_funcs); |
78 zip_funcs.zopen_file = ZipOpenFunc; | 119 zip_funcs.zopen_file = ZipOpenFunc; |
79 zip_func_ptrs = &zip_funcs; | 120 zip_func_ptrs = &zip_funcs; |
80 #endif | 121 #endif |
81 return unzOpen2(file_name_utf8.c_str(), zip_func_ptrs); | 122 return unzOpen2(file_name_utf8.c_str(), zip_func_ptrs); |
82 } | 123 } |
83 | 124 |
| 125 #if defined(OS_POSIX) |
| 126 unzFile OpenFdForUnzipping(int zip_fd) { |
| 127 zlib_filefunc_def zip_funcs; |
| 128 FillFdOpenFileFunc(&zip_funcs, zip_fd); |
| 129 // Passing dummy "fd" filename to zlib. |
| 130 return unzOpen2("fd", &zip_funcs); |
| 131 } |
| 132 #endif |
| 133 |
84 zipFile OpenForZipping(const std::string& file_name_utf8, int append_flag) { | 134 zipFile OpenForZipping(const std::string& file_name_utf8, int append_flag) { |
85 zlib_filefunc_def* zip_func_ptrs = NULL; | 135 zlib_filefunc_def* zip_func_ptrs = NULL; |
86 #if defined(OS_WIN) | 136 #if defined(OS_WIN) |
87 zlib_filefunc_def zip_funcs; | 137 zlib_filefunc_def zip_funcs; |
88 fill_win32_filefunc(&zip_funcs); | 138 fill_win32_filefunc(&zip_funcs); |
89 zip_funcs.zopen_file = ZipOpenFunc; | 139 zip_funcs.zopen_file = ZipOpenFunc; |
90 zip_func_ptrs = &zip_funcs; | 140 zip_func_ptrs = &zip_funcs; |
91 #endif | 141 #endif |
92 return zipOpen2(file_name_utf8.c_str(), | 142 return zipOpen2(file_name_utf8.c_str(), |
93 append_flag, | 143 append_flag, |
94 NULL, // global comment | 144 NULL, // global comment |
95 zip_func_ptrs); | 145 zip_func_ptrs); |
96 } | 146 } |
97 | 147 |
98 } // namespace internal | 148 } // namespace internal |
99 } // namespace zip | 149 } // namespace zip |
OLD | NEW |