+static void
+sparse_numblocks_coder (struct tar_stat_info const *st, char const *keyword,
+ struct xheader *xhdr,
+ void const *data __attribute__ ((unused)))
+{
+ code_num (st->sparse_map_avail, keyword, xhdr);
+}
+
+static void
+sparse_numblocks_decoder (struct tar_stat_info *st,
+ char const *keyword,
+ char const *arg,
+ size_t size __attribute__((unused)))
+{
+ uintmax_t u;
+ if (decode_num (&u, arg, SIZE_MAX, keyword))
+ {
+ st->sparse_map_size = u;
+ st->sparse_map = xcalloc (u, sizeof st->sparse_map[0]);
+ st->sparse_map_avail = 0;
+ }
+}
+
+static void
+sparse_offset_coder (struct tar_stat_info const *st, char const *keyword,
+ struct xheader *xhdr, void const *data)
+{
+ size_t const *pi = data;
+ code_num (st->sparse_map[*pi].offset, keyword, xhdr);
+}
+
+static void
+sparse_offset_decoder (struct tar_stat_info *st,
+ char const *keyword,
+ char const *arg,
+ size_t size __attribute__((unused)))
+{
+ uintmax_t u;
+ if (decode_num (&u, arg, TYPE_MAXIMUM (off_t), keyword))
+ {
+ if (st->sparse_map_avail < st->sparse_map_size)
+ st->sparse_map[st->sparse_map_avail].offset = u;
+ else
+ ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
+ "GNU.sparse.offset", arg));
+ }
+}
+
+static void
+sparse_numbytes_coder (struct tar_stat_info const *st, char const *keyword,
+ struct xheader *xhdr, void const *data)
+{
+ size_t const *pi = data;
+ code_num (st->sparse_map[*pi].numbytes, keyword, xhdr);
+}
+
+static void
+sparse_numbytes_decoder (struct tar_stat_info *st,
+ char const *keyword,
+ char const *arg,
+ size_t size __attribute__((unused)))
+{
+ uintmax_t u;
+ if (decode_num (&u, arg, TYPE_MAXIMUM (off_t), keyword))
+ {
+ if (st->sparse_map_avail < st->sparse_map_size)
+ st->sparse_map[st->sparse_map_avail++].numbytes = u;
+ else
+ ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
+ keyword, arg));
+ }
+}
+
+static void
+sparse_map_decoder (struct tar_stat_info *st,
+ char const *keyword,
+ char const *arg,
+ size_t size __attribute__((unused)))
+{
+ int offset = 1;
+
+ st->sparse_map_avail = 0;
+ while (1)
+ {
+ uintmax_t u;
+ char *delim;
+ struct sp_array e;
+
+ if (!ISDIGIT (*arg))
+ {
+ ERROR ((0, 0, _("Malformed extended header: invalid %s=%s"),
+ keyword, arg));
+ return;
+ }
+
+ errno = 0;
+ u = strtoumax (arg, &delim, 10);
+ if (offset)
+ {
+ e.offset = u;
+ if (!(u == e.offset && errno != ERANGE))
+ {
+ out_of_range_header (keyword, arg, 0, TYPE_MAXIMUM (off_t));
+ return;
+ }
+ }
+ else
+ {
+ e.numbytes = u;
+ if (!(u == e.numbytes && errno != ERANGE))
+ {
+ out_of_range_header (keyword, arg, 0, TYPE_MAXIMUM (off_t));
+ return;
+ }
+ if (st->sparse_map_avail < st->sparse_map_size)
+ st->sparse_map[st->sparse_map_avail++] = e;
+ else
+ {
+ ERROR ((0, 0, _("Malformed extended header: excess %s=%s"),
+ keyword, arg));
+ return;
+ }
+ }
+
+ offset = !offset;
+
+ if (*delim == 0)
+ break;
+ else if (*delim != ',')
+ {
+ ERROR ((0, 0,
+ _("Malformed extended header: invalid %s: unexpected delimiter %c"),
+ keyword, *delim));
+ return;
+ }
+
+ arg = delim + 1;
+ }
+
+ if (!offset)
+ ERROR ((0, 0,
+ _("Malformed extended header: invalid %s: odd number of values"),
+ keyword));
+}
+
+static void
+dumpdir_coder (struct tar_stat_info const *st, char const *keyword,
+ struct xheader *xhdr, void const *data)
+{
+ xheader_print_n (xhdr, keyword, data, dumpdir_size (data));
+}
+
+static void
+dumpdir_decoder (struct tar_stat_info *st,
+ char const *keyword __attribute__((unused)),
+ char const *arg,
+ size_t size)
+{
+ st->dumpdir = xmalloc (size);
+ memcpy (st->dumpdir, arg, size);
+}
+
+static void
+volume_label_coder (struct tar_stat_info const *st, char const *keyword,
+ struct xheader *xhdr, void const *data)
+{
+ code_string (data, keyword, xhdr);
+}
+
+static void
+volume_label_decoder (struct tar_stat_info *st,
+ char const *keyword __attribute__((unused)),
+ char const *arg,
+ size_t size __attribute__((unused)))
+{
+ decode_string (&volume_label, arg);
+}
+
+static void
+volume_size_coder (struct tar_stat_info const *st, char const *keyword,
+ struct xheader *xhdr, void const *data)
+{
+ off_t const *v = data;
+ code_num (*v, keyword, xhdr);
+}
+
+static void
+volume_size_decoder (struct tar_stat_info *st,
+ char const *keyword,
+ char const *arg, size_t size)
+{
+ uintmax_t u;
+ if (decode_num (&u, arg, TYPE_MAXIMUM (uintmax_t), keyword))
+ continued_file_size = u;
+}
+
+/* FIXME: Merge with volume_size_coder */
+static void
+volume_offset_coder (struct tar_stat_info const *st, char const *keyword,
+ struct xheader *xhdr, void const *data)
+{
+ off_t const *v = data;
+ code_num (*v, keyword, xhdr);
+}
+
+static void
+volume_offset_decoder (struct tar_stat_info *st,
+ char const *keyword,
+ char const *arg, size_t size)
+{
+ uintmax_t u;
+ if (decode_num (&u, arg, TYPE_MAXIMUM (uintmax_t), keyword))
+ continued_file_offset = u;
+}
+
+static void
+volume_filename_decoder (struct tar_stat_info *st,
+ char const *keyword __attribute__((unused)),
+ char const *arg,
+ size_t size __attribute__((unused)))
+{
+ decode_string (&continued_file_name, arg);
+}
+
+static void
+sparse_major_coder (struct tar_stat_info const *st, char const *keyword,
+ struct xheader *xhdr, void const *data)
+{
+ code_num (st->sparse_major, keyword, xhdr);
+}
+
+static void
+sparse_major_decoder (struct tar_stat_info *st,
+ char const *keyword,
+ char const *arg,
+ size_t size)
+{
+ uintmax_t u;
+ if (decode_num (&u, arg, TYPE_MAXIMUM (unsigned), keyword))
+ st->sparse_major = u;
+}
+
+static void
+sparse_minor_coder (struct tar_stat_info const *st, char const *keyword,
+ struct xheader *xhdr, void const *data)
+{
+ code_num (st->sparse_minor, keyword, xhdr);
+}
+
+static void
+sparse_minor_decoder (struct tar_stat_info *st,
+ char const *keyword,
+ char const *arg,
+ size_t size)
+{
+ uintmax_t u;
+ if (decode_num (&u, arg, TYPE_MAXIMUM (unsigned), keyword))
+ st->sparse_minor = u;
+}
+
+struct xhdr_tab const xhdr_tab[] = {
+ { "atime", atime_coder, atime_decoder, 0 },
+ { "comment", dummy_coder, dummy_decoder, 0 },
+ { "charset", dummy_coder, dummy_decoder, 0 },
+ { "ctime", ctime_coder, ctime_decoder, 0 },
+ { "gid", gid_coder, gid_decoder, 0 },
+ { "gname", gname_coder, gname_decoder, 0 },
+ { "linkpath", linkpath_coder, linkpath_decoder, 0 },
+ { "mtime", mtime_coder, mtime_decoder, 0 },
+ { "path", path_coder, path_decoder, 0 },
+ { "size", size_coder, size_decoder, 0 },
+ { "uid", uid_coder, uid_decoder, 0 },
+ { "uname", uname_coder, uname_decoder, 0 },
+
+ /* Sparse file handling */
+ { "GNU.sparse.name", path_coder, path_decoder,
+ XHDR_PROTECTED },
+ { "GNU.sparse.major", sparse_major_coder, sparse_major_decoder,
+ XHDR_PROTECTED },
+ { "GNU.sparse.minor", sparse_minor_coder, sparse_minor_decoder,
+ XHDR_PROTECTED },
+ { "GNU.sparse.realsize", sparse_size_coder, sparse_size_decoder,
+ XHDR_PROTECTED },
+ { "GNU.sparse.numblocks", sparse_numblocks_coder, sparse_numblocks_decoder,
+ XHDR_PROTECTED },
+
+ /* tar 1.14 - 1.15.90 keywords. */
+ { "GNU.sparse.size", sparse_size_coder, sparse_size_decoder,
+ XHDR_PROTECTED },
+ /* tar 1.14 - 1.15.1 keywords. Multiple instances of these appeared in 'x'
+ headers, and each of them was meaningful. It confilcted with POSIX specs,
+ which requires that "when extended header records conflict, the last one
+ given in the header shall take precedence." */
+ { "GNU.sparse.offset", sparse_offset_coder, sparse_offset_decoder,
+ XHDR_PROTECTED },
+ { "GNU.sparse.numbytes", sparse_numbytes_coder, sparse_numbytes_decoder,
+ XHDR_PROTECTED },
+ /* tar 1.15.90 keyword, introduced to remove the above-mentioned conflict. */
+ { "GNU.sparse.map", NULL /* Unused, see pax_dump_header() */,
+ sparse_map_decoder, 0 },
+
+ { "GNU.dumpdir", dumpdir_coder, dumpdir_decoder,
+ XHDR_PROTECTED },
+
+ /* Keeps the tape/volume label. May be present only in the global headers.