Use GLib's base64 implementation
authorMikael Hallendal <micke@imendio.com>
Wed, 18 Jun 2008 22:27:54 +0200
changeset 407 4401f2aa0692
parent 406 3c6b13d37d41
child 408 b07ef2183e6b
Use GLib's base64 implementation
loudmouth/Makefile.am
loudmouth/base64.c
loudmouth/base64.h
loudmouth/lm-internals.h
loudmouth/lm-proxy.c
loudmouth/lm-sasl.c
loudmouth/lm-utils.c
--- a/loudmouth/Makefile.am	Wed Jun 18 22:27:47 2008 +0200
+++ b/loudmouth/Makefile.am	Wed Jun 18 22:27:54 2008 +0200
@@ -59,8 +59,6 @@
 	lm-sasl.h                       \
 	md5.c                           \
 	md5.h                           \
-	base64.c                        \
-	base64.h                        \
 	$(NULL)
 
 libloudmouthinclude_HEADERS =		\
--- a/loudmouth/base64.c	Wed Jun 18 22:27:47 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,194 +0,0 @@
-/*
- * base64.c - Base 64 encoding/decoding implementation
- * Copyright (C) 2006 Collabora Ltd.
- *
- * This library 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 of the License, or (at your option) any later version.
- *
- * This library 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 library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- */
-
-#include <ctype.h>
-#include <string.h>
-
-#include <glib.h>
-
-#include "base64.h"
-
-/*
-|AAAA AABB|BBBB CCCC|CCDD DDDD|
-
-0xFC = 1111 1100
-0x03 = 0000 0011
-0xF0 = 1111 0000
-0x0F = 0000 1111
-0xC0 = 1100 0000
-0x3F = 0011 1111
-
-3 input bytes = 4 output bytes;
-2 input bytes = 2 output bytes;
-1 input byte  = 1 output byte.
-*/
-
-static const gchar *encoding =
-  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-static const guint decoding[256] =
-{
-  /* ... */
-   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-   0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-   0, 0, 0,
-  /* + */
-  62,
-  /* ... */
-   0, 0, 0,
-  /* / , 0-9 */
-  63,
-  52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
-  /* ... */
-   0, 0, 0, 0, 0, 0, 0,
-  /* A */
-   0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12,
-  13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
-  /* ... */
-   0, 0, 0, 0, 0, 0,
-  /* a */
-  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
-};
-
-#define GET_6_BITS_0(s) (((s)[0] & 0xFC) >> 2)
-#define GET_6_BITS_1(s) (((s)[0] & 0x03) << 4) | \
-                        (((s)[1] & 0xF0) >> 4)
-#define GET_6_BITS_2(s) (((s)[1] & 0x0F) << 2) | \
-                        (((s)[2] & 0xC0) >> 6)
-#define GET_6_BITS_3(s) (((s)[2] & 0x3F) << 0)
-
-#define GET_BYTE_0(s) (((decoding[(guchar)(s)[0]] & 0x3F) << 2) | \
-                       ((decoding[(guchar)(s)[1]] & 0x30) >> 4))
-#define GET_BYTE_1(s) (((decoding[(guchar)(s)[1]] & 0x0F) << 4) | \
-                       ((decoding[(guchar)(s)[2]] & 0x3C) >> 2))
-#define GET_BYTE_2(s) (((decoding[(guchar)(s)[2]] & 0x03) << 6) | \
-                       ((decoding[(guchar)(s)[3]] & 0xFF) << 0))
-
-gchar *_lm_base64_encode (const gchar *txt, gsize n)
-{
-  guint i;
-  guint len;
-  GString *tmp;
-  GString *str = g_string_new_len (txt, n);
-
-  len = str->len;
-  /* TODO: calculate requisite output string length and allocate that big a
-   * GString */
-  tmp = g_string_new ("");
-
-  for (i = 0; i < len; i += 3)
-    {
-      guint c1, c2, c3, c4;
-
-      switch (i + 3 - len)
-        {
-        case 1:
-          c1 = encoding[GET_6_BITS_0 (str->str + i)];
-          c2 = encoding[GET_6_BITS_1 (str->str + i)];
-          c3 = encoding[GET_6_BITS_2 (str->str + i)];
-          c4 = '=';
-          break;
-        case 2:
-          c1 = encoding[GET_6_BITS_0 (str->str + i)];
-          c2 = encoding[GET_6_BITS_1 (str->str + i)];
-          c3 = '=';
-          c4 = '=';
-          break;
-        default:
-          c1 = encoding[GET_6_BITS_0 (str->str + i)];
-          c2 = encoding[GET_6_BITS_1 (str->str + i)];
-          c3 = encoding[GET_6_BITS_2 (str->str + i)];
-          c4 = encoding[GET_6_BITS_3 (str->str + i)];
-        }
-
-      g_string_append_printf (tmp, "%c%c%c%c", c1, c2, c3, c4);
-    }
-
-  return g_string_free (tmp, FALSE);
-}
-
-gchar *_lm_base64_decode (const gchar *str, gsize *len)
-{
-  guint i;
-  GString *tmp;
-  char group[4];
-  guint filled = 0;
-
-  *len = 0;
-
-  for (i = 0; str[i]; i++)
-    {
-      if (str[i] != 'A' &&
-          str[i] != '=' &&
-          !isspace(str[i]) &&
-          decoding[(guchar) str[i]] == 0)
-        {
-          g_debug ("bad character %x at byte %u", (guchar)str[i], i);
-          return NULL;
-        }
-    }
-
-  tmp = g_string_new ("");
-
-  for (i = 0; str[i]; i++)
-    {
-      if (isspace(str[i]))
-        continue;
-
-      group[filled++] = str[i];
-
-      if (filled == 4)
-        {
-          if (group[3] == '=')
-            {
-              if (group[2] == '=')
-                {
-                  g_string_append_c (tmp, GET_BYTE_0(group));
-                }
-              else
-                {
-                  g_string_append_c (tmp, GET_BYTE_0(group));
-                  g_string_append_c (tmp, GET_BYTE_1(group));
-                }
-             }
-           else
-            {
-              g_string_append_c (tmp, GET_BYTE_0(group));
-              g_string_append_c (tmp, GET_BYTE_1(group));
-              g_string_append_c (tmp, GET_BYTE_2(group));
-            }
-          filled = 0;
-        }
-    }
-
-  if (filled)
-    {
-      g_debug ("insufficient padding at end of base64 string:\n%s", str);
-      g_string_free (tmp, TRUE);
-      return NULL;
-    }
-
-  *len = tmp->len;
-  return g_string_free (tmp, FALSE);
-}
-
-
--- a/loudmouth/base64.h	Wed Jun 18 22:27:47 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-/*
- * base64.h - Base 64 encoding/decoding implementation
- * Copyright (C) 2006 Collabora Ltd.
- *
- * This library 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 of the License, or (at your option) any later version.
- *
- * This library 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 library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- */
-
-#ifndef __BASE64_H__
-#define __BASE64_H__
-
-#include <glib.h>
-
-gchar *_lm_base64_encode (const gchar *str, gsize n);
-gchar *_lm_base64_decode (const gchar *str, gsize *len);
-
-#define base64_encode _lm_base64_encode
-#define base64_decode _lm_base64_decode
-
-#endif /* __BASE64_H__ */
--- a/loudmouth/lm-internals.h	Wed Jun 18 22:27:47 2008 +0200
+++ b/loudmouth/lm-internals.h	Wed Jun 18 22:27:54 2008 +0200
@@ -70,7 +70,6 @@
 void             _lm_utils_free_callback            (LmCallback       *cb);
 
 gchar *          _lm_utils_generate_id              (void);
