gsasl-commit
[Top][All Lists]
Advanced

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

CVS gsasl/lib/gl


From: gsasl-commit
Subject: CVS gsasl/lib/gl
Date: Fri, 26 Nov 2004 01:38:22 +0100

Update of /home/cvs/gsasl/lib/gl
In directory dopio:/tmp/cvs-serv4090/gl

Modified Files:
        Makefile.am 
Added Files:
        base64.c base64.h stdbool_.h 
Log Message:
Add base64.

--- /home/cvs/gsasl/lib/gl/Makefile.am  2004/10/31 22:17:27     1.11
+++ /home/cvs/gsasl/lib/gl/Makefile.am  2004/11/26 00:38:22     1.12
@@ -9,7 +9,7 @@
 #
 # Generated by gnulib-tool.
 # Invoked as: gnulib-tool --import
-# Reproduce by: gnulib-tool --import --dir=. --lib=libgl --source-base=gl 
--m4-base=gl/m4 --libtool --lgpl dummy gettext strdup
+# Reproduce by: gnulib-tool --import --dir=. --lib=libgl --source-base=gl 
--m4-base=gl/m4 --libtool --lgpl base64 dummy gettext stdbool strdup
 
 AUTOMAKE_OPTIONS = 1.5 gnits no-dependencies
 
@@ -25,6 +25,12 @@
 DISTCLEANFILES =
 MAINTAINERCLEANFILES =
 
+## begin gnulib module base64
+
+libgl_la_SOURCES += base64.h base64.c
+
+## end   gnulib module base64
+
 ## begin gnulib module dummy
 
 libgl_la_SOURCES += dummy.c
@@ -37,6 +43,21 @@
 
 ## end   gnulib module gettext
 
+## begin gnulib module stdbool
+
+BUILT_SOURCES += $(STDBOOL_H)
+EXTRA_DIST += stdbool_.h
+
+# We need the following in order to create an <stdbool.h> when the system
+# doesn't have one that works.
+all-local $(libgl_la_OBJECTS): $(STDBOOL_H)
+stdbool.h: stdbool_.h
+       sed -e 's/@''HAVE__BOOL''@/$(HAVE__BOOL)/g' < $(srcdir)/stdbool_.h > 
address@hidden
+       mv address@hidden $@
+MOSTLYCLEANFILES += stdbool.h stdbool.h-t
+
+## end   gnulib module stdbool
+
 ## begin gnulib module strdup
 
 libgl_la_SOURCES += strdup.h

--- /home/cvs/gsasl/lib/gl/base64.c     2004/11/26 00:38:22     NONE
+++ /home/cvs/gsasl/lib/gl/base64.c     2004/11/26 00:38:22     1.1
/* base64.c -- Encode binary data using printable characters.
   Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation; either version 2.1, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

/* Portions adapted from GNU MailUtils, by Simon Josefsson.  For more
   information, see RFC 3548 <http://www.ietf.org/rfc/rfc3548.txt>. */

/* Get malloc. */
#include <stdlib.h>

/* Get prototype. */
#include "base64.h"

/* Base64 encode IN array of size INLEN into OUT array of size OUTLEN.
   If OUTLEN is less than BASE64_LENGTH(INLEN), write as many bytes as
   possible.  If OUTLEN is larger than BASE64_LENGTH(INLEN), also zero
   terminate the output buffer. */
void
base64_encode (const char *in, size_t inlen, char *out, size_t outlen)
{
  const char *b64 =
    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  const unsigned char *iptr = (const unsigned char *) in;
  unsigned char *optr = (unsigned char *) out;

  while (inlen && outlen)
    {
      *optr++ = b64[iptr[0] >> 2];
      if (!--outlen)
        break;
      *optr++ = b64[((iptr[0] << 4) + (--inlen ? (iptr[1] >> 4) : 0)) & 0x3f];
      if (!--outlen)
        break;
      *optr++ = inlen ? b64[((iptr[1] << 2) +
                             (--inlen ? (iptr[2] >> 6) : 0)) & 0x3f] : '=';
      if (!--outlen)
        break;
      *optr++ = inlen ? b64[iptr[2] & 0x3f] : '=';
      if (!--outlen)
        break;
      if (inlen)
        inlen--;
      iptr += 3;
    }

  if (outlen)
    *optr = '\0';
}

