From: Sergey Poznyakoff Date: Mon, 1 Sep 2003 17:03:33 +0000 (+0000) Subject: Implemented coder functions. X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=4189af3785829d436f77b396b0ce2954b5b32294;p=chaz%2Ftar Implemented coder functions. --- diff --git a/src/xheader.c b/src/xheader.c index 114a499..10ef354 100644 --- a/src/xheader.c +++ b/src/xheader.c @@ -25,37 +25,53 @@ #include "common.h" +#define obstack_chunk_alloc xmalloc +#define obstack_chunk_free free +#include + /* Forward declarations */ -static void dummy_handler (struct tar_stat_info *st, char *keyword, char *arg); +static void dummy_coder (struct tar_stat_info *st, char *keyword, + struct xheader *xhdr); +static void dummy_decoder (struct tar_stat_info *st, char *keyword, char *arg); -static void atime_coder (struct tar_stat_info *st, char *keyword, char *arg); +static void atime_coder (struct tar_stat_info *st, char *keyword, + struct xheader *xhdr); static void atime_decoder (struct tar_stat_info *st, char *keyword, char *arg); -static void gid_coder (struct tar_stat_info *st, char *keyword, char *arg); +static void gid_coder (struct tar_stat_info *st, char *keyword, + struct xheader *xhdr); static void gid_decoder (struct tar_stat_info *st, char *keyword, char *arg); -static void gname_coder (struct tar_stat_info *st, char *keyword, char *arg); +static void gname_coder (struct tar_stat_info *st, char *keyword, + struct xheader *xhdr); static void gname_decoder (struct tar_stat_info *st, char *keyword, char *arg); -static void linkpath_coder (struct tar_stat_info *st, char *keyword, char *arg); +static void linkpath_coder (struct tar_stat_info *st, char *keyword, + struct xheader *xhdr); static void linkpath_decoder (struct tar_stat_info *st, char *keyword, char *arg); -static void mtime_coder (struct tar_stat_info *st, char *keyword, char *arg); +static void mtime_coder (struct tar_stat_info *st, char *keyword, + struct xheader *xhdr); static void mtime_decoder (struct tar_stat_info *st, char *keyword, char *arg); -static void ctime_coder (struct tar_stat_info *st, char *keyword, char *arg); +static void ctime_coder (struct tar_stat_info *st, char *keyword, + struct xheader *xhdr); static void ctime_decoder (struct tar_stat_info *st, char *keyword, char *arg); -static void path_coder (struct tar_stat_info *st, char *keyword, char *arg); +static void path_coder (struct tar_stat_info *st, char *keyword, + struct xheader *xhdr); static void path_decoder (struct tar_stat_info *st, char *keyword, char *arg); -static void size_coder (struct tar_stat_info *st, char *keyword, char *arg); +static void size_coder (struct tar_stat_info *st, char *keyword, + struct xheader *xhdr); static void size_decoder (struct tar_stat_info *st, char *keyword, char *arg); -static void uid_coder (struct tar_stat_info *st, char *keyword, char *arg); +static void uid_coder (struct tar_stat_info *st, char *keyword, + struct xheader *xhdr); static void uid_decoder (struct tar_stat_info *st, char *keyword, char *arg); -static void uname_coder (struct tar_stat_info *st, char *keyword, char *arg); +static void uname_coder (struct tar_stat_info *st, char *keyword, + struct xheader *xhdr); static void uname_decoder (struct tar_stat_info *st, char *keyword, char *arg); /* General Interface */ @@ -63,14 +79,14 @@ static void uname_decoder (struct tar_stat_info *st, char *keyword, char *arg); struct xhdr_tab { char *keyword; - void (*coder) (struct tar_stat_info *st, char *keyword, char *arg); + void (*coder) (struct tar_stat_info *st, char *keyword, struct xheader *xhdr); void (*decoder) (struct tar_stat_info *st, char *keyword, char *arg); }; struct xhdr_tab xhdr_tab[] = { { "atime", atime_coder, atime_decoder }, - { "comment", dummy_handler, dummy_handler }, - { "charset", dummy_handler, dummy_handler }, + { "comment", dummy_coder, dummy_decoder }, + { "charset", dummy_coder, dummy_decoder }, { "ctime", ctime_coder, ctime_decoder }, { "gid", gid_coder, gid_decoder }, { "gname", gname_coder, gname_decoder }, @@ -148,42 +164,176 @@ xheader_decode (struct tar_stat_info *st) { char *p, *endp; - p = extended_header.blocks->buffer; - endp = &extended_header.blocks[extended_header.nblocks-1].buffer - [sizeof(extended_header.blocks[0])-1]; + p = extended_header.buffer; + endp = &extended_header.buffer[extended_header.size-1]; + while (p < endp) if (decode_record (&p, st)) break; } +void +xheader_store (char *keyword, struct tar_stat_info *st) +{ + struct xhdr_tab *t; + + if (extended_header.buffer) + return; + t = locate_handler (keyword); + if (!t) + return; + if (!extended_header.stk) + { + extended_header.stk = xmalloc (sizeof (*extended_header.stk)); + obstack_init (extended_header.stk); + } + t->coder (st, keyword, &extended_header); +} + void xheader_read (union block *p, size_t size) { - size_t i; + size_t i, j; + size_t nblocks; - free (extended_header.blocks); - extended_header.nblocks = (size + BLOCKSIZE - 1) / BLOCKSIZE; - extended_header.blocks = xmalloc (sizeof (extended_header.blocks[0]) * - extended_header.nblocks); + free (extended_header.buffer); + extended_header.size = size; + nblocks = (size + BLOCKSIZE - 1) / BLOCKSIZE; + extended_header.buffer = xmalloc (size + 1); + set_next_block_after (p); - for (i = 0; i < extended_header.nblocks; i++) + for (i = j = 0; i < nblocks; i++) { + size_t len; + p = find_next_block (); - memcpy (&extended_header.blocks[i], p, sizeof (p[0])); + len = size; + if (len > BLOCKSIZE) + len = BLOCKSIZE; + memcpy (&extended_header.buffer[j], p->buffer, len); set_next_block_after (p); + + j += len; + size -= len; } } +size_t +format_uintmax (uintmax_t val, char *buf, size_t s) +{ + if (!buf) + { + s = 0; + do + s++; + while ((val /= 10) != 0); + } + else + { + char *p = buf + s - 1; + + do + { + *p-- = val % 10 + '0'; + } + while ((val /= 10) != 0); + + while (p >= buf) + *p-- = '0'; + } + return s; +} + +void +xheader_print (struct xheader *xhdr, char *keyword, char *value) +{ + size_t len = strlen (keyword) + strlen (value) + 3; /* ' ' + '=' + '\n' */ + size_t p, n = 0; + char nbuf[100]; + + do + { + p = n; + n = format_uintmax (len + p, NULL, 0); + } + while (n != p); + + format_uintmax (len + n, nbuf, n); + obstack_grow (xhdr->stk, nbuf, n); + obstack_1grow (xhdr->stk, ' '); + obstack_grow (xhdr->stk, keyword, strlen (keyword)); + obstack_1grow (xhdr->stk, '='); + obstack_grow (xhdr->stk, value, strlen (value)); + obstack_1grow (xhdr->stk, '\n'); +} + +void +xheader_finish (struct xheader *xhdr) +{ + obstack_1grow (xhdr->stk, 0); + xhdr->buffer = obstack_finish (xhdr->stk); + xhdr->size = strlen (xhdr->buffer); +} + +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; +} + /* Implementations */ static void -dummy_handler (struct tar_stat_info *st, char *keyword, char *arg) +code_string (char *string, char *keyword, struct xheader *xhdr) +{ + xheader_print (xhdr, keyword, string); +} + +static void +code_time (time_t t, char *keyword, struct xheader *xhdr) +{ + char sbuf[100]; + size_t s = format_uintmax (t, NULL, 0); + format_uintmax (t, sbuf, s); + sbuf[s++] = '.'; + format_uintmax (0, sbuf + s, 9); + sbuf[s+9] = 0; + xheader_print (xhdr, keyword, sbuf); +} + +static void +code_num (uintmax_t value, char *keyword, struct xheader *xhdr) +{ + char sbuf[100]; + size_t s = format_uintmax (value, NULL, 0); + format_uintmax (value, sbuf, s); + sbuf[s] = 0; + xheader_print (xhdr, keyword, sbuf); +} + +static void +dummy_coder (struct tar_stat_info *st, char *keyword, struct xheader *xhdr) +{ +} + +static void +dummy_decoder (struct tar_stat_info *st, char *keyword, char *arg) { } static void -atime_coder (struct tar_stat_info *st, char *keyword, char *arg) +atime_coder (struct tar_stat_info *st, char *keyword, struct xheader *xhdr) { + code_time (st->stat.st_atime, keyword, xhdr); } static void @@ -193,8 +343,9 @@ atime_decoder (struct tar_stat_info *st, char *keyword, char *arg) } static void -gid_coder (struct tar_stat_info *st, char *keyword, char *arg) +gid_coder (struct tar_stat_info *st, char *keyword, struct xheader *xhdr) { + code_num (st->stat.st_gid, keyword, xhdr); } static void @@ -204,30 +355,33 @@ gid_decoder (struct tar_stat_info *st, char *keyword, char *arg) } static void -gname_coder (struct tar_stat_info *st, char *keyword, char *arg) +gname_coder (struct tar_stat_info *st, char *keyword, struct xheader *xhdr) { + code_string (st->gname, keyword, xhdr); } static void gname_decoder (struct tar_stat_info *st, char *keyword, char *arg) { - st->gname = strdup (arg); + assign_string (&st->gname, arg); } static void -linkpath_coder (struct tar_stat_info *st, char *keyword, char *arg) +linkpath_coder (struct tar_stat_info *st, char *keyword, struct xheader *xhdr) { + code_string (st->link_name, keyword, xhdr); } static void linkpath_decoder (struct tar_stat_info *st, char *keyword, char *arg) { - st->link_name = strdup (arg); + assign_string (&st->link_name, arg); } static void -ctime_coder (struct tar_stat_info *st, char *keyword, char *arg) +ctime_coder (struct tar_stat_info *st, char *keyword, struct xheader *xhdr) { + code_time (st->stat.st_ctime, keyword, xhdr); } static void @@ -237,8 +391,9 @@ ctime_decoder (struct tar_stat_info *st, char *keyword, char *arg) } static void -mtime_coder (struct tar_stat_info *st, char *keyword, char *arg) +mtime_coder (struct tar_stat_info *st, char *keyword, struct xheader *xhdr) { + code_time (st->stat.st_mtime, keyword, xhdr); } static void @@ -248,8 +403,9 @@ mtime_decoder (struct tar_stat_info *st, char *keyword, char *arg) } static void -path_coder (struct tar_stat_info *st, char *keyword, char *arg) +path_coder (struct tar_stat_info *st, char *keyword, struct xheader *xhdr) { + code_string (st->orig_file_name, keyword, xhdr); } static void @@ -261,8 +417,9 @@ path_decoder (struct tar_stat_info *st, char *keyword, char *arg) } static void -size_coder (struct tar_stat_info *st, char *keyword, char *arg) +size_coder (struct tar_stat_info *st, char *keyword, struct xheader *xhdr) { + code_num (st->stat.st_size, keyword, xhdr); } static void @@ -272,8 +429,9 @@ size_decoder (struct tar_stat_info *st, char *keyword, char *arg) } static void -uid_coder (struct tar_stat_info *st, char *keyword, char *arg) +uid_coder (struct tar_stat_info *st, char *keyword, struct xheader *xhdr) { + code_num (st->stat.st_uid, keyword, xhdr); } static void @@ -283,13 +441,14 @@ uid_decoder (struct tar_stat_info *st, char *keyword, char *arg) } static void -uname_coder (struct tar_stat_info *st, char *keyword, char *arg) +uname_coder (struct tar_stat_info *st, char *keyword, struct xheader *xhdr) { + code_string (st->uname, keyword, xhdr); } static void uname_decoder (struct tar_stat_info *st, char *keyword, char *arg) { - st->uname = strdup (arg); + assign_string (&st->uname, arg); }