Source

nsis64 / Source / clzma.cpp

  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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
 * clzma.cpp
 * 
 * This file is a part of NSIS.
 * 
 * Copyright (C) 1999-2009 Nullsoft and Contributors
 * 
 * Licensed under the zlib/libpng license (the "License");
 * you may not use this file except in compliance with the License.
 * 
 * Licence details can be found in the file COPYING.
 * 
 * This software is provided 'as-is', without any express or implied
 * warranty.
 *
 * Unicode support by Jim Park -- 08/24/2007
 */

#include <algorithm> // for std::min
#include "clzma.h"

using namespace std;

#ifndef _WIN32
struct evnet_t
{
  pthread_cond_t cond;
  pthread_mutex_t mutex;
  bool signaled;
};

HANDLE CreateEvent(void *, BOOL, BOOL, TCHAR *)
{
  evnet_t *event = (evnet_t *) malloc(sizeof(evnet_t));
  if (!event)
    return 0;
  if (pthread_cond_init(&event->cond, NULL))
  {
    free(event);
    return 0;
  }
  if (pthread_mutex_init(&event->mutex, NULL))
  {
    pthread_cond_destroy(&event->cond);
    free(event);
    return 0;
  }
  event->signaled = false;
  return (HANDLE) event;
}

BOOL SetEvent(HANDLE _event)
{
  evnet_t *event = (evnet_t *) _event;
  if (pthread_mutex_lock(&event->mutex))
    return FALSE;
  event->signaled = true;
  pthread_cond_signal(&event->cond);
  if (pthread_mutex_unlock(&event->mutex))
    return FALSE;
  return TRUE;
}

BOOL ResetEvent(HANDLE _event)
{
  evnet_t *event = (evnet_t *) _event;
  if (pthread_mutex_lock(&event->mutex))
    return FALSE;
  event->signaled = false;
  if (pthread_mutex_unlock(&event->mutex))
    return FALSE;
  return TRUE;
}

BOOL CloseHandle(HANDLE _event)
{
  BOOL ret = TRUE;
  evnet_t *event = (evnet_t *) _event;
  if (!event)
    return FALSE;
  if (pthread_cond_destroy(&event->cond))
    ret = FALSE;
  if (pthread_mutex_destroy(&event->mutex))
    ret = FALSE;
  free(event);
  return ret;
}

#define WAIT_OBJECT_0 0
#define INFINITE 0
DWORD WaitForSingleObject(HANDLE _event, DWORD) {
  DWORD ret = WAIT_OBJECT_0;
  evnet_t *event = (evnet_t *) _event;
  if (pthread_mutex_lock(&event->mutex))
    return !WAIT_OBJECT_0;
  if (!event->signaled)
  {
    if (pthread_cond_wait(&event->cond, &event->mutex))
    {
      ret = !WAIT_OBJECT_0;
    }
  }
  event->signaled = false;
  pthread_mutex_unlock(&event->mutex);
  return ret;
}

#define WaitForMultipleObjects(x, list, y, t) WaitForSingleObject(list[0], t)

#endif

#ifdef _WIN32
DWORD CLZMA::lzmaCompressThread(LPVOID lpParameter)
#else
void* CLZMA::lzmaCompressThread(void *lpParameter)
#endif
{
  CLZMA *Compressor = (CLZMA *) lpParameter;
  if (!Compressor)
    return 0;

  Compressor->CompressReal();
  return 0;
}

int CLZMA::ConvertError(HRESULT result)
{
  if (result != S_OK)
  {
    if (result == E_OUTOFMEMORY)
      return LZMA_MEM_ERROR;
    else
      return LZMA_IO_ERROR;
  }
  return C_OK;
}

