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

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

Issue 8508003: zip: Add ZipReader and rework Unzip() using the new class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix a win failure Created 9 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « chrome/common/zip_reader.h ('k') | chrome/common/zip_reader_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/common/zip_reader.h"
6
7 #include "base/file_util.h"
8 #include "base/logging.h"
9 #include "base/string_util.h"
10 #include "base/utf_string_conversions.h"
11 #include "chrome/common/zip_internal.h"
12 #include "net/base/file_stream.h"
13 #include "third_party/zlib/contrib/minizip/unzip.h"
14 #if defined(OS_WIN)
15 #include "third_party/zlib/contrib/minizip/iowin32.h"
16 #endif
17
18 namespace zip {
19
20 // TODO(satorux): The implementation assumes that file names in zip files
21 // are encoded in UTF-8. This is true for zip files created by Zip()
22 // function in zip.h, but not true for user-supplied random zip files.
23 ZipReader::EntryInfo::EntryInfo(const std::string& file_name_in_zip,
24 const unz_file_info& raw_file_info)
25 : file_path_(FilePath::FromUTF8Unsafe(file_name_in_zip)),
26 is_directory_(false) {
27 original_size_ = raw_file_info.uncompressed_size;
28
29 // Directory entries in zip files end with "/".
30 is_directory_ = EndsWith(file_name_in_zip, "/", false);
31
32 // Check the file name here for directory traversal issues. In the name of
33 // simplicity and security, we might reject a valid file name such as "a..b".
34 is_unsafe_ = file_name_in_zip.find("..") != std::string::npos;
35
36 // We also consider that the file name is unsafe, if it's invalid UTF-8.
37 string16 file_name_utf16;
38 if (!UTF8ToUTF16(file_name_in_zip.data(), file_name_in_zip.size(),
39 &file_name_utf16)) {
40 is_unsafe_ = true;
41 }
42
43 // We also consider that the file name is unsafe, if it's absolute.
44 // On Windows, IsAbsolute() returns false for paths starting with "/".
45 if (file_path_.IsAbsolute() || StartsWithASCII(file_name_in_zip, "/", false))
46 is_unsafe_ = true;
47
48 // Construct the last modified time. The timezone info is not present in
49 // zip files, so we construct the time as local time.
50 base::Time::Exploded exploded_time = {}; // Zero-clear.
51 exploded_time.year = raw_file_info.tmu_date.tm_year;
52 // The month in zip file is 0-based, whereas ours is 1-based.
53 exploded_time.month = raw_file_info.tmu_date.tm_mon + 1;
54 exploded_time.day_of_month = raw_file_info.tmu_date.tm_mday;
55 exploded_time.hour = raw_file_info.tmu_date.tm_hour;
56 exploded_time.minute = raw_file_info.tmu_date.tm_min;
57 exploded_time.second = raw_file_info.tmu_date.tm_sec;
58 exploded_time.millisecond = 0;
59 if (exploded_time.HasValidValues()) {
60 last_modified_ = base::Time::FromLocalExploded(exploded_time);
61 } else {
62 // Use Unix time epoch if the time stamp data is invalid.
63 last_modified_ = base::Time::UnixEpoch();
64 }
65 }
66
67 ZipReader::ZipReader() {
68 Reset();
69 }
70
71 ZipReader::~ZipReader() {
72 Close();
73 }
74
75 bool ZipReader::Open(const FilePath& zip_file_path) {
76 DCHECK(!zip_file_);
77
78 // Use of "Unsafe" function does not look good, but there is no way to do
79 // this safely on Linux. See file_util.h for details.
80 zip_file_ = internal::OpenForUnzipping(zip_file_path.AsUTF8Unsafe());
81 if (!zip_file_) {
82 return false;
83 }
84
85 unz_global_info zip_info = {}; // Zero-clear.
86 if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) {
87 return false;
88 }
89 num_entries_ = zip_info.number_entry;
90 if (num_entries_ < 0)
91 return false;
92
93 // We are already at the end if the zip file is empty.
94 reached_end_ = (num_entries_ == 0);
95 return true;
96 }
97
98 void ZipReader::Close() {
99 if (zip_file_) {
100 unzClose(zip_file_);
101 }
102 Reset();
103 }
104
105 bool ZipReader::HasMore() {
106 return !reached_end_;
107 }
108
109 bool ZipReader::AdvanceToNextEntry() {
110 DCHECK(zip_file_);
111
112 // Should not go further if we already reached the end.
113 if (reached_end_)
114 return false;
115
116 unz_file_pos position = {};
117 if (unzGetFilePos(zip_file_, &position) != UNZ_OK)
118 return false;
119 const int current_entry_index = position.num_of_file;
120 // If we are currently at the last entry, then the next position is the
121 // end of the zip file, so mark that we reached the end.
122 if (current_entry_index + 1 == num_entries_) {
123 reached_end_ = true;
124 } else {
125 DCHECK_LT(current_entry_index + 1, num_entries_);
126 if (unzGoToNextFile(zip_file_) != UNZ_OK) {
127 return false;
128 }
129 }
130 current_entry_info_.reset();
131 return true;
132 }
133
134 bool ZipReader::OpenCurrentEntryInZip() {
135 DCHECK(zip_file_);
136
137 unz_file_info raw_file_info = {};
138 char raw_file_name_in_zip[internal::kZipMaxPath] = {};
139 const int result = unzGetCurrentFileInfo(zip_file_,
140 &raw_file_info,
141 raw_file_name_in_zip,
142 sizeof(raw_file_name_in_zip) - 1,
143 NULL, // extraField.
144 0, // extraFieldBufferSize.
145 NULL, // szComment.
146 0); // commentBufferSize.
147 if (result != UNZ_OK)
148 return NULL;
149 if (raw_file_name_in_zip[0] == '\0')
150 return NULL;
151 current_entry_info_.reset(
152 new EntryInfo(raw_file_name_in_zip, raw_file_info));
153 return true;
154 }
155
156 bool ZipReader::LocateAndOpenEntry(const FilePath& path_in_zip) {
157 DCHECK(zip_file_);
158
159 current_entry_info_.reset();
160 reached_end_ = false;
161 const int kDefaultCaseSensivityOfOS = 0;
162 const int result = unzLocateFile(zip_file_,
163 path_in_zip.AsUTF8Unsafe().c_str(),
164 kDefaultCaseSensivityOfOS);
165 if (result != UNZ_OK)
166 return false;
167
168 // Then Open the entry.
169 return OpenCurrentEntryInZip();
170 }
171
172 bool ZipReader::ExtractCurrentEntryToFilePath(
173 const FilePath& output_file_path) {
174 DCHECK(zip_file_);
175
176 // If this is a directory, just create it and return.
177 if (current_entry_info()->is_directory())
178 return file_util::CreateDirectory(output_file_path);
179
180 const int open_result = unzOpenCurrentFile(zip_file_);
181 if (open_result != UNZ_OK)
182 return false;
183
184 // We can't rely on parent directory entries being specified in the
185 // zip, so we make sure they are created.
186 FilePath output_dir_path = output_file_path.DirName();
187 if (!file_util::CreateDirectory(output_dir_path))
188 return false;
189
190 net::FileStream stream;
191 const int flags = (base::PLATFORM_FILE_CREATE_ALWAYS |
192 base::PLATFORM_FILE_WRITE);
193 if (stream.Open(output_file_path, flags) != 0)
194 return false;
195
196 bool success = true; // This becomes false when something bad happens.
197 while (true) {
198 char buf[internal::kZipBufSize];
199 const int num_bytes_read = unzReadCurrentFile(zip_file_, buf,
200 internal::kZipBufSize);
201 if (num_bytes_read == 0) {
202 // Reached the end of the file.
203 break;
204 } else if (num_bytes_read < 0) {
205 // If num_bytes_read < 0, then it's a specific UNZ_* error code.
206 success = false;
207 break;
208 } else if (num_bytes_read > 0) {
209 // Some data is read. Write it to the output file.
210 if (num_bytes_read != stream.Write(buf, num_bytes_read,
211 net::CompletionCallback())) {
212 success = false;
213 break;
214 }
215 }
216 }
217
218 stream.Close();
219 unzCloseCurrentFile(zip_file_);
220 return success;
221 }
222
223 bool ZipReader::ExtractCurrentEntryIntoDirectory(
224 const FilePath& output_directory_path) {
225 DCHECK(current_entry_info_.get());
226
227 FilePath output_file_path = output_directory_path.Append(
228 current_entry_info()->file_path());
229 return ExtractCurrentEntryToFilePath(output_file_path);
230 }
231
232 void ZipReader::Reset() {
233 zip_file_ = NULL;
234 num_entries_ = 0;
235 reached_end_ = false;
236 current_entry_info_.reset();
237 }
238
239 } // namespace zip
OLDNEW
« no previous file with comments | « chrome/common/zip_reader.h ('k') | chrome/common/zip_reader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698