Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 2
 * Copyright (c) 2002-2006 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
7 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
7 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
7 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
/*** tdf-read.c --- TDF reader ADT.
62
 *
63
 ** Author: Steve Folkes <smf@hermes.mod.uk>
64
 *
65
 *** Commentary:
66
 *
67
 * This file implements the TDF reader routines used by the TDF linker.
68
 *
69
 *** Change Log:
70
 * $Log: tdf-read.c,v $
71
 * Revision 1.1.1.1  1998/01/17  15:57:20  release
72
 * First version to be checked into rolling release.
73
 *
74
 * Revision 1.3  1995/09/22  08:39:37  smf
75
 * Fixed problems with incomplete structures (to shut "tcc" up).
76
 * Fixed some problems in "name-key.c" (no real problems, but rewritten to
77
 * reduce the warnings that were output by "tcc" and "gcc").
78
 * Fixed bug CR95_354.tld-common-id-problem (library capsules could be loaded
79
 * more than once).
80
 *
81
 * Revision 1.2  1994/12/12  11:46:55  smf
82
 * Performing changes for 'CR94_178.sid+tld-update' - bringing in line with
83
 * OSSG C Coding Standards.
84
 *
85
 * Revision 1.1.1.1  1994/07/25  16:03:38  smf
86
 * Initial import of TDF linker 3.5 non shared files.
87
 *
88
**/
89
 
90
/****************************************************************************/
91
 
92
#include "tdf-read.h"
93
#include "gen-errors.h"
94
#include "ostream.h"
95
 
96
#include "solve-cycles.h"
97
 
98
/*--------------------------------------------------------------------------*/
99
 
7 7u83 100
ExceptionP XX_tdf_read_error = EXCEPTION("error reading TDF capsule");
2 7u83 101
 
102
/*--------------------------------------------------------------------------*/
103
 
104
static unsigned
7 7u83 105
tdf_read_nibble(TDFReaderP reader)
2 7u83 106
{
107
    if (reader->new_byte) {
7 7u83 108
	switch (reader->type)EXHAUSTIVE {
2 7u83 109
	  case RT_STREAM:
7 7u83 110
	    if (bistream_read_byte(& (reader->u.bistream), & (reader->byte))) {
2 7u83 111
		reader->new_byte = FALSE;
7 7u83 112
		return(((unsigned)reader->byte >> 4) & 0xF);
2 7u83 113
	    }
7 7u83 114
	    E_unexpected_eof_in_tdf(reader);
115
	    THROW(XX_tdf_read_error);
2 7u83 116
	    UNREACHED;
117
	  case RT_STRING:
118
	    if (reader->u.string.current < reader->u.string.limit) {
7 7u83 119
		reader->byte     = (ByteT)(*(reader->u.string.current++));
2 7u83 120
		reader->new_byte = FALSE;
7 7u83 121
		reader->u.string.byte++;
122
		return(((unsigned)reader->byte >> 4) & 0xF);
2 7u83 123
	    }
7 7u83 124
	    E_unexpected_eof_in_tdf(reader);
125
	    THROW(XX_tdf_read_error);
2 7u83 126
	    UNREACHED;
127
	}
128
    }
129
    reader->new_byte = TRUE;
7 7u83 130
    return(reader->byte & 0xF);
2 7u83 131
}
132
 
133
/*--------------------------------------------------------------------------*/
134
 
135
BoolT
7 7u83 136
tdf_reader_open(TDFReaderP reader,			 CStringP   name)
2 7u83 137
{
138
    reader->type     = RT_STREAM;
139
    reader->new_byte = TRUE;
7 7u83 140
    if (!bistream_open(& (reader->u.bistream), name)) {
141
	return(FALSE);
2 7u83 142
    }
7 7u83 143
    return(TRUE);
2 7u83 144
}
145
 
146
void
7 7u83 147
tdf_reader_open_string(TDFReaderP reader,				CStringP   name, 
2 7u83 148
				NStringP   bytes)
149
{
7 7u83 150
    CStringP contents = nstring_contents(bytes);
151
    unsigned length   = nstring_length(bytes);
2 7u83 152
 
153
    reader->type              = RT_STRING;
154
    reader->new_byte          = TRUE;
155
    reader->u.string.contents = contents;
156
    reader->u.string.current  = contents;
157
    reader->u.string.limit    = (contents + length);
158
    reader->u.string.name     = name;
159
    reader->u.string.byte     = 0;
160
}
161
 
162
CStringP
7 7u83 163
tdf_reader_name(TDFReaderP reader)
2 7u83 164
{
7 7u83 165
    switch (reader->type)EXHAUSTIVE {
2 7u83 166
      case RT_STREAM:
7 7u83 167
	return(bistream_name(& (reader->u.bistream)));
2 7u83 168
      case RT_STRING:
7 7u83 169
	return(reader->u.string.name);
2 7u83 170
    }
171
    UNREACHED;
172
}
173
 
174
unsigned
7 7u83 175
tdf_reader_byte(TDFReaderP reader)
2 7u83 176
{
7 7u83 177
    switch (reader->type)EXHAUSTIVE {
2 7u83 178
      case RT_STREAM:
7 7u83 179
	return(bistream_byte(& (reader->u.bistream)));
2 7u83 180
      case RT_STRING:
7 7u83 181
	return(reader->u.string.byte);
2 7u83 182
    }
183
    UNREACHED;
184
}
185
 
