Fixed bug #830: pkg-config support. YAZ installs yaz.pc for Debian
[yaz-moved-to-github.git] / src / siconv.c
index bee184b..1cd81ed 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (C) 1995-2006, Index Data ApS
+ * Copyright (C) 1995-2007, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: siconv.c,v 1.22 2006-04-24 23:21:26 adam Exp $
+ * $Id: siconv.c,v 1.33 2007-01-18 14:45:05 adam Exp $
  */
 /**
  * \file siconv.c
  * is used by YAZ to interface with iconv (if present).
  * For systems where iconv is not present, this layer
  * provides a few important conversions: UTF-8, MARC-8, Latin-1.
+ *
+ * MARC-8 reference:
+ *  http://www.loc.gov/marc/specifications/speccharmarc8.html
  */
 
 #if HAVE_CONFIG_H
 #include <config.h>
 #endif
 
+#include <assert.h>
 #include <errno.h>
 #include <string.h>
 #include <ctype.h>
@@ -29,6 +33,7 @@
 #include <iconv.h>
 #endif
 
+
 #include <yaz/yaz-util.h>
 
 unsigned long yaz_marc8_1_conv(unsigned char *inp, size_t inbytesleft,
@@ -80,6 +85,8 @@ struct yaz_iconv_struct {
     size_t (*write_handle)(yaz_iconv_t cd, unsigned long x,
                            char **outbuf, size_t *outbytesleft,
                            int last);
+    size_t (*flush_handle)(yaz_iconv_t cd,
+                           char **outbuf, size_t *outbytesleft);
     int marc8_esc_mode;
 
     int comb_offset;
@@ -95,6 +102,7 @@ struct yaz_iconv_struct {
 
     unsigned long write_marc8_comb_ch[8];
     size_t write_marc8_comb_no;
+    unsigned write_marc8_second_half_char;
     unsigned long write_marc8_last;
     const char *write_marc8_page_chr;
 };
@@ -178,6 +186,7 @@ static unsigned long yaz_read_ISO8859_1 (yaz_iconv_t cd, unsigned char *inp,
     return x;
 }
 
+
 static size_t yaz_init_UTF8 (yaz_iconv_t cd, unsigned char *inp,
                              size_t inbytesleft, size_t *no_read)
 {
@@ -198,8 +207,9 @@ static size_t yaz_init_UTF8 (yaz_iconv_t cd, unsigned char *inp,
     return 0;
 }
 
-static unsigned long yaz_read_UTF8 (yaz_iconv_t cd, unsigned char *inp,
-                                    size_t inbytesleft, size_t *no_read)
+unsigned long yaz_read_UTF8_char(unsigned char *inp,
+                                 size_t inbytesleft, size_t *no_read,
+                                 int *error)
 {
     unsigned long x = 0;
 
@@ -211,7 +221,7 @@ static unsigned long yaz_read_UTF8 (yaz_iconv_t cd, unsigned char *inp,
     else if (inp[0] <= 0xbf || inp[0] >= 0xfe)
     {
         *no_read = 0;
-        cd->my_errno = YAZ_ICONV_EILSEQ;
+        *error = YAZ_ICONV_EILSEQ;
     }
     else if (inp[0] <= 0xdf && inbytesleft >= 2)
     {
@@ -221,7 +231,7 @@ static unsigned long yaz_read_UTF8 (yaz_iconv_t cd, unsigned char *inp,
         else
         {
             *no_read = 0;
-            cd->my_errno = YAZ_ICONV_EILSEQ;
+            *error = YAZ_ICONV_EILSEQ;
         }
     }
     else if (inp[0] <= 0xef && inbytesleft >= 3)
@@ -233,7 +243,7 @@ static unsigned long yaz_read_UTF8 (yaz_iconv_t cd, unsigned char *inp,
         else
         {
             *no_read = 0;
-            cd->my_errno = YAZ_ICONV_EILSEQ;
+            *error = YAZ_ICONV_EILSEQ;
         }
     }
     else if (inp[0] <= 0xf7 && inbytesleft >= 4)
@@ -245,7 +255,7 @@ static unsigned long yaz_read_UTF8 (yaz_iconv_t cd, unsigned char *inp,
         else
         {
             *no_read = 0;
-            cd->my_errno = YAZ_ICONV_EILSEQ;
+            *error = YAZ_ICONV_EILSEQ;
         }
     }
     else if (inp[0] <= 0xfb && inbytesleft >= 5)
@@ -258,7 +268,7 @@ static unsigned long yaz_read_UTF8 (yaz_iconv_t cd, unsigned char *inp,
         else
         {
             *no_read = 0;
-            cd->my_errno = YAZ_ICONV_EILSEQ;
+            *error = YAZ_ICONV_EILSEQ;
         }
     }
     else if (inp[0] <= 0xfd && inbytesleft >= 6)
@@ -271,17 +281,23 @@ static unsigned long yaz_read_UTF8 (yaz_iconv_t cd, unsigned char *inp,
         else
         {
             *no_read = 0;
-            cd->my_errno = YAZ_ICONV_EILSEQ;
+            *error = YAZ_ICONV_EILSEQ;
         }
     }
     else
     {
         *no_read = 0;
-        cd->my_errno = YAZ_ICONV_EINVAL;
+        *error = YAZ_ICONV_EINVAL;
     }
     return x;
 }
 
+static unsigned long yaz_read_UTF8 (yaz_iconv_t cd, unsigned char *inp,
+                                    size_t inbytesleft, size_t *no_read)
+{
+    return yaz_read_UTF8_char(inp, inbytesleft, no_read, &cd->my_errno);
+}
+
 static unsigned long yaz_read_UCS4 (yaz_iconv_t cd, unsigned char *inp,
                                     size_t inbytesleft, size_t *no_read)
 {
@@ -419,7 +435,7 @@ static unsigned long yaz_read_marc8_comb(yaz_iconv_t cd, unsigned char *inp,
         size_t inbytesleft0 = inbytesleft;
         inp++;
         inbytesleft--;
-        while(inbytesleft > 0 && strchr("(,$!", *inp))
+        while(inbytesleft > 0 && strchr("(,$!)-", *inp))
         {
             inbytesleft--;
             inp++;
@@ -485,9 +501,16 @@ static unsigned long yaz_read_marc8_comb(yaz_iconv_t cd, unsigned char *inp,
     }
 }
 
-static size_t yaz_write_UTF8 (yaz_iconv_t cd, unsigned long x,
-                              char **outbuf, size_t *outbytesleft,
-                              int last)
+static size_t yaz_write_UTF8(yaz_iconv_t cd, unsigned long x,
+                             char **outbuf, size_t *outbytesleft,
+                             int last)
+{
+    return yaz_write_UTF8_char(x, outbuf, outbytesleft, &cd->my_errno);
+}
+
+size_t yaz_write_UTF8_char(unsigned long x,
+                           char **outbuf, size_t *outbytesleft,
+                           int *error)
 {
     unsigned char *outp = (unsigned char *) *outbuf;
 
@@ -538,7 +561,7 @@ static size_t yaz_write_UTF8 (yaz_iconv_t cd, unsigned long x,
     }
     else 
     {
-        cd->my_errno = YAZ_ICONV_E2BIG;  /* not room for output */
+        *error = YAZ_ICONV_E2BIG;  /* not room for output */
         return (size_t)(-1);
     }
     *outbuf = (char *) outp;
@@ -724,7 +747,7 @@ static unsigned long lookup_marc8(yaz_iconv_t cd,
         x = yaz_marc8r_9_conv(inp, inbytesleft, &no_read_sub, comb);
         if (x)
         {
-            *page_chr = "\033(1";
+            *page_chr = "\033$1";
             return x;
         }
         cd->my_errno = YAZ_ICONV_EILSEQ;
@@ -736,7 +759,7 @@ static size_t flush_combos(yaz_iconv_t cd,
                            char **outbuf, size_t *outbytesleft)
 {
     unsigned long y = cd->write_marc8_last;
-    unsigned char byte, second_half = 0;
+    unsigned char byte;
     char out_buf[10];
     size_t i, out_no = 0;
 
@@ -763,31 +786,27 @@ static size_t flush_combos(yaz_iconv_t cd,
     {
         /* all MARC-8 combined characters are simple bytes */
         byte = (unsigned char )(cd->write_marc8_comb_ch[i]);
-        if (byte == 0xEB)
-            second_half = 0xEC;
-        else if (byte == 0xFA)
-            second_half = 0xFB;
-
         *(*outbuf)++ = byte;
         (*outbytesleft)--;
     }
     memcpy(*outbuf, out_buf, out_no);
     *outbuf += out_no;
     (*outbytesleft) -= out_no;
-    if (second_half)
+    if (cd->write_marc8_second_half_char)
     {
-        *(*outbuf)++ = second_half;
+        *(*outbuf)++ = cd->write_marc8_second_half_char;
         (*outbytesleft)--;
     }        
 
     cd->write_marc8_last = 0;
     cd->write_marc8_comb_no = 0;
+    cd->write_marc8_second_half_char = 0;
     return 0;
 }
 
-static size_t yaz_write_marc8(yaz_iconv_t cd, unsigned long x,
-                              char **outbuf, size_t *outbytesleft,
-                              int last)
+static size_t yaz_write_marc8_2(yaz_iconv_t cd, unsigned long x,
+                                char **outbuf, size_t *outbytesleft,
+                                int last)
 {
     int comb = 0;
     const char *page_chr = 0;
@@ -798,27 +817,53 @@ static size_t yaz_write_marc8(yaz_iconv_t cd, unsigned long x,
 
     if (comb)
     {
+        if (x == 0x0361)
+            cd->write_marc8_second_half_char = 0xEC;
+        else if (x == 0x0360)
+            cd->write_marc8_second_half_char = 0xFB;
+
         if (cd->write_marc8_comb_no < 6)
             cd->write_marc8_comb_ch[cd->write_marc8_comb_no++] = y;
     }
     else
     {
         size_t r = flush_combos(cd, outbuf, outbytesleft);
+        const char *old_page_chr = cd->write_marc8_page_chr;
         if (r)
             return r;
-        if (strcmp(page_chr, cd->write_marc8_page_chr))
+        if (strcmp(page_chr, old_page_chr))
         {
-            size_t plen = strlen(page_chr);
+            size_t plen = 0;
+            const char *page_out = page_chr;
 
-            if (*outbytesleft < plen)
+            if (*outbytesleft < 8)
             {
                 cd->my_errno = YAZ_ICONV_E2BIG;
+                
                 return (size_t) (-1);
             }
-            memcpy(*outbuf, page_chr, plen);
+            cd->write_marc8_page_chr = page_chr;
+
+            if (!strcmp(old_page_chr, "\033p") 
+                || !strcmp(old_page_chr, "\033g")
+                || !strcmp(old_page_chr, "\033b"))
+            {
+                /* Technique 1 leave */
+                page_out = "\033s";
+                if (strcmp(page_chr, "\033(B")) /* Not going ASCII page? */
+                {
+                    /* Must leave script + enter new page */
+                    plen = strlen(page_out);
+                    memcpy(*outbuf, page_out, plen);
+                    (*outbuf) += plen;
+                    (*outbytesleft) -= plen;
+                    page_out = page_chr;
+                }
+            }
+            plen = strlen(page_out);
+            memcpy(*outbuf, page_out, plen);
             (*outbuf) += plen;
             (*outbytesleft) -= plen;
-            cd->write_marc8_page_chr = page_chr;            
         }
         cd->write_marc8_last = y;
     }
@@ -837,6 +882,58 @@ static size_t yaz_write_marc8(yaz_iconv_t cd, unsigned long x,
     return 0;
 }
 
+static size_t yaz_flush_marc8(yaz_iconv_t cd,
+                              char **outbuf, size_t *outbytesleft)
+{
+    if (strcmp(cd->write_marc8_page_chr, "\033(B"))
+    {
+        if (*outbytesleft < 3)
+        {
+            cd->my_errno = YAZ_ICONV_E2BIG;
+            return (size_t) (-1);
+        }
+        memcpy(*outbuf, "\033(B", 3);
+        (*outbuf) += 3;
+        *outbytesleft -= 3;
+    }
+    return 0;
+}
+
+static size_t yaz_write_marc8(yaz_iconv_t cd, unsigned long x,
+                              char **outbuf, size_t *outbytesleft,
+                              int last)
+{
+    int i;
+    for (i = 0; latin1_comb[i].x1; i++)
+    {
+        if (x == latin1_comb[i].y)
+        {
+            size_t r ;
+            /* save the output pointers .. */
+            char *outbuf0 = *outbuf;
+            size_t outbytesleft0 = *outbytesleft;
+            int last_ch = cd->write_marc8_last;
+
+            r = yaz_write_marc8_2(cd, latin1_comb[i].x1,
+                                  outbuf, outbytesleft, 0);
+            if (r)
+                return r;
+            r = yaz_write_marc8_2(cd, latin1_comb[i].x2,
+                                  outbuf, outbytesleft, last);
+            if (r && cd->my_errno == YAZ_ICONV_E2BIG)
+            {
+                /* not enough room. reset output to original values */
+                *outbuf = outbuf0;
+                *outbytesleft = outbytesleft0;
+                cd->write_marc8_last = last_ch;
+            }
+            return r;
+        }
+    }
+    return yaz_write_marc8_2(cd, x, outbuf, outbytesleft, last);
+}
+
+
 #if HAVE_WCHAR_H
 static size_t yaz_write_wchar_t (yaz_iconv_t cd, unsigned long x,
                                  char **outbuf, size_t *outbytesleft,
@@ -873,14 +970,8 @@ yaz_iconv_t yaz_iconv_open (const char *tocode, const char *fromcode)
     cd->write_handle = 0;
     cd->read_handle = 0;
     cd->init_handle = 0;
+    cd->flush_handle = 0;
     cd->my_errno = YAZ_ICONV_UNKNOWN;
-    cd->marc8_esc_mode = 'B';
-    cd->comb_offset = cd->comb_size = 0;
-    cd->compose_char = 0;
-
-    cd->write_marc8_comb_no = 0;
-    cd->write_marc8_last = 0;
-    cd->write_marc8_page_chr = "\033(B";
 
     /* a useful hack: if fromcode has leading @,
        the library not use YAZ's own conversions .. */
@@ -917,9 +1008,15 @@ yaz_iconv_t yaz_iconv_open (const char *tocode, const char *fromcode)
         else if (!yaz_matchstr(tocode, "UCS4LE"))
             cd->write_handle = yaz_write_UCS4LE;
         else if (!yaz_matchstr(tocode, "MARC8"))
+        {
             cd->write_handle = yaz_write_marc8;
+            cd->flush_handle = yaz_flush_marc8;
+        }
         else if (!yaz_matchstr(tocode, "MARC8s"))
+        {
             cd->write_handle = yaz_write_marc8;
+            cd->flush_handle = yaz_flush_marc8;
+        }
 #if HAVE_WCHAR_H
         else if (!yaz_matchstr(tocode, "WCHAR_T"))
             cd->write_handle = yaz_write_wchar_t;
@@ -950,7 +1047,7 @@ yaz_iconv_t yaz_iconv_open (const char *tocode, const char *fromcode)
 size_t yaz_iconv(yaz_iconv_t cd, char **inbuf, size_t *inbytesleft,
                  char **outbuf, size_t *outbytesleft)
 {
-    char *inbuf0;
+    char *inbuf0 = 0;
     size_t r = 0;
 
 #if HAVE_ICONV_H
@@ -978,19 +1075,32 @@ size_t yaz_iconv(yaz_iconv_t cd, char **inbuf, size_t *inbytesleft,
         return r;
     }
 #endif
-    if (inbuf == 0 || *inbuf == 0)
+
+    if (inbuf)
+        inbuf0 = *inbuf;
+
+    if (cd->init_flag)
     {
-        cd->init_flag = 1;
         cd->my_errno = YAZ_ICONV_UNKNOWN;
-        return 0;
+        cd->marc8_esc_mode = 'B';
+        
+        cd->comb_offset = cd->comb_size = 0;
+        cd->compose_char = 0;
+        
+        cd->write_marc8_comb_no = 0;
+        cd->write_marc8_second_half_char = 0;
+        cd->write_marc8_last = 0;
+        cd->write_marc8_page_chr = "\033(B";
+        
+        cd->unget_x = 0;
+        cd->no_read_x = 0;
     }
-    inbuf0 = *inbuf;
 
     if (cd->init_flag)
     {
-        if (cd->init_handle)
+        if (cd->init_handle && inbuf && *inbuf)
         {
-            size_t no_read;
+            size_t no_read = 0;
             size_t r = (cd->init_handle)(cd, (unsigned char *) *inbuf,
                                          *inbytesleft, &no_read);
             if (r)
@@ -1003,22 +1113,26 @@ size_t yaz_iconv(yaz_iconv_t cd, char **inbuf, size_t *inbytesleft,
             *inbytesleft -= no_read;
             *inbuf += no_read;
         }
-        cd->init_flag = 0;
-        cd->unget_x = 0;
-        cd->no_read_x = 0;
     }
+    cd->init_flag = 0;
+
     while (1)
     {
         unsigned long x;
         size_t no_read;
 
-        if (*inbytesleft == 0)
+        if (cd->unget_x)
         {
-            r = *inbuf - inbuf0;
-            break;
+            x = cd->unget_x;
+            no_read = cd->no_read_x;
         }
-        if (!cd->unget_x)
+        else if (inbuf && *inbuf)
         {
+            if (*inbytesleft == 0)
+            {
+                r = *inbuf - inbuf0;
+                break;
+            }
             x = (cd->read_handle)(cd, (unsigned char *) *inbuf, *inbytesleft,
                                   &no_read);
             if (no_read == 0)
@@ -1029,8 +1143,12 @@ size_t yaz_iconv(yaz_iconv_t cd, char **inbuf, size_t *inbytesleft,
         }
         else
         {
-            x = cd->unget_x;
-            no_read = cd->no_read_x;
+            r = 0;
+            if (cd->flush_handle && outbuf && *outbuf)
+                r = (*cd->flush_handle)(cd, outbuf, outbytesleft);
+            if (r == 0)
+                cd->init_flag = 1;
+            break;
         }
         if (x)
         {