/* Allocate a buffer and store zero terminated base64 encoded data
   from array IN of size INLEN, returning BASE64_LENGTH(INLEN), i.e.,
   the length of the encoded data, excluding the terminating zero.  On
   return, the OUT variable will hold a pointer to newly allocated
   memory that must be deallocated by the caller, or NULL on memory
   allocation failure.  */
size_t
base64_encode_alloc (const char *in, size_t inlen, char **out)
{
  size_t outlen;

  outlen = BASE64_LENGTH (inlen);
  *out = malloc (outlen + 1);
  if (!*out)
    return outlen;

  base64_encode (in, inlen, *out, outlen + 1);

  return outlen;
}

/* Decode base64 encoded input array IN of length INLEN to output
   array OUT that can hold *OUTLEN bytes.  Return true if decoding was
   successful, false otherwise.  If *OUTLEN is too small, as many
   bytes as possible will be written to OUT.  On return, *OUTLEN holds
   the length of decode bytes in OUT.  Note that if any non-alphabet
   characters are encountered, decoding is stopped and false is
   returned. */
bool
base64_decode (const char *in, size_t inlen, char *out, size_t * outlen)
{
  static const signed char b64[0x100] = {
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -1, -1, -2, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -3, -1, -1,
    -1, 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  };
  const unsigned char *iptr = (const unsigned char *) in;
  unsigned char *optr = (unsigned char *) out;
  size_t len = *outlen;

  *outlen = 0;

  while (inlen >= 2)
    {
      if (!len--)
        return true;

      if (b64[iptr[0]] < 0 || b64[iptr[1]] < 0)
        return false;

      *optr++ = (b64[iptr[0]] << 2) | (b64[iptr[1]] >> 4);
      (*outlen)++;

      if (inlen == 2)
        return false;

      if (iptr[2] == '=')
        {
          if (iptr[3] != '=')
            return false;

          if (inlen != 4)
            return false;
        }
      else
        {
          if (!len--)
            return true;

          if (b64[iptr[2]] < 0)
            return false;

          *optr++ = ((b64[iptr[1]] << 4) & 0xf0) | (b64[iptr[2]] >> 2);
          (*outlen)++;

          if (inlen == 3)
            return false;

          if (iptr[3] == '=')
            {
              if (inlen != 4)
                return false;
            }
          else
            {
              if (!len--)
                return true;

              if (b64[iptr[3]] < 0)
                return false;

              *optr++ = ((b64[iptr[2]] << 6) & 0xc0) | b64[iptr[3]];
              (*outlen)++;
            }
        }
      iptr += 4;
      inlen -= 4;
    }

  if (inlen != 0)
    return false;

  return true;

}

/* Allocate an output buffer OUT, and decode the base64 encoded data
   stored in IN of size INLEN.  On return, the actual size of the
   decoded data is stored in *OUTLEN.  The function return true if
   decoding was successful, or false on memory allocation or decoding
   errors.  */
bool
base64_decode_alloc (const char *in, size_t inlen, char **out,
                     size_t * outlen)
{
  *outlen = 3 * inlen / 4;      /* FIXME: May allocate one 1 or 2 bytes too
                                   much, depending on input. */
  *out = malloc (*outlen);
  if (!*out)
    return false;
  return base64_decode (in, inlen, *out, outlen);
}
--- /home/cvs/gsasl/lib/gl/base64.h     2004/11/26 00:38:22     NONE
+++ /home/cvs/gsasl/lib/gl/base64.h     2004/11/26 00:38:22     1.1
/* base64.h -- Encode binary data using printable characters.
   Copyright (C) 2004 Free Software Foundation, Inc.
   Written by Simon Josefsson.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation; either version 2.1, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#ifndef BASE64_H
# define BASE64_H

/* Get size_t. */
#include <stddef.h>

