1 /* 7zMain.c - Test application for 7z Decoder
\r
2 2010-10-28 : Igor Pavlov : Public domain */
\r
11 #include "7zVersion.h"
\r
13 #ifndef USE_WINDOWS_FILE
\r
18 #include <sys/stat.h>
\r
23 static ISzAlloc g_Alloc = { SzAlloc, SzFree };
\r
25 static int Buf_EnsureSize(CBuf *dest, size_t size)
\r
27 if (dest->size >= size)
\r
29 Buf_Free(dest, &g_Alloc);
\r
30 return Buf_Create(dest, size, &g_Alloc);
\r
35 static Byte kUtf8Limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
\r
37 static Bool Utf16_To_Utf8(Byte *dest, size_t *destLen, const UInt16 *src, size_t srcLen)
\r
39 size_t destPos = 0, srcPos = 0;
\r
44 if (srcPos == srcLen)
\r
49 value = src[srcPos++];
\r
53 dest[destPos] = (char)value;
\r
57 if (value >= 0xD800 && value < 0xE000)
\r
60 if (value >= 0xDC00 || srcPos == srcLen)
\r
63 if (c2 < 0xDC00 || c2 >= 0xE000)
\r
65 value = (((value - 0xD800) << 10) | (c2 - 0xDC00)) + 0x10000;
\r
67 for (numAdds = 1; numAdds < 5; numAdds++)
\r
68 if (value < (((UInt32)1) << (numAdds * 5 + 6)))
\r
71 dest[destPos] = (char)(kUtf8Limits[numAdds - 1] + (value >> (6 * numAdds)));
\r
77 dest[destPos] = (char)(0x80 + ((value >> (6 * numAdds)) & 0x3F));
\r
80 while (numAdds != 0);
\r
86 static SRes Utf16_To_Utf8Buf(CBuf *dest, const UInt16 *src, size_t srcLen)
\r
90 Utf16_To_Utf8(NULL, &destLen, src, srcLen);
\r
92 if (!Buf_EnsureSize(dest, destLen))
\r
93 return SZ_ERROR_MEM;
\r
94 res = Utf16_To_Utf8(dest->data, &destLen, src, srcLen);
\r
95 dest->data[destLen] = 0;
\r
96 return res ? SZ_OK : SZ_ERROR_FAIL;
\r
100 static SRes Utf16_To_Char(CBuf *buf, const UInt16 *s, int fileMode)
\r
103 for (len = 0; s[len] != '\0'; len++);
\r
107 int size = len * 3 + 100;
\r
108 if (!Buf_EnsureSize(buf, size))
\r
109 return SZ_ERROR_MEM;
\r
111 char defaultChar = '_';
\r
113 int numChars = WideCharToMultiByte(fileMode ?
\r
118 AreFileApisANSI() ? CP_ACP : CP_OEMCP
\r
121 0, s, len, (char *)buf->data, size, &defaultChar, &defUsed);
\r
122 if (numChars == 0 || numChars >= size)
\r
123 return SZ_ERROR_FAIL;
\r
124 buf->data[numChars] = 0;
\r
129 fileMode = fileMode;
\r
130 return Utf16_To_Utf8Buf(buf, s, len);
\r
134 static WRes MyCreateDir(const UInt16 *name)
\r
136 #ifdef USE_WINDOWS_FILE
\r
138 return CreateDirectoryW(name, NULL) ? 0 : GetLastError();
\r
145 RINOK(Utf16_To_Char(&buf, name, 1));
\r
149 _mkdir((const char *)buf.data)
\r
151 mkdir((const char *)buf.data, 0755)
\r
154 Buf_Free(&buf, &g_Alloc);
\r
160 static WRes OutFile_OpenUtf16(CSzFile *p, const UInt16 *name)
\r
162 #ifdef USE_WINDOWS_FILE
\r
163 return OutFile_OpenW(p, name);
\r
168 RINOK(Utf16_To_Char(&buf, name, 1));
\r
169 res = OutFile_Open(p, (const char *)buf.data);
\r
170 Buf_Free(&buf, &g_Alloc);
\r
175 static SRes PrintString(const UInt16 *s)
\r
180 res = Utf16_To_Char(&buf, s, 0);
\r
182 printf("[%s]\n",(const char *)buf.data);
\r
183 Buf_Free(&buf, &g_Alloc);
\r
187 static void UInt64ToStr(UInt64 value, char *s)
\r
193 temp[pos++] = (char)('0' + (unsigned)(value % 10));
\r
196 while (value != 0);
\r
198 *s++ = temp[--pos];
\r
203 static char *UIntToStr(char *s, unsigned value, int numDigits)
\r
208 temp[pos++] = (char)('0' + (value % 10));
\r
209 while (value /= 10);
\r
210 for (numDigits -= pos; numDigits > 0; numDigits--)
\r
213 *s++ = temp[--pos];
\r
219 #define PERIOD_4 (4 * 365 + 1)
\r
220 #define PERIOD_100 (PERIOD_4 * 25 - 1)
\r
221 #define PERIOD_400 (PERIOD_100 * 4 + 1)
\r
223 static void ConvertFileTimeToString(const CNtfsFileTime *ft, char *s)
\r
225 unsigned year, mon, day, hour, min, sec;
\r
226 UInt64 v64 = (ft->Low | ((UInt64)ft->High << 32)) / 10000000;
\r
227 Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
\r
230 sec = (unsigned)(v64 % 60); v64 /= 60;
\r
231 min = (unsigned)(v64 % 60); v64 /= 60;
\r
232 hour = (unsigned)(v64 % 24); v64 /= 24;
\r
236 year = (unsigned)(1601 + v / PERIOD_400 * 400);
\r
239 t = v / PERIOD_100; if (t == 4) t = 3; year += t * 100; v -= t * PERIOD_100;
\r
240 t = v / PERIOD_4; if (t == 25) t = 24; year += t * 4; v -= t * PERIOD_4;
\r
241 t = v / 365; if (t == 4) t = 3; year += t; v -= t * 365;
\r
243 if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
\r
245 for (mon = 1; mon <= 12; mon++)
\r
247 unsigned s = ms[mon - 1];
\r
252 day = (unsigned)v + 1;
\r
253 s = UIntToStr(s, year, 4); *s++ = '-';
\r
254 s = UIntToStr(s, mon, 2); *s++ = '-';
\r
255 s = UIntToStr(s, day, 2); *s++ = ' ';
\r
256 s = UIntToStr(s, hour, 2); *s++ = ':';
\r
257 s = UIntToStr(s, min, 2); *s++ = ':';
\r
258 s = UIntToStr(s, sec, 2);
\r
261 void PrintError(char *sz)
\r
263 printf("\nERROR: %s\n", sz);
\r
266 static void GetAttribString(UInt32 Bool, Byte isDir, char *s)
\r
271 int _7zdecompress(char *path)
\r
273 CFileInStream archiveStream;
\r
274 CLookToRead lookStream;
\r
278 ISzAlloc allocTempImp;
\r
279 UInt16 *temp = NULL;
\r
280 size_t tempSize = 0;
\r
283 allocImp.Alloc = SzAlloc;
\r
284 allocImp.Free = SzFree;
\r
286 allocTempImp.Alloc = SzAllocTemp;
\r
287 allocTempImp.Free = SzFreeTemp;
\r
289 if (InFile_Open(&archiveStream.file, path))
\r
291 PrintError("can not open input file");
\r
295 FileInStream_CreateVTable(&archiveStream);
\r
296 LookToRead_CreateVTable(&lookStream, False);
\r
298 lookStream.realStream = &archiveStream.s;
\r
299 LookToRead_Init(&lookStream);
\r
301 CrcGenerateTable();
\r
304 res = SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp);
\r
307 int listCommand = 0, testCommand = 0, fullPaths = 0;
\r
315 if you need cache, use these 3 variables.
\r
316 if you use external function, you can make these variable as static.
\r
318 UInt32 blockIndex = 0xFFFFFFFF; /* it can have any value before first call (if outBuffer = 0) */
\r
319 Byte *outBuffer = 0; /* it must be 0 before first call for each new archive. */
\r
320 size_t outBufferSize = 0; /* it can have any value before first call (if outBuffer = 0) */
\r
322 for (i = 0; i < db.db.NumFiles; i++)
\r
325 size_t outSizeProcessed = 0;
\r
326 const CSzFileItem *f = db.db.Files + i;
\r
328 if (listCommand == 0 && f->IsDir && !fullPaths)
\r
330 len = SzArEx_GetFileNameUtf16(&db, i, NULL);
\r
332 if (len > tempSize)
\r
334 SzFree(NULL, temp);
\r
336 temp = (UInt16 *)SzAlloc(NULL, tempSize * sizeof(temp[0]));
\r
339 res = SZ_ERROR_MEM;
\r
344 SzArEx_GetFileNameUtf16(&db, i, temp);
\r
347 char attr[8], s[32], t[32];
\r
349 GetAttribString(f->AttribDefined ? f->Attrib : 0, f->IsDir, attr);
\r
351 UInt64ToStr(f->Size, s);
\r
352 if (f->MTimeDefined)
\r
353 ConvertFileTimeToString(&f->MTime, t);
\r
357 for (j = 0; j < 19; j++)
\r
362 printf("%s %s %10s ", t, attr, s);
\r
363 res = PrintString(temp);
\r
371 fputs(testCommand ?
\r
375 res = PrintString(temp);
\r
382 res = SzArEx_Extract(&db, &lookStream.s, i,
\r
383 &blockIndex, &outBuffer, &outBufferSize,
\r
384 &offset, &outSizeProcessed,
\r
385 &allocImp, &allocTempImp);
\r
392 size_t processedSize;
\r
394 UInt16 *name = (UInt16 *)temp;
\r
395 const UInt16 *destPath = (const UInt16 *)name;
\r
396 for (j = 0; name[j] != 0; j++)
\r
397 if (name[j] == '/')
\r
403 name[j] = CHAR_PATH_SEPARATOR;
\r
406 destPath = name + j + 1;
\r
408 PrintString(destPath);
\r
411 MyCreateDir(destPath);
\r
415 else if (OutFile_OpenUtf16(&outFile, destPath))
\r
417 PrintError("can not open output file");
\r
418 PrintString(destPath);
\r
419 res = SZ_ERROR_FAIL;
\r
422 processedSize = outSizeProcessed;
\r
423 if (File_Write(&outFile, outBuffer + offset, &processedSize) != 0 || processedSize != outSizeProcessed)
\r
425 PrintError("can not write output file");
\r
426 File_Close(&outFile);//wgid: 27067
\r
427 res = SZ_ERROR_FAIL;
\r
430 if (File_Close(&outFile))
\r
432 PrintError("can not close output file");
\r
433 res = SZ_ERROR_FAIL;
\r
436 #ifdef USE_WINDOWS_FILE
\r
437 if (f->AttribDefined)
\r
438 SetFileAttributesW(destPath, f->Attrib);
\r
443 IAlloc_Free(&allocImp, outBuffer);
\r
446 SzArEx_Free(&db, &allocImp);
\r
447 SzFree(NULL, temp);
\r
449 File_Close(&archiveStream.file);
\r
452 printf("\nEverything is Ok\n");
\r
455 if (res == SZ_ERROR_UNSUPPORTED)
\r
456 PrintError("decoder doesn't support this archive");
\r
457 else if (res == SZ_ERROR_MEM)
\r
458 PrintError("can not allocate memory");
\r
459 else if (res == SZ_ERROR_CRC)
\r
460 PrintError("CRC error");
\r
462 printf("\nERROR #%d\n", res);
\r