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
*/
29
 
59
 
30
 
60
 
31
#include "config.h"
61
#include "config.h"
32
#include "types.h"
62
#include "types.h"
33
#include "enc_types.h"
63
#include "enc_types.h"
-
 
64
#include "enc_cap.h"
34
#include "bitstream.h"
65
#include "bitstream.h"
35
#include "encode.h"
66
#include "encode.h"
36
#include "names.h"
67
#include "names.h"
37
#include "node.h"
68
#include "node.h"
38
#include "table.h"
69
#include "table.h"
Line 44... Line 75...
44
    CURRENT BITSTREAM
75
    CURRENT BITSTREAM
45
 
76
 
46
    This is the main bitstream.
77
    This is the main bitstream.
47
*/
78
*/
48
 
79
 
49
static bitstream *crt_bitstream ;
80
static bitstream *crt_bitstream;
50
 
81
 
51
 
82
 
52
/*
83
/*
53
    NUMBERS OF EQUATIONS AND VARIABLE SORTS
84
    NUMBERS OF EQUATIONS AND VARIABLE SORTS
54
 
85
 
55
    The number of equation types and variable sorts in the output capsule.
86
    The number of equation types and variable sorts in the output capsule.
56
*/
87
*/
57
 
88
 
58
static long eqn_total, var_total ;
89
static long eqn_total, var_total;
59
 
90
 
60
 
91
 
61
/*
92
/*
62
    LINKAGE INFORMATION ENCODING VARIABLES
93
    LINKAGE INFORMATION ENCODING VARIABLES
63
 
94
 
64
    The tld2 unit gives information on the use, declaration and
95
    The tld2 unit gives information on the use, declaration and
65
    definition of the externally named tags and tokens.
96
    definition of the externally named tags and tokens.
66
*/
97
*/
67
 
98
 
68
static bitstream *tld_bs ;
99
static bitstream *tld_bs;
69
 
100
 
70
 
101
 
71
/*
102
/*
72
    ALIGNMENT TAG ENCODING VARIABLES
103
    ALIGNMENT TAG ENCODING VARIABLES
73
 
104
 
74
    The alignment tag definitions are formed in the bitstream al_tag_defs_bs.
105
    The alignment tag definitions are formed in the bitstream al_tag_defs_bs.
75
    There are al_tag_total alignment tags, of which al_tag_external have
106
    There are al_tag_total alignment tags, of which al_tag_external have
76
    external names and al_tag_defs are defined.
107
    external names and al_tag_defs are defined.
77
*/
108
*/
78
 
109
 
79
static long al_tag_total = 0 ;
110
static long al_tag_total = 0;
80
static long al_tag_external = 0 ;
111
static long al_tag_external = 0;
81
static long al_tag_defs = 0 ;
112
static long al_tag_defs = 0;
82
static bitstream *al_tag_defs_bs ;
113
static bitstream *al_tag_defs_bs;
83
 
114
 
84
 
115
 
85
/*
116
/*
86
    AUXILIARY ALIGNMENT TAG ENCODING ROUTINE
117
    AUXILIARY ALIGNMENT TAG ENCODING ROUTINE
87
 
118
 
88
    The alignment tag encoding variables are modified according to the
119
    The alignment tag encoding variables are modified according to the
89
    construct p.
120
    construct p.
90
*/
121
*/
91
 
122
 
92
static void enc_al_tag_aux
123
static void
93
    PROTO_N ( ( p ) )
-
 
94
    PROTO_T ( construct *p )
124
enc_al_tag_aux(construct *p)
95
{
125
{
96
    al_tag_info *info = get_al_tag_info ( p ) ;
126
    al_tag_info *info = get_al_tag_info(p);
97
    al_tag_total++ ;
127
    al_tag_total++;
98
    if ( p->ename ) {
128
    if (p->ename) {
99
	al_tag_external++ ;
129
	al_tag_external++;
100
	enc_tdf_int ( tld_bs, ( long ) ( info->def ? 5 : 1 ) ) ;
130
	enc_tdf_int(tld_bs,(long)(info->def ? 5 : 1));
101
    }
131
    }
102
    if ( info->def == null ) return ;
132
    if (info->def == null) return;
103
    al_tag_defs++ ;
133
    al_tag_defs++;
104
    enc_aldef ( al_tag_defs_bs, p ) ;
134
    enc_aldef(al_tag_defs_bs, p);
105
    return ;
135
    return;
106
}
136
}
107
 
