archive_util.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*-
  2. * Copyright (c) 2009-2012,2014 Michihiro NAKAJIMA
  3. * Copyright (c) 2003-2007 Tim Kientzle
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "archive_platform.h"
  27. __FBSDID("$FreeBSD: head/lib/libarchive/archive_util.c 201098 2009-12-28 02:58:14Z kientzle $");
  28. #ifdef HAVE_SYS_TYPES_H
  29. #include <sys/types.h>
  30. #endif
  31. #ifdef HAVE_ERRNO_H
  32. #include <errno.h>
  33. #endif
  34. #ifdef HAVE_FCNTL_H
  35. #include <fcntl.h>
  36. #endif
  37. #ifdef HAVE_STDLIB_H
  38. #include <stdlib.h>
  39. #endif
  40. #ifdef HAVE_STRING_H
  41. #include <string.h>
  42. #endif
  43. #if defined(HAVE_WINCRYPT_H) && !defined(__CYGWIN__)
  44. #include <wincrypt.h>
  45. #endif
  46. #ifdef HAVE_ZLIB_H
  47. #include <cm_zlib.h>
  48. #endif
  49. #ifdef HAVE_LZMA_H
  50. #include <cm_lzma.h>
  51. #endif
  52. #ifdef HAVE_BZLIB_H
  53. #include <cm_bzlib.h>
  54. #endif
  55. #ifdef HAVE_LZ4_H
  56. #include <lz4.h>
  57. #endif
  58. #include "archive.h"
  59. #include "archive_private.h"
  60. #include "archive_random_private.h"
  61. #include "archive_string.h"
  62. #ifndef O_CLOEXEC
  63. #define O_CLOEXEC 0
  64. #endif
  65. static int archive_utility_string_sort_helper(char **, unsigned int);
  66. /* Generic initialization of 'struct archive' objects. */
  67. int
  68. __archive_clean(struct archive *a)
  69. {
  70. archive_string_conversion_free(a);
  71. return (ARCHIVE_OK);
  72. }
  73. int
  74. archive_version_number(void)
  75. {
  76. return (ARCHIVE_VERSION_NUMBER);
  77. }
  78. const char *
  79. archive_version_string(void)
  80. {
  81. return (ARCHIVE_VERSION_STRING);
  82. }
  83. int
  84. archive_errno(struct archive *a)
  85. {
  86. return (a->archive_error_number);
  87. }
  88. const char *
  89. archive_error_string(struct archive *a)
  90. {
  91. if (a->error != NULL && *a->error != '\0')
  92. return (a->error);
  93. else
  94. return (NULL);
  95. }
  96. int
  97. archive_file_count(struct archive *a)
  98. {
  99. return (a->file_count);
  100. }
  101. int
  102. archive_format(struct archive *a)
  103. {
  104. return (a->archive_format);
  105. }
  106. const char *
  107. archive_format_name(struct archive *a)
  108. {
  109. return (a->archive_format_name);
  110. }
  111. int
  112. archive_compression(struct archive *a)
  113. {
  114. return archive_filter_code(a, 0);
  115. }
  116. const char *
  117. archive_compression_name(struct archive *a)
  118. {
  119. return archive_filter_name(a, 0);
  120. }
  121. /*
  122. * Return a count of the number of compressed bytes processed.
  123. */
  124. int64_t
  125. archive_position_compressed(struct archive *a)
  126. {
  127. return archive_filter_bytes(a, -1);
  128. }
  129. /*
  130. * Return a count of the number of uncompressed bytes processed.
  131. */
  132. int64_t
  133. archive_position_uncompressed(struct archive *a)
  134. {
  135. return archive_filter_bytes(a, 0);
  136. }
  137. void
  138. archive_clear_error(struct archive *a)
  139. {
  140. archive_string_empty(&a->error_string);
  141. a->error = NULL;
  142. a->archive_error_number = 0;
  143. }
  144. void
  145. archive_set_error(struct archive *a, int error_number, const char *fmt, ...)
  146. {
  147. va_list ap;
  148. a->archive_error_number = error_number;
  149. if (fmt == NULL) {
  150. a->error = NULL;
  151. return;
  152. }
  153. archive_string_empty(&(a->error_string));
  154. va_start(ap, fmt);
  155. archive_string_vsprintf(&(a->error_string), fmt, ap);
  156. va_end(ap);
  157. a->error = a->error_string.s;
  158. }
  159. void
  160. archive_copy_error(struct archive *dest, struct archive *src)
  161. {
  162. dest->archive_error_number = src->archive_error_number;
  163. archive_string_copy(&dest->error_string, &src->error_string);
  164. dest->error = dest->error_string.s;
  165. }
  166. void
  167. __archive_errx(int retvalue, const char *msg)
  168. {
  169. static const char msg1[] = "Fatal Internal Error in libarchive: ";
  170. size_t s;
  171. s = write(2, msg1, strlen(msg1));
  172. (void)s; /* UNUSED */
  173. s = write(2, msg, strlen(msg));
  174. (void)s; /* UNUSED */
  175. s = write(2, "\n", 1);
  176. (void)s; /* UNUSED */
  177. exit(retvalue);
  178. }
  179. /*
  180. * Create a temporary file
  181. */
  182. #if defined(_WIN32) && !defined(__CYGWIN__)
  183. /*
  184. * Do not use Windows tmpfile() function.
  185. * It will make a temporary file under the root directory
  186. * and it'll cause permission error if a user who is
  187. * non-Administrator creates temporary files.
  188. * Also Windows version of mktemp family including _mktemp_s
  189. * are not secure.
  190. */
  191. int
  192. __archive_mktemp(const char *tmpdir)
  193. {
  194. static const wchar_t prefix[] = L"libarchive_";
  195. static const wchar_t suffix[] = L"XXXXXXXXXX";
  196. static const wchar_t num[] = {
  197. L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7',
  198. L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F',
  199. L'G', L'H', L'I', L'J', L'K', L'L', L'M', L'N',
  200. L'O', L'P', L'Q', L'R', L'S', L'T', L'U', L'V',
  201. L'W', L'X', L'Y', L'Z', L'a', L'b', L'c', L'd',
  202. L'e', L'f', L'g', L'h', L'i', L'j', L'k', L'l',
  203. L'm', L'n', L'o', L'p', L'q', L'r', L's', L't',
  204. L'u', L'v', L'w', L'x', L'y', L'z'
  205. };
  206. HCRYPTPROV hProv;
  207. struct archive_wstring temp_name;
  208. wchar_t *ws;
  209. DWORD attr;
  210. wchar_t *xp, *ep;
  211. int fd;
  212. hProv = (HCRYPTPROV)NULL;
  213. fd = -1;
  214. ws = NULL;
  215. archive_string_init(&temp_name);
  216. /* Get a temporary directory. */
  217. if (tmpdir == NULL) {
  218. size_t l;
  219. wchar_t *tmp;
  220. l = GetTempPathW(0, NULL);
  221. if (l == 0) {
  222. la_dosmaperr(GetLastError());
  223. goto exit_tmpfile;
  224. }
  225. tmp = malloc(l*sizeof(wchar_t));
  226. if (tmp == NULL) {
  227. errno = ENOMEM;
  228. goto exit_tmpfile;
  229. }
  230. GetTempPathW((DWORD)l, tmp);
  231. archive_wstrcpy(&temp_name, tmp);
  232. free(tmp);
  233. } else {
  234. if (archive_wstring_append_from_mbs(&temp_name, tmpdir,
  235. strlen(tmpdir)) < 0)
  236. goto exit_tmpfile;
  237. if (temp_name.s[temp_name.length-1] != L'/')
  238. archive_wstrappend_wchar(&temp_name, L'/');
  239. }
  240. /* Check if temp_name is a directory. */
  241. attr = GetFileAttributesW(temp_name.s);
  242. if (attr == (DWORD)-1) {
  243. if (GetLastError() != ERROR_FILE_NOT_FOUND) {
  244. la_dosmaperr(GetLastError());
  245. goto exit_tmpfile;
  246. }
  247. ws = __la_win_permissive_name_w(temp_name.s);
  248. if (ws == NULL) {
  249. errno = EINVAL;
  250. goto exit_tmpfile;
  251. }
  252. attr = GetFileAttributesW(ws);
  253. if (attr == (DWORD)-1) {
  254. la_dosmaperr(GetLastError());
  255. goto exit_tmpfile;
  256. }
  257. }
  258. if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) {
  259. errno = ENOTDIR;
  260. goto exit_tmpfile;
  261. }
  262. /*
  263. * Create a temporary file.
  264. */
  265. archive_wstrcat(&temp_name, prefix);
  266. archive_wstrcat(&temp_name, suffix);
  267. ep = temp_name.s + archive_strlen(&temp_name);
  268. xp = ep - wcslen(suffix);
  269. if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
  270. CRYPT_VERIFYCONTEXT)) {
  271. la_dosmaperr(GetLastError());
  272. goto exit_tmpfile;
  273. }
  274. for (;;) {
  275. wchar_t *p;
  276. HANDLE h;
  277. /* Generate a random file name through CryptGenRandom(). */
  278. p = xp;
  279. if (!CryptGenRandom(hProv, (DWORD)(ep - p)*sizeof(wchar_t),
  280. (BYTE*)p)) {
  281. la_dosmaperr(GetLastError());
  282. goto exit_tmpfile;
  283. }
  284. for (; p < ep; p++)
  285. *p = num[((DWORD)*p) % (sizeof(num)/sizeof(num[0]))];
  286. free(ws);
  287. ws = __la_win_permissive_name_w(temp_name.s);
  288. if (ws == NULL) {
  289. errno = EINVAL;
  290. goto exit_tmpfile;
  291. }
  292. /* Specifies FILE_FLAG_DELETE_ON_CLOSE flag is to
  293. * delete this temporary file immediately when this
  294. * file closed. */
  295. h = CreateFileW(ws,
  296. GENERIC_READ | GENERIC_WRITE | DELETE,
  297. 0,/* Not share */
  298. NULL,
  299. CREATE_NEW,/* Create a new file only */
  300. FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
  301. NULL);
  302. if (h == INVALID_HANDLE_VALUE) {
  303. /* The same file already exists. retry with
  304. * a new filename. */
  305. if (GetLastError() == ERROR_FILE_EXISTS)
  306. continue;
  307. /* Otherwise, fail creation temporary file. */
  308. la_dosmaperr(GetLastError());
  309. goto exit_tmpfile;
  310. }
  311. fd = _open_osfhandle((intptr_t)h, _O_BINARY | _O_RDWR);
  312. if (fd == -1) {
  313. CloseHandle(h);
  314. goto exit_tmpfile;
  315. } else
  316. break;/* success! */
  317. }
  318. exit_tmpfile:
  319. if (hProv != (HCRYPTPROV)NULL)
  320. CryptReleaseContext(hProv, 0);
  321. free(ws);
  322. archive_wstring_free(&temp_name);
  323. return (fd);
  324. }
  325. #else
  326. static int
  327. get_tempdir(struct archive_string *temppath)
  328. {
  329. const char *tmp;
  330. tmp = getenv("TMPDIR");
  331. if (tmp == NULL)
  332. #ifdef _PATH_TMP
  333. tmp = _PATH_TMP;
  334. #else
  335. tmp = "/tmp";
  336. #endif
  337. archive_strcpy(temppath, tmp);
  338. if (temppath->s[temppath->length-1] != '/')
  339. archive_strappend_char(temppath, '/');
  340. return (ARCHIVE_OK);
  341. }
  342. #if defined(HAVE_MKSTEMP)
  343. /*
  344. * We can use mkstemp().
  345. */
  346. int
  347. __archive_mktemp(const char *tmpdir)
  348. {
  349. struct archive_string temp_name;
  350. int fd = -1;
  351. archive_string_init(&temp_name);
  352. if (tmpdir == NULL) {
  353. if (get_tempdir(&temp_name) != ARCHIVE_OK)
  354. goto exit_tmpfile;
  355. } else {
  356. archive_strcpy(&temp_name, tmpdir);
  357. if (temp_name.s[temp_name.length-1] != '/')
  358. archive_strappend_char(&temp_name, '/');
  359. }
  360. archive_strcat(&temp_name, "libarchive_XXXXXX");
  361. fd = mkstemp(temp_name.s);
  362. if (fd < 0)
  363. goto exit_tmpfile;
  364. __archive_ensure_cloexec_flag(fd);
  365. unlink(temp_name.s);
  366. exit_tmpfile:
  367. archive_string_free(&temp_name);
  368. return (fd);
  369. }
  370. #else
  371. /*
  372. * We use a private routine.
  373. */
  374. int
  375. __archive_mktemp(const char *tmpdir)
  376. {
  377. static const char num[] = {
  378. '0', '1', '2', '3', '4', '5', '6', '7',
  379. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
  380. 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  381. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  382. 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
  383. 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
  384. 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  385. 'u', 'v', 'w', 'x', 'y', 'z'
  386. };
  387. struct archive_string temp_name;
  388. struct stat st;
  389. int fd;
  390. char *tp, *ep;
  391. fd = -1;
  392. archive_string_init(&temp_name);
  393. if (tmpdir == NULL) {
  394. if (get_tempdir(&temp_name) != ARCHIVE_OK)
  395. goto exit_tmpfile;
  396. } else
  397. archive_strcpy(&temp_name, tmpdir);
  398. if (temp_name.s[temp_name.length-1] == '/') {
  399. temp_name.s[temp_name.length-1] = '\0';
  400. temp_name.length --;
  401. }
  402. if (stat(temp_name.s, &st) < 0)
  403. goto exit_tmpfile;
  404. if (!S_ISDIR(st.st_mode)) {
  405. errno = ENOTDIR;
  406. goto exit_tmpfile;
  407. }
  408. archive_strcat(&temp_name, "/libarchive_");
  409. tp = temp_name.s + archive_strlen(&temp_name);
  410. archive_strcat(&temp_name, "XXXXXXXXXX");
  411. ep = temp_name.s + archive_strlen(&temp_name);
  412. do {
  413. char *p;
  414. p = tp;
  415. archive_random(p, ep - p);
  416. while (p < ep) {
  417. int d = *((unsigned char *)p) % sizeof(num);
  418. *p++ = num[d];
  419. }
  420. fd = open(temp_name.s, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC,
  421. 0600);
  422. } while (fd < 0 && errno == EEXIST);
  423. if (fd < 0)
  424. goto exit_tmpfile;
  425. __archive_ensure_cloexec_flag(fd);
  426. unlink(temp_name.s);
  427. exit_tmpfile:
  428. archive_string_free(&temp_name);
  429. return (fd);
  430. }
  431. #endif /* HAVE_MKSTEMP */
  432. #endif /* !_WIN32 || __CYGWIN__ */
  433. /*
  434. * Set FD_CLOEXEC flag to a file descriptor if it is not set.
  435. * We have to set the flag if the platform does not provide O_CLOEXEC
  436. * or F_DUPFD_CLOEXEC flags.
  437. *
  438. * Note: This function is absolutely called after creating a new file
  439. * descriptor even if the platform seemingly provides O_CLOEXEC or
  440. * F_DUPFD_CLOEXEC macros because it is possible that the platform
  441. * merely declares those macros, especially Linux 2.6.18 - 2.6.24 do it.
  442. */
  443. void
  444. __archive_ensure_cloexec_flag(int fd)
  445. {
  446. #if defined(_WIN32) && !defined(__CYGWIN__)
  447. (void)fd; /* UNUSED */
  448. #else
  449. int flags;
  450. if (fd >= 0) {
  451. flags = fcntl(fd, F_GETFD);
  452. if (flags != -1 && (flags & FD_CLOEXEC) == 0)
  453. fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
  454. }
  455. #endif
  456. }
  457. /*
  458. * Utility function to sort a group of strings using quicksort.
  459. */
  460. static int
  461. archive_utility_string_sort_helper(char **strings, unsigned int n)
  462. {
  463. unsigned int i, lesser_count, greater_count;
  464. char **lesser, **greater, **tmp, *pivot;
  465. int retval1, retval2;
  466. /* A list of 0 or 1 elements is already sorted */
  467. if (n <= 1)
  468. return (ARCHIVE_OK);
  469. lesser_count = greater_count = 0;
  470. lesser = greater = NULL;
  471. pivot = strings[0];
  472. for (i = 1; i < n; i++)
  473. {
  474. if (strcmp(strings[i], pivot) < 0)
  475. {
  476. lesser_count++;
  477. tmp = (char **)realloc(lesser,
  478. lesser_count * sizeof(char *));
  479. if (!tmp) {
  480. free(greater);
  481. free(lesser);
  482. return (ARCHIVE_FATAL);
  483. }
  484. lesser = tmp;
  485. lesser[lesser_count - 1] = strings[i];
  486. }
  487. else
  488. {
  489. greater_count++;
  490. tmp = (char **)realloc(greater,
  491. greater_count * sizeof(char *));
  492. if (!tmp) {
  493. free(greater);
  494. free(lesser);
  495. return (ARCHIVE_FATAL);
  496. }
  497. greater = tmp;
  498. greater[greater_count - 1] = strings[i];
  499. }
  500. }
  501. /* quicksort(lesser) */
  502. retval1 = archive_utility_string_sort_helper(lesser, lesser_count);
  503. for (i = 0; i < lesser_count; i++)
  504. strings[i] = lesser[i];
  505. free(lesser);
  506. /* pivot */
  507. strings[lesser_count] = pivot;
  508. /* quicksort(greater) */
  509. retval2 = archive_utility_string_sort_helper(greater, greater_count);
  510. for (i = 0; i < greater_count; i++)
  511. strings[lesser_count + 1 + i] = greater[i];
  512. free(greater);
  513. return (retval1 < retval2) ? retval1 : retval2;
  514. }
  515. int
  516. archive_utility_string_sort(char **strings)
  517. {
  518. unsigned int size = 0;
  519. while (strings[size] != NULL)
  520. size++;
  521. return archive_utility_string_sort_helper(strings, size);
  522. }