CLZMA::CLZMA(): _encoder(NULL)
{
  _encoder = new NCompress::NLZMA::CEncoder();
  _encoder->SetWriteEndMarkerMode(true);
#ifdef _WIN32
  hCompressionThread = NULL;
#else
  hCompressionThread = 0;
#endif
  hNeedIOEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  hIOReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  finish = FALSE;
  compressor_finished = TRUE;
  hCompressionThread = 0;
  SetNextOut(NULL, 0);
  SetNextIn(NULL, 0);

  AddRef(); // will be manually deleted, not released
}

CLZMA::~CLZMA()
{
  End();
  if (hNeedIOEvent)
  {
    CloseHandle(hNeedIOEvent);
    hNeedIOEvent = NULL;
  }
  if (hIOReadyEvent)
  {
    CloseHandle(hIOReadyEvent);
    hIOReadyEvent = NULL;
  }
  if (_encoder)
  {
    delete _encoder;
    _encoder = NULL;
  }
}

int CLZMA::Init(int level, unsigned int dicSize)
{
  End();

  compressor_finished = FALSE;
  finish = FALSE;
  res = C_OK;

  if (!hNeedIOEvent || !hIOReadyEvent)
  {
    return LZMA_INIT_ERROR;
  }

  ResetEvent(hNeedIOEvent);
  ResetEvent(hIOReadyEvent);

  res = C_OK;

  PROPID propdIDs [] =
  {
    NCoderPropID::kAlgorithm,
    NCoderPropID::kDictionarySize,
    NCoderPropID::kNumFastBytes
  };
  const int kNumProps = COUNTOF(propdIDs);
  PROPVARIANT props[kNumProps];
  // NCoderPropID::kAlgorithm
  props[0].vt = VT_UI4;
  props[0].ulVal = 2;
  // NCoderPropID::kDictionarySize
  props[1].vt = VT_UI4;
  props[1].ulVal = dicSize;
  // NCoderPropID::kNumFastBytes
  props[2].vt = VT_UI4;
  props[2].ulVal = 64;
  if (_encoder->SetCoderProperties(propdIDs, props, kNumProps) != 0)
    return LZMA_INIT_ERROR;
  return _encoder->SetStreams(this, this, 0, 0) == S_OK ? C_OK : LZMA_INIT_ERROR;
}

int CLZMA::End()
{
  // has compressor not finished?
  if (hCompressionThread && !compressor_finished)
  {
    // kill compression thread
    avail_in = 0;
    avail_out = 0;
    compressor_finished = TRUE;

    SetEvent(hIOReadyEvent);
#ifdef _WIN32
    WaitForSingleObject(hCompressionThread, INFINITE);
#else
    pthread_join(hCompressionThread, NULL);
#endif
  }
  if (hCompressionThread)
  {
#ifdef _WIN32
    CloseHandle(hCompressionThread);
    hCompressionThread = NULL;
#else
    pthread_detach(hCompressionThread);
    hCompressionThread = 0;
#endif
  }
  SetNextOut(NULL, 0);
  SetNextIn(NULL, 0);
  return C_OK;
}

int CLZMA::CompressReal()
{
  try
  {
    HRESULT hResult = _encoder->WriteCoderProperties(this);
    if (hResult == S_OK)
    {
      while (true)
      {
        UINT64 inSize, outSize;
        INT32 finished;
        hResult = _encoder->CodeOneBlock(&inSize, &outSize, &finished);
        if (hResult != S_OK && res == C_OK)
          res = ConvertError(hResult);
        if (res != C_OK)
          break;
        if (finished)
        {
          res = C_FINISHED;
          break;
        }
      }
    }
    else
    {
      if (res == C_OK)
        res = ConvertError(hResult);
    }
  }
  catch (...)
  {
    if (res == C_OK)
      res = LZMA_IO_ERROR;
  }

  compressor_finished = TRUE;
  SetEvent(hNeedIOEvent);
  return C_OK;
}