137
 
108
 
138
 
109
/*
139
/*
110
    AUXILIARY ROUTINE FOR ENCODING ALIGNMENT TAG NAMES
140
    AUXILIARY ROUTINE FOR ENCODING ALIGNMENT TAG NAMES
111
 
141
 
112
    The external name (if any) of the alignment tag construct p is encoded.
142
    The external name (if any) of the alignment tag construct p is encoded.
113
*/
143
*/
114
 
144
 
115
static void enc_al_tag_names
145
static void
116
    PROTO_N ( ( p ) )
-
 
117
    PROTO_T ( construct *p )
146
enc_al_tag_names(construct *p)
118
{
147
{
119
    if ( p->ename == null ) return ;
148
    if (p->ename == null) return;
120
    enc_external ( crt_bitstream, p ) ;
149
    enc_external(crt_bitstream, p);
121
    return ;
150
    return;
122
}
151
}
123
 
152
 
124
 
153
 
125
/*
154
/*
126
    TAG ENCODING VARIABLES
155
    TAG ENCODING VARIABLES
Line 129... Line 158...
129
    the definitions in tag_defs_bs.  There are tag_total tags, of
158
    the definitions in tag_defs_bs.  There are tag_total tags, of
130
    which tag_external have external names, tag_decs have declarations
159
    which tag_external have external names, tag_decs have declarations
131
    and tag_defs have definitions.
160
    and tag_defs have definitions.
132
*/
161
*/
133
 
162
 
134
static long tag_total = 0 ;
163
static long tag_total = 0;
135
static long tag_external = 0 ;
164
static long tag_external = 0;
136
static long tag_decs = 0 ;
165
static long tag_decs = 0;
137
static long tag_defs = 0 ;
166
static long tag_defs = 0;
138
static bitstream *tag_decs_bs ;
167
static bitstream *tag_decs_bs;
139
static bitstream *tag_defs_bs ;
168
static bitstream *tag_defs_bs;
140
 
169
 
141
 
170
 
142
/*
171
/*
143
    AUXILIARY TAG ENCODING ROUTINE
172
    AUXILIARY TAG ENCODING ROUTINE
144
 
173
 
145
    The tag encoding variables are modified according to the construct p.
174
    The tag encoding variables are modified according to the construct p.
146
*/
175
*/
147
 
176
 
148
static void enc_tag_aux
177
static void
149
    PROTO_N ( ( p ) )
-
 
150
    PROTO_T ( construct *p )
178
enc_tag_aux(construct *p)
151
{
179
{
152
    tag_info *info = get_tag_info ( p ) ;
180
    tag_info *info = get_tag_info(p);
153
    tag_total++ ;
181
    tag_total++;
154
    if ( info->var == 3 ) return ;
182
    if (info->var == 3) return;
155
    if ( p->ename ) {
183
    if (p->ename) {
156
	tag_external++ ;
184
	tag_external++;
157
	enc_tdf_int ( tld_bs, ( long ) ( info->def ? 7 : 3 ) ) ;
185
	enc_tdf_int(tld_bs,(long)(info->def ? 7 : 3));
158
    }
186
    }
159
    tag_decs++ ;
187
    tag_decs++;
160
    enc_tagdec ( tag_decs_bs, p ) ;
188
    enc_tagdec(tag_decs_bs, p);
161
    if ( info->def == null ) return ;
189
    if (info->def == null) return;
162
    tag_defs += enc_tagdef ( tag_defs_bs, p ) ;
190
    tag_defs += enc_tagdef(tag_defs_bs, p);
163
    return ;
191
    return;
164
}
192
}
165
 
193
 
166
 
194
 
167
/*
195
/*
168
    AUXILIARY ROUTINE FOR ENCODING TAG NAMES
196
    AUXILIARY ROUTINE FOR ENCODING TAG NAMES
169
 
197
 
170
    The external name (if any) of the tag construct p is encoded.
198
    The external name (if any) of the tag construct p is encoded.
171
*/
199
*/
172
 
200
 
173
static void enc_tag_names
201
static void
174
    PROTO_N ( ( p ) )
-
 
175
    PROTO_T ( construct *p )
