MARC-8 ANSEL fix and proper better handling of incompl. sequences.
[yaz-moved-to-github.git] / src / siconv.c
index 663d03a..1639759 100644 (file)
@@ -1,8 +1,8 @@
 /*
- * Copyright (C) 1995-2007, Index Data ApS
+ * Copyright (C) 1995-2008, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: siconv.c,v 1.48 2007-10-15 20:45:05 adam Exp $
+ * $Id: siconv.c,v 1.50 2008-03-12 08:53:28 adam Exp $
  */
 /**
  * \file siconv.c
@@ -100,7 +100,8 @@ struct yaz_iconv_struct {
                            char **outbuf, size_t *outbytesleft);
     size_t (*flush_handle)(yaz_iconv_t cd,
                            char **outbuf, size_t *outbytesleft);
-    int marc8_esc_mode;
+    int g0_mode;
+    int g1_mode;
 
     int comb_offset;
     int comb_size;
@@ -1166,27 +1167,48 @@ static unsigned long yaz_read_marc8_comb(yaz_iconv_t cd, unsigned char *inp,
                                          int *comb)
 {
     *no_read = 0;
-    while(inbytesleft >= 1 && inp[0] == 27)
+    while (inbytesleft > 0 && *inp == 27)
     {
+        int *modep = &cd->g0_mode;
         size_t inbytesleft0 = inbytesleft;
-        inp++;
+
         inbytesleft--;
-        while(inbytesleft > 0 && strchr("(,$!)-", *inp))
+        inp++;
+        if (inbytesleft == 0)
+            goto incomplete;
+        if (*inp == '$') /* set with multiple bytes */
         {
             inbytesleft--;
             inp++;
         }
-        if (inbytesleft <= 0)
+        if (inbytesleft == 0)
+            goto incomplete;
+        if (*inp == '(' || *inp == ',')  /* G0 */
         {
-            *no_read = 0;
-            cd->my_errno = YAZ_ICONV_EINVAL;
-            return 0;
+            inbytesleft--;
+            inp++;
+        }
+        else if (*inp == ')' || *inp == '-') /* G1 */
+        {
+            inbytesleft--;
+            inp++;
+            modep = &cd->g1_mode;
         }
-        cd->marc8_esc_mode = *inp++;
+        if (inbytesleft == 0)
+            goto incomplete;
+        if (*inp == '!') /* ANSEL is a special case */
+        {
+            inbytesleft--;
+            inp++;
+        }
+        if (inbytesleft == 0)
+            goto incomplete;
+        *modep = *inp++; /* Final character */
         inbytesleft--;
+
         (*no_read) += inbytesleft0 - inbytesleft;
     }
-    if (inbytesleft <= 0)
+    if (inbytesleft == 0)
         return 0;
     else if (*inp == ' ')
     {
@@ -1197,9 +1219,10 @@ static unsigned long yaz_read_marc8_comb(yaz_iconv_t cd, unsigned char *inp,
     {
         unsigned long x;
         size_t no_read_sub = 0;
+        int mode = *inp < 128 ? cd->g0_mode : cd->g1_mode;
         *comb = 0;
 
-        switch(cd->marc8_esc_mode)
+        switch(mode)
         {
         case 'B':  /* Basic ASCII */
         case 's':  /* ASCII */
@@ -1249,6 +1272,10 @@ static unsigned long yaz_read_marc8_comb(yaz_iconv_t cd, unsigned char *inp,
         *no_read += no_read_sub;
         return x;
     }
+incomplete:
+    *no_read = 0;
+    cd->my_errno = YAZ_ICONV_EINVAL;
+    return 0;
 }
 
 static size_t yaz_write_UTF8(yaz_iconv_t cd, unsigned long x,
@@ -1471,12 +1498,6 @@ static unsigned long lookup_marc8(yaz_iconv_t cd,
             *page_chr = ESC "(B";
             return x;
         }
-        x = yaz_marc8r_67_conv(inp, inbytesleft, &no_read_sub, comb);
-        if (x)
-        {
-            *page_chr = ESC "g";
-            return x;
-        }
         x = yaz_marc8r_62_conv(inp, inbytesleft, &no_read_sub, comb);
         if (x)
         {
@@ -1896,7 +1917,8 @@ size_t yaz_iconv(yaz_iconv_t cd, char **inbuf, size_t *inbytesleft,
     if (cd->init_flag)
     {
         cd->my_errno = YAZ_ICONV_UNKNOWN;
-        cd->marc8_esc_mode = 'B';
+        cd->g0_mode = 'B';
+        cd->g1_mode = 'B';
         
         cd->comb_offset = cd->comb_size = 0;
         cd->compose_char = 0;