Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

546 lines
13 KiB

  1. /* 7zMain.c - Test application for 7z Decoder
  2. 2015-01-02 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "../../7z.h"
  7. #include "../../7zAlloc.h"
  8. #include "../../7zBuf.h"
  9. #include "../../7zCrc.h"
  10. #include "../../7zFile.h"
  11. #include "../../7zVersion.h"
  12. #ifndef USE_WINDOWS_FILE
  13. /* for mkdir */
  14. #ifdef _WIN32
  15. #include <direct.h>
  16. #else
  17. #include <sys/stat.h>
  18. #include <errno.h>
  19. #endif
  20. #endif
  21. static ISzAlloc g_Alloc = { SzAlloc, SzFree };
  22. static int Buf_EnsureSize(CBuf *dest, size_t size)
  23. {
  24. if (dest->size >= size)
  25. return 1;
  26. Buf_Free(dest, &g_Alloc);
  27. return Buf_Create(dest, size, &g_Alloc);
  28. }
  29. #ifndef _WIN32
  30. static Byte kUtf8Limits[5] = { 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  31. static Bool Utf16_To_Utf8(Byte *dest, size_t *destLen, const UInt16 *src, size_t srcLen)
  32. {
  33. size_t destPos = 0, srcPos = 0;
  34. for (;;)
  35. {
  36. unsigned numAdds;
  37. UInt32 value;
  38. if (srcPos == srcLen)
  39. {
  40. *destLen = destPos;
  41. return True;
  42. }
  43. value = src[srcPos++];
  44. if (value < 0x80)
  45. {
  46. if (dest)
  47. dest[destPos] = (char)value;
  48. destPos++;
  49. continue;
  50. }
  51. if (value >= 0xD800 && value < 0xE000)
  52. {
  53. UInt32 c2;
  54. if (value >= 0xDC00 || srcPos == srcLen)
  55. break;
  56. c2 = src[srcPos++];
  57. if (c2 < 0xDC00 || c2 >= 0xE000)
  58. break;
  59. value = (((value - 0xD800) << 10) | (c2 - 0xDC00)) + 0x10000;
  60. }
  61. for (numAdds = 1; numAdds < 5; numAdds++)
  62. if (value < (((UInt32)1) << (numAdds * 5 + 6)))
  63. break;
  64. if (dest)
  65. dest[destPos] = (char)(kUtf8Limits[numAdds - 1] + (value >> (6 * numAdds)));
  66. destPos++;
  67. do
  68. {
  69. numAdds--;
  70. if (dest)
  71. dest[destPos] = (char)(0x80 + ((value >> (6 * numAdds)) & 0x3F));
  72. destPos++;
  73. }
  74. while (numAdds != 0);
  75. }
  76. *destLen = destPos;
  77. return False;
  78. }
  79. static SRes Utf16_To_Utf8Buf(CBuf *dest, const UInt16 *src, size_t srcLen)
  80. {
  81. size_t destLen = 0;
  82. Bool res;
  83. Utf16_To_Utf8(NULL, &destLen, src, srcLen);
  84. destLen += 1;
  85. if (!Buf_EnsureSize(dest, destLen))
  86. return SZ_ERROR_MEM;
  87. res = Utf16_To_Utf8(dest->data, &destLen, src, srcLen);
  88. dest->data[destLen] = 0;
  89. return res ? SZ_OK : SZ_ERROR_FAIL;
  90. }
  91. #endif
  92. static SRes Utf16_To_Char(CBuf *buf, const UInt16 *s
  93. #ifdef _WIN32
  94. , UINT codePage
  95. #endif
  96. )
  97. {
  98. unsigned len = 0;
  99. for (len = 0; s[len] != 0; len++);
  100. #ifdef _WIN32
  101. {
  102. unsigned size = len * 3 + 100;
  103. if (!Buf_EnsureSize(buf, size))
  104. return SZ_ERROR_MEM;
  105. {
  106. buf->data[0] = 0;
  107. if (len != 0)
  108. {
  109. char defaultChar = '_';
  110. BOOL defUsed;
  111. unsigned numChars = 0;
  112. numChars = WideCharToMultiByte(codePage, 0, s, len, (char *)buf->data, size, &defaultChar, &defUsed);
  113. if (numChars == 0 || numChars >= size)
  114. return SZ_ERROR_FAIL;
  115. buf->data[numChars] = 0;
  116. }
  117. return SZ_OK;
  118. }
  119. }
  120. #else
  121. return Utf16_To_Utf8Buf(buf, s, len);
  122. #endif
  123. }
  124. #ifdef _WIN32
  125. #ifndef USE_WINDOWS_FILE
  126. static UINT g_FileCodePage = CP_ACP;
  127. #endif
  128. #define MY_FILE_CODE_PAGE_PARAM ,g_FileCodePage
  129. #else
  130. #define MY_FILE_CODE_PAGE_PARAM
  131. #endif
  132. static WRes MyCreateDir(const UInt16 *name)
  133. {
  134. #ifdef USE_WINDOWS_FILE
  135. return CreateDirectoryW(name, NULL) ? 0 : GetLastError();
  136. #else
  137. CBuf buf;
  138. WRes res;
  139. Buf_Init(&buf);
  140. RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM));
  141. res =
  142. #ifdef _WIN32
  143. _mkdir((const char *)buf.data)
  144. #else
  145. mkdir((const char *)buf.data, 0777)
  146. #endif
  147. == 0 ? 0 : errno;
  148. Buf_Free(&buf, &g_Alloc);
  149. return res;
  150. #endif
  151. }
  152. static WRes OutFile_OpenUtf16(CSzFile *p, const UInt16 *name)
  153. {
  154. #ifdef USE_WINDOWS_FILE
  155. return OutFile_OpenW(p, name);
  156. #else
  157. CBuf buf;
  158. WRes res;
  159. Buf_Init(&buf);
  160. RINOK(Utf16_To_Char(&buf, name MY_FILE_CODE_PAGE_PARAM));
  161. res = OutFile_Open(p, (const char *)buf.data);
  162. Buf_Free(&buf, &g_Alloc);
  163. return res;
  164. #endif
  165. }
  166. static SRes PrintString(const UInt16 *s)
  167. {
  168. CBuf buf;
  169. SRes res;
  170. Buf_Init(&buf);
  171. res = Utf16_To_Char(&buf, s
  172. #ifdef _WIN32
  173. , CP_OEMCP
  174. #endif
  175. );
  176. if (res == SZ_OK)
  177. fputs((const char *)buf.data, stdout);
  178. Buf_Free(&buf, &g_Alloc);
  179. return res;
  180. }
  181. static void UInt64ToStr(UInt64 value, char *s)
  182. {
  183. char temp[32];
  184. int pos = 0;
  185. do
  186. {
  187. temp[pos++] = (char)('0' + (unsigned)(value % 10));
  188. value /= 10;
  189. }
  190. while (value != 0);
  191. do
  192. *s++ = temp[--pos];
  193. while (pos);
  194. *s = '\0';
  195. }
  196. static char *UIntToStr(char *s, unsigned value, int numDigits)
  197. {
  198. char temp[16];
  199. int pos = 0;
  200. do
  201. temp[pos++] = (char)('0' + (value % 10));
  202. while (value /= 10);
  203. for (numDigits -= pos; numDigits > 0; numDigits--)
  204. *s++ = '0';
  205. do
  206. *s++ = temp[--pos];
  207. while (pos);
  208. *s = '\0';
  209. return s;
  210. }
  211. static void UIntToStr_2(char *s, unsigned value)
  212. {
  213. s[0] = (char)('0' + (value / 10));
  214. s[1] = (char)('0' + (value % 10));
  215. }
  216. #define PERIOD_4 (4 * 365 + 1)
  217. #define PERIOD_100 (PERIOD_4 * 25 - 1)
  218. #define PERIOD_400 (PERIOD_100 * 4 + 1)
  219. static void ConvertFileTimeToString(const CNtfsFileTime *nt, char *s)
  220. {
  221. unsigned year, mon, hour, min, sec;
  222. Byte ms[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  223. unsigned t;
  224. UInt32 v;
  225. UInt64 v64 = nt->Low | ((UInt64)nt->High << 32);
  226. v64 /= 10000000;
  227. sec = (unsigned)(v64 % 60); v64 /= 60;
  228. min = (unsigned)(v64 % 60); v64 /= 60;
  229. hour = (unsigned)(v64 % 24); v64 /= 24;
  230. v = (UInt32)v64;
  231. year = (unsigned)(1601 + v / PERIOD_400 * 400);
  232. v %= PERIOD_400;
  233. t = v / PERIOD_100; if (t == 4) t = 3; year += t * 100; v -= t * PERIOD_100;
  234. t = v / PERIOD_4; if (t == 25) t = 24; year += t * 4; v -= t * PERIOD_4;
  235. t = v / 365; if (t == 4) t = 3; year += t; v -= t * 365;
  236. if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
  237. ms[1] = 29;
  238. for (mon = 0;; mon++)
  239. {
  240. unsigned s = ms[mon];
  241. if (v < s)
  242. break;
  243. v -= s;
  244. }
  245. s = UIntToStr(s, year, 4); *s++ = '-';
  246. UIntToStr_2(s, mon + 1); s[2] = '-'; s += 3;
  247. UIntToStr_2(s, (unsigned)v + 1); s[2] = ' '; s += 3;
  248. UIntToStr_2(s, hour); s[2] = ':'; s += 3;
  249. UIntToStr_2(s, min); s[2] = ':'; s += 3;
  250. UIntToStr_2(s, sec); s[2] = 0;
  251. }
  252. void PrintError(const char *sz)
  253. {
  254. printf("\nERROR: %s\n", sz);
  255. }
  256. #ifdef USE_WINDOWS_FILE
  257. static void GetAttribString(UInt32 wa, Bool isDir, char *s)
  258. {
  259. s[0] = (char)(((wa & FILE_ATTRIBUTE_DIRECTORY) != 0 || isDir) ? 'D' : '.');
  260. s[1] = (char)(((wa & FILE_ATTRIBUTE_READONLY ) != 0) ? 'R': '.');
  261. s[2] = (char)(((wa & FILE_ATTRIBUTE_HIDDEN ) != 0) ? 'H': '.');
  262. s[3] = (char)(((wa & FILE_ATTRIBUTE_SYSTEM ) != 0) ? 'S': '.');
  263. s[4] = (char)(((wa & FILE_ATTRIBUTE_ARCHIVE ) != 0) ? 'A': '.');
  264. s[5] = '\0';
  265. }
  266. #else
  267. static void GetAttribString(UInt32, Bool, char *s)
  268. {
  269. s[0] = '\0';
  270. }
  271. #endif
  272. // #define NUM_PARENTS_MAX 128
  273. int MY_CDECL main(int numargs, char *args[])
  274. {
  275. CFileInStream archiveStream;
  276. CLookToRead lookStream;
  277. CSzArEx db;
  278. SRes res;
  279. ISzAlloc allocImp;
  280. ISzAlloc allocTempImp;
  281. UInt16 *temp = NULL;
  282. size_t tempSize = 0;
  283. // UInt32 parents[NUM_PARENTS_MAX];
  284. printf("\n7z ANSI-C Decoder " MY_VERSION_COPYRIGHT_DATE "\n\n");
  285. if (numargs == 1)
  286. {
  287. printf(
  288. "Usage: 7zDec <command> <archive_name>\n\n"
  289. "<Commands>\n"
  290. " e: Extract files from archive (without using directory names)\n"
  291. " l: List contents of archive\n"
  292. " t: Test integrity of archive\n"
  293. " x: eXtract files with full paths\n");
  294. return 0;
  295. }
  296. if (numargs < 3)
  297. {
  298. PrintError("incorrect command");
  299. return 1;
  300. }
  301. #if defined(_WIN32) && !defined(USE_WINDOWS_FILE) && !defined(UNDER_CE)
  302. g_FileCodePage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
  303. #endif
  304. allocImp.Alloc = SzAlloc;
  305. allocImp.Free = SzFree;
  306. allocTempImp.Alloc = SzAllocTemp;
  307. allocTempImp.Free = SzFreeTemp;
  308. #ifdef UNDER_CE
  309. if (InFile_OpenW(&archiveStream.file, L"\test.7z"))
  310. #else
  311. if (InFile_Open(&archiveStream.file, args[2]))
  312. #endif
  313. {
  314. PrintError("can not open input file");
  315. return 1;
  316. }
  317. FileInStream_CreateVTable(&archiveStream);
  318. LookToRead_CreateVTable(&lookStream, False);
  319. lookStream.realStream = &archiveStream.s;
  320. LookToRead_Init(&lookStream);
  321. CrcGenerateTable();
  322. SzArEx_Init(&db);
  323. res = SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp);
  324. if (res == SZ_OK)
  325. {
  326. char *command = args[1];
  327. int listCommand = 0, testCommand = 0, fullPaths = 0;
  328. if (strcmp(command, "l") == 0) listCommand = 1;
  329. else if (strcmp(command, "t") == 0) testCommand = 1;
  330. else if (strcmp(command, "e") == 0) { }
  331. else if (strcmp(command, "x") == 0) { fullPaths = 1; }
  332. else
  333. {
  334. PrintError("incorrect command");
  335. res = SZ_ERROR_FAIL;
  336. }
  337. if (res == SZ_OK)
  338. {
  339. UInt32 i;
  340. /*
  341. if you need cache, use these 3 variables.
  342. if you use external function, you can make these variable as static.
  343. */
  344. UInt32 blockIndex = 0xFFFFFFFF; /* it can have any value before first call (if outBuffer = 0) */
  345. Byte *outBuffer = 0; /* it must be 0 before first call for each new archive. */
  346. size_t outBufferSize = 0; /* it can have any value before first call (if outBuffer = 0) */
  347. for (i = 0; i < db.NumFiles; i++)
  348. {
  349. size_t offset = 0;
  350. size_t outSizeProcessed = 0;
  351. // const CSzFileItem *f = db.Files + i;
  352. size_t len;
  353. int isDir = SzArEx_IsDir(&db, i);
  354. if (listCommand == 0 && isDir && !fullPaths)
  355. continue;
  356. len = SzArEx_GetFileNameUtf16(&db, i, NULL);
  357. // len = SzArEx_GetFullNameLen(&db, i);
  358. if (len > tempSize)
  359. {
  360. SzFree(NULL, temp);
  361. tempSize = len;
  362. temp = (UInt16 *)SzAlloc(NULL, tempSize * sizeof(temp[0]));
  363. if (!temp)
  364. {
  365. res = SZ_ERROR_MEM;
  366. break;
  367. }
  368. }
  369. SzArEx_GetFileNameUtf16(&db, i, temp);
  370. /*
  371. if (SzArEx_GetFullNameUtf16_Back(&db, i, temp + len) != temp)
  372. {
  373. res = SZ_ERROR_FAIL;
  374. break;
  375. }
  376. */
  377. if (listCommand)
  378. {
  379. char attr[8], s[32], t[32];
  380. UInt64 fileSize;
  381. GetAttribString(SzBitWithVals_Check(&db.Attribs, i) ? db.Attribs.Vals[i] : 0, isDir, attr);
  382. fileSize = SzArEx_GetFileSize(&db, i);
  383. UInt64ToStr(fileSize, s);
  384. if (SzBitWithVals_Check(&db.MTime, i))
  385. ConvertFileTimeToString(&db.MTime.Vals[i], t);
  386. else
  387. {
  388. size_t j;
  389. for (j = 0; j < 19; j++)
  390. t[j] = ' ';
  391. t[j] = '\0';
  392. }
  393. printf("%s %s %10s ", t, attr, s);
  394. res = PrintString(temp);
  395. if (res != SZ_OK)
  396. break;
  397. if (isDir)
  398. printf("/");
  399. printf("\n");
  400. continue;
  401. }
  402. fputs(testCommand ?
  403. "Testing ":
  404. "Extracting ",
  405. stdout);
  406. res = PrintString(temp);
  407. if (res != SZ_OK)
  408. break;
  409. if (isDir)
  410. printf("/");
  411. else
  412. {
  413. res = SzArEx_Extract(&db, &lookStream.s, i,
  414. &blockIndex, &outBuffer, &outBufferSize,
  415. &offset, &outSizeProcessed,
  416. &allocImp, &allocTempImp);
  417. if (res != SZ_OK)
  418. break;
  419. }
  420. if (!testCommand)
  421. {
  422. CSzFile outFile;
  423. size_t processedSize;
  424. size_t j;
  425. UInt16 *name = (UInt16 *)temp;
  426. const UInt16 *destPath = (const UInt16 *)name;
  427. for (j = 0; name[j] != 0; j++)
  428. if (name[j] == '/')
  429. {
  430. if (fullPaths)
  431. {
  432. name[j] = 0;
  433. MyCreateDir(name);
  434. name[j] = CHAR_PATH_SEPARATOR;
  435. }
  436. else
  437. destPath = name + j + 1;
  438. }
  439. if (isDir)
  440. {
  441. MyCreateDir(destPath);
  442. printf("\n");
  443. continue;
  444. }
  445. else if (OutFile_OpenUtf16(&outFile, destPath))
  446. {
  447. PrintError("can not open output file");
  448. res = SZ_ERROR_FAIL;
  449. break;
  450. }
  451. processedSize = outSizeProcessed;
  452. if (File_Write(&outFile, outBuffer + offset, &processedSize) != 0 || processedSize != outSizeProcessed)
  453. {
  454. PrintError("can not write output file");
  455. res = SZ_ERROR_FAIL;
  456. break;
  457. }
  458. if (File_Close(&outFile))
  459. {
  460. PrintError("can not close output file");
  461. res = SZ_ERROR_FAIL;
  462. break;
  463. }
  464. #ifdef USE_WINDOWS_FILE
  465. if (SzBitWithVals_Check(&db.Attribs, i))
  466. SetFileAttributesW(destPath, db.Attribs.Vals[i]);
  467. #endif
  468. }
  469. printf("\n");
  470. }
  471. IAlloc_Free(&allocImp, outBuffer);
  472. }
  473. }
  474. SzArEx_Free(&db, &allocImp);
  475. SzFree(NULL, temp);
  476. File_Close(&archiveStream.file);
  477. if (res == SZ_OK)
  478. {
  479. printf("\nEverything is Ok\n");
  480. return 0;
  481. }
  482. if (res == SZ_ERROR_UNSUPPORTED)
  483. PrintError("decoder doesn't support this archive");
  484. else if (res == SZ_ERROR_MEM)
  485. PrintError("can not allocate memory");
  486. else if (res == SZ_ERROR_CRC)
  487. PrintError("CRC error");
  488. else
  489. printf("\nERROR #%d\n", res);
  490. return 1;
  491. }