202
enc_tag_names(construct *p)
176
{
203
{
177
    tag_info *info = get_tag_info ( p ) ;
204
    tag_info *info = get_tag_info(p);
178
    if ( info->var == 3 ) return ;
205
    if (info->var == 3) return;
179
    if ( p->ename == null ) return ;
206
    if (p->ename == null) return;
180
    enc_external ( crt_bitstream, p ) ;
207
    enc_external(crt_bitstream, p);
181
    return ;
208
    return;
182
}
209
}
183
 
210
 
184
 
211
 
185
/*
212
/*
186
    TOKEN ENCODING VARIABLES
213
    TOKEN ENCODING VARIABLES
Line 189... Line 216...
189
    the definitions in tok_defs_bs.  There are tok_total tokens, of
216
    the definitions in tok_defs_bs.  There are tok_total tokens, of
190
    which tok_external have external names, tok_decs have declarations
217
    which tok_external have external names, tok_decs have declarations
191
    and tok_defs have definitions.
218
    and tok_defs have definitions.
192
*/
219
*/
193
 
220
 
194
static long tok_total = 0 ;
221
static long tok_total = 0;
195
static long tok_external = 0 ;
222
static long tok_external = 0;
196
static long tok_decs = 0 ;
223
static long tok_decs = 0;
197
static long tok_defs = 0 ;
224
static long tok_defs = 0;
198
static bitstream *tok_decs_bs ;
225
static bitstream *tok_decs_bs;
199
static bitstream *tok_defs_bs ;
226
static bitstream *tok_defs_bs;
200
 
227
 
201
 
228
 
202
/*
229
/*
203
    AUXILIARY TOKEN ENCODING ROUTINE
230
    AUXILIARY TOKEN ENCODING ROUTINE
204
 
231
 
205
    The token encoding variables are modified according to the construct p.
232
    The token encoding variables are modified according to the construct p.
206
*/
233
*/
207
 
234
 
208
static void enc_token_aux
235
static void
209
    PROTO_N ( ( p ) )
-
 
210
    PROTO_T ( construct *p )
236
enc_token_aux(construct *p)
211
{
237
{
212
    tok_info *info = get_tok_info ( p ) ;
238
    tok_info *info = get_tok_info(p);
213
    if ( p->encoding == -1 ) return ;
239
    if (p->encoding == -1) return;
214
    tok_total++ ;
240
    tok_total++;
215
    if ( info->dec == 0 ) return ;
241
    if (info->dec == 0) return;
216
    if ( p->ename ) {
242
    if (p->ename) {
217
	tok_external++ ;
243
	tok_external++;
218
	if ( info->def ) {
244
	if (info->def) {
219
	    enc_tdf_int ( tld_bs, ( long ) 5 ) ;
245
	    enc_tdf_int(tld_bs,(long)5);
220
	} else {
246
	} else {
221
	    enc_tdf_int ( tld_bs, ( long ) 3 ) ;
247
	    enc_tdf_int(tld_bs,(long)3);
222
	}
248
	}
223
    }
249
    }
224
    if ( info->def == null || !show_tokdefs ) {
250
    if (info->def == null || !show_tokdefs) {
225
	if ( info->args ) {
251
	if (info->args) {
226
	    tok_decs++ ;
252
	    tok_decs++;
227
	    enc_tokdec ( tok_decs_bs, p ) ;
253
	    enc_tokdec(tok_decs_bs, p);
228
	}
254
	}
229
    } else {
255
    } else {
230
	tok_defs++ ;
256
	tok_defs++;
231
	enc_tokdef ( tok_defs_bs, p ) ;
257
	enc_tokdef(tok_defs_bs, p);
232
    }
258
    }
233
    return ;
259
    return;
234
}
260
}
235
 
261
 
236
 
262
 
237
/*
263
/*
238
    AUXILIARY ROUTINE FOR ENCODING TOKEN NAMES
264
    AUXILIARY ROUTINE FOR ENCODING TOKEN NAMES
239
 
265
 
240
    The external name (if any) of the token construct p is encoded.
266
    The external name (if any) of the token construct p is encoded.
241
*/
267
*/
242
 
268
 
243
static void enc_token_names
269
static void
244
    PROTO_N ( ( p ) )
-
 
245
    PROTO_T ( construct *p )
270
enc_token_names(construct *p)
246
{
271
{
247
    tok_info *info = get_tok_info ( p ) ;
272
    tok_info *info = get_tok_info(p);
248
    if ( p->encoding == -1 ) return ;
273
    if (p->encoding == -1) return;
249
    if ( info->dec == 0 ) return ;
274
    if (info->dec == 0) return;
250
    if ( p->ename == null ) return ;
275
    if (p->ename == null) return;
251
    enc_external ( crt_bitstream, p ) ;
276
    enc_external(crt_bitstream, p);
252
    return ;
277
    return;
253
}
278
}
254
 
