archive_read_support_format_cpio.c 33 KB

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