archive_read_support_format_cpio.c 32 KB

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