Subversion Repositories tendra.SVN

Rev

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

Rev 5 Rev 6
Line -... Line 1...
-
 
1
/*
-
 
2
 * Copyright (c) 2002-2005 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 44... Line 74...
44
    ENCODE AN EXTENDED VALUE
74
    ENCODE AN EXTENDED VALUE
45
 
75
 
46
    The extended value n is encoded in b bits on the bitstream p.
76
    The extended value n is encoded in b bits on the bitstream p.
47
*/
77
*/
48
 
78
 
49
void enc_bits_extn
79
void
50
    PROTO_N ( ( p, b, n ) )
-
 
51
    PROTO_T ( bitstream *p X int b X long n )
80
enc_bits_extn(bitstream *p, int b, long n)
52
{
81
{
53
    long m = ( ( 1 << b ) - 1 ) ;
82
    long m = ((1 << b) - 1);
54
    if ( n == 0 ) fatal_error ( "Can't encode 0 as an extended value" ) ;
83
    if (n == 0)fatal_error("Can't encode 0 as an extended value");
55
    while ( n > m ) {
84
    while (n > m) {
56
	enc_bits ( p, b, ( long ) 0 ) ;
85
	enc_bits(p, b,(long)0);
57
	n -= m ;
86
	n -= m;
58
    }
87
    }
59
    enc_bits ( p, b, n ) ;
88
    enc_bits(p, b, n);
60
    return ;
89
    return;
61
}
90
}
62
 
91
 
63
 
92
 
64
/*
93
/*
65
    AUXILIARY TDF INTEGER ENCODING ROUTINE
94
    AUXILIARY TDF INTEGER ENCODING ROUTINE
66
 
95
 
67
    The value n is encoded as a series of octal digits into the
96
    The value n is encoded as a series of octal digits into the
68
    bitstream p.
97
    bitstream p.
69
*/
98
*/
70
 
99
 
71
static void enc_tdf_int_aux
100
static void
72
    PROTO_N ( ( p, n ) )
-
 
73
    PROTO_T ( bitstream *p X long n )
101
enc_tdf_int_aux(bitstream *p, long n)
74
{
102
{
75
    unsigned long m = ( unsigned long ) n ;
103
    unsigned long m = (unsigned long)n;
76
    if ( m >= 8 ) enc_tdf_int_aux ( p, ( long ) ( m >> 3 ) ) ;
104
    if (m >= 8)enc_tdf_int_aux(p,(long)(m >> 3));
77
    enc_bits ( p, 4, ( long ) ( m & 7 ) ) ;
105
    enc_bits(p, 4,(long)(m & 7));
78
    return ;
106
    return;
79
}
107
}
80
 
108
 
81
 
109
 
82
/*
110
/*
83
    ENCODE A TDF INTEGER
111
    ENCODE A TDF INTEGER
84
 
112
 
85
    The value n is encoded as a TDF integer into the bitstream p.
113
    The value n is encoded as a TDF integer into the bitstream p.
86
*/
114
*/
87
 
115
 
88
void enc_tdf_int
116
void
89
    PROTO_N ( ( p, n ) )
-
 
90
    PROTO_T ( bitstream *p X long n )
117
enc_tdf_int(bitstream *p, long n)
91
{
118
{
92
    unsigned long m = ( unsigned long ) n ;
119
    unsigned long m = (unsigned long)n;
93
    if ( m >= 8 ) enc_tdf_int_aux ( p, ( long ) ( m >> 3 ) ) ;
120
    if (m >= 8)enc_tdf_int_aux(p,(long)(m >> 3));
94
    enc_bits ( p, 4, ( long ) ( ( m & 7 ) | 8 ) ) ;
121
    enc_bits(p, 4,(long)((m & 7) | 8));
95
    return ;
122
    return;
96
}
123
}
97
 
124
 
98
 
125
 
99
/*
126
/*
100
    ENCODE AN ALIGNED STRING
127
    ENCODE AN ALIGNED STRING
Line 102... Line 129...
102
    The string s is encoded as an aligned string into the bitstream p.
129
    The string s is encoded as an aligned string into the bitstream p.
103
    n is either the length of s, or -1, indicating that strlen should
130
    n is either the length of s, or -1, indicating that strlen should
104
    be used to find the length.
131
    be used to find the length.
105
*/
132
*/
106
 
133
 
107
void enc_aligned_string
134
void
108
    PROTO_N ( ( p, s, n ) )
-
 
109
    PROTO_T ( bitstream *p X char *s X long n )
