Warning: Attempt to read property "date" on null in /usr/local/www/websvn.planix.org/blame.php on line 247

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – tendra.SVN – Blame – /trunk/src/tools/tpl/readstreams.c – Rev 6

Subversion Repositories tendra.SVN

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 7u83 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
 */
31
/*
32
    		 Crown Copyright (c) 1997
33
 
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:-
42
 
43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
45
 
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;
49
 
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;
53
 
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
#include "config.h"
62
#include "util.h"
63
#include "readstreams.h"
64
#include "enc_nos.h"
65
#include "errors.h"
66
#include "decodings.h"
67
#include "defs.h"
68
 
69
 
70
Instream *curin;
71
 
72
unsigned int
73
get_bit(void)
74
{
75
	unsigned int x;
76
	Chunk *ch = curin->ch;
77
	if (ch == (Chunk *)0) {
78
		return 2; /* end of stream */
79
	}
80
	while (ch->usage == 0 && ch->offst == 0) {
81
		Assert(curin->bit_pos == 0 && curin->byte_pos == 0);
82
		ch = curin->ch = ch->next;
83
		if (ch == (Chunk *)0) {
84
			return 2; /* end of stream */
85
		}
86
	}
87
	x = UI(ch->data[curin->byte_pos]) >> (7 - curin->bit_pos);
88
	if (++curin->bit_pos >= 8) {
89
		curin->bit_pos = 0;
90
		curin->byte_pos++;
91
	}
92
	if (curin->byte_pos == ch->usage && curin->bit_pos == ch->offst) {
93
		curin->ch = ch->next;
94
		curin->byte_pos = 0;
95
		curin->bit_pos = 0;
96
	}
97
	return(x & 1);
98
}
99
 
100
 
101
unsigned int
102
get_basic_int(int bts, Bool e)
103
{
104
	int i;
105
	unsigned int ans = get_bit();
106
	for (i = 1; i < bts; i++) {
107
		ans = (ans << 1) + get_bit();
108
	}
109
	if (e && ans==0) {
110
		return(get_basic_int(bts,e) + (UI(1) <<bts) - 1);
111
	}
112
	return ans;
113
}
114
 
115
 
116
unsigned int
117
get_tdfint(void)
118
{
119
	unsigned int x;
120
	unsigned int ans = 0;
121
	while ((x = get_basic_int(4, 0)) <= 7) {
122
		ans = (ans << 3) + x;
123
	}
124
	return ((ans << 3) + (x - 8));
125
}
126
 
127
 
128
static void
129
indent(int n)
130
{
131
	int i;
132
	for (i = 0; i < n; i++) {
133
		IGNORE printf(((i & 3) == 2) ? " |" : "  ");
134
	}
135
}
136
 
137
 
138
unsigned int
139
d_tdfint(int n)
140
{
141
	unsigned int x = get_tdfint();
142
	indent(n);
143
	IGNORE printf("%u\n", x);
144
	return x;
145
}
146
 
147
 
148
unsigned int
149
d_tdfstring(int n)
150
{
151
	unsigned int i;
152
	int k = (int)get_tdfint();
153
	unsigned int l = get_tdfint();
154
	char *s = CALLOC(char,l);
155
	for (i = 0; i < l; i++) {
156
		s[i] = (char)get_basic_int(k, 0);
157
	}
158
	indent(n);
159
	for (i = 0; i < l; i++) {
160
		IGNORE printf("%c", s[i]);
161
	}
162
	IGNORE printf("\n");
163
	return l;
164
}
165
 
166
 
167
void
168
consname(char *s, int n)
169
{
170
	indent(n);
171
	IGNORE printf("%s\n", s);
172
}
173
 
174
 
175
unsigned int
176
d_tdfbool(int n)
177
{
178
	unsigned int x = get_bit();
179
	indent(n);
180
	IGNORE printf((x) ? "true\n" : "false\n");
181
	return x;
182
}
183
 
184
 
185
unsigned int
186
d_bytestream(int n)
187
{
188
	unsigned int i = get_tdfint();
189
	while (curin->bit_pos != 0) {
190
		IGNORE get_bit();
191
	}
192
	for (; i != 0; i--) {
193
		IGNORE get_basic_int(8, 0);
194
	}
195
	indent(n);
196
	IGNORE printf("BYTESTREAM\n");
197
	return i;
198
}
199
 
200
 
201
unsigned int
202
d_tdfident(int n)
203
{
204
	return d_tdfstring(n);
205
}
206
 
207
 
