mirror of
git://git.gnupg.org/gnupg.git
synced 2025-04-13 22:21:09 +02:00
common: Improve t-zb32 to be used for manual encoding.
* common/t-support.h (no_exit_on_fail, errcount): New. (fail): Bump errcount. * common/t-zb32.c (main): Add options to allow manual use. Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
parent
5aadb4b62d
commit
d89a9fca46
@ -192,7 +192,10 @@ t_session_env_LDADD = $(t_common_ldadd)
|
|||||||
t_openpgp_oid_LDADD = $(t_common_ldadd)
|
t_openpgp_oid_LDADD = $(t_common_ldadd)
|
||||||
t_ssh_utils_LDADD = $(t_common_ldadd)
|
t_ssh_utils_LDADD = $(t_common_ldadd)
|
||||||
t_mapstrings_LDADD = $(t_common_ldadd)
|
t_mapstrings_LDADD = $(t_common_ldadd)
|
||||||
|
|
||||||
|
t_zb32_SOURCES = t-zb32.c $(t_extra_src)
|
||||||
t_zb32_LDADD = $(t_common_ldadd)
|
t_zb32_LDADD = $(t_common_ldadd)
|
||||||
|
|
||||||
t_mbox_util_LDADD = $(t_common_ldadd)
|
t_mbox_util_LDADD = $(t_common_ldadd)
|
||||||
t_iobuf_LDADD = $(t_common_ldadd)
|
t_iobuf_LDADD = $(t_common_ldadd)
|
||||||
|
|
||||||
|
@ -69,8 +69,15 @@ void gcry_free (void *a);
|
|||||||
#define pass() do { ; } while(0)
|
#define pass() do { ; } while(0)
|
||||||
#define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
|
#define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
|
||||||
__FILE__,__LINE__, (a)); \
|
__FILE__,__LINE__, (a)); \
|
||||||
exit (1); \
|
errcount++; \
|
||||||
|
if (!no_exit_on_fail) \
|
||||||
|
exit (1); \
|
||||||
} while(0)
|
} while(0)
|
||||||
|
|
||||||
|
/* If this flag is set the fail macro does not call exit. */
|
||||||
|
static int no_exit_on_fail;
|
||||||
|
/* Error counter. */
|
||||||
|
static int errcount;
|
||||||
|
|
||||||
|
|
||||||
#endif /*GNUPG_COMMON_T_SUPPORT_H*/
|
#endif /*GNUPG_COMMON_T_SUPPORT_H*/
|
||||||
|
214
common/t-zb32.c
214
common/t-zb32.c
@ -30,17 +30,21 @@
|
|||||||
#include <config.h>
|
#include <config.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#ifdef HAVE_DOSISH_SYSTEM
|
||||||
|
# include <fcntl.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "util.h"
|
#include "zb32.h"
|
||||||
|
#include "t-support.h"
|
||||||
|
|
||||||
#define pass() do { ; } while(0)
|
#define PGM "t-zb32"
|
||||||
#define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
|
|
||||||
__FILE__,__LINE__, (a)); \
|
|
||||||
errcount++; \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
|
static int verbose;
|
||||||
|
static int debug;
|
||||||
static int errcount;
|
static int errcount;
|
||||||
|
|
||||||
|
|
||||||
@ -89,8 +93,8 @@ test_zb32enc (void)
|
|||||||
output = zb32_encode (tests[tidx].data, tests[tidx].datalen);
|
output = zb32_encode (tests[tidx].data, tests[tidx].datalen);
|
||||||
if (!output)
|
if (!output)
|
||||||
{
|
{
|
||||||
fprintf (stderr, "%s:%d: error encoding test %d: %s\n",
|
fprintf (stderr, PGM": error encoding test %d: %s\n",
|
||||||
__FILE__, __LINE__, tidx, strerror (errno));
|
tidx, strerror (errno));
|
||||||
exit (1);
|
exit (1);
|
||||||
}
|
}
|
||||||
/* puts (output); */
|
/* puts (output); */
|
||||||
@ -101,13 +105,201 @@ test_zb32enc (void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Read the file FNAME or stdin if FNAME is NULL and return a malloced
|
||||||
|
buffer with the content. R_LENGTH received the length of the file.
|
||||||
|
Print a diagnostic and returns NULL on error. */
|
||||||
|
static char *
|
||||||
|
read_file (const char *fname, size_t *r_length)
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
char *buf;
|
||||||
|
size_t buflen;
|
||||||
|
|
||||||
|
if (!fname)
|
||||||
|
{
|
||||||
|
size_t nread, bufsize = 0;
|
||||||
|
|
||||||
|
fp = stdin;
|
||||||
|
#ifdef HAVE_DOSISH_SYSTEM
|
||||||
|
setmode (fileno(fp) , O_BINARY );
|
||||||
|
#endif
|
||||||
|
buf = NULL;
|
||||||
|
buflen = 0;
|
||||||
|
#define NCHUNK 8192
|
||||||
|
do
|
||||||
|
{
|
||||||
|
bufsize += NCHUNK;
|
||||||
|
if (!buf)
|
||||||
|
buf = xmalloc (bufsize);
|
||||||
|
else
|
||||||
|
buf = xrealloc (buf, bufsize);
|
||||||
|
|
||||||
|
nread = fread (buf+buflen, 1, NCHUNK, fp);
|
||||||
|
if (nread < NCHUNK && ferror (fp))
|
||||||
|
{
|
||||||
|
fprintf (stderr, PGM": error reading '[stdin]': %s\n",
|
||||||
|
strerror (errno));
|
||||||
|
xfree (buf);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
buflen += nread;
|
||||||
|
}
|
||||||
|
while (nread == NCHUNK);
|
||||||
|
#undef NCHUNK
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
|
fp = fopen (fname, "rb");
|
||||||
|
if (!fp)
|
||||||
|
{
|
||||||
|
fprintf (stderr, PGM": can't open '%s': %s\n",
|
||||||
|
fname, strerror (errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fstat (fileno(fp), &st))
|
||||||
|
{
|
||||||
|
fprintf (stderr, PGM": can't stat '%s': %s\n",
|
||||||
|
fname, strerror (errno));
|
||||||
|
fclose (fp);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
buflen = st.st_size;
|
||||||
|
buf = xmalloc (buflen+1);
|
||||||
|
if (fread (buf, buflen, 1, fp) != 1)
|
||||||
|
{
|
||||||
|
fprintf (stderr, PGM": error reading '%s': %s\n",
|
||||||
|
fname, strerror (errno));
|
||||||
|
fclose (fp);
|
||||||
|
xfree (buf);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
fclose (fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
*r_length = buflen;
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Debug helper to encode or decode to/from zb32. */
|
||||||
|
static void
|
||||||
|
endecode_file (const char *fname, int decode)
|
||||||
|
{
|
||||||
|
char *buffer;
|
||||||
|
size_t buflen;
|
||||||
|
char *result;
|
||||||
|
|
||||||
|
if (decode)
|
||||||
|
{
|
||||||
|
fprintf (stderr, PGM": decode mode has not yet been implemented\n");
|
||||||
|
errcount++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef HAVE_DOSISH_SYSTEM
|
||||||
|
if (decode)
|
||||||
|
setmode (fileno (stdout), O_BINARY);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
buffer = read_file (fname, &buflen);
|
||||||
|
if (!buffer)
|
||||||
|
{
|
||||||
|
errcount++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = zb32_encode (buffer, 8 * buflen);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
fprintf (stderr, PGM": error encoding data: %s\n", strerror (errno));
|
||||||
|
errcount++;
|
||||||
|
xfree (buffer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fputs (result, stdout);
|
||||||
|
putchar ('\n');
|
||||||
|
|
||||||
|
xfree (result);
|
||||||
|
xfree (buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char **argv)
|
main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
(void)argc;
|
int last_argc = -1;
|
||||||
(void)argv;
|
int opt_endecode = 0;
|
||||||
|
|
||||||
test_zb32enc ();
|
no_exit_on_fail = 1;
|
||||||
|
|
||||||
|
if (argc)
|
||||||
|
{ argc--; argv++; }
|
||||||
|
while (argc && last_argc != argc )
|
||||||
|
{
|
||||||
|
last_argc = argc;
|
||||||
|
if (!strcmp (*argv, "--"))
|
||||||
|
{
|
||||||
|
argc--; argv++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if (!strcmp (*argv, "--help"))
|
||||||
|
{
|
||||||
|
fputs ("usage: " PGM " [FILE]\n"
|
||||||
|
"Options:\n"
|
||||||
|
" --verbose Print timings etc.\n"
|
||||||
|
" --debug Flyswatter\n"
|
||||||
|
" --encode Encode FILE or stdin\n"
|
||||||
|
" --decode Decode FILE or stdin\n"
|
||||||
|
, stdout);
|
||||||
|
exit (0);
|
||||||
|
}
|
||||||
|
else if (!strcmp (*argv, "--verbose"))
|
||||||
|
{
|
||||||
|
verbose++;
|
||||||
|
argc--; argv++;
|
||||||
|
}
|
||||||
|
else if (!strcmp (*argv, "--debug"))
|
||||||
|
{
|
||||||
|
verbose += 2;
|
||||||
|
debug++;
|
||||||
|
argc--; argv++;
|
||||||
|
}
|
||||||
|
else if (!strcmp (*argv, "--encode"))
|
||||||
|
{
|
||||||
|
opt_endecode = 1;
|
||||||
|
argc--; argv++;
|
||||||
|
}
|
||||||
|
else if (!strcmp (*argv, "--decode"))
|
||||||
|
{
|
||||||
|
opt_endecode = -1;
|
||||||
|
argc--; argv++;
|
||||||
|
}
|
||||||
|
else if (!strncmp (*argv, "--", 2))
|
||||||
|
{
|
||||||
|
fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argc > 1)
|
||||||
|
{
|
||||||
|
fprintf (stderr, PGM ": to many arguments given\n");
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opt_endecode)
|
||||||
|
{
|
||||||
|
endecode_file (argc? *argv : NULL, (opt_endecode < 0));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
test_zb32enc ();
|
||||||
|
|
||||||
return !!errcount;
|
return !!errcount;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user