135
enc_aligned_string(bitstream *p, char *s, long n)
110
{
136
{
111
    long i ;
137
    long i;
112
    if ( n == -1 ) n = ( long ) strlen ( s ) ;
138
    if (n == -1)n = (long)strlen(s);
113
    enc_tdf_int ( p, ( long ) 8 ) ;
139
    enc_tdf_int(p,(long)8);
114
    enc_tdf_int ( p, n ) ;
140
    enc_tdf_int(p, n);
115
    align_bitstream ( p ) ;
141
    align_bitstream(p);
116
    for ( i = 0 ; i < n ; i++ ) {
142
    for (i = 0; i < n; i++) {
117
	long c = ( long ) s [i] ;
143
	long c = (long)s[i];
118
	enc_bits ( p, 8, c ) ;
144
	enc_bits(p, 8, c);
119
    }
145
    }
120
    return ;
146
    return;
121
}
147
}
122
 
148
 
123
 
149
 
124
/*
150
/*
125
    ENCODE AN EXTERNAL NAME
151
    ENCODE AN EXTERNAL NAME
126
 
152
 
127
    The external name of the construct p is encoded into the bitstream b.
153
    The external name of the construct p is encoded into the bitstream b.
128
*/
154
*/
129
 
155
 
130
void enc_external
156
void
131
    PROTO_N ( ( b, p ) )
-
 
132
    PROTO_T ( bitstream *b X construct *p )
157
enc_external(bitstream *b, construct *p)
133
{
158
{
134
    node *e = p->ename ;
159
    node *e = p->ename;
135
    enc_tdf_int ( b, p->encoding ) ;
160
    enc_tdf_int(b, p->encoding);
136
    if ( e->cons->encoding ) {
161
    if (e->cons->encoding) {
137
	node *q = e->son ;
162
	node *q = e->son;
138
	if ( q->cons->sortnum == SORT_tdfstring ) {
163
	if (q->cons->sortnum == SORT_tdfstring) {
139
	    node *r = q->bro ;
164
	    node *r = q->bro;
140
	    if ( r == null ) {
165
	    if (r == null) {
141
		enc_external_bits ( b, ENC_string_extern ) ;
166
		enc_external_bits(b, ENC_string_extern);
142
		align_bitstream ( b ) ;
167
		align_bitstream(b);
143
		enc_aligned_string ( b, q->cons->name, q->cons->encoding ) ;
168
		enc_aligned_string(b, q->cons->name, q->cons->encoding);
144
	    } else {
169
	    } else {
145
		enc_external_bits ( b, ENC_chain_extern ) ;
170
		enc_external_bits(b, ENC_chain_extern);
146
		align_bitstream ( b ) ;
171
		align_bitstream(b);
147
		enc_aligned_string ( b, q->cons->name, q->cons->encoding ) ;
172
		enc_aligned_string(b, q->cons->name, q->cons->encoding);
148
		enc_node ( b, r ) ;
173
		enc_node(b, r);
149
	    }
174
	    }
150
	} else {
175
	} else {
151
	    enc_external_bits ( b, ENC_unique_extern ) ;
176
	    enc_external_bits(b, ENC_unique_extern);
152
	    align_bitstream ( b ) ;
177
	    align_bitstream(b);
153
	    enc_tdf_int ( b, q->cons->encoding ) ;
178
	    enc_tdf_int(b, q->cons->encoding);
154
	    for ( q = e->son->son ; q ; q = q->bro ) {
179
	    for (q = e->son->son; q; q = q->bro) {
155
		enc_aligned_string ( b, q->cons->name, q->cons->encoding ) ;
180
		enc_aligned_string(b, q->cons->name, q->cons->encoding);
156
	    }
181
	    }
157
	}
182
	}
158
    } else {
183
    } else {
159
	enc_external_bits ( b, ENC_string_extern ) ;
184
	enc_external_bits(b, ENC_string_extern);
160
	align_bitstream ( b ) ;
185
	align_bitstream(b);
161
	enc_aligned_string ( b, p->name, ( long ) -1 ) ;
186
	enc_aligned_string(b, p->name,(long) -1);
162
    }
187
    }
163
    return ;
188
    return;
164
}
189
}
165
 
190
 
166
 
191
 
167
/*
192
/*
168
    FIND THE VALUE OF A STRING OF OCTAL DIGITS
193
    FIND THE VALUE OF A STRING OF OCTAL DIGITS
169
 
194
 
170
    The value of the node p, which represents a number, is returned.
195
    The value of the node p, which represents a number, is returned.
171
*/
196
*/
172
 
