| OLD | NEW |
| 1 // Copyright (c) 2010 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/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/C/7z.h" |
| 13 #include "third_party/lzma_sdk/Archive/7z/7zIn.h" | 13 #include "third_party/lzma_sdk/C/7zAlloc.h" |
| 14 #include "third_party/lzma_sdk/7zCrc.h" | 14 #include "third_party/lzma_sdk/C/7zCrc.h" |
| 15 #include "third_party/lzma_sdk/C/7zFile.h" |
| 16 #include "third_party/lzma_sdk/C/7zVersion.h" |
| 15 } | 17 } |
| 16 | 18 |
| 17 | 19 |
| 18 namespace { | |
| 19 | |
| 20 typedef struct _CFileInStream { | |
| 21 ISzInStream InStream; | |
| 22 HANDLE File; | |
| 23 } CFileInStream; | |
| 24 | |
| 25 | |
| 26 size_t LzmaReadFile(HANDLE file, void *data, size_t size) { | |
| 27 if (size == 0) | |
| 28 return 0; | |
| 29 | |
| 30 size_t processedSize = 0; | |
| 31 do { | |
| 32 DWORD processedLoc = 0; | |
| 33 BOOL res = ReadFile(file, data, (DWORD) size, &processedLoc, NULL); | |
| 34 data = (void *)((unsigned char *) data + processedLoc); | |
| 35 size -= processedLoc; | |
| 36 processedSize += processedLoc; | |
| 37 if (!res || processedLoc == 0) | |
| 38 break; | |
| 39 } while (size > 0); | |
| 40 | |
| 41 return 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; | |
| 69 } | |
| 70 | |
| 71 } // namespace | |
| 72 | |
| 73 // static | 20 // static |
| 74 int32 LzmaUtil::UnPackArchive(const std::wstring& archive, | 21 int32 LzmaUtil::UnPackArchive(const std::wstring& archive, |
| 75 const std::wstring& output_dir, | 22 const std::wstring& output_dir, |
| 76 std::wstring* output_file) { | 23 std::wstring* output_file) { |
| 77 VLOG(1) << "Opening archive " << archive; | 24 VLOG(1) << "Opening archive " << archive; |
| 78 LzmaUtil lzma_util; | 25 LzmaUtil lzma_util; |
| 79 DWORD ret; | 26 DWORD ret; |
| 80 if ((ret = lzma_util.OpenArchive(archive)) != NO_ERROR) { | 27 if ((ret = lzma_util.OpenArchive(archive)) != NO_ERROR) { |
| 81 LOG(ERROR) << "Unable to open install archive: " << archive | 28 LOG(ERROR) << "Unable to open install archive: " << archive |
| 82 << ", error: " << ret; | 29 << ", error: " << ret; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 | 61 |
| 115 DWORD LzmaUtil::UnPack(const std::wstring& location) { | 62 DWORD LzmaUtil::UnPack(const std::wstring& location) { |
| 116 return UnPack(location, NULL); | 63 return UnPack(location, NULL); |
| 117 } | 64 } |
| 118 | 65 |
| 119 DWORD LzmaUtil::UnPack(const std::wstring& location, | 66 DWORD LzmaUtil::UnPack(const std::wstring& location, |
| 120 std::wstring* output_file) { | 67 std::wstring* output_file) { |
| 121 if (!archive_handle_) | 68 if (!archive_handle_) |
| 122 return ERROR_INVALID_HANDLE; | 69 return ERROR_INVALID_HANDLE; |
| 123 | 70 |
| 124 CFileInStream archiveStream; | 71 CFileInStream archive_stream; |
| 125 ISzAlloc allocImp; | 72 CLookToRead look_stream; |
| 126 ISzAlloc allocTempImp; | 73 ISzAlloc alloc_imp; |
| 127 CArchiveDatabaseEx db; | 74 ISzAlloc alloc_temp_imp; |
| 75 CSzArEx db; |
| 128 DWORD ret = NO_ERROR; | 76 DWORD ret = NO_ERROR; |
| 129 | 77 |
| 130 archiveStream.File = archive_handle_; | 78 FileInStream_CreateVTable(&archive_stream); |
| 131 archiveStream.InStream.Read = SzFileReadImp; | 79 archive_stream.file.handle = archive_handle_; |
| 132 archiveStream.InStream.Seek = SzFileSeekImp; | 80 LookToRead_CreateVTable(&look_stream, False); |
| 133 allocImp.Alloc = SzAlloc; | 81 look_stream.realStream = &archive_stream.s; |
| 134 allocImp.Free = SzFree; | 82 LookToRead_Init(&look_stream); |
| 135 allocTempImp.Alloc = SzAllocTemp; | 83 |
| 136 allocTempImp.Free = SzFreeTemp; | 84 alloc_imp.Alloc = SzAlloc; |
| 85 alloc_imp.Free = SzFree; |
| 86 alloc_temp_imp.Alloc = SzAllocTemp; |
| 87 alloc_temp_imp.Free = SzFreeTemp; |
| 137 | 88 |
| 138 CrcGenerateTable(); | 89 CrcGenerateTable(); |
| 139 SzArDbExInit(&db); | 90 SzArEx_Init(&db); |
| 140 if ((ret = SzArchiveOpen(&archiveStream.InStream, &db, | 91 if ((ret = SzArEx_Open(&db, &look_stream.s, |
| 141 &allocImp, &allocTempImp)) != SZ_OK) { | 92 &alloc_imp, &alloc_temp_imp)) != SZ_OK) { |
| 142 LOG(ERROR) << L"Error returned by SzArchiveOpen: " << ret; | 93 LOG(ERROR) << L"Error returned by SzArEx_Open: " << ret; |
| 143 return ret; | 94 return ERROR_INVALID_HANDLE; |
| 144 } | 95 } |
| 145 | 96 |
| 146 Byte *outBuffer = 0; // it must be 0 before first call for each new archive | 97 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 | 98 UInt32 blockIndex = 0xFFFFFFFF; // can have any value if outBuffer = 0 |
| 148 size_t outBufferSize = 0; // can have any value if outBuffer = 0 | 99 size_t outBufferSize = 0; // can have any value if outBuffer = 0 |
| 149 | 100 |
| 150 for (unsigned int i = 0; i < db.Database.NumFiles; i++) { | 101 for (unsigned int i = 0; i < db.db.NumFiles; i++) { |
| 151 DWORD written; | 102 DWORD written; |
| 152 size_t offset; | 103 size_t offset; |
| 153 size_t outSizeProcessed; | 104 size_t outSizeProcessed; |
| 154 CFileItem *f = db.Database.Files + i; | 105 CSzFileItem* f = db.db.Files + i; |
| 155 | 106 |
| 156 if ((ret = SzExtract(&archiveStream.InStream, &db, i, &blockIndex, | 107 if ((ret = SzArEx_Extract(&db, &look_stream.s, i, &blockIndex, |
| 157 &outBuffer, &outBufferSize, &offset, &outSizeProcessed, | 108 &outBuffer, &outBufferSize, &offset, |
| 158 &allocImp, &allocTempImp)) != SZ_OK) { | 109 &outSizeProcessed, &alloc_imp, |
| 159 LOG(ERROR) << L"Error returned by SzExtract: " << ret; | 110 &alloc_temp_imp)) != SZ_OK) { |
| 111 LOG(ERROR) << L"Error returned by SzArEx_Extract: " << ret; |
| 160 break; | 112 break; |
| 161 } | 113 } |
| 162 | 114 |
| 163 FilePath file_path = FilePath(location).Append(UTF8ToWide(f->Name)); | 115 UInt16* name = NULL; |
| 116 size_t len = SzArEx_GetFileNameUtf16(&db, i, NULL); |
| 117 name = reinterpret_cast<UInt16*>(SzAlloc(NULL, len * sizeof(UInt16))); |
| 118 if (!name) { |
| 119 ret = GetLastError(); |
| 120 LOG(ERROR) << L"Can't allocate memory for filename string."; |
| 121 break; |
| 122 } |
| 123 SzArEx_GetFileNameUtf16(&db, i, name); |
| 124 std::wstring wname; |
| 125 if (!UTF16ToWide(reinterpret_cast<char16*>(name), len - 1, &wname)) { |
| 126 ret = GetLastError(); |
| 127 LOG(ERROR) << L"Can't convert filename string."; |
| 128 break; |
| 129 } |
| 130 SzFree(NULL, name); |
| 131 |
| 132 FilePath file_path = FilePath(location).Append(wname); |
| 164 if (output_file) | 133 if (output_file) |
| 165 *output_file = file_path.value(); | 134 *output_file = file_path.value(); |
| 166 | 135 |
| 167 // If archive entry is directory create it and move on to the next entry. | 136 // If archive entry is directory create it and move on to the next entry. |
| 168 if (f->IsDirectory) { | 137 if (f->IsDir) { |
| 169 CreateDirectory(file_path); | 138 CreateDirectory(file_path); |
| 170 continue; | 139 continue; |
| 171 } | 140 } |
| 172 | 141 |
| 173 CreateDirectory(file_path.DirName()); | 142 CreateDirectory(file_path.DirName()); |
| 174 | 143 |
| 175 HANDLE hFile; | 144 HANDLE hFile; |
| 176 hFile = CreateFile(file_path.value().c_str(), GENERIC_WRITE, 0, NULL, | 145 hFile = CreateFile(file_path.value().c_str(), GENERIC_WRITE, 0, NULL, |
| 177 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); | 146 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 178 if (hFile == INVALID_HANDLE_VALUE) { | 147 if (hFile == INVALID_HANDLE_VALUE) { |
| 179 ret = GetLastError(); | 148 ret = GetLastError(); |
| 180 LOG(ERROR) << L"Error returned by CreateFile: " << ret; | 149 LOG(ERROR) << L"Error returned by CreateFile: " << ret; |
| 181 break; | 150 break; |
| 182 } | 151 } |
| 183 | 152 |
| 184 if ((!WriteFile(hFile, outBuffer + offset, (DWORD) outSizeProcessed, | 153 if ((!WriteFile(hFile, outBuffer + offset, (DWORD) outSizeProcessed, |
| 185 &written, NULL)) || | 154 &written, NULL)) || |
| 186 (written != outSizeProcessed)) { | 155 (written != outSizeProcessed)) { |
| 187 ret = GetLastError(); | 156 ret = GetLastError(); |
| 188 CloseHandle(hFile); | 157 CloseHandle(hFile); |
| 189 LOG(ERROR) << L"Error returned by WriteFile: " << ret; | 158 LOG(ERROR) << L"Error returned by WriteFile: " << ret; |
| 190 break; | 159 break; |
| 191 } | 160 } |
| 192 | 161 |
| 193 if (f->IsLastWriteTimeDefined) { | 162 if (f->MTimeDefined) { |
| 194 if (!SetFileTime(hFile, NULL, NULL, | 163 if (!SetFileTime(hFile, NULL, NULL, |
| 195 (const FILETIME *)&(f->LastWriteTime))) { | 164 reinterpret_cast<FILETIME*>(&(f->MTime)))) { |
| 196 ret = GetLastError(); | 165 ret = GetLastError(); |
| 197 CloseHandle(hFile); | 166 CloseHandle(hFile); |
| 198 LOG(ERROR) << L"Error returned by SetFileTime: " << ret; | 167 LOG(ERROR) << L"Error returned by SetFileTime: " << ret; |
| 199 break; | 168 break; |
| 200 } | 169 } |
| 201 } | 170 } |
| 202 if (!CloseHandle(hFile)) { | 171 if (!CloseHandle(hFile)) { |
| 203 ret = GetLastError(); | 172 ret = GetLastError(); |
| 204 LOG(ERROR) << L"Error returned by CloseHandle: " << ret; | 173 LOG(ERROR) << L"Error returned by CloseHandle: " << ret; |
| 205 break; | 174 break; |
| 206 } | 175 } |
| 207 } // for loop | 176 } // for loop |
| 208 | 177 |
| 209 allocImp.Free(outBuffer); | 178 SzArEx_Free(&db, &alloc_imp); |
| 210 SzArDbExFree(&db, allocImp.Free); | |
| 211 return ret; | 179 return ret; |
| 212 } | 180 } |
| 213 | 181 |
| 214 void LzmaUtil::CloseArchive() { | 182 void LzmaUtil::CloseArchive() { |
| 215 if (archive_handle_) { | 183 if (archive_handle_) { |
| 216 CloseHandle(archive_handle_); | 184 CloseHandle(archive_handle_); |
| 217 archive_handle_ = NULL; | 185 archive_handle_ = NULL; |
| 218 } | 186 } |
| 219 } | 187 } |
| 220 | 188 |
| 221 bool LzmaUtil::CreateDirectory(const FilePath& dir) { | 189 bool LzmaUtil::CreateDirectory(const FilePath& dir) { |
| 222 bool ret = true; | 190 bool ret = true; |
| 223 if (directories_created_.find(dir.value()) == directories_created_.end()) { | 191 if (directories_created_.find(dir.value()) == directories_created_.end()) { |
| 224 ret = file_util::CreateDirectory(dir); | 192 ret = file_util::CreateDirectory(dir); |
| 225 if (ret) | 193 if (ret) |
| 226 directories_created_.insert(dir.value()); | 194 directories_created_.insert(dir.value()); |
| 227 } | 195 } |
| 228 return ret; | 196 return ret; |
| 229 } | 197 } |
| OLD | NEW |