More robust ISO2709 handling (in case of real bad formats).
[yaz-moved-to-github.git] / util / marcdisp.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: marcdisp.c,v $
7  * Revision 1.9  1999-12-21 16:24:48  adam
8  * More robust ISO2709 handling (in case of real bad formats).
9  *
10  * Revision 1.8  1999/11/30 13:47:12  adam
11  * Improved installation. Moved header files to include/yaz.
12  *
13  * Revision 1.7  1997/09/24 13:29:40  adam
14  * Added verbose option -v to marcdump utility.
15  *
16  * Revision 1.6  1997/09/04 07:52:27  adam
17  * Moved atoi_n function to separate source file.
18  *
19  * Revision 1.5  1997/05/01 15:08:15  adam
20  * Added log_mask_str_x routine.
21  *
22  * Revision 1.4  1995/09/29 17:12:34  quinn
23  * Smallish
24  *
25  * Revision 1.3  1995/09/27  15:03:03  quinn
26  * Modified function heads & prototypes.
27  *
28  * Revision 1.2  1995/05/16  08:51:12  quinn
29  * License, documentation, and memory fixes
30  *
31  * Revision 1.1  1995/04/10  10:28:46  quinn
32  * Added copy of CCL and MARC display
33  *
34  */
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <yaz/marcdisp.h>
40 #include <yaz/yaz-util.h>
41
42 int marc_display_ex (const char *buf, FILE *outf, int debug)
43 {
44     int entry_p;
45     int record_length;
46     int indicator_length;
47     int identifier_length;
48     int base_address;
49     int length_data_entry;
50     int length_starting;
51     int length_implementation;
52
53     if (!outf)
54         outf = stdout;
55     record_length = atoi_n (buf, 5);
56     if (record_length < 25)
57         return -1;
58     indicator_length = atoi_n (buf+10, 1);
59     identifier_length = atoi_n (buf+11, 1);
60     base_address = atoi_n (buf+12, 4);
61
62     indicator_length = identifier_length = 2;
63
64     length_data_entry = atoi_n (buf+20, 1);
65     length_starting = atoi_n (buf+21, 1);
66     length_implementation = atoi_n (buf+22, 1);
67
68     if (debug)
69     {
70         fprintf (outf, "Record length         %5d\n", record_length);
71         fprintf (outf, "Indicator length      %5d\n", indicator_length);
72         fprintf (outf, "Identifier length     %5d\n", identifier_length);
73         fprintf (outf, "Base address          %5d\n", base_address);
74         fprintf (outf, "Length data entry     %5d\n", length_data_entry);
75         fprintf (outf, "Length starting       %5d\n", length_starting);
76         fprintf (outf, "Length implementation %5d\n", length_implementation);
77     }
78     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
79     {
80         entry_p += 3+length_data_entry+length_starting;
81         if (entry_p >= record_length)
82             return -1;
83     }
84     base_address = entry_p+1;
85     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
86     {
87         int data_length;
88         int data_offset;
89         int end_offset;
90         int i, j;
91         char tag[4];
92
93         memcpy (tag, buf+entry_p, 3);
94         entry_p += 3;
95         tag[3] = '\0';
96         if (debug)
97             fprintf (outf, "Tag: ");
98         fprintf (outf, "%s ", tag);
99         data_length = atoi_n (buf+entry_p, length_data_entry);
100         entry_p += length_data_entry;
101         data_offset = atoi_n (buf+entry_p, length_starting);
102         entry_p += length_starting;
103         i = data_offset + base_address;
104         end_offset = i+data_length-1;
105         if (debug)
106             fprintf (outf, " Ind: ");
107         if (memcmp (tag, "00", 2) && indicator_length)
108         {
109             for (j = 0; j<indicator_length; j++)
110                 fprintf (outf, "%c", buf[i++]);
111         }
112         if (debug)
113             fprintf (outf, " Fields: ");
114         while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
115         {
116             if (memcmp (tag, "00", 2) && identifier_length)
117             {
118                 i++;
119                 fprintf (outf, " $"); 
120                 for (j = 1; j<identifier_length; j++)
121                     fprintf (outf, "%c", buf[i++]);
122                 fprintf (outf, " ");
123                 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
124                        buf[i] != ISO2709_FS && i < end_offset)
125                     fprintf (outf, "%c", buf[i++]);
126             }
127             else
128                 fprintf (outf, "%c", buf[i++]);
129         }
130         fprintf (outf, "\n");
131         if (i < end_offset)
132             fprintf (outf, "-- separator but not at end of field\n");
133         if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
134             fprintf (outf, "-- no separator at end of field\n");
135     }
136     return record_length;
137 }
138
139 int marc_display (const char *buf, FILE *outf)
140 {
141     return marc_display_ex (buf, outf, 0);
142 }
143
144