archive_write_set_format_ar.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*-
  2. * Copyright (c) 2007 Kai Wang
  3. * Copyright (c) 2007 Tim Kientzle
  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. * in this position and unchanged.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "archive_platform.h"
  28. __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_format_ar.c 201108 2009-12-28 03:28:21Z kientzle $");
  29. #ifdef HAVE_ERRNO_H
  30. #include <errno.h>
  31. #endif
  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_private.h"
  41. #include "archive_write_private.h"
  42. struct ar_w {
  43. uint64_t entry_bytes_remaining;
  44. uint64_t entry_padding;
  45. int is_strtab;
  46. int has_strtab;
  47. char wrote_global_header;
  48. char *strtab;
  49. };
  50. /*
  51. * Define structure of the "ar" header.
  52. */
  53. #define AR_name_offset 0
  54. #define AR_name_size 16
  55. #define AR_date_offset 16
  56. #define AR_date_size 12
  57. #define AR_uid_offset 28
  58. #define AR_uid_size 6
  59. #define AR_gid_offset 34
  60. #define AR_gid_size 6
  61. #define AR_mode_offset 40
  62. #define AR_mode_size 8
  63. #define AR_size_offset 48
  64. #define AR_size_size 10
  65. #define AR_fmag_offset 58
  66. #define AR_fmag_size 2
  67. static int archive_write_set_format_ar(struct archive_write *);
  68. static int archive_write_ar_header(struct archive_write *,
  69. struct archive_entry *);
  70. static ssize_t archive_write_ar_data(struct archive_write *,
  71. const void *buff, size_t s);
  72. static int archive_write_ar_free(struct archive_write *);
  73. static int archive_write_ar_close(struct archive_write *);
  74. static int archive_write_ar_finish_entry(struct archive_write *);
  75. static const char *ar_basename(const char *path);
  76. static int format_octal(int64_t v, char *p, int s);
  77. static int format_decimal(int64_t v, char *p, int s);
  78. int
  79. archive_write_set_format_ar_bsd(struct archive *_a)
  80. {
  81. struct archive_write *a = (struct archive_write *)_a;
  82. int r;
  83. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  84. ARCHIVE_STATE_NEW, "archive_write_set_format_ar_bsd");
  85. r = archive_write_set_format_ar(a);
  86. if (r == ARCHIVE_OK) {
  87. a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
  88. a->archive.archive_format_name = "ar (BSD)";
  89. }
  90. return (r);
  91. }
  92. int
  93. archive_write_set_format_ar_svr4(struct archive *_a)
  94. {
  95. struct archive_write *a = (struct archive_write *)_a;
  96. int r;
  97. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  98. ARCHIVE_STATE_NEW, "archive_write_set_format_ar_svr4");
  99. r = archive_write_set_format_ar(a);
  100. if (r == ARCHIVE_OK) {
  101. a->archive.archive_format = ARCHIVE_FORMAT_AR_GNU;
  102. a->archive.archive_format_name = "ar (GNU/SVR4)";
  103. }
  104. return (r);
  105. }
  106. /*
  107. * Generic initialization.
  108. */
  109. static int
  110. archive_write_set_format_ar(struct archive_write *a)
  111. {
  112. struct ar_w *ar;
  113. /* If someone else was already registered, unregister them. */
  114. if (a->format_free != NULL)
  115. (a->format_free)(a);
  116. ar = (struct ar_w *)malloc(sizeof(*ar));
  117. if (ar == NULL) {
  118. archive_set_error(&a->archive, ENOMEM, "Can't allocate ar data");
  119. return (ARCHIVE_FATAL);
  120. }
  121. memset(ar, 0, sizeof(*ar));
  122. a->format_data = ar;
  123. a->format_name = "ar";
  124. a->format_write_header = archive_write_ar_header;
  125. a->format_write_data = archive_write_ar_data;
  126. a->format_close = archive_write_ar_close;
  127. a->format_free = archive_write_ar_free;
  128. a->format_finish_entry = archive_write_ar_finish_entry;
  129. return (ARCHIVE_OK);
  130. }
  131. static int
  132. archive_write_ar_header(struct archive_write *a, struct archive_entry *entry)
  133. {
  134. int ret, append_fn;
  135. char buff[60];
  136. char *ss, *se;
  137. struct ar_w *ar;
  138. const char *pathname;
  139. const char *filename;
  140. int64_t size;
  141. append_fn = 0;
  142. ar = (struct ar_w *)a->format_data;
  143. ar->is_strtab = 0;
  144. filename = NULL;
  145. size = archive_entry_size(entry);
  146. /*
  147. * Reject files with empty name.
  148. */
  149. pathname = archive_entry_pathname(entry);
  150. if (*pathname == '\0') {
  151. archive_set_error(&a->archive, EINVAL,
  152. "Invalid filename");
  153. return (ARCHIVE_WARN);
  154. }
  155. /*
  156. * If we are now at the beginning of the archive,
  157. * we need first write the ar global header.
  158. */
  159. if (!ar->wrote_global_header) {
  160. __archive_write_output(a, "!<arch>\n", 8);
  161. ar->wrote_global_header = 1;
  162. }
  163. memset(buff, ' ', 60);
  164. strncpy(&buff[AR_fmag_offset], "`\n", 2);
  165. if (strcmp(pathname, "/") == 0 ) {
  166. /* Entry is archive symbol table in GNU format */
  167. buff[AR_name_offset] = '/';
  168. goto stat;
  169. }
  170. if (strcmp(pathname, "__.SYMDEF") == 0) {
  171. /* Entry is archive symbol table in BSD format */
  172. strncpy(buff + AR_name_offset, "__.SYMDEF", 9);
  173. goto stat;
  174. }
  175. if (strcmp(pathname, "//") == 0) {
  176. /*
  177. * Entry is archive filename table, inform that we should
  178. * collect strtab in next _data call.
  179. */
  180. ar->is_strtab = 1;
  181. buff[AR_name_offset] = buff[AR_name_offset + 1] = '/';
  182. /*
  183. * For archive string table, only ar_size field should
  184. * be set.
  185. */
  186. goto size;
  187. }
  188. /*
  189. * Otherwise, entry is a normal archive member.
  190. * Strip leading paths from filenames, if any.
  191. */
  192. if ((filename = ar_basename(pathname)) == NULL) {
  193. /* Reject filenames with trailing "/" */
  194. archive_set_error(&a->archive, EINVAL,
  195. "Invalid filename");
  196. return (ARCHIVE_WARN);
  197. }
  198. if (a->archive.archive_format == ARCHIVE_FORMAT_AR_GNU) {
  199. /*
  200. * SVR4/GNU variant use a "/" to mark then end of the filename,
  201. * make it possible to have embedded spaces in the filename.
  202. * So, the longest filename here (without extension) is
  203. * actually 15 bytes.
  204. */
  205. if (strlen(filename) <= 15) {
  206. strncpy(&buff[AR_name_offset],
  207. filename, strlen(filename));
  208. buff[AR_name_offset + strlen(filename)] = '/';
  209. } else {
  210. /*
  211. * For filename longer than 15 bytes, GNU variant
  212. * makes use of a string table and instead stores the
  213. * offset of the real filename to in the ar_name field.
  214. * The string table should have been written before.
  215. */
  216. if (ar->has_strtab <= 0) {
  217. archive_set_error(&a->archive, EINVAL,
  218. "Can't find string table");
  219. return (ARCHIVE_WARN);
  220. }
  221. se = (char *)malloc(strlen(filename) + 3);
  222. if (se == NULL) {
  223. archive_set_error(&a->archive, ENOMEM,
  224. "Can't allocate filename buffer");
  225. return (ARCHIVE_FATAL);
  226. }
  227. strncpy(se, filename, strlen(filename));
  228. strcpy(se + strlen(filename), "/\n");
  229. ss = strstr(ar->strtab, se);
  230. free(se);
  231. if (ss == NULL) {
  232. archive_set_error(&a->archive, EINVAL,
  233. "Invalid string table");
  234. return (ARCHIVE_WARN);
  235. }
  236. /*
  237. * GNU variant puts "/" followed by digits into
  238. * ar_name field. These digits indicates the real
  239. * filename string's offset to the string table.
  240. */
  241. buff[AR_name_offset] = '/';
  242. if (format_decimal(ss - ar->strtab,
  243. buff + AR_name_offset + 1,
  244. AR_name_size - 1)) {
  245. archive_set_error(&a->archive, ERANGE,
  246. "string table offset too large");
  247. return (ARCHIVE_WARN);
  248. }
  249. }
  250. } else if (a->archive.archive_format == ARCHIVE_FORMAT_AR_BSD) {
  251. /*
  252. * BSD variant: for any file name which is more than
  253. * 16 chars or contains one or more embedded space(s), the
  254. * string "#1/" followed by the ASCII length of the name is
  255. * put into the ar_name field. The file size (stored in the
  256. * ar_size field) is incremented by the length of the name.
  257. * The name is then written immediately following the
  258. * archive header.
  259. */
  260. if (strlen(filename) <= 16 && strchr(filename, ' ') == NULL) {
  261. strncpy(&buff[AR_name_offset], filename, strlen(filename));
  262. buff[AR_name_offset + strlen(filename)] = ' ';
  263. }
  264. else {
  265. strncpy(buff + AR_name_offset, "#1/", 3);
  266. if (format_decimal(strlen(filename),
  267. buff + AR_name_offset + 3,
  268. AR_name_size - 3)) {
  269. archive_set_error(&a->archive, ERANGE,
  270. "File name too long");
  271. return (ARCHIVE_WARN);
  272. }
  273. append_fn = 1;
  274. size += strlen(filename);
  275. }
  276. }
  277. stat:
  278. if (format_decimal(archive_entry_mtime(entry), buff + AR_date_offset, AR_date_size)) {
  279. archive_set_error(&a->archive, ERANGE,
  280. "File modification time too large");
  281. return (ARCHIVE_WARN);
  282. }
  283. if (format_decimal(archive_entry_uid(entry), buff + AR_uid_offset, AR_uid_size)) {
  284. archive_set_error(&a->archive, ERANGE,
  285. "Numeric user ID too large");
  286. return (ARCHIVE_WARN);
  287. }
  288. if (format_decimal(archive_entry_gid(entry), buff + AR_gid_offset, AR_gid_size)) {
  289. archive_set_error(&a->archive, ERANGE,
  290. "Numeric group ID too large");
  291. return (ARCHIVE_WARN);
  292. }
  293. if (format_octal(archive_entry_mode(entry), buff + AR_mode_offset, AR_mode_size)) {
  294. archive_set_error(&a->archive, ERANGE,
  295. "Numeric mode too large");
  296. return (ARCHIVE_WARN);
  297. }
  298. /*
  299. * Sanity Check: A non-pseudo archive member should always be
  300. * a regular file.
  301. */
  302. if (filename != NULL && archive_entry_filetype(entry) != AE_IFREG) {
  303. archive_set_error(&a->archive, EINVAL,
  304. "Regular file required for non-pseudo member");
  305. return (ARCHIVE_WARN);
  306. }
  307. size:
  308. if (format_decimal(size, buff + AR_size_offset, AR_size_size)) {
  309. archive_set_error(&a->archive, ERANGE,
  310. "File size out of range");
  311. return (ARCHIVE_WARN);
  312. }
  313. ret = __archive_write_output(a, buff, 60);
  314. if (ret != ARCHIVE_OK)
  315. return (ret);
  316. ar->entry_bytes_remaining = size;
  317. ar->entry_padding = ar->entry_bytes_remaining % 2;
  318. if (append_fn > 0) {
  319. ret = __archive_write_output(a, filename, strlen(filename));
  320. if (ret != ARCHIVE_OK)
  321. return (ret);
  322. ar->entry_bytes_remaining -= strlen(filename);
  323. }
  324. return (ARCHIVE_OK);
  325. }
  326. static ssize_t
  327. archive_write_ar_data(struct archive_write *a, const void *buff, size_t s)
  328. {
  329. struct ar_w *ar;
  330. int ret;
  331. ar = (struct ar_w *)a->format_data;
  332. if (s > ar->entry_bytes_remaining)
  333. s = ar->entry_bytes_remaining;
  334. if (ar->is_strtab > 0) {
  335. if (ar->has_strtab > 0) {
  336. archive_set_error(&a->archive, EINVAL,
  337. "More than one string tables exist");
  338. return (ARCHIVE_WARN);
  339. }
  340. ar->strtab = (char *)malloc(s);
  341. if (ar->strtab == NULL) {
  342. archive_set_error(&a->archive, ENOMEM,
  343. "Can't allocate strtab buffer");
  344. return (ARCHIVE_FATAL);
  345. }
  346. strncpy(ar->strtab, buff, s);
  347. ar->has_strtab = 1;
  348. }
  349. ret = __archive_write_output(a, buff, s);
  350. if (ret != ARCHIVE_OK)
  351. return (ret);
  352. ar->entry_bytes_remaining -= s;
  353. return (s);
  354. }
  355. static int
  356. archive_write_ar_free(struct archive_write *a)
  357. {
  358. struct ar_w *ar;
  359. ar = (struct ar_w *)a->format_data;
  360. if (ar == NULL)
  361. return (ARCHIVE_OK);
  362. if (ar->has_strtab > 0) {
  363. free(ar->strtab);
  364. ar->strtab = NULL;
  365. }
  366. free(ar);
  367. a->format_data = NULL;
  368. return (ARCHIVE_OK);
  369. }
  370. static int
  371. archive_write_ar_close(struct archive_write *a)
  372. {
  373. struct ar_w *ar;
  374. int ret;
  375. /*
  376. * If we haven't written anything yet, we need to write
  377. * the ar global header now to make it a valid ar archive.
  378. */
  379. ar = (struct ar_w *)a->format_data;
  380. if (!ar->wrote_global_header) {
  381. ar->wrote_global_header = 1;
  382. ret = __archive_write_output(a, "!<arch>\n", 8);
  383. return (ret);
  384. }
  385. return (ARCHIVE_OK);
  386. }
  387. static int
  388. archive_write_ar_finish_entry(struct archive_write *a)
  389. {
  390. struct ar_w *ar;
  391. int ret;
  392. ar = (struct ar_w *)a->format_data;
  393. if (ar->entry_bytes_remaining != 0) {
  394. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  395. "Entry remaining bytes larger than 0");
  396. return (ARCHIVE_WARN);
  397. }
  398. if (ar->entry_padding == 0) {
  399. return (ARCHIVE_OK);
  400. }
  401. if (ar->entry_padding != 1) {
  402. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  403. "Padding wrong size: %ju should be 1 or 0",
  404. (uintmax_t)ar->entry_padding);
  405. return (ARCHIVE_WARN);
  406. }
  407. ret = __archive_write_output(a, "\n", 1);
  408. return (ret);
  409. }
  410. /*
  411. * Format a number into the specified field using base-8.
  412. * NB: This version is slightly different from the one in
  413. * _ustar.c
  414. */
  415. static int
  416. format_octal(int64_t v, char *p, int s)
  417. {
  418. int len;
  419. char *h;
  420. len = s;
  421. h = p;
  422. /* Octal values can't be negative, so use 0. */
  423. if (v < 0) {
  424. while (len-- > 0)
  425. *p++ = '0';
  426. return (-1);
  427. }
  428. p += s; /* Start at the end and work backwards. */
  429. do {
  430. *--p = (char)('0' + (v & 7));
  431. v >>= 3;
  432. } while (--s > 0 && v > 0);
  433. if (v == 0) {
  434. memmove(h, p, len - s);
  435. p = h + len - s;
  436. while (s-- > 0)
  437. *p++ = ' ';
  438. return (0);
  439. }
  440. /* If it overflowed, fill field with max value. */
  441. while (len-- > 0)
  442. *p++ = '7';
  443. return (-1);
  444. }
  445. /*
  446. * Format a number into the specified field using base-10.
  447. */
  448. static int
  449. format_decimal(int64_t v, char *p, int s)
  450. {
  451. int len;
  452. char *h;
  453. len = s;
  454. h = p;
  455. /* Negative values in ar header are meaningless, so use 0. */
  456. if (v < 0) {
  457. while (len-- > 0)
  458. *p++ = '0';
  459. return (-1);
  460. }
  461. p += s;
  462. do {
  463. *--p = (char)('0' + (v % 10));
  464. v /= 10;
  465. } while (--s > 0 && v > 0);
  466. if (v == 0) {
  467. memmove(h, p, len - s);
  468. p = h + len - s;
  469. while (s-- > 0)
  470. *p++ = ' ';
  471. return (0);
  472. }
  473. /* If it overflowed, fill field with max value. */
  474. while (len-- > 0)
  475. *p++ = '9';
  476. return (-1);
  477. }
  478. static const char *
  479. ar_basename(const char *path)
  480. {
  481. const char *endp, *startp;
  482. endp = path + strlen(path) - 1;
  483. /*
  484. * For filename with trailing slash(es), we return
  485. * NULL indicating an error.
  486. */
  487. if (*endp == '/')
  488. return (NULL);
  489. /* Find the start of the base */
  490. startp = endp;
  491. while (startp > path && *(startp - 1) != '/')
  492. startp--;
  493. return (startp);
  494. }