Annotation of elwix/tools/oldlzma/SRC/7zip/Compress/LZMA_C/LzmaTest.c, revision 1.1

1.1     ! misho       1: /* 
        !             2: LzmaTest.c
        !             3: Test application for LZMA Decoder
        !             4: LZMA SDK 4.16 Copyright (c) 1999-2004 Igor Pavlov (2005-03-18)
        !             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.16 Copyright (c) 1999-2005 Igor Pavlov  2005-03-18\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:     if (dictionarySize == 0)
        !           157:       dictionarySize = 1; /* LZMA decoder can not work with dictionarySize = 0 */
        !           158:     dictionary = (unsigned char *)malloc(dictionarySize);
        !           159:     if (dictionary == 0)
        !           160:     {
        !           161:       sprintf(rs + strlen(rs), "\n can't allocate");
        !           162:       return 1;
        !           163:     }
        !           164:     res = LzmaDecoderInit((unsigned char *)lzmaInternalData, lzmaInternalSize,
        !           165:         lc, lp, pb,
        !           166:         dictionary, dictionarySize,
        !           167:         #ifdef _LZMA_IN_CB
        !           168:         &bo.InCallback
        !           169:         #else
        !           170:         (unsigned char *)inStream, compressedSize
        !           171:         #endif
        !           172:         );
        !           173:     if (res == 0)
        !           174:     for (nowPos = 0; nowPos < outSize;)
        !           175:     {
        !           176:       UInt32 blockSize = outSize - nowPos;
        !           177:       UInt32 kBlockSize = 0x10000;
        !           178:       if (blockSize > kBlockSize)
        !           179:         blockSize = kBlockSize;
        !           180:       res = LzmaDecode((unsigned char *)lzmaInternalData, 
        !           181:       ((unsigned char *)outStream) + nowPos, blockSize, &outSizeProcessed);
        !           182:       if (res != 0)
        !           183:         break;
        !           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 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: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>