197
 
173
static long octval
198
static long
174
    PROTO_N ( ( p ) )
-
 
175
    PROTO_T ( node *p )
199
octval(node *p)
176
{
200
{
177
    long n = ( long ) octal_to_ulong ( p->bro->cons->name ) ;
201
    long n = (long)octal_to_ulong(p->bro->cons->name);
178
    if ( p->cons->encoding ) n = -n ;
202
    if (p->cons->encoding)n = -n;
179
    return ( n ) ;
203
    return(n);
180
}
204
}
181
 
205
 
182
 
206
 
183
/*
207
/*
184
    ENCODE A NODE
208
    ENCODE A NODE
185
 
209
 
186
    The node p is encoded into the bitstream b.
210
    The node p is encoded into the bitstream b.
187
*/
211
*/
188
 
212
 
189
void enc_node
213
void
190
    PROTO_N ( ( b, p ) )
-
 
191
    PROTO_T ( bitstream *b X node *p )
214
enc_node(bitstream *b, node *p)
192
{
215
{
193
    while ( p ) {
216
    while (p) {
194
	construct *q = p->cons ;
217
	construct *q = p->cons;
195
	switch ( q->sortnum ) {
218
	switch (q->sortnum) {
196
 
219
 
197
	    case SORT_tdfbool : {
220
	    case SORT_tdfbool: {
198
		/* Encode a bit */
221
		/* Encode a bit */
199
		enc_bits ( b, 1, q->encoding ) ;
222
		enc_bits(b, 1, q->encoding);
200
		break ;
223
		break;
201
	    }
224
	    }
202
 
225
 
203
	    case SORT_bytestream : {
226
	    case SORT_bytestream: {
204
		/* Encode a bytestream */
227
		/* Encode a bytestream */
205
		bitstream *c = new_bitstream () ;
228
		bitstream *c = new_bitstream();
206
		enc_node ( c, p->son ) ;
229
		enc_node(c, p->son);
207
		enc_tdf_int ( b, bitstream_length ( c ) ) ;
230
		enc_tdf_int(b, bitstream_length(c));
208
		join_bitstreams ( b, c ) ;
231
		join_bitstreams(b, c);
209
		break ;
232
		break;
210
	    }
233
	    }
211
 
234
 
212
	    case SORT_completion : {
235
	    case SORT_completion: {
213
		/* Encode a completion */
236
		/* Encode a completion */
214
		if ( p->son ) enc_node ( b, p->son ) ;
237
		if (p->son)enc_node(b, p->son);
215
		break ;
238
		break;
216
	    }
239
	    }
217
 
240
 
218
	    case SORT_small_tdfint : {
241
	    case SORT_small_tdfint: {
219
		/* Encode a small integer */
242
		/* Encode a small integer */
220
		enc_tdf_int ( b, q->encoding ) ;
243
		enc_tdf_int(b, q->encoding);
221
		break ;
244
		break;
222
	    }
245
	    }
223
 
246
 
224
	    case SORT_tdfint : {
247
	    case SORT_tdfint: {
225
		/* Encode a number */
248
		/* Encode a number */
226
		char *num = q->name ;
249
		char *num = q->name;
227
		while ( *num ) {
250
		while (*num) {
228
		    long d = ( long ) ( *num - '0' ) ;
251
		    long d = (long)(*num - '0');
229
		    num++ ;
252
		    num++;
230
		    if ( *num == 0 ) d |= 8 ;
253
		    if (*num == 0)d |= 8;
231
		    enc_bits ( b, 4, d ) ;
254
		    enc_bits(b, 4, d);
232
		}
255
		}
233
		break ;
256
		break;
234
	    }
257
	    }
235
 
258
 
236
	    case SORT_option : {
259
	    case SORT_option: {
237
		/* Encode an optional argument */
260
		/* Encode an optional argument */
238
		if ( p->son ) {
261
		if (p->son) {
239
		    enc_bits ( b, 1, ( long ) 1 ) ;
262
		    enc_bits(b, 1,(long)1);
240
		    enc_node ( b, p->son ) ;
263
		    enc_node(b, p->son);
241
		} else {
264
		} else {
242
		    enc_bits ( b, 1, ( long ) 0 ) ;
265
		    enc_bits(b, 1,(long)0);
243
		}
266
		}
244
		break ;
267
		break;
245
	    }
268
	    }
246
 
269
 
247
	    case SORT_repeat : {
270
	    case SORT_repeat: {
248
		/* Encode a repeated argument */
271
		/* Encode a repeated argument */
249
		enc_list_start ( b ) ;
272
		enc_list_start(b);
250
		enc_tdf_int ( b, q->encoding ) ;
273
		enc_tdf_int(b, q->encoding);
251
		if ( p->son ) enc_node ( b, p->son ) ;
274
		if (p->son)enc_node(b, p->son);
252
		break ;
275
		break;
253
	    }
276
	    }
254
 
277
 
255
	    case SORT_tdfstring : {
278
	    case SORT_tdfstring: {
256
		/* Encode a string */
279
		/* Encode a string */
257
		long i, n = q->encoding ;
280
		long i, n = q->encoding;
258
		if ( n == -1 ) {
281
		if (n == -1) {
259
		    node *r = p->son ;
282
		    node *r = p->son;
260
		    long m = octval ( r ) ;
283
		    long m = octval(r);
261
		    if ( m < 0 ) m = -m ;
284
		    if (m < 0)m = -m;
262
		    r = r->bro->bro ;
285
		    r = r->bro->bro;
263
		    n = r->cons->encoding ;
286
		    n = r->cons->encoding;
264
		    r = r->son ;
287
		    r = r->son;
265
		    enc_tdf_int ( b, m ) ;
288
		    enc_tdf_int(b, m);
266
		    enc_tdf_int ( b, n ) ;
289
		    enc_tdf_int(b, n);
267
		    for ( i = 0 ; i < n ; i++ ) {
290
		    for (i = 0; i < n; i++) {
268
			enc_bits ( b, ( int ) m, octval ( r ) ) ;
291
			enc_bits(b,(int)m, octval(r));
269
			r = r->bro->bro ;
292
			r = r->bro->bro;
270
		    }
293
		    }
271
		} else {
294
		} else {
272
		    enc_tdf_int ( b, ( long ) 8 ) ;
295
		    enc_tdf_int(b,(long)8);
273
		    enc_tdf_int ( b, n ) ;
296
		    enc_tdf_int(b, n);
274
		    for ( i = 0 ; i < n ; i++ ) {
297
		    for (i = 0; i < n; i++) {
275
			long c = ( long ) q->name [i] ;
298
			long c = (long)q->name[i];
276
			enc_bits ( b, 8, c ) ;
299
			enc_bits(b, 8, c);
277
		    }
300
		    }
278
		}
301
		}
279
		break ;
302
		break;
280
	    }
303
	    }
281
 
304
 
282
	    case SORT_unknown : {
305
	    case SORT_unknown: {
283
		/* Encode an unknown bitstream */
306
		/* Encode an unknown bitstream */
284
		fatal_error ( "Can't encode unknown bitstream" ) ;
307
		fatal_error("Can't encode unknown bitstream");
285
		break ;
308
		break;
286
	    }
309
	    }
287
 
310
 
288
	    case SORT_al_tag : {
311
	    case SORT_al_tag: {
289
		/* Encode an alignment tag */
312
		/* Encode an alignment tag */
290
		long e = q->encoding ;
313
		long e = q->encoding;
291
		enc_al_tag_bits ( b, ( int ) e ) ;
314
		enc_al_tag_bits(b,(int)e);
292
		if ( e == ENC_make_al_tag ) {
315
		if (e == ENC_make_al_tag) {
293
		    enc_tdf_int ( b, p->son->cons->encoding ) ;
316
		    enc_tdf_int(b, p->son->cons->encoding);
294
		} else {
317
		} else {
295
		    if ( p->son ) enc_node ( b, p->son ) ;
318
		    if (p->son)enc_node(b, p->son);
296
		}
319
		}
297
		break ;
320
		break;
298
	    }
321
	    }
299
 
322
 
300
	    case SORT_label : {
323
	    case SORT_label: {
301
		/* Encode a label */
324
		/* Encode a label */
302
		long e = q->encoding ;
325
		long e = q->encoding;
303
		enc_label_bits ( b, ( int ) e ) ;
326
		enc_label_bits(b,(int)e);
304
		if ( e == ENC_make_label ) {
327
		if (e == ENC_make_label) {
305
		    enc_tdf_int ( b, p->son->cons->encoding ) ;
328
		    enc_tdf_int(b, p->son->cons->encoding);
306
		} else {
329
		} else {
307
		    if ( p->son ) enc_node ( b, p->son ) ;
330
		    if (p->son)enc_node(b, p->son);
308
		}
331
		}
309
		break ;
332
		break;
310
	    }
333
	    }
311
 
334
 
312
	    case SORT_tag : {
335
	    case SORT_tag: {
313
		/* Encode a tag */
336
		/* Encode a tag */
314
		long e = q->encoding ;
337
		long e = q->encoding;
315
		enc_tag_bits ( b, ( int ) e ) ;
338
		enc_tag_bits(b,(int)e);
316
		if ( e == ENC_make_tag ) {
339
		if (e == ENC_make_tag) {
317
		    enc_tdf_int ( b, p->son->cons->encoding ) ;
340
		    enc_tdf_int(b, p->son->cons->encoding);
318
		} else {
341
		} else {
319
		    if ( p->son ) enc_node ( b, p->son ) ;
342
		    if (p->son)enc_node(b, p->son);
320
		}
343
		}
321
		break ;
344
		break;
322
	    }
345
	    }
323
 
346
 
324
	    case SORT_token : {
347
	    case SORT_token: {
325
		/* Encode a token */
348
		/* Encode a token */
326
		tok_info *info = get_tok_info ( q ) ;
349
		tok_info *info = get_tok_info(q);
327
		if ( is_high ( info->res ) ) {
350
		if (is_high(info->res)) {
328
		    enc_token_bits ( b, ENC_token_apply_token ) ;
351
		    enc_token_bits(b, ENC_token_apply_token);
329
		    enc_token_bits ( b, ENC_make_tok ) ;
352
		    enc_token_bits(b, ENC_make_tok);
330
		    enc_tdf_int ( b, q->encoding ) ;
353
		    enc_tdf_int(b, q->encoding);
331
		    enc_tdf_int ( b, ( long ) 0 ) ;
354
		    enc_tdf_int(b,(long)0);
332
		} else {
355
		} else {
333
		    enc_token_bits ( b, ENC_make_tok ) ;
356
		    enc_token_bits(b, ENC_make_tok);
334
		    enc_tdf_int ( b, q->encoding ) ;
357
		    enc_tdf_int(b, q->encoding);
335
		}
358
		}
336
		if ( p->son ) {
359
		if (p->son) {
337
		    if ( p->son->cons != &token_cons ) {
360
		    if (p->son->cons != &token_cons) {
338
			bitstream *c = new_bitstream () ;
361
			bitstream *c = new_bitstream();
339
			enc_node ( c, p->son ) ;
362
			enc_node(c, p->son);
340
			enc_tdf_int ( b, bitstream_length ( c ) ) ;
363
			enc_tdf_int(b, bitstream_length(c));
341
			join_bitstreams ( b, c ) ;
364
			join_bitstreams(b, c);
342
		    }
365
		    }
343
		} else {
366
		} else {
344
		    enc_tdf_int ( b, ( long ) 0 ) ;
367
		    enc_tdf_int(b,(long)0);
345
		}
368
		}
346
		break ;
369
		break;
347
	    }
370
	    }
348
 
371
 
349
	    default : {
372
	    default : {
350
		/* Encode a simple sort */
373
		/* Encode a simple sort */
351
		int bits = sort_encoding [ q->sortnum ] ;
374
		int bits = sort_encoding[q->sortnum];
352
		int extn = sort_extension [ q->sortnum ] ;
375
		int extn = sort_extension[q->sortnum];
353
		if ( extn ) {
376
		if (extn) {
354
		    enc_bits_extn ( b, bits, q->encoding ) ;
377
		    enc_bits_extn(b, bits, q->encoding);
355
		} else {
378
		} else {
356
		    enc_bits ( b, bits, q->encoding ) ;
379
		    enc_bits(b, bits, q->encoding);
357
		}
380
		}
358
		if ( p->son ) enc_node ( b, p->son ) ;
381
		if (p->son)enc_node(b, p->son);
359
		break ;
382
		break;
360
	    }
383
	    }
361
	}
384
	}
362
	p = p->bro ;
385
	p = p->bro;
363
    }
386
    }
364
    return ;
387
    return;
365
}
388
}
366
 
