+static struct xhdr_tab const *
+locate_handler (char const *keyword)
+{
+ struct xhdr_tab const *p;
+
+ for (p = xhdr_tab; p->keyword; p++)
+ if (p->prefix)
+ {
+ if (strncmp (p->keyword, keyword, strlen(p->keyword)) == 0)
+ return p;
+ }
+ else
+ {
+ if (strcmp (p->keyword, keyword) == 0)
+ return p;
+ }
+
+ return NULL;
+}
+
+static bool
+xheader_protected_pattern_p (const char *pattern)
+{
+ struct xhdr_tab const *p;
+
+ for (p = xhdr_tab; p->keyword; p++)
+ if (!p->prefix && (p->flags & XHDR_PROTECTED)
+ && fnmatch (pattern, p->keyword, 0) == 0)
+ return true;
+ return false;
+}
+
+static bool
+xheader_protected_keyword_p (const char *keyword)
+{
+ struct xhdr_tab const *p;
+
+ for (p = xhdr_tab; p->keyword; p++)
+ if (!p->prefix && (p->flags & XHDR_PROTECTED)
+ && strcmp (p->keyword, keyword) == 0)
+ return true;
+ return false;
+}
+
+/* Decode a single extended header record, advancing *PTR to the next record.
+ Return true on success, false otherwise. */
+static bool
+decode_record (struct xheader *xhdr,
+ char **ptr,
+ void (*handler) (void *, char const *, char const *, size_t),
+ void *data)
+{
+ char *start = *ptr;
+ char *p = start;
+ size_t len;
+ char *len_lim;
+ char const *keyword;
+ char *nextp;
+ size_t len_max = xhdr->buffer + xhdr->size - start;
+
+ while (*p == ' ' || *p == '\t')
+ p++;
+
+ if (! ISDIGIT (*p))
+ {
+ if (*p)
+ ERROR ((0, 0, _("Malformed extended header: missing length")));
+ return false;
+ }
+
+ len = strtoumax (p, &len_lim, 10);
+
+ if (len_max < len)
+ {
+ int len_len = len_lim - p;
+ ERROR ((0, 0, _("Extended header length %*s is out of range"),
+ len_len, p));
+ return false;
+ }
+
+ nextp = start + len;
+
+ for (p = len_lim; *p == ' ' || *p == '\t'; p++)
+ continue;
+ if (p == len_lim)
+ {
+ ERROR ((0, 0,
+ _("Malformed extended header: missing blank after length")));
+ return false;
+ }
+
+ keyword = p;
+ p = strchr (p, '=');
+ if (! (p && p < nextp))
+ {
+ ERROR ((0, 0, _("Malformed extended header: missing equal sign")));
+ return false;
+ }
+
+ if (nextp[-1] != '\n')
+ {
+ ERROR ((0, 0, _("Malformed extended header: missing newline")));
+ return false;
+ }
+
+ *p = nextp[-1] = '\0';
+ handler (data, keyword, p + 1, nextp - p - 2); /* '=' + trailing '\n' */
+ *p = '=';
+ nextp[-1] = '\n';
+ *ptr = nextp;
+ return true;
+}
+
+static void
+run_override_list (struct keyword_list *kp, struct tar_stat_info *st)
+{
+ for (; kp; kp = kp->next)
+ {
+ struct xhdr_tab const *t = locate_handler (kp->pattern);
+ if (t)
+ t->decoder (st, t->keyword, kp->value, strlen (kp->value));
+ }
+}
+
+static void
+decx (void *data, char const *keyword, char const *value, size_t size)
+{
+ struct xhdr_tab const *t;
+ struct tar_stat_info *st = data;
+
+ if (xheader_keyword_deleted_p (keyword)
+ || xheader_keyword_override_p (keyword))
+ return;
+
+ t = locate_handler (keyword);
+ if (t)
+ t->decoder (st, keyword, value, size);
+ else
+ WARNOPT (WARN_UNKNOWN_KEYWORD,
+ (0, 0, _("Ignoring unknown extended header keyword '%s'"),
+ keyword));
+}
+
+void
+xheader_decode (struct tar_stat_info *st)
+{
+ run_override_list (keyword_global_override_list, st);
+ run_override_list (global_header_override_list, st);
+
+ if (st->xhdr.size)
+ {
+ char *p = st->xhdr.buffer + BLOCKSIZE;
+ while (decode_record (&st->xhdr, &p, decx, st))
+ continue;
+ }
+ run_override_list (keyword_override_list, st);
+
+ /* The archived (effective) file size is always set directly in tar header
+ field, possibly overridden by "size" extended header - in both cases,
+ result is now decoded in st->stat.st_size */
+ st->archive_file_size = st->stat.st_size;
+
+ /* The real file size (given by stat()) may be redefined for sparse
+ files in "GNU.sparse.realsize" extended header */
+ if (st->real_size_set)
+ st->stat.st_size = st->real_size;
+}
+
+static void
+decg (void *data, char const *keyword, char const *value,
+ size_t size __attribute__((unused)))
+{
+ struct keyword_list **kwl = data;
+ struct xhdr_tab const *tab = locate_handler (keyword);
+ if (tab && (tab->flags & XHDR_GLOBAL))
+ tab->decoder (data, keyword, value, size);
+ else
+ xheader_list_append (kwl, keyword, value);
+}
+
+void
+xheader_decode_global (struct xheader *xhdr)
+{
+ if (xhdr->size)
+ {
+ char *p = xhdr->buffer + BLOCKSIZE;
+
+ xheader_list_destroy (&global_header_override_list);
+ while (decode_record (xhdr, &p, decg, &global_header_override_list))
+ continue;
+ }
+}
+
+static void
+xheader_init (struct xheader *xhdr)
+{
+ if (!xhdr->stk)
+ {
+ xhdr->stk = xmalloc (sizeof *xhdr->stk);
+ obstack_init (xhdr->stk);
+ }
+}
+
+void
+xheader_store (char const *keyword, struct tar_stat_info *st,
+ void const *data)
+{
+ struct xhdr_tab const *t;
+
+ if (st->xhdr.buffer)
+ return;
+ t = locate_handler (keyword);
+ if (!t || !t->coder)
+ return;
+ if (xheader_keyword_deleted_p (keyword)
+ || xheader_keyword_override_p (keyword))
+ return;
+ xheader_init (&st->xhdr);
+ t->coder (st, keyword, &st->xhdr, data);
+}
+
+void
+xheader_read (struct xheader *xhdr, union block *p, off_t size)
+{
+ size_t j = 0;
+
+ if (size < 0)
+ size = 0; /* Already diagnosed. */
+
+ if (SIZE_MAX - BLOCKSIZE <= size)
+ xalloc_die ();
+
+ size += BLOCKSIZE;
+ xhdr->size = size;
+ xhdr->buffer = xmalloc (size + 1);
+ xhdr->buffer[size] = '\0';
+
+ do
+ {
+ size_t len = size;
+
+ if (len > BLOCKSIZE)
+ len = BLOCKSIZE;
+
+ if (!p)
+ FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
+
+ memcpy (&xhdr->buffer[j], p->buffer, len);
+ set_next_block_after (p);
+
+ p = find_next_block ();
+
+ j += len;
+ size -= len;
+ }
+ while (size > 0);
+}
+
+/* xattr_encode_keyword() substitutes '=' ~~> '%3D' and '%' ~~> '%25'
+ in extended attribute keywords. This is needed because the '=' character
+ has special purpose in extended attribute header - it splits keyword and
+ value part of header. If there was the '=' occurrence allowed inside
+ keyword, there would be no unambiguous way how to decode this extended
+ attribute.
+
+ (http://lists.gnu.org/archive/html/bug-tar/2012-10/msg00017.html)
+ */
+static char *
+xattr_encode_keyword(const char *keyword)
+{
+ static char *encode_buffer = NULL;
+ static size_t encode_buffer_size = 0;
+ size_t bp; /* keyword/buffer pointers */
+
+ if (!encode_buffer)
+ {
+ encode_buffer_size = 256;
+ encode_buffer = xmalloc (encode_buffer_size);
+ }
+ else
+ *encode_buffer = 0;
+
+ for (bp = 0; *keyword != 0; ++bp, ++keyword)
+ {
+ char c = *keyword;
+
+ if (bp + 2 /* enough for URL encoding also.. */ >= encode_buffer_size)
+ {
+ encode_buffer = x2realloc (encode_buffer, &encode_buffer_size);
+ }
+
+ if (c == '%')
+ {
+ strcpy (encode_buffer + bp, "%25");
+ bp += 2;
+ }
+ else if (c == '=')
+ {
+ strcpy (encode_buffer + bp, "%3D");
+ bp += 2;
+ }
+ else
+ encode_buffer[bp] = c;
+ }
+
+ encode_buffer[bp] = 0;
+
+ return encode_buffer;
+}
+
+static void
+xheader_print_n (struct xheader *xhdr, char const *keyword,
+ char const *value, size_t vsize)
+{
+ size_t p;
+ size_t n = 0;
+ char nbuf[UINTMAX_STRSIZE_BOUND];
+ char const *np;
+ size_t len, klen;
+
+ keyword = xattr_encode_keyword (keyword);
+ klen = strlen (keyword);
+ len = klen + vsize + 3; /* ' ' + '=' + '\n' */
+
+ do
+ {
+ p = n;
+ np = umaxtostr (len + p, nbuf);
+ n = nbuf + sizeof nbuf - 1 - np;
+ }
+ while (n != p);
+
+ x_obstack_grow (xhdr, np, n);
+ x_obstack_1grow (xhdr, ' ');
+ x_obstack_grow (xhdr, keyword, klen);
+ x_obstack_1grow (xhdr, '=');
+ x_obstack_grow (xhdr, value, vsize);
+ x_obstack_1grow (xhdr, '\n');
+}
+
+static void
+xheader_print (struct xheader *xhdr, char const *keyword, char const *value)
+{
+ xheader_print_n (xhdr, keyword, value, strlen (value));
+}
+
+void
+xheader_finish (struct xheader *xhdr)
+{
+ struct keyword_list *kp;
+
+ for (kp = keyword_override_list; kp; kp = kp->next)
+ code_string (kp->value, kp->pattern, xhdr);
+
+ xhdr->buffer = obstack_finish (xhdr->stk);
+}
+
+void
+xheader_destroy (struct xheader *xhdr)
+{
+ if (xhdr->stk)
+ {
+ obstack_free (xhdr->stk, NULL);
+ free (xhdr->stk);
+ xhdr->stk = NULL;
+ }
+ else
+ free (xhdr->buffer);
+ xhdr->buffer = 0;
+ xhdr->size = 0;
+}
+
+\f
+/* Buildable strings */
+
+void
+xheader_string_begin (struct xheader *xhdr)
+{
+ xhdr->string_length = 0;
+}
+
+void
+xheader_string_add (struct xheader *xhdr, char const *s)
+{
+ if (xhdr->buffer)
+ return;
+ xheader_init (xhdr);
+ xhdr->string_length += strlen (s);
+ x_obstack_grow (xhdr, s, strlen (s));
+}
+
+bool
+xheader_string_end (struct xheader *xhdr, char const *keyword)
+{
+ uintmax_t len;
+ uintmax_t p;
+ uintmax_t n = 0;
+ size_t size;
+ char nbuf[UINTMAX_STRSIZE_BOUND];
+ char const *np;
+ char *cp;
+
+ if (xhdr->buffer)
+ return false;
+ xheader_init (xhdr);
+
+ len = strlen (keyword) + xhdr->string_length + 3; /* ' ' + '=' + '\n' */
+
+ do
+ {
+ p = n;
+ np = umaxtostr (len + p, nbuf);
+ n = nbuf + sizeof nbuf - 1 - np;
+ }
+ while (n != p);
+
+ p = strlen (keyword) + n + 2;
+ size = p;
+ if (size != p)
+ {
+ ERROR ((0, 0,
+ _("Generated keyword/value pair is too long (keyword=%s, length=%s)"),
+ keyword, nbuf));
+ obstack_free (xhdr->stk, obstack_finish (xhdr->stk));
+ return false;
+ }
+ x_obstack_blank (xhdr, p);
+ x_obstack_1grow (xhdr, '\n');
+ cp = obstack_next_free (xhdr->stk) - xhdr->string_length - p - 1;
+ memmove (cp + p, cp, xhdr->string_length);
+ cp = stpcpy (cp, np);
+ *cp++ = ' ';
+ cp = stpcpy (cp, keyword);
+ *cp++ = '=';
+ return true;
+}
+
+\f
+/* Implementations */
+
+static void
+out_of_range_header (char const *keyword, char const *value,
+ intmax_t minval, uintmax_t maxval)
+{
+ char minval_buf[INT_BUFSIZE_BOUND (intmax_t)];
+ char maxval_buf[UINTMAX_STRSIZE_BOUND];
+ char *minval_string = imaxtostr (minval, minval_buf);
+ char *maxval_string = umaxtostr (maxval, maxval_buf);
+
+ /* TRANSLATORS: The first %s is the pax extended header keyword
+ (atime, gid, etc.). */
+ ERROR ((0, 0, _("Extended header %s=%s is out of range %s..%s"),
+ keyword, value, minval_string, maxval_string));
+}
+
+static void
+code_string (char const *string, char const *keyword, struct xheader *xhdr)
+{
+ char *outstr;
+ if (!utf8_convert (true, string, &outstr))
+ {
+ /* FIXME: report error */
+ outstr = xstrdup (string);
+ }
+ xheader_print (xhdr, keyword, outstr);
+ free (outstr);
+}
+
+static void
+decode_string (char **string, char const *arg)