--- /dev/null
+/*
+ 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
+
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+#include <ctype.h>
+
+#include <iso2709p.h>
+
+static int atoin (const char *buf, int n)
+{
+ int val = 0;
+ while (--n >= 0)
+ {
+ if (isdigit(*buf))
+ val = val*10 + (*buf - '0');
+ buf++;
+ }
+ return val;
+}
+
+static void strncpyx (char *d, const char *s, int n)
+{
+ while (--n >= 0 && *s)
+ if (*s != ISO2709_IDFS)
+ *d++ = *s++;
+ else
+ {
+ *d++ = ' ';
+ s++;
+ }
+ *d = '\0';
+#if 0
+ strncpy (d, s, n);
+ d[n] = '\0';
+#endif
+}
+
+char *iso2709_read (FILE *inf)
+{
+ char length_str[5];
+ int size;
+ char *buf;
+
+ if (fread (length_str, 1, 5, inf) != 5)
+ return NULL;
+ size = atoin (length_str, 5);
+ if (size <= 6)
+ return NULL;
+ if (!(buf = malloc (size+1)))
+ return NULL;
+ if (fread (buf+5, 1, size-5, inf) != (size-5))
+ {
+ free (buf);
+ return NULL;
+ }
+ memcpy (buf, length_str, 5);
+ buf[size] = '\0';
+ return buf;
+}
+
+Iso2709Rec iso2709_cvt (const char *buf)
+{
+ struct iso2709_dir **dpp, *dp;
+ int pos = 24;
+ Iso2709Rec p;
+
+ if (!(p = malloc (sizeof(*p))))
+ return NULL;
+
+ p->record_length = atoin (buf, 5);
+ strncpyx (p->record_status, buf+5, 1);
+ strncpyx (p->implementation_codes, buf+6, 4);
+ p->indicator_length = atoin (buf+10, 1);
+ p->identifier_length = atoin (buf+11, 1);
+ p->base_address = atoin (buf+12, 4);
+ strncpyx (p->user_systems, buf+17, 3);
+
+ 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);
+
+ dpp = &p->directory;
+
+ *dpp = NULL;
+ while (buf[pos] != ISO2709_FS)
+ {
+ *dpp = malloc (sizeof(**dpp));
+ (*dpp)->next = NULL;
+ strncpyx ((*dpp)->tag, buf+pos, 3);
+ pos += 3;
+ (*dpp)->length = atoin (buf+pos, p->length_data_entry);
+ pos += p->length_data_entry;
+ (*dpp)->offset = atoin (buf+pos, p->length_starting);
+ pos += p->length_starting + p->length_implementation;
+
+ dpp = &(*dpp)->next;
+ }
+ pos++;
+#if 0
+ fprintf (stderr, "indicator_len=%d, identifier_len=%d\n",
+ p->indicator_length, p->identifier_length);
+#endif
+ for (dp = p->directory; dp; dp = dp->next)
+ {
+ int tag00;
+ struct iso2709_field **fpp;
+ int dpos = pos+dp->offset;
+ char *save_indicator = NULL;
+
+ fpp = &dp->fields;
+
+ *fpp = malloc (sizeof(**fpp));
+ (*fpp)->next = NULL;
+ if (p->indicator_length && memcmp (dp->tag, "00", 2))
+ {
+ (*fpp)->indicator = malloc (p->indicator_length+1);
+ strncpyx ((*fpp)->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);
+ while (1)
+ {
+ int dpos_n;
+ if (p->identifier_length && !tag00)
+ {
+ (*fpp)->identifier = malloc (p->identifier_length+1);
+ strncpyx ((*fpp)->identifier, buf+dpos+1,
+ p->identifier_length-1);
+ dpos += p->identifier_length;
+ }
+ 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);
+ 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)
+ break;
+
+ save_indicator = (*fpp)->indicator;
+ fpp = &(*fpp)->next;
+ *fpp = malloc (sizeof(**fpp));
+ (*fpp)->next = NULL;
+ (*fpp)->indicator = save_indicator;
+ }
+ fprintf (stderr, "\n");
+ }
+ return p;
+}