389
 
367
 
390
 
368
/*
391
/*
369
    ENCODE A SORT
392
    ENCODE A SORT
370
*/
393
*/
371
 
394
 
372
static void enc_sort
395
static void
373
    PROTO_N ( ( b, s ) )
-
 
374
    PROTO_T ( bitstream *b X sortname s )
396
enc_sort(bitstream *b, sortname s)
375
{
397
{
376
    if ( is_high ( s ) ) {
398
    if (is_high(s)) {
377
	int i ;
399
	int i;
378
	high_sort *h = high_sorts + high_no ( s ) ;
400
	high_sort *h = high_sorts + high_no(s);
379
	enc_sort ( b, SORT_token ) ;
401
	enc_sort(b, SORT_token);
380
	enc_sort ( b, h->res ) ;
402
	enc_sort(b, h->res);
381
	enc_list_start ( b ) ;
403
	enc_list_start(b);
382
	enc_tdf_int ( b, ( long ) h->no_args ) ;
404
	enc_tdf_int(b,(long)h->no_args);
383
	for ( i = 0 ; i < h->no_args ; i++ ) {
405
	for (i = 0; i < h->no_args; i++) {
384
	    enc_sort ( b, h->args [i] ) ;
406
	    enc_sort(b, h->args[i]);
385
	}
407
	}
386
    } else {
408
    } else {
387
	enc_sortname_bits ( b, s ) ;
409
	enc_sortname_bits(b, s);
388
    }
410
    }
389
    return ;
411
    return;
390
}
412
}
391
 
