X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=util%2Fiso2709.c;h=d79e42283760c28482a2c7807d0e6c224735c3f3;hb=b9bf72fa7af887225b50f567f91c06ce4353d0c8;hp=812f8da51228f69b8fde1575715f086a065e57dd;hpb=39d29539fcda6555089fe3a581953829af06ac7e;p=egate.git diff --git a/util/iso2709.c b/util/iso2709.c index 812f8da..d79e422 100644 --- a/util/iso2709.c +++ b/util/iso2709.c @@ -4,7 +4,23 @@ * Europagate, 1994-1995. * * $Log: iso2709.c,v $ - * Revision 1.10 1995/03/10 09:10:56 adam + * 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 @@ -70,10 +86,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) @@ -99,6 +111,28 @@ 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) { @@ -133,9 +167,14 @@ Iso2709Rec iso2709_cvt (const char *buf) *dpp = NULL; while (buf[pos] != ISO2709_FS) { - *dpp = malloc (sizeof(**dpp)); - assert (*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); @@ -144,6 +183,11 @@ 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++; /* deal with datafields */ @@ -152,11 +196,20 @@ Iso2709Rec iso2709_cvt (const char *buf) int identifier_flag; struct iso2709_field **fpp; int dpos = pos+dp->offset; + int epos = pos+dp->offset+dp->length-1; + if (epos <= dpos) + { + iso2709_rm (p); + return NULL; + } fpp = &dp->fields; - *fpp = malloc (sizeof(**fpp)); - assert (*fpp); + if (!(*fpp = malloc (sizeof(**fpp)))) + { + iso2709_rm (p); + return NULL; + } (*fpp)->next = NULL; identifier_flag = 1; @@ -174,8 +227,11 @@ Iso2709Rec iso2709_cvt (const char *buf) identifier_flag = 0; if (identifier_flag && p->indicator_length) { - dp->indicator = malloc (p->indicator_length+1); - assert (dp->indicator); + 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; } @@ -186,32 +242,48 @@ Iso2709Rec iso2709_cvt (const char *buf) int dpos_n; if (p->identifier_length && identifier_flag) { - (*fpp)->identifier = malloc (p->identifier_length+1); strncpyx ((*fpp)->identifier, buf+dpos+1, p->identifier_length-1); dpos_n = dpos += p->identifier_length; while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS - && buf[dpos_n] != ISO2709_IDFS) + && buf[dpos_n] != ISO2709_IDFS && dpos_n < epos) dpos_n++; } else { - (*fpp)->identifier = NULL; + *(*fpp)->identifier = '\0'; dpos_n = dpos; - while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS) + while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS + && dpos_n < epos) dpos_n++; } - (*fpp)->data = malloc (dpos_n - dpos + 1); - assert ((*fpp)->data); + 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 (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; + } 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)); - assert (*fpp); + if (!(*fpp = malloc (sizeof(**fpp)))) + { + iso2709_rm (p); + return NULL; + } (*fpp)->next = NULL; } } @@ -228,7 +300,6 @@ void iso2709_rm (Iso2709Rec rec) for (field = dir->fields; field; field = field1) { - free (field->identifier); free (field->data); field1 = field->next; free (field); @@ -238,3 +309,4 @@ void iso2709_rm (Iso2709Rec rec) free (dir); } } +