bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Feature request - base64 Filename Safe Alphabet


From: Bo Borgerson
Subject: Re: Feature request - base64 Filename Safe Alphabet
Date: Wed, 30 Apr 2008 18:27:16 -0400
User-agent: Thunderbird 2.0.0.12 (X11/20080227)

Jim Meyering wrote:
> Beware:
> there are two versions of base64.c.
> The one in gnulib and another in coreutils/gl/lib.
> 
> Simon and I have been thinking about how to merge these
> two for some time, but I haven't found time since our last exchange.
> 
> Volunteers welcome ;-)


Hi,

This is an attempt at making a base64.c that supports the context
structure for coreutils but still presents a four-argument decode
interface for gnulib.

It doesn't address the differences in newline handling, and it's
definitely less efficient for four-argument decode calls.  Is this the
direction you were thinking for a merge of the two?

Thanks,

Bo
>From e63ed95710560a7da7f4fd681add4f0e8172bc7a Mon Sep 17 00:00:00 2001
From: Bo Borgerson <address@hidden>
Date: Wed, 30 Apr 2008 17:40:38 -0400
Subject: [PATCH] A step toward an upstream compatible base64

* gl/lib/base64.c (base64_decode_ctx): If no context structure was passed in,
initialize a local one and use it.  Be sure to flush.  Formerly base64_decode.
(base64_decode_alloc_ctx): Formerly base64_decode_alloc.
* gl/lib/base64.h (base64_decode): Macro for four-argument calls.
(base64_decode_alloc): Likewise.
* src/base64.c (do_decode): Call base64_decode_ctx instead of base64_decode.

Signed-off-by: Bo Borgerson <address@hidden>
---
 gl/lib/base64.c |   22 +++++++++++++++-------
 gl/lib/base64.h |   19 +++++++++++++------
 src/base64.c    |    2 +-
 3 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/gl/lib/base64.c b/gl/lib/base64.c
index 43f12c6..4a79eef 100644
--- a/gl/lib/base64.c
+++ b/gl/lib/base64.c
@@ -452,12 +452,20 @@ decode_4 (char const *restrict in, size_t inlen,
    bytes spans two input buffers.  */
 
 bool
-base64_decode (struct base64_decode_context *ctx,
-              const char *restrict in, size_t inlen,
-              char *restrict out, size_t *outlen)
+base64_decode_ctx (struct base64_decode_context *ctx,
+                  const char *restrict in, size_t inlen,
+                  char *restrict out, size_t *outlen)
 {
   size_t outleft = *outlen;
   bool flush_ctx = inlen == 0;
+  struct base64_decode_context local_ctx;
+
+  if (ctx == NULL)
+    {
+      ctx = &local_ctx;
+      base64_decode_ctx_init (ctx);
+      flush_ctx = true;
+    }
 
   while (true)
     {
@@ -529,9 +537,9 @@ base64_decode (struct base64_decode_context *ctx,
    input was invalid, in which case *OUT is NULL and *OUTLEN is
    undefined. */
 bool
-base64_decode_alloc (struct base64_decode_context *ctx,
-                    const char *in, size_t inlen, char **out,
-                    size_t *outlen)
+base64_decode_alloc_ctx (struct base64_decode_context *ctx,
+                        const char *in, size_t inlen, char **out,
+                        size_t *outlen)
 {
   /* This may allocate a few bytes too many, depending on input,
      but it's not worth the extra CPU time to compute the exact size.
@@ -544,7 +552,7 @@ base64_decode_alloc (struct base64_decode_context *ctx,
   if (!*out)
     return true;
 
-  if (!base64_decode (ctx, in, inlen, *out, &needlen))
+  if (!base64_decode_ctx (ctx, in, inlen, *out, &needlen))
     {
       free (*out);
       *out = NULL;
diff --git a/gl/lib/base64.h b/gl/lib/base64.h
index ba436e0..fa242c8 100644
--- a/gl/lib/base64.h
+++ b/gl/lib/base64.h
@@ -42,12 +42,19 @@ extern void base64_encode (const char *restrict in, size_t 
inlen,
 extern size_t base64_encode_alloc (const char *in, size_t inlen, char **out);
 
 extern void base64_decode_ctx_init (struct base64_decode_context *ctx);
-extern bool base64_decode (struct base64_decode_context *ctx,
-                          const char *restrict in, size_t inlen,
-                          char *restrict out, size_t *outlen);
 
-extern bool base64_decode_alloc (struct base64_decode_context *ctx,
-                                const char *in, size_t inlen,
-                                char **out, size_t *outlen);
+extern bool base64_decode_ctx (struct base64_decode_context *ctx,
+                              const char *restrict in, size_t inlen,
+                              char *restrict out, size_t *outlen);
+
+extern bool base64_decode_alloc_ctx (struct base64_decode_context *ctx,
+                                    const char *in, size_t inlen,
+                                    char **out, size_t *outlen);
+
+#define base64_decode(in, inlen, out, outlen) \
+       base64_decode_ctx (NULL, in, inlen, out, outlen)
+
+#define base64_decode_alloc(in, inlen, out, outlen) \
+       base64_decode_alloc_ctx (NULL, in, inlen, out, outlen)
 
 #endif /* BASE64_H */
diff --git a/src/base64.c b/src/base64.c
index aa2fc8f..983b8cb 100644
--- a/src/base64.c
+++ b/src/base64.c
@@ -223,7 +223,7 @@ do_decode (FILE *in, FILE *out, bool ignore_garbage)
          if (k == 1 && ctx.i == 0)
            break;
          n = BLOCKSIZE;
-         ok = base64_decode (&ctx, inbuf, (k == 0 ? sum : 0), outbuf, &n);
+         ok = base64_decode_ctx (&ctx, inbuf, (k == 0 ? sum : 0), outbuf, &n);
 
          if (fwrite (outbuf, 1, n, out) < n)
            error (EXIT_FAILURE, errno, _("write error"));
-- 
1.5.4.3


reply via email to

[Prev in Thread] Current Thread [Next in Thread]