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

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: address comments 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
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 if (file_path_.IsAbsolute())
45 is_unsafe_ = true;
46
47 // Construct the last modified time. The timezone info is not present in
48 // zip files, so we construct the time as local time.
49 base::Time::Exploded exploded_time = {}; // Zero-clear.
50 exploded_time.year = raw_file_info.tmu_date.tm_year;
51 // The month in zip file is 0-based, whereas ours is 1-based.
52 exploded_time.month = raw_file_info.tmu_date.tm_mon + 1;
53 exploded_time.day_of_month = raw_file_info.tmu_date.tm_mday;
54 exploded_time.hour = raw_file_info.tmu_date.tm_hour;
55 exploded_time.minute = raw_file_info.tmu_date.tm_min;
56 exploded_time.second = raw_file_info.tmu_date.tm_sec;
57 exploded_time.millisecond = 0;
58 last_modified_ = base::Time::FromLocalExploded(exploded_time);
59 }
60
61 ZipReader::ZipReader() {
62 Reset();
63 }
64
65 ZipReader::~ZipReader() {
66 Close();
67 }
68
69 bool ZipReader::Open(const FilePath& zip_file_path) {
70 DCHECK(!zip_file_);
71
72 // Use of "Unsafe" function does not look good, but there is no way to do
73 // this safely on Linux. See file_util.h for details.
74 zip_file_ = internal::OpenForUnzipping(zip_file_path.AsUTF8Unsafe());
75 if (!zip_file_) {
76 return false;
77 }
78
79 unz_global_info zip_info = {}; // Zero-clear.
80 if (unzGetGlobalInfo(zip_file_, &zip_info) != UNZ_OK) {
81 return false;
82 }
83 num_entries_ = zip_info.number_entry;
84 if (num_entries_ < 0)
85 return false;
86
87 // We are already at the end if the zip file is empty.
88 reached_end_ = (num_entries_ == 0);
89 return true;
90 }
91
92 void ZipReader::Close() {
93 if (zip_file_) {
94 unzClose(zip_file_);
95 }
96 Reset();
97 }
98
99 bool ZipReader::HasMore() {
100 return !reached_end_;
101 }
102
103 bool ZipReader::AdvanceToNextEntry() {
104 DCHECK(zip_file_);
105
106 // Should not go further if we already reached the end.
107 if (reached_end_)
108 return false;
109
110 unz_file_pos position = {};
111 if (unzGetFilePos(zip_file_, &position) != UNZ_OK)
112 return false;
113 const int current_entry_index = position.num_of_file;
114 // If we are currently at the last entry, then the next position is the
115 // end of the zip file, so mark that we reached the end.
116 if (current_entry_index + 1 == num_entries_) {
117 reached_end_ = true;
118 } else {
119 DCHECK_LT(current_entry_index + 1, num_entries_);
120 if (unzGoToNextFile(zip_file_) != UNZ_OK) {
121 return false;
122 }
123 }
124 current_entry_info_.reset();
125 return true;
126 }
127
128 bool ZipReader::OpenCurrentEntryInZip() {
129 DCHECK(zip_file_);
130
131 unz_file_info raw_file_info = {};
132 char raw_file_name_in_zip[internal::kZipMaxPath] = {};
133 const int result = unzGetCurrentFileInfo(zip_file_,
134 &raw_file_info,
135 raw_file_name_in_zip,
136 sizeof(raw_file_name_in_zip) - 1,
137 NULL, // extraField.
138 0, // extraFieldBufferSize.
139 NULL, // szComment.
140 0); // commentBufferSize.
141 if (result != UNZ_OK)
142 return NULL;
143 if (raw_file_name_in_zip[0] == '\0')
144 return NULL;
145 current_entry_info_.reset(
146 new EntryInfo(raw_file_name_in_zip, raw_file_info));
147 return true;
148 }
149
150 bool ZipReader::LocateAndOpenEntry(const FilePath& path_in_zip) {
151 DCHECK(zip_file_);
152
153 current_entry_info_.reset();
154 reached_end_ = false;
155 const int kDefaultCaseSensivityOfOS = 0;
156 const int result = unzLocateFile(zip_file_,
157 path_in_zip.AsUTF8Unsafe().c_str(),
158 kDefaultCaseSensivityOfOS);
159 if (result != UNZ_OK)
160 return false;
161
162 // Then Open the entry.
163 return OpenCurrentEntryInZip();
164 }
165
166 bool ZipReader::ExtractCurrentEntryToFilePath(
167 const FilePath& output_file_path) {
168 DCHECK(zip_file_);
169
170 // If this is a directory, just create it and return.
171 if (current_entry_info()->is_directory())
172 return file_util::CreateDirectory(output_file_path);
173
174 const int open_result = unzOpenCurrentFile(zip_file_);
175 if (open_result != UNZ_OK)
176 return false;
177
178 // We can't rely on parent directory entries being specified in the
179 // zip, so we make sure they are created.
180 FilePath output_dir_path = output_file_path.DirName();
181 if (!file_util::CreateDirectory(output_dir_path))
182 return false;
183
184 net::FileStream stream;
185 const int flags = (base::PLATFORM_FILE_CREATE_ALWAYS |
186 base::PLATFORM_FILE_WRITE);
187 if (stream.Open(output_file_path, flags) != 0)
188 return false;
189
190 bool success = true; // This becomes false when something bad happens.
191 while (true) {
192 char buf[internal::kZipBufSize];
193 const int num_bytes_read = unzReadCurrentFile(zip_file_, buf,
194 internal::kZipBufSize);
195 if (num_bytes_read == 0) {
196 // Reached the end of the file.
197 break;
198 } else if (num_bytes_read < 0) {
199 // If num_bytes_read < 0, then it's a specific UNZ_* error code.
200 success = false;
201 break;
202 } else if (num_bytes_read > 0) {
203 // Some data is read. Write it to the output file.
204 if (num_bytes_read != stream.Write(buf, num_bytes_read,
205 net::CompletionCallback())) {
206 success = false;
207 break;
208 }
209 }
210 }
211
212 stream.Close();
213 unzCloseCurrentFile(zip_file_);
214 return success;
215 }
216
217 bool ZipReader::ExtractCurrentEntryIntoDirectory(
218 const FilePath& output_directory_path) {
219 DCHECK(current_entry_info_.get());
220
221 FilePath output_file_path = output_directory_path.Append(
222 current_entry_info()->file_path());
223 return ExtractCurrentEntryToFilePath(output_file_path);
224 }
225
226 void ZipReader::Reset() {
227 zip_file_ = NULL;
228 num_entries_ = 0;
229 reached_end_ = false;
230 current_entry_info_.reset();
231 }
232
233 } // namespace zip
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698