Annotation of elwix/tools/oldlzma/SRC/7zip/Archive/7z_C/7zMain.c, revision 1.1

1.1     ! misho       1: /* 
        !             2: 7zMain.c
        !             3: Test application for 7z Decoder
        !             4: LZMA SDK 4.16 Copyright (c) 1999-2005 Igor Pavlov (2005-09-24)
        !             5: */
        !             6: 
        !             7: #include <stdio.h>
        !             8: #include <stdlib.h>
        !             9: #include <string.h>
        !            10: 
        !            11: #include "7zCrc.h"
        !            12: #include "7zIn.h"
        !            13: #include "7zExtract.h"
        !            14: 
        !            15: typedef struct _CFileInStream
        !            16: {
        !            17:   ISzInStream InStream;
        !            18:   FILE *File;
        !            19: } CFileInStream;
        !            20: 
        !            21: #ifdef _LZMA_IN_CB
        !            22: 
        !            23: #define kBufferSize (1 << 12)
        !            24: Byte g_Buffer[kBufferSize];
        !            25: 
        !            26: SZ_RESULT SzFileReadImp(void *object, void **buffer, size_t maxRequiredSize, size_t *processedSize)
        !            27: {
        !            28:   CFileInStream *s = (CFileInStream *)object;
        !            29:   size_t processedSizeLoc;
        !            30:   if (maxRequiredSize > kBufferSize)
        !            31:     maxRequiredSize = kBufferSize;
        !            32:   processedSizeLoc = fread(g_Buffer, 1, maxRequiredSize, s->File);
        !            33:   *buffer = g_Buffer;
        !            34:   if (processedSize != 0)
        !            35:     *processedSize = processedSizeLoc;
        !            36:   return SZ_OK;
        !            37: }
        !            38: 
        !            39: #else
        !            40: 
        !            41: SZ_RESULT SzFileReadImp(void *object, void *buffer, size_t size, size_t *processedSize)
        !            42: {
        !            43:   CFileInStream *s = (CFileInStream *)object;
        !            44:   size_t processedSizeLoc = fread(buffer, 1, size, s->File);
        !            45:   if (processedSize != 0)
        !            46:     *processedSize = processedSizeLoc;
        !            47:   return SZ_OK;
        !            48: }
        !            49: 
        !            50: #endif
        !            51: 
        !            52: SZ_RESULT SzFileSeekImp(void *object, CFileSize pos)
        !            53: {
        !            54:   CFileInStream *s = (CFileInStream *)object;
        !            55:   int res = fseek(s->File, (long)pos, SEEK_SET);
        !            56:   if (res == 0)
        !            57:     return SZ_OK;
        !            58:   return SZE_FAIL;
        !            59: }
        !            60: 
        !            61: void PrintError(char *sz)
        !            62: {
        !            63:   printf("\nERROR: %s\n", sz);
        !            64: }
        !            65: 
        !            66: int main(int numargs, char *args[])
        !            67: {
        !            68:   CFileInStream archiveStream;
        !            69:   CArchiveDatabaseEx db;
        !            70:   SZ_RESULT res;
        !            71:   ISzAlloc allocImp;
        !            72:   ISzAlloc allocTempImp;
        !            73: 
        !            74:   printf("\n7z ANSI-C Decoder 4.16 Copyright (c) 1999-2005 Igor Pavlov  2005-03-29\n");
        !            75:   if (numargs == 1)
        !            76:   {
        !            77:     printf(
        !            78:       "\nUsage: 7zDec <command> <archive_name>\n\n"
        !            79:       "<Commands>\n"
        !            80:       "  e: Extract files from archive\n"
        !            81:       "  l: List contents of archive\n"
        !            82:       "  t: Test integrity of archive\n");
        !            83:     return 0;
        !            84:   }
        !            85:   if (numargs < 3)
        !            86:   {
        !            87:     PrintError("incorrect command");
        !            88:     return 1;
        !            89:   }
        !            90: 
        !            91:   archiveStream.File = fopen(args[2], "rb");
        !            92:   if (archiveStream.File == 0)
        !            93:   {
        !            94:     PrintError("can not open input file");
        !            95:     return 1;
        !            96:   }
        !            97: 
        !            98:   archiveStream.InStream.Read = SzFileReadImp;
        !            99:   archiveStream.InStream.Seek = SzFileSeekImp;
        !           100: 
        !           101:   allocImp.Alloc = SzAlloc;
        !           102:   allocImp.Free = SzFree;
        !           103: 
        !           104:   allocTempImp.Alloc = SzAllocTemp;
        !           105:   allocTempImp.Free = SzFreeTemp;
        !           106: 
        !           107:   InitCrcTable();
        !           108:   SzArDbExInit(&db);
        !           109:   res = SzArchiveOpen(&archiveStream.InStream, &db, &allocImp, &allocTempImp);
        !           110:   if (res == SZ_OK)
        !           111:   {
        !           112:     char *command = args[1];
        !           113:     int listCommand = 0;
        !           114:     int testCommand = 0;
        !           115:     int extractCommand = 0;
        !           116:     if (strcmp(command, "l") == 0)
        !           117:       listCommand = 1;
        !           118:     if (strcmp(command, "t") == 0)
        !           119:       testCommand = 1;
        !           120:     else if (strcmp(command, "e") == 0)
        !           121:       extractCommand = 1;
        !           122: 
        !           123:     if (listCommand)
        !           124:     {
        !           125:       UInt32 i;
        !           126:       for (i = 0; i < db.Database.NumFiles; i++)
        !           127:       {
        !           128:         CFileItem *f = db.Database.Files + i;
        !           129:         printf("%10d  %s\n", (int)f->Size, f->Name);
        !           130:       }
        !           131:     }
        !           132:     else if (testCommand || extractCommand)
        !           133:     {
        !           134:       UInt32 i;
        !           135:       UInt32 blockIndex;
        !           136:       Byte *outBuffer = 0;
        !           137:       size_t outBufferSize;
        !           138:       printf("\n");
        !           139:       for (i = 0; i < db.Database.NumFiles; i++)
        !           140:       {
        !           141:         size_t offset;
        !           142:         size_t outSizeProcessed;
        !           143:         CFileItem *f = db.Database.Files + i;
        !           144:         printf(testCommand ? 
        !           145:             "Tesing    ":
        !           146:             "Extracting");
        !           147:         printf(" %s", f->Name);
        !           148:         res = SzExtract(&archiveStream.InStream, &db, i, 
        !           149:             &blockIndex, &outBuffer, &outBufferSize, 
        !           150:             &offset, &outSizeProcessed, 
        !           151:             &allocImp, &allocTempImp);
        !           152:         if (res != SZ_OK)
        !           153:           break;
        !           154:         if (!testCommand)
        !           155:         {
        !           156:           FILE *outputHandle;
        !           157:           UInt32 processedSize;
        !           158:           char *fileName = f->Name;
        !           159:           size_t nameLen = strlen(f->Name);
        !           160:           for (; nameLen > 0; nameLen--)
        !           161:             if (f->Name[nameLen - 1] == '/')
        !           162:             {
        !           163:               fileName = f->Name + nameLen;
        !           164:               break;
        !           165:             }
        !           166:             
        !           167:           outputHandle = fopen(fileName, "wb+");
        !           168:           if (outputHandle == 0)
        !           169:           {
        !           170:             PrintError("can not open output file");
        !           171:             res = SZE_FAIL;
        !           172:             break;
        !           173:           }
        !           174:           processedSize = fwrite(outBuffer + offset, 1, outSizeProcessed, outputHandle);
        !           175:           if (processedSize != outSizeProcessed)
        !           176:           {
        !           177:             PrintError("can not write output file");
        !           178:             res = SZE_FAIL;
        !           179:             break;
        !           180:           }
        !           181:           if (fclose(outputHandle))
        !           182:           {
        !           183:             PrintError("can not close output file");
        !           184:             res = SZE_FAIL;
        !           185:             break;
        !           186:           }
        !           187:         }
        !           188:         printf("\n");
        !           189:       }
        !           190:       allocImp.Free(outBuffer);
        !           191:     }
        !           192:     else
        !           193:     {
        !           194:       PrintError("incorrect command");
        !           195:       res = SZE_FAIL;
        !           196:     }
        !           197:   }
        !           198:   SzArDbExFree(&db, allocImp.Free);
        !           199: 
        !           200:   fclose(archiveStream.File);
        !           201:   if (res == SZ_OK)
        !           202:   {
        !           203:     printf("\nEverything is Ok\n");
        !           204:     return 0;
        !           205:   }
        !           206:   if (res == SZE_OUTOFMEMORY)
        !           207:     PrintError("can not allocate memory");
        !           208:   else     
        !           209:     printf("\nERROR #%d\n", res);
        !           210:   return 1;
        !           211: }

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