413
 
392
 
414
 
393
/*
415
/*
394
    ALIGNMENT TAG DEFINITION AUXILIARY ENCODING ROUTINE
416
    ALIGNMENT TAG DEFINITION AUXILIARY ENCODING ROUTINE
395
 
417
 
396
    The definition of the alignment tag p is encoded into the bitstream p.
418
    The definition of the alignment tag p is encoded into the bitstream p.
397
*/
419
*/
398
 
420
 
399
void enc_aldef
421
void
400
    PROTO_N ( ( b, p ) )
-
 
401
    PROTO_T ( bitstream *b X construct *p )
422
enc_aldef(bitstream *b, construct *p)
402
{
423
{
403
    al_tag_info *info = get_al_tag_info ( p ) ;
424
    al_tag_info *info = get_al_tag_info(p);
404
    enc_al_tagdef_bits ( b, ENC_make_al_tagdef ) ;
425
    enc_al_tagdef_bits(b, ENC_make_al_tagdef);
405
    enc_tdf_int ( b, p->encoding ) ;
426
    enc_tdf_int(b, p->encoding);
406
    enc_node ( b, info->def ) ;
427
    enc_node(b, info->def);
407
    return ;
428
    return;
408
}
429
}
409
 
430
 
410
 
431
 
411
/*
432
/*
412
    TAG DECLARATION AUXILIARY ENCODING ROUTINE
433
    TAG DECLARATION AUXILIARY ENCODING ROUTINE
413
 
434
 
414
    The declaration of the tag p is encoded into the bitstream p.
435
    The declaration of the tag p is encoded into the bitstream p.
415
*/
436
*/
416
 