186
unsigned
7 7u83 187
tdf_read_int(TDFReaderP reader)
2 7u83 188
{
189
    unsigned value = 0;
190
    unsigned limit = (UINT_MAX >> 3);
191
 
192
    for (;;) {
7 7u83 193
	unsigned nibble = tdf_read_nibble(reader);
2 7u83 194
 
195
	if (value > limit) {
7 7u83 196
	    E_tdf_integer_too_big_in_tdf(reader);
197
	    THROW(XX_tdf_read_error);
2 7u83 198
	    UNREACHED;
199
	}
200
	value <<= 3;
201
	value |= (nibble & 0x7);
202
	if (nibble & 0x8) {
7 7u83 203
	    return(value);
2 7u83 204
	}
205
    }
206
}
207
 
208
void
7 7u83 209
tdf_read_align(TDFReaderP reader)
2 7u83 210
{
211
    reader->new_byte = TRUE;
212
}
213
 
214
void
7 7u83 215
tdf_read_bytes(TDFReaderP reader,			NStringP   nstring)
2 7u83 216
{
7 7u83 217
    unsigned length   = nstring_length(nstring);
218
    CStringP contents = nstring_contents(nstring);
2 7u83 219
 
7 7u83 220
    tdf_read_align(reader);
221
    switch (reader->type)EXHAUSTIVE {
2 7u83 222
      case RT_STREAM:
7 7u83 223
	if (bistream_read_chars(&(reader->u.bistream), length, contents) !=
2 7u83 224
	    length) {
7 7u83 225
	    E_unexpected_eof_in_tdf(reader);
226
	    THROW(XX_tdf_read_error);
2 7u83 227
	    UNREACHED;
228
	}
229
	break;
230
      case RT_STRING:
231
	if ((reader->u.string.current + length) > reader->u.string.limit) {
7 7u83 232
	    E_unexpected_eof_in_tdf(reader);
233
	    THROW(XX_tdf_read_error);
2 7u83 234
	    UNREACHED;
235
	}
7 7u83 236
	nstring_insert_cstring(nstring, reader->u.string.current);
2 7u83 237
	reader->u.string.current += length;
238
	reader->u.string.byte    += length;
239
	break;
240
    }
241
}
242
 
243
void
7 7u83 244
tdf_read_string(TDFReaderP reader,			 NStringP   nstring)
2 7u83 245
{
7 7u83 246
    unsigned size = tdf_read_int(reader);
2 7u83 247
    unsigned length;
248
 
249
    if (size != 8) {
7 7u83 250
	E_unsupported_char_size_in_tdf(reader, size);
251
	THROW(XX_tdf_read_error);
2 7u83 252
	UNREACHED;
253
    }
7 7u83 254
    length = tdf_read_int(reader);
255
    nstring_init_length(nstring, length);
256
    tdf_read_bytes(reader, nstring);
2 7u83 257
}
258
 
259
void
7 7u83 260
tdf_read_name(TDFReaderP reader,		       NameKeyP   name)
2 7u83 261
{
7 7u83 262
    unsigned type = ((tdf_read_nibble(reader) >> 2) & 0x3);
2 7u83 263
    NStringT nstring;
264
    unsigned components;
265
    unsigned i;
266
 
7 7u83 267
    tdf_read_align(reader);
2 7u83 268
    switch (type) {
269
      case 0x1:
7 7u83 270
	tdf_read_string(reader, &nstring);
271
	name_key_init_string(name, &nstring);
2 7u83 272
	break;
273
      case 0x2:
7 7u83 274
	components = tdf_read_int(reader);
275
	name_key_init_unique(name, components);
276
	for (i = 0; i < components; i++) {
277
	    tdf_read_string(reader, &nstring);
278
	    name_key_set_component(name, i, &nstring);
2 7u83 279
	}
280
	break;
281
      default:
7 7u83 282
	E_bad_name_type_in_tdf(reader, type);
283
	THROW(XX_tdf_read_error);
2 7u83 284
	UNREACHED;
285
    }
286
}
287
 
288
void
7 7u83 289
tdf_read_eof(TDFReaderP reader)
2 7u83 290
{
291
    ByteT byte;
292
 
7 7u83 293
    switch (reader->type)EXHAUSTIVE {
2 7u83 294
      case RT_STREAM:
7 7u83 295
	if (bistream_read_byte(& (reader->u.bistream), &byte)) {
296
	    E_expected_eof_in_tdf(reader);
297
	    THROW(XX_tdf_read_error);
2 7u83 298
	    UNREACHED;
299
	}
300
	break;
301
      case RT_STRING:
302
	if (reader->u.string.current < reader->u.string.limit) {
7 7u83 303
	    E_expected_eof_in_tdf(reader);
304
	    THROW(XX_tdf_read_error);
2 7u83 305
	    UNREACHED;
306
	}
307
	break;
308
    }
309
}
310
 
311
void
7 7u83 312
tdf_reader_rewind(TDFReaderP reader)
2 7u83 313
{
7 7u83 314
    switch (reader->type)EXHAUSTIVE {
2 7u83 315
      case RT_STREAM:
7 7u83 316
	bistream_rewind(& (reader->u.bistream));
2 7u83 317
	break;
318
      case RT_STRING:
319
	reader->u.string.current = reader->u.string.contents;
320
	break;
321
    }
322
}
323
 
324
void
7 7u83 325
tdf_reader_close(TDFReaderP reader)
2 7u83 326
{
7 7u83 327
    switch (reader->type)EXHAUSTIVE {
2 7u83 328
      case RT_STREAM:
7 7u83 329
	bistream_close(& (reader->u.bistream));
2 7u83 330
	break;
331
      case RT_STRING:
332
	break;
333
    }
334
}
335
 
336
/*
337
 * Local variables(smf):
338
 * eval: (include::add-path-entry "../os-interface" "../library")
339
 * eval: (include::add-path-entry "../generated")
340
 * end:
341
**/