Re: [Easytag-mailing] [EasyTAG patch] use g_base64_{de,en}code()



Hi Adrian,

Thanks for the patch.
Indeed I think it is better to use glib functions as I'm trying to do in the whole code...

Regards,
Jérôme


Adrian Bunk wrote, the 27/08/2009 23:12 :
On Wed, Aug 26, 2009 at 02:01:38PM -0700, Kip Warner wrote:
Hey Adrian,

Hi Kip,

In that case, it sounds like a good idea if the performance is good.
Although since it works extremely fast now, I'm not sure if there will
be any benefit to swapping the code with another routine. Maybe
experiment.

the main benefit is using well-tested code of a standard library instead of shipping own implementations.

Can you try the patch below instead?

cu
Adrian

BTW: This patch also fixes the bugs that memory allocated
     in g_base64_encode() with malloc() was freed with g_free().



 configure.in       |    4
 src/Makefile.am    |    2
 src/Makefile.mingw |    1
 src/base64.c       |  206 ---------------------------------------------
 src/base64.h       |   42 ---------
 src/cddb.c         |   59 ------------
 src/ogg_tag.c      |   14 +--
 7 files changed, 9 insertions(+), 319 deletions(-)

diff --git a/configure.in b/configure.in
index 7f916b3..247aae7 100644
--- a/configure.in
+++ b/configure.in
@@ -76,9 +76,9 @@ dnl Checks for libraries.
 dnl -------------------------------
dnl GTK+-2 libraries
-AM_PATH_GLIB_2_0(2.8.0,, AC_MSG_ERROR([*** GLIB >= 2.8.0 not installed! ***]))
+AM_PATH_GLIB_2_0(2.12.0,, AC_MSG_ERROR([*** GLIB >= 2.8.0 not installed! ***]))
 AM_PATH_GTK_2_0(2.12.0,, AC_MSG_ERROR([*** GTK >= 2.12.0 not installed! ***]))
-PKG_CHECK_MODULES(GTK, gtk+-2.0 >= 2.12.0 glib-2.0 >= 2.8.0)
+PKG_CHECK_MODULES(GTK, gtk+-2.0 >= 2.12.0 glib-2.0 >= 2.12.0)
dnl Patch from Ben Taylor to fix a Solaris bug (for CDDB)
diff --git a/src/Makefile.am b/src/Makefile.am
index 02dd02e..99f24bb 100755
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -11,8 +11,6 @@ easytag_SOURCES = \
  ape_tag.h \
  bar.c \
  bar.h \
- base64.c \
- base64.h \
  browser.c \
  browser.h \
  cddb.c \
diff --git a/src/Makefile.mingw b/src/Makefile.mingw
index d4ed141..8c1f336 100755
--- a/src/Makefile.mingw
+++ b/src/Makefile.mingw
@@ -67,7 +67,6 @@ RC_SRC = win32/easytag.rc
 DLL_C_SRC = about.c \
     ape_tag.c \
     bar.c \
-    base64.c \
     browser.c \
     charset.c \
     cddb.c \
