From 82d12156ef5f948d44934ed44d79d24cc9e94366 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 11 Feb 2016 13:32:30 +0100 Subject: g13: Rename utils.c to g13tuple.c * g13/utils.c: Rename to g13tuple.c. * g13/utils.h: Rename to g13tuple.h. Change all users. * g13/Makefile.am: Adjust accordingly -- Signed-off-by: Werner Koch --- g13/Makefile.am | 10 +-- g13/backend.h | 2 +- g13/call-syshelp.c | 6 +- g13/create.c | 2 +- g13/g13tuple.c | 254 +++++++++++++++++++++++++++++++++++++++++++++++++++++ g13/g13tuple.h | 47 ++++++++++ g13/mount.c | 2 +- g13/mountinfo.c | 2 +- g13/sh-dmcrypt.c | 2 +- g13/t-g13tuple.c | 223 ++++++++++++++++++++++++++++++++++++++++++++++ g13/t-utils.c | 223 ---------------------------------------------- g13/utils.c | 254 ----------------------------------------------------- g13/utils.h | 47 ---------- 13 files changed, 539 insertions(+), 535 deletions(-) create mode 100644 g13/g13tuple.c create mode 100644 g13/g13tuple.h create mode 100644 g13/t-g13tuple.c delete mode 100644 g13/t-utils.c delete mode 100644 g13/utils.c delete mode 100644 g13/utils.h (limited to 'g13') diff --git a/g13/Makefile.am b/g13/Makefile.am index 5a2ffb252..34d5baf4b 100644 --- a/g13/Makefile.am +++ b/g13/Makefile.am @@ -36,7 +36,7 @@ g13_SOURCES = \ g13.c g13.h \ g13-common.c g13-common.h \ keyblob.h \ - utils.c utils.h \ + g13tuple.c g13tuple.h \ server.c server.h \ create.c create.h \ mount.c mount.h \ @@ -56,7 +56,7 @@ g13_syshelp_SOURCES = \ g13-syshelp.c g13-syshelp.h \ g13-common.c g13-common.h \ keyblob.h \ - utils.c utils.h \ + g13tuple.c g13tuple.h \ sh-cmd.c \ sh-blockdev.c \ sh-dmcrypt.c @@ -66,12 +66,12 @@ g13_syshelp_LDADD = $(libcommon) \ $(GPG_ERROR_LIBS) $(LIBINTL) $(LIBICONV) -module_tests = t-utils +module_tests = t-g13tuple t_common_ldadd = $(libcommon) $(LIBGCRYPT_LIBS) \ $(LIBASSUAN_LIBS) -t_utils_SOURCES = t-utils.c utils.c -t_utils_LDADD = $(t_common_ldadd) +t_g13tuple_SOURCES = t-g13tuple.c g13tuple.c +t_g13tuple_LDADD = $(t_common_ldadd) $(PROGRAMS) : $(libcommon) $(libcommonpth) diff --git a/g13/backend.h b/g13/backend.h index be81a04d5..0f391d0ab 100644 --- a/g13/backend.h +++ b/g13/backend.h @@ -21,7 +21,7 @@ #define G13_BACKEND_H #include "../common/membuf.h" -#include "utils.h" /* For tupledesc_t */ +#include "g13tuple.h" int be_parse_conttype_name (const char *name); int be_is_supported_conttype (int conttype); diff --git a/g13/call-syshelp.c b/g13/call-syshelp.c index 545bc1ab5..0e227ab16 100644 --- a/g13/call-syshelp.c +++ b/g13/call-syshelp.c @@ -29,7 +29,11 @@ #include "g13.h" #include #include "i18n.h" -#include "utils.h" +#include "g13tuple.h" +#include "keyblob.h" +#include "membuf.h" +#include "create.h" + /* Local data for this module. A pointer to this is stored in the CTRL object of each connection. */ diff --git a/g13/create.c b/g13/create.c index 3dac903df..fc2761877 100644 --- a/g13/create.c +++ b/g13/create.c @@ -32,7 +32,7 @@ #include "keyblob.h" #include "backend.h" -#include "utils.h" +#include "g13tuple.h" #include "../common/call-gpg.h" /* Create a new blob with all the session keys and other meta diff --git a/g13/g13tuple.c b/g13/g13tuple.c new file mode 100644 index 000000000..b90d81769 --- /dev/null +++ b/g13/g13tuple.c @@ -0,0 +1,254 @@ +/* g13tuple.c - Tuple handling + * Copyright (C) 2009 Free Software Foundation, Inc. + * Copyright (C) 2009, 2015, 2016 Werner Koch + * + * This file is part of GnuPG. + * + * GnuPG is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GnuPG is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include +#include +#include +#include +#include +#include + +#include "g13.h" +#include "g13tuple.h" + + +/* Definition of the tuple descriptor object. */ +struct tupledesc_s +{ + unsigned char *data; /* The tuple data. */ + size_t datalen; /* The length of the data. */ + size_t pos; /* The current position as used by next_tuple. */ + int refcount; /* Number of references hold. */ +}; + + + +/* Append the TAG and the VALUE to the MEMBUF. There is no error + checking here; this is instead done while getting the value back + from the membuf. */ +void +append_tuple (membuf_t *membuf, int tag, const void *value, size_t length) +{ + unsigned char buf[2]; + + assert (tag >= 0 && tag <= 0xffff); + assert (length <= 0xffff); + + buf[0] = tag >> 8; + buf[1] = tag; + put_membuf (membuf, buf, 2); + buf[0] = length >> 8; + buf[1] = length; + put_membuf (membuf, buf, 2); + if (length) + put_membuf (membuf, value, length); +} + + +/* Append the unsigned integer VALUE under TAG to MEMBUF. We make + * sure that the most significant bit is always cleared to explicitly + * flag the value as unsigned. */ +void +append_tuple_uint (membuf_t *membuf, int tag, unsigned long long value) +{ + unsigned char buf[16]; + unsigned char *p; + unsigned int len; + + p = buf + sizeof buf; + len = 0; + do + { + if (p == buf) + BUG () ; + *--p = (value & 0xff); + value >>= 8; + len++; + } + while (value); + + /* Prepend a zero byte if the first byte has its MSB set. */ + if ((*p & 0x80)) + { + if (p == buf) + BUG () ; + *--p = 0; + len++; + } + + append_tuple (membuf, tag, p, len); +} + + +/* Create a tuple object by moving the ownership of (DATA,DATALEN) to + * a new object. Returns 0 on success and stores the new object at + * R_TUPLEHD. The return object must be released using + * destroy_tuples(). */ +gpg_error_t +create_tupledesc (tupledesc_t *r_desc, void *data, size_t datalen) +{ + if (datalen < 5 || memcmp (data, "\x00\x00\x00\x01\x01", 5)) + return gpg_error (GPG_ERR_NOT_SUPPORTED); + + *r_desc = xtrymalloc (sizeof **r_desc); + if (!*r_desc) + return gpg_error_from_syserror (); + (*r_desc)->data = data; + (*r_desc)->datalen = datalen; + (*r_desc)->pos = 0; + (*r_desc)->refcount++; + return 0; +} + +/* Unref a tuple descriptor and if the refcount is down to 0 release + its allocated storage. */ +void +destroy_tupledesc (tupledesc_t tupledesc) +{ + if (!tupledesc) + return; + + if (!--tupledesc->refcount) + { + xfree (tupledesc->data); + xfree (tupledesc); + } +} + + +tupledesc_t +ref_tupledesc (tupledesc_t tupledesc) +{ + if (tupledesc) + tupledesc->refcount++; + return tupledesc; +} + + +/* Find the first tuple with tag TAG. On success return a pointer to + its value and store the length of the value at R_LENGTH. If no + tuple was found return NULL. For use by next_tuple, the last + position is stored in the descriptor. */ +const void * +find_tuple (tupledesc_t tupledesc, unsigned int tag, size_t *r_length) +{ + const unsigned char *s; + const unsigned char *s_end; /* Points right behind the data. */ + unsigned int t; + size_t n; + + s = tupledesc->data; + if (!s) + return NULL; + s_end = s + tupledesc->datalen; + while (s < s_end) + { + /* We use addresses for the overflow check to avoid undefined + behaviour. size_t should work with all flat memory models. */ + if ((size_t)s+3 >= (size_t)s_end || (size_t)s + 3 < (size_t)s) + break; + t = s[0] << 8; + t |= s[1]; + n = s[2] << 8; + n |= s[3]; + s += 4; + if ((size_t)s + n > (size_t)s_end || (size_t)s + n < (size_t)s) + break; + if (t == tag) + { + tupledesc->pos = (s + n) - tupledesc->data; + *r_length = n; + return s; + } + s += n; + } + return NULL; +} + + +/* Similar to find-tuple but expects an unsigned int value and stores + * that at R_VALUE. If the tag was not found GPG_ERR_NOT_FOUND is + * returned and 0 stored at R_VALUE. If the value cannot be converted + * to an unsigned integer GPG_ERR_ERANGE is returned. */ +gpg_error_t +find_tuple_uint (tupledesc_t tupledesc, unsigned int tag, + unsigned long long *r_value) +{ + const unsigned char *s; + size_t n; + unsigned long long value = 0; + + *r_value = 0; + + s = find_tuple (tupledesc, tag, &n); + if (!s) + return gpg_error (GPG_ERR_NOT_FOUND); + if (!n || (*s & 0x80)) /* No bytes or negative. */ + return gpg_error (GPG_ERR_ERANGE); + if (n && !*s) /* Skip a leading zero. */ + { + n--; + s++; + } + if (n > sizeof value) + return gpg_error (GPG_ERR_ERANGE); + for (; n; n--, s++) + { + value <<= 8; + value |= *s; + } + + *r_value = value; + return 0; +} + + +const void * +next_tuple (tupledesc_t tupledesc, unsigned int *r_tag, size_t *r_length) +{ + const unsigned char *s; + const unsigned char *s_end; /* Points right behind the data. */ + unsigned int t; + size_t n; + + s = tupledesc->data; + if (!s) + return NULL; + s_end = s + tupledesc->datalen; + s += tupledesc->pos; + if (s < s_end + && !((size_t)s + 3 >= (size_t)s_end || (size_t)s + 3 < (size_t)s)) + { + t = s[0] << 8; + t |= s[1]; + n = s[2] << 8; + n |= s[3]; + s += 4; + if (!((size_t)s + n > (size_t)s_end || (size_t)s + n < (size_t)s)) + { + tupledesc->pos = (s + n) - tupledesc->data; + *r_tag = t; + *r_length = n; + return s; + } + } + + return NULL; +} diff --git a/g13/g13tuple.h b/g13/g13tuple.h new file mode 100644 index 000000000..59cb6372d --- /dev/null +++ b/g13/g13tuple.h @@ -0,0 +1,47 @@ +/* g13tuple.h - Tuple handling + * Copyright (C) 2009 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GnuPG is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#ifndef G13_G13TUPLE_H +#define G13_G13TUPLE_H + +#include "../common/membuf.h" + +/* Append a new tuple to a memory buffer. */ +void append_tuple (membuf_t *membuf, + int tag, const void *value, size_t length); +void append_tuple_uint (membuf_t *membuf, int tag, + unsigned long long value); + +/* The tuple descriptor object. */ +struct tupledesc_s; +typedef struct tupledesc_s *tupledesc_t; + +gpg_error_t create_tupledesc (tupledesc_t *r_tupledesc, + void *data, size_t datalen); +void destroy_tupledesc (tupledesc_t tupledesc); +tupledesc_t ref_tupledesc (tupledesc_t tupledesc); +const void *find_tuple (tupledesc_t tupledesc, + unsigned int tag, size_t *r_length); +gpg_error_t find_tuple_uint (tupledesc_t tupledesc, unsigned int tag, + unsigned long long *r_value); +const void *next_tuple (tupledesc_t tupledesc, + unsigned int *r_tag, size_t *r_length); + + +#endif /*G13_G13TUPLE_H*/ diff --git a/g13/mount.c b/g13/mount.c index e9b9c1be7..a80f28d5e 100644 --- a/g13/mount.c +++ b/g13/mount.c @@ -32,7 +32,7 @@ #include "keyblob.h" #include "backend.h" -#include "utils.h" +#include "g13tuple.h" #include "../common/sysutils.h" #include "../common/call-gpg.h" #include "mountinfo.h" diff --git a/g13/mountinfo.c b/g13/mountinfo.c index 085fb86fe..1c4894dac 100644 --- a/g13/mountinfo.c +++ b/g13/mountinfo.c @@ -31,7 +31,7 @@ #include "mountinfo.h" #include "keyblob.h" -#include "utils.h" +#include "g13tuple.h" diff --git a/g13/sh-dmcrypt.c b/g13/sh-dmcrypt.c index a9aed0cc6..20eea2350 100644 --- a/g13/sh-dmcrypt.c +++ b/g13/sh-dmcrypt.c @@ -33,7 +33,7 @@ #include "g13-syshelp.h" #include #include "i18n.h" -#include "utils.h" +#include "g13tuple.h" #include "exectool.h" #include "keyblob.h" diff --git a/g13/t-g13tuple.c b/g13/t-g13tuple.c new file mode 100644 index 000000000..f986efab8 --- /dev/null +++ b/g13/t-g13tuple.c @@ -0,0 +1,223 @@ +/* t-g13tuple.c - Module test for g13tuple.c + * Copyright (C) 2016 Werner Koch + * + * This file is part of GnuPG. + * + * GnuPG is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GnuPG is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include +#include +#include +#include + + +#include "util.h" +#include "keyblob.h" +#include "g13tuple.h" + +#define PGM "t-g13tuple" + +static int verbose; +static int debug; +static int errcount; + +/* Test for the functions append_tuple_uint and find_tuple_unit. */ +static void +test_tuple_uint (void) +{ + static struct { + int tag; + int len; + char *data; + unsigned long long val; + gpg_err_code_t ec; + } tv[] = { + { 1, 0, "", 0, GPG_ERR_ERANGE }, + { 2, 1, "\x00", 0, 0}, + { 3, 1, "\x7f", 127ull, 0}, + { 4, 1, "\x80", 0, GPG_ERR_ERANGE }, + { 5, 1, "\x81", 0, GPG_ERR_ERANGE }, + { 6, 2, "\x80\x01", 0, GPG_ERR_ERANGE }, + { 7, 2, "\x00\x80", 128ull, 0 }, + { 8, 1, "\x01", 1, 0 }, + { 9, 1, "\x40", 64, 0 }, + { 10, 2, "\x40\x00", 16384, 0 }, + { 11, 8, "\x7f\xff\xff\xff\xff\xff\xff\xff", 0x7fffffffffffffffull, 0 }, + { 12, 9, "\x00\xff\xff\xff\xff\xff\xff\xff\xff", 0xffffffffffffffffull, 0}, + { 13, 9, "\x01\xff\xff\xff\xff\xff\xff\xff\xff", 0, GPG_ERR_ERANGE } + }; + int tidx; + gpg_error_t err; + membuf_t mb, mb2; + void *p; + const void *s; + size_t n; + tupledesc_t tuples; + tupledesc_t tuples2; + unsigned long long value; + int i; + + init_membuf (&mb, 512); + init_membuf (&mb2, 512); + append_tuple (&mb, KEYBLOB_TAG_BLOBVERSION, "\x01", 1); + append_tuple (&mb2, KEYBLOB_TAG_BLOBVERSION, "\x01", 1); + for (tidx=0; tidx < DIM (tv); tidx++) + { + append_tuple (&mb, tv[tidx].tag, tv[tidx].data, tv[tidx].len); + if (!tv[tidx].ec) + append_tuple_uint (&mb2, tv[tidx].tag, tv[tidx].val); + } + + p = get_membuf (&mb, &n); + if (!p) + { + err = gpg_error_from_syserror (); + fprintf (stderr, PGM ":%s: get_membuf failed: %s\n", + __func__, gpg_strerror (err)); + exit (1); + } + err = create_tupledesc (&tuples, p, n); + if (err) + { + fprintf (stderr, PGM ":%s: create_tupledesc failed: %s\n", + __func__, gpg_strerror (err)); + exit (1); + } + p = get_membuf (&mb2, &n); + if (!p) + { + err = gpg_error_from_syserror (); + fprintf (stderr, PGM ":%s: get_membuf failed: %s\n", + __func__, gpg_strerror (err)); + exit (1); + } + err = create_tupledesc (&tuples2, p, n); + if (err) + { + fprintf (stderr, PGM ":%s: create_tupledesc failed: %s\n", + __func__, gpg_strerror (err)); + exit (1); + } + + for (tidx=0; tidx < DIM (tv); tidx++) + { + err = find_tuple_uint (tuples, tv[tidx].tag, &value); + if (tv[tidx].ec != gpg_err_code (err)) + { + fprintf (stderr, PGM ":%s:tidx=%d: wrong error returned; " + "expected(%s) got(%s)\n", + __func__, tidx, + gpg_strerror (tv[tidx].ec), gpg_strerror (err)); + errcount++; + } + else if (!err && tv[tidx].val != value) + { + fprintf (stderr, PGM ":%s:tidx=%d: wrong value returned; " + "expected(%llx) got(%llx)\n", + __func__, tidx, tv[tidx].val, value); + errcount++; + } + + err = find_tuple_uint (tuples2, tv[tidx].tag, &value); + if (gpg_err_code (err) == GPG_ERR_NOT_FOUND) + { + if (!tv[tidx].ec) + { + fprintf (stderr, PGM ":%s:tidx=%d: find_tuple failed: %s\n", + __func__, tidx, gpg_strerror (err)); + errcount++; + } + } + else if (tv[tidx].ec != gpg_err_code (err)) + { + fprintf (stderr, PGM ":%s:tidx=%d: wrong error returned (2); " + "expected(%s) got(%s)\n", + __func__, tidx, + gpg_strerror (tv[tidx].ec), gpg_strerror (err)); + errcount++; + } + else if (!err && tv[tidx].val != value) + { + fprintf (stderr, PGM ":%s:tidx=%d: wrong value returned (2); " + "expected(%llx) got(%llx)\n", + __func__, tidx, tv[tidx].val, value); + errcount++; + } + + s = find_tuple (tuples2, tv[tidx].tag, &n); + if (!s) + ; + else if (tv[tidx].len != n) + { + fprintf (stderr, PGM ":%s:tidx=%d: wrong string length returned; " + "expected(%d) got(%zu)\n", + __func__, tidx, tv[tidx].len, n); + errcount++; + } + else if (memcmp (tv[tidx].data, s, n)) + { + fprintf (stderr, PGM ":%s:tidx=%d: wrong string returned:", + __func__, tidx); + for (i=0; i < n; i++) + fprintf (stderr, " %02x", ((unsigned char*)s)[i]); + fputc ('\n', stderr); + errcount++; + } + } + + destroy_tupledesc (tuples); + destroy_tupledesc (tuples2); +} + + + +int +main (int argc, char **argv) +{ + int last_argc = -1; + + gpgrt_init (); + if (argc) + { argc--; argv++; } + while (argc && last_argc != argc ) + { + last_argc = argc; + if (!strcmp (*argv, "--")) + { + argc--; argv++; + break; + } + else if (!strcmp (*argv, "--verbose")) + { + verbose++; + argc--; argv++; + } + else if (!strcmp (*argv, "--debug")) + { + verbose += 2; + debug++; + argc--; argv++; + } + else if (!strncmp (*argv, "--", 2)) + { + fprintf (stderr, PGM ": unknown option '%s'\n", *argv); + exit (1); + } + } + + test_tuple_uint (); + + return !!errcount; +} diff --git a/g13/t-utils.c b/g13/t-utils.c deleted file mode 100644 index 5605216ec..000000000 --- a/g13/t-utils.c +++ /dev/null @@ -1,223 +0,0 @@ -/* t-utils.c - Module test for utils.c - * Copyright (C) 2016 Werner Koch - * - * This file is part of GnuPG. - * - * GnuPG is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * GnuPG is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, see . - */ - -#include -#include -#include -#include - - -#include "util.h" -#include "keyblob.h" -#include "utils.h" - -#define PGM "t-utils" - -static int verbose; -static int debug; -static int errcount; - -/* Test for the functions append_tuple_uint and find_tuple_unit. */ -static void -test_tuple_uint (void) -{ - static struct { - int tag; - int len; - char *data; - unsigned long long val; - gpg_err_code_t ec; - } tv[] = { - { 1, 0, "", 0, GPG_ERR_ERANGE }, - { 2, 1, "\x00", 0, 0}, - { 3, 1, "\x7f", 127ull, 0}, - { 4, 1, "\x80", 0, GPG_ERR_ERANGE }, - { 5, 1, "\x81", 0, GPG_ERR_ERANGE }, - { 6, 2, "\x80\x01", 0, GPG_ERR_ERANGE }, - { 7, 2, "\x00\x80", 128ull, 0 }, - { 8, 1, "\x01", 1, 0 }, - { 9, 1, "\x40", 64, 0 }, - { 10, 2, "\x40\x00", 16384, 0 }, - { 11, 8, "\x7f\xff\xff\xff\xff\xff\xff\xff", 0x7fffffffffffffffull, 0 }, - { 12, 9, "\x00\xff\xff\xff\xff\xff\xff\xff\xff", 0xffffffffffffffffull, 0}, - { 13, 9, "\x01\xff\xff\xff\xff\xff\xff\xff\xff", 0, GPG_ERR_ERANGE } - }; - int tidx; - gpg_error_t err; - membuf_t mb, mb2; - void *p; - const void *s; - size_t n; - tupledesc_t tuples; - tupledesc_t tuples2; - unsigned long long value; - int i; - - init_membuf (&mb, 512); - init_membuf (&mb2, 512); - append_tuple (&mb, KEYBLOB_TAG_BLOBVERSION, "\x01", 1); - append_tuple (&mb2, KEYBLOB_TAG_BLOBVERSION, "\x01", 1); - for (tidx=0; tidx < DIM (tv); tidx++) - { - append_tuple (&mb, tv[tidx].tag, tv[tidx].data, tv[tidx].len); - if (!tv[tidx].ec) - append_tuple_uint (&mb2, tv[tidx].tag, tv[tidx].val); - } - - p = get_membuf (&mb, &n); - if (!p) - { - err = gpg_error_from_syserror (); - fprintf (stderr, PGM ":%s: get_membuf failed: %s\n", - __func__, gpg_strerror (err)); - exit (1); - } - err = create_tupledesc (&tuples, p, n); - if (err) - { - fprintf (stderr, PGM ":%s: create_tupledesc failed: %s\n", - __func__, gpg_strerror (err)); - exit (1); - } - p = get_membuf (&mb2, &n); - if (!p) - { - err = gpg_error_from_syserror (); - fprintf (stderr, PGM ":%s: get_membuf failed: %s\n", - __func__, gpg_strerror (err)); - exit (1); - } - err = create_tupledesc (&tuples2, p, n); - if (err) - { - fprintf (stderr, PGM ":%s: create_tupledesc failed: %s\n", - __func__, gpg_strerror (err)); - exit (1); - } - - for (tidx=0; tidx < DIM (tv); tidx++) - { - err = find_tuple_uint (tuples, tv[tidx].tag, &value); - if (tv[tidx].ec != gpg_err_code (err)) - { - fprintf (stderr, PGM ":%s:tidx=%d: wrong error returned; " - "expected(%s) got(%s)\n", - __func__, tidx, - gpg_strerror (tv[tidx].ec), gpg_strerror (err)); - errcount++; - } - else if (!err && tv[tidx].val != value) - { - fprintf (stderr, PGM ":%s:tidx=%d: wrong value returned; " - "expected(%llx) got(%llx)\n", - __func__, tidx, tv[tidx].val, value); - errcount++; - } - - err = find_tuple_uint (tuples2, tv[tidx].tag, &value); - if (gpg_err_code (err) == GPG_ERR_NOT_FOUND) - { - if (!tv[tidx].ec) - { - fprintf (stderr, PGM ":%s:tidx=%d: find_tuple failed: %s\n", - __func__, tidx, gpg_strerror (err)); - errcount++; - } - } - else if (tv[tidx].ec != gpg_err_code (err)) - { - fprintf (stderr, PGM ":%s:tidx=%d: wrong error returned (2); " - "expected(%s) got(%s)\n", - __func__, tidx, - gpg_strerror (tv[tidx].ec), gpg_strerror (err)); - errcount++; - } - else if (!err && tv[tidx].val != value) - { - fprintf (stderr, PGM ":%s:tidx=%d: wrong value returned (2); " - "expected(%llx) got(%llx)\n", - __func__, tidx, tv[tidx].val, value); - errcount++; - } - - s = find_tuple (tuples2, tv[tidx].tag, &n); - if (!s) - ; - else if (tv[tidx].len != n) - { - fprintf (stderr, PGM ":%s:tidx=%d: wrong string length returned; " - "expected(%d) got(%zu)\n", - __func__, tidx, tv[tidx].len, n); - errcount++; - } - else if (memcmp (tv[tidx].data, s, n)) - { - fprintf (stderr, PGM ":%s:tidx=%d: wrong string returned:", - __func__, tidx); - for (i=0; i < n; i++) - fprintf (stderr, " %02x", ((unsigned char*)s)[i]); - fputc ('\n', stderr); - errcount++; - } - } - - destroy_tupledesc (tuples); - destroy_tupledesc (tuples2); -} - - - -int -main (int argc, char **argv) -{ - int last_argc = -1; - - gpgrt_init (); - if (argc) - { argc--; argv++; } - while (argc && last_argc != argc ) - { - last_argc = argc; - if (!strcmp (*argv, "--")) - { - argc--; argv++; - break; - } - else if (!strcmp (*argv, "--verbose")) - { - verbose++; - argc--; argv++; - } - else if (!strcmp (*argv, "--debug")) - { - verbose += 2; - debug++; - argc--; argv++; - } - else if (!strncmp (*argv, "--", 2)) - { - fprintf (stderr, PGM ": unknown option '%s'\n", *argv); - exit (1); - } - } - - test_tuple_uint (); - - return !!errcount; -} diff --git a/g13/utils.c b/g13/utils.c deleted file mode 100644 index dbbaa0d8b..000000000 --- a/g13/utils.c +++ /dev/null @@ -1,254 +0,0 @@ -/* utils.c - Utility functions - * Copyright (C) 2009 Free Software Foundation, Inc. - * Copyright (C) 2009, 2015, 2016 Werner Koch - * - * This file is part of GnuPG. - * - * GnuPG is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * GnuPG is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, see . - */ - -#include -#include -#include -#include -#include -#include - -#include "g13.h" -#include "utils.h" - - -/* Definition of the tuple descriptor object. */ -struct tupledesc_s -{ - unsigned char *data; /* The tuple data. */ - size_t datalen; /* The length of the data. */ - size_t pos; /* The current position as used by next_tuple. */ - int refcount; /* Number of references hold. */ -}; - - - -/* Append the TAG and the VALUE to the MEMBUF. There is no error - checking here; this is instead done while getting the value back - from the membuf. */ -void -append_tuple (membuf_t *membuf, int tag, const void *value, size_t length) -{ - unsigned char buf[2]; - - assert (tag >= 0 && tag <= 0xffff); - assert (length <= 0xffff); - - buf[0] = tag >> 8; - buf[1] = tag; - put_membuf (membuf, buf, 2); - buf[0] = length >> 8; - buf[1] = length; - put_membuf (membuf, buf, 2); - if (length) - put_membuf (membuf, value, length); -} - - -/* Append the unsigned integer VALUE under TAG to MEMBUF. We make - * sure that the most significant bit is always cleared to explicitly - * flag the value as unsigned. */ -void -append_tuple_uint (membuf_t *membuf, int tag, unsigned long long value) -{ - unsigned char buf[16]; - unsigned char *p; - unsigned int len; - - p = buf + sizeof buf; - len = 0; - do - { - if (p == buf) - BUG () ; - *--p = (value & 0xff); - value >>= 8; - len++; - } - while (value); - - /* Prepend a zero byte if the first byte has its MSB set. */ - if ((*p & 0x80)) - { - if (p == buf) - BUG () ; - *--p = 0; - len++; - } - - append_tuple (membuf, tag, p, len); -} - - -/* Create a tuple object by moving the ownership of (DATA,DATALEN) to - * a new object. Returns 0 on success and stores the new object at - * R_TUPLEHD. The return object must be released using - * destroy_tuples(). */ -gpg_error_t -create_tupledesc (tupledesc_t *r_desc, void *data, size_t datalen) -{ - if (datalen < 5 || memcmp (data, "\x00\x00\x00\x01\x01", 5)) - return gpg_error (GPG_ERR_NOT_SUPPORTED); - - *r_desc = xtrymalloc (sizeof **r_desc); - if (!*r_desc) - return gpg_error_from_syserror (); - (*r_desc)->data = data; - (*r_desc)->datalen = datalen; - (*r_desc)->pos = 0; - (*r_desc)->refcount++; - return 0; -} - -/* Unref a tuple descriptor and if the refcount is down to 0 release - its allocated storage. */ -void -destroy_tupledesc (tupledesc_t tupledesc) -{ - if (!tupledesc) - return; - - if (!--tupledesc->refcount) - { - xfree (tupledesc->data); - xfree (tupledesc); - } -} - - -tupledesc_t -ref_tupledesc (tupledesc_t tupledesc) -{ - if (tupledesc) - tupledesc->refcount++; - return tupledesc; -} - - -/* Find the first tuple with tag TAG. On success return a pointer to - its value and store the length of the value at R_LENGTH. If no - tuple was found return NULL. For use by next_tuple, the last - position is stored in the descriptor. */ -const void * -find_tuple (tupledesc_t tupledesc, unsigned int tag, size_t *r_length) -{ - const unsigned char *s; - const unsigned char *s_end; /* Points right behind the data. */ - unsigned int t; - size_t n; - - s = tupledesc->data; - if (!s) - return NULL; - s_end = s + tupledesc->datalen; - while (s < s_end) - { - /* We use addresses for the overflow check to avoid undefined - behaviour. size_t should work with all flat memory models. */ - if ((size_t)s+3 >= (size_t)s_end || (size_t)s + 3 < (size_t)s) - break; - t = s[0] << 8; - t |= s[1]; - n = s[2] << 8; - n |= s[3]; - s += 4; - if ((size_t)s + n > (size_t)s_end || (size_t)s + n < (size_t)s) - break; - if (t == tag) - { - tupledesc->pos = (s + n) - tupledesc->data; - *r_length = n; - return s; - } - s += n; - } - return NULL; -} - - -/* Similar to find-tuple but expects an unsigned int value and stores - * that at R_VALUE. If the tag was not found GPG_ERR_NOT_FOUND is - * returned and 0 stored at R_VALUE. If the value cannot be converted - * to an unsigned integer GPG_ERR_ERANGE is returned. */ -gpg_error_t -find_tuple_uint (tupledesc_t tupledesc, unsigned int tag, - unsigned long long *r_value) -{ - const unsigned char *s; - size_t n; - unsigned long long value = 0; - - *r_value = 0; - - s = find_tuple (tupledesc, tag, &n); - if (!s) - return gpg_error (GPG_ERR_NOT_FOUND); - if (!n || (*s & 0x80)) /* No bytes or negative. */ - return gpg_error (GPG_ERR_ERANGE); - if (n && !*s) /* Skip a leading zero. */ - { - n--; - s++; - } - if (n > sizeof value) - return gpg_error (GPG_ERR_ERANGE); - for (; n; n--, s++) - { - value <<= 8; - value |= *s; - } - - *r_value = value; - return 0; -} - - -const void * -next_tuple (tupledesc_t tupledesc, unsigned int *r_tag, size_t *r_length) -{ - const unsigned char *s; - const unsigned char *s_end; /* Points right behind the data. */ - unsigned int t; - size_t n; - - s = tupledesc->data; - if (!s) - return NULL; - s_end = s + tupledesc->datalen; - s += tupledesc->pos; - if (s < s_end - && !((size_t)s + 3 >= (size_t)s_end || (size_t)s + 3 < (size_t)s)) - { - t = s[0] << 8; - t |= s[1]; - n = s[2] << 8; - n |= s[3]; - s += 4; - if (!((size_t)s + n > (size_t)s_end || (size_t)s + n < (size_t)s)) - { - tupledesc->pos = (s + n) - tupledesc->data; - *r_tag = t; - *r_length = n; - return s; - } - } - - return NULL; -} diff --git a/g13/utils.h b/g13/utils.h deleted file mode 100644 index 6c3902b90..000000000 --- a/g13/utils.h +++ /dev/null @@ -1,47 +0,0 @@ -/* utils.h - Defs for utility fucthe dispatcher to the various backends.ntions - * Copyright (C) 2009 Free Software Foundation, Inc. - * - * This file is part of GnuPG. - * - * GnuPG is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * GnuPG is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, see . - */ - -#ifndef G13_UTILS_H -#define G13_UTILS_H - -#include "../common/membuf.h" - -/* Append a new tuple to a memory buffer. */ -void append_tuple (membuf_t *membuf, - int tag, const void *value, size_t length); -void append_tuple_uint (membuf_t *membuf, int tag, - unsigned long long value); - -/* The tuple descriptor object. */ -struct tupledesc_s; -typedef struct tupledesc_s *tupledesc_t; - -gpg_error_t create_tupledesc (tupledesc_t *r_tupledesc, - void *data, size_t datalen); -void destroy_tupledesc (tupledesc_t tupledesc); -tupledesc_t ref_tupledesc (tupledesc_t tupledesc); -const void *find_tuple (tupledesc_t tupledesc, - unsigned int tag, size_t *r_length); -gpg_error_t find_tuple_uint (tupledesc_t tupledesc, unsigned int tag, - unsigned long long *r_value); -const void *next_tuple (tupledesc_t tupledesc, - unsigned int *r_tag, size_t *r_length); - - -#endif /*G13_UTILS_H*/ -- cgit v1.2.3