Subversion Repositories tendra.SVN

Rev

Rev 2 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 7
Line -... Line 1...
-
 
1
/*
-
 
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
 */
1
/*
31
/*
2
    		 Crown Copyright (c) 1997
32
    		 Crown Copyright (c) 1997
3
    
33
 
4
    This TenDRA(r) Computer Program is subject to Copyright
34
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
35
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
36
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
37
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
38
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
39
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
40
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
41
    shall be deemed to be acceptance of the following conditions:-
12
    
42
 
13
        (1) Its Recipients shall ensure that this Notice is
43
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
44
        reproduced upon any copies or amended versions of it;
15
    
45
 
16
        (2) Any amended version of it shall be clearly marked to
46
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
47
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
48
        for the relevant amendment or amendments;
19
    
49
 
20
        (3) Its onward transfer from a recipient to another
50
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
51
        party shall be deemed to be that party's acceptance of
22
        these conditions;
52
        these conditions;
23
    
53
 
24
        (4) DERA gives no warranty or assurance as to its
54
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
55
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
56
        no liability whatsoever in relation to any use to which
27
        it may be put.
57
        it may be put.
28
*/
58
*/
Line 65... Line 95...
65
 
95
 
66
#include "solve-cycles.h"
96
#include "solve-cycles.h"
67
 
97
 
68
/*--------------------------------------------------------------------------*/
98
/*--------------------------------------------------------------------------*/
69
 
99
 
70
ExceptionP XX_tdf_read_error = EXCEPTION ("error reading TDF capsule");
100
ExceptionP XX_tdf_read_error = EXCEPTION("error reading TDF capsule");
71
 
101
 
72
/*--------------------------------------------------------------------------*/
102
/*--------------------------------------------------------------------------*/
73
 
103
 
74
static unsigned
104
static unsigned
75
tdf_read_nibble PROTO_N ((reader))
105
tdf_read_nibble(TDFReaderP reader)
76
		PROTO_T (TDFReaderP reader)
-
 
77
{
106
{
78
    if (reader->new_byte) {
107
    if (reader->new_byte) {
79
	switch (reader->type) EXHAUSTIVE {
108
	switch (reader->type)EXHAUSTIVE {
80
	  case RT_STREAM:
109
	  case RT_STREAM:
81
	    if (bistream_read_byte (&(reader->u.bistream), &(reader->byte))) {
110
	    if (bistream_read_byte(& (reader->u.bistream), & (reader->byte))) {
82
		reader->new_byte = FALSE;
111
		reader->new_byte = FALSE;
83
		return (((unsigned) reader->byte >> 4) & 0xF);
112
		return(((unsigned)reader->byte >> 4) & 0xF);
84
	    }
113
	    }
85
	    E_unexpected_eof_in_tdf (reader);
114
	    E_unexpected_eof_in_tdf(reader);
86
	    THROW (XX_tdf_read_error);
115
	    THROW(XX_tdf_read_error);
87
	    UNREACHED;
116
	    UNREACHED;
88
	  case RT_STRING:
117
	  case RT_STRING:
89
	    if (reader->u.string.current < reader->u.string.limit) {
118
	    if (reader->u.string.current < reader->u.string.limit) {
90
		reader->byte     = (ByteT) (*(reader->u.string.current ++));
119
		reader->byte     = (ByteT)(*(reader->u.string.current++));
91
		reader->new_byte = FALSE;
120
		reader->new_byte = FALSE;
92
		reader->u.string.byte ++;
121
		reader->u.string.byte++;
93
		return (((unsigned) reader->byte >> 4) & 0xF);
122
		return(((unsigned)reader->byte >> 4) & 0xF);
94
	    }
123
	    }
95
	    E_unexpected_eof_in_tdf (reader);
124
	    E_unexpected_eof_in_tdf(reader);
96
	    THROW (XX_tdf_read_error);
125
	    THROW(XX_tdf_read_error);
97
	    UNREACHED;
126
	    UNREACHED;
98
	}
127
	}
99
    }
128
    }
100
    reader->new_byte = TRUE;
129
    reader->new_byte = TRUE;
101
    return (reader->byte & 0xF);
130
    return(reader->byte & 0xF);
102
}
131
}
103
 
132
 
104
/*--------------------------------------------------------------------------*/
133
/*--------------------------------------------------------------------------*/
105
 
134
 
106
BoolT
135
BoolT
107
tdf_reader_open PROTO_N ((reader, name))
136
tdf_reader_open(TDFReaderP reader,			 CStringP   name)
108
		PROTO_T (TDFReaderP reader X
-
 
109
			 CStringP   name)
