Email gateway obeys 'Reply-To:' in header.
[egate.git] / util / iso2709.c
index 62cb8fe..f583f06 100644 (file)
 /*
-   gw-res.c: Iso2709 record management
-
-   Europagate, 1994-1995.
-
-   $Log: iso2709.c,v $
-   Revision 1.1  1995/02/09 17:27:10  adam
-   Initial revision
-
+ * Copyright (c) 1995, the EUROPAGATE consortium (see below).
+ *
+ * The EUROPAGATE consortium members are:
+ *
+ *    University College Dublin
+ *    Danmarks Teknologiske Videnscenter
+ *    An Chomhairle Leabharlanna
+ *    Consejo Superior de Investigaciones Cientificas
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and
+ * its documentation, in whole or in part, for any purpose, is hereby granted,
+ * provided that:
+ *
+ * 1. This copyright and permission notice appear in all copies of the
+ * software and its documentation. Notices of copyright or attribution
+ * which appear at the beginning of any file must remain unchanged.
+ *
+ * 2. The names of EUROPAGATE or the project partners may not be used to
+ * endorse or promote products derived from this software without specific
+ * prior written permission.
+ *
+ * 3. Users of this software (implementors and gateway operators) agree to
+ * inform the EUROPAGATE consortium of their use of the software. This
+ * information will be used to evaluate the EUROPAGATE project and the
+ * software, and to plan further developments. The consortium may use
+ * the information in later publications.
+ * 
+ * 4. Users of this software agree to make their best efforts, when
+ * documenting their use of the software, to acknowledge the EUROPAGATE
+ * consortium, and the role played by the software in their work.
+ *
+ * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+ * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
+ * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
+ * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
+ * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
+ * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
+ * USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+/*
+ * Iso2709 record management
+ *
+ * Europagate, 1994-1995.
+ *
+ * $Log: iso2709.c,v $
+ * Revision 1.17  1995/12/20 16:28:08  adam
+ * Extra parameter block to gw_db_open. If block is 0 gw_db_open
+ * returns NULL if lock couldn't be satisfied.
+ * Minor changes in iso2709.c.
+ *
+ * Revision 1.16  1995/05/16  09:40:53  adam
+ * LICENSE.
+ *
+ * Revision 1.15  1995/03/31  10:42:41  adam
+ * Bug fix.
+ *
+ * Revision 1.14  1995/03/30  14:22:18  adam
+ * More work on new MARC anchor functions.
+ *
+ * Revision 1.13  1995/03/30  07:33:32  adam
+ * New 2709 function: iso2709_mk.
+ * First implementation of iso2709_a_insert.
+ *
+ * Revision 1.12  1995/03/29  16:08:56  adam
+ * Better error recovery when using bad records.
+ *
+ * Revision 1.11  1995/03/28  16:07:07  adam
+ * New function: iso2709_out. This function is the reverse of iso2709_cvt.
+ *
+ * Revision 1.10  1995/03/10  09:10:56  adam
+ * Removed dbc2709_cvt function. Makes heuristic guess for DBC2709 records.
+ *
+ * Revision 1.9  1995/03/08  12:36:39  adam
+ * New function: dbc2709_cvt.
+ *
+ * Revision 1.8  1995/03/08  12:03:15  adam
+ * Hack: When tags 00? are used, every separator (DC[1-3]) marks
+ * the end of the data field.
+ *
+ * Revision 1.7  1995/02/22  21:28:03  adam
+ * Changed header.
+ *
+ * Revision 1.5  1995/02/22  15:24:14  adam
+ * Function iso2709_cvt makes a litte check for the format. It returns
+ * NULL if the buffer parameter can never be a MARC record.
+ *
+ * Revision 1.4  1995/02/15  17:45:44  adam
+ * Bug fix in iso2709 module.
+ *
+ * Revision 1.3  1995/02/10  17:05:18  adam
+ * New function iso2709_display to display MARC records in a
+ * line-by-line format. The iso2709_cvt function no longer
+ * prints the record to stderr.
+ *
+ * Revision 1.2  1995/02/10  16:50:32  adam
+ * Indicator field moved to 'struct iso2709_dir' from 'struct
+ * iso2709_field'.
+ * Function iso2709_rm implemented - to delete a MARC record.
+ *
+ * Revision 1.1.1.1  1995/02/09  17:27:11  adam
+ * Initial version of email gateway under CVS control.
+ *
  */
 
 #include <stdlib.h>