-gchar *          _lm_utils_base64_encode            (const gchar      *str);
 gchar *          _lm_utils_hostname_to_punycode     (const gchar      *hostname);
 const gchar *    _lm_message_type_to_string         (LmMessageType     type);
 const gchar *    _lm_message_sub_type_to_string     (LmMessageSubType  type);
--- a/loudmouth/lm-proxy.c	Wed Jun 18 22:27:47 2008 +0200
+++ b/loudmouth/lm-proxy.c	Wed Jun 18 22:27:54 2008 +0200
@@ -88,7 +88,8 @@
 		tmp1 = g_strdup_printf ("%s:%s",
 					proxy->username, 
 					proxy->password);
-		tmp2 = _lm_utils_base64_encode (tmp1);
+		tmp2 = g_base64_encode ((const guchar *) tmp1, 
+					(gsize) strlen (tmp1));
 		g_free (tmp1);
 
 		str = g_strdup_printf ("CONNECT %s:%u HTTP/1.1\r\nHost: %s:%u\r\nProxy-Authorization: Basic %s\r\n\r\n",
--- a/loudmouth/lm-sasl.c	Wed Jun 18 22:27:47 2008 +0200
+++ b/loudmouth/lm-sasl.c	Wed Jun 18 22:27:54 2008 +0200
@@ -37,7 +37,6 @@
 #include "lm-sasl.h"
 
 #include "md5.h"
-#include "base64.h"
 
 typedef enum {
 	AUTH_TYPE_PLAIN  = 1,
@@ -198,7 +197,7 @@
 		n[i] = g_random_int();
 	}
 
-	return base64_encode ((gchar *)n, sizeof(n));
+	return g_base64_encode ((const guchar *)n, (gsize)sizeof(n));
 }
 
 static gchar *
