allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / scripts / squashfs / lzma / C / 7zip / Archive / 7z_C / 7zMain.c
blob6b496da9f39d33931484983283b62b926cc21e01
1 /*
2 7zMain.c
3 Test application for 7z Decoder
4 LZMA SDK 4.26 Copyright (c) 1999-2005 Igor Pavlov (2005-08-02)
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include "7zCrc.h"
12 #include "7zIn.h"
13 #include "7zExtract.h"
15 typedef struct _CFileInStream
17 ISzInStream InStream;
18 FILE *File;
19 } CFileInStream;
21 #ifdef _LZMA_IN_CB
23 #define kBufferSize (1 << 12)
24 Byte g_Buffer[kBufferSize];
26 SZ_RESULT SzFileReadImp(void *object, void **buffer, size_t maxRequiredSize, size_t *processedSize)
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;
39 #else
41 SZ_RESULT SzFileReadImp(void *object, void *buffer, size_t size, size_t *processedSize)
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;
50 #endif
52 SZ_RESULT SzFileSeekImp(void *object, CFileSize pos)
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;
61 void PrintError(char *sz)
63 printf("\nERROR: %s\n", sz);
66 int main(int numargs, char *args[])
68 CFileInStream archiveStream;
69 CArchiveDatabaseEx db;
70 SZ_RESULT res;
71 ISzAlloc allocImp;
72 ISzAlloc allocTempImp;
74 printf("\n7z ANSI-C Decoder 4.30 Copyright (c) 1999-2005 Igor Pavlov 2005-11-20\n");
75 if (numargs == 1)
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;
85 if (numargs < 3)
87 PrintError("incorrect command");
88 return 1;
91 archiveStream.File = fopen(args[2], "rb");
92 if (archiveStream.File == 0)
94 PrintError("can not open input file");
95 return 1;
98 archiveStream.InStream.Read = SzFileReadImp;
99 archiveStream.InStream.Seek = SzFileSeekImp;
101 allocImp.Alloc = SzAlloc;
102 allocImp.Free = SzFree;
104 allocTempImp.Alloc = SzAllocTemp;
105 allocTempImp.Free = SzFreeTemp;
107 InitCrcTable();
108 SzArDbExInit(&db);
109 res = SzArchiveOpen(&archiveStream.InStream, &db, &allocImp, &allocTempImp);
110 if (res == SZ_OK)
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;
123 if (listCommand)
125 UInt32 i;
126 for (i = 0; i < db.Database.NumFiles; i++)
128 CFileItem *f = db.Database.Files + i;
129 printf("%10d %s\n", (int)f->Size, f->Name);
132 else if (testCommand || extractCommand)
134 UInt32 i;
136 // if you need cache, use these 3 variables.
137 // if you use external function, you can make these variable as static.
138 UInt32 blockIndex = 0xFFFFFFFF; // it can have any value before first call (if outBuffer = 0)
139 Byte *outBuffer = 0; // it must be 0 before first call for each new archive.
140 size_t outBufferSize = 0; // it can have any value before first call (if outBuffer = 0)
142 printf("\n");
143 for (i = 0; i < db.Database.NumFiles; i++)
145 size_t offset;
146 size_t outSizeProcessed;
147 CFileItem *f = db.Database.Files + i;
148 if (f->IsDirectory)
149 printf("Directory ");
150 else
151 printf(testCommand ?
152 "Testing ":
153 "Extracting");
154 printf(" %s", f->Name);
155 if (f->IsDirectory)
157 printf("\n");
158 continue;
160 res = SzExtract(&archiveStream.InStream, &db, i,
161 &blockIndex, &outBuffer, &outBufferSize,
162 &offset, &outSizeProcessed,
163 &allocImp, &allocTempImp);
164 if (res != SZ_OK)
165 break;
166 if (!testCommand)
168 FILE *outputHandle;
169 UInt32 processedSize;
170 char *fileName = f->Name;
171 size_t nameLen = strlen(f->Name);
172 for (; nameLen > 0; nameLen--)
173 if (f->Name[nameLen - 1] == '/')
175 fileName = f->Name + nameLen;
176 break;
179 outputHandle = fopen(fileName, "wb+");
180 if (outputHandle == 0)
182 PrintError("can not open output file");
183 res = SZE_FAIL;
184 break;
186 processedSize = fwrite(outBuffer + offset, 1, outSizeProcessed, outputHandle);
187 if (processedSize != outSizeProcessed)
189 PrintError("can not write output file");
190 res = SZE_FAIL;
191 break;
193 if (fclose(outputHandle))
195 PrintError("can not close output file");
196 res = SZE_FAIL;
197 break;
200 printf("\n");
202 allocImp.Free(outBuffer);
204 else
206 PrintError("incorrect command");
207 res = SZE_FAIL;
210 SzArDbExFree(&db, allocImp.Free);
212 fclose(archiveStream.File);
213 if (res == SZ_OK)
215 printf("\nEverything is Ok\n");
216 return 0;
218 if (res == SZE_OUTOFMEMORY)
219 PrintError("can not allocate memory");
220 else
221 printf("\nERROR #%d\n", res);
222 return 1;