home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 March / CMCD0304.ISO / Software / Freeware / Programare / nullsoft / nsis20.exe / Source / 7zip / lzmaNSIS.cpp < prev    next >
C/C++ Source or Header  |  2004-01-05  |  4KB  |  155 lines

  1. // lzmaNSIS.h
  2.  
  3. #include <windows.h>
  4.  
  5. #include "lzmaNSIS.h"
  6. #include "7zip/Compress/LZMA_SMALL/LZMAConf.h"
  7. #include "7zip/Compress/LZMA_SMALL/LZMADecoder.h"
  8.  
  9. void __stdcall LZMAGetIO(CLZMAStateP lzmaState)
  10. {
  11.   LeaveCriticalSection(&lzmaState->cs);
  12.   while (!lzmaState->it_locked)
  13.     Sleep(0);
  14.   
  15.   lzmaState->dt_locked = FALSE;
  16.  
  17.   EnterCriticalSection(&lzmaState->cs);
  18.   lzmaState->dt_locked = TRUE;
  19.  
  20.   while (lzmaState->it_locked)
  21.     Sleep(0);
  22. }
  23.  
  24. extern "C"
  25. {
  26.  
  27. void __stdcall lzmaInit(CLZMAStateP lzmaState)
  28. {
  29.   if (lzmaState->hThread)
  30.   {
  31.     CloseHandle(lzmaState->hThread);
  32.     lzmaState->hThread = NULL;
  33.   }
  34.  
  35.   if (!lzmaState->lzmaDecoder)
  36.     lzmaState->lzmaDecoder = LZMAAlloc(sizeof(CLZMADecoder));
  37.  
  38.   if (!lzmaState->cs_initialized)
  39.   {
  40.     InitializeCriticalSection(&lzmaState->cs);
  41.     lzmaState->cs_initialized = TRUE;
  42.   }
  43.  
  44.   lzmaState->it_locked = TRUE;
  45.   lzmaState->dt_locked = FALSE;
  46.  
  47.   lzmaState->finished = FALSE;
  48. }
  49.  
  50. DWORD WINAPI lzmaDecompressThread(LPVOID lpParameter)
  51. {
  52.   CLZMAStateP lzmaState = (CLZMAStateP) lpParameter;
  53.   CLZMADecoder *lzmaDecodeder = (CLZMADecoder *) lzmaState->lzmaDecoder;
  54.  
  55.   EnterCriticalSection(&lzmaState->cs);
  56.   lzmaState->dt_locked = TRUE;
  57.  
  58.   while (lzmaState->it_locked)
  59.     Sleep(0);
  60.  
  61.   {
  62.     const kPropertiesSize = 5;
  63.     lzmaState->res = -4;
  64.     if (lzmaState->avail_in < kPropertiesSize)
  65.     {
  66.       goto finished;
  67.     }
  68.     LPBYTE properties = lzmaState->next_in;
  69.     lzmaState->avail_in -= kPropertiesSize;
  70.     lzmaState->next_in += kPropertiesSize;
  71.  
  72.     BYTE firstByte = properties[0];
  73.  
  74.     if (firstByte > (9*5*5))
  75.     {
  76.       goto finished;
  77.     }
  78.  
  79.     int numPosStateBits = firstByte / (9*5);
  80.     firstByte %= (9*5);
  81.     int numLiteralPosStateBits = firstByte / 9;
  82.     firstByte %= 9;
  83.     int numLiteralContextBits = firstByte;
  84.     
  85.     int memSize = (1 << (numLiteralContextBits + numLiteralPosStateBits)) * sizeof(CLZMALiteralDecoder2);
  86.  
  87.     if (lzmaState->DynamicData == 0 || firstByte != lzmaState->FirstProp)
  88.     {
  89.       if (lzmaState->DynamicData != 0)
  90.         LZMAFree(lzmaState->DynamicData);
  91.       lzmaState->DynamicData = LZMAAlloc(memSize);
  92.       lzmaState->FirstProp = firstByte;
  93.     }
  94.  
  95.     lzmaDecodeder->Create((LPBYTE) lzmaState->DynamicData, 
  96.       numLiteralContextBits, numLiteralPosStateBits, numPosStateBits);
  97.  
  98.     UINT32 dictionarySize = *(UINT32 *)(properties + 1);
  99.     if (dictionarySize != lzmaState->DictionarySize)
  100.     {
  101.       if (lzmaState->Dictionary)
  102.         LZMAFree(lzmaState->Dictionary);
  103.       lzmaState->Dictionary = LZMAAlloc(dictionarySize);
  104.       lzmaState->DictionarySize = dictionarySize;
  105.     }
  106.  
  107.     UINT32 res = lzmaDecodeder->Code(lzmaState);
  108.     
  109.     lzmaState->res = 1;
  110.     if (res != 0)
  111.       lzmaState->res = res;
  112.   }
  113.  
  114. finished:
  115.  
  116.   lzmaState->finished = TRUE;
  117.  
  118.   LeaveCriticalSection(&lzmaState->cs);
  119.   lzmaState->dt_locked = FALSE;
  120.   return 0;
  121. }
  122.  
  123. int __stdcall lzmaDecompress(CLZMAStateP lzmaState)
  124. {
  125.   if (!lzmaState->hThread)
  126.   {
  127.     DWORD dwThreadId;
  128.     lzmaState->hThread = CreateThread(0, 0, lzmaDecompressThread, (LPVOID) lzmaState, 0, &dwThreadId);
  129.     if (!lzmaState->hThread)
  130.       return -4;
  131.   }
  132.   else
  133.     LeaveCriticalSection(&lzmaState->cs);
  134.  
  135.   while (!lzmaState->dt_locked)
  136.     Sleep(0);
  137.  
  138.   lzmaState->it_locked = FALSE;
  139.  
  140.   EnterCriticalSection(&lzmaState->cs);
  141.   lzmaState->it_locked = TRUE;
  142.  
  143.   while (lzmaState->dt_locked)
  144.     Sleep(0);
  145.  
  146.   if (lzmaState->finished)
  147.   {
  148.     LeaveCriticalSection(&lzmaState->cs);
  149.     return lzmaState->res;
  150.   }
  151.  
  152.   return 0;
  153. }
  154.  
  155. } // extern "C"