@@ -40,10 +137,6 @@ static void strncpyx (char *d, const char *s, int n)
             s++;
         }
     *d = '\0';
-#if 0
-    strncpy (d, s, n);
-    d[n] = '\0';
-#endif
 }
 
 char *iso2709_read (FILE *inf)
@@ -69,6 +162,29 @@ char *iso2709_read (FILE *inf)
     return buf;
 }
 
+Iso2709Rec iso2709_mk (void)
+{
+    Iso2709Rec p;
+
+    if (!(p = malloc (sizeof(*p))))
+        return NULL;
+
+    p->record_length = 0;
+    strncpyx (p->record_status, " ", 1);
+    strncpyx (p->implementation_codes, "    ", 4);
+    p->indicator_length = 2;
+    p->identifier_length = 2;
+    p->base_address = 0;
+    strncpyx (p->user_systems, "   ", 3);
+    p->length_data_entry = 4;
+    p->length_starting = 5;
+    p->length_implementation = 0;
+    strncpyx (p->future_use, " ", 1);
+
+    p->directory = NULL;
+    return p;
+}
+
 Iso2709Rec iso2709_cvt (const char *buf)
 {
     struct iso2709_dir **dpp, *dp;
@@ -78,6 +194,7 @@ Iso2709Rec iso2709_cvt (const char *buf)
     if (!(p = malloc (sizeof(*p))))
         return NULL;
 
+    /* deal with record label (24 characters) */
     p->record_length = atoin (buf, 5);
     strncpyx (p->record_status, buf+5, 1);
     strncpyx (p->implementation_codes, buf+6, 4);
@@ -86,18 +203,29 @@ Iso2709Rec iso2709_cvt (const char *buf)
     p->base_address = atoin (buf+12, 4);
     strncpyx (p->user_systems, buf+17, 3);
 
+    if (p->record_length < 26)
+    {
+        free (p);
+        return NULL;
+    }
     p->length_data_entry = atoin (buf+20, 1);
     p->length_starting = atoin (buf+21, 1);
     p->length_implementation = atoin (buf+22, 1);
     strncpyx (p->future_use, buf+23, 1);
 
+    /* deal with directory */
     dpp = &p->directory;
-
     *dpp = NULL;
     while (buf[pos] != ISO2709_FS)
     {
-        *dpp = malloc (sizeof(**dpp));
+        if (!(*dpp = malloc (sizeof(**dpp))))
+        {
+            iso2709_rm (p);
+            return NULL;
+        }
         (*dpp)->next = NULL;
+        (*dpp)->fields = NULL;
+        (*dpp)->indicator = NULL;
         strncpyx ((*dpp)->tag, buf+pos, 3);
         pos += 3;
         (*dpp)->length = atoin (buf+pos, p->length_data_entry);
@@ -106,73 +234,130 @@ Iso2709Rec iso2709_cvt (const char *buf)
         pos += p->length_starting + p->length_implementation;
 
         dpp = &(*dpp)->next;
+        if (pos > p->record_length)
+        {
+            iso2709_rm (p);
+            return NULL;
+        }
     }
     pos++;
-#if 0
-    fprintf (stderr, "indicator_len=%d, identifier_len=%d\n", 
-             p->indicator_length, p->identifier_length);
-#endif
+    /* deal with datafields */
     for (dp = p->directory; dp; dp = dp->next)
     {
-        int tag00;
+        int identifier_flag;
         struct iso2709_field **fpp;
         int dpos = pos+dp->offset;
-        char *save_indicator = NULL;
+        int epos = pos+dp->offset+dp->length-1;
 
+        if (epos <= dpos)
+        {
+            iso2709_rm (p);
+            return NULL;
+        }
         fpp = &dp->fields;
 
-        *fpp = malloc (sizeof(**fpp));
+        if (!(*fpp = malloc (sizeof(**fpp))))
+        {
+            iso2709_rm (p);
+            return NULL;
+        }
         (*fpp)->next = NULL;
-        if (p->indicator_length && memcmp (dp->tag, "00", 2))
+
+        identifier_flag = 1;
+        if (p->indicator_length)
         {
-            (*fpp)->indicator = malloc (p->indicator_length+1);
-            strncpyx ((*fpp)->indicator, buf+dpos, p->indicator_length);
+#if WEIRD_ISO_DBC
+            if (buf[dpos+p->indicator_length] != ISO2709_IDFS)
+                identifier_flag = 0;
+#else
+            if (!memcmp (dp->tag, "00", 2))
+                identifier_flag = 0;
+#endif
+        }
+        else if (!memcmp (dp->tag, "00", 2))
+                identifier_flag = 0;
+        if (identifier_flag && p->indicator_length)
+        {
+            if (!(dp->indicator = malloc (p->indicator_length+1)))
+            {
+                iso2709_rm (p);
+                return NULL;
+            }
+            strncpyx (dp->indicator, buf+dpos, p->indicator_length);
             dpos += p->indicator_length;
         }
         else
-            (*fpp)->indicator = NULL;
-
-        if (memcmp (dp->tag, "00", 2))
-            tag00 = 0;
-        else
-            tag00 = 1;
-        fprintf (stderr, "%s", dp->tag);
+            dp->indicator = NULL;
         while (1)
         {
             int dpos_n;
-            if (p->identifier_length && !tag00)
+            if (p->identifier_length && identifier_flag)
             {
-                (*fpp)->identifier = malloc (p->identifier_length+1);
                 strncpyx ((*fpp)->identifier, buf+dpos+1,
                           p->identifier_length-1);
-                dpos += p->identifier_length;
+                dpos_n = dpos += p->identifier_length;
+                while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS
+                       && buf[dpos_n] != ISO2709_IDFS && dpos_n < epos)
+                    dpos_n++;
             }
             else
-                (*fpp)->identifier = NULL;
-
-            dpos_n = dpos;
-            while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_IDFS)
-                dpos_n++;
-
-            (*fpp)->data = malloc (dpos_n - dpos + 1);
+            {
+                *(*fpp)->identifier = '\0';
+                dpos_n = dpos;
+                while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS
+                       && dpos_n < epos)
+                    dpos_n++;
+            }
+            if (!((*fpp)->data = malloc (dpos_n - dpos + 1)))
+            {
+                iso2709_rm (p);
+                return NULL;
+            }
             strncpyx ((*fpp)->data, buf+dpos, dpos_n - dpos);
             dpos = dpos_n;
             