437
 
417
void enc_tagdec
438
void
418
    PROTO_N ( ( b, p ) )
-
 
419
    PROTO_T ( bitstream *b X construct *p )
439
enc_tagdec(bitstream *b, construct *p)
420
{
440
{
421
    int m = 0 ;
441
    int m = 0;
422
    tag_info *info = get_tag_info ( p ) ;
442
    tag_info *info = get_tag_info(p);
423
    switch ( info->var ) {
443
    switch (info->var) {
424
	case 0 : m = ENC_make_id_tagdec ; break ;
444
	case 0: m = ENC_make_id_tagdec; break;
425
	case 1 : m = ENC_make_var_tagdec ; break ;
445
	case 1: m = ENC_make_var_tagdec; break;
426
	case 2 : m = ENC_common_tagdec ; break ;
446
	case 2: m = ENC_common_tagdec; break;
427
    }
447
    }
428
    enc_tagdec_bits ( b, m ) ;
448
    enc_tagdec_bits(b, m);
429
    enc_tdf_int ( b, p->encoding ) ;
449
    enc_tdf_int(b, p->encoding);
430
    enc_node ( b, info->dec ) ;
450
    enc_node(b, info->dec);
431
    return ;
451
    return;
432
}
452
}
433
 
453
 
434
 
454
 
435
/*
455
/*
436
    TAG DEFINITION AUXILIARY ENCODING ROUTINE
456
    TAG DEFINITION AUXILIARY ENCODING ROUTINE
Line 438... Line 458...
438
    The definition of the tag p is encoded into the bitstream p.  Because
458
    The definition of the tag p is encoded into the bitstream p.  Because
439
    of common_tagdef, there may actually be more than one definition.
459
    of common_tagdef, there may actually be more than one definition.
440
    The number of definitions is returned.
460
    The number of definitions is returned.
441
*/
461
*/
442
 