-
 
110
{
137
{
111
    reader->type     = RT_STREAM;
138
    reader->type     = RT_STREAM;
112
    reader->new_byte = TRUE;
139
    reader->new_byte = TRUE;
113
    if (!bistream_open (&(reader->u.bistream), name)) {
140
    if (!bistream_open(& (reader->u.bistream), name)) {
114
	return (FALSE);
141
	return(FALSE);
115
    }
142
    }
116
    return (TRUE);
143
    return(TRUE);
117
}
144
}
118
 
145
 
119
void
146
void
120
tdf_reader_open_string PROTO_N ((reader, name, bytes))
147
tdf_reader_open_string(TDFReaderP reader,				CStringP   name, 
121
		       PROTO_T (TDFReaderP reader X
-
 
122
				CStringP   name X
-
 
123
				NStringP   bytes)
148
				NStringP   bytes)
124
{
149
{
125
    CStringP contents = nstring_contents (bytes);
150
    CStringP contents = nstring_contents(bytes);
126
    unsigned length   = nstring_length (bytes);
151
    unsigned length   = nstring_length(bytes);
127
 
152
 
128
    reader->type              = RT_STRING;
153
    reader->type              = RT_STRING;
129
    reader->new_byte          = TRUE;
154
    reader->new_byte          = TRUE;
130
    reader->u.string.contents = contents;
155
    reader->u.string.contents = contents;
131
    reader->u.string.current  = contents;
156
    reader->u.string.current  = contents;
Line 133... Line 158...
133
    reader->u.string.name     = name;
158
    reader->u.string.name     = name;
134
    reader->u.string.byte     = 0;
159
    reader->u.string.byte     = 0;
135
}
160
}
136
 
161
 
137
CStringP
162
CStringP
138
tdf_reader_name PROTO_N ((reader))
163
tdf_reader_name(TDFReaderP reader)
139
		PROTO_T (TDFReaderP reader)
-
 
140
{
164
{
141
    switch (reader->type) EXHAUSTIVE {
165
    switch (reader->type)EXHAUSTIVE {
142
      case RT_STREAM:
166
      case RT_STREAM:
143
	return (bistream_name (&(reader->u.bistream)));
167
	return(bistream_name(& (reader->u.bistream)));
144
      case RT_STRING:
168
      case RT_STRING:
145
	return (reader->u.string.name);
169
	return(reader->u.string.name);
146
    }
170
    }
147
    UNREACHED;
171
    UNREACHED;
148
}
172
}
149
 
173
 
150
unsigned
174
unsigned
151
tdf_reader_byte PROTO_N ((reader))
175
tdf_reader_byte(TDFReaderP reader)
152
		PROTO_T (TDFReaderP reader)
-
 
153
{
176
{
154
    switch (reader->type) EXHAUSTIVE {
177
    switch (reader->type)EXHAUSTIVE {
155
      case RT_STREAM:
178
      case RT_STREAM:
156
	return (bistream_byte (&(reader->u.bistream)));
179
	return(bistream_byte(& (reader->u.bistream)));
157
      case RT_STRING:
180
      case RT_STRING:
158
	return (reader->u.string.byte);
181
	return(reader->u.string.byte);
159
    }
182
    }
160
    UNREACHED;
183
    UNREACHED;
161
}
184
}
162
 
185
 
163
unsigned
186
unsigned
164
tdf_read_int PROTO_N ((reader))
187
tdf_read_int(TDFReaderP reader)
165
	     PROTO_T (TDFReaderP reader)
-
 
166
{
188
{
167
    unsigned value = 0;
189
    unsigned value = 0;
168
    unsigned limit = (UINT_MAX >> 3);
190
    unsigned limit = (UINT_MAX >> 3);
169
 
191
 
170
    for (;;) {
192
    for (;;) {
171
	unsigned nibble = tdf_read_nibble (reader);
193
	unsigned nibble = tdf_read_nibble(reader);
172
 
194
 
173
	if (value > limit) {
195
	if (value > limit) {
174
	    E_tdf_integer_too_big_in_tdf (reader);
196
	    E_tdf_integer_too_big_in_tdf(reader);
175
	    THROW (XX_tdf_read_error);
197
	    THROW(XX_tdf_read_error);
176
	    UNREACHED;
198
	    UNREACHED;
177
	}
199
	}
178
	value <<= 3;
200
	value <<= 3;
179
	value |= (nibble & 0x7);
201
	value |= (nibble & 0x7);
180
	if (nibble & 0x8) {
202
	if (nibble & 0x8) {
181
	    return (value);
203
	    return(value);
182
	}
204
	}
183
    }
205
    }
184
}
206
}
185
 
