1 /* t-zb32.c - Module tests for zb32.c
2 * Copyright (C) 2014 Werner Koch
4 * This file is part of GnuPG.
6 * This file is free software; you can redistribute it and/or modify
7 * it under the terms of either
9 * - the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at
11 * your option) any later version.
15 * - the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
19 * or both in parallel, as here.
21 * This file is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see <https://www.gnu.org/licenses/>.
37 #ifdef HAVE_DOSISH_SYSTEM
42 #include "t-support.h"
59 /* From the DESIGN document. */
64 { 10, "\x00\x00", "yy" },
65 { 10, "\x80\x80", "on" },
66 { 20, "\x8b\x88\x80", "tqre" },
67 { 24, "\xf0\xbf\xc7", "6n9hq" },
68 { 24, "\xd4\x7a\x04", "4t7ye" },
69 /* The next vector is strange: The DESIGN document from 2007 gives
70 "8ik66o" as result, the revision from 2009 gives "6im5sd". I
71 look at it for quite some time and came to the conclusion that
72 "6im54d" is the right encoding. */
73 { 30, "\xf5\x57\xbd\x0c", "6im54d" },
74 /* From ccrtp's Java code. */
75 { 40, "\x01\x01\x01\x01\x01", "yryonyeb" },
76 { 15, "\x01\x01", "yry" },
77 { 80, "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01", "yryonyebyryonyeb" },
78 { 15, "\x81\x81", "ogy" },
79 { 16, "\x81\x81", "ogyo" },
80 { 20, "\x81\x81\x81", "ogya" },
81 { 64, "\x81\x81\x81\x81\x81\x81\x81\x81", "ogyadycbogyan" },
83 { 160, "\x80\x61\x58\x70\xF5\xBA\xD6\x90\x33\x36"
84 /* */"\x86\xD0\xF2\xAD\x85\xAC\x1E\x42\xB3\x67",
85 /* */"oboioh8izmmjyc3so5exfmcfioxrfc58" },
91 for (tidx = 0; tidx < DIM(tests); tidx++)
93 output = zb32_encode (tests[tidx].data, tests[tidx].datalen);
96 fprintf (stderr, PGM": error encoding test %d: %s\n",
97 tidx, strerror (errno));
101 if (strcmp (output, tests[tidx].expected))
108 /* Read the file FNAME or stdin if FNAME is NULL and return a malloced
109 buffer with the content. R_LENGTH received the length of the file.
110 Print a diagnostic and returns NULL on error. */
112 read_file (const char *fname, size_t *r_length)
120 size_t nread, bufsize = 0;
123 #ifdef HAVE_DOSISH_SYSTEM
124 setmode (fileno(fp) , O_BINARY );
133 buf = xmalloc (bufsize);
135 buf = xrealloc (buf, bufsize);
137 nread = fread (buf+buflen, 1, NCHUNK, fp);
138 if (nread < NCHUNK && ferror (fp))
140 fprintf (stderr, PGM": error reading '[stdin]': %s\n",
147 while (nread == NCHUNK);
155 fp = fopen (fname, "rb");
158 fprintf (stderr, PGM": can't open '%s': %s\n",
159 fname, strerror (errno));
163 if (fstat (fileno(fp), &st))
165 fprintf (stderr, PGM": can't stat '%s': %s\n",
166 fname, strerror (errno));
172 buf = xmalloc (buflen+1);
173 if (fread (buf, buflen, 1, fp) != 1)
175 fprintf (stderr, PGM": error reading '%s': %s\n",
176 fname, strerror (errno));
189 /* Debug helper to encode or decode to/from zb32. */
191 endecode_file (const char *fname, int decode)
199 fprintf (stderr, PGM": decode mode has not yet been implemented\n");
204 #ifdef HAVE_DOSISH_SYSTEM
206 setmode (fileno (stdout), O_BINARY);
210 buffer = read_file (fname, &buflen);
217 result = zb32_encode (buffer, 8 * buflen);
220 fprintf (stderr, PGM": error encoding data: %s\n", strerror (errno));
226 fputs (result, stdout);
235 main (int argc, char **argv)
238 int opt_endecode = 0;
244 while (argc && last_argc != argc )
247 if (!strcmp (*argv, "--"))
252 else if (!strcmp (*argv, "--help"))
254 fputs ("usage: " PGM " [FILE]\n"
256 " --verbose Print timings etc.\n"
257 " --debug Flyswatter\n"
258 " --encode Encode FILE or stdin\n"
259 " --decode Decode FILE or stdin\n"
263 else if (!strcmp (*argv, "--verbose"))
268 else if (!strcmp (*argv, "--debug"))
274 else if (!strcmp (*argv, "--encode"))
279 else if (!strcmp (*argv, "--decode"))
284 else if (!strncmp (*argv, "--", 2))
286 fprintf (stderr, PGM ": unknown option '%s'\n", *argv);
293 fprintf (stderr, PGM ": to many arguments given\n");
299 endecode_file (argc? *argv : NULL, (opt_endecode < 0));