208
static unsigned int
209
d_X(unsigned int rsort, int n)
210
{
211
	switch (rsort) {
212
	case e_access:
213
		IGNORE d_access(n);
214
		break;
215
	case e_alignment_sort:
216
		IGNORE d_alignment(n);
217
		break;
218
	case e_al_tag:
219
		IGNORE d_al_tag(n);
220
		break;
221
	case e_bitfield_variety:
222
		IGNORE d_bitfield_variety(n);
223
		break;
224
	case e_bool:
225
		IGNORE d_bool(n);
226
		break;
227
	case e_error_treatment:
228
		IGNORE d_error_treatment(n);
229
		break;
230
	case e_exp:
231
		IGNORE d_exp(n);
232
		break;
233
	case e_floating_variety:
234
		IGNORE d_floating_variety(n);
235
		break;
236
	case e_label:
237
		IGNORE d_label(n);
238
		break;
239
	case e_nat:
240
		IGNORE d_nat(n);
241
		break;
242
	case e_ntest:
243
		IGNORE d_ntest(n);
244
		break;
245
	case e_rounding_mode:
246
		IGNORE d_rounding_mode(n);
247
		break;
248
	case e_shape:
249
		IGNORE d_shape(n);
250
		break;
251
	case e_signed_nat:
252
		IGNORE d_signed_nat(n);
253
		break;
254
	case e_tag:
255
		IGNORE d_tag(n);
256
		break;
257
	case e_token:
258
		IGNORE d_token(n);
259
		break;
260
	case e_transfer_mode:
261
		IGNORE d_transfer_mode(n);
262
		break;
263
	case e_variety:
264
		IGNORE d_variety(n);
265
		break;
266
	default:
267
		fail("Not a 1st class SORT\n");
268
	}
269
	return rsort;
270
}
271
 
272
 
273
static unsigned int
274
d_token_definition(int n)
275
{
276
	unsigned int srt;
277
	unsigned int nl;
278
	IGNORE get_basic_int(1, 0);
279
	srt = d_sortname(n);
280
	IGNORE get_basic_int(1, 0);
281
	nl = get_tdfint();
282
	for (; nl != 0; nl--) {
283
		IGNORE d_tokformals(n);
284
	}
285
	IGNORE d_X(srt, n + 1);
286
	return srt;
287
}
288
 
289
 
290
unsigned int
291
d_bitstream(char *s, int n)
292
{
293
	unsigned int i = get_tdfint();
294
	int l = (int)strlen(s);
295
	indent(n);
296
	IGNORE printf("BITSTREAM/%s\n", s);
297
	if (strcmp(s + l - 5, "_cond") == 0) {
298
		int j = 0;
299
		for (; i != 0; i--) {
300
			if (((j++) & 31) == 0) {
301
				IGNORE printf("\n");
302
				indent(n);
303
			}
304
			IGNORE printf("%u", get_bit());
305
		}
306
		IGNORE printf("\n");
307
	} else if (strcmp(s + l - 12, "_apply_token") == 0) {
308
		int j = 0;
309
		for (; i != 0; i--) {
310
			if (((j++) & 31) == 0) {
311
				IGNORE printf("\n");
312
				indent(n);
313
			}
314
			IGNORE printf("%u", get_bit());
315
		}
316
		IGNORE printf("\n");
317
	} else if (strcmp(s, "make_tokdef") == 0) {
318
		IGNORE d_token_definition(n + 1);
319
	} else if (strcmp(s, "use_tokdef") == 0) {
320
		IGNORE d_token_definition(n + 1);
321
	} else {
322
		IGNORE fprintf(stderr, "Don't understand bitstream.\n");
323
		exit(EXIT_FAILURE);
324
	}
325
	return i;
326
}
327
 
328
 
329
void
330
read_cur(unsigned int(*f)(int))
331
{
332
	curin = MALLOC(Instream);
333
	curin->ch = current_TDF->first;
334
	curin->byte_pos = 0;
335
	curin->bit_pos = 0;
336
	IGNORE f(0);
337
}
338
 
339
 
340
void
341
print_res(void)
342
{
343
	unsigned int i;
344
	TDF *hold = current_TDF;
345
	SELECT_UNIT(tokdec_unit);
346
	for (i = 0; i < current_TDF->no; i++) {
347
		if (i == 0) {
348
			read_cur(d_tokdec);
349
		}
350
		else {
351
			IGNORE d_tokdec(0);
352
		}
353
	}
354
	SELECT_UNIT(tokdef_unit);
355
	for (i = 0; i < current_TDF->no; i++) {
356
		if (i == 0) {
357
			read_cur(d_tokdef);
358
		}
359
		else {
360
			IGNORE d_tokdef(0);
361
		}
362
	}
363
	SELECT_UNIT(tagdec_unit);
364
	for (i = 0; i < current_TDF->no; i++) {
365
		if (i == 0) {
366
			read_cur(d_tagdec);
367
		}
368
		else {
369
			IGNORE d_tagdec(0);
370
		}
371
	}
372
	SELECT_UNIT(tagdef_unit);
373
	for (i = 0; i < current_TDF->no; i++) {
374
		if (i == 0) {
375
			read_cur(d_tagdef);
376
		}
377
		else {
378
			IGNORE d_tagdef(0);
379
		}
380
	}
381
	current_TDF = hold;
382
}