/* Get bool. */
#include <stdbool.h>

/* This uses that the expression (n+(k-1))/k means the smallest
   integer >= n/k, i.e., the ceiling of n/k.  */
#define BASE64_LENGTH(inlen) ((((inlen) + 2) / 3) * 4)

extern void base64_encode (const char *in, size_t inlen,
                           char *out, size_t outlen);

extern size_t base64_encode_alloc (const char *in, size_t inlen, char **out);

extern bool base64_decode (const char *in, size_t inlen,
                           char *out, size_t * outlen);

extern bool base64_decode_alloc (const char *in, size_t inlen,
                                 char **out, size_t * outlen);

#endif /* BASE64_H */
--- /home/cvs/gsasl/lib/gl/stdbool_.h   2004/11/26 00:38:22     NONE
+++ /home/cvs/gsasl/lib/gl/stdbool_.h   2004/11/26 00:38:22     1.1
/* Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
   Written by Bruno Haible <address@hidden>, 2001.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation; either version 2.1, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#ifndef _STDBOOL_H
#define _STDBOOL_H

/* ISO C 99 <stdbool.h> for platforms that lack it.  */

/* Usage suggestions:

   Programs that use <stdbool.h> should be aware of some limitations
   and standards compliance issues.

   Standards compliance:

       - <stdbool.h> must be #included before 'bool', 'false', 'true'
         can be used.

       - You cannot assume that sizeof (bool) == 1.

       - Programs should not undefine the macros bool, true, and false,
         as C99 lists that as an "obsolescent feature".

   Limitations of this substitute, when used in a C89 environment:

       - <stdbool.h> must be #included before the '_Bool' type can be used.

       - You cannot assume that _Bool is a typedef; it might be a macro.

       - In C99, casts and automatic conversions to '_Bool' or 'bool' are
         performed in such a way that every nonzero value gets converted
         to 'true', and zero gets converted to 'false'.  This doesn't work
         with this substitute.  With this substitute, only the values 0 and 1
         give the expected result when converted to _Bool' or 'bool'.

   Also, it is suggested that programs use 'bool' rather than '_Bool';
   this isn't required, but 'bool' is more common.  */


/* 7.16. Boolean type and values */

/* BeOS <sys/socket.h> already #defines false 0, true 1.  We use the same
   definitions below, but temporarily we have to #undef them.  */
#ifdef __BEOS__
# include <OS.h> /* defines bool but not _Bool */
# undef false
# undef true
#endif

/* For the sake of symbolic names in gdb, we define true and false as
   enum constants, not only as macros.
   It is tempting to write
      typedef enum { false = 0, true = 1 } _Bool;
   so that gdb prints values of type 'bool' symbolically. But if we do
   this, values of type '_Bool' may promote to 'int' or 'unsigned int'
   (see ISO C 99 6.7.2.2.(4)); however, '_Bool' must promote to 'int'
   (see ISO C 99 6.3.1.1.(2)).  So we add a negative value to the
   enum; this ensures that '_Bool' promotes to 'int'.  */
#if !(defined __cplusplus || defined __BEOS__)
# if address@hidden@
#  if defined __SUNPRO_C && (__SUNPRO_C < 0x550 || __STDC__ == 1)
    /* Avoid stupid "warning: _Bool is a keyword in ISO C99".  */
#   define _Bool signed char
enum { false = 0, true = 1 };
#  else
typedef enum { _Bool_must_promote_to_int = -1, false = 0, true = 1 } _Bool;
#  endif
# endif
#else
typedef bool _Bool;
#endif
#define bool _Bool

/* The other macros must be usable in preprocessor directives.  */
#define false 0
#define true 1
#define __bool_true_false_are_defined 1

#endif /* _STDBOOL_H */




reply via email to

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