/
7zMain.c
359 lines (301 loc) · 8.71 KB
/
7zMain.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
7zMain.c
Test application for 7z Decoder
LZMA SDK 4.43 Copyright (c) 1999-2006 Igor Pavlov (2006-06-04)
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <proto/exec.h>
#define __NOLIBBASE__
#include <proto/dos.h>
#include <proto/intuition.h>
#include "7zCrc.h"
#include "7zIn.h"
#include "7zExtract.h"
#include "7zMain.h"
#include "7zUtils.h"
#include "7zUI.h"
struct Library * DOSBase = NULL, * IntuitionBase = NULL;
struct GlobalData * G = NULL;
GLOBAL APTR _WBenchMsg;
typedef struct _CFileInStream
{
ISzInStream InStream;
BPTR File;
} CFileInStream;
/****************************************************************************/
#define TEMPLATE "ARCHIVE/A,TO/K,LIST=L/S,TEST=T/S,EXTRACT=E/S"
#define ARCHIVE_ARG ((STRPTR)arg[0])
#define TOFOLDER ((STRPTR)arg[1])
#define LIST_ARG arg[2]
#define TEST_ARG arg[3]
#define EXTRACT_ARG arg[4]
int main( VOID )
{
LONG arg[5] = {0};
struct RDArgs *args = NULL;
int rc = 10;
if((G = (struct GlobalData*)AllocMem(sizeof(struct GlobalData),MEMF_PUBLIC|MEMF_CLEAR)))
{
/* Initialize 'GlobalData' */
G->WbStartup = ((_WBenchMsg != NULL) ? TRUE : FALSE);
if((IntuitionBase = OpenLibrary("intuition.library", 0)))
{
if((DOSBase = OpenLibrary("dos.library", 0)))
{
args = ReadArgs( TEMPLATE, (long*)arg, NULL);
if(!args)
{
if((rc = _7zUI ( )) != 0 )
{
/* failed creating MUI App */
if( G->WbStartup )
DisplayBeep(NULL);
PrintFault(IoErr(),0L);
}
}
else
{
int mode = 1;
BPTR WhereToExtract = 0,
OldCurrentdir = 0;
G->CliUsage = TRUE;
if( TOFOLDER )
{
MakeDir( TOFOLDER );
mode=SwitchCurrentdir(TOFOLDER,
&WhereToExtract,
&OldCurrentdir );
}
if( mode )
{
mode = LIST_ARG ? 1 : TEST_ARG ? 2:3;
rc = _7zArchive( ARCHIVE_ARG, mode );
if( TOFOLDER )
SwitchCurrentdir(NULL,
&WhereToExtract,
&OldCurrentdir);
}
FreeArgs( args );
}
CloseLibrary( DOSBase );
}
CloseLibrary( IntuitionBase );
}
FreeMem( G, sizeof(struct GlobalData));
}
return rc;
}
void __main(void){}
/****************************************************************************/
/****************************************************************************/
#ifdef _LZMA_IN_CB
#define kBufferSize (1 << 12)
Byte g_Buffer[kBufferSize];
SZ_RESULT SzFileReadImp(void *object, void **buffer, size_t maxRequiredSize, size_t *processedSize)
{
CFileInStream *s = (CFileInStream *)object;
size_t processedSizeLoc;
if (maxRequiredSize > kBufferSize)
maxRequiredSize = kBufferSize;
processedSizeLoc = Read(s->File, g_Buffer, maxRequiredSize );
*buffer = g_Buffer;
if (processedSize != 0)
*processedSize = processedSizeLoc;
return SZ_OK;
}
#else
SZ_RESULT SzFileReadImp(void *object, void *buffer, size_t size, size_t *processedSize)
{
CFileInStream *s = (CFileInStream *)object;
size_t processedSizeLoc = Read(s->File, buffer, size );
if (processedSize != 0)
*processedSize = processedSizeLoc;
return SZ_OK;
}
#endif
/****************************************************************************/
SZ_RESULT SzFileSeekImp(void *object, CFileSize pos)
{
CFileInStream *s = (CFileInStream *)object;
LONG res = Seek(s->File, (long)pos, OFFSET_BEGINING);
if (res != -1)
return SZ_OK;
return SZE_FAIL;
}
/****************************************************************************/
STATIC STRPTR SzErrorString( SZ_RESULT errno )
{
switch( errno )
{
case SZE_DATA_ERROR:return "Data Error";
case SZE_OUTOFMEMORY:return "Cannot allocate memory";
case SZE_CRC_ERROR:return "CRC Error";
case SZE_NOTIMPL:return "unimplemented decoder instruction";
case SZE_FAIL:return "failed assertion";
case SZE_ARCHIVE_ERROR:return "Archive Error";
default:break;
}
return "unknown error";
}
/****************************************************************************/
/**
* ::7zArchive read and process a .7z archive
*
* @filename: 7z archive to load
* @mode: 1 = LIST, 2 = TEST, 3 = EXTRACT
*/
int _7zArchive( STRPTR filename, int mode )
{
CFileInStream archiveStream;
CArchiveDatabaseEx db;
SZ_RESULT res;
ISzAlloc allocImp;
ISzAlloc allocTempImp;
archiveStream.File = Open( filename, MODE_OLDFILE);
if (!archiveStream.File)
{
PrintError("Unable to open archive!");
goto done;
}
archiveStream.InStream.zRead = SzFileReadImp;
archiveStream.InStream.zSeek = SzFileSeekImp;
allocImp.Alloc = SzAlloc;
allocImp.Free = SzFree;
allocTempImp.Alloc = SzAllocTemp;
allocTempImp.Free = SzFreeTemp;
InitCrcTable();
SzArDbExInit(&db);
res = SzArchiveOpen(&archiveStream.InStream, &db, &allocImp, &allocTempImp);
if (res == SZ_OK)
{
if( G->CliUsage )
{
ShowProgramInfo ( );
Printf("\n%s archive %s\n",
(long)((mode == 1) ? "Listing":((mode == 2) ? "Testing":"Extracting")),
(long) FilePart( filename ));
}
if ( mode == 1 ) // LIST mode
{
UInt32 i;
if( G->CliUsage )
{
Printf("\n%7s%9s%14s%7s%8s\n",(long)"Date",(long)"Time",(long)"Size",(long)"CRC",(long)"Name");
for( i = 0; i < 60 ; i++ )
PutStr("-");
PutStr("\n");
}
for (i = 0; i < db.Database.NumFiles; i++)
{
CFileItem *f = db.Database.Files + i;
if( G->CliUsage )
{
UBYTE date[20];
ItemTime( f, date, sizeof(date), FALSE );
Printf("%s%11ld %08lx %s\n", (long)date, (long)f->Size,
f->IsFileCRCDefined ? f->FileCRC:0, (long)f->Name);
}
else
NListInsert((APTR) f );
}
}
else
{
UInt32 i;
// if you need cache, use these 3 variables.
// if you use external function, you can make these variable as static.
UInt32 blockIndex = 0xFFFFFFFF; // it can have any value before first call (if outBuffer = 0)
Byte *outBuffer = 0; // it must be 0 before first call for each new archive.
size_t outBufferSize = 0; // it can have any value before first call (if outBuffer = 0)
if( G->CliUsage )
PutStr("\n");
for (i = 0; i < db.Database.NumFiles; i++)
{
size_t offset;
size_t outSizeProcessed;
CFileItem *f = db.Database.Files + i;
if( G->CliUsage )
{
#if 0
if (f->IsDirectory)
PutStr("Directory");
else
Printf("%12s",(long)((mode == 2) ? "Testing":"Extracting"));
Printf(" %s", (long)f->Name);
#else
if (!f->IsDirectory)
Printf("%12s %s",(long)((mode == 2) ? "Testing":"Extracting"), (long)f->Name);
#endif
}
else
{
STATIC UBYTE msg[256];
SNPrintf( msg, sizeof(msg)-1, "%12s %s",
(mode == 2) ? "Testing":"Extracting", f->Name );
GaugeUpdate( msg, i*100/db.Database.NumFiles );
}
if (f->IsDirectory)
{
// if( G->CliUsage )
// PutStr("\n");
continue;
}
res = SzExtract(&archiveStream.InStream, &db, i,
&blockIndex, &outBuffer, &outBufferSize,
&offset, &outSizeProcessed,
&allocImp, &allocTempImp);
if (res != SZ_OK)
break;
if ( mode == 3 ) // EXTRACT mode
{
BPTR outputHandle;
UInt32 processedSize;
MakeDir( f->Name ); // creates ALL Directories pointing to this file
outputHandle = Open( f->Name, MODE_NEWFILE );
if (outputHandle == 0)
{
PrintError("Unable to open output file!");
res = SZE_FAIL;
break;
}
processedSize = Write(outputHandle, outBuffer + offset, outSizeProcessed);
if (processedSize != outSizeProcessed)
{
PrintError("Cannot write to output file!");
res = SZE_FAIL;
break;
}
Close(outputHandle);
SetFileTimeToFile( f );
}
if( G->CliUsage )
PutStr("\n");
}
allocImp.Free(outBuffer);
}
}
SzArDbExFree(&db, allocImp.Free);
Close(archiveStream.File);
if (res == SZ_OK)
{
if( G->CliUsage )
Printf("\n%s\n", (long)"Everything is Ok");
else
GaugeUpdate("Everything is Ok", 0 );
return 0;
}
#if 0
if (res == SZE_OUTOFMEMORY)
PrintError("can not allocate memory");
else if( G->CliUsage )
Printf("\nERROR #%ld\n", res);
#else
PrintError(SzErrorString( res ));
#endif
if( ! G->CliUsage )
GaugeUpdate("error %ld procesing archive", res );
done:
return 1;
}