diff --git a/src/base64.c b/src/base64.c
deleted file mode 100755
index 496f697..0000000
--- a/src/base64.c
+++ /dev/null
@@ -1,206 +0,0 @@
-/*
- * Copyright (c) 1995 - 1999 Kungliga Tekniska Högskolan
- * (Royal Institute of Technology, Stockholm, Sweden).
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * 3. Neither the name of the Institute nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
-   The following encoding technique is taken from RFC 1521 by Borenstein
-   and Freed.  It is reproduced here in a slightly edited form for
-   convenience.
-
-   A 65-character subset of US-ASCII is used, enabling 6 bits to be
-   represented per printable character. (The extra 65th character, "=",
-   is used to signify a special processing function.)
-
-   The encoding process represents 24-bit groups of input bits as output
-   strings of 4 encoded characters. Proceeding from left to right, a
-   24-bit input group is formed by concatenating 3 8-bit input groups.
-   These 24 bits are then treated as 4 concatenated 6-bit groups, each
-   of which is translated into a single digit in the base64 alphabet.
-
-   Each 6-bit group is used as an index into an array of 64 printable
-   characters. The character referenced by the index is placed in the
-   output string.
-
-                         Table 1: The Base64 Alphabet
-
-      Value Encoding  Value Encoding  Value Encoding  Value Encoding
-          0 A            17 R            34 i            51 z
-          1 B            18 S            35 j            52 0
-          2 C            19 T            36 k            53 1
-          3 D            20 U            37 l            54 2
-          4 E            21 V            38 m            55 3
-          5 F            22 W            39 n            56 4
-          6 G            23 X            40 o            57 5
-          7 H            24 Y            41 p            58 6
-          8 I            25 Z            42 q            59 7
-          9 J            26 a            43 r            60 8
-         10 K            27 b            44 s            61 9
-         11 L            28 c            45 t            62 +
-         12 M            29 d            46 u            63 /
-         13 N            30 e            47 v
-         14 O            31 f            48 w         (pad) =
-         15 P            32 g            49 x
-         16 Q            33 h            50 y
-
-   Special processing is performed if fewer than 24 bits are available
-   at the end of the data being encoded.  A full encoding quantum is
-   always completed at the end of a quantity.  When fewer than 24 input
-   bits are available in an input group, zero bits are added (on the
-   right) to form an integral number of 6-bit groups.  Padding at the
-   end of the data is performed using the '=' character.
-
-   Since all base64 input is an integral number of octets, only the
-         -------------------------------------------------
-   following cases can arise:
-
-       (1) the final quantum of encoding input is an integral
-           multiple of 24 bits; here, the final unit of encoded
-	   output will be an integral multiple of 4 characters
-	   with no "=" padding,
-       (2) the final quantum of encoding input is exactly 8 bits;
-           here, the final unit of encoded output will be two
-	   characters followed by two "=" padding characters, or
-       (3) the final quantum of encoding input is exactly 16 bits;
-           here, the final unit of encoded output will be three
-	   characters followed by one "=" padding character.
-   */
-
-/* $Id: base64.c,v 1.5 2001/05/28 17:33:41 joda Exp $ */
-
-/*
- * Code taken from Kerberos krb4-1.2.2
- */
-
-#include <stdlib.h>
-#include <string.h>
-#include "base64.h"
-
-static char base64_chars[] = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
-static int -pos(char c)
-{
-    char *p;
-    for (p = base64_chars; *p; p++)
-	if (*p == c)
-	    return p - base64_chars;
-    return -1;
-}
-
-int -base64_encode(const void *data, int size, char **str)
-{
-    char *s, *p;
-    int i;
-    int c;
-    const unsigned char *q;
-
-    p = s = (char *) malloc(size * 4 / 3 + 4);
-    if (p == NULL)
-        return -1;
-    q = (const unsigned char *) data;
-    i = 0;
-    for (i = 0; i < size;)
-    {
-        c = q[i++];
-        c *= 256;
-        if (i < size)
-            c += q[i];
-        i++;
-        c *= 256;
-        if (i < size)
-            c += q[i];
-        i++;
-        p[0] = base64_chars[(c & 0x00fc0000) >> 18];
-        p[1] = base64_chars[(c & 0x0003f000) >> 12];
-        p[2] = base64_chars[(c & 0x00000fc0) >> 6];
-        p[3] = base64_chars[(c & 0x0000003f) >> 0];
-        if (i > size)
-            p[3] = '=';
-        if (i > size + 1)
-            p[2] = '=';
-        p += 4;
-    }
-    *p = 0;
-    *str = s;
-    return strlen(s);
-}
-
-#define DECODE_ERROR 0xffffffff
-
-static unsigned int
-token_decode(const char *token)
-{
-    int i;
-    unsigned int val = 0;
-    int marker = 0;
- - if (strlen(token) < 4)
-        return DECODE_ERROR;
-    for (i = 0; i < 4; i++)
-    {
-        val *= 64;
-        if (token[i] == '=')
-            marker++;
-        else if (marker > 0)
-            return DECODE_ERROR;
-        else
-            val += pos(token[i]);
-    }
-    if (marker > 2)
-        return DECODE_ERROR;
-    return (marker << 24) | val;
-}
-
-int
-base64_decode(const char *str, void *data)
-{
-    const char *p;
-    unsigned char *q;
-
-    q = data;
-    for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4)
-    {
-        unsigned int val = token_decode(p);
-        unsigned int marker = (val >> 24) & 0xff;
- - if (val == DECODE_ERROR)
-            return -1;
-        *q++ = (val >> 16) & 0xff;
-        if (marker < 2)
-            *q++ = (val >> 8) & 0xff;
-        if (marker < 1)
-            *q++ = val & 0xff;
-    }
-    return q - (unsigned char *) data;
-}
diff --git a/src/base64.h b/src/base64.h
deleted file mode 100755
index f0ddea4..0000000
--- a/src/base64.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Högskolan
- * (Royal Institute of Technology, Stockholm, Sweden).
- * All rights reserved.
- * - * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * - * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * - * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * - * 3. Neither the name of the Institute nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-/* $Id: base64.h,v 1.2 1999/12/02 16:58:45 joda Exp $ */
-
-#ifndef _BASE64_H_
-#define _BASE64_H_
-
-int base64_encode(const void *data, int size, char **str);
-int base64_decode(const char *str, void *data);
-
-#endif
diff --git a/src/cddb.c b/src/cddb.c
index f23de7e..db4a31f 100755
--- a/src/cddb.c
+++ b/src/cddb.c
@@ -44,7 +44,6 @@
 #include "easytag.h"
 #include "et_core.h"
 #include "browser.h"
-#include "base64.h"
 #include "scan.h"
 #include "log.h"
 #include "misc.h"
@@ -197,7 +196,6 @@ gint Cddb_Track_List_Sort_Func (GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter
 void Cddb_Track_List_Sort_By_Ascending_Track_Number (void);
 void Cddb_Track_List_Sort_By_Ascending_Track_Name   (void);
-//char *base64_encode (char *str);
 gchar *Cddb_Format_Proxy_Authentification (void);
@@ -4133,57 +4131,6 @@ GdkPixbuf *Cddb_Get_Pixbuf_From_Server_Name (gchar *server_name)
 }
