lha_reader.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. Copyright (c) 2011, 2012, Simon Howard
  3. Permission to use, copy, modify, and/or distribute this software
  4. for any purpose with or without fee is hereby granted, provided
  5. that the above copyright notice and this permission notice appear
  6. in all copies.
  7. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  8. WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  9. WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  10. AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  11. CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  12. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  13. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "lha_arch.h"
  20. #include "lha_decoder.h"
  21. #include "lha_basic_reader.h"
  22. #include "public/lha_reader.h"
  23. #include "macbinary.h"
  24. typedef enum {
  25. // Initial state at start of stream:
  26. CURR_FILE_START,
  27. // Current file is a "normal" file (or directory) read from
  28. // the input stream.
  29. CURR_FILE_NORMAL,
  30. // Current file is a directory that has been popped from the
  31. // directory stack.
  32. CURR_FILE_FAKE_DIR,
  33. // Current file is a deferred symbolic link that has been left
  34. // to the end of the input stream to be created.
  35. CURR_FILE_DEFERRED_SYMLINK,
  36. // End of input stream has been reached.
  37. CURR_FILE_EOF,
  38. } CurrFileType;
  39. struct _LHAReader {
  40. LHABasicReader *reader;
  41. // The current file that we are processing (last file returned
  42. // by lha_reader_next_file).
  43. LHAFileHeader *curr_file;
  44. CurrFileType curr_file_type;
  45. // Pointer to decoder being used to decompress the current file,
  46. // or NULL if we have not yet started decompression.
  47. LHADecoder *decoder;
  48. // Pointer to "inner" decoder. Most of the time,
  49. // decoder == inner_decoder, but when decoding an archive
  50. // generated by MacLHA, inner_decoder points to the actual
  51. // decompressor.
  52. LHADecoder *inner_decoder;
  53. // Policy used to extract directories.
  54. LHAReaderDirPolicy dir_policy;
  55. // Directories that have been created by lha_reader_extract but
  56. // have not yet had their metadata set. This is a linked list
  57. // using the _next field in LHAFileHeader.
  58. // In the case of LHA_READER_DIR_END_OF_DIR this is a stack;
  59. // in the case of LHA_READER_DIR_END_OF_FILE it is a list.
  60. LHAFileHeader *dir_stack;
  61. // Symbolic links containing absolute paths or '..' are not
  62. // created immediately - instead, "placeholder" files are created
  63. // in their place, and the symbolic links created at the end
  64. // of extraction.
  65. LHAFileHeader *deferred_symlinks;
  66. };
  67. /**
  68. * Free the current decoder structure.
  69. *
  70. * If the reader has an allocated decoder being used to decompress the
  71. * current file, the decoder is freed and the decoder pointer reset
  72. * to NULL.
  73. *
  74. * @param reader Pointer to the LHA reader structure.
  75. */
  76. static void close_decoder(LHAReader *reader)
  77. {
  78. if (reader->decoder != NULL) {
  79. if (reader->inner_decoder == reader->decoder) {
  80. reader->inner_decoder = NULL;
  81. }
  82. lha_decoder_free(reader->decoder);
  83. reader->decoder = NULL;
  84. }
  85. if (reader->inner_decoder != NULL) {
  86. lha_decoder_free(reader->inner_decoder);
  87. reader->inner_decoder = NULL;
  88. }
  89. }
  90. /**
  91. * Create the decoder structure to decompress the data from the
  92. * current file.
  93. *
  94. * @param reader Pointer to the LHA reader structure.
  95. * @param callback Callback function to invoke to track progress.
  96. * @param callback_data Extra pointer to pass to the callback function.
  97. * @return Non-zero for success, zero for failure.
  98. */
  99. static int open_decoder(LHAReader *reader,
  100. LHADecoderProgressCallback callback,
  101. void *callback_data)
  102. {
  103. // Can only read from a normal file.
  104. if (reader->curr_file_type != CURR_FILE_NORMAL) {
  105. return 0;
  106. }
  107. reader->inner_decoder = lha_basic_reader_decode(reader->reader);
  108. if (reader->inner_decoder == NULL) {
  109. return 0;
  110. }
  111. // Set progress callback for decoder.
  112. if (callback != NULL) {
  113. lha_decoder_monitor(reader->inner_decoder,
  114. callback, callback_data);
  115. }
  116. // Some archives generated by MacLHA have a MacBinary header
  117. // attached to the start, which contains MacOS-specific
  118. // metadata about the compressed file. These are identified
  119. // and stripped off, using a "passthrough" decoder.
  120. if (reader->curr_file->os_type == LHA_OS_TYPE_MACOS) {
  121. reader->decoder = lha_macbinary_passthrough(
  122. reader->inner_decoder, reader->curr_file);
  123. if (reader->decoder == NULL) {
  124. return 0;
  125. }
  126. } else {
  127. reader->decoder = reader->inner_decoder;
  128. }
  129. return 1;
  130. }
  131. LHAReader *lha_reader_new(LHAInputStream *stream)
  132. {
  133. LHABasicReader *basic_reader;
  134. LHAReader *reader;
  135. reader = calloc(1, sizeof(LHAReader));
  136. if (reader == NULL) {
  137. return NULL;
  138. }
  139. basic_reader = lha_basic_reader_new(stream);
  140. if (basic_reader == NULL) {
  141. free(reader);
  142. return NULL;
  143. }
  144. reader->reader = basic_reader;
  145. reader->curr_file = NULL;
  146. reader->curr_file_type = CURR_FILE_START;
  147. reader->decoder = NULL;
  148. reader->inner_decoder = NULL;
  149. reader->dir_stack = NULL;
  150. reader->dir_policy = LHA_READER_DIR_END_OF_DIR;
  151. reader->deferred_symlinks = NULL;
  152. return reader;
  153. }
  154. void lha_reader_free(LHAReader *reader)
  155. {
  156. LHAFileHeader *header;
  157. // Shut down the current decoder, if there is one.
  158. close_decoder(reader);
  159. // Free any file headers in the stack.
  160. while (reader->dir_stack != NULL) {
  161. header = reader->dir_stack;
  162. reader->dir_stack = header->_next;
  163. lha_file_header_free(header);
  164. }
  165. lha_basic_reader_free(reader->reader);
  166. free(reader);
  167. }
  168. void lha_reader_set_dir_policy(LHAReader *reader,
  169. LHAReaderDirPolicy policy)
  170. {
  171. reader->dir_policy = policy;
  172. }
  173. /**
  174. * Check if the directory at the top of the stack should be popped.
  175. *
  176. * Extracting a directory is a two stage process; after the directory
  177. * is created, it is pushed onto the directory stack. Later the
  178. * directory must be popped off the stack and its metadata applied.
  179. *
  180. * @param reader Pointer to the LHA reader structure.
  181. * @return Non-zero if there is a directory at the top of
  182. * the stack that should be popped.
  183. */
  184. static int end_of_top_dir(LHAReader *reader)
  185. {
  186. LHAFileHeader *input;
  187. // No directories to pop?
  188. if (reader->dir_stack == NULL) {
  189. return 0;
  190. }
  191. // Once the end of the input stream is reached, all that is
  192. // left to do is pop off the remaining directories.
  193. input = lha_basic_reader_curr_file(reader->reader);
  194. if (input == NULL) {
  195. return 1;
  196. }
  197. switch (reader->dir_policy) {
  198. // Shouldn't happen?
  199. case LHA_READER_DIR_PLAIN:
  200. default:
  201. return 1;
  202. // Don't process directories until we reach the end of
  203. // the input stream.
  204. case LHA_READER_DIR_END_OF_FILE:
  205. return 0;
  206. // Once we reach a file from the input that is not within
  207. // the directory at the top of the stack, we have reached
  208. // the end of that directory, so we can pop it off.
  209. case LHA_READER_DIR_END_OF_DIR:
  210. return input->path == NULL
  211. || strncmp(input->path,
  212. reader->dir_stack->path,
  213. strlen(reader->dir_stack->path)) != 0;
  214. }
  215. }
  216. // Read the next file from the input stream.
  217. LHAFileHeader *lha_reader_next_file(LHAReader *reader)
  218. {
  219. // Free the current decoder if there is one.
  220. close_decoder(reader);
  221. // No point continuing once the end of the input stream has
  222. // been reached.
  223. if (reader->curr_file_type == CURR_FILE_EOF) {
  224. return NULL;
  225. }
  226. // Advance to the next file from the input stream?
  227. // Don't advance until we've done the fake directories first.
  228. if (reader->curr_file_type == CURR_FILE_START
  229. || reader->curr_file_type == CURR_FILE_NORMAL) {
  230. lha_basic_reader_next_file(reader->reader);
  231. }
  232. // If the last file we returned was a 'fake' directory, we must
  233. // now unreference it.
  234. if (reader->curr_file_type == CURR_FILE_FAKE_DIR) {
  235. lha_file_header_free(reader->curr_file);
  236. }
  237. // Pop off all appropriate directories from the stack first.
  238. if (end_of_top_dir(reader)) {
  239. reader->curr_file = reader->dir_stack;
  240. reader->dir_stack = reader->dir_stack->_next;
  241. reader->curr_file_type = CURR_FILE_FAKE_DIR;
  242. } else {
  243. reader->curr_file = lha_basic_reader_curr_file(reader->reader);
  244. reader->curr_file_type = CURR_FILE_NORMAL;
  245. }
  246. // Once we reach the end of the file, there may be deferred
  247. // symbolic links still to extract, so process those before
  248. // giving up and declaring end of file.
  249. if (reader->curr_file == NULL) {
  250. if (reader->deferred_symlinks != NULL) {
  251. reader->curr_file = reader->deferred_symlinks;
  252. reader->curr_file_type = CURR_FILE_DEFERRED_SYMLINK;
  253. reader->deferred_symlinks =
  254. reader->deferred_symlinks->_next;
  255. reader->curr_file->_next = NULL;
  256. } else {
  257. reader->curr_file_type = CURR_FILE_EOF;
  258. }
  259. }
  260. return reader->curr_file;
  261. }
  262. size_t lha_reader_read(LHAReader *reader, void *buf, size_t buf_len)
  263. {
  264. // The first time that we try to read the current file, we
  265. // must create the decoder to decompress it.
  266. if (reader->decoder == NULL) {
  267. if (!open_decoder(reader, NULL, NULL)) {
  268. return 0;
  269. }
  270. }
  271. // Read from decoder and return the result.
  272. return lha_decoder_read(reader->decoder, buf, buf_len);
  273. }
  274. /**
  275. * Decompress the current file.
  276. *
  277. * Assumes that @param open_decoder has already been called to
  278. * start the decode process.
  279. *
  280. * @param reader Pointer to the LHA reader structure.
  281. * @param output FILE handle to write decompressed data, or NULL
  282. * if the decompressed data should be discarded.
  283. * @return Non-zero if the file decompressed successfully.
  284. */
  285. static int do_decode(LHAReader *reader, FILE *output)
  286. {
  287. uint8_t buf[64];
  288. unsigned int bytes;
  289. // Decompress the current file.
  290. do {
  291. bytes = lha_reader_read(reader, buf, sizeof(buf));
  292. if (output != NULL) {
  293. if (fwrite(buf, 1, bytes, output) < bytes) {
  294. return 0;
  295. }
  296. }
  297. } while (bytes > 0);
  298. // Decoder stores output position and performs running CRC.
  299. // At the end of the stream these should match the header values.
  300. return lha_decoder_get_length(reader->inner_decoder)
  301. == reader->curr_file->length
  302. && lha_decoder_get_crc(reader->inner_decoder)
  303. == reader->curr_file->crc;
  304. }
  305. int lha_reader_check(LHAReader *reader,
  306. LHADecoderProgressCallback callback,
  307. void *callback_data)
  308. {
  309. if (reader->curr_file_type != CURR_FILE_NORMAL) {
  310. return 0;
  311. }
  312. // CRC checking of directories is not necessary.
  313. if (!strcmp(reader->curr_file->compress_method,
  314. LHA_COMPRESS_TYPE_DIR)) {
  315. return 1;
  316. }
  317. // Decode file.
  318. return open_decoder(reader, callback, callback_data)
  319. && do_decode(reader, NULL);
  320. }
  321. /**
  322. * Open an output stream into which to decompress the current file.
  323. *
  324. * @param reader Pointer to the LHA reader structure.
  325. * @param filename Name of the file to open.
  326. * @return FILE handle of the opened file, or NULL in
  327. * case of failure.
  328. */
  329. static FILE *open_output_file(LHAReader *reader, char *filename)
  330. {
  331. int unix_uid = -1, unix_gid = -1, unix_perms = -1;
  332. if (LHA_FILE_HAVE_EXTRA(reader->curr_file, LHA_FILE_UNIX_UID_GID)) {
  333. unix_uid = reader->curr_file->unix_uid;
  334. unix_gid = reader->curr_file->unix_gid;
  335. }
  336. if (LHA_FILE_HAVE_EXTRA(reader->curr_file, LHA_FILE_UNIX_PERMS)) {
  337. unix_perms = reader->curr_file->unix_perms;
  338. }
  339. return lha_arch_fopen(filename, unix_uid, unix_gid, unix_perms);
  340. }
  341. /**
  342. * Set file timestamps for the specified file.
  343. *
  344. * If possible, the more accurate Windows timestamp values are used;
  345. * otherwise normal Unix timestamps are used.
  346. *
  347. * @param path Path to the file or directory to set.
  348. * @param header Pointer to file header structure containing the
  349. * timestamps to set.
  350. * @return Non-zero if the timestamps were set successfully,
  351. * or zero for failure.
  352. */
  353. static int set_timestamps_from_header(char *path, LHAFileHeader *header)
  354. {
  355. #if LHA_ARCH == LHA_ARCH_WINDOWS
  356. if (LHA_FILE_HAVE_EXTRA(header, LHA_FILE_WINDOWS_TIMESTAMPS)) {
  357. return lha_arch_set_windows_timestamps(
  358. path,
  359. header->win_creation_time,
  360. header->win_modification_time,
  361. header->win_access_time
  362. );
  363. } else // ....
  364. #endif
  365. if (header->timestamp != 0) {
  366. return lha_arch_utime(path, header->timestamp);
  367. } else {
  368. return 1;
  369. }
  370. }
  371. /**
  372. * Set directory metadata.
  373. *
  374. * This is the second stage of directory extraction. Metadata (timestamps
  375. * and permissions) should be set on a dictory after the contents of
  376. * the directory has been extracted.
  377. *
  378. * @param header Pointer to file header structure containing the
  379. * metadata to set.
  380. * @param path Path to the directory on which to set the metadata.
  381. * @return Non-zero for success, or zero for failure.
  382. */
  383. static int set_directory_metadata(LHAFileHeader *header, char *path)
  384. {
  385. // Set timestamp:
  386. set_timestamps_from_header(path, header);
  387. // Set owner and group:
  388. if (LHA_FILE_HAVE_EXTRA(header, LHA_FILE_UNIX_UID_GID)) {
  389. if (!lha_arch_chown(path, header->unix_uid,
  390. header->unix_gid)) {
  391. // On most Unix systems, only root can change
  392. // ownership. But if we can't change ownership,
  393. // it isn't a fatal error. Ignore the failure
  394. // and continue.
  395. // TODO: Implement some kind of alternate handling
  396. // here?
  397. /* return 0; */
  398. }
  399. }
  400. // Set permissions on directory:
  401. if (LHA_FILE_HAVE_EXTRA(header, LHA_FILE_UNIX_PERMS)) {
  402. if (!lha_arch_chmod(path, header->unix_perms)) {
  403. return 0;
  404. }
  405. }
  406. return 1;
  407. }
  408. /**
  409. * "Extract" (create) a directory.
  410. *
  411. * The current file is assumed to be a directory. This is the first
  412. * stage in extracting a directory; after the directory is created,
  413. * it is added to the directory stack so that the metadata apply stage
  414. * runs later. (If the LHA_READER_DIR_PLAIN policy is used, metadata
  415. * is just applied now).
  416. *
  417. * @param reader Pointer to the LHA reader structure.
  418. * @param path Path to the directory, or NULL to use the path from
  419. * the file header.
  420. * @return Non-zero for success, or zero for failure.
  421. */
  422. static int extract_directory(LHAReader *reader, char *path)
  423. {
  424. LHAFileHeader *header;
  425. unsigned int mode;
  426. header = reader->curr_file;
  427. // If path is not specified, use the path from the file header.
  428. if (path == NULL) {
  429. path = header->path;
  430. }
  431. // Create directory. If there are permissions to be set, create
  432. // the directory with minimal permissions limited to the running
  433. // user. Otherwise use the default umask.
  434. if (LHA_FILE_HAVE_EXTRA(header, LHA_FILE_UNIX_PERMS)) {
  435. mode = 0700;
  436. } else {
  437. mode = 0777;
  438. }
  439. if (!lha_arch_mkdir(path, mode)) {
  440. // If the attempt to create the directory failed, it may
  441. // be because the directory already exists. Return success
  442. // if this is the case; it isn't really an error.
  443. return lha_arch_exists(path) == LHA_FILE_DIRECTORY;
  444. }
  445. // The directory has been created, but the metadata has not yet
  446. // been applied. It depends on the directory policy how this
  447. // is handled. If we are using LHA_READER_DIR_PLAIN, set
  448. // metadata now. Otherwise, save the directory for later.
  449. if (reader->dir_policy == LHA_READER_DIR_PLAIN) {
  450. set_directory_metadata(header, path);
  451. } else {
  452. lha_file_header_add_ref(header);
  453. header->_next = reader->dir_stack;
  454. reader->dir_stack = header;
  455. }
  456. return 1;
  457. }
  458. /**
  459. * Extract the current file.
  460. *
  461. * @param reader Pointer to the LHA reader structure.
  462. * @param filename Filename into which to extract the file, or NULL
  463. * to use the filename from the file header.
  464. * @param callback Callback function to invoke to track progress.
  465. * @param callback_data Extra pointer to pass to the callback function.
  466. * @return Non-zero if the file was successfully extracted,
  467. * or zero for failure.
  468. */
  469. static int extract_file(LHAReader *reader, char *filename,
  470. LHADecoderProgressCallback callback,
  471. void *callback_data)
  472. {
  473. FILE *fstream;
  474. char *tmp_filename = NULL;
  475. int result;
  476. // Construct filename?
  477. if (filename == NULL) {
  478. tmp_filename = lha_file_header_full_path(reader->curr_file);
  479. if (tmp_filename == NULL) {
  480. return 0;
  481. }
  482. filename = tmp_filename;
  483. }
  484. // Create decoder. If the file cannot be created, there is no
  485. // need to even create an output file. If successful, open the
  486. // output file and decode.
  487. result = 0;
  488. if (open_decoder(reader, callback, callback_data)) {
  489. fstream = open_output_file(reader, filename);
  490. if (fstream != NULL) {
  491. result = do_decode(reader, fstream);
  492. fclose(fstream);
  493. }
  494. }
  495. // Set timestamp on file:
  496. if (result) {
  497. set_timestamps_from_header(filename, reader->curr_file);
  498. }
  499. free(tmp_filename);
  500. return result;
  501. }
  502. /**
  503. * Determine whether a header contains a "dangerous" symbolic link.
  504. *
  505. * Symbolic links that begin with '/' or contain '..' as a path are
  506. * Potentially dangerous and could potentially be used to overwrite
  507. * arbitrary files on the filesystem. They therefore need to be
  508. * treated specially.
  509. *
  510. * @param header Pointer to a header structure defining a symbolic
  511. * link.
  512. * @return Non-zero if the symbolic link is potentially
  513. * dangerous.
  514. */
  515. static int is_dangerous_symlink(LHAFileHeader *header)
  516. {
  517. char *path_start;
  518. char *p;
  519. if (header->symlink_target == NULL) {
  520. return 0;
  521. }
  522. // Absolute path symlinks could be used to point to arbitrary
  523. // filesystem locations.
  524. if (header->symlink_target[0] == '/') {
  525. return 1;
  526. }
  527. // Check for paths containing '..'.
  528. path_start = header->symlink_target;
  529. for (p = header->symlink_target; *p != '\0'; ++p) {
  530. if (*p == '/') {
  531. if ((p - path_start) == 2
  532. && path_start[0] == '.' && path_start[1] == '.') {
  533. return 1;
  534. }
  535. path_start = p + 1;
  536. }
  537. }
  538. // The path might also end with '..' (no terminating /)
  539. if ((p - path_start) == 2
  540. && path_start[0] == '.' && path_start[1] == '.') {
  541. return 1;
  542. }
  543. return 0;
  544. }
  545. /**
  546. * Get the length of a path defined by a file header.
  547. *
  548. * @param header The file header structure.
  549. * @return Length of the header in bytes.
  550. */
  551. static size_t file_header_path_len(LHAFileHeader *header)
  552. {
  553. size_t result;
  554. result = 0;
  555. if (header->path != NULL) {
  556. result += strlen(header->path);
  557. }
  558. if (header->filename != NULL) {
  559. result += strlen(header->filename);
  560. }
  561. return result;
  562. }
  563. /**
  564. * Create a "placeholder" symbolic link.
  565. *
  566. * When a "dangerous" symbolic link is extracted, instead of creating it
  567. * immediately, create a "placeholder" empty file to go in its place, and
  568. * place it into the deferred_symlinks list to be created later.
  569. *
  570. * @param reader Pointer to the LHA reader structure.
  571. * @param filename Filename into which to extract the symlink.
  572. * @return Non-zero if the symlink was extracted successfully,
  573. * or zero for failure.
  574. */
  575. static int extract_placeholder_symlink(LHAReader *reader, char *filename)
  576. {
  577. LHAFileHeader **rover;
  578. FILE *f;
  579. f = lha_arch_fopen(filename, -1, -1, 0600);
  580. if (f == NULL) {
  581. return 0;
  582. }
  583. fclose(f);
  584. // Insert this header into the list of deferred symbolic links.
  585. // The list must be maintained in order of decreasing path length,
  586. // so that one symbolic link cannot depend on another. For example:
  587. //
  588. // etc -> /etc
  589. // etc/passwd -> /malicious_path/passwd
  590. rover = &reader->deferred_symlinks;
  591. while (*rover != NULL
  592. && file_header_path_len(*rover)
  593. > file_header_path_len(reader->curr_file)) {
  594. rover = &(*rover)->_next;
  595. }
  596. reader->curr_file->_next = *rover;
  597. *rover = reader->curr_file;
  598. // Save reference to the header so it won't be freed.
  599. lha_file_header_add_ref(reader->curr_file);
  600. return 1;
  601. }
  602. /**
  603. * Extract a Unix symbolic link.
  604. *
  605. * @param reader Pointer to the LHA reader structure.
  606. * @param filename Filename into which to extract the symlink, or NULL
  607. * to use the filename from the file header.
  608. * @return Non-zero if the symlink was extracted successfully,
  609. * or zero for failure.
  610. */
  611. static int extract_symlink(LHAReader *reader, char *filename)
  612. {
  613. char *tmp_filename = NULL;
  614. int result;
  615. // Construct filename?
  616. if (filename == NULL) {
  617. tmp_filename = lha_file_header_full_path(reader->curr_file);
  618. if (tmp_filename == NULL) {
  619. return 0;
  620. }
  621. filename = tmp_filename;
  622. }
  623. if (reader->curr_file_type == CURR_FILE_NORMAL
  624. && is_dangerous_symlink(reader->curr_file)) {
  625. return extract_placeholder_symlink(reader, filename);
  626. }
  627. result = lha_arch_symlink(filename, reader->curr_file->symlink_target);
  628. // TODO: Set symlink timestamp.
  629. free(tmp_filename);
  630. return result;
  631. }
  632. /**
  633. * Extract a "normal" file.
  634. *
  635. * This just extracts the file header most recently read by the
  636. * BasicReader.
  637. *
  638. * @param reader Pointer to the LHA reader structure.
  639. * @param filename Filename into which to extract the file, or NULL
  640. * to use the filename from the file header.
  641. * @param callback Callback function to invoke to track progress.
  642. * @param callback_data Extra pointer to pass to the callback function.
  643. * @return Non-zero if the file was successfully extracted,
  644. * or zero for failure.
  645. */
  646. static int extract_normal(LHAReader *reader,
  647. char *filename,
  648. LHADecoderProgressCallback callback,
  649. void *callback_data)
  650. {
  651. if (strcmp(reader->curr_file->compress_method,
  652. LHA_COMPRESS_TYPE_DIR) != 0) {
  653. return extract_file(reader, filename, callback, callback_data);
  654. } else if (reader->curr_file->symlink_target != NULL) {
  655. return extract_symlink(reader, filename);
  656. } else {
  657. return extract_directory(reader, filename);
  658. }
  659. }
  660. int lha_reader_extract(LHAReader *reader,
  661. char *filename,
  662. LHADecoderProgressCallback callback,
  663. void *callback_data)
  664. {
  665. switch (reader->curr_file_type) {
  666. case CURR_FILE_NORMAL:
  667. return extract_normal(reader, filename, callback,
  668. callback_data);
  669. case CURR_FILE_FAKE_DIR:
  670. if (filename == NULL) {
  671. filename = reader->curr_file->path;
  672. }
  673. set_directory_metadata(reader->curr_file, filename);
  674. return 1;
  675. case CURR_FILE_DEFERRED_SYMLINK:
  676. return extract_symlink(reader, filename);
  677. case CURR_FILE_START:
  678. case CURR_FILE_EOF:
  679. break;
  680. }
  681. return 0;
  682. }
  683. int lha_reader_current_is_fake(LHAReader *reader)
  684. {
  685. return reader->curr_file_type == CURR_FILE_FAKE_DIR
  686. || reader->curr_file_type == CURR_FILE_DEFERRED_SYMLINK;
  687. }