archive_read_support_format_cpio.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  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. int64_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. archive_read_format_cpio_cleanup);
  148. if (r != ARCHIVE_OK)
  149. free(cpio);
  150. return (ARCHIVE_OK);
  151. }
  152. static int
  153. archive_read_format_cpio_bid(struct archive_read *a)
  154. {
  155. const void *h;
  156. const unsigned char *p;
  157. struct cpio *cpio;
  158. int bid;
  159. cpio = (struct cpio *)(a->format->data);
  160. if ((h = __archive_read_ahead(a, 6, NULL)) == NULL)
  161. return (-1);
  162. p = (const unsigned char *)h;
  163. bid = 0;
  164. if (memcmp(p, "070707", 6) == 0) {
  165. /* ASCII cpio archive (odc, POSIX.1) */
  166. cpio->read_header = header_odc;
  167. bid += 48;
  168. /*
  169. * XXX TODO: More verification; Could check that only octal
  170. * digits appear in appropriate header locations. XXX
  171. */
  172. } else if (memcmp(p, "070701", 6) == 0) {
  173. /* ASCII cpio archive (SVR4 without CRC) */
  174. cpio->read_header = header_newc;
  175. bid += 48;
  176. /*
  177. * XXX TODO: More verification; Could check that only hex
  178. * digits appear in appropriate header locations. XXX
  179. */
  180. } else if (memcmp(p, "070702", 6) == 0) {
  181. /* ASCII cpio archive (SVR4 with CRC) */
  182. /* XXX TODO: Flag that we should check the CRC. XXX */
  183. cpio->read_header = header_newc;
  184. bid += 48;
  185. /*
  186. * XXX TODO: More verification; Could check that only hex
  187. * digits appear in appropriate header locations. XXX
  188. */
  189. } else if (p[0] * 256 + p[1] == 070707) {
  190. /* big-endian binary cpio archives */
  191. cpio->read_header = header_bin_be;
  192. bid += 16;
  193. /* Is more verification possible here? */
  194. } else if (p[0] + p[1] * 256 == 070707) {
  195. /* little-endian binary cpio archives */
  196. cpio->read_header = header_bin_le;
  197. bid += 16;
  198. /* Is more verification possible here? */
  199. } else
  200. return (ARCHIVE_WARN);
  201. return (bid);
  202. }
  203. static int
  204. archive_read_format_cpio_read_header(struct archive_read *a,
  205. struct archive_entry *entry)
  206. {
  207. struct cpio *cpio;
  208. const void *h;
  209. size_t namelength;
  210. size_t name_pad;
  211. int r;
  212. cpio = (struct cpio *)(a->format->data);
  213. r = (cpio->read_header(a, cpio, entry, &namelength, &name_pad));
  214. if (r < ARCHIVE_WARN)
  215. return (r);
  216. /* Read name from buffer. */
  217. h = __archive_read_ahead(a, namelength + name_pad, NULL);
  218. if (h == NULL)
  219. return (ARCHIVE_FATAL);
  220. __archive_read_consume(a, namelength + name_pad);
  221. archive_strncpy(&cpio->entry_name, (const char *)h, namelength);
  222. archive_entry_set_pathname(entry, cpio->entry_name.s);
  223. cpio->entry_offset = 0;
  224. /* If this is a symlink, read the link contents. */
  225. if (archive_entry_filetype(entry) == AE_IFLNK) {
  226. h = __archive_read_ahead(a, cpio->entry_bytes_remaining, NULL);
  227. if (h == NULL)
  228. return (ARCHIVE_FATAL);
  229. __archive_read_consume(a, cpio->entry_bytes_remaining);
  230. archive_strncpy(&cpio->entry_linkname, (const char *)h,
  231. cpio->entry_bytes_remaining);
  232. archive_entry_set_symlink(entry, cpio->entry_linkname.s);
  233. cpio->entry_bytes_remaining = 0;
  234. }
  235. /* XXX TODO: If the full mode is 0160200, then this is a Solaris
  236. * ACL description for the following entry. Read this body
  237. * and parse it as a Solaris-style ACL, then read the next
  238. * header. XXX */
  239. /* Compare name to "TRAILER!!!" to test for end-of-archive. */
  240. if (namelength == 11 && strcmp((const char *)h, "TRAILER!!!") == 0) {
  241. /* TODO: Store file location of start of block. */
  242. archive_set_error(&a->archive, 0, NULL);
  243. return (ARCHIVE_EOF);
  244. }
  245. /* Detect and record hardlinks to previously-extracted entries. */
  246. record_hardlink(cpio, entry);
  247. return (r);
  248. }
  249. static int
  250. archive_read_format_cpio_read_data(struct archive_read *a,
  251. const void **buff, size_t *size, off_t *offset)
  252. {
  253. ssize_t bytes_read;
  254. struct cpio *cpio;
  255. cpio = (struct cpio *)(a->format->data);
  256. if (cpio->entry_bytes_remaining > 0) {
  257. *buff = __archive_read_ahead(a, 1, &bytes_read);
  258. if (bytes_read <= 0)
  259. return (ARCHIVE_FATAL);
  260. if (bytes_read > cpio->entry_bytes_remaining)
  261. bytes_read = cpio->entry_bytes_remaining;
  262. *size = bytes_read;
  263. *offset = cpio->entry_offset;
  264. cpio->entry_offset += bytes_read;
  265. cpio->entry_bytes_remaining -= bytes_read;
  266. __archive_read_consume(a, bytes_read);
  267. return (ARCHIVE_OK);
  268. } else {
  269. while (cpio->entry_padding > 0) {
  270. *buff = __archive_read_ahead(a, 1, &bytes_read);
  271. if (bytes_read <= 0)
  272. return (ARCHIVE_FATAL);
  273. if (bytes_read > cpio->entry_padding)
  274. bytes_read = cpio->entry_padding;
  275. __archive_read_consume(a, bytes_read);
  276. cpio->entry_padding -= bytes_read;
  277. }
  278. *buff = NULL;
  279. *size = 0;
  280. *offset = cpio->entry_offset;
  281. return (ARCHIVE_EOF);
  282. }
  283. }
  284. /*
  285. * Skip forward to the next cpio newc header by searching for the
  286. * 07070[12] string. This should be generalized and merged with
  287. * find_odc_header below.
  288. */
  289. static int
  290. is_hex(const char *p, size_t len)
  291. {
  292. while (len-- > 0) {
  293. if ((*p >= '0' && *p <= '9')
  294. || (*p >= 'a' && *p <= 'f')
  295. || (*p >= 'A' && *p <= 'F'))
  296. ++p;
  297. else
  298. return (0);
  299. }
  300. return (1);
  301. }
  302. static int
  303. find_newc_header(struct archive_read *a)
  304. {
  305. const void *h;
  306. const char *p, *q;
  307. size_t skip, skipped = 0;
  308. ssize_t bytes;
  309. for (;;) {
  310. h = __archive_read_ahead(a, sizeof(struct cpio_newc_header), &bytes);
  311. if (h == NULL)
  312. return (ARCHIVE_FATAL);
  313. p = h;
  314. q = p + bytes;
  315. /* Try the typical case first, then go into the slow search.*/
  316. if (memcmp("07070", p, 5) == 0
  317. && (p[5] == '1' || p[5] == '2')
  318. && is_hex(p, sizeof(struct cpio_newc_header)))
  319. return (ARCHIVE_OK);
  320. /*
  321. * Scan ahead until we find something that looks
  322. * like an odc header.
  323. */
  324. while (p + sizeof(struct cpio_newc_header) < q) {
  325. switch (p[5]) {
  326. case '1':
  327. case '2':
  328. if (memcmp("07070", p, 5) == 0
  329. && is_hex(p, sizeof(struct cpio_newc_header))) {
  330. skip = p - (const char *)h;
  331. __archive_read_consume(a, skip);
  332. skipped += skip;
  333. if (skipped > 0) {
  334. archive_set_error(&a->archive,
  335. 0,
  336. "Skipped %d bytes before "
  337. "finding valid header",
  338. (int)skipped);
  339. return (ARCHIVE_WARN);
  340. }
  341. return (ARCHIVE_OK);
  342. }
  343. p += 2;
  344. break;
  345. case '0':
  346. p++;
  347. break;
  348. default:
  349. p += 6;
  350. break;
  351. }
  352. }
  353. skip = p - (const char *)h;
  354. __archive_read_consume(a, skip);
  355. skipped += skip;
  356. }
  357. }
  358. static int
  359. header_newc(struct archive_read *a, struct cpio *cpio,
  360. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  361. {
  362. const void *h;
  363. const struct cpio_newc_header *header;
  364. int r;
  365. r = find_newc_header(a);
  366. if (r < ARCHIVE_WARN)
  367. return (r);
  368. /* Read fixed-size portion of header. */
  369. h = __archive_read_ahead(a, sizeof(struct cpio_newc_header), NULL);
  370. if (h == NULL)
  371. return (ARCHIVE_FATAL);
  372. __archive_read_consume(a, sizeof(struct cpio_newc_header));
  373. /* Parse out hex fields. */
  374. header = (const struct cpio_newc_header *)h;
  375. if (memcmp(header->c_magic, "070701", 6) == 0) {
  376. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_NOCRC;
  377. a->archive.archive_format_name = "ASCII cpio (SVR4 with no CRC)";
  378. } else if (memcmp(header->c_magic, "070702", 6) == 0) {
  379. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_SVR4_CRC;
  380. a->archive.archive_format_name = "ASCII cpio (SVR4 with CRC)";
  381. } else {
  382. /* TODO: Abort here? */
  383. }
  384. archive_entry_set_devmajor(entry, atol16(header->c_devmajor, sizeof(header->c_devmajor)));
  385. archive_entry_set_devminor(entry, atol16(header->c_devminor, sizeof(header->c_devminor)));
  386. archive_entry_set_ino(entry, atol16(header->c_ino, sizeof(header->c_ino)));
  387. archive_entry_set_mode(entry, atol16(header->c_mode, sizeof(header->c_mode)));
  388. archive_entry_set_uid(entry, atol16(header->c_uid, sizeof(header->c_uid)));
  389. archive_entry_set_gid(entry, atol16(header->c_gid, sizeof(header->c_gid)));
  390. archive_entry_set_nlink(entry, atol16(header->c_nlink, sizeof(header->c_nlink)));
  391. archive_entry_set_rdevmajor(entry, atol16(header->c_rdevmajor, sizeof(header->c_rdevmajor)));
  392. archive_entry_set_rdevminor(entry, atol16(header->c_rdevminor, sizeof(header->c_rdevminor)));
  393. archive_entry_set_mtime(entry, atol16(header->c_mtime, sizeof(header->c_mtime)), 0);
  394. *namelength = atol16(header->c_namesize, sizeof(header->c_namesize));
  395. /* Pad name to 2 more than a multiple of 4. */
  396. *name_pad = (2 - *namelength) & 3;
  397. /*
  398. * Note: entry_bytes_remaining is at least 64 bits and
  399. * therefore guaranteed to be big enough for a 33-bit file
  400. * size.
  401. */
  402. cpio->entry_bytes_remaining =
  403. atol16(header->c_filesize, sizeof(header->c_filesize));
  404. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  405. /* Pad file contents to a multiple of 4. */
  406. cpio->entry_padding = 3 & -cpio->entry_bytes_remaining;
  407. return (r);
  408. }
  409. /*
  410. * Skip forward to the next cpio odc header by searching for the
  411. * 070707 string. This is a hand-optimized search that could
  412. * probably be easily generalized to handle all character-based
  413. * cpio variants.
  414. */
  415. static int
  416. is_octal(const char *p, size_t len)
  417. {
  418. while (len-- > 0) {
  419. if (*p < '0' || *p > '7')
  420. return (0);
  421. ++p;
  422. }
  423. return (1);
  424. }
  425. static int
  426. find_odc_header(struct archive_read *a)
  427. {
  428. const void *h;
  429. const char *p, *q;
  430. size_t skip, skipped = 0;
  431. ssize_t bytes;
  432. for (;;) {
  433. h = __archive_read_ahead(a, sizeof(struct cpio_odc_header), &bytes);
  434. if (h == NULL)
  435. return (ARCHIVE_FATAL);
  436. p = h;
  437. q = p + bytes;
  438. /* Try the typical case first, then go into the slow search.*/
  439. if (memcmp("070707", p, 6) == 0
  440. && is_octal(p, sizeof(struct cpio_odc_header)))
  441. return (ARCHIVE_OK);
  442. /*
  443. * Scan ahead until we find something that looks
  444. * like an odc header.
  445. */
  446. while (p + sizeof(struct cpio_odc_header) < q) {
  447. switch (p[5]) {
  448. case '7':
  449. if (memcmp("070707", p, 6) == 0
  450. && is_octal(p, sizeof(struct cpio_odc_header))) {
  451. skip = p - (const char *)h;
  452. __archive_read_consume(a, skip);
  453. skipped += skip;
  454. if (skipped > 0) {
  455. archive_set_error(&a->archive,
  456. 0,
  457. "Skipped %d bytes before "
  458. "finding valid header",
  459. (int)skipped);
  460. return (ARCHIVE_WARN);
  461. }
  462. return (ARCHIVE_OK);
  463. }
  464. p += 2;
  465. break;
  466. case '0':
  467. p++;
  468. break;
  469. default:
  470. p += 6;
  471. break;
  472. }
  473. }
  474. skip = p - (const char *)h;
  475. __archive_read_consume(a, skip);
  476. skipped += skip;
  477. }
  478. }
  479. static int
  480. header_odc(struct archive_read *a, struct cpio *cpio,
  481. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  482. {
  483. const void *h;
  484. int r;
  485. const struct cpio_odc_header *header;
  486. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_POSIX;
  487. a->archive.archive_format_name = "POSIX octet-oriented cpio";
  488. /* Find the start of the next header. */
  489. r = find_odc_header(a);
  490. if (r < ARCHIVE_WARN)
  491. return (r);
  492. /* Read fixed-size portion of header. */
  493. h = __archive_read_ahead(a, sizeof(struct cpio_odc_header), NULL);
  494. if (h == NULL)
  495. return (ARCHIVE_FATAL);
  496. __archive_read_consume(a, sizeof(struct cpio_odc_header));
  497. /* Parse out octal fields. */
  498. header = (const struct cpio_odc_header *)h;
  499. archive_entry_set_dev(entry, atol8(header->c_dev, sizeof(header->c_dev)));
  500. archive_entry_set_ino(entry, atol8(header->c_ino, sizeof(header->c_ino)));
  501. archive_entry_set_mode(entry, atol8(header->c_mode, sizeof(header->c_mode)));
  502. archive_entry_set_uid(entry, atol8(header->c_uid, sizeof(header->c_uid)));
  503. archive_entry_set_gid(entry, atol8(header->c_gid, sizeof(header->c_gid)));
  504. archive_entry_set_nlink(entry, atol8(header->c_nlink, sizeof(header->c_nlink)));
  505. archive_entry_set_rdev(entry, atol8(header->c_rdev, sizeof(header->c_rdev)));
  506. archive_entry_set_mtime(entry, atol8(header->c_mtime, sizeof(header->c_mtime)), 0);
  507. *namelength = atol8(header->c_namesize, sizeof(header->c_namesize));
  508. *name_pad = 0; /* No padding of filename. */
  509. /*
  510. * Note: entry_bytes_remaining is at least 64 bits and
  511. * therefore guaranteed to be big enough for a 33-bit file
  512. * size.
  513. */
  514. cpio->entry_bytes_remaining =
  515. atol8(header->c_filesize, sizeof(header->c_filesize));
  516. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  517. cpio->entry_padding = 0;
  518. return (r);
  519. }
  520. static int
  521. header_bin_le(struct archive_read *a, struct cpio *cpio,
  522. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  523. {
  524. const void *h;
  525. const struct cpio_bin_header *header;
  526. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_LE;
  527. a->archive.archive_format_name = "cpio (little-endian binary)";
  528. /* Read fixed-size portion of header. */
  529. h = __archive_read_ahead(a, sizeof(struct cpio_bin_header), NULL);
  530. if (h == NULL)
  531. return (ARCHIVE_FATAL);
  532. __archive_read_consume(a, sizeof(struct cpio_bin_header));
  533. /* Parse out binary fields. */
  534. header = (const struct cpio_bin_header *)h;
  535. archive_entry_set_dev(entry, header->c_dev[0] + header->c_dev[1] * 256);
  536. archive_entry_set_ino(entry, header->c_ino[0] + header->c_ino[1] * 256);
  537. archive_entry_set_mode(entry, header->c_mode[0] + header->c_mode[1] * 256);
  538. archive_entry_set_uid(entry, header->c_uid[0] + header->c_uid[1] * 256);
  539. archive_entry_set_gid(entry, header->c_gid[0] + header->c_gid[1] * 256);
  540. archive_entry_set_nlink(entry, header->c_nlink[0] + header->c_nlink[1] * 256);
  541. archive_entry_set_rdev(entry, header->c_rdev[0] + header->c_rdev[1] * 256);
  542. archive_entry_set_mtime(entry, le4(header->c_mtime), 0);
  543. *namelength = header->c_namesize[0] + header->c_namesize[1] * 256;
  544. *name_pad = *namelength & 1; /* Pad to even. */
  545. cpio->entry_bytes_remaining = le4(header->c_filesize);
  546. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  547. cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
  548. return (ARCHIVE_OK);
  549. }
  550. static int
  551. header_bin_be(struct archive_read *a, struct cpio *cpio,
  552. struct archive_entry *entry, size_t *namelength, size_t *name_pad)
  553. {
  554. const void *h;
  555. const struct cpio_bin_header *header;
  556. a->archive.archive_format = ARCHIVE_FORMAT_CPIO_BIN_BE;
  557. a->archive.archive_format_name = "cpio (big-endian binary)";
  558. /* Read fixed-size portion of header. */
  559. h = __archive_read_ahead(a, sizeof(struct cpio_bin_header), NULL);
  560. if (h == NULL)
  561. return (ARCHIVE_FATAL);
  562. __archive_read_consume(a, sizeof(struct cpio_bin_header));
  563. /* Parse out binary fields. */
  564. header = (const struct cpio_bin_header *)h;
  565. archive_entry_set_dev(entry, header->c_dev[0] * 256 + header->c_dev[1]);
  566. archive_entry_set_ino(entry, header->c_ino[0] * 256 + header->c_ino[1]);
  567. archive_entry_set_mode(entry, header->c_mode[0] * 256 + header->c_mode[1]);
  568. archive_entry_set_uid(entry, header->c_uid[0] * 256 + header->c_uid[1]);
  569. archive_entry_set_gid(entry, header->c_gid[0] * 256 + header->c_gid[1]);
  570. archive_entry_set_nlink(entry, header->c_nlink[0] * 256 + header->c_nlink[1]);
  571. archive_entry_set_rdev(entry, header->c_rdev[0] * 256 + header->c_rdev[1]);
  572. archive_entry_set_mtime(entry, be4(header->c_mtime), 0);
  573. *namelength = header->c_namesize[0] * 256 + header->c_namesize[1];
  574. *name_pad = *namelength & 1; /* Pad to even. */
  575. cpio->entry_bytes_remaining = be4(header->c_filesize);
  576. archive_entry_set_size(entry, cpio->entry_bytes_remaining);
  577. cpio->entry_padding = cpio->entry_bytes_remaining & 1; /* Pad to even. */
  578. return (ARCHIVE_OK);
  579. }
  580. static int
  581. archive_read_format_cpio_cleanup(struct archive_read *a)
  582. {
  583. struct cpio *cpio;
  584. cpio = (struct cpio *)(a->format->data);
  585. /* Free inode->name map */
  586. while (cpio->links_head != NULL) {
  587. struct links_entry *lp = cpio->links_head->next;
  588. if (cpio->links_head->name)
  589. free(cpio->links_head->name);
  590. free(cpio->links_head);
  591. cpio->links_head = lp;
  592. }
  593. archive_string_free(&cpio->entry_name);
  594. free(cpio);
  595. (a->format->data) = NULL;
  596. return (ARCHIVE_OK);
  597. }
  598. static int
  599. le4(const unsigned char *p)
  600. {
  601. return ((p[0]<<16) + (p[1]<<24) + (p[2]<<0) + (p[3]<<8));
  602. }
  603. static int
  604. be4(const unsigned char *p)
  605. {
  606. return ((p[0]<<24) + (p[1]<<16) + (p[2]<<8) + (p[3]));
  607. }
  608. /*
  609. * Note that this implementation does not (and should not!) obey
  610. * locale settings; you cannot simply substitute strtol here, since
  611. * it does obey locale.
  612. */
  613. static int64_t
  614. atol8(const char *p, unsigned char_cnt)
  615. {
  616. int64_t l;
  617. int digit;
  618. l = 0;
  619. while (char_cnt-- > 0) {
  620. if (*p >= '0' && *p <= '7')
  621. digit = *p - '0';
  622. else
  623. return (l);
  624. p++;
  625. l <<= 3;
  626. l |= digit;
  627. }
  628. return (l);
  629. }
  630. static int64_t
  631. atol16(const char *p, unsigned char_cnt)
  632. {
  633. int64_t l;
  634. int digit;
  635. l = 0;
  636. while (char_cnt-- > 0) {
  637. if (*p >= 'a' && *p <= 'f')
  638. digit = *p - 'a' + 10;
  639. else if (*p >= 'A' && *p <= 'F')
  640. digit = *p - 'A' + 10;
  641. else if (*p >= '0' && *p <= '9')
  642. digit = *p - '0';
  643. else
  644. return (l);
  645. p++;
  646. l <<= 4;
  647. l |= digit;
  648. }
  649. return (l);
  650. }
  651. static void
  652. record_hardlink(struct cpio *cpio, struct archive_entry *entry)
  653. {
  654. struct links_entry *le;
  655. dev_t dev;
  656. int64_t ino;
  657. dev = archive_entry_dev(entry);
  658. ino = archive_entry_ino64(entry);
  659. /*
  660. * First look in the list of multiply-linked files. If we've
  661. * already dumped it, convert this entry to a hard link entry.
  662. */
  663. for (le = cpio->links_head; le; le = le->next) {
  664. if (le->dev == dev && le->ino == ino) {
  665. archive_entry_copy_hardlink(entry, le->name);
  666. if (--le->links <= 0) {
  667. if (le->previous != NULL)
  668. le->previous->next = le->next;
  669. if (le->next != NULL)
  670. le->next->previous = le->previous;
  671. if (cpio->links_head == le)
  672. cpio->links_head = le->next;
  673. free(le->name);
  674. free(le);
  675. }
  676. return;
  677. }
  678. }
  679. le = (struct links_entry *)malloc(sizeof(struct links_entry));
  680. if (le == NULL)
  681. __archive_errx(1, "Out of memory adding file to list");
  682. if (cpio->links_head != NULL)
  683. cpio->links_head->previous = le;
  684. le->next = cpio->links_head;
  685. le->previous = NULL;
  686. cpio->links_head = le;
  687. le->dev = dev;
  688. le->ino = ino;
  689. le->links = archive_entry_nlink(entry) - 1;
  690. le->name = strdup(archive_entry_pathname(entry));
  691. if (le->name == NULL)
  692. __archive_errx(1, "Out of memory adding file to list");
  693. }