gnokii-commit
[Top][All Lists]
Advanced

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

gnokii/common gsm-encoding.c,1.48,1.49 ldif.c,1.7,1.8


From: BORBELY Zoltan <address@hidden>
Subject: gnokii/common gsm-encoding.c,1.48,1.49 ldif.c,1.7,1.8
Date: Mon, 20 Oct 2003 00:42:25 +0000

Update of /cvsroot/gnokii/gnokii/common
In directory subversions:/tmp/cvs-serv13892/common

Modified Files:
        gsm-encoding.c ldif.c 
Log Message:
utf8, base64 and LDIF export fix


Index: ldif.c
===================================================================
RCS file: /cvsroot/gnokii/gnokii/common/ldif.c,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** ldif.c      22 Sep 2003 19:37:36 -0000      1.7
--- ldif.c      20 Oct 2003 00:42:22 -0000      1.8
***************
*** 38,49 ****
  #include "gnokii-internal.h"
  
! /* a utility function to reuse code */
  static int ldif_entry_write(FILE *f, const char *parameter, const char 
*value, int convertToUTF8)
  {
!       char *buf = NULL;
  
        if (string_base64(value)) {
!               base64_encode(buf, value, convertToUTF8);
                fprintf(f, "%s:: %s\n", parameter, buf);
        } else {
                fprintf(f, "%s: %s\n", parameter, value);
--- 38,69 ----
  #include "gnokii-internal.h"
  
! /* write the string value to the file and convert it to base64 if necessary
!    convertToUTF8 is necessary for Mozilla, but might not be appropriate for
!    other programs that use the LDIF format, base64 encoding is compliant with 
the LDIF format
! */
  static int ldif_entry_write(FILE *f, const char *parameter, const char 
*value, int convertToUTF8)
  {
!       char *buf;
!       int n, inlen, buflen;
  
        if (string_base64(value)) {
!               inlen = strlen(value);
!               buf = NULL;
!               n = 3 * inlen;
! 
!               do {
!                       buflen = n;
!                       if (buf) free(buf);
!                       buf = malloc(buflen + 1);
!                       if (convertToUTF8)
!                               n = utf8_base64_encode(buf, buflen, value, 
inlen);
!                       else
!                               n = base64_encode(buf, buflen, value, inlen);
!               } while (n >= buflen);
! 
                fprintf(f, "%s:: %s\n", parameter, buf);
+ 
+               free(buf);
+ 
        } else {
                fprintf(f, "%s: %s\n", parameter, value);
***************
*** 57,60 ****
--- 77,83 ----
        int i;
  
+       /* this works for the import, but mozilla exports strings that contain 
chars >= 0x80 as
+               dn:: <utf-8+base64 encoded: cn=entryname>
+       */
        fprintf(f, "dn: cn=%s\n", entry->name);
        fprintf(f, "objectclass: top\n");
***************
*** 119,123 ****
  #define BEGINS(a) ( !strncmp(buf, a, strlen(a)) )
  #define STORE2(a, b, c) if (BEGINS(a)) { c; strncpy(b, buf+strlen(a), 
strlen(buf)-strlen(a)-1); continue; }
! #define STORE2_BASE64(a, b, c) if (BEGINS(a)) { c; base64_decode(b, 
buf+strlen(a), strlen(buf)-strlen(a)-1); continue; }
  
  #define STORE(a, b) STORE2(a, b, (void) 0)
--- 142,146 ----
  #define BEGINS(a) ( !strncmp(buf, a, strlen(a)) )
  #define STORE2(a, b, c) if (BEGINS(a)) { c; strncpy(b, buf+strlen(a), 
strlen(buf)-strlen(a)-1); continue; }
! #define STORE2_BASE64(a, b, c) if (BEGINS(a)) { c; utf8_base64_decode(b, 
GN_PHONEBOOK_NAME_MAX_LENGTH, buf+strlen(a), strlen(buf)-strlen(a)-1); 
continue; }
  
  #define STORE(a, b) STORE2(a, b, (void) 0)

