Debian Codegraph @

Source/clzma.cpp

raw
1 /*
2 * clzma.cpp
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 *
16 * Unicode support by Jim Park -- 08/24/2007
17 */
18
19 #include <algorithm> // for std::min
20 #include "clzma.h"
21
22 using namespace std;
23
24 #ifndef _WIN32
25 struct evnet_t
26 {
27 pthread_cond_t cond;
28 pthread_mutex_t mutex;
29 bool signaled;
30 };
31
32 HANDLE CreateEvent(void *, BOOL, BOOL, TCHAR *)
33 {
34 evnet_t *event = (evnet_t *) malloc(sizeof(evnet_t));
35 if (!event)
36 return 0;
37 if (pthread_cond_init(&event->cond, NULL))
38 {
39 free(event);
40 return 0;
41 }
42 if (pthread_mutex_init(&event->mutex, NULL))
43 {
44 pthread_cond_destroy(&event->cond);
45 free(event);
46 return 0;
47 }
48 event->signaled = false;
49 return (HANDLE) event;
50 }
51
52 BOOL SetEvent(HANDLE _event)
53 {
54 evnet_t *event = (evnet_t *) _event;
55 if (pthread_mutex_lock(&event->mutex))
56 return FALSE;
57 event->signaled = true;
58 pthread_cond_signal(&event->cond);
59 if (pthread_mutex_unlock(&event->mutex))
60 return FALSE;
61 return TRUE;
62 }
63
64 BOOL ResetEvent(HANDLE _event)
65 {
66 evnet_t *event = (evnet_t *) _event;
67 if (pthread_mutex_lock(&event->mutex))
68 return FALSE;
69 event->signaled = false;
70 if (pthread_mutex_unlock(&event->mutex))
71 return FALSE;
72 return TRUE;
73 }
74
75 BOOL CloseHandle(HANDLE _event)
76 {
77 BOOL ret = TRUE;
78 evnet_t *event = (evnet_t *) _event;
79 if (!event)
80 return FALSE;
81 if (pthread_cond_destroy(&event->cond))
82 ret = FALSE;
83 if (pthread_mutex_destroy(&event->mutex))
84 ret = FALSE;
85 free(event);
86 return ret;
87 }
88
89 #define WAIT_OBJECT_0 0
90 #define INFINITE 0
91 DWORD WaitForSingleObject(HANDLE _event, DWORD) {
92 DWORD ret = WAIT_OBJECT_0;
93 evnet_t *event = (evnet_t *) _event;
94 if (pthread_mutex_lock(&event->mutex))
95 return !WAIT_OBJECT_0;
96 if (!event->signaled)
97 {
98 if (pthread_cond_wait(&event->cond, &event->mutex))
99 {
100 ret = !WAIT_OBJECT_0;
101 }
102 }
103 event->signaled = false;
104 pthread_mutex_unlock(&event->mutex);
105 return ret;
106 }
107
108 #define WaitForMultipleObjects(x, list, y, t) WaitForSingleObject(list[0], t)
109
110 #endif
111
112 #ifdef _WIN32
113 DWORD CLZMA::lzmaCompressThread(LPVOID lpParameter)
114 #else
115 void* CLZMA::lzmaCompressThread(void *lpParameter)
116 #endif
117 {
118 CLZMA *Compressor = (CLZMA *) lpParameter;
119 if (!Compressor)
120 return 0;
121
122 Compressor->CompressReal();
123 return 0;
124 }
125
126 int CLZMA::ConvertError(HRESULT result)
127 {
128 if (result != S_OK)
129 {
130 if (result == E_OUTOFMEMORY)
131 return LZMA_MEM_ERROR;
132 else
133 return LZMA_IO_ERROR;
134 }
135 return C_OK;
136 }
137
138 CLZMA::CLZMA(): _encoder(NULL)
139 {
140 _encoder = new NCompress::NLZMA::CEncoder();
141 _encoder->SetWriteEndMarkerMode(true);
142 #ifdef _WIN32
143 hCompressionThread = NULL;
144 #else
145 hCompressionThread = 0;
146 #endif
147 hNeedIOEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
148 hIOReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
149 finish = FALSE;
150 compressor_finished = TRUE;
151 hCompressionThread = 0;
152 SetNextOut(NULL, 0);
153 SetNextIn(NULL, 0);
154
155 AddRef(); // will be manually deleted, not released
156 }
157
158 CLZMA::~CLZMA()
159 {
160 End();
161 if (hNeedIOEvent)
162 {
163 CloseHandle(hNeedIOEvent);
164 hNeedIOEvent = NULL;
165 }
166 if (hIOReadyEvent)
167 {
168 CloseHandle(hIOReadyEvent);
169 hIOReadyEvent = NULL;
170 }
171 if (_encoder)
172 {
173 delete _encoder;
174 _encoder = NULL;
175 }
176 }
177
178 int CLZMA::Init(int level, unsigned int dicSize)
179 {
180 End();
181
182 compressor_finished = FALSE;
183 finish = FALSE;
184 res = C_OK;
185
186 if (!hNeedIOEvent || !hIOReadyEvent)
187 {
188 return LZMA_INIT_ERROR;
189 }
190
191 ResetEvent(hNeedIOEvent);
192 ResetEvent(hIOReadyEvent);
193
194 res = C_OK;
195
196 PROPID propdIDs [] =
197 {
198 NCoderPropID::kAlgorithm,
199 NCoderPropID::kDictionarySize,
200 NCoderPropID::kNumFastBytes
201 };
202 const int kNumProps = COUNTOF(propdIDs);
203 PROPVARIANT props[kNumProps];
204 // NCoderPropID::kAlgorithm
205 props[0].vt = VT_UI4;
206 props[0].ulVal = 2;
207 // NCoderPropID::kDictionarySize
208 props[1].vt = VT_UI4;
209 props[1].ulVal = dicSize;
210 // NCoderPropID::kNumFastBytes
211 props[2].vt = VT_UI4;
212 props[2].ulVal = 64;
213 if (_encoder->SetCoderProperties(propdIDs, props, kNumProps) != 0)
214 return LZMA_INIT_ERROR;
215 return _encoder->SetStreams(this, this, 0, 0) == S_OK ? C_OK : LZMA_INIT_ERROR;
216 }
217
218 int CLZMA::End()
219 {
220 // has compressor not finished?
221 if (hCompressionThread && !compressor_finished)
222 {
223 // kill compression thread
224 avail_in = 0;
225 avail_out = 0;
226 compressor_finished = TRUE;
227
228 SetEvent(hIOReadyEvent);
229 #ifdef _WIN32
230 WaitForSingleObject(hCompressionThread, INFINITE);
231 #else
232 pthread_join(hCompressionThread, NULL);
233 #endif
234 }
235 if (hCompressionThread)
236 {
237 #ifdef _WIN32
238 CloseHandle(hCompressionThread);
239 hCompressionThread = NULL;
240 #else
241 pthread_detach(hCompressionThread);
242 hCompressionThread = 0;
243 #endif
244 }
245 SetNextOut(NULL, 0);
246 SetNextIn(NULL, 0);
247 return C_OK;
248 }
249
250 int CLZMA::CompressReal()
251 {
252 try
253 {
254 HRESULT hResult = _encoder->WriteCoderProperties(this);
255 if (hResult == S_OK)
256 {
257 while (true)
258 {
259 UINT64 inSize, outSize;
260 INT32 finished;
261 hResult = _encoder->CodeOneBlock(&inSize, &outSize, &finished);
262 if (hResult != S_OK && res == C_OK)
263 res = ConvertError(hResult);
264 if (res != C_OK)
265 break;
266 if (finished)
267 {
268 res = C_FINISHED;
269 break;
270 }
271 }
272 }
273 else
274 {
275 if (res == C_OK)
276 res = ConvertError(hResult);
277 }
278 }
279 catch (...)
280 {
281 if (res == C_OK)
282 res = LZMA_IO_ERROR;
283 }
284
285 compressor_finished = TRUE;
286 SetEvent(hNeedIOEvent);
287 return C_OK;
288 }
289
290 int CLZMA::Compress(bool flush)
291 {
292 if (compressor_finished)
293 {
294 // act like zlib when it comes to stream ending
295 if (flush)
296 return C_OK;
297 else
298 return LZMA_BAD_CALL;
299 }
300
301 finish = flush;
302
303 if (!hCompressionThread)
304 {
305 #ifdef _WIN32
306 DWORD dwThreadId;
307
308 hCompressionThread = CreateThread(0, 0, lzmaCompressThread, (LPVOID) this, 0, &dwThreadId);
309 if (!hCompressionThread)
310 #else
311 if (pthread_create(&hCompressionThread, NULL, lzmaCompressThread, (LPVOID) this))
312 #endif
313 return LZMA_INIT_ERROR;
314 }
315 else
316 {
317 SetEvent(hIOReadyEvent);
318 }
319
320 HANDLE waitList[2] = {hNeedIOEvent, (HANDLE) hCompressionThread};
321 if (WaitForMultipleObjects(2, waitList, FALSE, INFINITE) != WAIT_OBJECT_0)
322 {
323 // thread ended or WaitForMultipleObjects failed
324 compressor_finished = TRUE;
325 SetEvent(hIOReadyEvent);
326 return LZMA_THREAD_ERROR;
327 }
328
329 if (compressor_finished)
330 {
331 return res;
332 }
333
334 return C_OK;
335 }
336
337 void CLZMA::GetMoreIO()
338 {
339 SetEvent(hNeedIOEvent);
340 if (WaitForSingleObject(hIOReadyEvent, INFINITE) != WAIT_OBJECT_0)
341 {
342 compressor_finished = TRUE;
343 res = LZMA_THREAD_ERROR;
344 }
345 }
346
347 HRESULT CLZMA::Read(void *data, UINT32 size, UINT32 *processedSize)
348 {
349 return ReadPart(data, size, processedSize);
350 }
351
352 HRESULT CLZMA::ReadPart(void *data, UINT32 size, UINT32 *processedSize)
353 {
354 if (processedSize)
355 *processedSize = 0;
356 while (size)
357 {
358 if (!avail_in)
359 {
360 if (finish)
361 {
362 return S_OK;
363 }
364 GetMoreIO();
365 if (!avail_in)
366 {
367 if (finish)
368 {
369 return S_OK;
370 }
371 return E_ABORT;
372 }
373 if (compressor_finished)
374 return E_ABORT;
375 }
376 UINT32 l = min(size, avail_in);
377 memcpy(data, next_in, l);
378 avail_in -= l;
379 size -= l;
380 next_in += l;
381 data = LPBYTE(data) + l;
382 if (processedSize)
383 *processedSize += l;
384 }
385 return S_OK;
386 }
387
388 HRESULT CLZMA::Write(const void *data, UINT32 size, UINT32 *processedSize)
389 {
390 return WritePart(data, size, processedSize);
391 }
392
393 HRESULT CLZMA::WritePart(const void *data, UINT32 size, UINT32 *processedSize)
394 {
395 if (processedSize)
396 *processedSize = 0;
397 while (size)
398 {
399 if (!avail_out)
400 {
401 GetMoreIO();
402 if (!avail_out)
403 return E_ABORT;
404 }
405 UINT32 l = min(size, avail_out);
406 memcpy(next_out, data, l);
407 avail_out -= l;
408 size -= l;
409 next_out += l;
410 data = LPBYTE(data) + l;
411 if (processedSize)
412 *processedSize += l;
413 }
414 return S_OK;
415 }
416
417 void CLZMA::SetNextIn(char *in, unsigned int size)
418 {
419 next_in = (LPBYTE) in;
420 avail_in = size;
421 }
422
423 void CLZMA::SetNextOut(char *out, unsigned int size)
424 {
425 next_out = (LPBYTE) out;
426 avail_out = size;
427 }
428
429 char* CLZMA::GetNextOut()
430 {
431 return (char *) next_out;
432 }
433
434 unsigned int CLZMA::GetAvailIn()
435 {
436 return avail_in;
437 }
438
439 unsigned int CLZMA::GetAvailOut()
440 {
441 return avail_out;
442 }
443
444 const TCHAR* CLZMA::GetName()
445 {
446 return _T("lzma");
447 }
448
449 const TCHAR* CLZMA::GetErrStr(int err)
450 {
451 switch (err)
452 {
453 case LZMA_BAD_CALL:
454 return _T("bad call");
455 case LZMA_INIT_ERROR:
456 return _T("initialization failed");
457 case LZMA_THREAD_ERROR:
458 return _T("thread synchronization error");
459 case LZMA_IO_ERROR:
460 return _T("input/output error");
461 case LZMA_MEM_ERROR:
462 return _T("not enough memory");
463 default:
464 return _T("unknown error");
465 }
466 }