462
 
443
int enc_tagdef
463
int
444
    PROTO_N ( ( b, p ) )
-
 
445
    PROTO_T ( bitstream *b X construct *p )
464
enc_tagdef(bitstream *b, construct *p)
446
{
465
{
447
    int n = 0 ;
466
    int n = 0;
448
    int m = 0 ;
467
    int m = 0;
449
    tag_info *info = get_tag_info ( p ) ;
468
    tag_info *info = get_tag_info(p);
450
    node *d = info->def ;
469
    node *d = info->def;
451
    switch ( info->var ) {
470
    switch (info->var) {
452
	case 0 : m = ENC_make_id_tagdef ; break ;
471
	case 0: m = ENC_make_id_tagdef; break;
453
	case 1 : m = ENC_make_var_tagdef ; break ;
472
	case 1: m = ENC_make_var_tagdef; break;
454
	case 2 : m = ENC_common_tagdef ; break ;
473
	case 2: m = ENC_common_tagdef; break;
455
    }
474
    }
456
    while ( d ) {
475
    while (d) {
457
	/* Can have multiple definitions */
476
	/* Can have multiple definitions */
458
	enc_tagdef_bits ( b, m ) ;
477
	enc_tagdef_bits(b, m);
459
	enc_tdf_int ( b, p->encoding ) ;
478
	enc_tdf_int(b, p->encoding);
460
	enc_node ( b, d->son ) ;
479
	enc_node(b, d->son);
461
	d = d->bro ;
480
	d = d->bro;
462
	n++ ;
481
	n++;
463
    }
482
    }
464
    return ( n ) ;
483
    return(n);
465
}
484
}
466
 
485
 
467
 
486
 
468
/*
487
/*
469
    WORK OUT THE NUMBER OF FORMAL ARGUMENTS GIVEN A STRING
488
    WORK OUT THE NUMBER OF FORMAL ARGUMENTS GIVEN A STRING
470
*/
489
*/
471
 
490
 
472
static long no_formals
491
static long
473
    PROTO_N ( ( args ) )
-
 
474
    PROTO_T ( char *args )
492
no_formals(char *args)
475
{
493
{
476
    long n = 0 ;
494
    long n = 0;
477
    while ( *args ) {
495
    while (*args) {
478
	args = find_sortname ( args, ( sortname * ) null ) ;
496
	args = find_sortname(args,(sortname *)null);
479
	args++ ;
497
	args++;
480
	n = n + 1 ;
498
	n = n + 1;
481
    }
499
    }
482
    return ( n ) ;
500
    return(n);
483
}
501
}
484
 
502
 
485
 
503
 
486
/*
504
/*
487
    TOKEN DECLARATION AUXILIARY ENCODING ROUTINE
505
    TOKEN DECLARATION AUXILIARY ENCODING ROUTINE
488
 
506
 
489
    The declaration of the token p is encoded into the bitstream p.
507
    The declaration of the token p is encoded into the bitstream p.
490
*/
508
*/
491
 
509
 
492
void enc_tokdec
510
void
493
    PROTO_N ( ( b, p ) )
-
 
494
    PROTO_T ( bitstream *b X construct *p )
511
enc_tokdec(bitstream *b, construct *p)
495
{
512
{
496
    tok_info *info = get_tok_info ( p ) ;
513
    tok_info *info = get_tok_info(p);
497
    enc_tokdec_bits ( b, ENC_make_tokdec ) ;
514
    enc_tokdec_bits(b, ENC_make_tokdec);
498
    enc_tdf_int ( b, p->encoding ) ;
515
    enc_tdf_int(b, p->encoding);
499
 
516
 
500
    /* Deal with signature */
517
    /* Deal with signature */
501
    if ( info->sig == null ) {
518
    if (info->sig == null) {
502
	enc_bits ( b, 1, ( long ) 0 ) ;
519
	enc_bits(b, 1,(long)0);
503
    } else {
520
    } else {
504
	enc_node ( b, info->sig ) ;
521
	enc_node(b, info->sig);
505
    }
522
    }
506
 
523
 
507
    /* Encode token sort */
524
    /* Encode token sort */
508
    enc_sort ( b, SORT_token ) ;
525
    enc_sort(b, SORT_token);
509
 
526
 
510
    /* Encode the token result sort */
527
    /* Encode the token result sort */
511
    enc_sort ( b, info->res ) ;
528
    enc_sort(b, info->res);
512
 
529
 
513
    /* Encode the token argument sorts */
530
    /* Encode the token argument sorts */
514
    enc_list_start ( b ) ;
531
    enc_list_start(b);
515
    if ( info->args ) {
532
    if (info->args) {
516
	char *q = info->args ;
533
	char *q = info->args;
517
	enc_tdf_int ( b, no_formals ( q ) ) ;
534
	enc_tdf_int(b, no_formals(q));
518
	while ( *q ) {
535
	while (*q) {
519
	    sortname s ;
536
	    sortname s;
520
	    q = find_sortname ( q, &s ) ;
537
	    q = find_sortname(q, &s);
521
	    q++ ;
538
	    q++;
522
	    enc_sort ( b, s ) ;
539
	    enc_sort(b, s);
523
	}
540
	}
524
    } else {
541
    } else {
525
	enc_tdf_int ( b, ( long ) 0 ) ;
542
	enc_tdf_int(b,(long)0);
526
    }
543
    }
527
    return ;
544
    return;
528
}
545
}
529
 
