1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
7 * \file marc_read_iso2709.c
8 * \brief Implements reading of MARC as ISO2709
22 #include <yaz/marcdisp.h>
23 #include <yaz/wrbuf.h>
24 #include <yaz/yaz-util.h>
26 int yaz_marc_read_iso2709(yaz_marc_t mt, const char *buf, int bsize)
31 int identifier_length;
34 int length_data_entry;
36 int length_implementation;
40 record_length = atoi_n (buf, 5);
41 if (record_length < 25)
43 yaz_marc_cprintf(mt, "Record length %d < 24", record_length);
46 /* ballout if bsize is known and record_length is less than that */
47 if (bsize != -1 && record_length > bsize)
49 yaz_marc_cprintf(mt, "Record appears to be larger than buffer %d < %d",
50 record_length, bsize);
53 if (yaz_marc_get_debug(mt))
54 yaz_marc_cprintf(mt, "Record length %5d", record_length);
56 yaz_marc_set_leader(mt, buf,
62 &length_implementation);
64 /* First pass. determine length of directory & base of data */
65 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
67 /* length of directory entry */
68 int l = 3 + length_data_entry + length_starting;
69 if (entry_p + l >= record_length)
71 yaz_marc_cprintf(mt, "Directory offset %d: end of record."
72 " Missing FS char", entry_p);
75 if (yaz_marc_get_debug(mt))
77 yaz_marc_cprintf(mt, "Directory offset %d: Tag %.3s",
78 entry_p, buf+entry_p);
80 /* Check for digits in length info */
82 if (!isdigit(*(const unsigned char *) (buf + entry_p+l)))
86 /* Not all digits, so stop directory scan */
87 yaz_marc_cprintf(mt, "Directory offset %d: Bad value for data"
88 " length and/or length starting", entry_p);
91 entry_p += 3 + length_data_entry + length_starting;
93 end_of_directory = entry_p;
94 if (base_address != entry_p+1)
96 yaz_marc_cprintf(mt, "Base address not at end of directory,"
97 " base %d, end %d", base_address, entry_p+1);
100 /* Second pass. parse control - and datafields */
101 for (entry_p = 24; entry_p != end_of_directory; )
108 int identifier_flag = 0;
109 int entry_p0 = entry_p;
111 memcpy (tag, buf+entry_p, 3);
114 data_length = atoi_n(buf+entry_p, length_data_entry);
115 entry_p += length_data_entry;
116 data_offset = atoi_n(buf+entry_p, length_starting);
117 entry_p += length_starting;
118 i = data_offset + base_address;
119 end_offset = i+data_length-1;
121 if (data_length <= 0 || data_offset < 0)
124 if (yaz_marc_get_debug(mt))
126 yaz_marc_cprintf(mt, "Tag: %s. Directory offset %d: data-length %d,"
128 tag, entry_p0, data_length, data_offset);
130 if (end_offset >= record_length)
132 yaz_marc_cprintf(mt, "Directory offset %d: Data out of bounds %d >= %d",
133 entry_p0, end_offset, record_length);
137 if (memcmp (tag, "00", 2))
138 identifier_flag = 1; /* if not 00X assume subfields */
139 else if (indicator_length < 4 && indicator_length > 0)
141 /* Danmarc 00X have subfields */
142 if (buf[i + indicator_length] == ISO2709_IDFS)
144 else if (buf[i + indicator_length + 1] == ISO2709_IDFS)
151 i += identifier_flag-1;
152 yaz_marc_add_datafield(mt, tag, buf+i, indicator_length);
153 i += indicator_length;
155 while (i < end_offset &&
156 buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
158 int code_offset = i+1;
161 while (i < end_offset &&
162 buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
163 buf[i] != ISO2709_FS)
165 yaz_marc_add_subfield(mt, buf+code_offset, i - code_offset);
172 while (i < end_offset &&
173 buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
175 yaz_marc_add_controlfield(mt, tag, buf+i0, i-i0);
179 yaz_marc_cprintf(mt, "Separator but not at end of field length=%d",
182 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
184 yaz_marc_cprintf(mt, "No separator at end of field length=%d",
188 return record_length;
194 * indent-tabs-mode: nil
196 * vim: shiftwidth=4 tabstop=8 expandtab