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

Side by Side Diff: chrome/installer/util/lzma_util.cc

Issue 10152012: Second attempt to update lzma_sdk to 9.20 (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 8 months 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
« no previous file with comments | « no previous file | third_party/lzma_sdk/7z.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/installer/util/lzma_util.h" 5 #include "chrome/installer/util/lzma_util.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 10
11 extern "C" { 11 extern "C" {
12 #include "third_party/lzma_sdk/Archive/7z/7zExtract.h" 12 #include "third_party/lzma_sdk/7z.h"
13 #include "third_party/lzma_sdk/Archive/7z/7zIn.h" 13 #include "third_party/lzma_sdk/7zAlloc.h"
14 #include "third_party/lzma_sdk/7zCrc.h" 14 #include "third_party/lzma_sdk/7zCrc.h"
15 #include "third_party/lzma_sdk/7zFile.h"
15 } 16 }
16 17
17 18
18 namespace { 19 namespace {
19 20
20 typedef struct _CFileInStream { 21 SRes LzmaReadFile(HANDLE file, void *data, size_t *size) {
21 ISzInStream InStream; 22 if (*size == 0)
22 HANDLE File; 23 return SZ_OK;
23 } CFileInStream;
24
25
26 size_t LzmaReadFile(HANDLE file, void *data, size_t size) {
27 if (size == 0)
28 return 0;
29 24
30 size_t processedSize = 0; 25 size_t processedSize = 0;
26 DWORD maxSize = *size;
31 do { 27 do {
32 DWORD processedLoc = 0; 28 DWORD processedLoc = 0;
33 BOOL res = ReadFile(file, data, (DWORD) size, &processedLoc, NULL); 29 BOOL res = ReadFile(file, data, maxSize, &processedLoc, NULL);
34 data = (void *)((unsigned char *) data + processedLoc); 30 data = (void *)((unsigned char *) data + processedLoc);
35 size -= processedLoc; 31 maxSize -= processedLoc;
36 processedSize += processedLoc; 32 processedSize += processedLoc;
37 if (!res || processedLoc == 0) 33 if (processedLoc == 0) {
38 break; 34 if (res)
39 } while (size > 0); 35 return SZ_ERROR_READ;
36 else
37 break;
38 }
39 } while (maxSize > 0);
40 40
41 return processedSize; 41 *size = processedSize;
42 }
43
44 SZ_RESULT SzFileSeekImp(void *object, CFileSize pos) {
45 CFileInStream *s = (CFileInStream *) object;
46 LARGE_INTEGER value;
47 value.LowPart = (DWORD) pos;
48 value.HighPart = (LONG) ((UInt64) pos >> 32);
49 value.LowPart = SetFilePointer(s->File, value.LowPart, &value.HighPart,
50 FILE_BEGIN);
51 return ((value.LowPart == 0xFFFFFFFF) && (GetLastError() != NO_ERROR)) ?
52 SZE_FAIL : SZ_OK;
53 }
54
55 SZ_RESULT SzFileReadImp(void *object, void **buffer,
56 size_t maxRequiredSize, size_t *processedSize) {
57 const int kBufferSize = 1 << 12;
58 static Byte g_Buffer[kBufferSize];
59 if (maxRequiredSize > kBufferSize)
60 maxRequiredSize = kBufferSize;
61
62 CFileInStream *s = (CFileInStream *) object;
63 size_t processedSizeLoc;
64 processedSizeLoc = LzmaReadFile(s->File, g_Buffer, maxRequiredSize);
65 *buffer = g_Buffer;
66 if (processedSize != 0)
67 *processedSize = processedSizeLoc;
68 return SZ_OK; 42 return SZ_OK;
69 } 43 }
70 44
45 SRes SzFileSeekImp(void *object, Int64 *pos, ESzSeek origin) {
46 CFileInStream *s = (CFileInStream *) object;
47 LARGE_INTEGER value;
48 value.LowPart = (DWORD) *pos;
49 value.HighPart = (LONG) ((UInt64) *pos >> 32);
50 DWORD moveMethod;
51 switch (origin) {
52 case SZ_SEEK_SET:
53 moveMethod = FILE_BEGIN;
54 break;
55 case SZ_SEEK_CUR:
56 moveMethod = FILE_CURRENT;
57 break;
58 case SZ_SEEK_END:
59 moveMethod = FILE_END;
60 break;
61 default:
62 return SZ_ERROR_PARAM;
63 }
64 value.LowPart = SetFilePointer(s->file.handle, value.LowPart, &value.HighPart,
65 moveMethod);
66 *pos = ((Int64)value.HighPart << 32) | value.LowPart;
67 return ((value.LowPart == 0xFFFFFFFF) && (GetLastError() != NO_ERROR)) ?
68 SZ_ERROR_FAIL : SZ_OK;
69 }
70
71 SRes SzFileReadImp(void *object, void *buffer, size_t *size) {
72 CFileInStream *s = (CFileInStream *) object;
73 return LzmaReadFile(s->file.handle, buffer, size);
74 }
75
71 } // namespace 76 } // namespace
72 77
73 // static 78 // static
74 int32 LzmaUtil::UnPackArchive(const std::wstring& archive, 79 int32 LzmaUtil::UnPackArchive(const std::wstring& archive,
75 const std::wstring& output_dir, 80 const std::wstring& output_dir,
76 std::wstring* output_file) { 81 std::wstring* output_file) {
77 VLOG(1) << "Opening archive " << archive; 82 VLOG(1) << "Opening archive " << archive;
78 LzmaUtil lzma_util; 83 LzmaUtil lzma_util;
79 DWORD ret; 84 DWORD ret;
80 if ((ret = lzma_util.OpenArchive(archive)) != NO_ERROR) { 85 if ((ret = lzma_util.OpenArchive(archive)) != NO_ERROR) {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 DWORD LzmaUtil::UnPack(const std::wstring& location) { 120 DWORD LzmaUtil::UnPack(const std::wstring& location) {
116 return UnPack(location, NULL); 121 return UnPack(location, NULL);
117 } 122 }
118 123
119 DWORD LzmaUtil::UnPack(const std::wstring& location, 124 DWORD LzmaUtil::UnPack(const std::wstring& location,
120 std::wstring* output_file) { 125 std::wstring* output_file) {
121 if (!archive_handle_) 126 if (!archive_handle_)
122 return ERROR_INVALID_HANDLE; 127 return ERROR_INVALID_HANDLE;
123 128
124 CFileInStream archiveStream; 129 CFileInStream archiveStream;
130 CLookToRead lookStream;
131 CSzArEx db;
125 ISzAlloc allocImp; 132 ISzAlloc allocImp;
126 ISzAlloc allocTempImp; 133 ISzAlloc allocTempImp;
127 CArchiveDatabaseEx db;
128 DWORD ret = NO_ERROR; 134 DWORD ret = NO_ERROR;
129 135
130 archiveStream.File = archive_handle_; 136 archiveStream.file.handle = archive_handle_;
131 archiveStream.InStream.Read = SzFileReadImp; 137 archiveStream.s.Read = SzFileReadImp;
132 archiveStream.InStream.Seek = SzFileSeekImp; 138 archiveStream.s.Seek = SzFileSeekImp;
139 LookToRead_CreateVTable(&lookStream, false);
140 lookStream.realStream = &archiveStream.s;
141
133 allocImp.Alloc = SzAlloc; 142 allocImp.Alloc = SzAlloc;
134 allocImp.Free = SzFree; 143 allocImp.Free = SzFree;
135 allocTempImp.Alloc = SzAllocTemp; 144 allocTempImp.Alloc = SzAllocTemp;
136 allocTempImp.Free = SzFreeTemp; 145 allocTempImp.Free = SzFreeTemp;
137 146
138 CrcGenerateTable(); 147 CrcGenerateTable();
139 SzArDbExInit(&db); 148 SzArEx_Init(&db);
140 if ((ret = SzArchiveOpen(&archiveStream.InStream, &db, 149 if ((ret = SzArEx_Open(&db, &lookStream.s,
141 &allocImp, &allocTempImp)) != SZ_OK) { 150 &allocImp, &allocTempImp)) != SZ_OK) {
142 LOG(ERROR) << L"Error returned by SzArchiveOpen: " << ret; 151 LOG(ERROR) << L"Error returned by SzArchiveOpen: " << ret;
143 return ret; 152 return ERROR_INVALID_HANDLE;
144 } 153 }
145 154
146 Byte *outBuffer = 0; // it must be 0 before first call for each new archive 155 Byte *outBuffer = 0; // it must be 0 before first call for each new archive
147 UInt32 blockIndex = 0xFFFFFFFF; // can have any value if outBuffer = 0 156 UInt32 blockIndex = 0xFFFFFFFF; // can have any value if outBuffer = 0
148 size_t outBufferSize = 0; // can have any value if outBuffer = 0 157 size_t outBufferSize = 0; // can have any value if outBuffer = 0
149 158
150 for (unsigned int i = 0; i < db.Database.NumFiles; i++) { 159 for (unsigned int i = 0; i < db.db.NumFiles; i++) {
151 DWORD written; 160 DWORD written;
152 size_t offset; 161 size_t offset;
153 size_t outSizeProcessed; 162 size_t outSizeProcessed;
154 CFileItem *f = db.Database.Files + i; 163 CSzFileItem *f = db.db.Files + i;
155 164
156 if ((ret = SzExtract(&archiveStream.InStream, &db, i, &blockIndex, 165 if ((ret = SzArEx_Extract(&db, &lookStream.s, i, &blockIndex,
157 &outBuffer, &outBufferSize, &offset, &outSizeProcessed, 166 &outBuffer, &outBufferSize, &offset, &outSizeProcessed,
158 &allocImp, &allocTempImp)) != SZ_OK) { 167 &allocImp, &allocTempImp)) != SZ_OK) {
159 LOG(ERROR) << L"Error returned by SzExtract: " << ret; 168 LOG(ERROR) << L"Error returned by SzExtract: " << ret;
169 ret = ERROR_INVALID_HANDLE;
160 break; 170 break;
161 } 171 }
162 172
163 FilePath file_path = FilePath(location).Append(UTF8ToWide(f->Name)); 173 size_t file_name_length = SzArEx_GetFileNameUtf16(&db, i, NULL);
174 if (file_name_length < 1) {
175 LOG(ERROR) << L"Couldn't get file name";
176 ret = ERROR_INVALID_HANDLE;
177 break;
178 }
179 std::vector<UInt16> file_name(file_name_length);
180 SzArEx_GetFileNameUtf16(&db, i, &file_name[0]);
181 // |file_name| is NULL-terminated.
182 FilePath file_path = FilePath(location).Append(
183 FilePath::StringType(file_name.begin(), file_name.end() - 1));
184
164 if (output_file) 185 if (output_file)
165 *output_file = file_path.value(); 186 *output_file = file_path.value();
166 187
167 // If archive entry is directory create it and move on to the next entry. 188 // If archive entry is directory create it and move on to the next entry.
168 if (f->IsDirectory) { 189 if (f->IsDir) {
169 CreateDirectory(file_path); 190 CreateDirectory(file_path);
170 continue; 191 continue;
171 } 192 }
172 193
173 CreateDirectory(file_path.DirName()); 194 CreateDirectory(file_path.DirName());
174 195
175 HANDLE hFile; 196 HANDLE hFile;
176 hFile = CreateFile(file_path.value().c_str(), GENERIC_WRITE, 0, NULL, 197 hFile = CreateFile(file_path.value().c_str(), GENERIC_WRITE, 0, NULL,
177 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 198 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
178 if (hFile == INVALID_HANDLE_VALUE) { 199 if (hFile == INVALID_HANDLE_VALUE) {
179 ret = GetLastError(); 200 ret = GetLastError();
180 LOG(ERROR) << L"Error returned by CreateFile: " << ret; 201 LOG(ERROR) << L"Error returned by CreateFile: " << ret;
181 break; 202 break;
182 } 203 }
183 204
184 if ((!WriteFile(hFile, outBuffer + offset, (DWORD) outSizeProcessed, 205 if ((!WriteFile(hFile, outBuffer + offset, (DWORD) outSizeProcessed,
185 &written, NULL)) || 206 &written, NULL)) ||
186 (written != outSizeProcessed)) { 207 (written != outSizeProcessed)) {
187 ret = GetLastError(); 208 ret = GetLastError();
188 CloseHandle(hFile); 209 CloseHandle(hFile);
189 LOG(ERROR) << L"Error returned by WriteFile: " << ret; 210 LOG(ERROR) << L"Error returned by WriteFile: " << ret;
190 break; 211 break;
191 } 212 }
192 213
193 if (f->IsLastWriteTimeDefined) { 214 if (f->MTimeDefined) {
194 if (!SetFileTime(hFile, NULL, NULL, 215 if (!SetFileTime(hFile, NULL, NULL,
195 (const FILETIME *)&(f->LastWriteTime))) { 216 (const FILETIME *)&(f->MTime))) {
196 ret = GetLastError(); 217 ret = GetLastError();
197 CloseHandle(hFile); 218 CloseHandle(hFile);
198 LOG(ERROR) << L"Error returned by SetFileTime: " << ret; 219 LOG(ERROR) << L"Error returned by SetFileTime: " << ret;
199 break; 220 break;
200 } 221 }
201 } 222 }
202 if (!CloseHandle(hFile)) { 223 if (!CloseHandle(hFile)) {
203 ret = GetLastError(); 224 ret = GetLastError();
204 LOG(ERROR) << L"Error returned by CloseHandle: " << ret; 225 LOG(ERROR) << L"Error returned by CloseHandle: " << ret;
205 break; 226 break;
206 } 227 }
207 } // for loop 228 } // for loop
208 229
209 allocImp.Free(outBuffer); 230 IAlloc_Free(&allocImp, outBuffer);
210 SzArDbExFree(&db, allocImp.Free); 231 SzArEx_Free(&db, &allocImp);
211 return ret; 232 return ret;
212 } 233 }
213 234
214 void LzmaUtil::CloseArchive() { 235 void LzmaUtil::CloseArchive() {
215 if (archive_handle_) { 236 if (archive_handle_) {
216 CloseHandle(archive_handle_); 237 CloseHandle(archive_handle_);
217 archive_handle_ = NULL; 238 archive_handle_ = NULL;
218 } 239 }
219 } 240 }
220 241
221 bool LzmaUtil::CreateDirectory(const FilePath& dir) { 242 bool LzmaUtil::CreateDirectory(const FilePath& dir) {
222 bool ret = true; 243 bool ret = true;
223 if (directories_created_.find(dir.value()) == directories_created_.end()) { 244 if (directories_created_.find(dir.value()) == directories_created_.end()) {
224 ret = file_util::CreateDirectory(dir); 245 ret = file_util::CreateDirectory(dir);
225 if (ret) 246 if (ret)
226 directories_created_.insert(dir.value()); 247 directories_created_.insert(dir.value());
227 } 248 }
228 return ret; 249 return ret;
229 } 250 }
OLDNEW
« no previous file with comments | « no previous file | third_party/lzma_sdk/7z.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698