-            if (!save_indicator && (*fpp)->indicator)
-                fprintf (stderr, " %s", (*fpp)->indicator);
-            if ((*fpp)->identifier)
-                fprintf (stderr, " *%s", (*fpp)->identifier);
-            fprintf (stderr, " %s", (*fpp)->data);
-            if (buf[dpos] == ISO2709_FS)
+            if (dpos >= epos)
+            {
+                if (buf[dpos] != ISO2709_FS && buf[dpos] != ISO2709_RS)
+                    fprintf (stderr, "Missing separator at end of field "
+                             "in %s %s\n", dp->tag, (*fpp)->identifier);
                 break;
-            
-            save_indicator = (*fpp)->indicator;
+            }
+            if (buf[dpos] == ISO2709_FS || buf[dpos] == ISO2709_RS)
+            {
+                fprintf (stderr, "Unexpected separator inside field %s %s\n",
+                         dp->tag, (*fpp)->identifier);
+                break;
+            }
             fpp = &(*fpp)->next;
-            *fpp = malloc (sizeof(**fpp));
+            if (!(*fpp = malloc (sizeof(**fpp))))
+            {
+                iso2709_rm (p);
+                return NULL;
+            }
             (*fpp)->next = NULL;
-            (*fpp)->indicator = save_indicator;
         }
-        fprintf (stderr, "\n");
     }
     return p;
 }
+
+void iso2709_rm (Iso2709Rec rec)
+{
+    struct iso2709_dir *dir, *dir1;
+
+    for (dir = rec->directory; dir; dir = dir1)
+    {
+        struct iso2709_field *field, *field1;
+
+        for (field = dir->fields; field; field = field1)
+        {
+            free (field->data);
+            field1 = field->next;
+            free (field);
+        }
+        free (dir->indicator);
+        dir1 = dir->next;
+        free (dir);
+    }
+}
+