279
 
255
 
280
 
256
/*
281
/*
257
    LABEL ENCODING VARIABLES
282
    LABEL ENCODING VARIABLES
258
 
283
 
259
    There are lab_total labels.
284
    There are lab_total labels.
260
*/
285
*/
261
 
286
 
262
static long lab_total = 0 ;
287
static long lab_total = 0;
263
 
288
 
264
 
289
 
265
/*
290
/*
266
    AUXILIARY LABEL ENCODING ROUTINE
291
    AUXILIARY LABEL ENCODING ROUTINE
267
 
292
 
268
    The label encoding variables are modified according to the construct p.
293
    The label encoding variables are modified according to the construct p.
269
*/
294
*/
270
 
295
 
271
/*ARGSUSED*/ static void enc_label_aux
296
/*ARGSUSED*/ static void
272
    PROTO_N ( ( p ) )
-
 
273
    PROTO_T ( construct *p )
297
enc_label_aux(construct *p)
274
{
298
{
275
    UNUSED ( p ) ;
299
    UNUSED(p);
276
    lab_total++ ;
300
    lab_total++;
277
    return ;
301
    return;
278
}
302
}
279
 
303
 
280
 
304
 
281
/*
305
/*
282
    ENCODE A SET OF LINKS
306
    ENCODE A SET OF LINKS
Line 285... Line 309...
285
    nlinks are encoded into the bitstream p.  Since the same numbers
309
    nlinks are encoded into the bitstream p.  Since the same numbers
286
    are used for variable sorts in all the encoded units these links
310
    are used for variable sorts in all the encoded units these links
287
    are identities.
311
    are identities.
288
*/
312
*/
289
 
313
 
290
static void enc_links
314
static void
291
    PROTO_N ( ( p, ntok, nalign, ntag ) )
-
 
292
    PROTO_T ( bitstream *p X long ntok X long nalign X long ntag )
315
enc_links(bitstream *p, long ntok, long nalign, long ntag)
293
{
316
{
294
    long i ;
317
    long i;
295
    enc_tdf_int ( p, var_total ) ;
318
    enc_tdf_int(p, var_total);
296
    if ( tok_total ) enc_tdf_int ( p, ntok ) ;
319
    if (tok_total)enc_tdf_int(p, ntok);
297
    if ( al_tag_total ) enc_tdf_int ( p, nalign ) ;
320
    if (al_tag_total)enc_tdf_int(p, nalign);
298
    if ( tag_total ) enc_tdf_int ( p, ntag ) ;
321
    if (tag_total)enc_tdf_int(p, ntag);
299
    enc_tdf_int ( p, var_total ) ;
322
    enc_tdf_int(p, var_total);
300
 
323
 
301
    /* Token links */
324
    /* Token links */
302
    if ( tok_total ) {
325
    if (tok_total) {
303
	enc_tdf_int ( p, ntok ) ;
326
	enc_tdf_int(p, ntok);
304
	for ( i = 0 ; i < ntok ; i++ ) {
327
	for (i = 0; i < ntok; i++) {
305
	    enc_tdf_int ( p, i ) ;
328
	    enc_tdf_int(p, i);
306
	    enc_tdf_int ( p, i ) ;
329
	    enc_tdf_int(p, i);
307
	}
330
	}
308
    }
331
    }
309
 
332
 
310
    /* Alignment tag links */
333
    /* Alignment tag links */
311
    if ( al_tag_total ) {
334
    if (al_tag_total) {
312
	enc_tdf_int ( p, nalign ) ;
335
	enc_tdf_int(p, nalign);
313
	for ( i = 0 ; i < nalign ; i++ ) {
336
	for (i = 0; i < nalign; i++) {
314
	    enc_tdf_int ( p, i ) ;
337
	    enc_tdf_int(p, i);
315
	    enc_tdf_int ( p, i ) ;
338
	    enc_tdf_int(p, i);
316
	}
339
	}
317
    }
340
    }
318
 
341
 
319
    /* Tag links */
342
    /* Tag links */
320
    if ( tag_total ) {
343
    if (tag_total) {
321
	enc_tdf_int ( p, ntag ) ;
344
	enc_tdf_int(p, ntag);
322
	for ( i = 0 ; i < ntag ; i++ ) {
345
	for (i = 0; i < ntag; i++) {
323
	    enc_tdf_int ( p, i ) ;
346
	    enc_tdf_int(p, i);
324
	    enc_tdf_int ( p, i ) ;
347
	    enc_tdf_int(p, i);
325
	}
348
	}
326
    }
349
    }
327
    return ;
350
    return;
328
}
351
}
329
 