207
 
186
void
208
void
187
tdf_read_align PROTO_N ((reader))
209
tdf_read_align(TDFReaderP reader)
188
	       PROTO_T (TDFReaderP reader)
-
 
189
{
210
{
190
    reader->new_byte = TRUE;
211
    reader->new_byte = TRUE;
191
}
212
}
192
 
213
 
193
void
214
void
194
tdf_read_bytes PROTO_N ((reader, nstring))
215
tdf_read_bytes(TDFReaderP reader,			NStringP   nstring)
195
	       PROTO_T (TDFReaderP reader X
-
 
196
			NStringP   nstring)
-
 
197
{
216
{
198
    unsigned length   = nstring_length (nstring);
217
    unsigned length   = nstring_length(nstring);
199
    CStringP contents = nstring_contents (nstring);
218
    CStringP contents = nstring_contents(nstring);
200
 
219
 
201
    tdf_read_align (reader);
220
    tdf_read_align(reader);
202
    switch (reader->type) EXHAUSTIVE {
221
    switch (reader->type)EXHAUSTIVE {
203
      case RT_STREAM:
222
      case RT_STREAM:
204
	if (bistream_read_chars (&(reader->u.bistream), length, contents) !=
223
	if (bistream_read_chars(&(reader->u.bistream), length, contents) !=
205
	    length) {
224
	    length) {
206
	    E_unexpected_eof_in_tdf (reader);
225
	    E_unexpected_eof_in_tdf(reader);
207
	    THROW (XX_tdf_read_error);
226
	    THROW(XX_tdf_read_error);
208
	    UNREACHED;
227
	    UNREACHED;
209
	}
228
	}
210
	break;
229
	break;
211
      case RT_STRING:
230
      case RT_STRING:
212
	if ((reader->u.string.current + length) > reader->u.string.limit) {
231
	if ((reader->u.string.current + length) > reader->u.string.limit) {
213
	    E_unexpected_eof_in_tdf (reader);
232
	    E_unexpected_eof_in_tdf(reader);
214
	    THROW (XX_tdf_read_error);
233
	    THROW(XX_tdf_read_error);
215
	    UNREACHED;
234
	    UNREACHED;
216
	}
235
	}
217
	nstring_insert_cstring (nstring, reader->u.string.current);
236
	nstring_insert_cstring(nstring, reader->u.string.current);
218
	reader->u.string.current += length;
237
	reader->u.string.current += length;
219
	reader->u.string.byte    += length;
238
	reader->u.string.byte    += length;
220
	break;
239
	break;
221
    }
240
    }
222
}
241
}
223
 
242
 
224
void
243
void
225
tdf_read_string PROTO_N ((reader, nstring))
244
tdf_read_string(TDFReaderP reader,			 NStringP   nstring)
226
		PROTO_T (TDFReaderP reader X
-
 
227
			 NStringP   nstring)
-
 
228
{
245
{
229
    unsigned size = tdf_read_int (reader);
246
    unsigned size = tdf_read_int(reader);
230
    unsigned length;
247
    unsigned length;
231
 
248
 
232
    if (size != 8) {
249
    if (size != 8) {
233
	E_unsupported_char_size_in_tdf (reader, size);
250
	E_unsupported_char_size_in_tdf(reader, size);
234
	THROW (XX_tdf_read_error);
251
	THROW(XX_tdf_read_error);
235
	UNREACHED;
252
	UNREACHED;
236
    }
253
    }
237
    length = tdf_read_int (reader);
254
    length = tdf_read_int(reader);
238
    nstring_init_length (nstring, length);
255
    nstring_init_length(nstring, length);
239
    tdf_read_bytes (reader, nstring);
256
    tdf_read_bytes(reader, nstring);
240
}
257
}
241
 
258
 
242
void
259
void
243
tdf_read_name PROTO_N ((reader, name))
260
tdf_read_name(TDFReaderP reader,		       NameKeyP   name)
244
	      PROTO_T (TDFReaderP reader X
-
 
245
		       NameKeyP   name)
-
 
