archive_read_support_format_cpio.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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. 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 int 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 int 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 filnames 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;
  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. h = __archive_read_ahead(a,
  371. (size_t)cpio->entry_bytes_remaining, NULL);
  372. if (h == NULL)
  373. return (ARCHIVE_FATAL);
  374. if (archive_entry_copy_symlink_l(entry, (const char *)h,
  375. (size_t)cpio->entry_bytes_remaining, sconv) != 0) {
  376. if (errno == ENOMEM) {
  377. archive_set_error(&a->archive, ENOMEM,
  378. "Can't allocate memory for Linkname");
  379. return (ARCHIVE_FATAL);
  380. }
  381. archive_set_error(&a->archive,
  382. ARCHIVE_ERRNO_FILE_FORMAT,
  383. "Linkname can't be converted from %s to "
  384. "current locale.",
  385. archive_string_conversion_charset_name(sconv));
  386. r = ARCHIVE_WARN;
  387. }
  388. __archive_read_consume(a, cpio->entry_bytes_remaining);
  389. cpio->entry_bytes_remaining = 0;
  390. }
  391. /* XXX TODO: If the full mode is 0160200, then this is a Solaris
  392. * ACL description for the following entry. Read this body
  393. * and parse it as a Solaris-style ACL, then read the next
  394. * header. XXX */
  395. /* Compare name to "TRAILER!!!" to test for end-of-archive. */
  396. if (namelength == 11 && strcmp((const char *)h, "TRAILER!!!") == 0) {
  397. /* TODO: Store file location of start of block. */
  398. archive_clear_error(&a->archive);
  399. return (ARCHIVE_EOF);
  400. }
  401. /* Detect and record hardlinks to previously-extracted entries. */
  402. if (record_hardlink(a, cpio, entry) != ARCHIVE_OK) {
  403. return (ARCHIVE_FATAL);
  404. }
  405. return (r);
  406. }
  407. static int
  408. archive_read_format_cpio_read_data(struct archive_read *a,
  409. const void **buff, size_t *size, int64_t *offset)
  410. {
  411. ssize_t bytes_read;
  412. struct cpio *cpio;
  413. cpio = (struct cpio *)(a->format->data);
  414. if (cpio->entry_bytes_unconsumed) {
  415. __archive_read_consume(a, cpio->entry_bytes_unconsumed);
  416. cpio->entry_bytes_unconsumed = 0;
  417. }
  418. if (cpio->entry_bytes_remaining > 0) {
  419. *buff = __archive_read_ahead(a, 1, &bytes_read);
  420. if (bytes_read <= 0)
  421. return (ARCHIVE_FATAL);
  422. if (bytes_read > cpio->entry_bytes_remaining)
  423. bytes_read = (ssize_t)cpio->entry_bytes_remaining;
  424. *size = bytes_read;
  425. cpio->entry_bytes_unconsumed = bytes_read;
  426. *offset = cpio->entry_offset;
  427. cpio->entry_offset += bytes_read;
  428. cpio->entry_bytes_remaining -= bytes_read;
  429. return (ARCHIVE_OK);
  430. } else {
  431. if (cpio->entry_padding !=
  432. __archive_read_consume(a, cpio->entry_padding)) {
  433. return (ARCHIVE_FATAL);
  434. }
  435. cpio->entry_padding = 0;
  436. *buff = NULL;
  437. *size = 0;
  438. *offset = cpio->entry_offset;
  439. return (ARCHIVE_EOF);
  440. }
  441. }
  442. static int
  443. archive_read_format_cpio_skip(struct archive_read *a)
  444. {
  445. struct cpio *cpio = (struct cpio *)(a->format->data);
  446. int64_t to_skip = cpio->entry_bytes_remaining + cpio->entry_padding +
  447. cpio->entry_bytes_unconsumed;
  448. if (to_skip != __archive_read_consume(a, to_skip)) {
  449. return (ARCHIVE_FATAL);
  450. }
  451. cpio->entry_bytes_remaining = 0;
  452. cpio->entry_padding = 0;
  453. cpio->entry_bytes_unconsumed = 0;
  454. return (ARCHIVE_OK);
  455. }
  456. /*
  457. * Skip forward to the next cpio newc header by searching for the
  458. * 07070[12] string. This should be generalized and merged with
  459. * find_odc_header below.
  460. */
  461. static int
  462. is_hex(const char *p, size_t len)
  463. {
  464. while (len-- > 0) {
  465. if ((*p >= '0' && *p <= '9')
  466. || (*p >= 'a' && *p <= 'f')
  467. || (*p >= 'A' && *p <= 'F'))
  468. ++p;
  469. else
  470. return (0);
  471. }
  472. return (1);
  473. }
  474. static int
  475. find_newc_header(struct archive_read *a)
  476. {
  477. const void *h;
  478. const char *p, *q;
  479. size_t skip, skipped = 0;
  480. ssize_t bytes;
  481. for (;;) {
  482. h = __archive_read_ahead(a, newc_header_size, &bytes);
  483. if (h == NULL)
  484. return (ARCHIVE_FATAL);
  485. p = h;
  486. q = p + bytes;
  487. /* Try the typical case first, then go into the slow search.*/
  488. if (memcmp("07070", p, 5) == 0
  489. && (p[5] == '1' || p[5] == '2')
  490. && is_hex(p, newc_header_size))
  491. return (ARCHIVE_OK);
  492. /*
  493. * Scan ahead until we find something that looks
  494. * like a newc header.
  495. */
  496. while (p + newc_header_size <= q) {
  497. switch (p[5]) {
  498. case '1':
  499. case '2':
  500. if (memcmp("07070", p, 5) == 0
  501. && is_hex(p, newc_header_size)) {
  502. skip = p - (const char *)h;
  503. __archive_read_consume(a, skip);
  504. skipped += skip;
  505. if (skipped > 0) {
  506. archive_set_error(&a->archive,
  507. 0,
  508. "Skipped %d bytes before "
  509. "finding valid header",
  510. (int)skipped);
  511. return (ARCHIVE_WARN);
  512. }
  513. return (ARCHIVE_OK);
  514. }
  515. p += 2;
  516. break;
  517. case '0':
  518. p++;
  519. break;
  520. default:
  521. p += 6;
  522. break;
  523. }
  524. }
  525. skip = p - (const char *)h;
  526. __archive_read_consume(a, skip);
  527. skipped += skip;
  528. }
  529. }
  530. static int
  531. header_newc(struct archive_read *a, struct cpio *cpio,
  532. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  533. {
  534. const void *h;
  535. const char *header;
  536. int r;
  537. r = find_newc_header(a);
  538. if (r < ARCHIVE_WARN)
  539. return (r);
  540. /* Read fixed-size portion of header. */
  541. h = __archive_read_ahead(a, newc_header_size, NULL);
  542. if (h == NULL)
  543. return (ARCHIVE_FATAL);
  544. /* Parse out hex fields. */
  545. header = (const char *)h;
  546. if (memcmp(header + newc_magic_offset, "070701", 6) == 0) {
  547. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
  548. a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
  549. } else if (memcmp(header + newc_magic_offset, "070702", 6) == 0) {
  550. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
  551. a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
  552. } else {
  553. /* TODO: Abort here? */
  554. }
  555. archive_entry_set_devmajor(entry,
  556. (dev_t)atol16(header + newc_devmajor_offset, newc_devmajor_size));
  557. archive_entry_set_devminor(entry,
  558. (dev_t)atol16(header + newc_devminor_offset, newc_devminor_size));
  559. archive_entry_set_ino(entry, atol16(header + newc_ino_offset, newc_ino_size));
  560. archive_entry_set_mode(entry,
  561. (mode_t)atol16(header + newc_mode_offset, newc_mode_size));
  562. archive_entry_set_uid(entry, atol16(header + newc_uid_offset, newc_uid_size));
  563. archive_entry_set_gid(entry, atol16(header + newc_gid_offset, newc_gid_size));
  564. archive_entry_set_nlink(entry,
  565. (unsigned int)atol16(header + newc_nlink_offset, newc_nlink_size));
  566. archive_entry_set_rdevmajor(entry,
  567. (dev_t)atol16(header + newc_rdevmajor_offset, newc_rdevmajor_size));
  568. archive_entry_set_rdevminor(entry,
  569. (dev_t)atol16(header + newc_rdevminor_offset, newc_rdevminor_size));
  570. archive_entry_set_mtime(entry, atol16(header + newc_mtime_offset, newc_mtime_size), 0);
  571. *namelength = (size_t)atol16(header + newc_namesize_offset, newc_namesize_size);
  572. /* Pad name to 2 more than a multiple of 4. */
  573. *name_pad = (2 - *namelength) & 3;
  574. /*
  575. * Note: entry_bytes_remaining is at least 64 bits and
  576. * therefore guaranteed to be big enough for a 33-bit file
  577. * size.
  578. */
  579. cpio->entry_bytes_remaining =
  580. atol16(header + newc_filesize_offset, newc_filesize_size);
  581. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  582. /* Pad file contents to a multiple of 4. */
  583. cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
  584. __archive_read_consume(a, newc_header_size);
  585. return (r);
  586. }
  587. /*
  588. * Skip forward to the next cpio odc header by searching for the
  589. * 070707 string. This is a hand-optimized search that could
  590. * probably be easily generalized to handle all character-based
  591. * cpio variants.
  592. */
  593. static int
  594. is_octal(const char *p, size_t len)
  595. {
  596. while (len-- > 0) {
  597. if (*p < '0' || *p > '7')
  598. return (0);
  599. ++p;
  600. }
  601. return (1);
  602. }
  603. static int
  604. is_afio_large(const char *h, size_t len)
  605. {
  606. if (len < afiol_header_size)
  607. return (0);
  608. if (h[afiol_ino_m_offset] != 'm'
  609. || h[afiol_mtime_n_offset] != 'n'
  610. || h[afiol_xsize_s_offset] != 's'
  611. || h[afiol_filesize_c_offset] != ':')
  612. return (0);
  613. if (!is_hex(h + afiol_dev_offset, afiol_ino_m_offset - afiol_dev_offset))
  614. return (0);
  615. if (!is_hex(h + afiol_mode_offset, afiol_mtime_n_offset - afiol_mode_offset))
  616. return (0);
  617. if (!is_hex(h + afiol_namesize_offset, afiol_xsize_s_offset - afiol_namesize_offset))
  618. return (0);
  619. if (!is_hex(h + afiol_filesize_offset, afiol_filesize_size))
  620. return (0);
  621. return (1);
  622. }
  623. static int
  624. find_odc_header(struct archive_read *a)
  625. {
  626. const void *h;
  627. const char *p, *q;
  628. size_t skip, skipped = 0;
  629. ssize_t bytes;
  630. for (;;) {
  631. h = __archive_read_ahead(a, odc_header_size, &bytes);
  632. if (h == NULL)
  633. return (ARCHIVE_FATAL);
  634. p = h;
  635. q = p + bytes;
  636. /* Try the typical case first, then go into the slow search.*/
  637. if (memcmp("070707", p, 6) == 0 && is_octal(p, odc_header_size))
  638. return (ARCHIVE_OK);
  639. if (memcmp("070727", p, 6) == 0 && is_afio_large(p, bytes)) {
  640. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
  641. return (ARCHIVE_OK);
  642. }
  643. /*
  644. * Scan ahead until we find something that looks
  645. * like an odc header.
  646. */
  647. while (p + odc_header_size <= q) {
  648. switch (p[5]) {
  649. case '7':
  650. if ((memcmp("070707", p, 6) == 0
  651. && is_octal(p, odc_header_size))
  652. || (memcmp("070727", p, 6) == 0
  653. && is_afio_large(p, q - p))) {
  654. skip = p - (const char *)h;
  655. __archive_read_consume(a, skip);
  656. skipped += skip;
  657. if (p[4] == '2')
  658. a->archive.archive_format =
  659. ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
  660. if (skipped > 0) {
  661. archive_set_error(&a->archive,
  662. 0,
  663. "Skipped %d bytes before "
  664. "finding valid header",
  665. (int)skipped);
  666. return (ARCHIVE_WARN);
  667. }
  668. return (ARCHIVE_OK);
  669. }
  670. p += 2;
  671. break;
  672. case '0':
  673. p++;
  674. break;
  675. default:
  676. p += 6;
  677. break;
  678. }
  679. }
  680. skip = p - (const char *)h;
  681. __archive_read_consume(a, skip);
  682. skipped += skip;
  683. }
  684. }
  685. static int
  686. header_odc(struct archive_read *a, struct cpio *cpio,
  687. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  688. {
  689. const void *h;
  690. int r;
  691. const char *header;
  692. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
  693. a->archive.archive_format_name = "POSIX octet-oriented cpio";
  694. /* Find the start of the next header. */
  695. r = find_odc_header(a);
  696. if (r < ARCHIVE_WARN)
  697. return (r);
  698. if (a->archive.archive_format == ARCHIVE_FORMAT_CPIO_AFIO_LARGE) {
  699. int r2 = (header_afiol(a, cpio, entry, namelength, name_pad));
  700. if (r2 == ARCHIVE_OK)
  701. return (r);
  702. else
  703. return (r2);
  704. }
  705. /* Read fixed-size portion of header. */
  706. h = __archive_read_ahead(a, odc_header_size, NULL);
  707. if (h == NULL)
  708. return (ARCHIVE_FATAL);
  709. /* Parse out octal fields. */
  710. header = (const char *)h;
  711. archive_entry_set_dev(entry,
  712. (dev_t)atol8(header + odc_dev_offset, odc_dev_size));
  713. archive_entry_set_ino(entry, atol8(header + odc_ino_offset, odc_ino_size));
  714. archive_entry_set_mode(entry,
  715. (mode_t)atol8(header + odc_mode_offset, odc_mode_size));
  716. archive_entry_set_uid(entry, atol8(header + odc_uid_offset, odc_uid_size));
  717. archive_entry_set_gid(entry, atol8(header + odc_gid_offset, odc_gid_size));
  718. archive_entry_set_nlink(entry,
  719. (unsigned int)atol8(header + odc_nlink_offset, odc_nlink_size));
  720. archive_entry_set_rdev(entry,
  721. (dev_t)atol8(header + odc_rdev_offset, odc_rdev_size));
  722. archive_entry_set_mtime(entry, atol8(header + odc_mtime_offset, odc_mtime_size), 0);
  723. *namelength = (size_t)atol8(header + odc_namesize_offset, odc_namesize_size);
  724. *name_pad = 0; /* No padding of filename. */
  725. /*
  726. * Note: entry_bytes_remaining is at least 64 bits and
  727. * therefore guaranteed to be big enough for a 33-bit file
  728. * size.
  729. */
  730. cpio->entry_bytes_remaining =
  731. atol8(header + odc_filesize_offset, odc_filesize_size);
  732. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  733. cpio->entry_padding = 0;
  734. __archive_read_consume(a, odc_header_size);
  735. return (r);
  736. }
  737. /*
  738. * NOTE: if a filename suffix is ".z", it is the file gziped by afio.
  739. * it would be nice that we can show uncompressed file size and we can
  740. * uncompressed file contents automatically, unfortunately we have nothing
  741. * to get a uncompressed file size while reading each header. it means
  742. * we also cannot uncompressed file contens under the our framework.
  743. */
  744. static int
  745. header_afiol(struct archive_read *a, struct cpio *cpio,
  746. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  747. {
  748. const void *h;
  749. const char *header;
  750. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_AFIO_LARGE;
  751. a->archive.archive_format_name = "afio large ASCII";
  752. /* Read fixed-size portion of header. */
  753. h = __archive_read_ahead(a, afiol_header_size, NULL);
  754. if (h == NULL)
  755. return (ARCHIVE_FATAL);
  756. /* Parse out octal fields. */
  757. header = (const char *)h;
  758. archive_entry_set_dev(entry,
  759. (dev_t)atol16(header + afiol_dev_offset, afiol_dev_size));
  760. archive_entry_set_ino(entry, atol16(header + afiol_ino_offset, afiol_ino_size));
  761. archive_entry_set_mode(entry,
  762. (mode_t)atol8(header + afiol_mode_offset, afiol_mode_size));
  763. archive_entry_set_uid(entry, atol16(header + afiol_uid_offset, afiol_uid_size));
  764. archive_entry_set_gid(entry, atol16(header + afiol_gid_offset, afiol_gid_size));
  765. archive_entry_set_nlink(entry,
  766. (unsigned int)atol16(header + afiol_nlink_offset, afiol_nlink_size));
  767. archive_entry_set_rdev(entry,
  768. (dev_t)atol16(header + afiol_rdev_offset, afiol_rdev_size));
  769. archive_entry_set_mtime(entry, atol16(header + afiol_mtime_offset, afiol_mtime_size), 0);
  770. *namelength = (size_t)atol16(header + afiol_namesize_offset, afiol_namesize_size);
  771. *name_pad = 0; /* No padding of filename. */
  772. cpio->entry_bytes_remaining =
  773. atol16(header + afiol_filesize_offset, afiol_filesize_size);
  774. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  775. cpio->entry_padding = 0;
  776. __archive_read_consume(a, afiol_header_size);
  777. return (ARCHIVE_OK);
  778. }
  779. static int
  780. header_bin_le(struct archive_read *a, struct cpio *cpio,
  781. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  782. {
  783. const void *h;
  784. const unsigned char *header;
  785. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
  786. a->archive.archive_format_name = "cpio (little-endian binary)";
  787. /* Read fixed-size portion of header. */
  788. h = __archive_read_ahead(a, bin_header_size, NULL);
  789. if (h == NULL)
  790. return (ARCHIVE_FATAL);
  791. /* Parse out binary fields. */
  792. header = (const unsigned char *)h;
  793. archive_entry_set_dev(entry, header[bin_dev_offset] + header[bin_dev_offset + 1] * 256);
  794. archive_entry_set_ino(entry, header[bin_ino_offset] + header[bin_ino_offset + 1] * 256);
  795. archive_entry_set_mode(entry, header[bin_mode_offset] + header[bin_mode_offset + 1] * 256);
  796. archive_entry_set_uid(entry, header[bin_uid_offset] + header[bin_uid_offset + 1] * 256);
  797. archive_entry_set_gid(entry, header[bin_gid_offset] + header[bin_gid_offset + 1] * 256);
  798. archive_entry_set_nlink(entry, header[bin_nlink_offset] + header[bin_nlink_offset + 1] * 256);
  799. archive_entry_set_rdev(entry, header[bin_rdev_offset] + header[bin_rdev_offset + 1] * 256);
  800. archive_entry_set_mtime(entry, le4(header + bin_mtime_offset), 0);
  801. *namelength = header[bin_namesize_offset] + header[bin_namesize_offset + 1] * 256;
  802. *name_pad = *namelength & 1; /* Pad to even. */
  803. cpio->entry_bytes_remaining = le4(header + bin_filesize_offset);
  804. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  805. cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
  806. __archive_read_consume(a, bin_header_size);
  807. return (ARCHIVE_OK);
  808. }
  809. static int
  810. header_bin_be(struct archive_read *a, struct cpio *cpio,
  811. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  812. {
  813. const void *h;
  814. const unsigned char *header;
  815. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
  816. a->archive.archive_format_name = "cpio (big-endian binary)";
  817. /* Read fixed-size portion of header. */
  818. h = __archive_read_ahead(a, bin_header_size, NULL);
  819. if (h == NULL)
  820. return (ARCHIVE_FATAL);
  821. /* Parse out binary fields. */
  822. header = (const unsigned char *)h;
  823. archive_entry_set_dev(entry, header[bin_dev_offset] * 256 + header[bin_dev_offset + 1]);
  824. archive_entry_set_ino(entry, header[bin_ino_offset] * 256 + header[bin_ino_offset + 1]);
  825. archive_entry_set_mode(entry, header[bin_mode_offset] * 256 + header[bin_mode_offset + 1]);
  826. archive_entry_set_uid(entry, header[bin_uid_offset] * 256 + header[bin_uid_offset + 1]);
  827. archive_entry_set_gid(entry, header[bin_gid_offset] * 256 + header[bin_gid_offset + 1]);
  828. archive_entry_set_nlink(entry, header[bin_nlink_offset] * 256 + header[bin_nlink_offset + 1]);
  829. archive_entry_set_rdev(entry, header[bin_rdev_offset] * 256 + header[bin_rdev_offset + 1]);
  830. archive_entry_set_mtime(entry, be4(header + bin_mtime_offset), 0);
  831. *namelength = header[bin_namesize_offset] * 256 + header[bin_namesize_offset + 1];
  832. *name_pad = *namelength & 1; /* Pad to even. */
  833. cpio->entry_bytes_remaining = be4(header + bin_filesize_offset);
  834. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  835. cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
  836. __archive_read_consume(a, bin_header_size);
  837. return (ARCHIVE_OK);
  838. }
  839. static int
  840. archive_read_format_cpio_cleanup(struct archive_read *a)
  841. {
  842. struct cpio *cpio;
  843. cpio = (struct cpio *)(a->format->data);
  844. /* Free inode->name map */
  845. while (cpio->links_head != NULL) {
  846. struct links_entry *lp = cpio->links_head->next;
  847. if (cpio->links_head->name)
  848. free(cpio->links_head->name);
  849. free(cpio->links_head);
  850. cpio->links_head = lp;
  851. }
  852. free(cpio);
  853. (a->format->data) = NULL;
  854. return (ARCHIVE_OK);
  855. }
  856. static int
  857. le4(const unsigned char *p)
  858. {
  859. return ((p[0]<<16) + (p[1]<<24) + (p[2]<<0) + (p[3]<<8));
  860. }
  861. static int
  862. be4(const unsigned char *p)
  863. {
  864. return ((p[0]<<24) + (p[1]<<16) + (p[2]<<8) + (p[3]));
  865. }
  866. /*
  867. * Note that this implementation does not (and should not!) obey
  868. * locale settings; you cannot simply substitute strtol here, since
  869. * it does obey locale.
  870. */
  871. static int64_t
  872. atol8(const char *p, unsigned char_cnt)
  873. {
  874. int64_t l;
  875. int digit;
  876. l = 0;
  877. while (char_cnt-- > 0) {
  878. if (*p >= '0' && *p <= '7')
  879. digit = *p - '0';
  880. else
  881. return (l);
  882. p++;
  883. l <<= 3;
  884. l |= digit;
  885. }
  886. return (l);
  887. }
  888. static int64_t
  889. atol16(const char *p, unsigned char_cnt)
  890. {
  891. int64_t l;
  892. int digit;
  893. l = 0;
  894. while (char_cnt-- > 0) {
  895. if (*p >= 'a' && *p <= 'f')
  896. digit = *p - 'a' + 10;
  897. else if (*p >= 'A' && *p <= 'F')
  898. digit = *p - 'A' + 10;
  899. else if (*p >= '0' && *p <= '9')
  900. digit = *p - '0';
  901. else
  902. return (l);
  903. p++;
  904. l <<= 4;
  905. l |= digit;
  906. }
  907. return (l);
  908. }
  909. static int
  910. record_hardlink(struct archive_read *a,
  911. struct cpio *cpio, struct archive_entry *entry)
  912. {
  913. struct links_entry *le;
  914. dev_t dev;
  915. int64_t ino;
  916. if (archive_entry_nlink(entry) <= 1)
  917. return (ARCHIVE_OK);
  918. dev = archive_entry_dev(entry);
  919. ino = archive_entry_ino64(entry);
  920. /*
  921. * First look in the list of multiply-linked files. If we've
  922. * already dumped it, convert this entry to a hard link entry.
  923. */
  924. for (le = cpio->links_head; le; le = le->next) {
  925. if (le->dev == dev && le->ino == ino) {
  926. archive_entry_copy_hardlink(entry, le->name);
  927. if (--le->links <= 0) {
  928. if (le->previous != NULL)
  929. le->previous->next = le->next;
  930. if (le->next != NULL)
  931. le->next->previous = le->previous;
  932. if (cpio->links_head == le)
  933. cpio->links_head = le->next;
  934. free(le->name);
  935. free(le);
  936. }
  937. return (ARCHIVE_OK);
  938. }
  939. }
  940. le = (struct links_entry *)malloc(sizeof(struct links_entry));
  941. if (le == NULL) {
  942. archive_set_error(&a->archive,
  943. ENOMEM, "Out of memory adding file to list");
  944. return (ARCHIVE_FATAL);
  945. }
  946. if (cpio->links_head != NULL)
  947. cpio->links_head->previous = le;
  948. le->next = cpio->links_head;
  949. le->previous = NULL;
  950. cpio->links_head = le;
  951. le->dev = dev;
  952. le->ino = ino;
  953. le->links = archive_entry_nlink(entry) - 1;
  954. le->name = strdup(archive_entry_pathname(entry));
  955. if (le->name == NULL) {
  956. archive_set_error(&a->archive,
  957. ENOMEM, "Out of memory adding file to list");
  958. return (ARCHIVE_FATAL);
  959. }
  960. return (ARCHIVE_OK);
  961. }