forked from jrsoftware/issrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathislzma_exe.c
384 lines (334 loc) · 10.3 KB
/
islzma_exe.c
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
Inno Setup
Copyright (C) 1997-2010 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
External EXE-based LZMA encoder
Built on Visual Studio 2005 SP1
$jrsoftware: issrc/Projects/lzma2/Encoder/islzma_exe.c,v 1.2 2010/03/24 19:55:40 jr Exp $
Structures and functions in this file are derived from
LZMA.pas revision 1.49.2.3.
Intentional deviations from the original Pascal code:
- The WaitForMultipleObjects() calls in WakeMainAndWaitUntil and
BeginEncode additionally wait on ProcessData.ParentProcess.
Everything else *should* be 100% consistent.
*/
#include <windows.h>
#include <shlwapi.h>
#include "../C/Types.h"
#include "islzma.h"
#define ISLZMA_EXE_VERSION 101
typedef BYTE Byte;
typedef LONG Longint;
typedef ULONG LongWord;
typedef LongWord THandle32;
#define THandle32ToHandle(h) ULongToHandle(h)
struct TLZMACompressorRingBuffer {
volatile Longint Count; // updated by reader and writer using InterlockedExchangeAdd only
Longint WriterOffset; // accessed only by writer thread
Longint ReaderOffset; // accessed only by reader thread
Byte Buf[0x100000];
};
struct TLZMACompressorSharedEvents {
THandle32 TerminateWorkerEvent;
THandle32 StartEncodeEvent;
THandle32 EndWaitOnInputEvent;
THandle32 EndWaitOnOutputEvent;
THandle32 EndWaitOnProgressEvent;
THandle32 WorkerWaitingOnInputEvent;
THandle32 WorkerWaitingOnOutputEvent;
THandle32 WorkerHasProgressEvent;
THandle32 WorkerEncodeFinishedEvent;
};
struct TLZMACompressorSharedData {
volatile BOOL NoMoreInput;
volatile LongWord ProgressKB;
volatile SRes EncodeResult;
struct TLZMACompressorRingBuffer InputBuffer;
struct TLZMACompressorRingBuffer OutputBuffer;
};
struct TLZMACompressorProcessData {
LongWord StructSize;
THandle32 ParentProcess;
BOOL LZMA2;
struct LZMAEncoderProps EncoderProps;
struct TLZMACompressorSharedEvents Events;
LongWord SharedDataStructSize;
THandle32 SharedDataMapping;
};
static struct TLZMACompressorProcessData ProcessData;
static struct TLZMACompressorSharedEvents *FEvents;
static struct TLZMACompressorSharedData *FShared;
static volatile LONG FReadLock, FWriteLock, FProgressLock;
static volatile DWORD FLastProgressTick;
static Longint RingBufferInternalWriteOrRead(struct TLZMACompressorRingBuffer *Ring,
const BOOL AWrite, Longint *Offset, void *Data, Longint Size)
{
Byte *P = Data;
Longint Bytes;
Longint Result = 0;
while (Size > 0) {
if (AWrite) {
Bytes = sizeof(Ring->Buf) - Ring->Count;
} else {
Bytes = Ring->Count;
}
if (Bytes == 0) {
/* Buffer is full (write) or empty (read) */
break;
}
if (Bytes > Size) {
Bytes = Size;
}
if (Bytes > (Longint)sizeof(Ring->Buf) - *Offset) {
Bytes = (Longint)sizeof(Ring->Buf) - *Offset;
}
if (AWrite) {
memcpy(&Ring->Buf[*Offset], P, Bytes);
InterlockedExchangeAdd(&Ring->Count, Bytes);
} else {
memcpy(P, &Ring->Buf[*Offset], Bytes);
InterlockedExchangeAdd(&Ring->Count, -Bytes);
}
if (*Offset + Bytes == sizeof(Ring->Buf)) {
*Offset = 0;
} else {
*Offset += Bytes;
}
Size -= Bytes;
Result += Bytes;
P += Bytes;
}
return Result;
}
static Longint RingBufferRead(struct TLZMACompressorRingBuffer *Ring,
void *Buf, Longint Size)
{
return RingBufferInternalWriteOrRead(Ring, FALSE, &Ring->ReaderOffset,
Buf, Size);
}
static Longint RingBufferWrite(struct TLZMACompressorRingBuffer *Ring,
void *Buf, Longint Size)
{
return RingBufferInternalWriteOrRead(Ring, TRUE, &Ring->WriterOffset,
Buf, Size);
}
static HRESULT WakeMainAndWaitUntil(HANDLE AWakeEvent, HANDLE AWaitEvent)
{
HANDLE H[3];
if (!SetEvent(AWakeEvent)) {
SetEvent(THandle32ToHandle(FEvents->TerminateWorkerEvent));
return E_FAIL;
}
H[0] = THandle32ToHandle(FEvents->TerminateWorkerEvent);
H[1] = THandle32ToHandle(ProcessData.ParentProcess);
H[2] = AWaitEvent;
switch (WaitForMultipleObjects(3, H, FALSE, INFINITE)) {
case WAIT_OBJECT_0 + 0:
case WAIT_OBJECT_0 + 1:
return E_ABORT;
case WAIT_OBJECT_0 + 2:
return S_OK;
default:
SetEvent(THandle32ToHandle(FEvents->TerminateWorkerEvent));
return E_FAIL;
}
}
static HRESULT FillBuffer(const BOOL AWrite, void *Data, size_t Size,
size_t *ProcessedSize)
/* Called from worker thread (or a thread spawned by the worker thread) */
{
Byte *P;
Longint Bytes;
HRESULT Result;
*ProcessedSize = 0;
if (Size > MAXLONG) {
return E_INVALIDARG;
}
P = Data;
while (Size != 0) {
if (AWrite) {
Bytes = RingBufferWrite(&FShared->OutputBuffer, P, (Longint)Size);
} else {
Bytes = RingBufferRead(&FShared->InputBuffer, P, (Longint)Size);
}
if (Bytes == 0) {
if (AWrite) {
/* Output buffer full; wait for the main thread to flush it */
Result = WakeMainAndWaitUntil(
THandle32ToHandle(FEvents->WorkerWaitingOnOutputEvent),
THandle32ToHandle(FEvents->EndWaitOnOutputEvent));
if (Result != S_OK) {
return Result;
}
} else {
/* Input buffer empty; wait for the main thread to fill it */
if (FShared->NoMoreInput) {
break;
}
Result = WakeMainAndWaitUntil(
THandle32ToHandle(FEvents->WorkerWaitingOnInputEvent),
THandle32ToHandle(FEvents->EndWaitOnInputEvent));
if (Result != S_OK) {
return Result;
}
}
} else {
*ProcessedSize += Bytes;
Size -= Bytes;
P += Bytes;
}
}
return S_OK;
}
static HRESULT Read(void *Data, size_t Size, size_t *ProcessedSize)
/* Called from worker thread (or a thread spawned by the worker thread) */
{
HRESULT Result;
/* Sanity check: Make sure we're the only thread inside Read */
if (InterlockedExchange(&FReadLock, 1) != 0) {
return E_FAIL;
}
Result = FillBuffer(FALSE, Data, Size, ProcessedSize);
FReadLock = 0;
return Result;
}
static HRESULT Write(const void *Data, size_t Size, size_t *ProcessedSize)
/* Called from worker thread (or a thread spawned by the worker thread) */
{
HRESULT Result;
/* Sanity check: Make sure we're the only thread inside Write */
if (InterlockedExchange(&FWriteLock, 1) != 0) {
return E_FAIL;
}
Result = FillBuffer(TRUE, (void *)Data, Size, ProcessedSize);
FWriteLock = 0;
return Result;
}
static HRESULT ProgressMade(const UInt64 TotalBytesProcessed)
/* Called from worker thread (or a thread spawned by the worker thread) */
{
DWORD T;
UInt64 KBProcessed;
HRESULT Result;
T = GetTickCount();
if (T - FLastProgressTick >= 100) {
/* Sanity check: Make sure we're the only thread inside Progress */
if (InterlockedExchange(&FProgressLock, 1) != 0) {
return E_FAIL;
}
FLastProgressTick = T;
/* Make sure TotalBytesProcessed isn't negative. LZMA's Types.h says
"-1 for size means unknown value", though I don't see any place
where LzmaEnc actually does call Progress with inSize = -1. */
if ((Int64)TotalBytesProcessed >= 0) {
KBProcessed = TotalBytesProcessed;
KBProcessed /= 1024;
FShared->ProgressKB = (LongWord)KBProcessed;
}
Result = WakeMainAndWaitUntil(
THandle32ToHandle(FEvents->WorkerHasProgressEvent),
THandle32ToHandle(FEvents->EndWaitOnProgressEvent));
FProgressLock = 0;
} else {
Result = S_OK;
}
return Result;
}
static SRes LZMASeqInStreamReadWrapper(void *p, void *buf, size_t *size)
{
if (Read(buf, *size, size) == S_OK) {
return SZ_OK;
} else {
return SZ_ERROR_READ;
}
}
static size_t LZMASeqOutStreamWriteWrapper(void *p, const void *buf, size_t size)
{
size_t Result;
if (Write(buf, size, &Result) != S_OK) {
return 0;
}
return Result;
}
static SRes LZMACompressProgressProgressWrapper(void *p, UInt64 inSize, UInt64 outSize)
{
if (ProgressMade(inSize) == S_OK) {
return SZ_OK;
} else {
return SZ_ERROR_PROGRESS;
}
}
static int BeginEncode(void)
{
static ISeqInStream InStream = { LZMASeqInStreamReadWrapper };
static ISeqOutStream OutStream = { LZMASeqOutStreamWriteWrapper };
static ICompressProgress CompressProgress = { LZMACompressProgressProgressWrapper };
struct LZMAHandle *FLZMAHandle;
SRes res;
HANDLE H[3];
res = LZMA_Init(ProcessData.LZMA2, &FLZMAHandle);
if (res != S_OK) {
return MAKELONG(res, 20);
}
res = LZMA_SetProps(FLZMAHandle, &ProcessData.EncoderProps,
sizeof(ProcessData.EncoderProps));
if (res != S_OK) {
return MAKELONG(res, 21);
}
// WorkerThreadProc:
H[0] = THandle32ToHandle(FEvents->TerminateWorkerEvent);
H[1] = THandle32ToHandle(ProcessData.ParentProcess);
H[2] = THandle32ToHandle(FEvents->StartEncodeEvent);
while (WaitForMultipleObjects(3, H, FALSE, INFINITE) == WAIT_OBJECT_0 + 2) {
FShared->EncodeResult = LZMA_Encode(FLZMAHandle, &InStream, &OutStream,
&CompressProgress);
if (!SetEvent(THandle32ToHandle(FEvents->WorkerEncodeFinishedEvent))) {
break;
}
}
res = LZMA_End(FLZMAHandle);
return MAKELONG(res, 22);
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nCmdShow)
{
int Version;
THandle32 ProcessDataMapping;
struct TLZMACompressorProcessData *ProcessDataView;
if (__argc < 3) {
MessageBox(NULL, TEXT("This program is used internally by ")
TEXT("the Inno Setup Compiler for LZMA compression. ")
TEXT("It cannot be started directly."),
TEXT("LZMA Compression Helper"), MB_OK | MB_ICONINFORMATION);
return MAKELONG(0, 1);
}
if (!StrToIntEx(__wargv[1], STIF_DEFAULT, &Version) ||
Version != ISLZMA_EXE_VERSION) {
return MAKELONG(0, 2);
}
if (!StrToIntEx(__wargv[2], STIF_SUPPORT_HEX, &ProcessDataMapping)) {
return MAKELONG(0, 3);
}
ProcessDataView = MapViewOfFile(THandle32ToHandle(ProcessDataMapping),
FILE_MAP_READ, 0, 0, sizeof(ProcessData));
if (!ProcessDataView) {
return MAKELONG(GetLastError(), 4);
}
ProcessData = *ProcessDataView;
UnmapViewOfFile(ProcessDataView);
CloseHandle(THandle32ToHandle(ProcessDataMapping));
if (ProcessData.StructSize != sizeof(ProcessData)) {
return MAKELONG(0, 5);
}
if (ProcessData.SharedDataStructSize != sizeof(*FShared)) {
return MAKELONG(0, 6);
}
FEvents = &ProcessData.Events;
FShared = MapViewOfFile(THandle32ToHandle(ProcessData.SharedDataMapping),
FILE_MAP_WRITE, 0, 0, sizeof(*FShared));
if (!FShared) {
return MAKELONG(GetLastError(), 7);
}
return BeginEncode();
}