Debian Codegraph @

utils/lzma/SRC/7zip/Archive/7z_C/7zMain.c

raw
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 */
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.26 Copyright (c) 1999-2005 Igor Pavlov 2005-08-02\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
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)
141
142 printf("\n");
143 for (i = 0; i < db.Database.NumFiles; i++)
144 {
145 size_t offset;
146 size_t outSizeProcessed;
147 CFileItem *f = db.Database.Files + i;
148 printf(testCommand ?
149 "Testing ":
150 "Extracting");
151 printf(" %s", f->Name);
152 res = SzExtract(&archiveStream.InStream, &db, i,
153 &blockIndex, &outBuffer, &outBufferSize,
154 &offset, &outSizeProcessed,
155 &allocImp, &allocTempImp);
156 if (res != SZ_OK)
157 break;
158 if (!testCommand)
159 {
160 FILE *outputHandle;
161 UInt32 processedSize;
162 char *fileName = f->Name;
163 size_t nameLen = strlen(f->Name);
164 for (; nameLen > 0; nameLen--)
165 if (f->Name[nameLen - 1] == '/')
166 {
167 fileName = f->Name + nameLen;
168 break;
169 }
170
171 outputHandle = fopen(fileName, "wb+");
172 if (outputHandle == 0)
173 {
174 PrintError("can not open output file");
175 res = SZE_FAIL;
176 break;
177 }
178 processedSize = fwrite(outBuffer + offset, 1, outSizeProcessed, outputHandle);
179 if (processedSize != outSizeProcessed)
180 {
181 PrintError("can not write output file");
182 res = SZE_FAIL;
183 break;
184 }
185 if (fclose(outputHandle))
186 {
187 PrintError("can not close output file");
188 res = SZE_FAIL;
189 break;
190 }
191 }
192 printf("\n");
193 }
194 allocImp.Free(outBuffer);
195 }
196 else
197 {
198 PrintError("incorrect command");
199 res = SZE_FAIL;
200 }
201 }
202 SzArDbExFree(&db, allocImp.Free);
203
204 fclose(archiveStream.File);
205 if (res == SZ_OK)
206 {
207 printf("\nEverything is Ok\n");
208 return 0;
209 }
210 if (res == SZE_OUTOFMEMORY)
211 PrintError("can not allocate memory");
212 else
213 printf("\nERROR #%d\n", res);
214 return 1;
215 }