-/*
- * Function taken from gFTP.
- * The standard to Base64 encoding can be found in RFC2045
- */
-/*
-char *base64_encode (char *str)
-{
-    char *newstr, *newpos, *fillpos, *pos;
-    unsigned char table[64], encode[3];
-    int i, num;
-
-    // Build table
-    for (i = 0; i < 26; i++)
-    {
-        table[i] = 'A' + i;
-        table[i + 26] = 'a' + i;
-    }
-
-    for (i = 0; i < 10; i++)
-        table[i + 52] = '0' + i;
-
-    table[62] = '+';
-    table[63] = '/';
-
-
-    num = strlen (str) / 3;
-    if (strlen (str) % 3 > 0)
-        num++;
-    newstr = g_malloc (num * 4 + 1);
-    newstr[num * 4] = '\0';
-    newpos = newstr;
-
-    pos = str;
-    while (*pos != '\0')
-    {
-        memset (encode, 0, sizeof (encode));
-        for (i = 0; i < 3 && *pos != '\0'; i++)
-            encode[i] = *pos++;
-
-        fillpos = newpos;
-        *newpos++ = table[encode[0] >> 2];
-        *newpos++ = table[(encode[0] & 3)   << 4 | encode[1] >> 4];
-        *newpos++ = table[(encode[1] & 0xF) << 2 | encode[2] >> 6];
-        *newpos++ = table[encode[2] & 0x3F];
-        while (i < 3)
-            fillpos[++i] = '=';
-    }
-    return (newstr);
-}
-*/
-
 gchar *Cddb_Format_Proxy_Authentification (void)
 {
     gchar *ret;
@@ -4191,13 +4138,11 @@ gchar *Cddb_Format_Proxy_Authentification (void)
     if (CDDB_USE_PROXY &&  CDDB_PROXY_USER_NAME != NULL && *CDDB_PROXY_USER_NAME != '\0')
     {
- gchar *tempstr;
+        const gchar *tempstr;
         gchar *str_encoded;
-        gint size;
tempstr = g_strconcat(CDDB_PROXY_USER_NAME, ":", CDDB_PROXY_USER_PASSWORD, NULL);
-        //str_encoded = base64_encode(tempstr);
-        size = base64_encode(tempstr, strlen(tempstr), &str_encoded);
+        str_encoded = g_base64_encode((const guchar *)tempstr, strlen(tempstr));
ret = g_strdup_printf("Proxy-authorization: Basic %s\r\n", str_encoded);
         g_free (str_encoded);
diff --git a/src/ogg_tag.c b/src/ogg_tag.c
index 1fa12a2..e8147c5 100755
--- a/src/ogg_tag.c
+++ b/src/ogg_tag.c
@@ -38,7 +38,6 @@
 #include "et_core.h"
 #include "log.h"
 #include "misc.h"
-#include "base64.h"
 #include "picture.h"
 #include "setting.h"
 #include "charset.h"
@@ -490,8 +489,8 @@ gboolean Ogg_Tag_Read_File_Tag (gchar *filename, File_Tag *FileTag)
     field_num = 0;
     while ( (string = vorbis_comment_query(vc,"COVERART",field_num++)) != NULL )
     {
-        gchar *data;
-        gint size;
+        guchar *data;
+        gsize size;
         Picture *pic;
pic = Picture_Allocate();
@@ -504,11 +503,9 @@ gboolean Ogg_Tag_Read_File_Tag (gchar *filename, File_Tag *FileTag)
         pic->data = NULL;
// Decode picture data
-        data = g_strdup(string);
-        size = base64_decode(string, data);
-        if ( data && (pic->data = g_memdup(data, size)) )
+        data = g_base64_decode(string, &size);
+        if ( data && (pic->data = data) )
             pic->size = size;
-        g_free(data);
if ( (string = vorbis_comment_query(vc,"COVERARTTYPE",field_num-1)) != NULL )
         {
@@ -810,7 +807,6 @@ gboolean Ogg_Tag_Write_File_Tag (ET_File *ETFile)
         if (pic->data)
         {
             gchar *data_encoded = NULL;
-            gint size;
             Picture_Format format = Picture_Format_From_Data(pic);
string = g_strdup_printf("COVERARTMIME=%s",Picture_Mime_Type_String(format));
@@ -831,7 +827,7 @@ gboolean Ogg_Tag_Write_File_Tag (ET_File *ETFile)
                 g_free(string);
             }
- size = base64_encode(pic->data, pic->size, &data_encoded);
+            data_encoded = g_base64_encode(pic->data, pic->size);
             string = g_strdup_printf("COVERART=%s",data_encoded);
             vorbis_comment_add(vc,string);
             g_free(data_encoded);


--
EasyTAG - Tag editor for MP3 and Ogg Vorbis files
http://easytag.sourceforge.net
--
Jerome COUDERC <easytag gmail com>





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