home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 October / CMCD1004.ISO / Software / Shareware / Utilitare / pec / pec2setup.exe / lzma / sdk / LzmaTest.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-04-25  |  5.7 KB  |  248 lines

  1. /* 
  2. LzmaTest.c
  3. Test application for LZMA Decoder
  4. LZMA SDK 4.01 Copyright (c) 1999-2004 Igor Pavlov (2004-02-15)
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #include "LzmaDecode.h"
  12.  
  13. size_t MyReadFile(FILE *file, void *data, size_t size)
  14. {
  15.   return (fread(data, 1, size, file) == size);
  16. }
  17.  
  18. #ifdef _LZMA_IN_CB
  19. typedef struct _CBuffer
  20. {
  21.   ILzmaInCallback InCallback;
  22.   unsigned char *Buffer;
  23.   unsigned int Size;
  24. } CBuffer;
  25.  
  26. int LzmaReadCompressed(void *object, unsigned char **buffer, unsigned int *size)
  27. {
  28.   CBuffer *bo = (CBuffer *)object;
  29.   *size = bo->Size; /* You can specify any available size here */
  30.   *buffer = bo->Buffer;
  31.   bo->Buffer += *size; 
  32.   bo->Size -= *size;
  33.   return LZMA_RESULT_OK;
  34. }
  35. #endif
  36.  
  37. int main2(int numargs, const char *args[], char *rs)
  38. {
  39.   FILE *inputHandle, *outputHandle;
  40.   unsigned int length, processedSize;
  41.   unsigned int compressedSize, outSize, outSizeProcessed, lzmaInternalSize;
  42.   void *inStream, *outStream, *lzmaInternalData;
  43.   unsigned char properties[5];
  44.   unsigned char prop0;
  45.   int ii;
  46.   int lc, lp, pb;
  47.   int res;
  48.   #ifdef _LZMA_IN_CB
  49.   CBuffer bo;
  50.   #endif
  51.  
  52.   sprintf(rs + strlen(rs), "\nLZMA Decoder 4.01 Copyright (c) 2001-2003 Igor Pavlov  2004-02-15\n");
  53.   if (numargs < 2 || numargs > 3)
  54.   {
  55.     sprintf(rs + strlen(rs), "\nUsage:  lzmaDec file.lzma [outFile]\n");
  56.     return 1;
  57.   }
  58.  
  59.   inputHandle = fopen(args[1], "rb");
  60.   if (inputHandle == 0)
  61.   {
  62.     sprintf(rs + strlen(rs), "\n Open input file error");
  63.     return 1;
  64.   }
  65.  
  66.   fseek(inputHandle, 0, SEEK_END);
  67.   length = ftell(inputHandle);
  68.   fseek(inputHandle, 0, SEEK_SET);
  69.  
  70.   if (!MyReadFile(inputHandle, properties, sizeof(properties)))
  71.     return 1;
  72.   
  73.   outSize = 0;
  74.   for (ii = 0; ii < 4; ii++)
  75.   {
  76.     unsigned char b;
  77.     if (!MyReadFile(inputHandle, &b, sizeof(b)))
  78.       return 1;
  79.     outSize += (unsigned int)(b) << (ii * 8);
  80.   }
  81.  
  82.   if (outSize == 0xFFFFFFFF)
  83.   {
  84.     sprintf(rs + strlen(rs), "\nstream version is not supported");
  85.     return 1;
  86.   }
  87.  
  88.   for (ii = 0; ii < 4; ii++)
  89.   {
  90.     unsigned char b;
  91.     if (!MyReadFile(inputHandle, &b, sizeof(b)))
  92.       return 1;
  93.     if (b != 0)
  94.     {
  95.       sprintf(rs + strlen(rs), "\n too long file");
  96.       return 1;
  97.     }
  98.   }
  99.  
  100.   compressedSize = length - 13;
  101.   inStream = malloc(compressedSize);
  102.   if (inStream == 0)
  103.   {
  104.     sprintf(rs + strlen(rs), "\n can't allocate");
  105.     return 1;
  106.   }
  107.   if (!MyReadFile(inputHandle, inStream, compressedSize))
  108.   {
  109.     sprintf(rs + strlen(rs), "\n can't read");
  110.     return 1;
  111.   }
  112.  
  113.   fclose(inputHandle);
  114.  
  115.   prop0 = properties[0];
  116.   if (prop0 >= (9*5*5))
  117.   {
  118.     sprintf(rs + strlen(rs), "\n Properties error");
  119.     return 1;
  120.   }
  121.   for (pb = 0; prop0 >= (9 * 5); 
  122.     pb++, prop0 -= (9 * 5));
  123.   for (lp = 0; prop0 >= 9; 
  124.     lp++, prop0 -= 9);
  125.   lc = prop0;
  126.  
  127.   lzmaInternalSize = 
  128.     (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb);
  129.  
  130.   #ifdef _LZMA_OUT_READ
  131.   lzmaInternalSize += 100;
  132.   #endif
  133.  
  134.   outStream = malloc(outSize);
  135.   lzmaInternalData = malloc(lzmaInternalSize);
  136.   if (outStream == 0 || lzmaInternalData == 0)
  137.   {
  138.     sprintf(rs + strlen(rs), "\n can't allocate");
  139.     return 1;
  140.   }
  141.  
  142.   #ifdef _LZMA_IN_CB
  143.   bo.InCallback.Read = LzmaReadCompressed;
  144.   bo.Buffer = (unsigned char *)inStream;
  145.   bo.Size = compressedSize;
  146.   #endif
  147.  
  148.   #ifdef _LZMA_OUT_READ
  149.   {
  150.     UInt32 nowPos;
  151.     unsigned char *dictionary;
  152.     UInt32 dictionarySize = 0;
  153.     int i;
  154.     for (i = 0; i < 4; i++)
  155.       dictionarySize += (UInt32)(properties[1 + i]) << (i * 8);
  156.     dictionary = malloc(dictionarySize);
  157.     if (dictionary == 0)
  158.     {
  159.       sprintf(rs + strlen(rs), "\n can't allocate");
  160.       return 1;
  161.     }
  162.     LzmaDecoderInit((unsigned char *)lzmaInternalData, lzmaInternalSize,
  163.         lc, lp, pb,
  164.         dictionary, dictionarySize,
  165.         #ifdef _LZMA_IN_CB
  166.         &bo.InCallback
  167.         #else
  168.         (unsigned char *)inStream, compressedSize
  169.         #endif
  170.         );
  171.     for (nowPos = 0; nowPos < outSize;)
  172.     {
  173.       UInt32 blockSize = outSize - nowPos;
  174.       UInt32 kBlockSize = 0x10000;
  175.       if (blockSize > kBlockSize)
  176.         blockSize = kBlockSize;
  177.       res = LzmaDecode((unsigned char *)lzmaInternalData, 
  178.       ((unsigned char *)outStream) + nowPos, blockSize, &outSizeProcessed);
  179.       if (res != 0)
  180.       {
  181.         sprintf(rs + strlen(rs), "\nerror = %d\n", res);
  182.         return 1;
  183.       }
  184.       if (outSizeProcessed == 0)
  185.       {
  186.         outSize = nowPos;
  187.         break;
  188.       }
  189.       nowPos += outSizeProcessed;
  190.     }
  191.     free(dictionary);
  192.   }
  193.  
  194.   #else
  195.   res = LzmaDecode((unsigned char *)lzmaInternalData, lzmaInternalSize,
  196.       lc, lp, pb,
  197.       #ifdef _LZMA_IN_CB
  198.       &bo.InCallback,
  199.       #else
  200.       (unsigned char *)inStream, compressedSize,
  201.       #endif
  202.       (unsigned char *)outStream, outSize, &outSizeProcessed);
  203.   outSize = outSizeProcessed;
  204.   #endif
  205.  
  206.   if (res != 0)
  207.   {
  208.     sprintf(rs + strlen(rs), "\nerror = %d\n", res);
  209.     return 1;
  210.   }
  211.  
  212.   if (numargs > 2)
  213.   {
  214.     outputHandle = fopen(args[2], "wb+");
  215.     if (outputHandle == 0)
  216.     {
  217.       sprintf(rs + strlen(rs), "\n Open output file error");
  218.       return 1;
  219.     }
  220.     processedSize = fwrite(outStream, 1, outSize, outputHandle);
  221.     if (processedSize != outSize)
  222.     {
  223.       sprintf(rs + strlen(rs), "\n can't write");
  224.       return 1;
  225.     }
  226.     fclose(outputHandle);
  227.   }
  228.   free(lzmaInternalData);
  229.   free(outStream);
  230.   free(inStream);
  231.   return 0;
  232. }
  233.  
  234. int __cdecl main(int numargs, const char *args[])
  235. {
  236.   char sz[800] = { 0 };
  237.   int code = main2(numargs, args, sz);
  238.   printf(sz);
  239.   return code;
  240. }
  241.  
  242. /*
  243. int __cdecl main()
  244. {
  245.   return LzmaDecode(0, 0, 0, 0, 0, 0, 0, 0);
  246. }
  247. */
  248.