forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecompress.cc
303 lines (255 loc) · 9.4 KB
/
decompress.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chrome/installer/mini_installer/decompress.h"
#include <windows.h>
#include <fcntl.h> // for _O_* constants
#include <fdi.h>
#include <stddef.h>
#include <stdlib.h>
#include "chrome/installer/mini_installer/mini_file.h"
namespace {
// A simple struct to hold data passed to and from FDICopy via its |pvUser|
// argument.
struct ExpandContext {
// The path to the single destination file.
const wchar_t* const dest_path;
// The destination file; valid once the destination is created.
mini_installer::MiniFile dest_file;
// Set to true if the file was extracted to |dest_path|. Note that |dest_file|
// may be valid even in case of failure.
bool succeeded;
};
FNALLOC(Alloc) {
return ::HeapAlloc(::GetProcessHeap(), 0, cb);
}
FNFREE(Free) {
::HeapFree(::GetProcessHeap(), 0, pv);
}
// Converts a wide string to utf8. Set |len| to -1 if |str| is zero terminated
// and you want to convert the entire string.
// The returned string will have been allocated with Alloc(), so free it
// with a call to Free().
char* WideToUtf8(const wchar_t* str, int len) {
char* ret = nullptr;
int size =
WideCharToMultiByte(CP_UTF8, 0, str, len, nullptr, 0, nullptr, nullptr);
if (size) {
if (len != -1)
++size; // include space for the terminator.
ret = reinterpret_cast<char*>(Alloc(size * sizeof(ret[0])));
if (ret) {
WideCharToMultiByte(CP_UTF8, 0, str, len, ret, size, nullptr, nullptr);
if (len != -1)
ret[size - 1] = '\0'; // terminate the string
}
}
return ret;
}
wchar_t* Utf8ToWide(const char* str) {
wchar_t* ret = nullptr;
int size = MultiByteToWideChar(CP_UTF8, 0, str, -1, nullptr, 0);
if (size) {
ret = reinterpret_cast<wchar_t*>(Alloc(size * sizeof(ret[0])));
if (ret)
MultiByteToWideChar(CP_UTF8, 0, str, -1, ret, size);
}
return ret;
}
template <typename T>
class scoped_ptr {
public:
explicit scoped_ptr(T* a) : a_(a) {}
~scoped_ptr() {
if (a_)
Free(a_);
}
operator T*() { return a_; }
private:
T* a_;
};
FNOPEN(Open) {
DWORD access = 0;
DWORD disposition = 0;
if (oflag & _O_RDWR) {
access = GENERIC_READ | GENERIC_WRITE;
} else if (oflag & _O_WRONLY) {
access = GENERIC_WRITE;
} else {
access = GENERIC_READ;
}
if (oflag & _O_CREAT) {
disposition = CREATE_ALWAYS;
} else {
disposition = OPEN_EXISTING;
}
scoped_ptr<wchar_t> path(Utf8ToWide(pszFile));
HANDLE file =
CreateFileW(path, access, FILE_SHARE_DELETE | FILE_SHARE_READ, nullptr,
disposition, FILE_ATTRIBUTE_NORMAL, nullptr);
return reinterpret_cast<INT_PTR>(file);
}
FNREAD(Read) {
DWORD read = 0;
if (!::ReadFile(reinterpret_cast<HANDLE>(hf), pv, cb, &read, nullptr))
read = static_cast<DWORD>(-1L);
return read;
}
FNWRITE(Write) {
DWORD written = 0;
if (!::WriteFile(reinterpret_cast<HANDLE>(hf), pv, cb, &written, nullptr))
written = static_cast<DWORD>(-1L);
return written;
}
FNCLOSE(Close) {
return ::CloseHandle(reinterpret_cast<HANDLE>(hf)) ? 0 : -1;
}
FNSEEK(Seek) {
return ::SetFilePointer(reinterpret_cast<HANDLE>(hf), dist, nullptr,
seektype);
}
FNFDINOTIFY(Notify) {
// Since we will only ever be decompressing a single file at a time
// we take a shortcut and provide a pointer to the wide destination file
// of the file we want to write. This way we don't have to bother with
// utf8/wide conversion and concatenation of directory and file name.
ExpandContext& context = *reinterpret_cast<ExpandContext*>(pfdin->pv);
switch (fdint) {
case fdintCOPY_FILE:
context.dest_file.Create(context.dest_path);
// By sheer coincidence, CreateFileW's success/failure results match that
// of fdintCOPY_FILE. The handle given out here is closed either by
// FDICopy (in case of error) or below when handling fdintCLOSE_FILE_INFO
// (in case of success).
return reinterpret_cast<INT_PTR>(context.dest_file.DuplicateHandle());
case fdintCLOSE_FILE_INFO: {
// Set the file's creation time and file attributes.
FILE_BASIC_INFO info = {};
FILETIME file_time;
FILETIME local;
if (DosDateTimeToFileTime(pfdin->date, pfdin->time, &file_time) &&
LocalFileTimeToFileTime(&file_time, &local)) {
info.CreationTime.u.LowPart = local.dwLowDateTime;
info.CreationTime.u.HighPart = local.dwHighDateTime;
}
info.FileAttributes =
pfdin->attribs & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE);
::SetFileInformationByHandle(reinterpret_cast<HANDLE>(pfdin->hf),
FileBasicInfo, &info, sizeof(info));
// Close the handle given out above in fdintCOPY_FILE.
::CloseHandle(reinterpret_cast<HANDLE>(pfdin->hf));
context.succeeded = true;
return -1; // Break: the one file was extracted.
}
case fdintCABINET_INFO:
case fdintENUMERATE:
return 0; // Continue: success.
case fdintPARTIAL_FILE:
case fdintNEXT_CABINET:
default:
return -1; // Break: error.
}
}
// Module handle of cabinet.dll
HMODULE g_fdi = nullptr;
// API prototypes.
typedef HFDI(DIAMONDAPI* FDICreateFn)(PFNALLOC alloc,
PFNFREE free,
PFNOPEN open,
PFNREAD read,
PFNWRITE write,
PFNCLOSE close,
PFNSEEK seek,
int cpu_type,
PERF perf);
typedef BOOL(DIAMONDAPI* FDIDestroyFn)(HFDI fdi);
typedef BOOL(DIAMONDAPI* FDICopyFn)(HFDI fdi,
char* cab,
char* cab_path,
int flags,
PFNFDINOTIFY notify,
PFNFDIDECRYPT decrypt,
void* context);
FDICreateFn g_FDICreate = nullptr;
FDIDestroyFn g_FDIDestroy = nullptr;
FDICopyFn g_FDICopy = nullptr;
bool InitializeFdi() {
if (!g_fdi) {
// It has been observed that some users do not have the expected
// environment variables set, so we try a couple that *should* always be
// present and fallback to the default Windows install path if all else
// fails.
// The cabinet.dll should be available on all supported versions of Windows.
static const wchar_t* const candidate_paths[] = {
L"%WINDIR%\\system32\\cabinet.dll",
L"%SYSTEMROOT%\\system32\\cabinet.dll",
L"C:\\Windows\\system32\\cabinet.dll",
};
wchar_t path[MAX_PATH] = {};
for (size_t i = 0; i < _countof(candidate_paths); ++i) {
path[0] = L'\0';
DWORD result =
::ExpandEnvironmentStringsW(candidate_paths[i], path, _countof(path));
if (result > 0 && result <= _countof(path))
g_fdi = ::LoadLibraryExW(path, nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);
if (g_fdi)
break;
}
}
if (g_fdi) {
g_FDICreate =
reinterpret_cast<FDICreateFn>(::GetProcAddress(g_fdi, "FDICreate"));
g_FDIDestroy =
reinterpret_cast<FDIDestroyFn>(::GetProcAddress(g_fdi, "FDIDestroy"));
g_FDICopy = reinterpret_cast<FDICopyFn>(::GetProcAddress(g_fdi, "FDICopy"));
}
return g_FDICreate && g_FDIDestroy && g_FDICopy;
}
} // namespace
namespace mini_installer {
bool Expand(const wchar_t* source, const wchar_t* destination) {
if (!InitializeFdi())
return false;
// Start by splitting up the source path and convert to utf8 since the
// cabinet API doesn't support wide strings.
const wchar_t* source_name = source + lstrlenW(source);
while (source_name > source && *source_name != L'\\')
--source_name;
if (source_name == source)
return false;
// Convert the name to utf8.
source_name++;
scoped_ptr<char> source_name_utf8(WideToUtf8(source_name, -1));
// The directory part is assumed to have a trailing backslash.
scoped_ptr<char> source_path_utf8(WideToUtf8(source, source_name - source));
if (!source_name_utf8 || !source_path_utf8)
return false;
ERF erf = {0};
HFDI fdi = g_FDICreate(&Alloc, &Free, &Open, &Read, &Write, &Close, &Seek,
cpuUNKNOWN, &erf);
if (!fdi)
return false;
ExpandContext context = {destination, {}, /*succeeded=*/false};
g_FDICopy(fdi, source_name_utf8, source_path_utf8, 0, &Notify, nullptr,
&context);
g_FDIDestroy(fdi);
if (context.succeeded) {
// https://crbug.com/1443320: We see crashes on Windows 10 when running
// setup.exe in which it appears that an entire hunk of the file is zeros or
// random memory. There is nothing out of the ordinary in the way that this
// file is written (0x8000 byte chunks via normal WriteFile calls). As an
// experiment, flush the file before closing it.
::FlushFileBuffers(context.dest_file.GetHandleUnsafe());
return true;
}
// Delete the output file if it was created.
if (context.dest_file.IsValid())
context.dest_file.DeleteOnClose();
return false;
}
} // namespace mini_installer