archive_read_support_format_cpio.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2010-2012 Michihiro NAKAJIMA
  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_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. /* #include <stdint.h> */ /* See archive_platform.h */
  31. #ifdef HAVE_STDLIB_H
  32. #include <stdlib.h>
  33. #endif
  34. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. #include "archive.h"
  38. #include "archive_entry.h"
  39. #include "archive_entry_locale.h"
  40. #include "archive_private.h"
  41. #include "archive_read_private.h"
  42. #define bin_magic_offset 0
  43. #define bin_magic_size 2
  44. #define bin_dev_offset 2
  45. #define bin_dev_size 2
  46. #define bin_ino_offset 4
  47. #define bin_ino_size 2
  48. #define bin_mode_offset 6
  49. #define bin_mode_size 2
  50. #define bin_uid_offset 8
  51. #define bin_uid_size 2
  52. #define bin_gid_offset 10
  53. #define bin_gid_size 2
  54. #define bin_nlink_offset 12
  55. #define bin_nlink_size 2
  56. #define bin_rdev_offset 14
  57. #define bin_rdev_size 2
  58. #define bin_mtime_offset 16
  59. #define bin_mtime_size 4
  60. #define bin_namesize_offset 20
  61. #define bin_namesize_size 2
  62. #define bin_filesize_offset 22
  63. #define bin_filesize_size 4
  64. #define bin_header_size 26
  65. #define odc_magic_offset 0
  66. #define odc_magic_size 6
  67. #define odc_dev_offset 6
  68. #define odc_dev_size 6
  69. #define odc_ino_offset 12
  70. #define odc_ino_size 6
  71. #define odc_mode_offset 18
  72. #define odc_mode_size 6
  73. #define odc_uid_offset 24
  74. #define odc_uid_size 6
  75. #define odc_gid_offset 30
  76. #define odc_gid_size 6
  77. #define odc_nlink_offset 36
  78. #define odc_nlink_size 6
  79. #define odc_rdev_offset 42
  80. #define odc_rdev_size 6
  81. #define odc_mtime_offset 48
  82. #define odc_mtime_size 11
  83. #define odc_namesize_offset 59
  84. #define odc_namesize_size 6
  85. #define odc_filesize_offset 65
  86. #define odc_filesize_size 11
  87. #define odc_header_size 76
  88. #define newc_magic_offset 0
  89. #define newc_magic_size 6
  90. #define newc_ino_offset 6
  91. #define newc_ino_size 8
  92. #define newc_mode_offset 14
  93. #define newc_mode_size 8
  94. #define newc_uid_offset 22
  95. #define newc_uid_size 8
  96. #define newc_gid_offset 30
  97. #define newc_gid_size 8
  98. #define newc_nlink_offset 38
  99. #define newc_nlink_size 8
  100. #define newc_mtime_offset 46
  101. #define newc_mtime_size 8
  102. #define newc_filesize_offset 54
  103. #define newc_filesize_size 8
  104. #define newc_devmajor_offset 62
  105. #define newc_devmajor_size 8
  106. #define newc_devminor_offset 70
  107. #define newc_devminor_size 8
  108. #define newc_rdevmajor_offset 78
  109. #define newc_rdevmajor_size 8
  110. #define newc_rdevminor_offset 86
  111. #define newc_rdevminor_size 8
  112. #define newc_namesize_offset 94
  113. #define newc_namesize_size 8
  114. #define newc_checksum_offset 102
  115. #define newc_checksum_size 8
  116. #define newc_header_size 110
  117. /*
  118. * An afio large ASCII header, which they named itself.
  119. * afio utility uses this header, if a file size is larger than 2G bytes
  120. * or inode/uid/gid is bigger than 65535(0xFFFF) or mtime is bigger than
  121. * 0x7fffffff, which we cannot record to odc header because of its limit.
  122. * If not, uses odc header.
  123. */
  124. #define afiol_magic_offset 0
  125. #define afiol_magic_size 6
  126. #define afiol_dev_offset 6
  127. #define afiol_dev_size 8 /* hex */
  128. #define afiol_ino_offset 14
  129. #define afiol_ino_size 16 /* hex */
  130. #define afiol_ino_m_offset 30 /* 'm' */
  131. #define afiol_mode_offset 31
  132. #define afiol_mode_size 6 /* oct */
  133. #define afiol_uid_offset 37
  134. #define afiol_uid_size 8 /* hex */
  135. #define afiol_gid_offset 45
  136. #define afiol_gid_size 8 /* hex */
  137. #define afiol_nlink_offset 53
  138. #define afiol_nlink_size 8 /* hex */
  139. #define afiol_rdev_offset 61
  140. #define afiol_rdev_size 8 /* hex */
  141. #define afiol_mtime_offset 69
  142. #define afiol_mtime_size 16 /* hex */
  143. #define afiol_mtime_n_offset 85 /* 'n' */
  144. #define afiol_namesize_offset 86
  145. #define afiol_namesize_size 4 /* hex */
  146. #define afiol_flag_offset 90
  147. #define afiol_flag_size 4 /* hex */
  148. #define afiol_xsize_offset 94
  149. #define afiol_xsize_size 4 /* hex */
  150. #define afiol_xsize_s_offset 98 /* 's' */
  151. #define afiol_filesize_offset 99
  152. #define afiol_filesize_size 16 /* hex */
  153. #define afiol_filesize_c_offset 115 /* ':' */
  154. #define afiol_header_size 116
  155. struct links_entry {
  156. struct links_entry *next;
  157. struct links_entry *previous;
  158. unsigned int links;
  159. dev_t dev;
  160. int64_t ino;
  161. char *name;
  162. };
  163. #define CPIO_MAGIC 0x13141516
  164. struct cpio {
  165. int magic;
  166. int (*read_header)(struct archive_read *, struct cpio *,
  167. struct archive_entry *, size_t *, size_t *);
  168. struct links_entry *links_head;
  169. int64_t entry_bytes_remaining;
  170. int64_t entry_bytes_unconsumed;
  171. int64_t entry_offset;
  172. int64_t entry_padding;
  173. struct archive_string_conv *opt_sconv;
  174. struct archive_string_conv *sconv_default;
  175. int init_default_conversion;
  176. int option_pwb;
  177. };
  178. static int64_t atol16(const char *, unsigned);
  179. static int64_t atol8(const char *, unsigned);
  180. static int archive_read_format_cpio_bid(struct archive_read *, int);
  181. static int archive_read_format_cpio_options(struct archive_read *,
  182. const char *, const char *);
  183. static int archive_read_format_cpio_cleanup(struct archive_read *);
  184. static int archive_read_format_cpio_read_data(struct archive_read *,
  185. const void **, size_t *, int64_t *);
  186. static int archive_read_format_cpio_read_header(struct archive_read *,
  187. struct archive_entry *);
  188. static int archive_read_format_cpio_skip(struct archive_read *);
  189. static int64_t be4(const unsigned char *);
  190. static int find_odc_header(struct archive_read *);
  191. static int find_newc_header(struct archive_read *);
  192. static int header_bin_be(struct archive_read *, struct cpio *,
  193. struct archive_entry *, size_t *, size_t *);
  194. static int header_bin_le(struct archive_read *, struct cpio *,
  195. struct archive_entry *, size_t *, size_t *);
  196. static int header_newc(struct archive_read *, struct cpio *,
  197. struct archive_entry *, size_t *, size_t *);
  198. static int header_odc(struct archive_read *, struct cpio *,
  199. struct archive_entry *, size_t *, size_t *);
  200. static int header_afiol(struct archive_read *, struct cpio *,
  201. struct archive_entry *, size_t *, size_t *);
  202. static int is_octal(const char *, size_t);
  203. static int is_hex(const char *, size_t);
  204. static int64_t le4(const unsigned char *);
  205. static int record_hardlink(struct archive_read *a,
  206. struct cpio *cpio, struct archive_entry *entry);
  207. int
  208. archive_read_support_format_cpio(struct archive *_a)
  209. {
  210. struct archive_read *a = (struct archive_read *)_a;
  211. struct cpio *cpio;
  212. int r;
  213. archive_check_magic(_a, ARCHIVE_READ_MAGIC,
  214. ARCHIVE_STATE_NEW, "archive_read_support_format_cpio");
  215. cpio = (struct cpio *)calloc(1, sizeof(*cpio));
  216. if (cpio == NULL) {
  217. archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
  218. return (ARCHIVE_FATAL);
  219. }
  220. cpio->magic = CPIO_MAGIC;
  221. r = __archive_read_register_format(a,
  222. cpio,
  223. "cpio",
  224. archive_read_format_cpio_bid,
  225. archive_read_format_cpio_options,
  226. archive_read_format_cpio_read_header,
  227. archive_read_format_cpio_read_data,
  228. archive_read_format_cpio_skip,
  229. NULL,
  230. archive_read_format_cpio_cleanup,
  231. NULL,
  232. NULL);
  233. if (r != ARCHIVE_OK)
  234. free(cpio);
  235. return (ARCHIVE_OK);
  236. }
  237. static int
  238. archive_read_format_cpio_bid(struct archive_read *a, int best_bid)
  239. {
  240. const unsigned char *p;
  241. struct cpio *cpio;
  242. int bid;
  243. (void)best_bid; /* UNUSED */
  244. cpio = (struct cpio *)(a->format->data);
  245. if ((p = __archive_read_ahead(a, 6, NULL)) == NULL)
  246. return (-1);
  247. bid = 0;
  248. if (memcmp(p, "070707", 6) == 0) {
  249. /* ASCII cpio archive (odc, POSIX.1) */
  250. cpio->read_header = header_odc;
  251. bid += 48;
  252. /*
  253. * XXX TODO: More verification; Could check that only octal
  254. * digits appear in appropriate header locations. XXX
  255. */
  256. } else if (memcmp(p, "070727", 6) == 0) {
  257. /* afio large ASCII cpio archive */
  258. cpio->read_header = header_odc;
  259. bid += 48;
  260. /*
  261. * XXX TODO: More verification; Could check that almost hex
  262. * digits appear in appropriate header locations. XXX
  263. */
  264. } else if (memcmp(p, "070701", 6) == 0) {
  265. /* ASCII cpio archive (SVR4 without CRC) */
  266. cpio->read_header = header_newc;
  267. bid += 48;
  268. /*
  269. * XXX TODO: More verification; Could check that only hex
  270. * digits appear in appropriate header locations. XXX
  271. */
  272. } else if (memcmp(p, "070702", 6) == 0) {
  273. /* ASCII cpio archive (SVR4 with CRC) */
  274. /* XXX TODO: Flag that we should check the CRC. XXX */
  275. cpio->read_header = header_newc;
  276. bid += 48;
  277. /*
  278. * XXX TODO: More verification; Could check that only hex
  279. * digits appear in appropriate header locations. XXX
  280. */
  281. } else if (p[0] * 256 + p[1] == 070707) {
  282. /* big-endian binary cpio archives */
  283. cpio->read_header = header_bin_be;
  284. bid += 16;
  285. /* Is more verification possible here? */
  286. } else if (p[0] + p[1] * 256 == 070707) {
  287. /* little-endian binary cpio archives */
  288. cpio->read_header = header_bin_le;
  289. bid += 16;
  290. /* Is more verification possible here? */
  291. } else
  292. return (ARCHIVE_WARN);
  293. return (bid);
  294. }
  295. static int
  296. archive_read_format_cpio_options(struct archive_read *a,
  297. const char *key, const char *val)
  298. {
  299. struct cpio *cpio;
  300. int ret = ARCHIVE_FAILED;
  301. cpio = (struct cpio *)(a->format->data);
  302. if (strcmp(key, "compat-2x") == 0) {
  303. /* Handle filenames as libarchive 2.x */
  304. cpio->init_default_conversion = (val != NULL)?1:0;
  305. return (ARCHIVE_OK);
  306. } else if (strcmp(key, "hdrcharset") == 0) {
  307. if (val == NULL || val[0] == 0)
  308. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  309. "cpio: hdrcharset option needs a character-set name");
  310. else {
  311. cpio->opt_sconv =
  312. archive_string_conversion_from_charset(
  313. &a->archive, val, 0);
  314. if (cpio->opt_sconv != NULL)
  315. ret = ARCHIVE_OK;
  316. else
  317. ret = ARCHIVE_FATAL;
  318. }
  319. return (ret);
  320. } else if (strcmp(key, "pwb") == 0) {
  321. if (val != NULL && val[0] != 0)
  322. cpio->option_pwb = 1;
  323. return (ARCHIVE_OK);
  324. }
  325. /* Note: The "warn" return is just to inform the options
  326. * supervisor that we didn't handle it. It will generate
  327. * a suitable error if no one used this option. */
  328. return (ARCHIVE_WARN);
  329. }
  330. static int
  331. archive_read_format_cpio_read_header(struct archive_read *a,
  332. struct archive_entry *entry)
  333. {
  334. struct cpio *cpio;
  335. const void *h, *hl;
  336. struct archive_string_conv *sconv;
  337. size_t namelength;
  338. size_t name_pad;
  339. int r;
  340. cpio = (struct cpio *)(a->format->data);
  341. sconv = cpio->opt_sconv;
  342. if (sconv == NULL) {
  343. if (!cpio->init_default_conversion) {
  344. cpio->sconv_default =
  345. archive_string_default_conversion_for_read(
  346. &(a->archive));
  347. cpio->init_default_conversion = 1;
  348. }
  349. sconv = cpio->sconv_default;
  350. }
  351. r = (cpio->read_header(a, cpio, entry, &namelength, &name_pad));
  352. if (r < ARCHIVE_WARN)
  353. return (r);
  354. /* Read name from buffer. */
  355. h = __archive_read_ahead(a, namelength + name_pad, NULL);
  356. if (h == NULL)
  357. return (ARCHIVE_FATAL);
  358. if (archive_entry_copy_pathname_l(entry,
  359. (const char *)h, namelength, sconv) != 0) {
  360. if (errno == ENOMEM) {
  361. archive_set_error(&a->archive, ENOMEM,
  362. "Can't allocate memory for Pathname");
  363. return (ARCHIVE_FATAL);
  364. }
  365. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  366. "Pathname can't be converted from %s to current locale.",
  367. archive_string_conversion_charset_name(sconv));
  368. r = ARCHIVE_WARN;
  369. }
  370. cpio->entry_offset = 0;
  371. __archive_read_consume(a, namelength + name_pad);
  372. /* If this is a symlink, read the link contents. */
  373. if (archive_entry_filetype(entry) == AE_IFLNK) {
  374. if (cpio->entry_bytes_remaining > 1024 * 1024) {
  375. archive_set_error(&a->archive, ENOMEM,
  376. "Rejecting malformed cpio archive: symlink contents exceed 1 megabyte");
  377. return (ARCHIVE_FATAL);
  378. }
  379. hl = __archive_read_ahead(a,
  380. (size_t)cpio->entry_bytes_remaining, NULL);
  381. if (hl == NULL)
  382. return (ARCHIVE_FATAL);
  383. if (archive_entry_copy_symlink_l(entry, (const char *)hl,
  384. (size_t)cpio->entry_bytes_remaining, sconv) != 0) {
  385. if (errno == ENOMEM) {
  386. archive_set_error(&a->archive, ENOMEM,
  387. "Can't allocate memory for Linkname");
  388. return (ARCHIVE_FATAL);
  389. }
  390. archive_set_error(&a->archive,
  391. ARCHIVE_ERRNO_FILE_FORMAT,
  392. "Linkname can't be converted from %s to "
  393. "current locale.",
  394. archive_string_conversion_charset_name(sconv));
  395. r = ARCHIVE_WARN;
  396. }
  397. __archive_read_consume(a, cpio->entry_bytes_remaining);
  398. cpio->entry_bytes_remaining = 0;
  399. }
  400. /* XXX TODO: If the full mode is 0160200, then this is a Solaris
  401. * ACL description for the following entry. Read this body
  402. * and parse it as a Solaris-style ACL, then read the next
  403. * header. XXX */
  404. /* Compare name to "TRAILER!!!" to test for end-of-archive. */
  405. if (namelength == 11 && strncmp((const char *)h, "TRAILER!!!",
  406. 10) == 0) {
  407. /* TODO: Store file location of start of block. */
  408. archive_clear_error(&a->archive);
  409. return (ARCHIVE_EOF);
  410. }
  411. /* Detect and record hardlinks to previously-extracted entries. */
  412. if (record_hardlink(a, cpio, entry) != ARCHIVE_OK) {
  413. return (ARCHIVE_FATAL);
  414. }
  415. return (r);
  416. }
  417. static int
  418. archive_read_format_cpio_read_data(struct archive_read *a,
  419. const void **buff, size_t *size, int64_t *offset)
  420. {
  421. ssize_t bytes_read;
  422. struct cpio *cpio;
  423. cpio = (struct cpio *)(a->format->data);
  424. if (cpio->entry_bytes_unconsumed) {
  425. __archive_read_consume(a, cpio->entry_bytes_unconsumed);
  426. cpio->entry_bytes_unconsumed = 0;
  427. }
  428. if (cpio->entry_bytes_remaining > 0) {
  429. *buff = __archive_read_ahead(a, 1, &bytes_read);
  430. if (bytes_read <= 0)
  431. return (ARCHIVE_FATAL);
  432. if (bytes_read > cpio->entry_bytes_remaining)
  433. bytes_read = (ssize_t)cpio->entry_bytes_remaining;
  434. *size = bytes_read;
  435. cpio->entry_bytes_unconsumed = bytes_read;
  436. *offset = cpio->entry_offset;
  437. cpio->entry_offset += bytes_read;
  438. cpio->entry_bytes_remaining -= bytes_read;
  439. return (ARCHIVE_OK);
  440. } else {
  441. if (cpio->entry_padding !=
  442. __archive_read_consume(a, cpio->entry_padding)) {
  443. return (ARCHIVE_FATAL);
  444. }
  445. cpio->entry_padding = 0;
  446. *buff = NULL;
  447. *size = 0;
  448. *offset = cpio->entry_offset;
  449. return (ARCHIVE_EOF);
  450. }
  451. }
  452. static int
  453. archive_read_format_cpio_skip(struct archive_read *a)
  454. {
  455. struct cpio *cpio = (struct cpio *)(a->format->data);
  456. int64_t to_skip = cpio->entry_bytes_remaining + cpio->entry_padding +
  457. cpio->entry_bytes_unconsumed;
  458. if (to_skip != __archive_read_consume(a, to_skip)) {
  459. return (ARCHIVE_FATAL);
  460. }
  461. cpio->entry_bytes_remaining = 0;
  462. cpio->entry_padding = 0;
  463. cpio->entry_bytes_unconsumed = 0;
  464. return (ARCHIVE_OK);
  465. }
  466. /*
  467. * Skip forward to the next cpio newc header by searching for the
  468. * 07070[12] string. This should be generalized and merged with
  469. * find_odc_header below.
  470. */
  471. static int
  472. is_hex(const char *p, size_t len)
  473. {
  474. while (len-- > 0) {
  475. if ((*p >= '0' && *p <= '9')
  476. || (*p >= 'a' && *p <= 'f')
  477. || (*p >= 'A' && *p <= 'F'))
  478. ++p;
  479. else
  480. return (0);
  481. }
  482. return (1);
  483. }
  484. static int
  485. find_newc_header(struct archive_read *a)
  486. {
  487. const void *h;
  488. const char *p, *q;
  489. size_t skip, skipped = 0;
  490. ssize_t bytes;
  491. for (;;) {
  492. h = __archive_read_ahead(a, newc_header_size, &bytes);
  493. if (h == NULL)
  494. return (ARCHIVE_FATAL);
  495. p = h;
  496. q = p + bytes;
  497. /* Try the typical case first, then go into the slow search.*/
  498. if (memcmp("07070", p, 5) == 0
  499. && (p[5] == '1' || p[5] == '2')
  500. && is_hex(p, newc_header_size))
  501. return (ARCHIVE_OK);
  502. /*
  503. * Scan ahead until we find something that looks
  504. * like a newc header.
  505. */
  506. while (p + newc_header_size <= q) {
  507. switch (p[5]) {
  508. case '1':
  509. case '2':
  510. if (memcmp("07070", p, 5) == 0
  511. && is_hex(p, newc_header_size)) {
  512. skip = p - (const char *)h;
  513. __archive_read_consume(a, skip);
  514. skipped += skip;
  515. if (skipped > 0) {
  516. archive_set_error(&a->archive,
  517. 0,
  518. "Skipped %d bytes before "
  519. "finding valid header",
  520. (int)skipped);
  521. return (ARCHIVE_WARN);
  522. }
  523. return (ARCHIVE_OK);
  524. }
  525. p += 2;
  526. break;
  527. case '0':
  528. p++;
  529. break;
  530. default:
  531. p += 6;
  532. break;
  533. }
  534. }
  535. skip = p - (const char *)h;
  536. __archive_read_consume(a, skip);
  537. skipped += skip;
  538. }
  539. }
  540. static int
  541. header_newc(struct archive_read *a, struct cpio *cpio,
  542. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  543. {
  544. const void *h;
  545. const char *header;
  546. int r;
  547. r = find_newc_header(a);
  548. if (r < ARCHIVE_WARN)
  549. return (r);
  550. /* Read fixed-size portion of header. */
  551. h = __archive_read_ahead(a, newc_header_size, NULL);
  552. if (h == NULL)
  553. return (ARCHIVE_FATAL);
  554. /* Parse out hex fields. */
  555. header = (const char *)h;
  556. if (memcmp(header + newc_magic_offset, "070701", 6) == 0) {
  557. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
  558. a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
  559. } else if (memcmp(header + newc_magic_offset, "070702", 6) == 0) {
  560. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
  561. a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
  562. } else {
  563. /* TODO: Abort here? */
  564. }
  565. archive_entry_set_devmajor(entry,
  566. (dev_t)atol16(header + newc_devmajor_offset, newc_devmajor_size));
  567. archive_entry_set_devminor(entry,
  568. (dev_t)atol16(header + newc_devminor_offset, newc_devminor_size));
  569. archive_entry_set_ino(entry, atol16(header + newc_ino_offset, newc_ino_size));
  570. archive_entry_set_mode(entry,
  571. (mode_t)atol16(header + newc_mode_offset, newc_mode_size));
  572. archive_entry_set_uid(entry, atol16(header + newc_uid_offset, newc_uid_size));
  573. archive_entry_set_gid(entry, atol16(header + newc_gid_offset, newc_gid_size));
  574. archive_entry_set_nlink(entry,
  575. (unsigned int)atol16(header + newc_nlink_offset, newc_nlink_size));
  576. archive_entry_set_rdevmajor(entry,
  577. (dev_t)atol16(header + newc_rdevmajor_offset, newc_rdevmajor_size));
  578. archive_entry_set_rdevminor(entry,
  579. (dev_t)atol16(header + newc_rdevminor_offset, newc_rdevminor_size));
  580. archive_entry_set_mtime(entry, atol16(header + newc_mtime_offset, newc_mtime_size), 0);
  581. *namelength = (size_t)atol16(header + newc_namesize_offset, newc_namesize_size);
  582. /* Pad name to 2 more than a multiple of 4. */
  583. *name_pad = (2 - *namelength) & 3;
  584. /* Make sure that the padded name length fits into size_t. */
  585. if (*name_pad > SIZE_MAX - *namelength) {
  586. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  587. "cpio archive has invalid namelength");
  588. return (ARCHIVE_FATAL);
  589. }
  590. /*
  591. * Note: entry_bytes_remaining is at least 64 bits and
  592. * therefore guaranteed to be big enough for a 33-bit file
  593. * size.
  594. */
  595. cpio->entry_bytes_remaining =
  596. atol16(header + newc_filesize_offset, newc_filesize_size);
  597. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  598. /* Pad file contents to a multiple of 4. */
  599. cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
  600. __archive_read_consume(a, newc_header_size);
  601. return (r);
  602. }
  603. /*
  604. * Skip forward to the next cpio odc header by searching for the
  605. * 070707 string. This is a hand-optimized search that could
  606. * probably be easily generalized to handle all character-based
  607. * cpio variants.
  608. */
  609. static int
  610. is_octal(const char *p, size_t len)
  611. {
  612. while (len-- > 0) {
  613. if (*p < '0' || *p > '7')
  614. return (0);
  615. ++p;
  616. }
  617. return (1);
  618. }
  619. static int
  620. is_afio_large(const char *h, size_t len)
  621. {
  622. if (len < afiol_header_size)
  623. return (0);
  624. if (h[afiol_ino_m_offset] != 'm'
  625. || h[afiol_mtime_n_offset] != 'n'
  626. || h[afiol_xsize_s_offset] != 's'
  627. || h[afiol_filesize_c_offset] != ':')
  628. return (0);
  629. if (!is_hex(h + afiol_dev_offset, afiol_ino_m_offset - afiol_dev_offset))
  630. return (0);
  631. if (!is_hex(h + afiol_mode_offset, afiol_mtime_n_offset - afiol_mode_offset))
  632. return (0);
  633. if (!is_hex(h + afiol_namesize_offset, afiol_xsize_s_offset - afiol_namesize_offset))
  634. return (0);
  635. if (!is_hex(h + afiol_filesize_offset, afiol_filesize_size))
  636. return (0);
  637. return (1);
  638. }
  639. static int
  640. find_odc_header(struct archive_read *a)
  641. {
  642. const void *h;
  643. const char *p, *q;
  644. size_t skip, skipped = 0;
  645. ssize_t bytes;
  646. for (;;) {
  647. h = __archive_read_ahead(a, odc_header_size, &bytes);
  648. if (h == NULL)
  649. return (ARCHIVE_FATAL);
  650. p = h;
  651. q = p + bytes;
  652. /* Try the typical case first, then go into the slow search.*/
  653. if (memcmp("070707", p, 6) == 0 && is_octal(p, odc_header_size))
  654. return (ARCHIVE_OK);
  655. if (memcmp("070727", p, 6) == 0 && is_afio_large(p, bytes)) {
  656. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
  657. return (ARCHIVE_OK);
  658. }
  659. /*
  660. * Scan ahead until we find something that looks
  661. * like an odc header.
  662. */
  663. while (p + odc_header_size <= q) {
  664. switch (p[5]) {
  665. case '7':
  666. if ((memcmp("070707", p, 6) == 0
  667. && is_octal(p, odc_header_size))
  668. || (memcmp("070727", p, 6) == 0
  669. && is_afio_large(p, q - p))) {
  670. skip = p - (const char *)h;
  671. __archive_read_consume(a, skip);
  672. skipped += skip;
  673. if (p[4] == '2')
  674. a->archive.archive_format =
  675. ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
  676. if (skipped > 0) {
  677. archive_set_error(&a->archive,
  678. 0,
  679. "Skipped %d bytes before "
  680. "finding valid header",
  681. (int)skipped);
  682. return (ARCHIVE_WARN);
  683. }
  684. return (ARCHIVE_OK);
  685. }
  686. p += 2;
  687. break;
  688. case '0':
  689. p++;
  690. break;
  691. default:
  692. p += 6;
  693. break;
  694. }
  695. }
  696. skip = p - (const char *)h;
  697. __archive_read_consume(a, skip);
  698. skipped += skip;
  699. }
  700. }
  701. static int
  702. header_odc(struct archive_read *a, struct cpio *cpio,
  703. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  704. {
  705. const void *h;
  706. int r;
  707. const char *header;
  708. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
  709. a->archive.archive_format_name = "POSIX octet-oriented cpio";
  710. /* Find the start of the next header. */
  711. r = find_odc_header(a);
  712. if (r < ARCHIVE_WARN)
  713. return (r);
  714. if (a->archive.archive_format == ARCHIVE_FORMAT_CPIO_AFIO_LARGE) {
  715. int r2 = (header_afiol(a, cpio, entry, namelength, name_pad));
  716. if (r2 == ARCHIVE_OK)
  717. return (r);
  718. else
  719. return (r2);
  720. }
  721. /* Read fixed-size portion of header. */
  722. h = __archive_read_ahead(a, odc_header_size, NULL);
  723. if (h == NULL)
  724. return (ARCHIVE_FATAL);
  725. /* Parse out octal fields. */
  726. header = (const char *)h;
  727. archive_entry_set_dev(entry,
  728. (dev_t)atol8(header + odc_dev_offset, odc_dev_size));
  729. archive_entry_set_ino(entry, atol8(header + odc_ino_offset, odc_ino_size));
  730. archive_entry_set_mode(entry,
  731. (mode_t)atol8(header + odc_mode_offset, odc_mode_size));
  732. archive_entry_set_uid(entry, atol8(header + odc_uid_offset, odc_uid_size));
  733. archive_entry_set_gid(entry, atol8(header + odc_gid_offset, odc_gid_size));
  734. archive_entry_set_nlink(entry,
  735. (unsigned int)atol8(header + odc_nlink_offset, odc_nlink_size));
  736. archive_entry_set_rdev(entry,
  737. (dev_t)atol8(header + odc_rdev_offset, odc_rdev_size));
  738. archive_entry_set_mtime(entry, atol8(header + odc_mtime_offset, odc_mtime_size), 0);
  739. *namelength = (size_t)atol8(header + odc_namesize_offset, odc_namesize_size);
  740. *name_pad = 0; /* No padding of filename. */
  741. /*
  742. * Note: entry_bytes_remaining is at least 64 bits and
  743. * therefore guaranteed to be big enough for a 33-bit file
  744. * size.
  745. */
  746. cpio->entry_bytes_remaining =
  747. atol8(header + odc_filesize_offset, odc_filesize_size);
  748. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  749. cpio->entry_padding = 0;
  750. __archive_read_consume(a, odc_header_size);
  751. return (r);
  752. }
  753. /*
  754. * NOTE: if a filename suffix is ".z", it is the file gziped by afio.
  755. * it would be nice that we can show uncompressed file size and we can
  756. * uncompressed file contents automatically, unfortunately we have nothing
  757. * to get a uncompressed file size while reading each header. It means
  758. * we also cannot uncompress file contents under our framework.
  759. */
  760. static int
  761. header_afiol(struct archive_read *a, struct cpio *cpio,
  762. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  763. {
  764. int64_t t;
  765. const void *h;
  766. const char *header;
  767. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
  768. a->archive.archive_format_name = "afio large ASCII";
  769. /* Read fixed-size portion of header. */
  770. h = __archive_read_ahead(a, afiol_header_size, NULL);
  771. if (h == NULL)
  772. return (ARCHIVE_FATAL);
  773. /* Parse out octal fields. */
  774. header = (const char *)h;
  775. archive_entry_set_dev(entry,
  776. (dev_t)atol16(header + afiol_dev_offset, afiol_dev_size));
  777. t = atol16(header + afiol_ino_offset, afiol_ino_size);
  778. if (t < 0) {
  779. archive_set_error(&a->archive, 0, "Nonsensical ino value");
  780. return (ARCHIVE_FATAL);
  781. }
  782. archive_entry_set_ino(entry, t);
  783. archive_entry_set_mode(entry,
  784. (mode_t)atol8(header + afiol_mode_offset, afiol_mode_size));
  785. archive_entry_set_uid(entry, atol16(header + afiol_uid_offset, afiol_uid_size));
  786. archive_entry_set_gid(entry, atol16(header + afiol_gid_offset, afiol_gid_size));
  787. archive_entry_set_nlink(entry,
  788. (unsigned int)atol16(header + afiol_nlink_offset, afiol_nlink_size));
  789. archive_entry_set_rdev(entry,
  790. (dev_t)atol16(header + afiol_rdev_offset, afiol_rdev_size));
  791. archive_entry_set_mtime(entry, atol16(header + afiol_mtime_offset, afiol_mtime_size), 0);
  792. *namelength = (size_t)atol16(header + afiol_namesize_offset, afiol_namesize_size);
  793. *name_pad = 0; /* No padding of filename. */
  794. t = atol16(header + afiol_filesize_offset, afiol_filesize_size);
  795. if (t < 0) {
  796. archive_set_error(&a->archive, 0, "Nonsensical file size");
  797. return (ARCHIVE_FATAL);
  798. }
  799. cpio->entry_bytes_remaining = t;
  800. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  801. cpio->entry_padding = 0;
  802. __archive_read_consume(a, afiol_header_size);
  803. return (ARCHIVE_OK);
  804. }
  805. static int
  806. header_bin_le(struct archive_read *a, struct cpio *cpio,
  807. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  808. {
  809. const void *h;
  810. const unsigned char *header;
  811. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
  812. a->archive.archive_format_name = "cpio (little-endian binary)";
  813. /* Read fixed-size portion of header. */
  814. h = __archive_read_ahead(a, bin_header_size, NULL);
  815. if (h == NULL) {
  816. archive_set_error(&a->archive, 0,
  817. "End of file trying to read next cpio header");
  818. return (ARCHIVE_FATAL);
  819. }
  820. /* Parse out binary fields. */
  821. header = (const unsigned char *)h;
  822. archive_entry_set_dev(entry, header[bin_dev_offset] + header[bin_dev_offset + 1] * 256);
  823. archive_entry_set_ino(entry, header[bin_ino_offset] + header[bin_ino_offset + 1] * 256);
  824. archive_entry_set_mode(entry, header[bin_mode_offset] + header[bin_mode_offset + 1] * 256);
  825. if (cpio->option_pwb) {
  826. /* turn off random bits left over from V6 inode */
  827. archive_entry_set_mode(entry, archive_entry_mode(entry) & 067777);
  828. if ((archive_entry_mode(entry) & AE_IFMT) == 0)
  829. archive_entry_set_mode(entry, archive_entry_mode(entry) | AE_IFREG);
  830. }
  831. archive_entry_set_uid(entry, header[bin_uid_offset] + header[bin_uid_offset + 1] * 256);
  832. archive_entry_set_gid(entry, header[bin_gid_offset] + header[bin_gid_offset + 1] * 256);
  833. archive_entry_set_nlink(entry, header[bin_nlink_offset] + header[bin_nlink_offset + 1] * 256);
  834. archive_entry_set_rdev(entry, header[bin_rdev_offset] + header[bin_rdev_offset + 1] * 256);
  835. archive_entry_set_mtime(entry, le4(header + bin_mtime_offset), 0);
  836. *namelength = header[bin_namesize_offset] + header[bin_namesize_offset + 1] * 256;
  837. *name_pad = *namelength & 1; /* Pad to even. */
  838. cpio->entry_bytes_remaining = le4(header + bin_filesize_offset);
  839. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  840. cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
  841. __archive_read_consume(a, bin_header_size);
  842. return (ARCHIVE_OK);
  843. }
  844. static int
  845. header_bin_be(struct archive_read *a, struct cpio *cpio,
  846. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  847. {
  848. const void *h;
  849. const unsigned char *header;
  850. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
  851. a->archive.archive_format_name = "cpio (big-endian binary)";
  852. /* Read fixed-size portion of header. */
  853. h = __archive_read_ahead(a, bin_header_size, NULL);
  854. if (h == NULL) {
  855. archive_set_error(&a->archive, 0,
  856. "End of file trying to read next cpio header");
  857. return (ARCHIVE_FATAL);
  858. }
  859. /* Parse out binary fields. */
  860. header = (const unsigned char *)h;
  861. archive_entry_set_dev(entry, header[bin_dev_offset] * 256 + header[bin_dev_offset + 1]);
  862. archive_entry_set_ino(entry, header[bin_ino_offset] * 256 + header[bin_ino_offset + 1]);
  863. archive_entry_set_mode(entry, header[bin_mode_offset] * 256 + header[bin_mode_offset + 1]);
  864. if (cpio->option_pwb) {
  865. /* turn off random bits left over from V6 inode */
  866. archive_entry_set_mode(entry, archive_entry_mode(entry) & 067777);
  867. if ((archive_entry_mode(entry) & AE_IFMT) == 0)
  868. archive_entry_set_mode(entry, archive_entry_mode(entry) | AE_IFREG);
  869. }
  870. archive_entry_set_uid(entry, header[bin_uid_offset] * 256 + header[bin_uid_offset + 1]);
  871. archive_entry_set_gid(entry, header[bin_gid_offset] * 256 + header[bin_gid_offset + 1]);
  872. archive_entry_set_nlink(entry, header[bin_nlink_offset] * 256 + header[bin_nlink_offset + 1]);
  873. archive_entry_set_rdev(entry, header[bin_rdev_offset] * 256 + header[bin_rdev_offset + 1]);
  874. archive_entry_set_mtime(entry, be4(header + bin_mtime_offset), 0);
  875. *namelength = header[bin_namesize_offset] * 256 + header[bin_namesize_offset + 1];
  876. *name_pad = *namelength & 1; /* Pad to even. */
  877. cpio->entry_bytes_remaining = be4(header + bin_filesize_offset);
  878. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  879. cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
  880. __archive_read_consume(a, bin_header_size);
  881. return (ARCHIVE_OK);
  882. }
  883. static int
  884. archive_read_format_cpio_cleanup(struct archive_read *a)
  885. {
  886. struct cpio *cpio;
  887. cpio = (struct cpio *)(a->format->data);
  888. /* Free inode->name map */
  889. while (cpio->links_head != NULL) {
  890. struct links_entry *lp = cpio->links_head->next;
  891. free(cpio->links_head->name);
  892. free(cpio->links_head);
  893. cpio->links_head = lp;
  894. }
  895. free(cpio);
  896. (a->format->data) = NULL;
  897. return (ARCHIVE_OK);
  898. }
  899. static int64_t
  900. le4(const unsigned char *p)
  901. {
  902. return ((p[0] << 16) | (((int64_t)p[1]) << 24) | (p[2] << 0) | (p[3] << 8));
  903. }
  904. static int64_t
  905. be4(const unsigned char *p)
  906. {
  907. return ((((int64_t)p[0]) << 24) | (p[1] << 16) | (p[2] << 8) | (p[3]));
  908. }
  909. /*
  910. * Note that this implementation does not (and should not!) obey
  911. * locale settings; you cannot simply substitute strtol here, since
  912. * it does obey locale.
  913. */
  914. static int64_t
  915. atol8(const char *p, unsigned char_cnt)
  916. {
  917. uint64_t l;
  918. int digit;
  919. l = 0;
  920. while (char_cnt-- > 0) {
  921. if (*p >= '0' && *p <= '7')
  922. digit = *p - '0';
  923. else
  924. return ((int64_t)l);
  925. p++;
  926. l <<= 3;
  927. l |= digit;
  928. }
  929. return ((int64_t)l);
  930. }
  931. static int64_t
  932. atol16(const char *p, unsigned char_cnt)
  933. {
  934. uint64_t l;
  935. int digit;
  936. l = 0;
  937. while (char_cnt-- > 0) {
  938. if (*p >= 'a' && *p <= 'f')
  939. digit = *p - 'a' + 10;
  940. else if (*p >= 'A' && *p <= 'F')
  941. digit = *p - 'A' + 10;
  942. else if (*p >= '0' && *p <= '9')
  943. digit = *p - '0';
  944. else
  945. return ((int64_t)l);
  946. p++;
  947. l <<= 4;
  948. l |= digit;
  949. }
  950. return ((int64_t)l);
  951. }
  952. static int
  953. record_hardlink(struct archive_read *a,
  954. struct cpio *cpio, struct archive_entry *entry)
  955. {
  956. struct links_entry *le;
  957. dev_t dev;
  958. int64_t ino;
  959. if (archive_entry_nlink(entry) <= 1)
  960. return (ARCHIVE_OK);
  961. dev = archive_entry_dev(entry);
  962. ino = archive_entry_ino64(entry);
  963. /*
  964. * First look in the list of multiply-linked files. If we've
  965. * already dumped it, convert this entry to a hard link entry.
  966. */
  967. for (le = cpio->links_head; le; le = le->next) {
  968. if (le->dev == dev && le->ino == ino) {
  969. archive_entry_copy_hardlink(entry, le->name);
  970. if (--le->links <= 0) {
  971. if (le->previous != NULL)
  972. le->previous->next = le->next;
  973. if (le->next != NULL)
  974. le->next->previous = le->previous;
  975. if (cpio->links_head == le)
  976. cpio->links_head = le->next;
  977. free(le->name);
  978. free(le);
  979. }
  980. return (ARCHIVE_OK);
  981. }
  982. }
  983. le = (struct links_entry *)malloc(sizeof(struct links_entry));
  984. if (le == NULL) {
  985. archive_set_error(&a->archive,
  986. ENOMEM, "Out of memory adding file to list");
  987. return (ARCHIVE_FATAL);
  988. }
  989. if (cpio->links_head != NULL)
  990. cpio->links_head->previous = le;
  991. le->next = cpio->links_head;
  992. le->previous = NULL;
  993. cpio->links_head = le;
  994. le->dev = dev;
  995. le->ino = ino;
  996. le->links = archive_entry_nlink(entry) - 1;
  997. le->name = strdup(archive_entry_pathname(entry));
  998. if (le->name == NULL) {
  999. archive_set_error(&a->archive,
  1000. ENOMEM, "Out of memory adding file to list");
  1001. return (ARCHIVE_FATAL);
  1002. }
  1003. return (ARCHIVE_OK);
  1004. }