archive_read_support_format_cpio.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_cpio.c,v 1.27 2008/12/06 06:45:15 kientzle Exp $");
  27. #ifdef HAVE_ERRNO_H
  28. #include <errno.h>
  29. #endif
  30. /* #include <stdint.h> */ /* See archive_platform.h */
  31. #ifdef HAVE_STDLIB_H
  32. #include <stdlib.h>
  33. #endif
  34. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. #include "archive.h"
  38. #include "archive_entry.h"
  39. #include "archive_private.h"
  40. #include "archive_read_private.h"
  41. struct cpio_bin_header {
  42. unsigned char c_magic[2];
  43. unsigned char c_dev[2];
  44. unsigned char c_ino[2];
  45. unsigned char c_mode[2];
  46. unsigned char c_uid[2];
  47. unsigned char c_gid[2];
  48. unsigned char c_nlink[2];
  49. unsigned char c_rdev[2];
  50. unsigned char c_mtime[4];
  51. unsigned char c_namesize[2];
  52. unsigned char c_filesize[4];
  53. };
  54. struct cpio_odc_header {
  55. char c_magic[6];
  56. char c_dev[6];
  57. char c_ino[6];
  58. char c_mode[6];
  59. char c_uid[6];
  60. char c_gid[6];
  61. char c_nlink[6];
  62. char c_rdev[6];
  63. char c_mtime[11];
  64. char c_namesize[6];
  65. char c_filesize[11];
  66. };
  67. struct cpio_newc_header {
  68. char c_magic[6];
  69. char c_ino[8];
  70. char c_mode[8];
  71. char c_uid[8];
  72. char c_gid[8];
  73. char c_nlink[8];
  74. char c_mtime[8];
  75. char c_filesize[8];
  76. char c_devmajor[8];
  77. char c_devminor[8];
  78. char c_rdevmajor[8];
  79. char c_rdevminor[8];
  80. char c_namesize[8];
  81. char c_crc[8];
  82. };
  83. struct links_entry {
  84. struct links_entry *next;
  85. struct links_entry *previous;
  86. int links;
  87. dev_t dev;
  88. ino_t ino;
  89. char *name;
  90. };
  91. #define CPIO_MAGIC 0x13141516
  92. struct cpio {
  93. int magic;
  94. int (*read_header)(struct archive_read *, struct cpio *,
  95. struct archive_entry *, size_t *, size_t *);
  96. struct links_entry *links_head;
  97. struct archive_string entry_name;
  98. struct archive_string entry_linkname;
  99. off_t entry_bytes_remaining;
  100. off_t entry_offset;
  101. off_t entry_padding;
  102. };
  103. static int64_t atol16(const char *, unsigned);
  104. static int64_t atol8(const char *, unsigned);
  105. static int archive_read_format_cpio_bid(struct archive_read *);
  106. static int archive_read_format_cpio_cleanup(struct archive_read *);
  107. static int archive_read_format_cpio_read_data(struct archive_read *,
  108. const void **, size_t *, off_t *);
  109. static int archive_read_format_cpio_read_header(struct archive_read *,
  110. struct archive_entry *);
  111. static int be4(const unsigned char *);
  112. static int find_odc_header(struct archive_read *);
  113. static int find_newc_header(struct archive_read *);
  114. static int header_bin_be(struct archive_read *, struct cpio *,
  115. struct archive_entry *, size_t *, size_t *);
  116. static int header_bin_le(struct archive_read *, struct cpio *,
  117. struct archive_entry *, size_t *, size_t *);
  118. static int header_newc(struct archive_read *, struct cpio *,
  119. struct archive_entry *, size_t *, size_t *);
  120. static int header_odc(struct archive_read *, struct cpio *,
  121. struct archive_entry *, size_t *, size_t *);
  122. static int is_octal(const char *, size_t);
  123. static int is_hex(const char *, size_t);
  124. static int le4(const unsigned char *);
  125. static void record_hardlink(struct cpio *cpio, struct archive_entry *entry);
  126. int
  127. archive_read_support_format_cpio(struct archive *_a)
  128. {
  129. struct archive_read *a = (struct archive_read *)_a;
  130. struct cpio *cpio;
  131. int r;
  132. cpio = (struct cpio *)malloc(sizeof(*cpio));
  133. if (cpio == NULL) {
  134. archive_set_error(&a->archive, ENOMEM, "Can't allocate cpio data");
  135. return (ARCHIVE_FATAL);
  136. }
  137. memset(cpio, 0, sizeof(*cpio));
  138. cpio->magic = CPIO_MAGIC;
  139. r = __archive_read_register_format(a,
  140. cpio,
  141. "cpio",
  142. archive_read_format_cpio_bid,
  143. NULL,
  144. archive_read_format_cpio_read_header,
  145. archive_read_format_cpio_read_data,
  146. NULL,
  147. NULL,
  148. NULL,
  149. archive_read_format_cpio_cleanup);
  150. if (r != ARCHIVE_OK)
  151. free(cpio);
  152. return (ARCHIVE_OK);
  153. }
  154. static int
  155. archive_read_format_cpio_bid(struct archive_read *a)
  156. {
  157. const void *h;
  158. const unsigned char *p;
  159. struct cpio *cpio;
  160. int bid;
  161. cpio = (struct cpio *)(a->format->data);
  162. if ((h = __archive_read_ahead(a, 6, NULL)) == NULL)
  163. return (-1);
  164. p = (const unsigned char *)h;
  165. bid = 0;
  166. if (memcmp(p, "070707", 6) == 0) {
  167. /* ASCII cpio archive (odc, POSIX.1) */
  168. cpio->read_header = header_odc;
  169. bid += 48;
  170. /*
  171. * XXX TODO: More verification; Could check that only octal
  172. * digits appear in appropriate header locations. XXX
  173. */
  174. } else if (memcmp(p, "070701", 6) == 0) {
  175. /* ASCII cpio archive (SVR4 without CRC) */
  176. cpio->read_header = header_newc;
  177. bid += 48;
  178. /*
  179. * XXX TODO: More verification; Could check that only hex
  180. * digits appear in appropriate header locations. XXX
  181. */
  182. } else if (memcmp(p, "070702", 6) == 0) {
  183. /* ASCII cpio archive (SVR4 with CRC) */
  184. /* XXX TODO: Flag that we should check the CRC. XXX */
  185. cpio->read_header = header_newc;
  186. bid += 48;
  187. /*
  188. * XXX TODO: More verification; Could check that only hex
  189. * digits appear in appropriate header locations. XXX
  190. */
  191. } else if (p[0] * 256 + p[1] == 070707) {
  192. /* big-endian binary cpio archives */
  193. cpio->read_header = header_bin_be;
  194. bid += 16;
  195. /* Is more verification possible here? */
  196. } else if (p[0] + p[1] * 256 == 070707) {
  197. /* little-endian binary cpio archives */
  198. cpio->read_header = header_bin_le;
  199. bid += 16;
  200. /* Is more verification possible here? */
  201. } else
  202. return (ARCHIVE_WARN);
  203. return (bid);
  204. }
  205. static int
  206. archive_read_format_cpio_read_header(struct archive_read *a,
  207. struct archive_entry *entry)
  208. {
  209. struct cpio *cpio;
  210. const void *h;
  211. size_t namelength;
  212. size_t name_pad;
  213. int r;
  214. cpio = (struct cpio *)(a->format->data);
  215. r = (cpio->read_header(a, cpio, entry, &namelength, &name_pad));
  216. if (r < ARCHIVE_WARN)
  217. return (r);
  218. /* Read name from buffer. */
  219. h = __archive_read_ahead(a, namelength + name_pad, NULL);
  220. if (h == NULL)
  221. return (ARCHIVE_FATAL);
  222. __archive_read_consume(a, namelength + name_pad);
  223. archive_strncpy(&cpio->entry_name, (const char *)h, namelength);
  224. archive_entry_set_pathname(entry, cpio->entry_name.s);
  225. cpio->entry_offset = 0;
  226. /* If this is a symlink, read the link contents. */
  227. if (archive_entry_filetype(entry) == AE_IFLNK) {
  228. h = __archive_read_ahead(a, cpio->entry_bytes_remaining, NULL);
  229. if (h == NULL)
  230. return (ARCHIVE_FATAL);
  231. __archive_read_consume(a, cpio->entry_bytes_remaining);
  232. archive_strncpy(&cpio->entry_linkname, (const char *)h,
  233. cpio->entry_bytes_remaining);
  234. archive_entry_set_symlink(entry, cpio->entry_linkname.s);
  235. cpio->entry_bytes_remaining = 0;
  236. }
  237. /* Compare name to "TRAILER!!!" to test for end-of-archive. */
  238. if (namelength == 11 && strcmp((const char *)h, "TRAILER!!!") == 0) {
  239. /* TODO: Store file location of start of block. */
  240. archive_set_error(&a->archive, 0, NULL);
  241. return (ARCHIVE_EOF);
  242. }
  243. /* Detect and record hardlinks to previously-extracted entries. */
  244. record_hardlink(cpio, entry);
  245. return (r);
  246. }
  247. static int
  248. archive_read_format_cpio_read_data(struct archive_read *a,
  249. const void **buff, size_t *size, off_t *offset)
  250. {
  251. ssize_t bytes_read;
  252. struct cpio *cpio;
  253. cpio = (struct cpio *)(a->format->data);
  254. if (cpio->entry_bytes_remaining > 0) {
  255. *buff = __archive_read_ahead(a, 1, &bytes_read);
  256. if (bytes_read <= 0)
  257. return (ARCHIVE_FATAL);
  258. if (bytes_read > cpio->entry_bytes_remaining)
  259. bytes_read = cpio->entry_bytes_remaining;
  260. *size = bytes_read;
  261. *offset = cpio->entry_offset;
  262. cpio->entry_offset += bytes_read;
  263. cpio->entry_bytes_remaining -= bytes_read;
  264. __archive_read_consume(a, bytes_read);
  265. return (ARCHIVE_OK);
  266. } else {
  267. while (cpio->entry_padding > 0) {
  268. *buff = __archive_read_ahead(a, 1, &bytes_read);
  269. if (bytes_read <= 0)
  270. return (ARCHIVE_FATAL);
  271. if (bytes_read > cpio->entry_padding)
  272. bytes_read = cpio->entry_padding;
  273. __archive_read_consume(a, bytes_read);
  274. cpio->entry_padding -= bytes_read;
  275. }
  276. *buff = NULL;
  277. *size = 0;
  278. *offset = cpio->entry_offset;
  279. return (ARCHIVE_EOF);
  280. }
  281. }
  282. /*
  283. * Skip forward to the next cpio newc header by searching for the
  284. * 07070[12] string. This should be generalized and merged with
  285. * find_odc_header below.
  286. */
  287. static int
  288. is_hex(const char *p, size_t len)
  289. {
  290. while (len-- > 0) {
  291. if ((*p >= '0' && *p <= '9')
  292. || (*p >= 'a' && *p <= 'f')
  293. || (*p >= 'A' && *p <= 'F'))
  294. ++p;
  295. else
  296. return (0);
  297. }
  298. return (1);
  299. }
  300. static int
  301. find_newc_header(struct archive_read *a)
  302. {
  303. const void *h;
  304. const char *p, *q;
  305. size_t skip, skipped = 0;
  306. ssize_t bytes;
  307. for (;;) {
  308. h = __archive_read_ahead(a, sizeof(struct cpio_newc_header), &bytes);
  309. if (h == NULL)
  310. return (ARCHIVE_FATAL);
  311. p = h;
  312. q = p + bytes;
  313. /* Try the typical case first, then go into the slow search.*/
  314. if (memcmp("07070", p, 5) == 0
  315. && (p[5] == '1' || p[5] == '2')
  316. && is_hex(p, sizeof(struct cpio_newc_header)))
  317. return (ARCHIVE_OK);
  318. /*
  319. * Scan ahead until we find something that looks
  320. * like an odc header.
  321. */
  322. while (p + sizeof(struct cpio_newc_header) < q) {
  323. switch (p[5]) {
  324. case '1':
  325. case '2':
  326. if (memcmp("07070", p, 5) == 0
  327. && is_hex(p, sizeof(struct cpio_newc_header))) {
  328. skip = p - (const char *)h;
  329. __archive_read_consume(a, skip);
  330. skipped += skip;
  331. if (skipped > 0) {
  332. archive_set_error(&a->archive,
  333. 0,
  334. "Skipped %d bytes before "
  335. "finding valid header",
  336. (int)skipped);
  337. return (ARCHIVE_WARN);
  338. }
  339. return (ARCHIVE_OK);
  340. }
  341. p += 2;
  342. break;
  343. case '0':
  344. p++;
  345. break;
  346. default:
  347. p += 6;
  348. break;
  349. }
  350. }
  351. skip = p - (const char *)h;
  352. __archive_read_consume(a, skip);
  353. skipped += skip;
  354. }
  355. }
  356. static int
  357. header_newc(struct archive_read *a, struct cpio *cpio,
  358. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  359. {
  360. const void *h;
  361. const struct cpio_newc_header *header;
  362. int r;
  363. r = find_newc_header(a);
  364. if (r < ARCHIVE_WARN)
  365. return (r);
  366. /* Read fixed-size portion of header. */
  367. h = __archive_read_ahead(a, sizeof(struct cpio_newc_header), NULL);
  368. if (h == NULL)
  369. return (ARCHIVE_FATAL);
  370. __archive_read_consume(a, sizeof(struct cpio_newc_header));
  371. /* Parse out hex fields. */
  372. header = (const struct cpio_newc_header *)h;
  373. if (memcmp(header->c_magic, "070701", 6) == 0) {
  374. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
  375. a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
  376. } else if (memcmp(header->c_magic, "070702", 6) == 0) {
  377. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
  378. a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
  379. } else {
  380. /* TODO: Abort here? */
  381. }
  382. archive_entry_set_devmajor(entry, atol16(header->c_devmajor, sizeof(header->c_devmajor)));
  383. archive_entry_set_devminor(entry, atol16(header->c_devminor, sizeof(header->c_devminor)));
  384. archive_entry_set_ino(entry, atol16(header->c_ino, sizeof(header->c_ino)));
  385. archive_entry_set_mode(entry, atol16(header->c_mode, sizeof(header->c_mode)));
  386. archive_entry_set_uid(entry, atol16(header->c_uid, sizeof(header->c_uid)));
  387. archive_entry_set_gid(entry, atol16(header->c_gid, sizeof(header->c_gid)));
  388. archive_entry_set_nlink(entry, atol16(header->c_nlink, sizeof(header->c_nlink)));
  389. archive_entry_set_rdevmajor(entry, atol16(header->c_rdevmajor, sizeof(header->c_rdevmajor)));
  390. archive_entry_set_rdevminor(entry, atol16(header->c_rdevminor, sizeof(header->c_rdevminor)));
  391. archive_entry_set_mtime(entry, atol16(header->c_mtime, sizeof(header->c_mtime)), 0);
  392. *namelength = atol16(header->c_namesize, sizeof(header->c_namesize));
  393. /* Pad name to 2 more than a multiple of 4. */
  394. *name_pad = (2 - *namelength) & 3;
  395. /* Make sure that the padded name length fits into size_t. */
  396. if ((size_t)(*namelength + *name_pad) < *namelength) {
  397. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  398. "cpio archive has invalid namelength");
  399. return (ARCHIVE_FATAL);
  400. }
  401. /*
  402. * Note: entry_bytes_remaining is at least 64 bits and
  403. * therefore guaranteed to be big enough for a 33-bit file
  404. * size.
  405. */
  406. cpio->entry_bytes_remaining =
  407. atol16(header->c_filesize, sizeof(header->c_filesize));
  408. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  409. /* Pad file contents to a multiple of 4. */
  410. cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
  411. return (r);
  412. }
  413. /*
  414. * Skip forward to the next cpio odc header by searching for the
  415. * 070707 string. This is a hand-optimized search that could
  416. * probably be easily generalized to handle all character-based
  417. * cpio variants.
  418. */
  419. static int
  420. is_octal(const char *p, size_t len)
  421. {
  422. while (len-- > 0) {
  423. if (*p < '0' || *p > '7')
  424. return (0);
  425. ++p;
  426. }
  427. return (1);
  428. }
  429. static int
  430. find_odc_header(struct archive_read *a)
  431. {
  432. const void *h;
  433. const char *p, *q;
  434. size_t skip, skipped = 0;
  435. ssize_t bytes;
  436. for (;;) {
  437. h = __archive_read_ahead(a, sizeof(struct cpio_odc_header), &bytes);
  438. if (h == NULL)
  439. return (ARCHIVE_FATAL);
  440. p = h;
  441. q = p + bytes;
  442. /* Try the typical case first, then go into the slow search.*/
  443. if (memcmp("070707", p, 6) == 0
  444. && is_octal(p, sizeof(struct cpio_odc_header)))
  445. return (ARCHIVE_OK);
  446. /*
  447. * Scan ahead until we find something that looks
  448. * like an odc header.
  449. */
  450. while (p + sizeof(struct cpio_odc_header) < q) {
  451. switch (p[5]) {
  452. case '7':
  453. if (memcmp("070707", p, 6) == 0
  454. && is_octal(p, sizeof(struct cpio_odc_header))) {
  455. skip = p - (const char *)h;
  456. __archive_read_consume(a, skip);
  457. skipped += skip;
  458. if (skipped > 0) {
  459. archive_set_error(&a->archive,
  460. 0,
  461. "Skipped %d bytes before "
  462. "finding valid header",
  463. (int)skipped);
  464. return (ARCHIVE_WARN);
  465. }
  466. return (ARCHIVE_OK);
  467. }
  468. p += 2;
  469. break;
  470. case '0':
  471. p++;
  472. break;
  473. default:
  474. p += 6;
  475. break;
  476. }
  477. }
  478. skip = p - (const char *)h;
  479. __archive_read_consume(a, skip);
  480. skipped += skip;
  481. }
  482. }
  483. static int
  484. header_odc(struct archive_read *a, struct cpio *cpio,
  485. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  486. {
  487. const void *h;
  488. int r;
  489. const struct cpio_odc_header *header;
  490. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
  491. a->archive.archive_format_name = "POSIX octet-oriented cpio";
  492. /* Find the start of the next header. */
  493. r = find_odc_header(a);
  494. if (r < ARCHIVE_WARN)
  495. return (r);
  496. /* Read fixed-size portion of header. */
  497. h = __archive_read_ahead(a, sizeof(struct cpio_odc_header), NULL);
  498. if (h == NULL)
  499. return (ARCHIVE_FATAL);
  500. __archive_read_consume(a, sizeof(struct cpio_odc_header));
  501. /* Parse out octal fields. */
  502. header = (const struct cpio_odc_header *)h;
  503. archive_entry_set_dev(entry, atol8(header->c_dev, sizeof(header->c_dev)));
  504. archive_entry_set_ino(entry, atol8(header->c_ino, sizeof(header->c_ino)));
  505. archive_entry_set_mode(entry, atol8(header->c_mode, sizeof(header->c_mode)));
  506. archive_entry_set_uid(entry, atol8(header->c_uid, sizeof(header->c_uid)));
  507. archive_entry_set_gid(entry, atol8(header->c_gid, sizeof(header->c_gid)));
  508. archive_entry_set_nlink(entry, atol8(header->c_nlink, sizeof(header->c_nlink)));
  509. archive_entry_set_rdev(entry, atol8(header->c_rdev, sizeof(header->c_rdev)));
  510. archive_entry_set_mtime(entry, atol8(header->c_mtime, sizeof(header->c_mtime)), 0);
  511. *namelength = atol8(header->c_namesize, sizeof(header->c_namesize));
  512. *name_pad = 0; /* No padding of filename. */
  513. /*
  514. * Note: entry_bytes_remaining is at least 64 bits and
  515. * therefore guaranteed to be big enough for a 33-bit file
  516. * size.
  517. */
  518. cpio->entry_bytes_remaining =
  519. atol8(header->c_filesize, sizeof(header->c_filesize));
  520. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  521. cpio->entry_padding = 0;
  522. return (r);
  523. }
  524. static int
  525. header_bin_le(struct archive_read *a, struct cpio *cpio,
  526. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  527. {
  528. const void *h;
  529. const struct cpio_bin_header *header;
  530. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
  531. a->archive.archive_format_name = "cpio (little-endian binary)";
  532. /* Read fixed-size portion of header. */
  533. h = __archive_read_ahead(a, sizeof(struct cpio_bin_header), NULL);
  534. if (h == NULL)
  535. return (ARCHIVE_FATAL);
  536. __archive_read_consume(a, sizeof(struct cpio_bin_header));
  537. /* Parse out binary fields. */
  538. header = (const struct cpio_bin_header *)h;
  539. archive_entry_set_dev(entry, header->c_dev[0] + header->c_dev[1] * 256);
  540. archive_entry_set_ino(entry, header->c_ino[0] + header->c_ino[1] * 256);
  541. archive_entry_set_mode(entry, header->c_mode[0] + header->c_mode[1] * 256);
  542. archive_entry_set_uid(entry, header->c_uid[0] + header->c_uid[1] * 256);
  543. archive_entry_set_gid(entry, header->c_gid[0] + header->c_gid[1] * 256);
  544. archive_entry_set_nlink(entry, header->c_nlink[0] + header->c_nlink[1] * 256);
  545. archive_entry_set_rdev(entry, header->c_rdev[0] + header->c_rdev[1] * 256);
  546. archive_entry_set_mtime(entry, le4(header->c_mtime), 0);
  547. *namelength = header->c_namesize[0] + header->c_namesize[1] * 256;
  548. *name_pad = *namelength & 1; /* Pad to even. */
  549. cpio->entry_bytes_remaining = le4(header->c_filesize);
  550. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  551. cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
  552. return (ARCHIVE_OK);
  553. }
  554. static int
  555. header_bin_be(struct archive_read *a, struct cpio *cpio,
  556. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  557. {
  558. const void *h;
  559. const struct cpio_bin_header *header;
  560. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
  561. a->archive.archive_format_name = "cpio (big-endian binary)";
  562. /* Read fixed-size portion of header. */
  563. h = __archive_read_ahead(a, sizeof(struct cpio_bin_header), NULL);
  564. if (h == NULL)
  565. return (ARCHIVE_FATAL);
  566. __archive_read_consume(a, sizeof(struct cpio_bin_header));
  567. /* Parse out binary fields. */
  568. header = (const struct cpio_bin_header *)h;
  569. archive_entry_set_dev(entry, header->c_dev[0] * 256 + header->c_dev[1]);
  570. archive_entry_set_ino(entry, header->c_ino[0] * 256 + header->c_ino[1]);
  571. archive_entry_set_mode(entry, header->c_mode[0] * 256 + header->c_mode[1]);
  572. archive_entry_set_uid(entry, header->c_uid[0] * 256 + header->c_uid[1]);
  573. archive_entry_set_gid(entry, header->c_gid[0] * 256 + header->c_gid[1]);
  574. archive_entry_set_nlink(entry, header->c_nlink[0] * 256 + header->c_nlink[1]);
  575. archive_entry_set_rdev(entry, header->c_rdev[0] * 256 + header->c_rdev[1]);
  576. archive_entry_set_mtime(entry, be4(header->c_mtime), 0);
  577. *namelength = header->c_namesize[0] * 256 + header->c_namesize[1];
  578. *name_pad = *namelength & 1; /* Pad to even. */
  579. cpio->entry_bytes_remaining = be4(header->c_filesize);
  580. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  581. cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
  582. return (ARCHIVE_OK);
  583. }
  584. static int
  585. archive_read_format_cpio_cleanup(struct archive_read *a)
  586. {
  587. struct cpio *cpio;
  588. cpio = (struct cpio *)(a->format->data);
  589. /* Free inode->name map */
  590. while (cpio->links_head != NULL) {
  591. struct links_entry *lp = cpio->links_head->next;
  592. if (cpio->links_head->name)
  593. free(cpio->links_head->name);
  594. free(cpio->links_head);
  595. cpio->links_head = lp;
  596. }
  597. archive_string_free(&cpio->entry_name);
  598. free(cpio);
  599. (a->format->data) = NULL;
  600. return (ARCHIVE_OK);
  601. }
  602. static int
  603. le4(const unsigned char *p)
  604. {
  605. return ((p[0]<<16) + (p[1]<<24) + (p[2]<<0) + (p[3]<<8));
  606. }
  607. static int
  608. be4(const unsigned char *p)
  609. {
  610. return (p[0] + (p[1]<<8) + (p[2]<<16) + (p[3]<<24));
  611. }
  612. /*
  613. * Note that this implementation does not (and should not!) obey
  614. * locale settings; you cannot simply substitute strtol here, since
  615. * it does obey locale.
  616. */
  617. static int64_t
  618. atol8(const char *p, unsigned char_cnt)
  619. {
  620. int64_t l;
  621. int digit;
  622. l = 0;
  623. while (char_cnt-- > 0) {
  624. if (*p >= '0' && *p <= '7')
  625. digit = *p - '0';
  626. else
  627. return (l);
  628. p++;
  629. l <<= 3;
  630. l |= digit;
  631. }
  632. return (l);
  633. }
  634. static int64_t
  635. atol16(const char *p, unsigned char_cnt)
  636. {
  637. int64_t l;
  638. int digit;
  639. l = 0;
  640. while (char_cnt-- > 0) {
  641. if (*p >= 'a' && *p <= 'f')
  642. digit = *p - 'a' + 10;
  643. else if (*p >= 'A' && *p <= 'F')
  644. digit = *p - 'A' + 10;
  645. else if (*p >= '0' && *p <= '9')
  646. digit = *p - '0';
  647. else
  648. return (l);
  649. p++;
  650. l <<= 4;
  651. l |= digit;
  652. }
  653. return (l);
  654. }
  655. static void
  656. record_hardlink(struct cpio *cpio, struct archive_entry *entry)
  657. {
  658. struct links_entry *le;
  659. dev_t dev;
  660. ino_t ino;
  661. dev = archive_entry_dev(entry);
  662. ino = archive_entry_ino(entry);
  663. /*
  664. * First look in the list of multiply-linked files. If we've
  665. * already dumped it, convert this entry to a hard link entry.
  666. */
  667. for (le = cpio->links_head; le; le = le->next) {
  668. if (le->dev == dev && le->ino == ino) {
  669. archive_entry_copy_hardlink(entry, le->name);
  670. if (--le->links <= 0) {
  671. if (le->previous != NULL)
  672. le->previous->next = le->next;
  673. if (le->next != NULL)
  674. le->next->previous = le->previous;
  675. if (cpio->links_head == le)
  676. cpio->links_head = le->next;
  677. free(le->name);
  678. free(le);
  679. }
  680. return;
  681. }
  682. }
  683. le = (struct links_entry *)malloc(sizeof(struct links_entry));
  684. if (le == NULL)
  685. __archive_errx(1, "Out of memory adding file to list");
  686. if (cpio->links_head != NULL)
  687. cpio->links_head->previous = le;
  688. le->next = cpio->links_head;
  689. le->previous = NULL;
  690. cpio->links_head = le;
  691. le->dev = dev;
  692. le->ino = ino;
  693. le->links = archive_entry_nlink(entry) - 1;
  694. le->name = strdup(archive_entry_pathname(entry));
  695. if (le->name == NULL)
  696. __archive_errx(1, "Out of memory adding file to list");
  697. }