Parolin 0.7.9 6796
Console (soon DLLs) to do a tar like job
Loading...
Searching...
No Matches
xheader_k.c
Go to the documentation of this file.
1
2#include <shlwapi.h>
3
4#define xcalloc calloc
5#define xmalloc malloc
6
7/* This declaration must be extern, because ISO C99 section 6.9.2
8 prohibits a tentative definition that has both internal linkage and
9 incomplete type. If we made it static, we'd have to declare its
10 size which would be a maintenance pain; if we put its initializer
11 here, we'd need a boatload of forward declarations, which would be
12 even more of a pain. */
13extern struct xhdr_tab const xhdr_tab[];
14
19void *
20x2realloc(void *p, size_t *pn)
21{
22 return x2nrealloc(p, pn, 1);
23}
24
25/* Bound on length of the string representing an unsigned integer
26 value representable in B bits. log10 (2.0) < 146/485. The
27 smallest value of B where this bound is not tight is 2621. */
28
29enum { SYSINT_BUFSIZE =
31enum { BILLION = 1000000000, LOG10_BILLION = 9 };
33 UINTMAX_STRSIZE_BOUND + LOG10_BILLION + sizeof "-." - 1 };
34
35#ifndef HAVE_ICONV
36
37# undef iconv_open
38# define iconv_open(tocode, fromcode) ((iconv_t)-1)
39
40# undef iconv
41# define iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft) (errno = ENOSYS, (size_t)-1)
42
43# undef iconv_close
44# define iconv_close(cd) 0
45
46# undef iconv_t
47# define iconv_t int
48
49#endif
50
51static iconv_t conv_desc[2] = { (iconv_t)-1, (iconv_t)-1 };
52
53bool
55
56static bool
57xheader_keyword_override_p(const char *keyword);
58
59int
60fnmatch(const char *pattern, const char * string, int flags);
61
62bool
63xheader_keyword_deleted_p(const char *kw);
64
65static void
66decx(void *data, char const *keyword, char const *value, size_t size);
67
68static struct xhdr_tab const *
69locate_handler(char const *keyword);
70
71static void
72run_override_list(struct keyword_list *kp, struct tar_stat_info *st);
73
74void
76
77void
78_obstack_free(struct obstack *h, void *obj);
79
80static void
81call_freefun(struct obstack *h, void *old_chunk);
82
83static void *
84call_chunkfun(struct obstack *h, size_t size);
85
86void
88
89void *
90xrealloc(void *p, size_t n);
91
92static void
93xheader_xattr__add(struct xattr_array **xattr_map,
94 size_t *xattr_map_size,
95 const char *key, const char *val, size_t len);
96
97char *
98stpcpy(char *dest, const char *src);
99
100void
102 const char *key, const char *val, size_t len);
103
104static void
105xattr_decode_keyword(char *keyword);
106
107static void
108xattr_decoder(struct tar_stat_info *st,
109 char const *keyword, char const *arg, size_t size);
110
111static void
112xattr_coder(struct tar_stat_info const *st, char const *keyword,
113 struct xheader *xhdr, void const *data);
114
115static void
116xattr_acls_d_decoder(struct tar_stat_info *st,
117 char const *keyword, char const *arg, size_t size);
118
119static void
120xattr_acls_d_coder(struct tar_stat_info const *st, char const *keyword,
121 struct xheader *xhdr, void const *data);
122
123static void
124xattr_acls_a_decoder(struct tar_stat_info *st,
125 char const *keyword, char const *arg, size_t size);
126
127static void
128xattr_acls_a_coder(struct tar_stat_info const *st, char const *keyword,
129 struct xheader *xhdr, void const *data);
130
131static void
132xattr_selinux_decoder(struct tar_stat_info *st,
133 char const *keyword, char const *arg, size_t size);
134
135static void
136xattr_selinux_coder(struct tar_stat_info const *st, char const *keyword,
137 struct xheader *xhdr, void const *data);
138
139static void
140volume_offset_decoder(struct tar_stat_info *st,
141 char const *keyword,
142 char const *arg, size_t size);
143
144static void
145volume_offset_coder(struct tar_stat_info const *st, char const *keyword,
146 struct xheader *xhdr, void const *data);
147
148static void
149volume_size_decoder(struct tar_stat_info *st,
150 char const *keyword,
151 char const *arg, size_t size);
152
153static void
154volume_size_coder(struct tar_stat_info const *st, char const *keyword,
155 struct xheader *xhdr, void const *data);
156
157static void
158volume_filename_decoder(struct tar_stat_info *st,
159 char const *keyword __attribute__((unused)),
160 char const *arg,
161 size_t size __attribute__((unused)));
162
163static void
164volume_label_decoder(struct tar_stat_info *st,
165 char const *keyword __attribute__((unused)),
166 char const *arg,
167 size_t size __attribute__((unused)));
168
169static void
170volume_label_coder(struct tar_stat_info const *st, char const *keyword,
171 struct xheader *xhdr, void const *data);
172
173static void
174dumpdir_decoder(struct tar_stat_info *st,
175 char const *keyword __attribute__((unused)),
176 char const *arg,
177 size_t size);
178
179size_t
180dumpdir_size(const char *p);
181
182static void
183dumpdir_coder(struct tar_stat_info const *st, char const *keyword,
184 struct xheader *xhdr, void const *data);
185
186static void
187sparse_map_decoder(struct tar_stat_info *st,
188 char const *keyword,
189 char const *arg,
190 size_t size __attribute__((unused)));
191
192static void
193sparse_numbytes_decoder(struct tar_stat_info *st,
194 char const *keyword,
195 char const *arg,
196 size_t size __attribute__((unused)));
197
198static void
199sparse_numbytes_coder(struct tar_stat_info const *st, char const *keyword,
200 struct xheader *xhdr, void const *data);
201
202static void
203sparse_offset_decoder(struct tar_stat_info *st,
204 char const *keyword,
205 char const *arg,
206 size_t size __attribute__((unused)));
207
208static void
209sparse_offset_coder(struct tar_stat_info const *st, char const *keyword,
210 struct xheader *xhdr, void const *data);
211
212static void
213sparse_numblocks_decoder(struct tar_stat_info *st,
214 char const *keyword,
215 char const *arg,
216 size_t size __attribute__((unused)));
217
218static void
219sparse_numblocks_coder(struct tar_stat_info const *st, char const *keyword,
220 struct xheader *xhdr,
221 void const *data __attribute__ ((unused)));
222
223static void
224sparse_size_decoder(struct tar_stat_info *st,
225 char const *keyword,
226 char const *arg,
227 size_t size __attribute__((unused)));
228
229static void
230sparse_size_coder(struct tar_stat_info const *st, char const *keyword,
231 struct xheader *xhdr, void const *data);
232
233static void
234sparse_minor_decoder(struct tar_stat_info *st,
235 char const *keyword,
236 char const *arg,
237 size_t size);
238
239static void
240sparse_minor_coder(struct tar_stat_info const *st, char const *keyword,
241 struct xheader *xhdr, void const *data);
242
243static void
244sparse_major_decoder(struct tar_stat_info *st,
245 char const *keyword,
246 char const *arg,
247 size_t size);
248
249static void
250sparse_major_coder(struct tar_stat_info const *st, char const *keyword,
251 struct xheader *xhdr, void const *data);
252
253static void
254sparse_path_decoder(struct tar_stat_info *st,
255 char const *keyword __attribute__((unused)),
256 char const *arg,
257 size_t size __attribute__((unused)));
258
259static void
260uname_decoder(struct tar_stat_info *st,
261 char const *keyword __attribute__((unused)),
262 char const *arg,
263 size_t size __attribute__((unused)));
264
265static void
266uname_coder(struct tar_stat_info const *st, char const *keyword,
267 struct xheader *xhdr, void const *data __attribute__ ((unused)));
268
269static void
270uid_decoder(struct tar_stat_info *st,
271 char const *keyword,
272 char const *arg,
273 size_t size __attribute__((unused)));
274
275static void
276uid_coder(struct tar_stat_info const *st, char const *keyword,
277 struct xheader *xhdr, void const *data __attribute__ ((unused)));
278
279static bool
280decode_num_v2_arp(uintmax_t *num, char const *arg, uintmax_t maxval,
281 char const *keyword);
282
283static void
284size_decoder(struct tar_stat_info *st,
285 char const *keyword,
286 char const *arg,
287 size_t size __attribute__((unused)));
288
289static void
290code_num(uintmax_t value, char const *keyword, struct xheader *xhdr);
291
292static void
293size_coder(struct tar_stat_info const *st, char const *keyword,
294 struct xheader *xhdr, void const *data __attribute__ ((unused)));
295
296static void
297raw_path_decoder(struct tar_stat_info *st, char const *arg);
298
299static void
300path_decoder(struct tar_stat_info *st,
301 char const *keyword __attribute__((unused)),
302 char const *arg,
303 size_t size __attribute__((unused)));
304
305static void
306path_coder(struct tar_stat_info const *st, char const *keyword,
307 struct xheader *xhdr, void const *data __attribute__ ((unused)));
308
309static void
310mtime_decoder(struct tar_stat_info *st,
311 char const *keyword,
312 char const *arg,
313 size_t size __attribute__((unused)));
314
315static void
316mtime_coder(struct tar_stat_info const *st, char const *keyword,
317 struct xheader *xhdr, void const *data);
318
319static void
320linkpath_decoder(struct tar_stat_info *st,
321 char const *keyword __attribute__((unused)),
322 char const *arg,
323 size_t size __attribute__((unused)));
324
325static void
326linkpath_coder(struct tar_stat_info const *st, char const *keyword,
327 struct xheader *xhdr, void const *data __attribute__ ((unused)));
328
329static void
330decode_string(char **string, char const *arg);
331
332static void
333gname_decoder(struct tar_stat_info *st,
334 char const *keyword __attribute__((unused)),
335 char const *arg,
336 size_t size __attribute__((unused)));
337
338bool
339utf8_convert(bool to_utf, char const *input, char **output);
340
341static iconv_t
342utf8_init(bool to_utf);
343
344static void
345code_string(char const *string, char const *keyword, struct xheader *xhdr);
346
347static void
348gname_coder(struct tar_stat_info const *st, char const *keyword,
349 struct xheader *xhdr, void const *data __attribute__ ((unused)));
350intmax_t
351represent_uintmax(uintmax_t n);
352
353intmax_t
354strtosysint(char const *arg, char **arglim, intmax_t minval, uintmax_t maxval);
355
356static bool
357decode_signed_num(intmax_t *num, char const *arg,
358 intmax_t minval, uintmax_t maxval,
359 char const *keyword);
360
361static void
362gid_decoder(struct tar_stat_info *st,
363 char const *keyword,
364 char const *arg,
365 size_t size __attribute__((unused)));
366
367char *
368sysinttostr(uintmax_t value, intmax_t minval, uintmax_t maxval,
369 char buf[SYSINT_BUFSIZE]);
370
371static void
372code_signed_num(uintmax_t value, char const *keyword,
373 intmax_t minval, uintmax_t maxval, struct xheader *xhdr);
374
375static void
376gid_coder(struct tar_stat_info const *st, char const *keyword,
377 struct xheader *xhdr, void const *data __attribute__ ((unused)));
378
379static void
380ctime_decoder(struct tar_stat_info *st,
381 char const *keyword,
382 char const *arg,
383 size_t size __attribute__((unused)));
384
385static void
386ctime_coder(struct tar_stat_info const *st, char const *keyword,
387 struct xheader *xhdr, void const *data __attribute__ ((unused)));
388
389static void
390dummy_coder(struct tar_stat_info const *st __attribute__ ((unused)),
391 char const *keyword __attribute__ ((unused)),
392 struct xheader *xhdr __attribute__ ((unused)),
393 void const *data __attribute__ ((unused)));
394
395static void
396dummy_decoder(struct tar_stat_info *st __attribute__ ((unused)),
397 char const *keyword __attribute__ ((unused)),
398 char const *arg __attribute__ ((unused)),
399 size_t size __attribute__((unused)));
400
401char *
402imaxtostr(intmax_t i, char *buf);
403
404static void
405out_of_range_header(char const *keyword, char const *value,
406 intmax_t minval, uintmax_t maxval);
407
408bool
409valid_timespec(struct timespec t);
410
411static bool
412decode_time(struct timespec *ts, char const *arg, char const *keyword);
413
414static void
415atime_decoder(struct tar_stat_info *st,
416 char const *keyword,
417 char const *arg,
418 size_t size __attribute__((unused)));
419
420static intmax_t
421strtoimax(char const *ptr, char **endptr, int base);
422
423void
424code_ns_fraction(int ns, char *p);
425
426struct timespec
427decode_timespec(char const *arg, char **arg_lim, bool parse_fraction);
428
429static void
430x_obstack_1grow(struct xheader *xhdr, char c);
431
432static void
433x_obstack_grow(struct xheader *xhdr, const char *ptr, size_t length);
434
435char *
436umaxtostr(uintmax_t i, char *buf);
437
438static char *
439xattr_encode_keyword(const char *keyword);
440
441static void
442xheader_print_n(struct xheader *xhdr, char const *keyword,
443 char const *value, size_t vsize);
444
445static void
446xheader_print(struct xheader *xhdr, char const *keyword, char const *value);
447
448static void
449atime_coder(struct tar_stat_info const *st, char const *keyword,
450 struct xheader *xhdr, void const *data __attribute__ ((unused)));
451static void
452
453code_time(struct timespec t, char const *keyword, struct xheader *xhdr);
454
455static bool
456decode_record(struct xheader *xhdr,
457 char **ptr,
458 void (*handler)(void *, char const *, char const *, size_t),
459 void *data);
460
462//functions
463
465
470static bool
471xheader_keyword_override_p(const char *keyword)
472{
473 struct keyword_list *kp;
474
475 for (kp = keyword_override_list; kp; kp = kp->next)
476 if (strcmp(kp->pattern, keyword) == 0)
477 return true;
478 return false;
479}
480
481#include "fnmatch_arp.c"
482
487bool
489{
490 struct keyword_list *kp;
491
492 for (kp = keyword_pattern_list; kp; kp = kp->next)
493 if (fnmatch(kp->pattern, kw, 0) == 0)
494 return true;
495 return false;
496}
497
502static void
503decx(void *data, char const *keyword, char const *value, size_t size)
504{
505 struct xhdr_tab const *t;
506 struct tar_stat_info *st = data;
507
508 if (xheader_keyword_deleted_p(keyword)
509 || xheader_keyword_override_p(keyword))
510 return;
511
512 t = locate_handler(keyword);
513 if (t)
514 t->decoder(st, keyword, value, size);
515 else
516 {
517#if 0
519 (0, 0, _("Ignoring unknown extended header keyword '%s'"),
520 keyword));
521#endif
522 pedro_dprintf(2, "Ignoring unknown extended header keyword '%s'", keyword);
523 }
524}
525
530static struct xhdr_tab const *
531locate_handler(char const *keyword)
532{
533 struct xhdr_tab const *p;
534
535 for (p = xhdr_tab; p->keyword; p++)
536 if (p->prefix)
537 {
538 if (strncmp(p->keyword, keyword, strlen(p->keyword)) == 0)
539 return p;
540 }
541 else
542 {
543 if (strcmp(p->keyword, keyword) == 0)
544 return p;
545 }
546
547 return NULL;
548}
549
554static void
555run_override_list(struct keyword_list *kp, struct tar_stat_info *st)
556{
557 for (; kp; kp = kp->next)
558 {
559 struct xhdr_tab const *t = locate_handler(kp->pattern);
560 if (t)
561 t->decoder(st, t->keyword, kp->value, strlen(kp->value));
562 }
563}
564
569void
571{
572 run_override_list(keyword_global_override_list, st);
573 run_override_list(global_header_override_list, st);
574
575 if (st->xhdr.size)
576 {
577 char *p = st->xhdr.buffer + BLOCKSIZE;
578 while (decode_record(&st->xhdr, &p, decx, st))
579 continue;
580 }
581 run_override_list(keyword_override_list, st);
582
583 /* The archived (effective) file size is always set directly in tar header
584 field, possibly overridden by "size" extended header - in both cases,
585 result is now decoded in st->stat.st_size */
586 st->archive_file_size = st->stat.st_size;
587
588 /* The real file size (given by stat()) may be redefined for sparse
589 files in "GNU.sparse.realsize" extended header */
590 if (st->real_size_set)
591 st->stat.st_size = st->real_size;
592}
593
594/* Free objects in obstack H, including OBJ and everything allocate
595 more recently than OBJ. If OBJ is zero, free everything in H. */
596
601void
602_obstack_free(struct obstack *h, void *obj)
603{
604 struct _obstack_chunk *lp; /* below addr of any objects in this chunk */
605 struct _obstack_chunk *plp; /* point to previous chunk if any */
606
607 lp = h->chunk;
608 /* We use >= because there cannot be an object at the beginning of a chunk.
609 But there can be an empty object at that address
610 at the end of another chunk. */
611 while (lp != 0 && ((void *)lp >= obj || (void *)(lp)->limit < obj))
612 {
613 plp = lp->prev;
614 call_freefun(h, lp);
615 lp = plp;
616 /* If we switch chunks, we can't tell whether the new current
617 chunk contains an empty object, so assume that it may. */
618 h->maybe_empty_object = 1;
619 }
620 if (lp)
621 {
622 h->object_base = h->next_free = (char *)(obj);
623 h->chunk_limit = lp->limit;
624 h->chunk = lp;
625 }
626 else if (obj != 0)
627 /* obj is not in any of the chunks! */
628 abort();
629}
630
635static void
636call_freefun(struct obstack *h, void *old_chunk)
637{
638 if (h->use_extra_arg)
639 h->freefun.extra(h->extra_arg, old_chunk);
640 else
641 h->freefun.plain(old_chunk);
642}
643
644/* Call functions with either the traditional malloc/free calling
645 interface, or the mmalloc/mfree interface (that adds an extra first
646 argument), based on the value of use_extra_arg. */
647
652void
654{
655 struct _obstack_chunk *old_chunk = h->chunk;
656 struct _obstack_chunk *new_chunk = 0;
657 size_t obj_size = h->next_free - h->object_base;
658 char *object_base;
659
660 /* Compute size for new chunk. */
661 size_t sum1 = obj_size + length;
662 size_t sum2 = sum1 + h->alignment_mask;
663 size_t new_size = sum2 + (obj_size >> 3) + 100;
664
665 if (new_size < sum2)
666 new_size = sum2;
667 if (new_size < h->chunk_size)
668 new_size = h->chunk_size;
669
670 /* Allocate and initialize the new chunk. */
671 if (obj_size <= sum1 && sum1 <= sum2)
672 new_chunk = call_chunkfun(h, new_size);
673 if (!new_chunk)
674 {
675 assert(0 && "Memory exausted");
676 pedro_dprintf(2, "Memory exausted");
677 }
678 h->chunk = new_chunk;
679 new_chunk->prev = old_chunk;
680 new_chunk->limit = h->chunk_limit = (char *)new_chunk + new_size;
681
682 /* Compute an aligned object_base in the new chunk */
683 object_base =
684 __PTR_ALIGN((char *)new_chunk, new_chunk->contents, h->alignment_mask);
685
686 /* Move the existing object to the new chunk. */
687 memcpy(object_base, h->object_base, obj_size);
688
689 /* If the object just copied was the only data in OLD_CHUNK,
690 free that chunk and remove it from the chain.
691 But not if that chunk might contain an empty object. */
692 if (!h->maybe_empty_object
693 && (h->object_base
694 == __PTR_ALIGN((char *)old_chunk, old_chunk->contents,
695 h->alignment_mask)))
696 {
697 new_chunk->prev = old_chunk->prev;
698 call_freefun(h, old_chunk);
699 }
700
701 h->object_base = object_base;
702 h->next_free = h->object_base + obj_size;
703 /* The new chunk certainly contains no empty object yet. */
704 h->maybe_empty_object = 0;
705}
706
711void *
712xrealloc(void *p, size_t n)
713{
714 if (!n && p)
715 {
716 /* The GNU and C99 realloc behaviors disagree here. Act like
717 GNU, even if the underlying realloc is C99. */
718 free(p);
719 return NULL;
720 }
721
722 p = realloc(p, n);
723 if (!p && n)
724 {
725 assert(0 && "will never occur...");
726 }
727 return p;
728}
729
734static void
735xheader_xattr__add(struct xattr_array **xattr_map,
736 size_t *xattr_map_size,
737 const char *key, const char *val, size_t len)
738{
739 size_t pos = (*xattr_map_size)++;
740
741 *xattr_map = xrealloc(*xattr_map,
742 *xattr_map_size * sizeof(struct xattr_array));
743 (*xattr_map)[pos].xkey = xstrdup(key);
744 (*xattr_map)[pos].xval_ptr = xmemdup(val, len + 1);
745 (*xattr_map)[pos].xval_len = len;
746}
747
752char *
753stpcpy(char *dest, const char *src)
754{
755 register char *d = dest;
756 register const char *s = src;
757
758 do
759 *d++ = *s;
760 while (*s++ != '\0');
761
762 return d - 1;
763}
764
769void
771 const char *key, const char *val, size_t len)
772{
773 size_t klen = strlen(key);
774 char *xkey = xmalloc(strlen("SCHILY.xattr.") + klen + 1);
775 char *tmp = xkey;
776
777 tmp = stpcpy(tmp, "SCHILY.xattr.");
778 stpcpy(tmp, key);
779
780 xheader_xattr__add(&st->xattr_map, &st->xattr_map_size, xkey, val, len);
781
782 free(xkey);
783}
784
785/* This is reversal function for xattr_encode_keyword. See comment for
786 xattr_encode_keyword() for more info. */
787
792static void
793xattr_decode_keyword(char *keyword)
794{
795 char *kpr, *kpl; /* keyword pointer left/right */
796
797 kpr = kpl = keyword;
798
799 for (;;)
800 {
801 if (*kpr == '%')
802 {
803 if (kpr[1] == '3' && kpr[2] == 'D')
804 {
805 *kpl = '=';
806 kpr += 3;
807 kpl++;
808 continue;
809 }
810 else if (kpr[1] == '2' && kpr[2] == '5')
811 {
812 *kpl = '%';
813 kpr += 3;
814 kpl++;
815 continue;
816 }
817 }
818
819 *kpl = *kpr;
820
821 if (*kpr == 0)
822 break;
823
824 kpr++;
825 kpl++;
826 }
827}
828
833static void
834xattr_decoder(struct tar_stat_info *st,
835 char const *keyword, char const *arg, size_t size)
836{
837 char *xstr, *xkey;
838
839 /* copy keyword */
840 size_t klen_raw = strlen(keyword);
841
842 xkey = alloca(klen_raw + 1);
843 memcpy(xkey, keyword, klen_raw + 1) /* including null-terminating */;
844
845 /* copy value */
846 xstr = alloca(size + 1);
847 memcpy(xstr, arg, size + 1); /* separator included, for GNU tar '\n' */;
848
849 xattr_decode_keyword(xkey);
850
851 xheader_xattr_add(st, xkey + strlen("SCHILY.xattr."), xstr, size);
852}
853
858static void
859xattr_coder(struct tar_stat_info const *st, char const *keyword,
860 struct xheader *xhdr, void const *data)
861{
862 struct xattr_array *xattr_map = st->xattr_map;
863 const size_t *off = data;
864
865 xheader_print_n(xhdr, keyword,
866 xattr_map[*off].xval_ptr, xattr_map[*off].xval_len);
867}
868
873static void
874xattr_acls_d_decoder(struct tar_stat_info *st,
875 __attribute__((unused)) char const *keyword, char const *arg, size_t size)
876{
877 st->acls_d_ptr = xmemdup(arg, size + 1);
878 st->acls_d_len = size;
879}
880
885static void
886xattr_acls_d_coder(struct tar_stat_info const *st, char const *keyword,
887 struct xheader *xhdr, __attribute__((unused)) void const *data)
888{
889 xheader_print_n(xhdr, keyword, st->acls_d_ptr, st->acls_d_len);
890}
891
896static void
897xattr_acls_a_decoder(struct tar_stat_info *st,
898 __attribute__((unused)) char const *keyword, char const *arg, size_t size)
899{
900 st->acls_a_ptr = xmemdup(arg, size + 1);
901 st->acls_a_len = size;
902}
903
908static void
909xattr_acls_a_coder(struct tar_stat_info const *st, char const *keyword,
910 struct xheader *xhdr, __attribute__((unused)) void const *data)
911{
912 xheader_print_n(xhdr, keyword, st->acls_a_ptr, st->acls_a_len);
913}
914
919static void
920xattr_selinux_decoder(struct tar_stat_info *st,
921 __attribute__((unused)) char const *keyword, char const *arg, __attribute__((unused)) size_t size)
922{
923 decode_string(&st->cntx_name, arg);
924}
925
930static void
931xattr_selinux_coder(struct tar_stat_info const *st, char const *keyword,
932 struct xheader *xhdr, __attribute__((unused)) void const *data)
933{
934 code_string(st->cntx_name, keyword, xhdr);
935}
936
941static void
942volume_offset_decoder(__attribute__((unused)) struct tar_stat_info *st,
943 char const *keyword,
944 char const *arg, __attribute__((unused)) size_t size)
945{
946 uintmax_t u;
947
948 if (decode_num_v2_arp(&u, arg, TYPE_MAXIMUM(uintmax_t), keyword))
950}
951
952/* FIXME: Merge with volume_size_coder */
953
958static void
959volume_offset_coder(__attribute__((unused)) struct tar_stat_info const *st, char const *keyword,
960 struct xheader *xhdr, void const *data)
961{
962 off_t const *v = data;
963
964 code_num(*v, keyword, xhdr);
965}
966
971static void
972volume_size_decoder(__attribute__((unused)) struct tar_stat_info *st,
973 char const *keyword,
974 char const *arg, __attribute__((unused)) size_t size)
975{
976 uintmax_t u;
977
978 if (decode_num_v2_arp(&u, arg, TYPE_MAXIMUM(uintmax_t), keyword))
980}
981
986static void
987volume_size_coder(__attribute__((unused)) struct tar_stat_info const *st, char const *keyword,
988 struct xheader *xhdr, void const *data)
989{
990 off_t const *v = data;
991
992 code_num(*v, keyword, xhdr);
993}
994
999static void
1000volume_filename_decoder(__attribute__((unused)) struct tar_stat_info *st,
1001 char const *keyword __attribute__((unused)),
1002 char const *arg,
1003 size_t size __attribute__((unused)))
1004{
1005 decode_string(&continued_file_name, arg);
1006}
1007
1012static void
1013volume_label_decoder(__attribute__((unused)) struct tar_stat_info *st,
1014 char const *keyword __attribute__((unused)),
1015 char const *arg,
1016 size_t size __attribute__((unused)))
1017{
1018 decode_string(&volume_label, arg);
1019}
1020
1025static void
1026volume_label_coder(__attribute__((unused)) struct tar_stat_info const *st, char const *keyword,
1027 struct xheader *xhdr, void const *data)
1028{
1029 code_string(data, keyword, xhdr);
1030}
1031
1036static void
1037dumpdir_decoder(struct tar_stat_info *st,
1038 char const *keyword __attribute__((unused)),
1039 char const *arg,
1040 size_t size)
1041{
1042 st->dumpdir = xmalloc(size);
1043 memcpy(st->dumpdir, arg, size);
1044}
1045
1046/* Return size in bytes of the dumpdir array P */
1047
1052size_t
1053dumpdir_size(const char *p)
1054{
1055 size_t totsize = 0;
1056
1057 while (*p)
1058 {
1059 size_t size = strlen(p) + 1;
1060 totsize += size;
1061 p += size;
1062 }
1063 return totsize + 1;
1064}
1065
1070static void
1071dumpdir_coder(__attribute__((unused)) struct tar_stat_info const *st, char const *keyword,
1072 struct xheader *xhdr, void const *data)
1073{
1074 xheader_print_n(xhdr, keyword, data, dumpdir_size(data));
1075}
1076
1081static void
1082sparse_map_decoder(struct tar_stat_info *st,
1083 char const *keyword,
1084 char const *arg,
1085 size_t size __attribute__((unused)))
1086{
1087 int offset = 1;
1088 struct sp_array e;
1089
1090 st->sparse_map_avail = 0;
1091 while (1)
1092 {
1093 intmax_t u;
1094 char *delim;
1095
1096 if (!ISDIGIT(*arg))
1097 {
1098#if 0
1099 ERROR((0, 0, _("Malformed extended header: invalid %s=%s"),
1100 keyword, arg));
1101#endif
1102 pedro_dprintf(2, "Malformed extended header: invalid %s=%s",
1103 keyword, arg);
1104 return;
1105 }
1106
1107 errno = 0;
1108 u = strtoimax(arg, &delim, 10);
1109 if (TYPE_MAXIMUM(off_t) < u)
1110 {
1111 u = TYPE_MAXIMUM(off_t);
1112 errno = ERANGE;
1113 }
1114 if (offset)
1115 {
1116 e.offset = u;
1117 if (errno == ERANGE)
1118 {
1119 out_of_range_header(keyword, arg, 0, TYPE_MAXIMUM(off_t));
1120 return;
1121 }
1122 }
1123 else
1124 {
1125 e.numbytes = u;
1126 if (errno == ERANGE)
1127 {
1128 out_of_range_header(keyword, arg, 0, TYPE_MAXIMUM(off_t));
1129 return;
1130 }
1131 if (st->sparse_map_avail < st->sparse_map_size)
1132 st->sparse_map[st->sparse_map_avail++] = e;
1133 else
1134 {
1135#if 0
1136 ERROR((0, 0, _("Malformed extended header: excess %s=%s"),
1137 keyword, arg));
1138#endif
1139 pedro_dprintf(2, "Malformed extended header: excess %s=%s",
1140 keyword, arg);
1141 return;
1142 }
1143 }
1144
1145 offset = !offset;
1146
1147 if (*delim == 0)
1148 break;
1149 else if (*delim != ',')
1150 {
1151#if 0
1152 ERROR((0, 0,
1153 _("Malformed extended header: invalid %s: unexpected delimiter %c"),
1154 keyword, *delim));
1155#endif
1156 pedro_dprintf(2, "Malformed extended header: invalid %s: unexpected delimiter %c",
1157 keyword, *delim);
1158 return;
1159 }
1160
1161 arg = delim + 1;
1162 }
1163
1164 if (!offset)
1165 {
1166#if 0
1167 ERROR((0, 0,
1168 _("Malformed extended header: invalid %s: odd number of values"),
1169 keyword));
1170#endif
1171 pedro_dprintf(2, "Malformed extended header: invalid %s: odd number of values",
1172 keyword);
1173 }
1174}
1175
1180static void
1181sparse_numbytes_decoder(struct tar_stat_info *st,
1182 char const *keyword,
1183 char const *arg,
1184 size_t size __attribute__((unused)))
1185{
1186 uintmax_t u;
1187
1188 if (decode_num_v2_arp(&u, arg, TYPE_MAXIMUM(off_t), keyword))
1189 {
1190 if (st->sparse_map_avail < st->sparse_map_size)
1191 st->sparse_map[st->sparse_map_avail++].numbytes = u;
1192 else
1193 {
1194#if 0
1195 ERROR((0, 0, _("Malformed extended header: excess %s=%s"),
1196 keyword, arg));
1197#endif
1198 pedro_dprintf(2, "Malformed extended header: excess %s=%s",
1199 keyword, arg);
1200 }
1201 }
1202}
1203
1208static void
1209sparse_numbytes_coder(struct tar_stat_info const *st, char const *keyword,
1210 struct xheader *xhdr, void const *data)
1211{
1212 size_t const *pi = data;
1213
1214 code_num(st->sparse_map[*pi].numbytes, keyword, xhdr);
1215}
1216
1221static void
1222sparse_offset_decoder(struct tar_stat_info *st,
1223 char const *keyword,
1224 char const *arg,
1225 size_t size __attribute__((unused)))
1226{
1227 uintmax_t u;
1228
1229 if (decode_num_v2_arp(&u, arg, TYPE_MAXIMUM(off_t), keyword))
1230 {
1231 if (st->sparse_map_avail < st->sparse_map_size)
1232 st->sparse_map[st->sparse_map_avail].offset = u;
1233 else
1234 {
1235#if 0
1236 ERROR((0, 0, _("Malformed extended header: excess %s=%s"),
1237 "GNU.sparse.offset", arg));
1238#endif
1239
1240 pedro_dprintf(2, "Malformed extended header: excess %s=%s",
1241 "GNU.sparse.offset", arg);
1242 }
1243 }
1244}
1245
1250static void
1251sparse_offset_coder(struct tar_stat_info const *st, char const *keyword,
1252 struct xheader *xhdr, void const *data)
1253{
1254 size_t const *pi = data;
1255
1256 code_num(st->sparse_map[*pi].offset, keyword, xhdr);
1257}
1258
1263static void
1264sparse_numblocks_decoder(struct tar_stat_info *st,
1265 char const *keyword,
1266 char const *arg,
1267 size_t size __attribute__((unused)))
1268{
1269 uintmax_t u;
1270
1271 if (decode_num_v2_arp(&u, arg, SIZE_MAX, keyword))
1272 {
1273 st->sparse_map_size = u;
1274 st->sparse_map = xcalloc(u, sizeof st->sparse_map[0]);
1275 st->sparse_map_avail = 0;
1276 }
1277}
1278
1283static void
1284sparse_numblocks_coder(struct tar_stat_info const *st, char const *keyword,
1285 struct xheader *xhdr,
1286 void const *data __attribute__ ((unused)))
1287{
1288 code_num(st->sparse_map_avail, keyword, xhdr);
1289}
1290
1295static void
1296sparse_size_decoder(struct tar_stat_info *st,
1297 char const *keyword,
1298 char const *arg,
1299 size_t size __attribute__((unused)))
1300{
1301 uintmax_t u;
1302
1303 if (decode_num_v2_arp(&u, arg, TYPE_MAXIMUM(off_t), keyword))
1304 {
1305 st->real_size_set = true;
1306 st->real_size = u;
1307 }
1308}
1309
1314static void
1315sparse_size_coder(struct tar_stat_info const *st, char const *keyword,
1316 struct xheader *xhdr, void const *data)
1317{
1318 size_coder(st, keyword, xhdr, data);
1319}
1320
1325static void
1326sparse_minor_decoder(struct tar_stat_info *st,
1327 char const *keyword,
1328 char const *arg,
1329 __attribute__((unused)) size_t size)
1330{
1331 uintmax_t u;
1332
1333 if (decode_num_v2_arp(&u, arg, TYPE_MAXIMUM(unsigned), keyword))
1334 st->sparse_minor = u;
1335}
1336
1341static void
1342sparse_minor_coder(struct tar_stat_info const *st, char const *keyword,
1343 struct xheader *xhdr, __attribute__((unused)) void const *data)
1344{
1345 code_num(st->sparse_minor, keyword, xhdr);
1346}
1347
1352static void
1353sparse_major_decoder(struct tar_stat_info *st,
1354 char const *keyword,
1355 char const *arg,
1356 __attribute__((unused)) size_t size)
1357{
1358 uintmax_t u;
1359
1360 if (decode_num_v2_arp(&u, arg, TYPE_MAXIMUM(unsigned), keyword))
1361 st->sparse_major = u;
1362}
1363
1368static void
1369sparse_major_coder(struct tar_stat_info const *st, char const *keyword,
1370 struct xheader *xhdr, __attribute__((unused)) void const *data)
1371{
1372 code_num(st->sparse_major, keyword, xhdr);
1373}
1374
1379static void
1380sparse_path_decoder(struct tar_stat_info *st,
1381 char const *keyword __attribute__((unused)),
1382 char const *arg,
1383 size_t size __attribute__((unused)))
1384{
1385 st->sparse_name_done = true;
1386 raw_path_decoder(st, arg);
1387}
1388
1393static void
1394uname_decoder(struct tar_stat_info *st,
1395 char const *keyword __attribute__((unused)),
1396 char const *arg,
1397 size_t size __attribute__((unused)))
1398{
1399 decode_string(&st->uname, arg);
1400}
1401
1406static void
1407uname_coder(struct tar_stat_info const *st, char const *keyword,
1408 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1409{
1410 code_string(st->uname, keyword, xhdr);
1411}
1412
1417static void
1418uid_decoder(struct tar_stat_info *st,
1419 char const *keyword,
1420 char const *arg,
1421 size_t size __attribute__((unused)))
1422{
1423 intmax_t u;
1424
1425 if (decode_signed_num(&u, arg, TYPE_MINIMUM(uid_t),
1426 TYPE_MAXIMUM(uid_t), keyword))
1427 st->stat.st_uid = u;
1428}
1429
1434static void
1435uid_coder(struct tar_stat_info const *st, char const *keyword,
1436 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1437{
1438 code_signed_num(st->stat.st_uid, keyword,
1440}
1441
1446static bool
1447decode_num_v2_arp(uintmax_t *num, char const *arg, uintmax_t maxval,
1448 char const *keyword)
1449{
1450 intmax_t i;
1451
1452 if (!decode_signed_num(&i, arg, 0, maxval, keyword))
1453 return false;
1454 *num = i;
1455 return true;
1456}
1457
1462static void
1463size_decoder(struct tar_stat_info *st,
1464 char const *keyword,
1465 char const *arg,
1466 size_t size __attribute__((unused)))
1467{
1468 uintmax_t u;
1469
1470 if (decode_num_v2_arp(&u, arg, TYPE_MAXIMUM(off_t), keyword))
1471 st->stat.st_size = u;
1472}
1473
1478static void
1479code_num(uintmax_t value, char const *keyword, struct xheader *xhdr)
1480{
1481 code_signed_num(value, keyword, 0, UINTMAX_MAX, xhdr);
1482}
1483
1488static void
1489size_coder(struct tar_stat_info const *st, char const *keyword,
1490 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1491{
1492 code_num(st->stat.st_size, keyword, xhdr);
1493}
1494
1499static void
1500raw_path_decoder(struct tar_stat_info *st, char const *arg)
1501{
1502 decode_string(&st->orig_file_name, arg);
1503 decode_string(&st->file_name, arg);
1505 //pedro_dprintf(-1, "aqui st->had_trailing_slash %s %d\n", st->file_name, st->had_trailing_slash);
1506}
1507
1512static void
1513path_decoder(struct tar_stat_info *st,
1514 char const *keyword __attribute__((unused)),
1515 char const *arg,
1516 size_t size __attribute__((unused)))
1517{
1518 if (!st->sparse_name_done)
1519 raw_path_decoder(st, arg);
1520}
1521
1526static void
1527path_coder(struct tar_stat_info const *st, char const *keyword,
1528 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1529{
1530 code_string(st->file_name, keyword, xhdr);
1531}
1532
1537static void
1538mtime_decoder(struct tar_stat_info *st,
1539 char const *keyword,
1540 char const *arg,
1541 size_t size __attribute__((unused)))
1542{
1543 struct timespec ts;
1544
1545 if (decode_time(&ts, arg, keyword))
1546 st->mtime = ts;
1547}
1548
1553static void
1554mtime_coder(struct tar_stat_info const *st, char const *keyword,
1555 struct xheader *xhdr, void const *data)
1556{
1557 struct timespec const *mtime = data;
1558
1559 code_time(mtime ? *mtime : st->mtime, keyword, xhdr);
1560}
1561
1566static void
1567linkpath_decoder(struct tar_stat_info *st,
1568 char const *keyword __attribute__((unused)),
1569 char const *arg,
1570 size_t size __attribute__((unused)))
1571{
1572 decode_string(&st->link_name, arg);
1573}
1574
1579static void
1580linkpath_coder(struct tar_stat_info const *st, char const *keyword,
1581 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1582{
1583 code_string(st->link_name, keyword, xhdr);
1584}
1585
1590static void
1591decode_string(char **string, char const *arg)
1592{
1593 if (*string)
1594 {
1595 free(*string);
1596 *string = NULL;
1597 }
1598 if (!utf8_convert(false, arg, string))
1599 {
1600 /* FIXME: report error and act accordingly to --pax invalid=UTF-8 */
1601 assign_string(string, arg);
1602 }
1603}
1604
1609static void
1610gname_decoder(struct tar_stat_info *st,
1611 char const *keyword __attribute__((unused)),
1612 char const *arg,
1613 size_t size __attribute__((unused)))
1614{
1615 decode_string(&st->gname, arg);
1616}
1617
1622bool
1623utf8_convert(bool to_utf, char const *input, char **output)
1624{
1625 char *ob, *ret;
1626 size_t inlen;
1627 size_t outlen;
1628 iconv_t cd = utf8_init(to_utf);
1629
1630 if (cd == 0)
1631 {
1632 *output = xstrdup(input);
1633 return true;
1634 }
1635 else if (cd == (iconv_t)-1)
1636 return false;
1637
1638 inlen = strlen(input) + 1;
1639 outlen = inlen * MB_LEN_MAX + 1;
1640 ob = ret = malloc(outlen);
1641 //ib = (char *) input;
1642 /* According to POSIX, "if iconv() encounters a character in the input
1643 buffer that is valid, but for which an identical character does not
1644 exist in the target codeset, iconv() shall perform an
1645 implementation-defined conversion on this character." It will "update
1646 the variables pointed to by the arguments to reflect the extent of the
1647 conversion and return the number of non-identical conversions performed".
1648 On error, it returns -1.
1649 In other words, non-zero return always indicates failure, either because
1650 the input was not fully converted, or because it was converted in a
1651 non-reversible way.
1652 */
1653 if (iconv(cd, &ib, &inlen, &ob, &outlen) != 0)
1654 {
1655 free(ret);
1656 return false;
1657 }
1658 *ob = 0;
1659 *output = ret;
1660 return true;
1661}
1662
1667static iconv_t
1668utf8_init(bool to_utf)
1669{
1670 if (conv_desc[(int)to_utf] == (iconv_t)-1)
1671 {
1672 if (to_utf)
1673 conv_desc[(int)to_utf] = iconv_open("UTF-8", locale_charset());
1674 else
1675 conv_desc[(int)to_utf] = iconv_open(locale_charset(), "UTF-8");
1676 }
1677 return conv_desc[(int)to_utf];
1678}
1679
1684static void
1685code_string(char const *string, char const *keyword, struct xheader *xhdr)
1686{
1687 char *outstr;
1688
1689 if (!utf8_convert(true, string, &outstr))
1690 {
1691 /* FIXME: report error */
1692 outstr = xstrdup(string);
1693 }
1694 xheader_print(xhdr, keyword, outstr);
1695 free(outstr);
1696}
1697
1702static void
1703gname_coder(struct tar_stat_info const *st, char const *keyword,
1704 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1705{
1706 code_string(st->gname, keyword, xhdr);
1707}
1708
1709/* Represent N using a signed integer I such that (uintmax_t) I == N.
1710 With a good optimizing compiler, this is equivalent to (intmax_t) i
1711 and requires zero machine instructions. */
1712
1713#if !(UINTMAX_MAX / 2 <= INTMAX_MAX)
1714# error "represent_uintmax returns intmax_t to represent uintmax_t"
1715#endif
1716
1721intmax_t
1723{
1724 if (n <= INTMAX_MAX)
1725 return n;
1726 else
1727 {
1728 /* Avoid signed integer overflow on picky platforms. */
1729 intmax_t nd = n - INTMAX_MIN;
1730 return nd + INTMAX_MIN;
1731 }
1732}
1733
1734/* Convert a prefix of the string ARG to a system integer type whose
1735 minimum value is MINVAL and maximum MAXVAL. If MINVAL is negative,
1736 negative integers MINVAL .. -1 are assumed to be represented using
1737 leading '-' in the usual way. If the represented value exceeds
1738 INTMAX_MAX, return a negative integer V such that (uintmax_t) V
1739 yields the represented value. If ARGLIM is nonnull, store into
1740 * ARGLIM a pointer to the first character after the prefix.
1741
1742 This is the inverse of sysinttostr.
1743
1744 On a normal return, set errno = 0.
1745 On conversion error, return 0 and set errno = EINVAL.
1746 On overflow, return an extreme value and set errno = ERANGE. */
1747#if !(INTMAX_MAX <= UINTMAX_MAX)
1748# error "strtosysint: nonnegative intmax_t does not fit in uintmax_t"
1749#endif
1750
1755intmax_t
1756strtosysint(char const *arg, char **arglim, intmax_t minval, uintmax_t maxval)
1757{
1758 errno = 0;
1759 if (maxval <= INTMAX_MAX)
1760 {
1761 if (ISDIGIT(arg[*arg == '-']))
1762 {
1763 intmax_t i = strtoimax(arg, arglim, 10);
1764 intmax_t imaxval = maxval;
1765 if (minval <= i && i <= imaxval)
1766 return i;
1767 errno = ERANGE;
1768 return i < minval ? (intmax_t) minval : (intmax_t) maxval;
1769 }
1770 }
1771 else
1772 {
1773 if (ISDIGIT(*arg))
1774 {
1775 uintmax_t i = strtoumax(arg, arglim, 10);
1776 if (i <= maxval)
1777 return represent_uintmax(i);
1778 errno = ERANGE;
1779 return maxval;
1780 }
1781 }
1782
1783 errno = EINVAL;
1784 return 0;
1785}
1786
1791static bool
1792decode_signed_num(intmax_t *num, char const *arg,
1793 intmax_t minval, uintmax_t maxval,
1794 char const *keyword)
1795{
1796 char *arg_lim;
1797 intmax_t u = strtosysint(arg, &arg_lim, minval, maxval);
1798
1799 if (errno == EINVAL || *arg_lim)
1800 {
1801#if 0
1802 ERROR((0, 0, _("Malformed extended header: invalid %s=%s"),
1803 keyword, arg));
1804#endif
1805 pedro_dprintf(2, "Malformed extended header: invalid %s=%s",
1806 keyword, arg);
1807 return false;
1808 }
1809
1810 if (errno == ERANGE)
1811 {
1812 out_of_range_header(keyword, arg, minval, maxval);
1813 return false;
1814 }
1815
1816 *num = u;
1817 return true;
1818}
1819
1824static void
1825gid_decoder(struct tar_stat_info *st,
1826 char const *keyword,
1827 char const *arg,
1828 size_t size __attribute__((unused)))
1829{
1830 intmax_t u;
1831
1832 if (decode_signed_num(&u, arg, TYPE_MINIMUM(gid_t),
1833 TYPE_MAXIMUM(gid_t), keyword))
1834 st->stat.st_gid = u;
1835}
1836
1837/* Convert VALUE, which is converted from a system integer type whose
1838 minimum value is MINVAL and maximum MINVAL, to an decimal
1839 integer string. Use the storage in BUF and return a pointer to the
1840 converted string. If VALUE is converted from a negative integer in
1841 the range MINVAL .. -1, represent it with a string representation
1842 of the negative integer, using leading '-'. */
1843#if !(INTMAX_MAX <= UINTMAX_MAX / 2)
1844# error "sysinttostr: uintmax_t cannot represent all intmax_t values"
1845#endif
1846
1851char *
1852sysinttostr(uintmax_t value, intmax_t minval, uintmax_t maxval,
1853 char buf[SYSINT_BUFSIZE])
1854{
1855 if (value <= maxval)
1856 return umaxtostr(value, buf);
1857 else
1858 {
1859 intmax_t i = value - minval;
1860 return imaxtostr(i + minval, buf);
1861 }
1862}
1863
1868static void
1869code_signed_num(uintmax_t value, char const *keyword,
1870 intmax_t minval, uintmax_t maxval, struct xheader *xhdr)
1871{
1872 char sbuf[SYSINT_BUFSIZE];
1873
1874 xheader_print(xhdr, keyword, sysinttostr(value, minval, maxval, sbuf));
1875}
1876
1881static void
1882gid_coder(struct tar_stat_info const *st, char const *keyword,
1883 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1884{
1885 code_signed_num(st->stat.st_gid, keyword,
1887}
1888
1893static void
1894ctime_decoder(struct tar_stat_info *st,
1895 char const *keyword,
1896 char const *arg,
1897 size_t size __attribute__((unused)))
1898{
1899 struct timespec ts;
1900
1901 if (decode_time(&ts, arg, keyword))
1902 st->ctime = ts;
1903}
1904
1909static void
1910ctime_coder(struct tar_stat_info const *st, char const *keyword,
1911 struct xheader *xhdr, void const *data __attribute__ ((unused)))
1912{
1913 code_time(st->ctime, keyword, xhdr);
1914}
1915
1920static void
1921dummy_coder(struct tar_stat_info const *st __attribute__ ((unused)),
1922 char const *keyword __attribute__ ((unused)),
1923 struct xheader *xhdr __attribute__ ((unused)),
1924 void const *data __attribute__ ((unused)))
1925{
1926}
1927
1932static void
1933dummy_decoder(struct tar_stat_info *st __attribute__ ((unused)),
1934 char const *keyword __attribute__ ((unused)),
1935 char const *arg __attribute__ ((unused)),
1936 size_t size __attribute__((unused)))
1937{
1938}
1939
1944char *
1945imaxtostr(intmax_t i, char *buf)
1946{
1947 char *p = buf + INT_STRLEN_BOUND(intmax_t);
1948
1949 *p = 0;
1950
1951 if (i < 0)
1952 {
1953 do
1954 *--p = '0' - i % 10;
1955 while ((i /= 10) != 0);
1956
1957 *--p = '-';
1958 }
1959 else
1960 {
1961 do
1962 *--p = '0' + i % 10;
1963 while ((i /= 10) != 0);
1964 }
1965
1966 return p;
1967}
1968/* Implementations */
1969
1974static void
1975out_of_range_header(char const *keyword, char const *value,
1976 intmax_t minval, uintmax_t maxval)
1977{
1978 char minval_buf[INT_BUFSIZE_BOUND(intmax_t)];
1979 char maxval_buf[UINTMAX_STRSIZE_BOUND];
1980 char *minval_string = imaxtostr(minval, minval_buf);
1981 char *maxval_string = umaxtostr(maxval, maxval_buf);
1982
1983 /* TRANSLATORS: The first %s is the pax extended header keyword
1984 (atime, gid, etc.). */
1985
1986#if 0
1987 ERROR((0, 0, _("Extended header %s=%s is out of range %s..%s"),
1988 keyword, value, minval_string, maxval_string));
1989#endif
1990
1991 pedro_dprintf(2, "Extended header %s=%s is out of range %s..%s",
1992 keyword, value, minval_string, maxval_string);
1993}
1994
1999bool
2000valid_timespec(struct timespec t)
2001{
2002 return 0 <= t.tv_nsec;
2003}
2004
2009static bool
2010decode_time(struct timespec *ts, char const *arg, char const *keyword)
2011{
2012 char *arg_lim;
2013 struct timespec t = decode_timespec(arg, &arg_lim, true);
2014
2015 if (!valid_timespec(t))
2016 {
2017 if (arg < arg_lim && !*arg_lim)
2018 out_of_range_header(keyword, arg, TYPE_MINIMUM(time_t),
2019 TYPE_MAXIMUM(time_t));
2020 else
2021 {
2022#if 0
2023 ERROR((0, 0, _("Malformed extended header: invalid %s=%s"),
2024 keyword, arg));
2025#endif
2026 pedro_dprintf(2, "Malformed extended header: invalid %s=%s", keyword, arg);
2027 }
2028 return false;
2029 }
2030
2031 *ts = t;
2032 return true;
2033}
2034
2039static void
2040atime_decoder(struct tar_stat_info *st,
2041 char const *keyword,
2042 char const *arg,
2043 size_t size __attribute__((unused)))
2044{
2045 struct timespec ts;
2046
2047 if (decode_time(&ts, arg, keyword))
2048 st->atime = ts;
2049}
2050
2051/* Output fraction and trailing digits appropriate for a nanoseconds
2052 count equal to NS, but don't output unnecessary '.' or trailing
2053 zeros. */
2054
2059void
2060code_ns_fraction(int ns, char *p)
2061{
2062 if (ns == 0)
2063 *p = '\0';
2064 else
2065 {
2066 int i = 9;
2067 *p++ = '.';
2068
2069 while (ns % 10 == 0)
2070 {
2071 ns /= 10;
2072 i--;
2073 }
2074
2075 p[i] = '\0';
2076
2077 for (;;)
2078 {
2079 p[--i] = '0' + ns % 10;
2080 if (i == 0)
2081 break;
2082 ns /= 10;
2083 }
2084 }
2085}
2086
2091char const *
2092code_timespec(struct timespec t, char sbuf[TIMESPEC_STRSIZE_BOUND])
2093{
2094 time_t s = t.tv_sec;
2095 int ns = t.tv_nsec;
2096 char *np;
2097 bool negative = s < 0;
2098
2099 /* ignore invalid values of ns */
2100 if (BILLION <= ns || ns < 0)
2101 ns = 0;
2102
2103 if (negative && ns != 0)
2104 {
2105 s++;
2106 ns = BILLION - ns;
2107 }
2108
2109 np = umaxtostr(negative ? -(uintmax_t)s : (uintmax_t)s, sbuf + 1);
2110 if (negative)
2111 *--np = '-';
2113 return np;
2114}
2115
2120struct timespec
2121decode_timespec(char const *arg, char **arg_lim, bool parse_fraction)
2122{
2123 time_t s = TYPE_MINIMUM(time_t);
2124 int ns = -1;
2125 char const *p = arg;
2126 bool negative = *arg == '-';
2127 struct timespec r;
2128
2129 if (!ISDIGIT(arg[negative]))
2130 errno = EINVAL;
2131 else
2132 {
2133 errno = 0;
2134
2135 if (negative)
2136 {
2137 intmax_t i = strtoimax(arg, arg_lim, 10);
2138 if (TYPE_SIGNED(time_t) ? TYPE_MINIMUM(time_t) <= i : 0 <= i)
2139 s = i;
2140 else
2141 errno = ERANGE;
2142 }
2143 else
2144 {
2145 uintmax_t i = strtoumax(arg, arg_lim, 10);
2146 if (i <= TYPE_MAXIMUM(time_t))
2147 s = i;
2148 else
2149 errno = ERANGE;
2150 }
2151
2152 p = *arg_lim;
2153 ns = 0;
2154
2155 if (parse_fraction && *p == '.')
2156 {
2157 int digits = 0;
2158 bool trailing_nonzero = false;
2159
2160 while (ISDIGIT(*++p))
2161 if (digits < LOG10_BILLION)
2162 digits++, ns = 10 * ns + (*p - '0');
2163 else
2164 trailing_nonzero |= *p != '0';
2165
2166 while (digits < LOG10_BILLION)
2167 digits++, ns *= 10;
2168
2169 if (negative)
2170 {
2171 /* Convert "-1.10000000000001" to s == -2, ns == 89999999.
2172 I.e., truncate time stamps towards minus infinity while
2173 converting them to internal form. */
2174 ns += trailing_nonzero;
2175 if (ns != 0)
2176 {
2177 if (s == TYPE_MINIMUM(time_t))
2178 ns = -1;
2179 else
2180 {
2181 s--;
2182 ns = BILLION - ns;
2183 }
2184 }
2185 }
2186 }
2187
2188 if (errno == ERANGE)
2189 ns = -1;
2190 }
2191
2192 *arg_lim = (char *)p;
2193 r.tv_sec = s;
2194 r.tv_nsec = ns;
2195 return r;
2196}
2197
2202static void
2203x_obstack_1grow(struct xheader *xhdr, char c)
2204{
2205 obstack_1grow(xhdr->stk, c);
2206 xhdr->size++;
2207}
2208
2213static void
2214x_obstack_grow(struct xheader *xhdr, const char *ptr, size_t length)
2215{
2216 obstack_grow(xhdr->stk, ptr, length);
2217 xhdr->size += length;
2218}
2219
2224static char *
2225xattr_encode_keyword(const char *keyword)
2226{
2227 static char *encode_buffer = NULL;
2228 static size_t encode_buffer_size = 0;
2229 size_t bp; /* keyword/buffer pointers */
2230
2231 if (!encode_buffer)
2232 {
2233 encode_buffer_size = 256;
2234 encode_buffer = malloc(encode_buffer_size);
2235 }
2236 else
2237 *encode_buffer = 0;
2238
2239 for (bp = 0; *keyword != 0; ++bp, ++keyword)
2240 {
2241 char c = *keyword;
2242
2243 if (bp + 2 /* enough for URL encoding also.. */ >= encode_buffer_size)
2244 {
2245 encode_buffer = x2realloc(encode_buffer, &encode_buffer_size);
2246 }
2247
2248 if (c == '%')
2249 {
2250 strcpy(encode_buffer + bp, "%25");
2251 bp += 2;
2252 }
2253 else if (c == '=')
2254 {
2255 strcpy(encode_buffer + bp, "%3D");
2256 bp += 2;
2257 }
2258 else
2259 encode_buffer[bp] = c;
2260 }
2261
2262 encode_buffer[bp] = 0;
2263
2264 return encode_buffer;
2265}
2266
2271static void
2272xheader_print_n(struct xheader *xhdr, char const *keyword,
2273 char const *value, size_t vsize)
2274{
2275 size_t p;
2276 size_t n = 0;
2277 char nbuf[UINTMAX_STRSIZE_BOUND];
2278 char const *np;
2279 size_t len, klen;
2280
2281 keyword = xattr_encode_keyword(keyword);
2282 klen = strlen(keyword);
2283 len = klen + vsize + 3; /* ' ' + '=' + '\n' */
2284
2285 do
2286 {
2287 p = n;
2288 np = umaxtostr(len + p, nbuf);
2289 n = nbuf + sizeof nbuf - 1 - np;
2290 }while (n != p);
2291
2292 x_obstack_grow(xhdr, np, n);
2293 x_obstack_1grow(xhdr, ' ');
2294 x_obstack_grow(xhdr, keyword, klen);
2295 x_obstack_1grow(xhdr, '=');
2296 x_obstack_grow(xhdr, value, vsize);
2297 x_obstack_1grow(xhdr, '\n');
2298}
2299
2304static void
2305xheader_print(struct xheader *xhdr, char const *keyword, char const *value)
2306{
2307 xheader_print_n(xhdr, keyword, value, strlen(value));
2308}
2309
2314static void
2315atime_coder(struct tar_stat_info const *st, char const *keyword,
2316 struct xheader *xhdr, void const *data __attribute__ ((unused)))
2317{
2318 code_time(st->atime, keyword, xhdr);
2319}
2320
2325static void
2326code_time(struct timespec t, char const *keyword, struct xheader *xhdr)
2327{
2329
2330 xheader_print(xhdr, keyword, code_timespec(t, buf));
2331}
2332
2334 { "atime", atime_coder, atime_decoder, 0, false },
2335 { "comment", dummy_coder, dummy_decoder, 0, false },
2336 { "charset", dummy_coder, dummy_decoder, 0, false },
2337 { "ctime", ctime_coder, ctime_decoder, 0, false },
2338 { "gid", gid_coder, gid_decoder, 0, false },
2339 { "gname", gname_coder, gname_decoder, 0, false },
2340 { "linkpath", linkpath_coder, linkpath_decoder, 0, false },
2341 { "mtime", mtime_coder, mtime_decoder, 0, false },
2342 { "path", path_coder, path_decoder, 0, false },
2343 { "size", size_coder, size_decoder, 0, false },
2344 { "uid", uid_coder, uid_decoder, 0, false },
2345 { "uname", uname_coder, uname_decoder, 0, false },
2346
2347 /* Sparse file handling */
2348 { "GNU.sparse.name", path_coder, sparse_path_decoder,
2349 XHDR_PROTECTED, false },
2350 { "GNU.sparse.major", sparse_major_coder, sparse_major_decoder,
2351 XHDR_PROTECTED, false },
2352 { "GNU.sparse.minor", sparse_minor_coder, sparse_minor_decoder,
2353 XHDR_PROTECTED, false },
2354 { "GNU.sparse.realsize", sparse_size_coder, sparse_size_decoder,
2355 XHDR_PROTECTED, false },
2356 { "GNU.sparse.numblocks", sparse_numblocks_coder, sparse_numblocks_decoder,
2357 XHDR_PROTECTED, false },
2358
2359 /* tar 1.14 - 1.15.90 keywords. */
2360 { "GNU.sparse.size", sparse_size_coder, sparse_size_decoder,
2361 XHDR_PROTECTED, false },
2362 /* tar 1.14 - 1.15.1 keywords. Multiple instances of these appeared in 'x'
2363 headers, and each of them was meaningful. It confilcted with POSIX specs,
2364 which requires that "when extended header records conflict, the last one
2365 given in the header shall take precedence." */
2366 { "GNU.sparse.offset", sparse_offset_coder, sparse_offset_decoder,
2367 XHDR_PROTECTED, false },
2368 { "GNU.sparse.numbytes", sparse_numbytes_coder, sparse_numbytes_decoder,
2369 XHDR_PROTECTED, false },
2370 /* tar 1.15.90 keyword, introduced to remove the above-mentioned conflict. */
2371 { "GNU.sparse.map", NULL /* Unused, see pax_dump_header() */,
2372 sparse_map_decoder, 0, false },
2373
2374 { "GNU.dumpdir", dumpdir_coder, dumpdir_decoder,
2375 XHDR_PROTECTED, false },
2376
2377 /* Keeps the tape/volume label. May be present only in the global headers.
2378 Equivalent to GNUTYPE_VOLHDR. */
2379 { "GNU.volume.label", volume_label_coder, volume_label_decoder,
2380 XHDR_PROTECTED | XHDR_GLOBAL, false },
2381
2382 /* These may be present in a first global header of the archive.
2383 They provide the same functionality as GNUTYPE_MULTIVOL header.
2384 The GNU.volume.size keeps the real_s_sizeleft value, which is
2385 otherwise kept in the size field of a multivolume header. The
2386 GNU.volume.offset keeps the offset of the start of this volume,
2387 otherwise kept in oldgnu_header.offset. */
2388 { "GNU.volume.filename", volume_label_coder, volume_filename_decoder,
2389 XHDR_PROTECTED | XHDR_GLOBAL, false },
2390 { "GNU.volume.size", volume_size_coder, volume_size_decoder,
2391 XHDR_PROTECTED | XHDR_GLOBAL, false },
2392 { "GNU.volume.offset", volume_offset_coder, volume_offset_decoder,
2393 XHDR_PROTECTED | XHDR_GLOBAL, false },
2394
2395 /* We get the SELinux value from filecon, so add a namespace for SELinux
2396 instead of storing it in SCHILY.xattr.* (which would be RAW). */
2397 { "RHT.security.selinux",
2398 xattr_selinux_coder, xattr_selinux_decoder, 0, false },
2399
2400 /* ACLs, use the star format... */
2401 { "SCHILY.acl.access",
2402 xattr_acls_a_coder, xattr_acls_a_decoder, 0, false },
2403
2404 { "SCHILY.acl.default",
2405 xattr_acls_d_coder, xattr_acls_d_decoder, 0, false },
2406
2407 /* We are storing all extended attributes using this rule even if some of them
2408 were stored by some previous rule (duplicates) -- we just have to make sure
2409 they are restored *only once* during extraction later on. */
2410 { "SCHILY.xattr", xattr_coder, xattr_decoder, 0, true },
2411
2412 { NULL, NULL, NULL, 0, false }
2413};
2414
2419void
2420xheader_read(struct xheader *xhdr, union block *p, off_t size)
2421{
2422 size_t j = 0;
2423
2424 if (size < 0)
2425 size = 0; /* Already diagnosed. */
2426
2427 if ((off_t)(SIZE_MAX - BLOCKSIZE) <= size)
2428 {
2429 //xalloc_die ();
2430
2431 assert(0 && "never will occur");
2432 }
2433 size += BLOCKSIZE;
2434
2435 xhdr->size = size;
2436 xhdr->buffer = xmalloc(size + 1);
2437 xhdr->buffer[size] = '\0';
2438
2439 do
2440 {
2441 size_t len = size;
2442
2443 if (len > BLOCKSIZE)
2444 len = BLOCKSIZE;
2445
2446 if (!p)
2447 {
2448 //FATAL_ERROR ((0, 0, _("Unexpected EOF in archive")));
2449
2450 pedro_dprintf(2, "Unexpected EOF in archive");
2451 }
2452 memcpy(&xhdr->buffer[j], p->buffer, len);
2454
2455 p = find_next_block();
2456
2457 j += len;
2458 size -= len;
2459 }while (size > 0);
2460}
2461
2462//by amanda & ricardo 2021
static const void * data
Definition XzCrc64.c:50
void pedro_dprintf(int amanda_level, char *format,...)
Definition pedro.c:43
#define assert(condition)
Definition lz4.c:273
const char * src
Definition lz4.h:866
char * dest
Definition lz4.h:806
#define __attribute__(unused_ric_since_2004)
Definition main_cr.c:84
#define input(b, o, c, n, m)
Definition compress42.c:610
#define output(b, o, c, n)
Definition compress42.c:592
#define __PTR_ALIGN(B, P, A)
Definition obstack.h:146
#define _OBSTACK_SIZE_T
Definition obstack.h:128
#define obstack_grow(h, where, length)
Definition obstack.h:464
#define obstack_1grow(h, datum)
Definition obstack.h:481
void * x2nrealloc(void *p, size_t *pn, size_t s)
Definition sparse_2_k.c:3330
int gid_t
Definition sparse_2_k.c:116
#define ISDIGIT(c)
Definition sparse_2_k.c:555
int uid_t
Definition sparse_2_k.c:117
char buf[N_BUF]
Definition spewG.c:36
Definition obstack.h:168
char * limit
Definition obstack.h:169
char contents[__FLEXIBLE_ARRAY_MEMBER]
Definition obstack.h:171
struct _obstack_chunk * prev
Definition obstack.h:170
Definition poolTests.c:28
Definition tar_1_33.c:3105
char * value
Definition tar_1_33.c:3108
struct keyword_list * next
Definition tar_1_33.c:3106
char * pattern
Definition tar_1_33.c:3107
Definition obstack.h:175
Definition tar.h:265
off_t numbytes
Definition tar.h:267
off_t offset
Definition tar.h:266
Definition tar.h:287
char * acls_a_ptr
Definition tar.h:300
struct sp_array * sparse_map
Definition tar.h:326
size_t acls_a_len
Definition tar.h:301
char * gname
Definition tar.h:296
struct xheader xhdr
Definition tar.h:340
unsigned sparse_minor
Definition tar.h:321
off_t real_size
Definition tar.h:328
size_t sparse_map_avail
Definition tar.h:322
size_t sparse_map_size
Definition tar.h:325
off_t archive_file_size
Definition tar.h:314
bool had_trailing_slash
Definition tar.h:291
char * acls_d_ptr
Definition tar.h:303
char * file_name
Definition tar.h:289
char * dumpdir
Definition tar.h:346
struct xattr_array * xattr_map
Definition tar.h:337
struct _stat stat
Definition tar.h:306
char * orig_file_name
Definition tar.h:288
struct timespec mtime
Definition tar.h:311
unsigned sparse_major
Definition tar.h:320
bool real_size_set
Definition tar.h:329
struct timespec ctime
Definition tar.h:312
bool sparse_name_done
Definition tar.h:332
char * uname
Definition tar.h:295
char * cntx_name
Definition tar.h:298
size_t xattr_map_size
Definition tar.h:336
struct timespec atime
Definition tar.h:310
char * link_name
Definition tar.h:293
size_t acls_d_len
Definition tar.h:304
Definition tar.h:280
char * xval_ptr
Definition tar.h:282
size_t xval_len
Definition tar.h:283
Definition tar_1_33.c:3051
bool prefix
Definition tar_1_33.c:3057
char const * keyword
Definition tar_1_33.c:3052
void(* decoder)(struct tar_stat_info *, char const *, char const *, size_t)
Definition tar_1_33.c:3055
Definition tar.h:271
struct obstack * stk
Definition tar.h:272
size_t size
Definition tar.h:273
char * buffer
Definition tar.h:274
#define BLOCKSIZE
Definition tar.h:248
#define INT_BUFSIZE_BOUND(t)
Definition tar_1_33.c:541
#define XHDR_PROTECTED
Definition tar_1_33.c:3043
#define WARNOPT(opt, args)
Definition tar_1_33.c:3280
#define TYPE_SIGNED(t)
Definition tar_1_33.c:3232
char * xstrdup(char const *string)
Safe strdup() that never returns NULL.
Definition tar_1_33.c:3038
void set_next_block_after(union block *block)
Definition tar_1_33.c:5178
#define WARN_UNKNOWN_KEYWORD
Definition tar_1_33.c:3262
void assign_string(char **string, const char *value)
Definition tar_1_33.c:5155
uintmax_t continued_file_size
Definition tar_1_33.c:3010
#define UINTMAX_STRSIZE_BOUND
Definition tar_1_33.c:543
char * continued_file_name
Definition tar_1_33.c:3009
uintmax_t continued_file_offset
Definition tar_1_33.c:3011
#define TYPE_MINIMUM(t)
Definition tar_1_33.c:3236
union block * find_next_block(void)
Definition tar_1_33.c:6798
#define INT_STRLEN_BOUND(t)
Definition tar_1_33.c:76
#define TYPE_MAXIMUM(t)
Definition tar_1_33.c:3237
void * xmemdup(void const *p, size_t s)
Definition tar_1_33.c:3024
char * volume_label
Definition tar_1_33.c:3008
#define XHDR_GLOBAL
Definition tar_1_33.c:3044
Definition tar.h:373
char buffer[BLOCKSIZE]
Definition tar.h:374
size_t size
Definition platform.h:559
int
Definition lzoconf.h:340
#define const
Definition zconf.h:230
#define d(i)
Definition sha256.c:44
#define c(i)
Definition sha256.c:43
#define h(i)
Definition sha256.c:48
#define e(i)
Definition sha256.c:45
void _obstack_free(struct obstack *h, void *obj)
Definition xheader_k.c:602
int fnmatch(const char *pattern, const char *string, int flags)
Definition fnmatch_arp.c:78
char * umaxtostr(uintmax_t i, char *buf)
Definition create_zen_ar.c:952
struct timespec decode_timespec(char const *arg, char **arg_lim, bool parse_fraction)
Definition xheader_k.c:2121
void _obstack_newchunk(struct obstack *h, _OBSTACK_SIZE_T length)
Definition xheader_k.c:653
#define iconv_t
Definition xheader_k.c:47
intmax_t represent_uintmax(uintmax_t n)
Definition xheader_k.c:1722
bool xheader_keyword_deleted_p(const char *kw)
Definition xheader_k.c:488
void xheader_decode(struct tar_stat_info *st)
Definition xheader_k.c:570
void * xrealloc(void *p, size_t n)
Safe realloc() that never returns NULL.
Definition xheader_k.c:712
#define iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft)
Definition xheader_k.c:41
@ BILLION
Definition xheader_k.c:31
@ LOG10_BILLION
Definition xheader_k.c:31
#define xmalloc
Definition xheader_k.c:5
intmax_t strtosysint(char const *arg, char **arglim, intmax_t minval, uintmax_t maxval)
Definition xheader_k.c:1756
char * stpcpy(char *dest, const char *src)
Definition xheader_k.c:753
char * sysinttostr(uintmax_t value, intmax_t minval, uintmax_t maxval, char buf[SYSINT_BUFSIZE])
Definition xheader_k.c:1852
bool valid_timespec(struct timespec t)
Definition xheader_k.c:2000
void xheader_read(struct xheader *xhdr, union block *p, off_t size)
Definition xheader_k.c:2420
@ TIMESPEC_STRSIZE_BOUND
Definition xheader_k.c:32
bool utf8_convert(bool to_utf, char const *input, char **output)
Definition xheader_k.c:1623
void * x2realloc(void *p, size_t *pn)
Definition xheader_k.c:20
char const * code_timespec(struct timespec t, char sbuf[TIMESPEC_STRSIZE_BOUND])
Definition xheader_k.c:2092
bool strip_trailing_slashes_unix(char *file)
Definition tar_1_33.c:5134
void xheader_xattr_add(struct tar_stat_info *st, const char *key, const char *val, size_t len)
Definition xheader_k.c:770
void code_ns_fraction(int ns, char *p)
Definition xheader_k.c:2060
char * imaxtostr(intmax_t i, char *buf)
Definition xheader_k.c:1945
#define xcalloc
Definition xheader_k.c:4
size_t dumpdir_size(const char *p)
Definition xheader_k.c:1053
#define iconv_open(tocode, fromcode)
Definition xheader_k.c:38
@ SYSINT_BUFSIZE
Definition xheader_k.c:29
uint32_t flags
Definition container.h:628
lzma_index ** i
Definition index.h:629
#define NULL
Definition getopt1.c:37
#define _(msgid)
Definition getopt.c:49
static uint32_t const uint8_t uint32_t len
Definition memcmplen.h:44
static uint32_t const uint8_t uint32_t uint32_t limit
Definition memcmplen.h:45
#define alloca(size)
Definition getopt.c:66
ret
Definition zlib_interface.c:30
#define EINVAL
Definition errno.h:13
#define ERROR(name)
Definition zstdseek_compress.c:27