summaryrefslogtreecommitdiffstats
path: root/common/t-b64.c
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2008-06-09 16:49:28 +0200
committerWerner Koch <wk@gnupg.org>2008-06-09 16:49:28 +0200
commit5a788b9e00ea70595f3c50b66023531f25dbb5b9 (patch)
treed396bed5ebe536e9f7b1e27cec2c325623a38512 /common/t-b64.c
parentAs a failsafe measure use memcpy instead of strcpy in gnupg_copy_time. (diff)
downloadgnupg2-5a788b9e00ea70595f3c50b66023531f25dbb5b9.tar.xz
gnupg2-5a788b9e00ea70595f3c50b66023531f25dbb5b9.zip
Add Base64 decoder. Not yet used but complements out encoder.
Diffstat (limited to 'common/t-b64.c')
-rw-r--r--common/t-b64.c97
1 files changed, 96 insertions, 1 deletions
diff --git a/common/t-b64.c b/common/t-b64.c
index 0f26ab31e..a230dc033 100644
--- a/common/t-b64.c
+++ b/common/t-b64.c
@@ -65,13 +65,92 @@ test_b64enc_pgp (const char *string)
}
+static void
+test_b64enc_file (const char *fname)
+{
+ gpg_error_t err;
+ struct b64state state;
+ FILE *fp;
+ char buffer[50];
+ size_t nread;
+
+ fp = fname ? fopen (fname, "r") : stdin;
+ if (!fp)
+ {
+ fprintf (stderr, "%s:%d: can't open `%s': %s\n",
+ __FILE__, __LINE__, fname? fname:"[stdin]", strerror (errno));
+ fail (0);
+ }
+
+ err = b64enc_start (&state, stdout, "DATA");
+ if (err)
+ fail (1);
+
+ while ( (nread = fread (buffer, 1, sizeof buffer, fp)) )
+ {
+ err = b64enc_write (&state, buffer, nread);
+ if (err)
+ fail (2);
+ }
+ err = b64enc_finish (&state);
+ if (err)
+ fail (3);
+
+ fclose (fp);
+ pass ();
+}
+
+static void
+test_b64dec_file (const char *fname)
+{
+ gpg_error_t err;
+ struct b64state state;
+ FILE *fp;
+ char buffer[50];
+ size_t nread, nbytes;
+
+ fp = fname ? fopen (fname, "r") : stdin;
+ if (!fp)
+ {
+ fprintf (stderr, "%s:%d: can't open `%s': %s\n",
+ __FILE__, __LINE__, fname? fname:"[stdin]", strerror (errno));
+ fail (0);
+ }
+
+ err = b64dec_start (&state, "");
+ if (err)
+ fail (1);
+
+ while ( (nread = fread (buffer, 1, sizeof buffer, fp)) )
+ {
+ err = b64dec_proc (&state, buffer, nread, &nbytes);
+ if (err)
+ {
+ if (gpg_err_code (err) == GPG_ERR_EOF)
+ break;
+ fail (2);
+ }
+ else if (nbytes)
+ fwrite (buffer, 1, nbytes, stdout);
+ }
+
+ err = b64dec_finish (&state);
+ if (err)
+ fail (3);
+
+ fclose (fp);
+ pass ();
+}
int
main (int argc, char **argv)
{
+ int do_encode = 0;
+ int do_decode = 0;
+
if (argc)
{ argc--; argv++; }
if (argc && !strcmp (argv[0], "--verbose"))
@@ -80,7 +159,23 @@ main (int argc, char **argv)
argc--; argv++;
}
- test_b64enc_pgp (argc? *argv: NULL);
+ if (argc && !strcmp (argv[0], "--encode"))
+ {
+ do_encode = 1;
+ argc--; argv++;
+ }
+ else if (argc && !strcmp (argv[0], "--decode"))
+ {
+ do_decode = 1;
+ argc--; argv++;
+ }
+
+ if (do_encode)
+ test_b64enc_file (argc? *argv: NULL);
+ else if (do_decode)
+ test_b64dec_file (argc? *argv: NULL);
+ else
+ test_b64enc_pgp (argc? *argv: NULL);
return !!errcount;
}