archive_util.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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. #ifdef HAVE_SYS_TYPES_H
  28. #include <sys/types.h>
  29. #endif
  30. #ifdef HAVE_ERRNO_H
  31. #include <errno.h>
  32. #endif
  33. #ifdef HAVE_FCNTL_H
  34. #include <fcntl.h>
  35. #endif
  36. #ifdef HAVE_STDLIB_H
  37. #include <stdlib.h>
  38. #endif
  39. #ifdef HAVE_STRING_H
  40. #include <string.h>
  41. #endif
  42. #if defined(_WIN32) && !defined(__CYGWIN__)
  43. #if defined(HAVE_BCRYPT_H) && _WIN32_WINNT >= _WIN32_WINNT_VISTA
  44. /* don't use bcrypt when XP needs to be supported */
  45. #include <bcrypt.h>
  46. /* Common in other bcrypt implementations, but missing from VS2008. */
  47. #ifndef BCRYPT_SUCCESS
  48. #define BCRYPT_SUCCESS(r) ((NTSTATUS)(r) == STATUS_SUCCESS)
  49. #endif
  50. #elif defined(HAVE_WINCRYPT_H)
  51. #include <wincrypt.h>
  52. #endif
  53. #endif
  54. #ifdef HAVE_ZLIB_H
  55. #include <cm3p/zlib.h>
  56. #endif
  57. #ifdef HAVE_LZMA_H
  58. #include <cm3p/lzma.h>
  59. #endif
  60. #ifdef HAVE_BZLIB_H
  61. #include <cm3p/bzlib.h>
  62. #endif
  63. #ifdef HAVE_LZ4_H
  64. #include <lz4.h>
  65. #endif
  66. #include "archive.h"
  67. #include "archive_private.h"
  68. #include "archive_random_private.h"
  69. #include "archive_string.h"
  70. #ifndef O_CLOEXEC
  71. #define O_CLOEXEC 0
  72. #endif
  73. static int archive_utility_string_sort_helper(char **, unsigned int);
  74. /* Generic initialization of 'struct archive' objects. */
  75. int
  76. __archive_clean(struct archive *a)
  77. {
  78. archive_string_conversion_free(a);
  79. return (ARCHIVE_OK);
  80. }
  81. int
  82. archive_version_number(void)
  83. {
  84. return (ARCHIVE_VERSION_NUMBER);
  85. }
  86. const char *
  87. archive_version_string(void)
  88. {
  89. return (ARCHIVE_VERSION_STRING);
  90. }
  91. int
  92. archive_errno(struct archive *a)
  93. {
  94. return (a->archive_error_number);
  95. }
  96. const char *
  97. archive_error_string(struct archive *a)
  98. {
  99. if (a->error != NULL && *a->error != '\0')
  100. return (a->error);
  101. else
  102. return (NULL);
  103. }
  104. int
  105. archive_file_count(struct archive *a)
  106. {
  107. return (a->file_count);
  108. }
  109. int
  110. archive_format(struct archive *a)
  111. {
  112. return (a->archive_format);
  113. }
  114. const char *
  115. archive_format_name(struct archive *a)
  116. {
  117. return (a->archive_format_name);
  118. }
  119. int
  120. archive_compression(struct archive *a)
  121. {
  122. return archive_filter_code(a, 0);
  123. }
  124. const char *
  125. archive_compression_name(struct archive *a)
  126. {
  127. return archive_filter_name(a, 0);
  128. }
  129. /*
  130. * Return a count of the number of compressed bytes processed.
  131. */
  132. la_int64_t
  133. archive_position_compressed(struct archive *a)
  134. {
  135. return archive_filter_bytes(a, -1);
  136. }
  137. /*
  138. * Return a count of the number of uncompressed bytes processed.
  139. */
  140. la_int64_t
  141. archive_position_uncompressed(struct archive *a)
  142. {
  143. return archive_filter_bytes(a, 0);
  144. }
  145. void
  146. archive_clear_error(struct archive *a)
  147. {
  148. archive_string_empty(&a->error_string);
  149. a->error = NULL;
  150. a->archive_error_number = 0;
  151. }
  152. void
  153. archive_set_error(struct archive *a, int error_number, const char *fmt, ...)
  154. {
  155. va_list ap;
  156. a->archive_error_number = error_number;
  157. if (fmt == NULL) {
  158. a->error = NULL;
  159. return;
  160. }
  161. archive_string_empty(&(a->error_string));
  162. va_start(ap, fmt);
  163. archive_string_vsprintf(&(a->error_string), fmt, ap);
  164. va_end(ap);
  165. a->error = a->error_string.s;
  166. }
  167. void
  168. archive_copy_error(struct archive *dest, struct archive *src)
  169. {
  170. dest->archive_error_number = src->archive_error_number;
  171. archive_string_copy(&dest->error_string, &src->error_string);
  172. dest->error = dest->error_string.s;
  173. }
  174. void
  175. __archive_errx(int retvalue, const char *msg)
  176. {
  177. static const char msg1[] = "Fatal Internal Error in libarchive: ";
  178. size_t s;
  179. s = write(2, msg1, strlen(msg1));
  180. (void)s; /* UNUSED */
  181. s = write(2, msg, strlen(msg));
  182. (void)s; /* UNUSED */
  183. s = write(2, "\n", 1);
  184. (void)s; /* UNUSED */
  185. exit(retvalue);
  186. }
  187. /*
  188. * Create a temporary file
  189. */
  190. #if defined(_WIN32) && !defined(__CYGWIN__)
  191. /*
  192. * Do not use Windows tmpfile() function.
  193. * It will make a temporary file under the root directory
  194. * and it'll cause permission error if a user who is
  195. * non-Administrator creates temporary files.
  196. * Also Windows version of mktemp family including _mktemp_s
  197. * are not secure.
  198. */
  199. static int
  200. __archive_mktempx(const char *tmpdir, wchar_t *template)
  201. {
  202. static const wchar_t prefix[] = L"libarchive_";
  203. static const wchar_t suffix[] = L"XXXXXXXXXX";
  204. static const wchar_t num[] = {
  205. L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7',
  206. L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F',
  207. L'G', L'H', L'I', L'J', L'K', L'L', L'M', L'N',
  208. L'O', L'P', L'Q', L'R', L'S', L'T', L'U', L'V',
  209. L'W', L'X', L'Y', L'Z', L'a', L'b', L'c', L'd',
  210. L'e', L'f', L'g', L'h', L'i', L'j', L'k', L'l',
  211. L'm', L'n', L'o', L'p', L'q', L'r', L's', L't',
  212. L'u', L'v', L'w', L'x', L'y', L'z'
  213. };
  214. struct archive_wstring temp_name;
  215. wchar_t *ws;
  216. DWORD attr;
  217. wchar_t *xp, *ep;
  218. int fd;
  219. #if defined(HAVE_BCRYPT_H) && _WIN32_WINNT >= _WIN32_WINNT_VISTA
  220. BCRYPT_ALG_HANDLE hAlg = NULL;
  221. #else
  222. HCRYPTPROV hProv = (HCRYPTPROV)NULL;
  223. #endif
  224. fd = -1;
  225. ws = NULL;
  226. archive_string_init(&temp_name);
  227. if (template == NULL) {
  228. /* Get a temporary directory. */
  229. if (tmpdir == NULL) {
  230. size_t l;
  231. wchar_t *tmp;
  232. l = GetTempPathW(0, NULL);
  233. if (l == 0) {
  234. la_dosmaperr(GetLastError());
  235. goto exit_tmpfile;
  236. }
  237. tmp = malloc(l*sizeof(wchar_t));
  238. if (tmp == NULL) {
  239. errno = ENOMEM;
  240. goto exit_tmpfile;
  241. }
  242. GetTempPathW((DWORD)l, tmp);
  243. archive_wstrcpy(&temp_name, tmp);
  244. free(tmp);
  245. } else {
  246. if (archive_wstring_append_from_mbs(&temp_name, tmpdir,
  247. strlen(tmpdir)) < 0)
  248. goto exit_tmpfile;
  249. if (temp_name.length == 0 ||
  250. temp_name.s[temp_name.length-1] != L'/')
  251. archive_wstrappend_wchar(&temp_name, L'/');
  252. }
  253. /* Check if temp_name is a directory. */
  254. attr = GetFileAttributesW(temp_name.s);
  255. if (attr == (DWORD)-1) {
  256. if (GetLastError() != ERROR_FILE_NOT_FOUND) {
  257. la_dosmaperr(GetLastError());
  258. goto exit_tmpfile;
  259. }
  260. ws = __la_win_permissive_name_w(temp_name.s);
  261. if (ws == NULL) {
  262. errno = EINVAL;
  263. goto exit_tmpfile;
  264. }
  265. attr = GetFileAttributesW(ws);
  266. if (attr == (DWORD)-1) {
  267. la_dosmaperr(GetLastError());
  268. goto exit_tmpfile;
  269. }
  270. }
  271. if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) {
  272. errno = ENOTDIR;
  273. goto exit_tmpfile;
  274. }
  275. /*
  276. * Create a temporary file.
  277. */
  278. archive_wstrcat(&temp_name, prefix);
  279. archive_wstrcat(&temp_name, suffix);
  280. ep = temp_name.s + archive_strlen(&temp_name);
  281. xp = ep - wcslen(suffix);
  282. template = temp_name.s;
  283. } else {
  284. xp = wcschr(template, L'X');
  285. if (xp == NULL) /* No X, programming error */
  286. abort();
  287. for (ep = xp; *ep == L'X'; ep++)
  288. continue;
  289. if (*ep) /* X followed by non X, programming error */
  290. abort();
  291. }
  292. #if defined(HAVE_BCRYPT_H) && _WIN32_WINNT >= _WIN32_WINNT_VISTA
  293. if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_RNG_ALGORITHM,
  294. NULL, 0))) {
  295. la_dosmaperr(GetLastError());
  296. goto exit_tmpfile;
  297. }
  298. #else
  299. if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL,
  300. CRYPT_VERIFYCONTEXT)) {
  301. la_dosmaperr(GetLastError());
  302. goto exit_tmpfile;
  303. }
  304. #endif
  305. for (;;) {
  306. wchar_t *p;
  307. HANDLE h;
  308. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  309. CREATEFILE2_EXTENDED_PARAMETERS createExParams;
  310. #endif
  311. /* Generate a random file name through CryptGenRandom(). */
  312. p = xp;
  313. #if defined(HAVE_BCRYPT_H) && _WIN32_WINNT >= _WIN32_WINNT_VISTA
  314. if (!BCRYPT_SUCCESS(BCryptGenRandom(hAlg, (PUCHAR)p,
  315. (DWORD)(ep - p)*sizeof(wchar_t), 0))) {
  316. la_dosmaperr(GetLastError());
  317. goto exit_tmpfile;
  318. }
  319. #else
  320. if (!CryptGenRandom(hProv, (DWORD)(ep - p)*sizeof(wchar_t),
  321. (BYTE*)p)) {
  322. la_dosmaperr(GetLastError());
  323. goto exit_tmpfile;
  324. }
  325. #endif
  326. for (; p < ep; p++)
  327. *p = num[((DWORD)*p) % (sizeof(num)/sizeof(num[0]))];
  328. free(ws);
  329. ws = __la_win_permissive_name_w(template);
  330. if (ws == NULL) {
  331. errno = EINVAL;
  332. goto exit_tmpfile;
  333. }
  334. if (template == temp_name.s) {
  335. attr = FILE_ATTRIBUTE_TEMPORARY |
  336. FILE_FLAG_DELETE_ON_CLOSE;
  337. } else {
  338. /* mkstemp */
  339. attr = FILE_ATTRIBUTE_NORMAL;
  340. }
  341. # if _WIN32_WINNT >= 0x0602 /* _WIN32_WINNT_WIN8 */
  342. ZeroMemory(&createExParams, sizeof(createExParams));
  343. createExParams.dwSize = sizeof(createExParams);
  344. createExParams.dwFileAttributes = attr & 0xFFFF;
  345. createExParams.dwFileFlags = attr & 0xFFF00000;
  346. h = CreateFile2(ws,
  347. GENERIC_READ | GENERIC_WRITE | DELETE,
  348. 0,/* Not share */
  349. CREATE_NEW,
  350. &createExParams);
  351. #else
  352. h = CreateFileW(ws,
  353. GENERIC_READ | GENERIC_WRITE | DELETE,
  354. 0,/* Not share */
  355. NULL,
  356. CREATE_NEW,/* Create a new file only */
  357. attr,
  358. NULL);
  359. #endif
  360. if (h == INVALID_HANDLE_VALUE) {
  361. /* The same file already exists. retry with
  362. * a new filename. */
  363. if (GetLastError() == ERROR_FILE_EXISTS)
  364. continue;
  365. /* Otherwise, fail creation temporary file. */
  366. la_dosmaperr(GetLastError());
  367. goto exit_tmpfile;
  368. }
  369. fd = _open_osfhandle((intptr_t)h, _O_BINARY | _O_RDWR);
  370. if (fd == -1) {
  371. la_dosmaperr(GetLastError());
  372. CloseHandle(h);
  373. goto exit_tmpfile;
  374. } else
  375. break;/* success! */
  376. }
  377. exit_tmpfile:
  378. #if defined(HAVE_BCRYPT_H) && _WIN32_WINNT >= _WIN32_WINNT_VISTA
  379. if (hAlg != NULL)
  380. BCryptCloseAlgorithmProvider(hAlg, 0);
  381. #else
  382. if (hProv != (HCRYPTPROV)NULL)
  383. CryptReleaseContext(hProv, 0);
  384. #endif
  385. free(ws);
  386. if (template == temp_name.s)
  387. archive_wstring_free(&temp_name);
  388. return (fd);
  389. }
  390. int
  391. __archive_mktemp(const char *tmpdir)
  392. {
  393. return __archive_mktempx(tmpdir, NULL);
  394. }
  395. int
  396. __archive_mkstemp(wchar_t *template)
  397. {
  398. return __archive_mktempx(NULL, template);
  399. }
  400. #else
  401. static int
  402. get_tempdir(struct archive_string *temppath)
  403. {
  404. const char *tmp;
  405. tmp = getenv("TMPDIR");
  406. if (tmp == NULL)
  407. #ifdef _PATH_TMP
  408. tmp = _PATH_TMP;
  409. #else
  410. tmp = "/tmp";
  411. #endif
  412. archive_strcpy(temppath, tmp);
  413. if (temppath->length == 0 || temppath->s[temppath->length-1] != '/')
  414. archive_strappend_char(temppath, '/');
  415. return (ARCHIVE_OK);
  416. }
  417. #if defined(HAVE_MKSTEMP)
  418. /*
  419. * We can use mkstemp().
  420. */
  421. int
  422. __archive_mktemp(const char *tmpdir)
  423. {
  424. struct archive_string temp_name;
  425. int fd = -1;
  426. archive_string_init(&temp_name);
  427. if (tmpdir == NULL) {
  428. if (get_tempdir(&temp_name) != ARCHIVE_OK)
  429. goto exit_tmpfile;
  430. } else {
  431. archive_strcpy(&temp_name, tmpdir);
  432. if (temp_name.length == 0 ||
  433. temp_name.s[temp_name.length-1] != '/')
  434. archive_strappend_char(&temp_name, '/');
  435. }
  436. #ifdef O_TMPFILE
  437. fd = open(temp_name.s, O_RDWR|O_CLOEXEC|O_TMPFILE|O_EXCL, 0600);
  438. if(fd >= 0)
  439. goto exit_tmpfile;
  440. #endif
  441. archive_strcat(&temp_name, "libarchive_XXXXXX");
  442. fd = mkstemp(temp_name.s);
  443. if (fd < 0)
  444. goto exit_tmpfile;
  445. __archive_ensure_cloexec_flag(fd);
  446. unlink(temp_name.s);
  447. exit_tmpfile:
  448. archive_string_free(&temp_name);
  449. return (fd);
  450. }
  451. int
  452. __archive_mkstemp(char *template)
  453. {
  454. int fd = -1;
  455. fd = mkstemp(template);
  456. if (fd >= 0)
  457. __archive_ensure_cloexec_flag(fd);
  458. return (fd);
  459. }
  460. #else /* !HAVE_MKSTEMP */
  461. /*
  462. * We use a private routine.
  463. */
  464. static int
  465. __archive_mktempx(const char *tmpdir, char *template)
  466. {
  467. static const char num[] = {
  468. '0', '1', '2', '3', '4', '5', '6', '7',
  469. '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
  470. 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
  471. 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
  472. 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
  473. 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
  474. 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  475. 'u', 'v', 'w', 'x', 'y', 'z'
  476. };
  477. struct archive_string temp_name;
  478. struct stat st;
  479. int fd;
  480. char *tp, *ep;
  481. fd = -1;
  482. if (template == NULL) {
  483. archive_string_init(&temp_name);
  484. if (tmpdir == NULL) {
  485. if (get_tempdir(&temp_name) != ARCHIVE_OK)
  486. goto exit_tmpfile;
  487. } else
  488. archive_strcpy(&temp_name, tmpdir);
  489. if (temp_name.length > 0 && temp_name.s[temp_name.length-1] == '/') {
  490. temp_name.s[temp_name.length-1] = '\0';
  491. temp_name.length --;
  492. }
  493. if (la_stat(temp_name.s, &st) < 0)
  494. goto exit_tmpfile;
  495. if (!S_ISDIR(st.st_mode)) {
  496. errno = ENOTDIR;
  497. goto exit_tmpfile;
  498. }
  499. archive_strcat(&temp_name, "/libarchive_");
  500. tp = temp_name.s + archive_strlen(&temp_name);
  501. archive_strcat(&temp_name, "XXXXXXXXXX");
  502. ep = temp_name.s + archive_strlen(&temp_name);
  503. template = temp_name.s;
  504. } else {
  505. tp = strchr(template, 'X');
  506. if (tp == NULL) /* No X, programming error */
  507. abort();
  508. for (ep = tp; *ep == 'X'; ep++)
  509. continue;
  510. if (*ep) /* X followed by non X, programming error */
  511. abort();
  512. }
  513. do {
  514. char *p;
  515. p = tp;
  516. archive_random(p, ep - p);
  517. while (p < ep) {
  518. int d = *((unsigned char *)p) % sizeof(num);
  519. *p++ = num[d];
  520. }
  521. fd = open(template, O_CREAT | O_EXCL | O_RDWR | O_CLOEXEC,
  522. 0600);
  523. } while (fd < 0 && errno == EEXIST);
  524. if (fd < 0)
  525. goto exit_tmpfile;
  526. __archive_ensure_cloexec_flag(fd);
  527. if (template == temp_name.s)
  528. unlink(temp_name.s);
  529. exit_tmpfile:
  530. if (template == temp_name.s)
  531. archive_string_free(&temp_name);
  532. return (fd);
  533. }
  534. int
  535. __archive_mktemp(const char *tmpdir)
  536. {
  537. return __archive_mktempx(tmpdir, NULL);
  538. }
  539. int
  540. __archive_mkstemp(char *template)
  541. {
  542. return __archive_mktempx(NULL, template);
  543. }
  544. #endif /* !HAVE_MKSTEMP */
  545. #endif /* !_WIN32 || __CYGWIN__ */
  546. /*
  547. * Set FD_CLOEXEC flag to a file descriptor if it is not set.
  548. * We have to set the flag if the platform does not provide O_CLOEXEC
  549. * or F_DUPFD_CLOEXEC flags.
  550. *
  551. * Note: This function is absolutely called after creating a new file
  552. * descriptor even if the platform seemingly provides O_CLOEXEC or
  553. * F_DUPFD_CLOEXEC macros because it is possible that the platform
  554. * merely declares those macros, especially Linux 2.6.18 - 2.6.24 do it.
  555. */
  556. void
  557. __archive_ensure_cloexec_flag(int fd)
  558. {
  559. #if defined(_WIN32) && !defined(__CYGWIN__)
  560. (void)fd; /* UNUSED */
  561. #else
  562. int flags;
  563. if (fd >= 0) {
  564. flags = fcntl(fd, F_GETFD);
  565. if (flags != -1 && (flags & FD_CLOEXEC) == 0)
  566. fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
  567. }
  568. #endif
  569. }
  570. /*
  571. * Utility function to sort a group of strings using quicksort.
  572. */
  573. static int
  574. archive_utility_string_sort_helper(char **strings, unsigned int n)
  575. {
  576. unsigned int i, lesser_count, greater_count;
  577. char **lesser, **greater, **tmp, *pivot;
  578. int retval1, retval2;
  579. /* A list of 0 or 1 elements is already sorted */
  580. if (n <= 1)
  581. return (ARCHIVE_OK);
  582. lesser_count = greater_count = 0;
  583. lesser = greater = NULL;
  584. pivot = strings[0];
  585. for (i = 1; i < n; i++)
  586. {
  587. if (strcmp(strings[i], pivot) < 0)
  588. {
  589. lesser_count++;
  590. tmp = realloc(lesser, lesser_count * sizeof(*tmp));
  591. if (!tmp) {
  592. free(greater);
  593. free(lesser);
  594. return (ARCHIVE_FATAL);
  595. }
  596. lesser = tmp;
  597. lesser[lesser_count - 1] = strings[i];
  598. }
  599. else
  600. {
  601. greater_count++;
  602. tmp = realloc(greater, greater_count * sizeof(*tmp));
  603. if (!tmp) {
  604. free(greater);
  605. free(lesser);
  606. return (ARCHIVE_FATAL);
  607. }
  608. greater = tmp;
  609. greater[greater_count - 1] = strings[i];
  610. }
  611. }
  612. /* quicksort(lesser) */
  613. retval1 = archive_utility_string_sort_helper(lesser, lesser_count);
  614. for (i = 0; i < lesser_count; i++)
  615. strings[i] = lesser[i];
  616. free(lesser);
  617. /* pivot */
  618. strings[lesser_count] = pivot;
  619. /* quicksort(greater) */
  620. retval2 = archive_utility_string_sort_helper(greater, greater_count);
  621. for (i = 0; i < greater_count; i++)
  622. strings[lesser_count + 1 + i] = greater[i];
  623. free(greater);
  624. return (retval1 < retval2) ? retval1 : retval2;
  625. }
  626. int
  627. archive_utility_string_sort(char **strings)
  628. {
  629. unsigned int size = 0;
  630. while (strings[size] != NULL)
  631. size++;
  632. return archive_utility_string_sort_helper(strings, size);
  633. }