LICENSE.
[egate.git] / util / iso2709.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  */
44 /*
45  * Iso2709 record management
46  *
47  * Europagate, 1994-1995.
48  *
49  * $Log: iso2709.c,v $
50  * Revision 1.16  1995/05/16 09:40:53  adam
51  * LICENSE.
52  *
53  * Revision 1.15  1995/03/31  10:42:41  adam
54  * Bug fix.
55  *
56  * Revision 1.14  1995/03/30  14:22:18  adam
57  * More work on new MARC anchor functions.
58  *
59  * Revision 1.13  1995/03/30  07:33:32  adam
60  * New 2709 function: iso2709_mk.
61  * First implementation of iso2709_a_insert.
62  *
63  * Revision 1.12  1995/03/29  16:08:56  adam
64  * Better error recovery when using bad records.
65  *
66  * Revision 1.11  1995/03/28  16:07:07  adam
67  * New function: iso2709_out. This function is the reverse of iso2709_cvt.
68  *
69  * Revision 1.10  1995/03/10  09:10:56  adam
70  * Removed dbc2709_cvt function. Makes heuristic guess for DBC2709 records.
71  *
72  * Revision 1.9  1995/03/08  12:36:39  adam
73  * New function: dbc2709_cvt.
74  *
75  * Revision 1.8  1995/03/08  12:03:15  adam
76  * Hack: When tags 00? are used, every separator (DC[1-3]) marks
77  * the end of the data field.
78  *
79  * Revision 1.7  1995/02/22  21:28:03  adam
80  * Changed header.
81  *
82  * Revision 1.5  1995/02/22  15:24:14  adam
83  * Function iso2709_cvt makes a litte check for the format. It returns
84  * NULL if the buffer parameter can never be a MARC record.
85  *
86  * Revision 1.4  1995/02/15  17:45:44  adam
87  * Bug fix in iso2709 module.
88  *
89  * Revision 1.3  1995/02/10  17:05:18  adam
90  * New function iso2709_display to display MARC records in a
91  * line-by-line format. The iso2709_cvt function no longer
92  * prints the record to stderr.
93  *
94  * Revision 1.2  1995/02/10  16:50:32  adam
95  * Indicator field moved to 'struct iso2709_dir' from 'struct
96  * iso2709_field'.
97  * Function iso2709_rm implemented - to delete a MARC record.
98  *
99  * Revision 1.1.1.1  1995/02/09  17:27:11  adam
100  * Initial version of email gateway under CVS control.
101  *
102  */
103
104 #include <stdlib.h>
105 #include <string.h>
106 #include <stdio.h>
107 #include <assert.h>
108 #include <ctype.h>
109
110 #include <iso2709p.h>
111
112 static int atoin (const char *buf, int n)
113 {
114     int val = 0;
115     while (--n >= 0)
116     {
117         if (isdigit(*buf))
118             val = val*10 + (*buf - '0');
119         buf++;
120     }
121     return val;
122 }
123
124 static void strncpyx (char *d, const char *s, int n)
125 {
126     while (--n >= 0 && *s)
127         if (*s != ISO2709_IDFS)
128             *d++ = *s++;
129         else
130         {
131             *d++ = ' ';
132             s++;
133         }
134     *d = '\0';
135 }
136
137 char *iso2709_read (FILE *inf)
138 {
139     char length_str[5];
140     int size;
141     char *buf;
142
143     if (fread (length_str, 1, 5, inf) != 5)
144         return NULL;
145     size = atoin (length_str, 5);
146     if (size <= 6)
147         return NULL;
148     if (!(buf = malloc (size+1)))
149         return NULL;
150     if (fread (buf+5, 1, size-5, inf) != (size-5))
151     {
152         free (buf);
153         return NULL;
154     }
155     memcpy (buf, length_str, 5);
156     buf[size] = '\0';
157     return buf;
158 }
159
160 Iso2709Rec iso2709_mk (void)
161 {
162     Iso2709Rec p;
163
164     if (!(p = malloc (sizeof(*p))))
165         return NULL;
166
167     p->record_length = 0;
168     strncpyx (p->record_status, " ", 1);
169     strncpyx (p->implementation_codes, "    ", 4);
170     p->indicator_length = 2;
171     p->identifier_length = 2;
172     p->base_address = 0;
173     strncpyx (p->user_systems, "   ", 3);
174     p->length_data_entry = 4;
175     p->length_starting = 5;
176     p->length_implementation = 0;
177     strncpyx (p->future_use, " ", 1);
178
179     p->directory = NULL;
180     return p;
181 }
182
183 Iso2709Rec iso2709_cvt (const char *buf)
184 {
185     struct iso2709_dir **dpp, *dp;
186     int pos = 24;
187     Iso2709Rec p;
188
189     if (!(p = malloc (sizeof(*p))))
190         return NULL;
191
192     /* deal with record label (24 characters) */
193     p->record_length = atoin (buf, 5);
194     strncpyx (p->record_status, buf+5, 1);
195     strncpyx (p->implementation_codes, buf+6, 4);
196     p->indicator_length = atoin (buf+10, 1);
197     p->identifier_length = atoin (buf+11, 1);
198     p->base_address = atoin (buf+12, 4);
199     strncpyx (p->user_systems, buf+17, 3);
200
201     if (p->record_length < 26)
202     {
203         free (p);
204         return NULL;
205     }
206     p->length_data_entry = atoin (buf+20, 1);
207     p->length_starting = atoin (buf+21, 1);
208     p->length_implementation = atoin (buf+22, 1);
209     strncpyx (p->future_use, buf+23, 1);
210
211     /* deal with directory */
212     dpp = &p->directory;
213     *dpp = NULL;
214     while (buf[pos] != ISO2709_FS)
215     {
216         if (!(*dpp = malloc (sizeof(**dpp))))
217         {
218             iso2709_rm (p);
219             return NULL;
220         }
221         (*dpp)->next = NULL;
222         (*dpp)->fields = NULL;
223         (*dpp)->indicator = NULL;
224         strncpyx ((*dpp)->tag, buf+pos, 3);
225         pos += 3;
226         (*dpp)->length = atoin (buf+pos, p->length_data_entry);
227         pos += p->length_data_entry;
228         (*dpp)->offset = atoin (buf+pos, p->length_starting);
229         pos += p->length_starting + p->length_implementation;
230
231         dpp = &(*dpp)->next;
232         if (pos > p->record_length)
233         {
234             iso2709_rm (p);
235             return NULL;
236         }
237     }
238     pos++;
239     /* deal with datafields */
240     for (dp = p->directory; dp; dp = dp->next)
241     {
242         int identifier_flag;
243         struct iso2709_field **fpp;
244         int dpos = pos+dp->offset;
245         int epos = pos+dp->offset+dp->length-1;
246
247         if (epos <= dpos)
248         {
249             iso2709_rm (p);
250             return NULL;
251         }
252         fpp = &dp->fields;
253
254         if (!(*fpp = malloc (sizeof(**fpp))))
255         {
256             iso2709_rm (p);
257             return NULL;
258         }
259         (*fpp)->next = NULL;
260
261         identifier_flag = 1;
262         if (p->indicator_length)
263         {
264 #if STUPID_ISO_DBC
265             if (buf[dpos+p->indicator_length] != ISO2709_IDFS)
266                 identifier_flag = 0;
267 #else
268             if (!memcmp (dp->tag, "00", 2))
269                 identifier_flag = 0;
270 #endif
271         }
272         else if (!memcmp (dp->tag, "00", 2))
273                 identifier_flag = 0;
274         if (identifier_flag && p->indicator_length)
275         {
276             if (!(dp->indicator = malloc (p->indicator_length+1)))
277             {
278                 iso2709_rm (p);
279                 return NULL;
280             }
281             strncpyx (dp->indicator, buf+dpos, p->indicator_length);
282             dpos += p->indicator_length;
283         }
284         else
285             dp->indicator = NULL;
286         while (1)
287         {
288             int dpos_n;
289             if (p->identifier_length && identifier_flag)
290             {
291                 strncpyx ((*fpp)->identifier, buf+dpos+1,
292                           p->identifier_length-1);
293                 dpos_n = dpos += p->identifier_length;
294                 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS
295                        && buf[dpos_n] != ISO2709_IDFS && dpos_n < epos)
296                     dpos_n++;
297             }
298             else
299             {
300                 *(*fpp)->identifier = '\0';
301                 dpos_n = dpos;
302                 while (buf[dpos_n] != ISO2709_FS && buf[dpos_n] != ISO2709_RS
303                        && dpos_n < epos)
304                     dpos_n++;
305             }
306             if (!((*fpp)->data = malloc (dpos_n - dpos + 1)))
307             {
308                 iso2709_rm (p);
309                 return NULL;
310             }
311             strncpyx ((*fpp)->data, buf+dpos, dpos_n - dpos);
312             dpos = dpos_n;
313             
314             if (dpos >= epos)
315             {
316                 if (buf[dpos] != ISO2709_FS && buf[dpos] != ISO2709_RS)
317                     fprintf (stderr, "Missing separator at end of field "
318                              "in %s %s\n", dp->tag, (*fpp)->identifier);
319                 break;
320             }
321             if (buf[dpos] == ISO2709_FS || buf[dpos] == ISO2709_RS)
322             {
323                 fprintf (stderr, "Unexpected separator inside field %s %s\n",
324                          dp->tag, (*fpp)->identifier);
325                 break;
326             }
327             fpp = &(*fpp)->next;
328             if (!(*fpp = malloc (sizeof(**fpp))))
329             {
330                 iso2709_rm (p);
331                 return NULL;
332             }
333             (*fpp)->next = NULL;
334         }
335     }
336     return p;
337 }
338
339 void iso2709_rm (Iso2709Rec rec)
340 {
341     struct iso2709_dir *dir, *dir1;
342
343     for (dir = rec->directory; dir; dir = dir1)
344     {
345         struct iso2709_field *field, *field1;
346
347         for (field = dir->fields; field; field = field1)
348         {
349             free (field->data);
350             field1 = field->next;
351             free (field);
352         }
353         free (dir->indicator);
354         dir1 = dir->next;
355         free (dir);
356     }
357 }
358