Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(445)

Side by Side Diff: chrome/common/zip_internal.cc

Issue 8873039: Add an API to unpack Zip files directly from and to file descriptors. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Style fixes. Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 void* FdOpenFileFunc(void* opaque, const char* filename, int mode) {
satorux1 2011/12/15 00:40:45 Function comment is missing. http://google-style
Jorge Lucangeli Obes 2011/12/15 01:06:19 Done.
72 FILE* file = NULL;
73 const char* mode_fopen = NULL;
74
75 if ((mode & ZLIB_FILEFUNC_MODE_READWRITEFILTER) == ZLIB_FILEFUNC_MODE_READ)
76 mode_fopen = "rb";
77 else if (mode & ZLIB_FILEFUNC_MODE_EXISTING)
78 mode_fopen = "r+b";
79 else if (mode & ZLIB_FILEFUNC_MODE_CREATE)
80 mode_fopen = "wb";
81
82 if ((filename != NULL) && (mode_fopen != NULL))
83 file = fdopen(*static_cast<int*>(opaque), mode_fopen);
84
85 return file;
86 }
87
88 // We don't actually close the file stream since that would close
89 // the underlying file descriptor, and we don't own it. We do free
90 // |opaque| since we malloc'ed in fill_fdopen_filefunc.
91 int CloseFileFunc(void* opaque, void* stream) {
92 free(opaque);
93 return 0;
94 }
95
96 void FillFdOpenFileFunc(zlib_filefunc_def* pzlib_filefunc_def, int fd) {
satorux1 2011/12/15 00:40:45 function comment.
Jorge Lucangeli Obes 2011/12/15 01:06:19 Done.
97 fill_fopen_filefunc(pzlib_filefunc_def);
98 pzlib_filefunc_def->zopen_file = FdOpenFileFunc;
99 pzlib_filefunc_def->zclose_file = CloseFileFunc;
100 int* ptr_fd = static_cast<int*>(malloc(sizeof(fd)));
101 *ptr_fd = fd;
102 pzlib_filefunc_def->opaque = ptr_fd;
103 }
104 #endif // defined(OS_POSIX)
105
68 } // namespace 106 } // namespace
69 107
70 namespace zip { 108 namespace zip {
71 namespace internal { 109 namespace internal {
72 110
73 unzFile OpenForUnzipping(const std::string& file_name_utf8) { 111 unzFile OpenForUnzipping(const std::string& file_name_utf8) {
74 zlib_filefunc_def* zip_func_ptrs = NULL; 112 zlib_filefunc_def* zip_func_ptrs = NULL;
75 #if defined(OS_WIN) 113 #if defined(OS_WIN)
76 zlib_filefunc_def zip_funcs; 114 zlib_filefunc_def zip_funcs;
77 fill_win32_filefunc(&zip_funcs); 115 fill_win32_filefunc(&zip_funcs);
78 zip_funcs.zopen_file = ZipOpenFunc; 116 zip_funcs.zopen_file = ZipOpenFunc;
79 zip_func_ptrs = &zip_funcs; 117 zip_func_ptrs = &zip_funcs;
80 #endif 118 #endif
81 return unzOpen2(file_name_utf8.c_str(), zip_func_ptrs); 119 return unzOpen2(file_name_utf8.c_str(), zip_func_ptrs);
82 } 120 }
83 121
122 #if defined(OS_POSIX)
123 unzFile OpenFdForUnzipping(int zip_fd) {
124 zlib_filefunc_def zip_funcs;
125 FillFdOpenFileFunc(&zip_funcs, zip_fd);
126 // Passing dummy "fd" filename to zlib.
127 return unzOpen2("fd", &zip_funcs);
128 }
129 #endif
130
84 zipFile OpenForZipping(const std::string& file_name_utf8, int append_flag) { 131 zipFile OpenForZipping(const std::string& file_name_utf8, int append_flag) {
85 zlib_filefunc_def* zip_func_ptrs = NULL; 132 zlib_filefunc_def* zip_func_ptrs = NULL;
86 #if defined(OS_WIN) 133 #if defined(OS_WIN)
87 zlib_filefunc_def zip_funcs; 134 zlib_filefunc_def zip_funcs;
88 fill_win32_filefunc(&zip_funcs); 135 fill_win32_filefunc(&zip_funcs);
89 zip_funcs.zopen_file = ZipOpenFunc; 136 zip_funcs.zopen_file = ZipOpenFunc;
90 zip_func_ptrs = &zip_funcs; 137 zip_func_ptrs = &zip_funcs;
91 #endif 138 #endif
92 return zipOpen2(file_name_utf8.c_str(), 139 return zipOpen2(file_name_utf8.c_str(),
93 append_flag, 140 append_flag,
94 NULL, // global comment 141 NULL, // global comment
95 zip_func_ptrs); 142 zip_func_ptrs);
96 } 143 }
97 144
98 } // namespace internal 145 } // namespace internal
99 } // namespace zip 146 } // namespace zip
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698