352
 
330
 
353
 
331
/*
354
/*
332
    EQUATION TYPES
355
    EQUATION TYPES
Line 346... Line 369...
346
 
369
 
347
    The ne equations of type t (see above) given in the bitstream q are
370
    The ne equations of type t (see above) given in the bitstream q are
348
    encoded into the bitstream p.
371
    encoded into the bitstream p.
349
*/
372
*/
350
 
373
 
351
static void enc_equation
374
static void
352
    PROTO_N ( ( p, ne, q, t ) )
-
 
353
    PROTO_T ( bitstream *p X long ne X bitstream *q X int t )
375
enc_equation(bitstream *p, long ne, bitstream *q, int t)
354
{
376
{
355
    long n ;
377
    long n;
356
    bitstream *u ;
378
    bitstream *u;
357
 
379
 
358
    if ( ne == 0 ) {
380
    if (ne == 0) {
359
	/* There are no sets of equations */
381
	/* There are no sets of equations */
360
	enc_tdf_int ( p, ( long ) 0 ) ;
382
	enc_tdf_int(p,(long)0);
361
	return ;
383
	return;
362
    }
384
    }
363
 
385
 
364
    /* There is one set of equations */
386
    /* There is one set of equations */
365
    enc_tdf_int ( p, ( long ) 1 ) ;
387
    enc_tdf_int(p,(long)1);
366
    u = new_bitstream () ;
388
    u = new_bitstream();
367
 
389
 
368
    /* Encode the links */
390
    /* Encode the links */
369
    switch ( t ) {
391
    switch (t) {
370
	case EQN_VERS : {
392
	case EQN_VERS: {
371
	    enc_links ( p, ( long ) 0, ( long ) 0, ( long ) 0 ) ;
393
	    enc_links(p,(long)0,(long)0,(long)0);
372
	    enc_tdf_int ( u, ne ) ;
394
	    enc_tdf_int(u, ne);
373
	    break ;
395
	    break;
374
	}
396
	}
375
	case EQN_TLD : {
397
	case EQN_TLD: {
376
	    enc_tdf_int ( p, ( long ) 0 ) ;
398
	    enc_tdf_int(p,(long)0);
377
	    enc_tdf_int ( p, ( long ) 0 ) ;
399
	    enc_tdf_int(p,(long)0);
378
	    break ;
400
	    break;
379
	}
401
	}
380
	case EQN_TOKDEC : {
402
	case EQN_TOKDEC: {
381
	    enc_links ( p, tok_total, ( long ) 0, ( long ) 0 ) ;
403
	    enc_links(p, tok_total,(long)0,(long)0);
382
	    enc_tdf_int ( u, ne ) ;
404
	    enc_tdf_int(u, ne);
383
	    break ;
405
	    break;
384
	}
406
	}
385
	default : {
407
	default : {
386
	    enc_links ( p, tok_total, al_tag_total, tag_total ) ;
408
	    enc_links(p, tok_total, al_tag_total, tag_total);
387
	    enc_tdf_int ( u, lab_total ) ;
409
	    enc_tdf_int(u, lab_total);
388
	    enc_tdf_int ( u, ne ) ;
410
	    enc_tdf_int(u, ne);
389
	    break ;
411
	    break;
390
	}
412
	}
391
    }
413
    }
392
 
414
 
393
    /* Append the body to the links */
415
    /* Append the body to the links */
394
    join_bitstreams ( u, q ) ;
416
    join_bitstreams(u, q);
395
    align_bitstream ( u ) ;
417
    align_bitstream(u);
396
 
418
 
397
    /* Precede links and body by their length in bytes */
419
    /* Precede links and body by their length in bytes */
398
    n = bitstream_length ( u ) ;
420
    n = bitstream_length(u);
399
    enc_tdf_int ( p, ( long ) ( n / BYTESIZE ) ) ;
421
    enc_tdf_int(p,(long)(n / BYTESIZE));
400
    align_bitstream ( p ) ;
422
    align_bitstream(p);
401
    join_bitstreams ( p, u ) ;
423
    join_bitstreams(p, u);
402
    return ;
424
    return;
403
}
425
}
404
 