@@ -320,7 +319,8 @@
 		return FALSE;
 	}
 
-	response64 = base64_encode ((gchar *)response, strlen(response));
+	response64 = g_base64_encode ((const guchar *) response, 
+				      (gsize) strlen(response));
 
 	msg = lm_message_new (NULL, LM_MESSAGE_TYPE_RESPONSE);
 	lm_message_node_set_attributes (msg->node,
@@ -408,7 +408,7 @@
 		return FALSE;
 	}
 
-	challenge = (gchar *) base64_decode (encoded, &len);
+	challenge = (gchar *) g_base64_decode (encoded, &len);
 	h = sasl_digest_md5_challenge_to_hash (challenge);
 	g_free(challenge);
 
@@ -610,7 +610,8 @@
 		g_string_append (str, sasl->username);
 		g_string_append_c (str, '\0');
 		g_string_append (str, sasl->password);
-		cstr = base64_encode ((gchar *)str->str, str->len);
+		cstr = g_base64_encode ((const guchar *) str->str, 
+					(gsize) str->len);
 
 		lm_message_node_set_value (auth_msg->node, cstr);
 
--- a/loudmouth/lm-utils.c	Wed Jun 18 22:27:47 2008 +0200
+++ b/loudmouth/lm-utils.c	Wed Jun 18 22:27:54 2008 +0200
@@ -80,69 +80,6 @@
 	return g_strdup_printf ("%ld%ld", val, tv.tv_usec);
 }
 
-gchar * 
-_lm_utils_base64_encode (const gchar *s)
-{
-	static const gchar *base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-	guint    i, j;
-	guint32  bits = 0;
-	guint    maxlen = (strlen(s) * 2) + 3;
-	gchar   *str;
-
-	str = g_malloc(maxlen);
-
-	j = 0;
-	for (i = 0; i < strlen(s); i++) {
-		bits <<= 8;
-		bits |= s[i] & 0xff;
-
-		if (!((i+1) % 3)) {
-			guint indices[4];
-
-			indices[0] = (bits >> 18) & 0x3f;
-			indices[1] = (bits >> 12) & 0x3f;
-			indices[2] = (bits >> 6) & 0x3f;
-			indices[3] = bits & 0x3f;
-			bits = 0;
-
-			str[j++] = base64chars[(indices[0])];
-			str[j++] = base64chars[(indices[1])];
-			str[j++] = base64chars[(indices[2])];
-			str[j++] = base64chars[(indices[3])];
-		}
-	}
-
-	if (j + 4 < maxlen) {
-		if ((i % 3) == 1) {
-			guint indices[2];
-
-			indices[0] = (bits >> 2) & 0x3f;
-			indices[1] = (bits << 4) & 0x3f;
-
-			str[j++] = base64chars[(indices[0])];
-			str[j++] = base64chars[(indices[1])];
-			str[j++] = '=';
-			str[j++] = '=';
-		} else if ((i % 3) == 2) {
-			guint indices[3];
-
-			indices[0] = (bits >> 10) & 0x3f;
-			indices[1] = (bits >> 4) & 0x3f;
-			indices[2] = (bits << 2) & 0x3f;
-
-			str[j++] = base64chars[(indices[0])];
-			str[j++] = base64chars[(indices[1])];
-			str[j++] = base64chars[(indices[2])];
-			str[j++] = '=';
-		}
-	}
-
-	str[j] = '\0';
-
-	return str;
-}
-
 gchar*
 _lm_utils_hostname_to_punycode (const gchar *hostname)
 {