archive_write_set_format_v7tar.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. #include "archive_write_set_format_private.h"
  44. struct v7tar {
  45. uint64_t entry_bytes_remaining;
  46. uint64_t entry_padding;
  47. struct archive_string_conv *opt_sconv;
  48. struct archive_string_conv *sconv_default;
  49. int init_default_conversion;
  50. };
  51. /*
  52. * Define structure of POSIX 'v7tar' tar header.
  53. */
  54. #define V7TAR_name_offset 0
  55. #define V7TAR_name_size 100
  56. #define V7TAR_mode_offset 100
  57. #define V7TAR_mode_size 6
  58. #define V7TAR_mode_max_size 8
  59. #define V7TAR_uid_offset 108
  60. #define V7TAR_uid_size 6
  61. #define V7TAR_uid_max_size 8
  62. #define V7TAR_gid_offset 116
  63. #define V7TAR_gid_size 6
  64. #define V7TAR_gid_max_size 8
  65. #define V7TAR_size_offset 124
  66. #define V7TAR_size_size 11
  67. #define V7TAR_size_max_size 12
  68. #define V7TAR_mtime_offset 136
  69. #define V7TAR_mtime_size 11
  70. #define V7TAR_mtime_max_size 12
  71. #define V7TAR_checksum_offset 148
  72. #define V7TAR_checksum_size 8
  73. #define V7TAR_typeflag_offset 156
  74. #define V7TAR_typeflag_size 1
  75. #define V7TAR_linkname_offset 157
  76. #define V7TAR_linkname_size 100
  77. #define V7TAR_padding_offset 257
  78. #define V7TAR_padding_size 255
  79. /*
  80. * A filled-in copy of the header for initialization.
  81. */
  82. static const char template_header[] = {
  83. /* name: 100 bytes */
  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,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,
  87. 0,0,0,0,
  88. /* Mode, space-null termination: 8 bytes */
  89. '0','0','0','0','0','0', ' ','\0',
  90. /* uid, space-null termination: 8 bytes */
  91. '0','0','0','0','0','0', ' ','\0',
  92. /* gid, space-null termination: 8 bytes */
  93. '0','0','0','0','0','0', ' ','\0',
  94. /* size, space termination: 12 bytes */
  95. '0','0','0','0','0','0','0','0','0','0','0', ' ',
  96. /* mtime, space termination: 12 bytes */
  97. '0','0','0','0','0','0','0','0','0','0','0', ' ',
  98. /* Initial checksum value: 8 spaces */
  99. ' ',' ',' ',' ',' ',' ',' ',' ',
  100. /* Typeflag: 1 byte */
  101. 0,
  102. /* Linkname: 100 bytes */
  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,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,
  106. 0,0,0,0,
  107. /* Padding: 255 bytes */
  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,0,
  115. 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
  116. };
  117. static ssize_t archive_write_v7tar_data(struct archive_write *a, const void *buff,
  118. size_t s);
  119. static int archive_write_v7tar_free(struct archive_write *);
  120. static int archive_write_v7tar_close(struct archive_write *);
  121. static int archive_write_v7tar_finish_entry(struct archive_write *);
  122. static int archive_write_v7tar_header(struct archive_write *,
  123. struct archive_entry *entry);
  124. static int archive_write_v7tar_options(struct archive_write *,
  125. const char *, const char *);
  126. static int format_256(int64_t, char *, int);
  127. static int format_number(int64_t, char *, int size, int max, int strict);
  128. static int format_octal(int64_t, char *, int);
  129. static int format_header_v7tar(struct archive_write *, char h[512],
  130. struct archive_entry *, int, struct archive_string_conv *);
  131. /*
  132. * Set output format to 'v7tar' format.
  133. */
  134. int
  135. archive_write_set_format_v7tar(struct archive *_a)
  136. {
  137. struct archive_write *a = (struct archive_write *)_a;
  138. struct v7tar *v7tar;
  139. archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
  140. ARCHIVE_STATE_NEW, "archive_write_set_format_v7tar");
  141. /* If someone else was already registered, unregister them. */
  142. if (a->format_free != NULL)
  143. (a->format_free)(a);
  144. /* Basic internal sanity test. */
  145. if (sizeof(template_header) != 512) {
  146. archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
  147. "Internal: template_header wrong size: %zu should be 512",
  148. sizeof(template_header));
  149. return (ARCHIVE_FATAL);
  150. }
  151. v7tar = (struct v7tar *)calloc(1, sizeof(*v7tar));
  152. if (v7tar == NULL) {
  153. archive_set_error(&a->archive, ENOMEM,
  154. "Can't allocate v7tar data");
  155. return (ARCHIVE_FATAL);
  156. }
  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[0] != '\0' && 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 pathname, 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. archive_entry_free(entry_main);
  308. return (ret);
  309. }
  310. ret2 = __archive_write_output(a, buff, 512);
  311. if (ret2 < ARCHIVE_WARN) {
  312. archive_entry_free(entry_main);
  313. return (ret2);
  314. }
  315. if (ret2 < ret)
  316. ret = ret2;
  317. v7tar->entry_bytes_remaining = archive_entry_size(entry);
  318. v7tar->entry_padding = 0x1ff & (-(int64_t)v7tar->entry_bytes_remaining);
  319. archive_entry_free(entry_main);
  320. return (ret);
  321. }
  322. /*
  323. * Format a basic 512-byte "v7tar" header.
  324. *
  325. * Returns -1 if format failed (due to field overflow).
  326. * Note that this always formats as much of the header as possible.
  327. * If "strict" is set to zero, it will extend numeric fields as
  328. * necessary (overwriting terminators or using base-256 extensions).
  329. *
  330. */
  331. static int
  332. format_header_v7tar(struct archive_write *a, char h[512],
  333. struct archive_entry *entry, int strict,
  334. struct archive_string_conv *sconv)
  335. {
  336. unsigned int checksum;
  337. int i, r, ret;
  338. size_t copy_length;
  339. const char *p, *pp;
  340. int mytartype;
  341. ret = 0;
  342. mytartype = -1;
  343. /*
  344. * The "template header" already includes the "v7tar"
  345. * signature, various end-of-field markers and other required
  346. * elements.
  347. */
  348. memcpy(h, &template_header, 512);
  349. /*
  350. * Because the block is already null-filled, and strings
  351. * are allowed to exactly fill their destination (without null),
  352. * I use memcpy(dest, src, strlen()) here a lot to copy strings.
  353. */
  354. r = archive_entry_pathname_l(entry, &pp, &copy_length, sconv);
  355. if (r != 0) {
  356. if (errno == ENOMEM) {
  357. archive_set_error(&a->archive, ENOMEM,
  358. "Can't allocate memory for Pathname");
  359. return (ARCHIVE_FATAL);
  360. }
  361. archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
  362. "Can't translate pathname '%s' to %s",
  363. pp, archive_string_conversion_charset_name(sconv));
  364. ret = ARCHIVE_WARN;
  365. }
  366. if (strict && copy_length < V7TAR_name_size)
  367. memcpy(h + V7TAR_name_offset, pp, copy_length);
  368. else if (!strict && copy_length <= V7TAR_name_size)
  369. memcpy(h + V7TAR_name_offset, pp, copy_length);
  370. else {
  371. /* Prefix is too long. */
  372. archive_set_error(&a->archive, ENAMETOOLONG,
  373. "Pathname too long");
  374. ret = ARCHIVE_FAILED;
  375. }
  376. r = archive_entry_hardlink_l(entry, &p, &copy_length, sconv);
  377. if (r != 0) {
  378. if (errno == ENOMEM) {
  379. archive_set_error(&a->archive, ENOMEM,
  380. "Can't allocate memory for Linkname");
  381. return (ARCHIVE_FATAL);
  382. }
  383. archive_set_error(&a->archive,
  384. ARCHIVE_ERRNO_FILE_FORMAT,
  385. "Can't translate linkname '%s' to %s",
  386. p, archive_string_conversion_charset_name(sconv));
  387. ret = ARCHIVE_WARN;
  388. }
  389. if (copy_length > 0)
  390. mytartype = '1';
  391. else {
  392. r = archive_entry_symlink_l(entry, &p, &copy_length, sconv);
  393. if (r != 0) {
  394. if (errno == ENOMEM) {
  395. archive_set_error(&a->archive, ENOMEM,
  396. "Can't allocate memory for Linkname");
  397. return (ARCHIVE_FATAL);
  398. }
  399. archive_set_error(&a->archive,
  400. ARCHIVE_ERRNO_FILE_FORMAT,
  401. "Can't translate linkname '%s' to %s",
  402. p, archive_string_conversion_charset_name(sconv));
  403. ret = ARCHIVE_WARN;
  404. }
  405. }
  406. if (copy_length > 0) {
  407. if (copy_length >= V7TAR_linkname_size) {
  408. archive_set_error(&a->archive, ENAMETOOLONG,
  409. "Link contents too long");
  410. ret = ARCHIVE_FAILED;
  411. copy_length = V7TAR_linkname_size;
  412. }
  413. memcpy(h + V7TAR_linkname_offset, p, copy_length);
  414. }
  415. if (format_number(archive_entry_mode(entry) & 07777,
  416. h + V7TAR_mode_offset, V7TAR_mode_size,
  417. V7TAR_mode_max_size, strict)) {
  418. archive_set_error(&a->archive, ERANGE,
  419. "Numeric mode too large");
  420. ret = ARCHIVE_FAILED;
  421. }
  422. if (format_number(archive_entry_uid(entry),
  423. h + V7TAR_uid_offset, V7TAR_uid_size, V7TAR_uid_max_size, strict)) {
  424. archive_set_error(&a->archive, ERANGE,
  425. "Numeric user ID too large");
  426. ret = ARCHIVE_FAILED;
  427. }
  428. if (format_number(archive_entry_gid(entry),
  429. h + V7TAR_gid_offset, V7TAR_gid_size, V7TAR_gid_max_size, strict)) {
  430. archive_set_error(&a->archive, ERANGE,
  431. "Numeric group ID too large");
  432. ret = ARCHIVE_FAILED;
  433. }
  434. if (format_number(archive_entry_size(entry),
  435. h + V7TAR_size_offset, V7TAR_size_size,
  436. V7TAR_size_max_size, strict)) {
  437. archive_set_error(&a->archive, ERANGE,
  438. "File size out of range");
  439. ret = ARCHIVE_FAILED;
  440. }
  441. if (format_number(archive_entry_mtime(entry),
  442. h + V7TAR_mtime_offset, V7TAR_mtime_size,
  443. V7TAR_mtime_max_size, strict)) {
  444. archive_set_error(&a->archive, ERANGE,
  445. "File modification time too large");
  446. ret = ARCHIVE_FAILED;
  447. }
  448. if (mytartype >= 0) {
  449. h[V7TAR_typeflag_offset] = mytartype;
  450. } else {
  451. switch (archive_entry_filetype(entry)) {
  452. case AE_IFREG: case AE_IFDIR:
  453. break;
  454. case AE_IFLNK:
  455. h[V7TAR_typeflag_offset] = '2';
  456. break;
  457. default:
  458. /* AE_IFBLK, AE_IFCHR, AE_IFIFO, AE_IFSOCK
  459. * and unknown */
  460. __archive_write_entry_filetype_unsupported(
  461. &a->archive, entry, "v7tar");
  462. ret = ARCHIVE_FAILED;
  463. }
  464. }
  465. checksum = 0;
  466. for (i = 0; i < 512; i++)
  467. checksum += 255 & (unsigned int)h[i];
  468. format_octal(checksum, h + V7TAR_checksum_offset, 6);
  469. /* Can't be pre-set in the template. */
  470. h[V7TAR_checksum_offset + 6] = '\0';
  471. return (ret);
  472. }
  473. /*
  474. * Format a number into a field, with some intelligence.
  475. */
  476. static int
  477. format_number(int64_t v, char *p, int s, int maxsize, int strict)
  478. {
  479. int64_t limit;
  480. limit = ((int64_t)1 << (s*3));
  481. /* "Strict" only permits octal values with proper termination. */
  482. if (strict)
  483. return (format_octal(v, p, s));
  484. /*
  485. * In non-strict mode, we allow the number to overwrite one or
  486. * more bytes of the field termination. Even old tar
  487. * implementations should be able to handle this with no
  488. * problem.
  489. */
  490. if (v >= 0) {
  491. while (s <= maxsize) {
  492. if (v < limit)
  493. return (format_octal(v, p, s));
  494. s++;
  495. limit <<= 3;
  496. }
  497. }
  498. /* Base-256 can handle any number, positive or negative. */
  499. return (format_256(v, p, maxsize));
  500. }
  501. /*
  502. * Format a number into the specified field using base-256.
  503. */
  504. static int
  505. format_256(int64_t v, char *p, int s)
  506. {
  507. p += s;
  508. while (s-- > 0) {
  509. *--p = (char)(v & 0xff);
  510. v >>= 8;
  511. }
  512. *p |= 0x80; /* Set the base-256 marker bit. */
  513. return (0);
  514. }
  515. /*
  516. * Format a number into the specified field.
  517. */
  518. static int
  519. format_octal(int64_t v, char *p, int s)
  520. {
  521. int len;
  522. len = s;
  523. /* Octal values can't be negative, so use 0. */
  524. if (v < 0) {
  525. while (len-- > 0)
  526. *p++ = '0';
  527. return (-1);
  528. }
  529. p += s; /* Start at the end and work backwards. */
  530. while (s-- > 0) {
  531. *--p = (char)('0' + (v & 7));
  532. v >>= 3;
  533. }
  534. if (v == 0)
  535. return (0);
  536. /* If it overflowed, fill field with max value. */
  537. while (len-- > 0)
  538. *p++ = '7';
  539. return (-1);
  540. }
  541. static int
  542. archive_write_v7tar_close(struct archive_write *a)
  543. {
  544. return (__archive_write_nulls(a, 512*2));
  545. }
  546. static int
  547. archive_write_v7tar_free(struct archive_write *a)
  548. {
  549. struct v7tar *v7tar;
  550. v7tar = (struct v7tar *)a->format_data;
  551. free(v7tar);
  552. a->format_data = NULL;
  553. return (ARCHIVE_OK);
  554. }
  555. static int
  556. archive_write_v7tar_finish_entry(struct archive_write *a)
  557. {
  558. struct v7tar *v7tar;
  559. int ret;
  560. v7tar = (struct v7tar *)a->format_data;
  561. ret = __archive_write_nulls(a,
  562. (size_t)(v7tar->entry_bytes_remaining + v7tar->entry_padding));
  563. v7tar->entry_bytes_remaining = v7tar->entry_padding = 0;
  564. return (ret);
  565. }
  566. static ssize_t
  567. archive_write_v7tar_data(struct archive_write *a, const void *buff, size_t s)
  568. {
  569. struct v7tar *v7tar;
  570. int ret;
  571. v7tar = (struct v7tar *)a->format_data;
  572. if (s > v7tar->entry_bytes_remaining)
  573. s = (size_t)v7tar->entry_bytes_remaining;
  574. ret = __archive_write_output(a, buff, s);
  575. v7tar->entry_bytes_remaining -= s;
  576. if (ret != ARCHIVE_OK)
  577. return (ret);
  578. return (s);
  579. }