426
 
405
 
427
 
406
/*
428
/*
407
    VERSION NUMBERS
429
    VERSION NUMBERS
408
 
430
 
409
    These variables give the major and minor version numbers.
431
    These variables give the major and minor version numbers.
410
*/
432
*/
411
 
433
 
412
static char *magic_number = VERSION_capsule ;
434
static char *magic_number = VERSION_capsule;
413
long version_major = VERSION_major ;
435
long version_major = VERSION_major;
414
long version_minor = VERSION_minor ;
436
long version_minor = VERSION_minor;
415
 
437
 
416
 
438
 
417
/*
439
/*
418
    ENCODE A CAPSULE
440
    ENCODE A CAPSULE
419
 
441
 
420
    A complete capsule is encoded.
442
    A complete capsule is encoded.
421
*/
443
*/
422
 
444
 
423
void enc_capsule
445
void
424
    PROTO_Z ()
446
enc_capsule(void)
425
{
447
{
426
    long n ;
448
    long n;
427
    bitstream *vers_bs ;
449
    bitstream *vers_bs;
428
    char *m = magic_number ;
450
    char *m = magic_number;
429
    bitstream *p = new_bitstream () ;
451
    bitstream *p = new_bitstream();
430
 
452
 
431
    /* Map to lowest applicable version number */
453
    /* Map to lowest applicable version number */
432
    if ( version_major == 4 ) {
454
    if (version_major == 4) {
433
	if ( version_minor == 1 ) version_minor = 0 ;
455
	if (version_minor == 1)version_minor = 0;
434
    }
456
    }
435
 
457
 
436
    /* Initialize the equation bitstreams */
458
    /* Initialize the equation bitstreams */
437
    tld_bs = new_bitstream () ;
459
    tld_bs = new_bitstream();
438
    tok_decs_bs = new_bitstream () ;
460
    tok_decs_bs = new_bitstream();
439
    tok_defs_bs = new_bitstream () ;
461
    tok_defs_bs = new_bitstream();
440
    al_tag_defs_bs = new_bitstream () ;
462
    al_tag_defs_bs = new_bitstream();
441
    tag_decs_bs = new_bitstream () ;
463
    tag_decs_bs = new_bitstream();
442
    tag_defs_bs = new_bitstream () ;
464
    tag_defs_bs = new_bitstream();
443
 
465
 
444
    /* Analyse all the tags, tokens etc */
466
    /* Analyse all the tags, tokens etc */
445
    enc_tdf_int ( tld_bs, ( long ) 1 ) ;
467
    enc_tdf_int(tld_bs,(long)1);
446
    apply_to_all ( enc_label_aux, SORT_label ) ;
468
    apply_to_all(enc_label_aux, SORT_label);
447
    apply_to_all ( enc_token_aux, SORT_token ) ;
469
    apply_to_all(enc_token_aux, SORT_token);
448
    apply_to_all ( enc_al_tag_aux, SORT_al_tag ) ;
470
    apply_to_all(enc_al_tag_aux, SORT_al_tag);
449
    apply_to_all ( enc_tag_aux, SORT_tag ) ;
471
    apply_to_all(enc_tag_aux, SORT_tag);
450
 
472
 
451
    /* Check on output options */
473
    /* Check on output options */
452
    if ( !show_tokdecs ) tok_decs = 0 ;
474
    if (!show_tokdecs)tok_decs = 0;
453
    if ( !show_tokdefs ) tok_defs = 0 ;
475
    if (!show_tokdefs)tok_defs = 0;
454
    if ( !show_aldefs ) al_tag_defs = 0 ;
476
    if (!show_aldefs)al_tag_defs = 0;
455
    if ( !show_tagdecs ) tag_decs = 0 ;
477
    if (!show_tagdecs)tag_decs = 0;
456
    if ( !show_tagdefs ) tag_defs = 0 ;
478
    if (!show_tagdefs)tag_defs = 0;
457
 
479
 
458
    /* Output equation types */
480
    /* Output equation types */
459
    eqn_total = 2 ;
481
    eqn_total = 2;
460
    if ( tok_decs ) eqn_total++ ;
482
    if (tok_decs)eqn_total++;
461
    if ( tok_defs ) eqn_total++ ;
483
    if (tok_defs)eqn_total++;
462
    if ( al_tag_defs ) eqn_total++ ;
484
    if (al_tag_defs)eqn_total++;
463
    if ( tag_decs ) eqn_total++ ;
485
    if (tag_decs)eqn_total++;
464
    if ( tag_defs ) eqn_total++ ;
486
    if (tag_defs)eqn_total++;
465
    while ( n = ( long ) *( m++ ), n != 0 ) {
487
    while (n = (long)*(m++), n != 0) {
466
	enc_bits ( p, 8, n ) ;
488
	enc_bits(p, 8, n);
467
    }
489
    }
468
    enc_tdf_int ( p, version_major ) ;
490
    enc_tdf_int(p, version_major);
469
    enc_tdf_int ( p, version_minor ) ;
491
    enc_tdf_int(p, version_minor);
470
    align_bitstream ( p ) ;
492
    align_bitstream(p);
471
    enc_tdf_int ( p, eqn_total ) ;
493
    enc_tdf_int(p, eqn_total);
472
    enc_aligned_string ( p, LINK_tld_props, ( long ) -1 ) ;
494
    enc_aligned_string(p, LINK_tld_props,(long) -1);
473
    enc_aligned_string ( p, LINK_version_props, ( long ) -1 ) ;
495
    enc_aligned_string(p, LINK_version_props,(long) -1);
474
    if ( tok_decs ) {
496
    if (tok_decs) {
475
	enc_aligned_string ( p, LINK_tokdec_props, ( long ) -1 ) ;
497
	enc_aligned_string(p, LINK_tokdec_props,(long) -1);
476
    }
498
    }
477
    if ( tok_defs ) {
499
    if (tok_defs) {
478
	enc_aligned_string ( p, LINK_tokdef_props, ( long ) -1 ) ;
500
	enc_aligned_string(p, LINK_tokdef_props,(long) -1);
479
    }
501
    }
480
    if ( al_tag_defs ) {
502
    if (al_tag_defs) {
481
	enc_aligned_string ( p, LINK_al_tagdef_props, ( long ) -1 ) ;
503
	enc_aligned_string(p, LINK_al_tagdef_props,(long) -1);
482
    }
504
    }
483
    if ( tag_decs ) {
505
    if (tag_decs) {
484
	enc_aligned_string ( p, LINK_tagdec_props, ( long ) -1 ) ;
506
	enc_aligned_string(p, LINK_tagdec_props,(long) -1);
485
    }
507
    }
486
    if ( tag_defs ) {
508
    if (tag_defs) {
487
	enc_aligned_string ( p, LINK_tagdef_props, ( long ) -1 ) ;
509
	enc_aligned_string(p, LINK_tagdef_props,(long) -1);
488
    }
510
    }
489
 
511
 
490
    /* Adjust totals for removed variables */
512
    /* Adjust totals for removed variables */
491
    tok_total += sort_removed [ SORT_token ] ;
513
    tok_total += sort_removed[SORT_token];
492
    tag_total += sort_removed [ SORT_tag ] ;
514
    tag_total += sort_removed[SORT_tag];
493
    al_tag_total += sort_removed [ SORT_al_tag ] ;
515
    al_tag_total += sort_removed[SORT_al_tag];
494
    lab_total += sort_removed [ SORT_label ] ;
516
    lab_total += sort_removed[SORT_label];
495
 
517
 
496
    /* Output variable sorts */
518
    /* Output variable sorts */
497
    var_total = 0 ;
519
    var_total = 0;
498
    if ( tok_total ) var_total++ ;
520
    if (tok_total)var_total++;
499
    if ( al_tag_total ) var_total++ ;
521
    if (al_tag_total)var_total++;
500
    if ( tag_total ) var_total++ ;
522
    if (tag_total)var_total++;
501
    enc_tdf_int ( p, var_total ) ;
523
    enc_tdf_int(p, var_total);
502
    if ( tok_total ) {
524
    if (tok_total) {
503
	enc_aligned_string ( p, LINK_token, ( long ) -1 ) ;
525
	enc_aligned_string(p, LINK_token,(long) -1);
504
	enc_tdf_int ( p, tok_total ) ;
526
	enc_tdf_int(p, tok_total);
505
    }
527
    }
506
    if ( al_tag_total ) {
528
    if (al_tag_total) {
507
	enc_aligned_string ( p, LINK_al_tag, ( long ) -1 ) ;
529
	enc_aligned_string(p, LINK_al_tag,(long) -1);
508
	enc_tdf_int ( p, al_tag_total ) ;
530
	enc_tdf_int(p, al_tag_total);
509
    }
531
    }
510
    if ( tag_total ) {
532
    if (tag_total) {
511
	enc_aligned_string ( p, LINK_tag, ( long ) -1 ) ;
533
	enc_aligned_string(p, LINK_tag,(long) -1);
512
	enc_tdf_int ( p, tag_total ) ;
534
	enc_tdf_int(p, tag_total);
513
    }
535
    }
514
 
536
 
515
    /* Output external names */
537
    /* Output external names */
516
    enc_tdf_int ( p, var_total ) ;
538
    enc_tdf_int(p, var_total);
517
    crt_bitstream = p ;
539
    crt_bitstream = p;
518
    if ( tok_total ) {
540
    if (tok_total) {
519
	enc_tdf_int ( p, tok_external ) ;
541
	enc_tdf_int(p, tok_external);
520
	apply_to_all ( enc_token_names, SORT_token ) ;
542
	apply_to_all(enc_token_names, SORT_token);
521
    }
543
    }
522
    if ( al_tag_total ) {
544
    if (al_tag_total) {
523
	enc_tdf_int ( p, al_tag_external ) ;
545
	enc_tdf_int(p, al_tag_external);
524
	apply_to_all ( enc_al_tag_names, SORT_al_tag ) ;
546
	apply_to_all(enc_al_tag_names, SORT_al_tag);
525
    }
547
    }
526
    if ( tag_total ) {
548
    if (tag_total) {
527
	enc_tdf_int ( p, tag_external ) ;
549
	enc_tdf_int(p, tag_external);
528
	apply_to_all ( enc_tag_names, SORT_tag ) ;
550
	apply_to_all(enc_tag_names, SORT_tag);
529
    }
551
    }
530
 
552
 
531
    /* Output equations */
553
    /* Output equations */
532
    enc_tdf_int ( p, eqn_total ) ;
554
    enc_tdf_int(p, eqn_total);
533
    enc_equation ( p, ( long ) 1, tld_bs, EQN_TLD ) ;
555
    enc_equation(p,(long)1, tld_bs, EQN_TLD);
534
    vers_bs = new_bitstream () ;
556
    vers_bs = new_bitstream();
535
    enc_version_bits ( vers_bs, ENC_make_version ) ;
557
    enc_version_bits(vers_bs, ENC_make_version);
536
    enc_tdf_int ( vers_bs, version_major ) ;
558
    enc_tdf_int(vers_bs, version_major);
537
    enc_tdf_int ( vers_bs, version_minor ) ;
559
    enc_tdf_int(vers_bs, version_minor);
538
    enc_equation ( p, ( long ) 1, vers_bs, EQN_VERS ) ;
560
    enc_equation(p,(long)1, vers_bs, EQN_VERS);
539
    if ( tok_decs ) enc_equation ( p, tok_decs, tok_decs_bs, EQN_TOKDEC ) ;
561
    if (tok_decs)enc_equation(p, tok_decs, tok_decs_bs, EQN_TOKDEC);
540
    if ( tok_defs ) enc_equation ( p, tok_defs, tok_defs_bs, EQN_TOKDEF ) ;
562
    if (tok_defs)enc_equation(p, tok_defs, tok_defs_bs, EQN_TOKDEF);
541
    if ( al_tag_defs ) {
563
    if (al_tag_defs) {
542
	enc_equation ( p, al_tag_defs, al_tag_defs_bs, EQN_ALDEF ) ;
564
	enc_equation(p, al_tag_defs, al_tag_defs_bs, EQN_ALDEF);
543
    }
565
    }
544
    if ( tag_decs ) enc_equation ( p, tag_decs, tag_decs_bs, EQN_TAGDEC ) ;
566
    if (tag_decs)enc_equation(p, tag_decs, tag_decs_bs, EQN_TAGDEC);
545
    if ( tag_defs ) enc_equation ( p, tag_defs, tag_defs_bs, EQN_TAGDEF ) ;
567
    if (tag_defs)enc_equation(p, tag_defs, tag_defs_bs, EQN_TAGDEF);
546
 
568
 
547
    /* Send bitstream to output file */
569
    /* Send bitstream to output file */
548
    print_bitstream ( p ) ;
570
    print_bitstream(p);
549
    return ;
571
    return;
550
}
572
}