246
{
261
{
247
    unsigned type = ((tdf_read_nibble (reader) >> 2) & 0x3);
262
    unsigned type = ((tdf_read_nibble(reader) >> 2) & 0x3);
248
    NStringT nstring;
263
    NStringT nstring;
249
    unsigned components;
264
    unsigned components;
250
    unsigned i;
265
    unsigned i;
251
 
266
 
252
    tdf_read_align (reader);
267
    tdf_read_align(reader);
253
    switch (type) {
268
    switch (type) {
254
      case 0x1:
269
      case 0x1:
255
	tdf_read_string (reader, &nstring);
270
	tdf_read_string(reader, &nstring);
256
	name_key_init_string (name, &nstring);
271
	name_key_init_string(name, &nstring);
257
	break;
272
	break;
258
      case 0x2:
273
      case 0x2:
259
	components = tdf_read_int (reader);
274
	components = tdf_read_int(reader);
260
	name_key_init_unique (name, components);
275
	name_key_init_unique(name, components);
261
	for (i = 0; i < components; i ++) {
276
	for (i = 0; i < components; i++) {
262
	    tdf_read_string (reader, &nstring);
277
	    tdf_read_string(reader, &nstring);
263
	    name_key_set_component (name, i, &nstring);
278
	    name_key_set_component(name, i, &nstring);
264
	}
279
	}
265
	break;
280
	break;
266
      default:
281
      default:
267
	E_bad_name_type_in_tdf (reader, type);
282
	E_bad_name_type_in_tdf(reader, type);
268
	THROW (XX_tdf_read_error);
283
	THROW(XX_tdf_read_error);
269
	UNREACHED;
284
	UNREACHED;
270
    }
285
    }
271
}
286
}
272
 
287
 
273
void
288
void
274
tdf_read_eof PROTO_N ((reader))
289
tdf_read_eof(TDFReaderP reader)
275
	     PROTO_T (TDFReaderP reader)
-
 
276
{
290
{
277
    ByteT byte;
291
    ByteT byte;
278
 
292
 
279
    switch (reader->type) EXHAUSTIVE {
293
    switch (reader->type)EXHAUSTIVE {
280
      case RT_STREAM:
294
      case RT_STREAM:
281
	if (bistream_read_byte (&(reader->u.bistream), &byte)) {
295
	if (bistream_read_byte(& (reader->u.bistream), &byte)) {
282
	    E_expected_eof_in_tdf (reader);
296
	    E_expected_eof_in_tdf(reader);
283
	    THROW (XX_tdf_read_error);
297
	    THROW(XX_tdf_read_error);
284
	    UNREACHED;
298
	    UNREACHED;
285
	}
299
	}
286
	break;
300
	break;
287
      case RT_STRING:
301
      case RT_STRING:
288
	if (reader->u.string.current < reader->u.string.limit) {
302
	if (reader->u.string.current < reader->u.string.limit) {
289
	    E_expected_eof_in_tdf (reader);
303
	    E_expected_eof_in_tdf(reader);
290
	    THROW (XX_tdf_read_error);
304
	    THROW(XX_tdf_read_error);
291
	    UNREACHED;
305
	    UNREACHED;
292
	}
306
	}
293
	break;
307
	break;
294
    }
308
    }
295
}
309
}
296
 
310
 
297
void
311
void
298
tdf_reader_rewind PROTO_N ((reader))
312
tdf_reader_rewind(TDFReaderP reader)
299
		  PROTO_T (TDFReaderP reader)
-
 
300
{
313
{
301
    switch (reader->type) EXHAUSTIVE {
314
    switch (reader->type)EXHAUSTIVE {
302
      case RT_STREAM:
315
      case RT_STREAM:
303
	bistream_rewind (&(reader->u.bistream));
316
	bistream_rewind(& (reader->u.bistream));
304
	break;
317
	break;
305
      case RT_STRING:
318
      case RT_STRING:
306
	reader->u.string.current = reader->u.string.contents;
319
	reader->u.string.current = reader->u.string.contents;
307
	break;
320
	break;
308
    }
321
    }
309
}
322
}
310
 
323
 
311
void
324
void
312
tdf_reader_close PROTO_N ((reader))
325
tdf_reader_close(TDFReaderP reader)
313
		 PROTO_T (TDFReaderP reader)
-
 
314
{
326
{
315
    switch (reader->type) EXHAUSTIVE {
327
    switch (reader->type)EXHAUSTIVE {
316
      case RT_STREAM:
328
      case RT_STREAM:
317
	bistream_close (&(reader->u.bistream));
329
	bistream_close(& (reader->u.bistream));
318
	break;
330
	break;
319
      case RT_STRING:
331
      case RT_STRING:
320
	break;
332
	break;
321
    }
333
    }
322
}
334
}