archive_write_set_format_v7tar.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*-
  2. * Copyright (c) 2003-2007 Tim Kientzle
  3. * Copyright (c) 2011-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$");
  28. #ifdef HAVE_ERRNO_H
  29. #include <errno.h>
  30. #endif
  31. #include <stdio.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_write_private.h"
  43. struct v7tar {
  44. uint64_t entry_bytes_remaining;
  45. uint64_t entry_padding;
  46. struct archive_string_conv *opt_sconv;
  47. struct archive_string_conv *sconv_default;
  48. int init_default_conversion;
  49. };
  50. /*
  51. * Define structure of POSIX 'v7tar' tar header.
  52. */
  53. #define V7TAR_name_offset 0
  54. #define V7TAR_name_size 100
  55. #define V7TAR_mode_offset 100
  56. #define V7TAR_mode_size 6
  57. #define V7TAR_mode_max_size 8
  58. #define V7TAR_uid_offset 108
  59. #define V7TAR_uid_size 6
  60. #define V7TAR_uid_max_size 8
  61. #define V7TAR_gid_offset 116
  62. #define V7TAR_gid_size 6
  63. #define V7TAR_gid_max_size 8
  64. #define V7TAR_size_offset 124
  65. #define V7TAR_size_size 11
  66. #define V7TAR_size_max_size 12
  67. #define V7TAR_mtime_offset 136
  68. #define V7TAR_mtime_size 11
  69. #define V7TAR_mtime_max_size 12
  70. #define V7TAR_checksum_offset 148
  71. #define V7TAR_checksum_size 8
  72. #define V7TAR_typeflag_offset 156
  73. #define V7TAR_typeflag_size 1
  74. #define V7TAR_linkname_offset 157
  75. #define V7TAR_linkname_size 100
  76. #define V7TAR_padding_offset 257
  77. #define V7TAR_padding_size 255
  78. /*
  79. * A filled-in copy of the header for initialization.
  80. */
  81. static const char template_header[] = {
  82. /* name: 100 bytes */
  83. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  84. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  85. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  86. 0,0,0,0,
  87. /* Mode, space-null termination: 8 bytes */
  88. '0','0','0','0','0','0', ' ','\0',
  89. /* uid, space-null termination: 8 bytes */
  90. '0','0','0','0','0','0', ' ','\0',
  91. /* gid, space-null termination: 8 bytes */
  92. '0','0','0','0','0','0', ' ','\0',
  93. /* size, space termation: 12 bytes */
  94. '0','0','0','0','0','0','0','0','0','0','0', ' ',
  95. /* mtime, space termation: 12 bytes */
  96. '0','0','0','0','0','0','0','0','0','0','0', ' ',
  97. /* Initial checksum value: 8 spaces */
  98. ' ',' ',' ',' ',' ',' ',' ',' ',
  99. /* Typeflag: 1 byte */
  100. 0,
  101. /* Linkname: 100 bytes */
  102. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  103. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  104. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  105. 0,0,0,0,
  106. /* Padding: 255 bytes */
  107. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  108. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  109. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  110. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  111. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  112. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  113. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
  114. 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0
  115. };
  116. static ssize_t archive_write_v7tar_data(struct archive_write *a, const void *buff,
  117. size_t s);
  118. static int archive_write_v7tar_free(struct archive_write *);
  119. static int archive_write_v7tar_close(struct archive_write *);
  120. static int archive_write_v7tar_finish_entry(struct archive_write *);
  121. static int archive_write_v7tar_header(struct archive_write *,
  122. struct archive_entry *entry);
  123. static int archive_write_v7tar_options(struct archive_write *,
  124. const char *, const char *);
  125. static int format_256(int64_t, char *, int);
  126. static int format_number(int64_t, char *, int size, int max, int strict);
  127. static int format_octal(int64_t, char *, int);
  128. static int format_header_v7tar(struct archive_write *, char h[512],
  129. struct archive_entry *, int, struct archive_string_conv *);
  130. /*
  131. * Set output format to 'v7tar' format.
  132. */
  133. int
  134. archive_write_set_format_v7tar(struct archive *_a)
  135. {
  136. struct archive_write *a = (struct archive_write *)_a;
  137. struct v7tar *v7tar;
  138. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  139. ARCHIVE_STATE_NEW, "archive_write_set_format_v7tar");
  140. /* If someone else was already registered, unregister them. */
  141. if (a->format_free != NULL)
  142. (a->format_free)(a);
  143. /* Basic internal sanity test. */
  144. if (sizeof(template_header) != 512) {
  145. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  146. "Internal: template_header wrong size: %zu should be 512",
  147. sizeof(template_header));
  148. return (ARCHIVE_FATAL);
  149. }
  150. v7tar = (struct v7tar *)malloc(sizeof(*v7tar));
  151. if (v7tar == NULL) {
  152. archive_set_error(&a->archive, ENOMEM,
  153. "Can't allocate v7tar data");
  154. return (ARCHIVE_FATAL);
  155. }
  156. memset(v7tar, 0, sizeof(*v7tar));
  157. a->format_data = v7tar;
  158. a->format_name = "tar (non-POSIX)";
  159. a->format_options = archive_write_v7tar_options;
  160. a->format_write_header = archive_write_v7tar_header;
  161. a->format_write_data = archive_write_v7tar_data;
  162. a->format_close = archive_write_v7tar_close;
  163. a->format_free = archive_write_v7tar_free;
  164. a->format_finish_entry = archive_write_v7tar_finish_entry;
  165. a->archive.archive_format = ARCHIVE_FORMAT_TAR;
  166. a->archive.archive_format_name = "tar (non-POSIX)";
  167. return (ARCHIVE_OK);
  168. }
  169. static int
  170. archive_write_v7tar_options(struct archive_write *a, const char *key,
  171. const char *val)
  172. {
  173. struct v7tar *v7tar = (struct v7tar *)a->format_data;
  174. int ret = ARCHIVE_FAILED;
  175. if (strcmp(key, "hdrcharset") == 0) {
  176. if (val == NULL || val[0] == 0)
  177. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  178. "%s: hdrcharset option needs a character-set name",
  179. a->format_name);
  180. else {
  181. v7tar->opt_sconv = archive_string_conversion_to_charset(
  182. &a->archive, val, 0);
  183. if (v7tar->opt_sconv != NULL)
  184. ret = ARCHIVE_OK;
  185. else
  186. ret = ARCHIVE_FATAL;
  187. }
  188. return (ret);
  189. }
  190. /* Note: The "warn" return is just to inform the options
  191. * supervisor that we didn't handle it. It will generate
  192. * a suitable error if no one used this option. */
  193. return (ARCHIVE_WARN);
  194. }
  195. static int
  196. archive_write_v7tar_header(struct archive_write *a, struct archive_entry *entry)
  197. {
  198. char buff[512];
  199. int ret, ret2;
  200. struct v7tar *v7tar;
  201. struct archive_entry *entry_main;
  202. struct archive_string_conv *sconv;
  203. v7tar = (struct v7tar *)a->format_data;
  204. /* Setup default string conversion. */
  205. if (v7tar->opt_sconv == NULL) {
  206. if (!v7tar->init_default_conversion) {
  207. v7tar->sconv_default =
  208. archive_string_default_conversion_for_write(
  209. &(a->archive));
  210. v7tar->init_default_conversion = 1;
  211. }
  212. sconv = v7tar->sconv_default;
  213. } else
  214. sconv = v7tar->opt_sconv;
  215. /* Sanity check. */
  216. if (archive_entry_pathname(entry) == NULL) {
  217. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  218. "Can't record entry in tar file without pathname");
  219. return (ARCHIVE_FAILED);
  220. }
  221. /* Only regular files (not hardlinks) have data. */
  222. if (archive_entry_hardlink(entry) != NULL ||
  223. archive_entry_symlink(entry) != NULL ||
  224. !(archive_entry_filetype(entry) == AE_IFREG))
  225. archive_entry_set_size(entry, 0);
  226. if (AE_IFDIR == archive_entry_filetype(entry)) {
  227. const char *p;
  228. size_t path_length;
  229. /*
  230. * Ensure a trailing '/'. Modify the entry so
  231. * the client sees the change.
  232. */
  233. #if defined(_WIN32) && !defined(__CYGWIN__)
  234. const wchar_t *wp;
  235. wp = archive_entry_pathname_w(entry);
  236. if (wp != NULL && wp[wcslen(wp) -1] != L'/') {
  237. struct archive_wstring ws;
  238. archive_string_init(&ws);
  239. path_length = wcslen(wp);
  240. if (archive_wstring_ensure(&ws,
  241. path_length + 2) == NULL) {
  242. archive_set_error(&a->archive, ENOMEM,
  243. "Can't allocate v7tar data");
  244. archive_wstring_free(&ws);
  245. return(ARCHIVE_FATAL);
  246. }
  247. /* Should we keep '\' ? */
  248. if (wp[path_length -1] == L'\\')
  249. path_length--;
  250. archive_wstrncpy(&ws, wp, path_length);
  251. archive_wstrappend_wchar(&ws, L'/');
  252. archive_entry_copy_pathname_w(entry, ws.s);
  253. archive_wstring_free(&ws);
  254. p = NULL;
  255. } else
  256. #endif
  257. p = archive_entry_pathname(entry);
  258. /*
  259. * On Windows, this is a backup operation just in
  260. * case getting WCS failed. On POSIX, this is a
  261. * normal operation.
  262. */
  263. if (p != NULL && p[strlen(p) - 1] != '/') {
  264. struct archive_string as;
  265. archive_string_init(&as);
  266. path_length = strlen(p);
  267. if (archive_string_ensure(&as,
  268. path_length + 2) == NULL) {
  269. archive_set_error(&a->archive, ENOMEM,
  270. "Can't allocate v7tar data");
  271. archive_string_free(&as);
  272. return(ARCHIVE_FATAL);
  273. }
  274. #if defined(_WIN32) && !defined(__CYGWIN__)
  275. /* NOTE: This might break the pathname
  276. * if the current code page is CP932 and
  277. * the pathname includes a character '\'
  278. * as a part of its multibyte pathname. */
  279. if (p[strlen(p) -1] == '\\')
  280. path_length--;
  281. else
  282. #endif
  283. archive_strncpy(&as, p, path_length);
  284. archive_strappend_char(&as, '/');
  285. archive_entry_copy_pathname(entry, as.s);
  286. archive_string_free(&as);
  287. }
  288. }
  289. #if defined(_WIN32) && !defined(__CYGWIN__)
  290. /* Make sure the path separators in pahtname, hardlink and symlink
  291. * are all slash '/', not the Windows path separator '\'. */
  292. entry_main = __la_win_entry_in_posix_pathseparator(entry);
  293. if (entry_main == NULL) {
  294. archive_set_error(&a->archive, ENOMEM,
  295. "Can't allocate v7tar data");
  296. return(ARCHIVE_FATAL);
  297. }
  298. if (entry != entry_main)
  299. entry = entry_main;
  300. else
  301. entry_main = NULL;
  302. #else
  303. entry_main = NULL;
  304. #endif
  305. ret = format_header_v7tar(a, buff, entry, 1, sconv);
  306. if (ret < ARCHIVE_WARN) {
  307. if (entry_main)
  308. archive_entry_free(entry_main);
  309. return (ret);
  310. }
  311. ret2 = __archive_write_output(a, buff, 512);
  312. if (ret2 < ARCHIVE_WARN) {
  313. if (entry_main)
  314. archive_entry_free(entry_main);
  315. return (ret2);
  316. }
  317. if (ret2 < ret)
  318. ret = ret2;
  319. v7tar->entry_bytes_remaining = archive_entry_size(entry);
  320. v7tar->entry_padding = 0x1ff & (-(int64_t)v7tar->entry_bytes_remaining);
  321. if (entry_main)
  322. archive_entry_free(entry_main);
  323. return (ret);
  324. }
  325. /*
  326. * Format a basic 512-byte "v7tar" header.
  327. *
  328. * Returns -1 if format failed (due to field overflow).
  329. * Note that this always formats as much of the header as possible.
  330. * If "strict" is set to zero, it will extend numeric fields as
  331. * necessary (overwriting terminators or using base-256 extensions).
  332. *
  333. */
  334. static int
  335. format_header_v7tar(struct archive_write *a, char h[512],
  336. struct archive_entry *entry, int strict,
  337. struct archive_string_conv *sconv)
  338. {
  339. unsigned int checksum;
  340. int i, r, ret;
  341. size_t copy_length;
  342. const char *p, *pp;
  343. int mytartype;
  344. ret = 0;
  345. mytartype = -1;
  346. /*
  347. * The "template header" already includes the "v7tar"
  348. * signature, various end-of-field markers and other required
  349. * elements.
  350. */
  351. memcpy(h, &template_header, 512);
  352. /*
  353. * Because the block is already null-filled, and strings
  354. * are allowed to exactly fill their destination (without null),
  355. * I use memcpy(dest, src, strlen()) here a lot to copy strings.
  356. */
  357. r = archive_entry_pathname_l(entry, &pp, &copy_length, sconv);
  358. if (r != 0) {
  359. if (errno == ENOMEM) {
  360. archive_set_error(&a->archive, ENOMEM,
  361. "Can't allocate memory for Pathname");
  362. return (ARCHIVE_FATAL);
  363. }
  364. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  365. "Can't translate pathname '%s' to %s",
  366. pp, archive_string_conversion_charset_name(sconv));
  367. ret = ARCHIVE_WARN;
  368. }
  369. if (strict && copy_length < V7TAR_name_size)
  370. memcpy(h + V7TAR_name_offset, pp, copy_length);
  371. else if (!strict && copy_length <= V7TAR_name_size)
  372. memcpy(h + V7TAR_name_offset, pp, copy_length);
  373. else {
  374. /* Prefix is too long. */
  375. archive_set_error(&a->archive, ENAMETOOLONG,
  376. "Pathname too long");
  377. ret = ARCHIVE_FAILED;
  378. }
  379. r = archive_entry_hardlink_l(entry, &p, &copy_length, sconv);
  380. if (r != 0) {
  381. if (errno == ENOMEM) {
  382. archive_set_error(&a->archive, ENOMEM,
  383. "Can't allocate memory for Linkname");
  384. return (ARCHIVE_FATAL);
  385. }
  386. archive_set_error(&a->archive,
  387. ARCHIVE_ERRNO_FILE_FORMAT,
  388. "Can't translate linkname '%s' to %s",
  389. p, archive_string_conversion_charset_name(sconv));
  390. ret = ARCHIVE_WARN;
  391. }
  392. if (copy_length > 0)
  393. mytartype = '1';
  394. else {
  395. r = archive_entry_symlink_l(entry, &p, &copy_length, sconv);
  396. if (r != 0) {
  397. if (errno == ENOMEM) {
  398. archive_set_error(&a->archive, ENOMEM,
  399. "Can't allocate memory for Linkname");
  400. return (ARCHIVE_FATAL);
  401. }
  402. archive_set_error(&a->archive,
  403. ARCHIVE_ERRNO_FILE_FORMAT,
  404. "Can't translate linkname '%s' to %s",
  405. p, archive_string_conversion_charset_name(sconv));
  406. ret = ARCHIVE_WARN;
  407. }
  408. }
  409. if (copy_length > 0) {
  410. if (copy_length >= V7TAR_linkname_size) {
  411. archive_set_error(&a->archive, ENAMETOOLONG,
  412. "Link contents too long");
  413. ret = ARCHIVE_FAILED;
  414. copy_length = V7TAR_linkname_size;
  415. }
  416. memcpy(h + V7TAR_linkname_offset, p, copy_length);
  417. }
  418. if (format_number(archive_entry_mode(entry) & 07777,
  419. h + V7TAR_mode_offset, V7TAR_mode_size,
  420. V7TAR_mode_max_size, strict)) {
  421. archive_set_error(&a->archive, ERANGE,
  422. "Numeric mode too large");
  423. ret = ARCHIVE_FAILED;
  424. }
  425. if (format_number(archive_entry_uid(entry),
  426. h + V7TAR_uid_offset, V7TAR_uid_size, V7TAR_uid_max_size, strict)) {
  427. archive_set_error(&a->archive, ERANGE,
  428. "Numeric user ID too large");
  429. ret = ARCHIVE_FAILED;
  430. }
  431. if (format_number(archive_entry_gid(entry),
  432. h + V7TAR_gid_offset, V7TAR_gid_size, V7TAR_gid_max_size, strict)) {
  433. archive_set_error(&a->archive, ERANGE,
  434. "Numeric group ID too large");
  435. ret = ARCHIVE_FAILED;
  436. }
  437. if (format_number(archive_entry_size(entry),
  438. h + V7TAR_size_offset, V7TAR_size_size,
  439. V7TAR_size_max_size, strict)) {
  440. archive_set_error(&a->archive, ERANGE,
  441. "File size out of range");
  442. ret = ARCHIVE_FAILED;
  443. }
  444. if (format_number(archive_entry_mtime(entry),
  445. h + V7TAR_mtime_offset, V7TAR_mtime_size,
  446. V7TAR_mtime_max_size, strict)) {
  447. archive_set_error(&a->archive, ERANGE,
  448. "File modification time too large");
  449. ret = ARCHIVE_FAILED;
  450. }
  451. if (mytartype >= 0) {
  452. h[V7TAR_typeflag_offset] = mytartype;
  453. } else {
  454. switch (archive_entry_filetype(entry)) {
  455. case AE_IFREG: case AE_IFDIR:
  456. break;
  457. case AE_IFLNK:
  458. h[V7TAR_typeflag_offset] = '2';
  459. break;
  460. case AE_IFCHR:
  461. archive_set_error(&a->archive,
  462. ARCHIVE_ERRNO_FILE_FORMAT,
  463. "tar format cannot archive character device");
  464. return (ARCHIVE_FAILED);
  465. case AE_IFBLK:
  466. archive_set_error(&a->archive,
  467. ARCHIVE_ERRNO_FILE_FORMAT,
  468. "tar format cannot archive block device");
  469. return (ARCHIVE_FAILED);
  470. case AE_IFIFO:
  471. archive_set_error(&a->archive,
  472. ARCHIVE_ERRNO_FILE_FORMAT,
  473. "tar format cannot archive fifo");
  474. return (ARCHIVE_FAILED);
  475. case AE_IFSOCK:
  476. archive_set_error(&a->archive,
  477. ARCHIVE_ERRNO_FILE_FORMAT,
  478. "tar format cannot archive socket");
  479. return (ARCHIVE_FAILED);
  480. default:
  481. archive_set_error(&a->archive,
  482. ARCHIVE_ERRNO_FILE_FORMAT,
  483. "tar format cannot archive this (mode=0%lo)",
  484. (unsigned long)archive_entry_mode(entry));
  485. ret = ARCHIVE_FAILED;
  486. }
  487. }
  488. checksum = 0;
  489. for (i = 0; i < 512; i++)
  490. checksum += 255 & (unsigned int)h[i];
  491. format_octal(checksum, h + V7TAR_checksum_offset, 6);
  492. /* Can't be pre-set in the template. */
  493. h[V7TAR_checksum_offset + 6] = '\0';
  494. return (ret);
  495. }
  496. /*
  497. * Format a number into a field, with some intelligence.
  498. */
  499. static int
  500. format_number(int64_t v, char *p, int s, int maxsize, int strict)
  501. {
  502. int64_t limit;
  503. limit = ((int64_t)1 << (s*3));
  504. /* "Strict" only permits octal values with proper termination. */
  505. if (strict)
  506. return (format_octal(v, p, s));
  507. /*
  508. * In non-strict mode, we allow the number to overwrite one or
  509. * more bytes of the field termination. Even old tar
  510. * implementations should be able to handle this with no
  511. * problem.
  512. */
  513. if (v >= 0) {
  514. while (s <= maxsize) {
  515. if (v < limit)
  516. return (format_octal(v, p, s));
  517. s++;
  518. limit <<= 3;
  519. }
  520. }
  521. /* Base-256 can handle any number, positive or negative. */
  522. return (format_256(v, p, maxsize));
  523. }
  524. /*
  525. * Format a number into the specified field using base-256.
  526. */
  527. static int
  528. format_256(int64_t v, char *p, int s)
  529. {
  530. p += s;
  531. while (s-- > 0) {
  532. *--p = (char)(v & 0xff);
  533. v >>= 8;
  534. }
  535. *p |= 0x80; /* Set the base-256 marker bit. */
  536. return (0);
  537. }
  538. /*
  539. * Format a number into the specified field.
  540. */
  541. static int
  542. format_octal(int64_t v, char *p, int s)
  543. {
  544. int len;
  545. len = s;
  546. /* Octal values can't be negative, so use 0. */
  547. if (v < 0) {
  548. while (len-- > 0)
  549. *p++ = '0';
  550. return (-1);
  551. }
  552. p += s; /* Start at the end and work backwards. */
  553. while (s-- > 0) {
  554. *--p = (char)('0' + (v & 7));
  555. v >>= 3;
  556. }
  557. if (v == 0)
  558. return (0);
  559. /* If it overflowed, fill field with max value. */
  560. while (len-- > 0)
  561. *p++ = '7';
  562. return (-1);
  563. }
  564. static int
  565. archive_write_v7tar_close(struct archive_write *a)
  566. {
  567. return (__archive_write_nulls(a, 512*2));
  568. }
  569. static int
  570. archive_write_v7tar_free(struct archive_write *a)
  571. {
  572. struct v7tar *v7tar;
  573. v7tar = (struct v7tar *)a->format_data;
  574. free(v7tar);
  575. a->format_data = NULL;
  576. return (ARCHIVE_OK);
  577. }
  578. static int
  579. archive_write_v7tar_finish_entry(struct archive_write *a)
  580. {
  581. struct v7tar *v7tar;
  582. int ret;
  583. v7tar = (struct v7tar *)a->format_data;
  584. ret = __archive_write_nulls(a,
  585. (size_t)(v7tar->entry_bytes_remaining + v7tar->entry_padding));
  586. v7tar->entry_bytes_remaining = v7tar->entry_padding = 0;
  587. return (ret);
  588. }
  589. static ssize_t
  590. archive_write_v7tar_data(struct archive_write *a, const void *buff, size_t s)
  591. {
  592. struct v7tar *v7tar;
  593. int ret;
  594. v7tar = (struct v7tar *)a->format_data;
  595. if (s > v7tar->entry_bytes_remaining)
  596. s = (size_t)v7tar->entry_bytes_remaining;
  597. ret = __archive_write_output(a, buff, s);
  598. v7tar->entry_bytes_remaining -= s;
  599. if (ret != ARCHIVE_OK)
  600. return (ret);
  601. return (s);
  602. }