bug-gnulib
[Top][All Lists]
Advanced

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

Re: [bug-gnulib] Re: base64


From: Paul Eggert
Subject: Re: [bug-gnulib] Re: base64
Date: Sun, 28 Nov 2004 20:00:34 -0800
User-agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux)

I don't see why SIZE_MAX is part of the interface to
base64_encode_alloc.  Can't base64_encode_alloc just set *out to NULL
on memory allocation error, regardless of whether the error is due to
lack of memory or address-arithmetic overflow?  The caller shouldn't
care about the distinction.  If you get rid of the SIZE_MAX, you'll
simplify the interface, implementation, and usage.

Simon Josefsson <address@hidden> writes:

> Does anyone know if any standard guarantee that the characters
> A-Za-z0-9+/ must be encoded in the 0..255 interval in 'char', if char
> is larger than 8 bits?

POSIX 1003.1-2001 requires that char and unsigned char must be 8-bit
quantities.  C99 does not.  See
<http://www.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap03.html#tag_03_84>.

> +/* Get malloc. */
> +#include <stdlib.h>
> +
> +/* Get prototype. */
> +#include "base64.h"

The gnulib tradition is to put "base64.h" first (just after config.h).
That catches more header-dependency mistakes.

> +/* C89 compliant way to cast 'const char *' to 'const unsigned char *'. */
> +static inline const unsigned char *to_cucharp (const char *ch) { return ch; }

As I recall, this isn't C89 compliant.  You can't convert char * to
unsigned char * (even if you add a cast).  There is no way to do it
portably.  You have to convert char to unsigned char upon each
dereference, as in
<http://lists.gnu.org/archive/html/bug-gnulib/2004-11/msg00170.html>.
(The code is equally efficient with decent compilers.)

> +#if UCHAR_MAX > 255
> +  if (ch > 255)
> +    return false;
> +#endif

>From long experience I've learned to avoid #if when possible.
Here you can write this:

  if (UCHAR_MAX > 255 && ch > 255)
    return false;

or, if you prefer brevity, this:

  if (ch > 255)
    return false;

as decent compilers will optimize the tests away.

> +  size_t outleft = *outlen;
> +
> +  if (outlen)
> +    *outlen = 0;

Something is odd here, since outlen obviously must be nonnull.
Also, for efficiency the code should assign to *outlen just once,
rather than updating it on each output byte.




reply via email to

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