Index: gsm-encoding.c
===================================================================
RCS file: /cvsroot/gnokii/gnokii/common/gsm-encoding.c,v
retrieving revision 1.48
retrieving revision 1.49
diff -C2 -d -r1.48 -r1.49
*** gsm-encoding.c      22 Sep 2003 20:10:30 -0000      1.48
--- gsm-encoding.c      20 Oct 2003 00:42:22 -0000      1.49
***************
*** 361,367 ****
        buf[4] = '\0';
        for (i_len = 0; i_len < len ; i_len++) {
!               buf[0] = *(src + i_len * 4); 
                buf[1] = *(src + i_len * 4 + 1);
!               buf[2] = *(src + i_len * 4 + 2); 
                buf[3] = *(src + i_len * 4 + 3);
                switch (length = char_uni_alphabet_decode(strtol(buf, NULL, 
16), dest + o_len)) {
--- 361,367 ----
        buf[4] = '\0';
        for (i_len = 0; i_len < len ; i_len++) {
!               buf[0] = *(src + i_len * 4);
                buf[1] = *(src + i_len * 4 + 1);
!               buf[2] = *(src + i_len * 4 + 2);
                buf[3] = *(src + i_len * 4 + 3);
                switch (length = char_uni_alphabet_decode(strtol(buf, NULL, 
16), dest + o_len)) {
***************
*** 382,386 ****
        wchar_t wc;
        int i_len = 0, o_len, length;
!  
        for (o_len = 0; i_len < len ; o_len++) {
                switch (length = char_uni_alphabet_encode(src + i_len, &wc)) {
--- 382,386 ----
        wchar_t wc;
        int i_len = 0, o_len, length;
! 
        for (o_len = 0; i_len < len ; o_len++) {
                switch (length = char_uni_alphabet_encode(src + i_len, &wc)) {
***************
*** 392,396 ****
                        break;
                }
!               sprintf(dest + (o_len << 2), "%lx", wc); 
        }
        return;
--- 392,396 ----
                        break;
                }
!               sprintf(dest + (o_len << 2), "%lx", wc);
        }
        return;
***************
*** 552,585 ****
  
  /* UTF-8 conversion functions */
! int utf8_decode(char *outstring, const char *instring, int inlen)
  {
!       int outlen = inlen;
  #ifdef HAVE_ICONV
        iconv_t cd;
!       char *iter = (char *)instring;
  
        cd = iconv_open(nl_langinfo(CODESET), "UTF-8");
!       outlen = iconv(cd, &iter, (size_t *)&inlen, &outstring, (size_t 
*)&outlen);
        iconv_close(cd);
!       outstring[outlen] = 0;
  #endif
!       return outlen;
  }
  
! int utf8_encode(char *outstring, const char *instring, int inlen)
  {
!       int aux = inlen;
!       int outlen = inlen * 2;
!       int len = 0;
  #ifdef HAVE_ICONV
        iconv_t cd;
-       char *iter = (char *)instring;
  
        cd = iconv_open("UTF-8", nl_langinfo(CODESET));
!       len = iconv(cd, &iter, &inlen, &outstring, &outlen);
        iconv_close(cd);
  #endif
!       if (len < 0) return -1;
!       else return 2 * aux - outlen;
  }
  
--- 552,598 ----
  
  /* UTF-8 conversion functions */
! int utf8_decode(char *outstring, int outlen, const char *instring, int inlen)
  {
!       size_t nconv;
!       char *pin, *pout;
! 
  #ifdef HAVE_ICONV
        iconv_t cd;
! 
!       pin = (char *)instring;
!       pout = outstring;
  
        cd = iconv_open(nl_langinfo(CODESET), "UTF-8");
!       nconv = iconv(cd, &pin, &inlen, &pout, &outlen);
        iconv_close(cd);
!       *pout = 0;
! #else
! #     error "iconv missing"
  #endif
!       return (nconv < 0) ?  -1 : pout - outstring;
  }
  
! int utf8_encode(char *outstring, int outlen, const char *instring, int inlen)
  {
!       size_t outleft, inleft, nconv;
!       char *pin, *pout;
! 
!       outleft = outlen;
!       inleft = inlen;
!       pin = (char *)instring;
!       pout = outstring;
! 
  #ifdef HAVE_ICONV
        iconv_t cd;
  
        cd = iconv_open("UTF-8", nl_langinfo(CODESET));
! 
!       nconv = iconv(cd, &pin, &inleft, &pout, &outleft);
!       *pout = 0;
        iconv_close(cd);
+ #else
+ #     error "iconv is missing"
  #endif
!       return (nconv < 0) ?  -1 : pout - outstring;
  }
  
***************
*** 596,621 ****
  /*
     encodes a null-terminated input string with base64 encoding.
-    convertToUTF8 is necessary for Mozilla, but might not be appropriate for
-    other programs that use the LDIF format, base64 encoding is compliant with 
the LDIF format
     the buffer outstring needs to be at least 1.333 times bigger than the 
input string length
-    *outlen contains the actual length of the converted string
  */
! int base64_encode(char *outstring, const char *instring, int convertToUTF8)
  {
        char *in1, *pin, *pout;
        char *outtemp = NULL;
!       int inleft, inlen, outleft, inprocessed, outlen;
        unsigned int i1, i2, i3, i4;
  
-       /* convert to UTF-8 if necessary */
-       if (convertToUTF8) {
-               inlen = strlen(instring);
-               outtemp = malloc(inlen);
-               outlen = utf8_encode(outtemp, instring, inlen);
-               instring = outtemp;
-               inlen = outlen;
-       } else {
-               inlen = strlen(instring);
-       }
  
        /* copy the input string, need multiple of 3 chars */
--- 609,621 ----
  /*
     encodes a null-terminated input string with base64 encoding.
     the buffer outstring needs to be at least 1.333 times bigger than the 
input string length
  */
! int base64_encode(char *outstring, int outlen, const char *instring, int 
inlen)
  {
        char *in1, *pin, *pout;
        char *outtemp = NULL;
!       int inleft, outleft, inprocessed;
        unsigned int i1, i2, i3, i4;
  
  
        /* copy the input string, need multiple of 3 chars */
***************
*** 627,631 ****
        inleft = inlen;
        outleft = outlen;
-       outleft = 1000;
        inprocessed = 0;
        pin = in1;
--- 627,630 ----
***************
*** 662,669 ****
                /* update the counters */
                inprocessed += 3;
-               outlen += 4;
                outleft -= 4;
        }
  
        if (outtemp)
                free(outtemp);
--- 661,668 ----
                /* update the counters */
                inprocessed += 3;
                outleft -= 4;
        }
  
+       if (outleft > 0) *pout = 0;
        if (outtemp)
                free(outtemp);
***************
*** 671,678 ****
        free(in1);
  
!       return 1;
  }
  
! int base64_decode(char *dest, const char *source, int length)
  {
        int dtable[256];
--- 670,677 ----
        free(in1);
  
!       return pout - outstring;
  }
  
! int base64_decode(char *dest, int destlen, const char *source, int inlen)
  {
        int dtable[256];
***************
*** 680,686 ****
        int dpos = 0;
        int spos = 0;
-       char *aux;
  
-       aux = calloc(length, sizeof(char));
        for (i = 0; i < 255; i++) {
                dtable[i] = 0x80;
--- 679,683 ----
***************
*** 704,708 ****
  
                for (i = 0; i < 4; i++) {
!                       if (spos >= length) {
                                goto endloop;
                        }
--- 701,705 ----
  
                for (i = 0; i < 4; i++) {
!                       if (spos >= inlen || dpos >= destlen) {
                                goto endloop;
                        }
***************
*** 727,734 ****
                o[2] = (b[2] << 6) | b[3];
                i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
!               if (i >= 1) aux[dpos++] = o[0];
!               if (i >= 2) aux[dpos++] = o[1];
!               if (i >= 3) aux[dpos++] = o[2];
!               aux[dpos] = 0;
                if (i < 3) {
                        goto endloop;
--- 724,731 ----
                o[2] = (b[2] << 6) | b[3];
                i = a[2] == '=' ? 1 : (a[3] == '=' ? 2 : 3);
!               if (i >= 1) dest[dpos++] = o[0];
!               if (i >= 2) dest[dpos++] = o[1];
!               if (i >= 3) dest[dpos++] = o[2];
!               dest[dpos] = 0;
                if (i < 3) {
                        goto endloop;
***************
*** 736,740 ****
        }
  endloop:
!       retval = utf8_decode(dest, aux, dpos);
        free(aux);
        return retval;
--- 733,765 ----
        }
  endloop:
!       return dpos;
! }
! 
! int utf8_base64_encode(char *dest, int destlen, const char *in, int inlen)
! {
!       char *aux;
!       int retval;
! 
!       aux = calloc(destlen + 1, sizeof(char));
! 
!       retval = utf8_encode(aux, destlen, in, inlen);
!       if (retval >= 0)
!               retval = base64_encode(dest, destlen, aux, retval);
! 
!       free(aux);
!       return retval;
! }
! 
! int utf8_base64_decode(char *dest, int destlen, const char *in, int inlen)
! {
!       char *aux;
!       int retval;
! 
!       aux = calloc(destlen + 1, sizeof(char));
! 
!       retval = base64_decode(aux, destlen, in, inlen);
!       if (retval >= 0)
!               retval = utf8_decode(dest, destlen, aux, retval);
! 
        free(aux);
        return retval;





reply via email to

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