546
 
530
 
547
 
531
/*
548
/*
532
    TOKEN DEFINITION AUXILIARY ENCODING ROUTINE
549
    TOKEN DEFINITION AUXILIARY ENCODING ROUTINE
533
 
550
 
534
    The definition of the token p is encoded into the bitstream p.
551
    The definition of the token p is encoded into the bitstream p.
535
*/
552
*/
536
 
553
 
537
void enc_tokdef
554
void
538
    PROTO_N ( ( b, p ) )
-
 
539
    PROTO_T ( bitstream *b X construct *p )
555
enc_tokdef(bitstream *b, construct *p)
540
{
556
{
541
    bitstream *c = new_bitstream () ;
557
    bitstream *c = new_bitstream();
542
    tok_info *info = get_tok_info ( p ) ;
558
    tok_info *info = get_tok_info(p);
543
    enc_tokdef_bits ( b, ENC_make_tokdef ) ;
559
    enc_tokdef_bits(b, ENC_make_tokdef);
544
    enc_tdf_int ( b, p->encoding ) ;
560
    enc_tdf_int(b, p->encoding);
545
 
561
 
546
    /* Deal with signature */
562
    /* Deal with signature */
547
    if ( info->sig == null ) {
563
    if (info->sig == null) {
548
	enc_bits ( b, 1, ( long ) 0 ) ;
564
	enc_bits(b, 1,(long)0);
549
    } else {
565
    } else {
550
	enc_node ( b, info->sig ) ;
566
	enc_node(b, info->sig);
551
    }
567
    }
552
 
568
 
553
    /* Encode token definition type */
569
    /* Encode token definition type */
554
    enc_token_defn_bits ( c, ENC_token_definition ) ;
570
    enc_token_defn_bits(c, ENC_token_definition);
555
 
571
 
556
    /* Encode the token result sort */
572
    /* Encode the token result sort */
557
    enc_sort ( c, info->res ) ;
573
    enc_sort(c, info->res);
558
 
574
 
559
    /* Encode the token arguments */
575
    /* Encode the token arguments */
560
    enc_list_start ( c ) ;
576
    enc_list_start(c);
561
    if ( info->args ) {
577
    if (info->args) {
562
	construct **q = info->pars ;
578
	construct **q = info->pars;
563
	enc_tdf_int ( c, no_formals ( info->args ) ) ;
579
	enc_tdf_int(c, no_formals(info->args));
564
	while ( *q ) {
580
	while (*q) {
565
	    tok_info *qinfo = get_tok_info ( *q ) ;
581
	    tok_info *qinfo = get_tok_info(*q);
566
	    enc_sort ( c, qinfo->res ) ;
582
	    enc_sort(c, qinfo->res);
567
	    enc_tdf_int ( c, ( *q )->encoding ) ;
583
	    enc_tdf_int(c,(*q) ->encoding);
568
	    q++ ;
584
	    q++;
569
	}
585
	}
570
    } else {
586
    } else {
571
	enc_tdf_int ( c, ( long ) 0 ) ;
587
	enc_tdf_int(c,(long)0);
572
    }
588
    }
573
 
589
 
574
    /* Encode the token definition */
590
    /* Encode the token definition */
575
    enc_node ( c, info->def ) ;
591
    enc_node(c, info->def);
576
    enc_tdf_int ( b, bitstream_length ( c ) ) ;
592
    enc_tdf_int(b, bitstream_length(c));
577
    join_bitstreams ( b, c ) ;
593
    join_bitstreams(b, c);
578
    return ;
594
    return;
579
}
595
}