int CLZMA::Compress(bool flush)
{
  if (compressor_finished)
  {
    // act like zlib when it comes to stream ending
    if (flush)
      return C_OK;
    else
      return LZMA_BAD_CALL;
  }

  finish = flush;

  if (!hCompressionThread)
  {
#ifdef _WIN32
    DWORD dwThreadId;

    hCompressionThread = CreateThread(0, 0, lzmaCompressThread, (LPVOID) this, 0, &dwThreadId);
    if (!hCompressionThread)
#else
    if (pthread_create(&hCompressionThread, NULL, lzmaCompressThread, (LPVOID) this))
#endif
      return LZMA_INIT_ERROR;
  }
  else
  {
    SetEvent(hIOReadyEvent);
  }

  HANDLE waitList[2] = {hNeedIOEvent, (HANDLE) hCompressionThread};
  if (WaitForMultipleObjects(2, waitList, FALSE, INFINITE) != WAIT_OBJECT_0)
  {
    // thread ended or WaitForMultipleObjects failed
    compressor_finished = TRUE;
    SetEvent(hIOReadyEvent);
    return LZMA_THREAD_ERROR;
  }

  if (compressor_finished)
  {
    return res;
  }

  return C_OK;
}

void CLZMA::GetMoreIO()
{
  SetEvent(hNeedIOEvent);
  if (WaitForSingleObject(hIOReadyEvent, INFINITE) != WAIT_OBJECT_0)
  {
    compressor_finished = TRUE;
    res = LZMA_THREAD_ERROR;
  }
}

HRESULT CLZMA::Read(void *data, UINT32 size, UINT32 *processedSize)
{
  return ReadPart(data, size, processedSize);
}

HRESULT CLZMA::ReadPart(void *data, UINT32 size, UINT32 *processedSize)
{
  if (processedSize)
    *processedSize = 0;
  while (size)
  {
    if (!avail_in)
    {
      if (finish)
      {
        return S_OK;
      }
      GetMoreIO();
      if (!avail_in)
      {
        if (finish)
        {
          return S_OK;
        }
        return E_ABORT;
      }
      if (compressor_finished)
        return E_ABORT;
    }
    UINT32 l = min(size, avail_in);
    memcpy(data, next_in, l);
    avail_in -= l;
    size -= l;
    next_in += l;
    data = LPBYTE(data) + l;
    if (processedSize)
      *processedSize += l;
  }
  return S_OK;
}

HRESULT CLZMA::Write(const void *data, UINT32 size, UINT32 *processedSize)
{
  return WritePart(data, size, processedSize);
}

HRESULT CLZMA::WritePart(const void *data, UINT32 size, UINT32 *processedSize)
{
  if (processedSize)
    *processedSize = 0;
  while (size)
  {
    if (!avail_out)
    {
      GetMoreIO();
      if (!avail_out)
        return E_ABORT;
    }
    UINT32 l = min(size, avail_out);
    memcpy(next_out, data, l);
    avail_out -= l;
    size -= l;
    next_out += l;
    data = LPBYTE(data) + l;
    if (processedSize)
      *processedSize += l;
  }
  return S_OK;
}

void CLZMA::SetNextIn(char *in, unsigned int size)
{
  next_in = (LPBYTE) in;
  avail_in = size;
}

void CLZMA::SetNextOut(char *out, unsigned int size)
{
  next_out = (LPBYTE) out;
  avail_out = size;
}

char* CLZMA::GetNextOut()
{
  return (char *) next_out;
}

unsigned int CLZMA::GetAvailIn()
{
  return avail_in;
}

unsigned int CLZMA::GetAvailOut()
{
  return avail_out;
}

const TCHAR* CLZMA::GetName()
{
  return _T("lzma");
}

const TCHAR* CLZMA::GetErrStr(int err)
{
  switch (err)
  {
  case LZMA_BAD_CALL:
    return _T("bad call");
  case LZMA_INIT_ERROR:
    return _T("initialization failed");
  case LZMA_THREAD_ERROR:
    return _T("thread synchronization error");
  case LZMA_IO_ERROR:
    return _T("input/output error");
  case LZMA_MEM_ERROR:
    return _T("not enough memory");
  default:
    return _T("unknown error");
  }
}