Use GLib's implementation of Base64 instead of our own.
[claws.git] / src / ldif.c
index 794f108495689742ba334d6881a40e32f25a8baa..89f750beac677750a82f234df82d75991dbcdc86 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2001-2007 Match Grun and the Claws Mail team
+ * Copyright (C) 2001-2012 Match Grun and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 /*
@@ -32,7 +32,6 @@
 #include "addritem.h"
 #include "addrcache.h"
 
-#include "base64.h"
 #include "utils.h"
 
 #define        LDIF_SEP_TAG    ':'
@@ -47,7 +46,6 @@ LdifFile *ldif_create() {
        ldifFile = g_new0( LdifFile, 1 );
        ldifFile->path = NULL;
        ldifFile->file = NULL;
-       ldifFile->bufptr = ldifFile->buffer;
        ldifFile->hashFields = g_hash_table_new( g_str_hash, g_str_equal );
        ldifFile->tempList = NULL;
        ldifFile->dirtyFlag = TRUE;
@@ -64,7 +62,7 @@ LdifFile *ldif_create() {
  * \param value    Value of access flag.
  */
 void ldif_set_file( LdifFile *ldifFile, const gchar *value ) {
-       g_return_if_fail( ldifFile != NULL );
+       cm_return_if_fail( ldifFile != NULL );
 
        if( ldifFile->path ) {
                if( strcmp( ldifFile->path, value ) != 0 )
@@ -84,7 +82,7 @@ void ldif_set_file( LdifFile *ldifFile, const gchar *value ) {
  * \param value    File specification.
  */
 void ldif_set_accessed( LdifFile *ldifFile, const gboolean value ) {
-       g_return_if_fail( ldifFile != NULL );
+       cm_return_if_fail( ldifFile != NULL );
        ldifFile->accessFlag = value;
 }
 
@@ -124,7 +122,7 @@ static void ldif_free_fieldrec( Ldif_FieldRec *rec ) {
  *              named.
  */
 void ldif_field_set_name( Ldif_FieldRec *rec, const gchar *value ) {
-       g_return_if_fail( rec != NULL );
+       cm_return_if_fail( rec != NULL );
 
        if( ! rec->reserved ) {
                rec->userName = mgu_replace_string( rec->userName, value );
@@ -139,7 +137,7 @@ void ldif_field_set_name( Ldif_FieldRec *rec, const gchar *value ) {
  *              fields cannot be unselected.
  */
 void ldif_field_set_selected( Ldif_FieldRec *rec, const gboolean value ) {
-       g_return_if_fail( rec != NULL );
+       cm_return_if_fail( rec != NULL );
 
        if( ! rec->reserved ) {
                rec->selected = value;
@@ -152,7 +150,7 @@ void ldif_field_set_selected( Ldif_FieldRec *rec, const gboolean value ) {
  * \param rec   LDIF field object.
  */
 void ldif_field_toggle( Ldif_FieldRec *rec ) {
-       g_return_if_fail( rec != NULL );
+       cm_return_if_fail( rec != NULL );
 
        if( ! rec->reserved ) {
                rec->selected = !rec->selected;
@@ -168,8 +166,6 @@ void ldif_field_toggle( Ldif_FieldRec *rec ) {
 */
 static gint ldif_hash_free_vis( gpointer key, gpointer value, gpointer data ) {
        ldif_free_fieldrec( ( Ldif_FieldRec * ) value );
-       value = NULL;
-       key = NULL;
        return -1;
 }
 
@@ -178,7 +174,7 @@ static gint ldif_hash_free_vis( gpointer key, gpointer value, gpointer data ) {
  * \param ldifFile LDIF import control object.
  */
 void ldif_free( LdifFile *ldifFile ) {
-       g_return_if_fail( ldifFile != NULL );
+       cm_return_if_fail( ldifFile != NULL );
 
        /* Close file */
        if( ldifFile->file ) fclose( ldifFile->file );
@@ -210,24 +206,22 @@ void ldif_free( LdifFile *ldifFile ) {
  * \return <i>TRUE</i> if file opened successfully.
  */
 static gint ldif_open_file( LdifFile* ldifFile ) {
-       /* printf( "Opening file\n" ); */
+       /* g_print( "Opening file\n" ); */
        if( ldifFile->path ) {
                ldifFile->file = g_fopen( ldifFile->path, "rb" );
                if( ! ldifFile->file ) {
-                       /* printf( "can't open %s\n", ldifFile->path ); */
+                       /* g_print( "can't open %s\n", ldifFile->path ); */
                        ldifFile->retVal = MGU_OPEN_FILE;
                        return ldifFile->retVal;
                }
        }
        else {
-               /* printf( "file not specified\n" ); */
+               /* g_print( "file not specified\n" ); */
                ldifFile->retVal = MGU_NO_FILE;
                return ldifFile->retVal;
        }
 
        /* Setup a buffer area */
-       ldifFile->buffer[0] = '\0';
-       ldifFile->bufptr = ldifFile->buffer;
        ldifFile->retVal = MGU_SUCCESS;
        return ldifFile->retVal;
 }
@@ -237,7 +231,7 @@ static gint ldif_open_file( LdifFile* ldifFile ) {
  * \param  ldifFile LDIF import control object.
  */
 static void ldif_close_file( LdifFile *ldifFile ) {
-       g_return_if_fail( ldifFile != NULL );
+       cm_return_if_fail( ldifFile != NULL );
        if( ldifFile->file ) fclose( ldifFile->file );
        ldifFile->file = NULL;
 }
@@ -248,14 +242,17 @@ static void ldif_close_file( LdifFile *ldifFile ) {
  * \return ptr to buffer where line starts.
  */
 static gchar *ldif_get_line( LdifFile *ldifFile ) {
-       gchar buf[ LDIFBUFSIZE ];
+       gchar *buf = g_malloc(LDIFBUFSIZE);
        gint ch;
        int i = 0;
+       int cur_alloc = LDIFBUFSIZE;
 
-       if( feof( ldifFile->file ) ) 
+       if( feof( ldifFile->file ) ) {
+               g_free(buf);
                return NULL;
+       }
 
-       while( i < LDIFBUFSIZE-1 ) {
+       while( i < cur_alloc-1 ) {
                ch = fgetc( ldifFile->file );
                if (ferror( ldifFile->file ))
                        ldifFile->retVal = MGU_ERROR_READ;
@@ -272,6 +269,10 @@ static gchar *ldif_get_line( LdifFile *ldifFile ) {
                        break;
                buf[i] = ch;
                i++;
+               if (i == cur_alloc-1 && cur_alloc < LDIFBUFSIZE * 32) {
+                       cur_alloc += LDIFBUFSIZE;
+                       buf = g_realloc(buf, cur_alloc);
+               }
        }
        buf[i] = '\0';
 
@@ -291,7 +292,7 @@ static gchar *ldif_get_tagname( char* line, gboolean *flag64 ) {
        gchar *tag = NULL;
        gchar *lptr = line;
        gchar *sptr = NULL;
-
+       
        while( *lptr++ ) {
                /* Check for language tag */
                if( *lptr == LDIF_LANG_TAG ) {
@@ -312,8 +313,7 @@ static gchar *ldif_get_tagname( char* line, gboolean *flag64 ) {
 
                        tag = g_strndup( line, len+1 );
                        tag[ len ] = '\0';
-                       g_strdown( tag );
-                       return tag;
+                        return tag;
                }
        }
        return tag;
@@ -394,7 +394,7 @@ static void ldif_build_items(
        ItemEMail *email;
 
        nodeAddress = rec->listAddress;
-       if( nodeAddress == NULL ) return;
+//     if( nodeAddress == NULL ) return;
 
        /* Find longest first name in list */
        nodeFirst = rec->listFName;
@@ -523,8 +523,7 @@ static void ldif_add_value(
 {
        gchar *nm, *val;
 
-       nm = g_strdup( tagName );
-       g_strdown( nm );
+       nm = g_utf8_strdown( tagName, -1 );
        if( tagValue ) {
                val = g_strdup( tagValue );
        }
@@ -533,19 +532,19 @@ static void ldif_add_value(
        }
        g_strstrip( val );
 
-       if( g_utf8_collate( nm, LDIF_TAG_COMMONNAME ) == 0 ) {
+       if( g_utf8_collate( nm, g_utf8_strdown( LDIF_TAG_COMMONNAME, -1 ) ) == 0 ) {
                rec->listCName = g_slist_append( rec->listCName, val );
        }
-       else if( g_utf8_collate( nm, LDIF_TAG_FIRSTNAME ) == 0 ) {
+       else if( g_utf8_collate( nm, g_utf8_strdown( LDIF_TAG_FIRSTNAME, -1 ) ) == 0 ) {
                rec->listFName = g_slist_append( rec->listFName, val );
        }
-       else if( g_utf8_collate( nm, LDIF_TAG_LASTNAME ) == 0 ) {
+       else if( g_utf8_collate( nm, g_utf8_strdown( LDIF_TAG_LASTNAME, -1 ) ) == 0 ) {
                rec->listLName = g_slist_append( rec->listLName, val );
        }
-       else if( g_utf8_collate( nm, LDIF_TAG_NICKNAME ) == 0 ) {
+       else if( g_utf8_collate( nm, g_utf8_strdown( LDIF_TAG_NICKNAME, -1 ) ) == 0 ) {
                rec->listNName = g_slist_append( rec->listNName, val );
        }
-       else if( g_utf8_collate( nm, LDIF_TAG_EMAIL ) == 0 ) {
+       else if( g_utf8_collate( nm, g_utf8_strdown( LDIF_TAG_EMAIL, -1 ) ) == 0 ) {
                rec->listAddress = g_slist_append( rec->listAddress, val );
        }
        else {
@@ -608,6 +607,8 @@ static void ldif_read_file( LdifFile *ldifFile, AddressCache *cache ) {
        long posEnd = 0L;
        long posCur = 0L;
        GHashTable *hashField;
+       gchar *out;
+       gsize len;
 
        hashField = ldifFile->hashFields;
        rec = g_new0( Ldif_ParsedRec, 1 );
@@ -641,10 +642,8 @@ static void ldif_read_file( LdifFile *ldifFile, AddressCache *cache ) {
                                /* Save record */
                                fullValue = mgu_list_coalesce( listValue );
                                if (fullValue && last64) {
-                                       gchar *out = g_malloc(strlen(fullValue));
-                                       int len = 0;
-                                       if ((len = base64_decode(out, fullValue,
-                                                       strlen(fullValue))) >= 0) {
+                                       out = g_base64_decode(fullValue, &len);
+                                       if (len >= 0) {
                                                g_free(fullValue);
                                                fullValue = out;
                                                fullValue[len] = '\0';
@@ -692,10 +691,8 @@ static void ldif_read_file( LdifFile *ldifFile, AddressCache *cache ) {
                                                        fullValue =
                                                                mgu_list_coalesce( listValue );
                                                        if (fullValue && last64) {
-                                                               gchar *out = g_malloc(strlen(fullValue));
-                                                               int len = 0;
-                                                               if ((len = base64_decode(out, fullValue,
-                                                                               strlen(fullValue))) >= 0) {
+                                                               out = g_base64_decode(fullValue, &len);
+                                                               if (len >= 0) {
                                                                        g_free(fullValue);
                                                                        fullValue = out;
                                                                        fullValue[len] = '\0';
@@ -716,7 +713,6 @@ static void ldif_read_file( LdifFile *ldifFile, AddressCache *cache ) {
                                                        mgu_free_list( listValue );
                                                        lastTag = NULL;
                                                        listValue = NULL;
-                                                       last64 = FALSE;
                                                }
 
                                                lastTag = g_strdup( tagName );
@@ -753,30 +749,30 @@ static void ldif_hash_add_list( GHashTable *table, GSList *list ) {
                gchar *tag = node->data;
                if( ! g_hash_table_lookup( table, tag ) ) {
                        Ldif_FieldRec *rec = NULL;
-                       gchar *key = g_strdup( tag );
+                       gchar *key = g_utf8_strdown( tag, -1 );
 
                        rec = ldif_create_fieldrec( tag );
-                       if( g_utf8_collate( tag, LDIF_TAG_DN ) == 0 ) {
+                       if( g_utf8_collate( key, LDIF_TAG_DN ) == 0 ) {
                                rec->reserved = rec->selected = TRUE;
                                rec->userName = g_strdup( "dn" );
                        }
-                       else if( g_utf8_collate( tag, LDIF_TAG_COMMONNAME ) == 0 ) {
+                       else if( g_utf8_collate( key, g_utf8_strdown( LDIF_TAG_COMMONNAME, -1 ) ) == 0 ) {
                                rec->reserved = rec->selected = TRUE;
                                rec->userName = g_strdup( _( "Display Name" ) );
                        }
-                       else if( g_utf8_collate( tag, LDIF_TAG_FIRSTNAME ) == 0 ) {
+                       else if( g_utf8_collate( key, g_utf8_strdown( LDIF_TAG_FIRSTNAME, -1 ) ) == 0 ) {
                                rec->reserved = rec->selected = TRUE;
                                rec->userName = g_strdup( _( "First Name" ) );
                        }
-                       else if( g_utf8_collate( tag, LDIF_TAG_LASTNAME ) == 0 ) {
+                       else if( g_utf8_collate( key, g_utf8_strdown( LDIF_TAG_LASTNAME, -1 ) ) == 0 ) {
                                rec->reserved = rec->selected = TRUE;
                                rec->userName = g_strdup( _( "Last Name" ) );
                        }
-                       else if( g_utf8_collate( tag, LDIF_TAG_NICKNAME ) == 0 ) {
+                       else if( g_utf8_collate( key, g_utf8_strdown( LDIF_TAG_NICKNAME, -1 ) ) == 0 ) {
                                rec->reserved = rec->selected = TRUE;
                                rec->userName = g_strdup( _( "Nick Name" ) );
                        }
-                       else if( g_utf8_collate( tag, LDIF_TAG_EMAIL ) == 0 ) {
+                       else if( g_utf8_collate( key, g_utf8_strdown( LDIF_TAG_EMAIL, -1 ) ) == 0 ) {
                                rec->reserved = rec->selected = TRUE;
                                rec->userName = g_strdup( _( "Email Address" ) );
                        }
@@ -920,7 +916,7 @@ static void ldif_read_tag_list( LdifFile *ldifFile ) {
  * \return Status code.
  */
 gint ldif_import_data( LdifFile *ldifFile, AddressCache *cache ) {
-       g_return_val_if_fail( ldifFile != NULL, MGU_BAD_ARGS );
+       cm_return_val_if_fail( ldifFile != NULL, MGU_BAD_ARGS );
        ldifFile->retVal = MGU_SUCCESS;
        addrcache_clear( cache );
        cache->dataRead = FALSE;
@@ -944,7 +940,7 @@ gint ldif_import_data( LdifFile *ldifFile, AddressCache *cache ) {
  * \return Status code.
  */
 gint ldif_read_tags( LdifFile *ldifFile ) {
-       g_return_val_if_fail( ldifFile != NULL, MGU_BAD_ARGS );
+       cm_return_val_if_fail( ldifFile != NULL, MGU_BAD_ARGS );
        ldifFile->retVal = MGU_SUCCESS;
        if( ldifFile->dirtyFlag ) {
                ldif_open_file( ldifFile );
@@ -970,7 +966,7 @@ gint ldif_read_tags( LdifFile *ldifFile ) {
 GList *ldif_get_fieldlist( LdifFile *ldifFile ) {
        GList *list = NULL;
 
-       g_return_val_if_fail( ldifFile != NULL, NULL );
+       cm_return_val_if_fail( ldifFile != NULL, NULL );
        if( ldifFile->hashFields ) {
                ldifFile->tempList = NULL;
                g_hash_table_foreach( ldifFile->hashFields, ldif_hash2list_vis, ldifFile );