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 45... Line 75...
45
    This routine dumps the contents of the bitstream bs to its associated
75
    This routine dumps the contents of the bitstream bs to its associated
46
    file.  The contents will be lost if this is the null file.  Any odd
76
    file.  The contents will be lost if this is the null file.  Any odd
47
    bits will remain in the bitstream.
77
    bits will remain in the bitstream.
48
*/
78
*/
49
 
79
 
50
static void dump_bitstream
80
static void
51
    PROTO_N ( ( bs ) )
-
 
52
    PROTO_T ( BITSTREAM *bs )
81
dump_bitstream(BITSTREAM *bs)
53
{
82
{
54
    size_t n = ( size_t ) bs->bytes ;
83
	size_t n = (size_t)bs->bytes;
55
    if ( n ) {
84
	if (n) {
56
	string t = bs->text ;
85
		string t = bs->text;
57
	FILE *f = bs->file ;
86
		FILE *f = bs->file;
58
	if ( f ) {
87
		if (f) {
59
	    IGNORE fwrite ( ( gen_ptr ) t, sizeof ( character ), n, f ) ;
88
			IGNORE fwrite((gen_ptr)t, sizeof(character), n, f);
-
 
89
		}
-
 
90
		t[0] = t[n];
60
	}
91
	}
61
	t [0] = t [n] ;
-
 
62
    }
-
 
63
    bs->bytes = 0 ;
92
	bs->bytes = 0;
64
    return ;
93
	return;
65
}
94
}
66
 
95
 
67
 
96
 
68
/*
97
/*
69
    LIST OF FREE BITSTREAMS
98
    LIST OF FREE BITSTREAMS
70
 
99
 
71
    This variable gives the list of bitstreams from which new bitstreams
100
    This variable gives the list of bitstreams from which new bitstreams
72
    are allocated.
101
    are allocated.
73
*/
102
*/
74
 
103
 
75
static BITSTREAM *free_bitstreams = NULL ;
104
static BITSTREAM *free_bitstreams = NULL;
76
 
105
 
77
 
106
 
78
/*
107
/*
79
    CREATE A NEW BITSTREAM
108
    CREATE A NEW BITSTREAM
80
 
109
 
81
    This routine creates a new bitstream with associated file f and
110
    This routine creates a new bitstream with associated file f and
82
    linkage lnk.
111
    linkage lnk.
83
*/
112
*/
84
 
113
 
85
BITSTREAM *start_bitstream
114
BITSTREAM *
86
    PROTO_N ( ( f, lnk ) )
-
 
87
    PROTO_T ( FILE *f X gen_ptr lnk )
115
start_bitstream(FILE *f, gen_ptr lnk)
88
{
116
{
89
    BITSTREAM *bs = free_bitstreams ;
117
	BITSTREAM *bs = free_bitstreams;
90
    if ( bs ) {
118
	if (bs) {
91
	free_bitstreams = bs->prev ;
119
		free_bitstreams = bs->prev;
92
    } else {
120
	} else {
93
	bs = xmalloc_one ( BITSTREAM ) ;
121
		bs = xmalloc_one(BITSTREAM);
94
	bs->text = xustr ( ( gen_size ) CHUNK_SIZE ) ;
122
		bs->text = xustr((gen_size)CHUNK_SIZE);
95
    }
123
	}
96
    bs->bytes = 0 ;
124
	bs->bytes = 0;
97
    bs->bits = 0 ;
125
	bs->bits = 0;
98
    bs->size = 0 ;
126
	bs->size = 0;
99
    bs->file = f ;
127
	bs->file = f;
100
    bs->link = lnk ;
128
	bs->link = lnk;
101
    bs->prev = NULL ;
129
	bs->prev = NULL;
102
    return ( bs ) ;
130
	return (bs);
103
}
131
}
104
 
132
 
105
 
133
 
106
/*
134
/*
107
    END A BITSTREAM
135
    END A BITSTREAM
108
 
136
 
109
    This routine frees the bitstream bs including clearing the buffer
137
    This routine frees the bitstream bs including clearing the buffer
110
    for any file bitstream if w is true.
138
    for any file bitstream if w is true.
111
*/
139
*/
112
 
140
 
113
void end_bitstream
141
void
114
    PROTO_N ( ( bs, w ) )
-
 
115
    PROTO_T ( BITSTREAM *bs X int w )
142
end_bitstream(BITSTREAM *bs, int w)
116
{
143
{
117
    BITSTREAM *ps = bs ;
144
	BITSTREAM *ps = bs;
118
    if ( w && ps->file ) {
145
	if (w && ps->file) {
119
	/* Align bitstream and dump */
146
		/* Align bitstream and dump */
120
	ps = enc_boundary ( ps ) ;
147
		ps = enc_boundary(ps);
121
	dump_bitstream ( ps ) ;
148
		dump_bitstream(ps);
122
    }
149
	}
123
    while ( ps->prev ) ps = ps->prev ;
150
	while (ps->prev) {
-
 
151
		ps = ps->prev;
-
 
152
	}
124
    ps->prev = free_bitstreams ;
153
	ps->prev = free_bitstreams;
125
    free_bitstreams = bs ;
154
	free_bitstreams = bs;
126
    return ;
155
	return;
127
}
156
}
128
 
157
 
129
 
158
 
130
/*
159
/*
131
    EXTEND A BITSTREAM
160
    EXTEND A BITSTREAM
132
 
161
 
133
    This routine extends the bitstream bs by dumping its contents to a
162
    This routine extends the bitstream bs by dumping its contents to a
134
    file or linking it to a fresh chunk.
163
    file or linking it to a fresh chunk.
135
*/
164
*/
136
 
165
 
137
static BITSTREAM *extend_bitstream
166
static BITSTREAM *
138
    PROTO_N ( ( bs ) )
-
 
139
    PROTO_T ( BITSTREAM *bs )
167
extend_bitstream(BITSTREAM *bs)
140
{
168
{
141
    bs->bytes = CHUNK_SIZE ;
169
	bs->bytes = CHUNK_SIZE;
142
    bs->bits = 0 ;
170
	bs->bits = 0;
143
    if ( bs->file ) {
171
	if (bs->file) {
144
	dump_bitstream ( bs ) ;
172
		dump_bitstream(bs);
145
    } else {
173
	} else {
146
	BITSTREAM *ps = start_bitstream ( NIL ( FILE ), bs->link ) ;
174
		BITSTREAM *ps = start_bitstream(NIL(FILE), bs->link);
147
	ps->prev = bs ;
175
		ps->prev = bs;
148
	bs = ps ;
176
		bs = ps;
149
    }
177
	}
150
    return ( bs ) ;
178
	return (bs);
151
}
179
}
152
 
180
 
153
 
181
 
154
/*
182
/*
155
    ADD A NUMBER OF BITS TO A BITSTREAM
183
    ADD A NUMBER OF BITS TO A BITSTREAM
156
 
184
 
157
    This routine adds the n bits given by d to the bitstream bs.  n will
185
    This routine adds the n bits given by d to the bitstream bs.  n will
158
    always be at most 16.
186
    always be at most 16.
159
*/
187
*/
160
 
188
 
161
BITSTREAM *enc_bits
189
BITSTREAM *
162
    PROTO_N ( ( bs, n, d ) )
-
 
163
    PROTO_T ( BITSTREAM *bs X unsigned n X unsigned d )
190
enc_bits(BITSTREAM *bs, unsigned n, unsigned d)
164
{
191
{
165
    if ( n ) {
192
	if (n) {
166
	unsigned long b = ( unsigned long ) d ;
193
		unsigned long b = (unsigned long)d;
167
	string text = bs->text ;
194
		string text = bs->text;
168
	unsigned bytes = bs->bytes ;
195
		unsigned bytes = bs->bytes;
169
	unsigned bits = bs->bits ;
196
		unsigned bits = bs->bits;
170
	if ( bits ) {
197
		if (bits) {
171
	    /* Add existing bits */
198
			/* Add existing bits */
172
	    unsigned r = ( BYTE_SIZE - bits ) ;
199
			unsigned r = (BYTE_SIZE - bits);
173
	    unsigned long c = ( unsigned long ) text [ bytes ] ;
200
			unsigned long c = (unsigned long)text[bytes];
174
	    b |= ( ( c >> r ) << n ) ;
201
			b |= ((c >> r) << n);
175
	    n += bits ;
202
			n += bits;
176
	}
203
		}
177
	bits = ( n % BYTE_SIZE ) ;
204
		bits = (n % BYTE_SIZE);
178
	if ( bits ) {
205
		if (bits) {
179
	    /* Zero pad to a whole number of bytes */
206
			/* Zero pad to a whole number of bytes */
180
	    unsigned r = ( BYTE_SIZE - bits ) ;
207
			unsigned r = (BYTE_SIZE - bits);
181
	    b <<= r ;
208
			b <<= r;
182
	    n += r ;
209
			n += r;
183
	}
210
		}
184
	do {
211
		do {
185
	    /* Output each byte */
212
			/* Output each byte */
186
	    if ( bytes >= CHUNK_SIZE ) {
213
			if (bytes >= CHUNK_SIZE) {
187
		/* Start new chunk if necessary */
214
				/* Start new chunk if necessary */
188
		bs = extend_bitstream ( bs ) ;
215
				bs = extend_bitstream(bs);
189
		text = bs->text ;
216
				text = bs->text;
190
		bytes = 0 ;
217
				bytes = 0;
191
	    }
218
			}
192
	    n -= BYTE_SIZE ;
219
			n -= BYTE_SIZE;
193
	    text [ bytes ] = ( character ) ( ( b >> n ) & BYTE_MASK ) ;
220
			text[bytes] = (character)((b >> n) & BYTE_MASK);
194
	    bytes++ ;
221
			bytes++;
195
	} while ( n ) ;
222
		} while (n);
196
	if ( bits ) bytes-- ;
223
		if (bits) {
-
 
224
			bytes--;
-
 
225
		}
197
	bs->bytes = bytes ;
226
		bs->bytes = bytes;
198
	bs->bits = bits ;
227
		bs->bits = bits;
199
    }
228
	}
200
    return ( bs ) ;
229
	return (bs);
201
}
230
}
202
 
231
 
203
 
232
 
204
/*
233
/*
205
    ADD A LARGER NUMBER OF BITS TO A BITSTREAM
234
    ADD A LARGER NUMBER OF BITS TO A BITSTREAM
206
 
235
 
207
    This routine is identical to enc_bits except that it works for up to
236
    This routine is identical to enc_bits except that it works for up to
208
    32 bits.
237
    32 bits.
209
*/
238
*/
210
 
239
 
211
BITSTREAM *enc_long_bits
240
BITSTREAM *
212
    PROTO_N ( ( bs, n, d ) )
-
 
213
    PROTO_T ( BITSTREAM *bs X unsigned n X unsigned long d )
241
enc_long_bits(BITSTREAM *bs, unsigned n, unsigned long d)
214
{
242
{
215
    if ( n > 16 ) {
243
	if (n > 16) {
216
	bs = enc_bits ( bs, n - 16, ( unsigned ) ( d >> 16 ) ) ;
244
		bs = enc_bits(bs, n - 16,(unsigned)(d >> 16));
217
	n = 16 ;
245
		n = 16;
218
    }
246
	}
219
    bs = enc_bits ( bs, n, ( unsigned ) d ) ;
247
	bs = enc_bits(bs, n,(unsigned)d);
220
    return ( bs ) ;
248
	return (bs);
221
}
249
}
222
 
250
 
223
 
251
 
224
/*
252
/*
225
    ADD A NUMBER OF BYTES TO A BITSTREAM
253
    ADD A NUMBER OF BYTES TO A BITSTREAM
226
 
254
 
227
    This routine adds the n bytes given by s to the bitstream bs.
255
    This routine adds the n bytes given by s to the bitstream bs.
228
*/
256
*/
229
 
257
 
230
BITSTREAM *enc_bytes
258
BITSTREAM *
231
    PROTO_N ( ( bs, n, s ) )
-
 
232
    PROTO_T ( BITSTREAM *bs X unsigned long n X string s )
259
enc_bytes(BITSTREAM *bs, unsigned long n, string s)
233
{
260
{
234
    if ( bs->bits ) {
261
	if (bs->bits) {
235
	/* Unaligned bitstream */
262
		/* Unaligned bitstream */
236
	unsigned long i ;
263
		unsigned long i;
237
	for ( i = 0 ; i < n ; i++ ) {
264
		for (i = 0; i < n; i++) {
238
	    unsigned c = ( unsigned ) s [i] ;
265
			unsigned c = (unsigned)s[i];
239
	    bs = enc_bits ( bs, ( unsigned ) BYTE_SIZE, c ) ;
266
			bs = enc_bits(bs,(unsigned)BYTE_SIZE, c);
240
	}
267
		}
241
    } else {
268
	} else {
242
	/* Aligned bitstream */
269
		/* Aligned bitstream */
243
	unsigned long i ;
270
		unsigned long i;
244
	string text = bs->text ;
271
		string text = bs->text;
245
	unsigned bytes = bs->bytes ;
272
		unsigned bytes = bs->bytes;
246
	for ( i = 0 ; i < n ; i++ ) {
273
		for (i = 0; i < n; i++) {
247
	    if ( bytes >= CHUNK_SIZE ) {
274
			if (bytes >= CHUNK_SIZE) {
248
		/* Start new chunk if necessary */
275
				/* Start new chunk if necessary */
249
		bs = extend_bitstream ( bs ) ;
276
				bs = extend_bitstream(bs);
250
		text = bs->text ;
277
				text = bs->text;
251
		bytes = 0 ;
278
				bytes = 0;
252
	    }
279
			}
253
	    text [ bytes ] = s [i] ;
280
			text[bytes] = s[i];
254
	    bytes++ ;
281
			bytes++;
-
 
282
		}
-
 
283
		bs->bytes = bytes;
255
	}
284
	}
256
	bs->bytes = bytes ;
-
 
257
    }
-
 
258
    return ( bs ) ;
285
	return (bs);
259
}
286
}
260
 
287
 
261
 
288
 
262
/*
289
/*
263
    ADD A NUMBER OF ASCII CHARACTERS TO A BITSTREAM
290
    ADD A NUMBER OF ASCII CHARACTERS TO A BITSTREAM
264
 
291
 
265
    This routine adds the n ASCII characters given by s to the bitstream bs.
292
    This routine adds the n ASCII characters given by s to the bitstream bs.
266
*/
293
*/
267
 
294
 
268
BITSTREAM *enc_ascii
295
BITSTREAM *
269
    PROTO_N ( ( bs, n, s ) )
-
 
270
    PROTO_T ( BITSTREAM *bs X unsigned long n X string s )
296
enc_ascii(BITSTREAM *bs, unsigned long n, string s)
271
{
297
{
272
    if ( is_ascii ) {
298
	if (is_ascii) {
273
	bs = enc_bytes ( bs, n, s ) ;
299
		bs = enc_bytes(bs, n, s);
274
    } else {
-
 
275
	if ( bs->bits ) {
-
 
276
	    /* Unaligned bitstream */
-
 
277
	    unsigned long i ;
-
 
278
	    for ( i = 0 ; i < n ; i++ ) {
-
 
279
		int ch = CHAR_SIMPLE ;
-
 
280
		unsigned long c = ( unsigned long ) s [i] ;
-
 
281
		c = to_ascii ( c, &ch ) ;
-
 
282
		bs = enc_bits ( bs, ( unsigned ) BYTE_SIZE, ( unsigned ) c ) ;
-
 
283
	    }
-
 
284
	} else {
300
	} else {
-
 
301
		if (bs->bits) {
-
 
302
			/* Unaligned bitstream */
-
 
303
			unsigned long i;
-
 
304
			for (i = 0; i < n; i++) {
-
 
305
				int ch = CHAR_SIMPLE;
-
 
306
				unsigned long c = (unsigned long)s[i];
-
 
307
				c = to_ascii(c, &ch);
-
 
308
				bs = enc_bits(bs, (unsigned)BYTE_SIZE,
-
 
309
					      (unsigned)c);
-
 
310
			}
-
 
311
		} else {
285
	    /* Aligned bitstream */
312
			/* Aligned bitstream */
286
	    unsigned long i ;
313
			unsigned long i;
287
	    string text = bs->text ;
314
			string text = bs->text;
288
	    unsigned bytes = bs->bytes ;
315
			unsigned bytes = bs->bytes;
289
	    for ( i = 0 ; i < n ; i++ ) {
316
			for (i = 0; i < n; i++) {
290
		int ch = CHAR_SIMPLE ;
317
				int ch = CHAR_SIMPLE;
291
		unsigned long c = ( unsigned long ) s [i] ;
318
				unsigned long c = (unsigned long)s[i];
292
		if ( bytes >= CHUNK_SIZE ) {
319
				if (bytes >= CHUNK_SIZE) {
293
		    /* Start new chunk if necessary */
320
					/* Start new chunk if necessary */
294
		    bs = extend_bitstream ( bs ) ;
321
					bs = extend_bitstream(bs);
295
		    text = bs->text ;
322
					text = bs->text;
296
		    bytes = 0 ;
323
					bytes = 0;
-
 
324
				}
-
 
325
				c = to_ascii(c, &ch);
-
 
326
				text[bytes] = (unsigned char)c;
-
 
327
				bytes++;
-
 
328
			}
-
 
329
			bs->bytes = bytes;
297
		}
330
		}
298
		c = to_ascii ( c, &ch ) ;
-
 
299
		text [ bytes ] = ( unsigned char ) c ;
-
 
300
		bytes++ ;
-
 
301
	    }
-
 
302
	    bs->bytes = bytes ;
-
 
303
	}
331
	}
304
    }
-
 
305
    return ( bs ) ;
332
	return (bs);
306
}
333
}
307
 
334
 
308
 
335
 
309
/*
336
/*
310
    COPY A BITSTREAM
337
    COPY A BITSTREAM
311
 
338
 
312
    This routine copies a component of the bitstream bs to the end of
339
    This routine copies a component of the bitstream bs to the end of
313
    the bitstream fs.
340
    the bitstream fs.
314
*/
341
*/
315
 
342
 
316
static BITSTREAM *copy_bitstream
343
static BITSTREAM *
317
    PROTO_N ( ( fs, bs ) )
-
 
318
    PROTO_T ( BITSTREAM *fs X BITSTREAM *bs )
344
copy_bitstream(BITSTREAM *fs, BITSTREAM *bs)
319
{
345
{
320
    string text = bs->text ;
346
	string text = bs->text;
321
    unsigned bits = bs->bits ;
347
	unsigned bits = bs->bits;
322
    unsigned bytes = bs->bytes ;
348
	unsigned bytes = bs->bytes;
323
    if ( bytes ) {
349
	if (bytes) {
324
	/* Copy any bytes */
350
		/* Copy any bytes */
325
	fs = enc_bytes ( fs, ( unsigned long ) bytes, text ) ;
351
		fs = enc_bytes(fs,(unsigned long)bytes, text);
326
    }
352
	}
327
    if ( bits ) {
353
	if (bits) {
328
	/* Copy any spare bits */
354
		/* Copy any spare bits */
329
	unsigned c = ( unsigned ) text [ bytes ] ;
355
		unsigned c = (unsigned)text[bytes];
330
	c >>= ( BYTE_SIZE - bits ) ;
356
		c >>= (BYTE_SIZE - bits);
331
	fs = enc_bits ( fs, bits, c ) ;
357
		fs = enc_bits(fs, bits, c);
332
    }
358
	}
333
    return ( fs ) ;
359
	return (fs);
334
}
360
}
335
 
361
 
336
 
362
 
337
/*
363
/*
338
    JOIN TWO BITSTREAMS
364
    JOIN TWO BITSTREAMS
339
 
365
 
340
    This routine adds the bitstream bs to the end of the bitstream fs.
366
    This routine adds the bitstream bs to the end of the bitstream fs.
341
    bs will not have an associated file.
367
    bs will not have an associated file.
342
*/
368
*/
343
 
369
 
344
BITSTREAM *join_bitstreams
370
BITSTREAM *
345
    PROTO_N ( ( fs, bs ) )
-
 
346
    PROTO_T ( BITSTREAM *fs X BITSTREAM *bs )
371
join_bitstreams(BITSTREAM *fs, BITSTREAM *bs)
347
{
372
{
348
    FILE *f ;
373
	FILE *f;
349
    BITSTREAM *ps ;
374
	BITSTREAM *ps;
350
    if ( bs == NULL ) return ( fs ) ;
375
	if (bs == NULL) {
-
 
376
		return (fs);
-
 
377
	}
351
    if ( fs == NULL ) return ( bs ) ;
378
	if (fs == NULL) {
-
 
379
		return (bs);
-
 
380
	}
352
    if ( fs->link == NULL ) fs->link = bs->link ;
381
	if (fs->link == NULL) {
-
 
382
		fs->link = bs->link;
-
 
383
	}
353
 
384
 
354
    /* Copy bitstream to file */
385
	/* Copy bitstream to file */
355
    ps = bs->prev ;
386
	ps = bs->prev;
356
    f = fs->file ;
387
	f = fs->file;
357
    if ( f ) {
388
	if (f) {
-
 
389
		if (ps) {
358
	if ( ps ) fs = join_bitstreams ( fs, ps ) ;
390
			fs = join_bitstreams(fs, ps);
-
 
391
		}
359
	if ( fs->bits == 0 && bs->bytes ) {
392
		if (fs->bits == 0 && bs->bytes) {
360
	    /* Dump any bytes to file */
393
			/* Dump any bytes to file */
361
	    bs->file = f ;
394
			bs->file = f;
-
 
395
			if (fs->bytes) {
362
	    if ( fs->bytes ) dump_bitstream ( fs ) ;
396
				dump_bitstream(fs);
-
 
397
			}
363
	    dump_bitstream ( bs ) ;
398
			dump_bitstream(bs);
364
	    bs->file = NULL ;
399
			bs->file = NULL;
365
	}
400
		}
366
	fs = copy_bitstream ( fs, bs ) ;
401
		fs = copy_bitstream(fs, bs);
-
 
402
		if (ps == NULL) {
367
	if ( ps == NULL ) end_bitstream ( bs, 0 ) ;
403
			end_bitstream(bs, 0);
-
 
404
		}
368
	return ( fs ) ;
405
		return (fs);
369
    }
406
	}
370
 
407
 
371
    /* Copy small bitstreams */
408
	/* Copy small bitstreams */
372
    if ( ps == NULL && bs->bytes < CHUNK_COPY ) {
409
	if (ps == NULL && bs->bytes < CHUNK_COPY) {
373
	fs = copy_bitstream ( fs, bs ) ;
410
		fs = copy_bitstream(fs, bs);
374
	end_bitstream ( bs, 0 ) ;
411
		end_bitstream(bs, 0);
375
	return ( fs ) ;
412
		return (fs);
376
    }
413
	}
377
 
414
 
378
    /* Link larger bitstreams */
415
	/* Link larger bitstreams */
379
    ps = bs ;
416
	ps = bs;
380
    while ( ps->prev ) ps = ps->prev ;
417
	while (ps->prev) {
-
 
418
		ps = ps->prev;
-
 
419
	}
381
    ps->prev = fs ;
420
	ps->prev = fs;
382
    return ( bs ) ;
421
	return (bs);
383
}
422
}
384
 
423
 
385
 
424
 
386
/*
425
/*
387
    FIND THE LENGTH OF A BITSTREAM
426
    FIND THE LENGTH OF A BITSTREAM
388
 
427
 
389
    This routine returns the length of the bitstream bs in bits.
428
    This routine returns the length of the bitstream bs in bits.
390
*/
429
*/
391
 
430
 
392
unsigned length_bitstream
431
unsigned
393
    PROTO_N ( ( bs ) )
-
 
394
    PROTO_T ( BITSTREAM *bs )
432
length_bitstream(BITSTREAM *bs)
395
{
433
{
396
    unsigned n = 0 ;
434
	unsigned n = 0;
397
    while ( bs ) {
435
	while (bs) {
398
	n += ( BYTE_SIZE * bs->bytes + bs->bits ) ;
436
		n += (BYTE_SIZE * bs->bytes + bs->bits);
399
	bs = bs->prev ;
437
		bs = bs->prev;
400
    }
438
	}
401
    return ( n ) ;
439
	return (n);
402
}
440
}
403
 
441
 
404
 
442
 
405
/*
443
/*
406
    ALIGN A BITSTREAM TO A BYTE BOUNDARY
444
    ALIGN A BITSTREAM TO A BYTE BOUNDARY
407
 
445
 
408
    This routine aligns the bitstream bs so that it consists of a whole
446
    This routine aligns the bitstream bs so that it consists of a whole
409
    number of bytes.
447
    number of bytes.
410
*/
448
*/
411
 
449
 
412
BITSTREAM *enc_boundary
450
BITSTREAM *
413
    PROTO_N ( ( bs ) )
-
 
414
    PROTO_T ( BITSTREAM *bs )
451
enc_boundary(BITSTREAM *bs)
415
{
452
{
416
    unsigned n = length_bitstream ( bs ) ;
453
	unsigned n = length_bitstream(bs);
417
    unsigned r = ( n % BYTE_SIZE ) ;
454
	unsigned r = (n % BYTE_SIZE);
418
    if ( r ) {
455
	if (r) {
419
	/* Add extra bits if necessary */
456
		/* Add extra bits if necessary */
420
	bs = enc_bits ( bs, BYTE_SIZE - r, ( unsigned ) 0 ) ;
457
		bs = enc_bits(bs, BYTE_SIZE - r,(unsigned)0);
421
    }
458
	}
422
    return ( bs ) ;
459
	return (bs);
423
}
460
}
424
 
461
 
425
 
462
 
426
/*
463
/*
427
    ADD AN EXTENDED BIT PATTERN TO A BITSTREAM
464
    ADD AN EXTENDED BIT PATTERN TO A BITSTREAM
428
 
465
 
429
    This routine adds the bit pattern d to the bitstream bs using the
466
    This routine adds the bit pattern d to the bitstream bs using the
430
    extended encoding as n-bit packets.  Note that d cannot be zero.
467
    extended encoding as n-bit packets.  Note that d cannot be zero.
431
*/
468
*/
432
 
469
 
433
BITSTREAM *enc_extn
470
BITSTREAM *
434
    PROTO_N ( ( bs, n, d ) )
-
 
435
    PROTO_T ( BITSTREAM *bs X unsigned n X unsigned d )
471
enc_extn(BITSTREAM *bs, unsigned n, unsigned d)
436
{
472
{
437
    unsigned e = ( ( unsigned ) 1 << n ) - 1 ;
473
	unsigned e = ((unsigned)1 << n) - 1;
438
    while ( d > e ) {
474
	while (d > e) {
439
	/* Encode zero for each multiple of e */
475
		/* Encode zero for each multiple of e */
440
	bs = enc_bits ( bs, n, ( unsigned ) 0 ) ;
476
		bs = enc_bits(bs, n,(unsigned)0);
441
	d -= e ;
477
		d -= e;
442
    }
478
	}
443
    /* Encode the remainder */
479
	/* Encode the remainder */
444
    bs = enc_bits ( bs, n, d ) ;
480
	bs = enc_bits(bs, n, d);
445
    return ( bs ) ;
481
	return (bs);
446
}
482
}
447
 
483
 
448
 
484
 
449
/*
485
/*
450
    ADD A SERIES OF OCTAL DIGITS TO A BITSTREAM
486
    ADD A SERIES OF OCTAL DIGITS TO A BITSTREAM
451
 
487
 
452
    This is an auxiliary routine used by enc_int.  It is identical
488
    This is an auxiliary routine used by enc_int.  It is identical
453
    except that the last digit is not marked.
489
    except that the last digit is not marked.
454
*/
490
*/
455
 
491
 
456
BITSTREAM *enc_int_aux
492
BITSTREAM *
457
    PROTO_N ( ( bs, n ) )
-
 
458
    PROTO_T ( BITSTREAM *bs X unsigned long n )
493
enc_int_aux(BITSTREAM *bs, unsigned long n)
459
{
494
{
460
    unsigned m ;
495
	unsigned m;
-
 
496
	if (n >= 8) {
461
    if ( n >= 8 ) bs = enc_int_aux ( bs, n >> 3 ) ;
497
		bs = enc_int_aux(bs, n >> 3);
-
 
498
	}
462
    m = ( unsigned ) ( n & 0x7 ) ;
499
	m = (unsigned)(n & 0x7);
463
    bs = enc_bits ( bs, ( unsigned ) 4, m ) ;
500
	bs = enc_bits(bs,(unsigned)4, m);
464
    return ( bs ) ;
501
	return (bs);
465
}
502
}
466
 
503
 
467
 
504
 
468
/*
505
/*
469
    ADD AN INTEGER TO A BITSTREAM
506
    ADD AN INTEGER TO A BITSTREAM
470
 
507
 
471
    This routine adds the integer n as a series of octal digits to the
508
    This routine adds the integer n as a series of octal digits to the
472
    bitstream bs.  The last digit is marked by means of a set bit.
509
    bitstream bs.  The last digit is marked by means of a set bit.
473
*/
510
*/
474
 
511
 
475
BITSTREAM *enc_int
512
BITSTREAM *
476
    PROTO_N ( ( bs, n ) )
-
 
477
    PROTO_T ( BITSTREAM *bs X unsigned long n )
513
enc_int(BITSTREAM *bs, unsigned long n)
478
{
514
{
479
    unsigned m ;
515
	unsigned m;
480
    if ( n >= 8 ) bs = enc_int_aux ( bs, n >> 3 ) ;
516
	if (n >= 8)bs = enc_int_aux(bs, n >> 3);
481
    m = ( unsigned ) ( ( n & 0x7 ) | 0x8 ) ;
517
	m = (unsigned)((n & 0x7) | 0x8);
482
    bs = enc_bits ( bs, ( unsigned ) 4, m ) ;
518
	bs = enc_bits(bs,(unsigned)4, m);
483
    return ( bs ) ;
519
	return (bs);
484
}
520
}
485
 
521
 
486
 
522
 
487
/*
523
/*
488
    ADD AN IDENTIFIER TO A BITSTREAM
524
    ADD AN IDENTIFIER TO A BITSTREAM
Line 491... Line 527...
491
    to the bitstream bs.  This is encoded as two integers, giving the
527
    to the bitstream bs.  This is encoded as two integers, giving the
492
    bits per character and the number of characters, followed by an
528
    bits per character and the number of characters, followed by an
493
    alignment, followed by the characters comprising the identifier.
529
    alignment, followed by the characters comprising the identifier.
494
*/
530
*/
495
 
531
 
496
BITSTREAM *enc_ident
532
BITSTREAM *
497
    PROTO_N ( ( bs, s, n ) )
-
 
498
    PROTO_T ( BITSTREAM *bs X string s X unsigned long n )
533
enc_ident(BITSTREAM *bs, string s, unsigned long n)
499
{
534
{
500
    bs = enc_int ( bs, ( unsigned long ) BYTE_SIZE ) ;
535
	bs = enc_int(bs,(unsigned long)BYTE_SIZE);
501
    bs = enc_int ( bs, n ) ;
536
	bs = enc_int(bs, n);
502
    bs = enc_boundary ( bs ) ;
537
	bs = enc_boundary(bs);
503
    bs = enc_ascii ( bs, n, s ) ;
538
	bs = enc_ascii(bs, n, s);
504
    return ( bs ) ;
539
	return (bs);
505
}
540
}
506
 
541
 
507
 
542
 
508
/*
543
/*
509
    ADD A STRING TO A BITSTREAM
544
    ADD A STRING TO A BITSTREAM
510
 
545
 
511
    This routine adds the string s of length n to the bitstream bs.
546
    This routine adds the string s of length n to the bitstream bs.
512
*/
547
*/
513
 
548
 
514
BITSTREAM *enc_tdfstring
549
BITSTREAM *
515
    PROTO_N ( ( bs, n, s ) )
-
 
516
    PROTO_T ( BITSTREAM *bs X unsigned long n X string s )
550
enc_tdfstring(BITSTREAM *bs, unsigned long n, string s)
517
{
551
{
518
    bs = enc_int ( bs, ( unsigned long ) BYTE_SIZE ) ;
552
	bs = enc_int(bs,(unsigned long)BYTE_SIZE);
519
    bs = enc_int ( bs, n ) ;
553
	bs = enc_int(bs, n);
520
    bs = enc_ascii ( bs, n, s ) ;
554
	bs = enc_ascii(bs, n, s);
521
    return ( bs ) ;
555
	return (bs);
522
}
556
}
523
 
557
 
524
 
558
 
525
/*
559
/*
526
    ADD A STRING TO A BITSTREAM
560
    ADD A STRING TO A BITSTREAM
527
 
561
 
528
    This routine is identical to enc_tdfstring except that strlen is
562
    This routine is identical to enc_tdfstring except that strlen is
529
    used to calculate the length of the string.
563
    used to calculate the length of the string.
530
*/
564
*/
531
 
565
 
532
BITSTREAM *enc_ustring
566
BITSTREAM *
533
    PROTO_N ( ( bs, s ) )
-
 
534
    PROTO_T ( BITSTREAM *bs X string s )
567
enc_ustring(BITSTREAM *bs, string s)
535
{
568
{
536
    unsigned long n = ( unsigned long ) ustrlen ( s ) ;
569
	unsigned long n = (unsigned long)ustrlen(s);
537
    bs = enc_int ( bs, ( unsigned long ) BYTE_SIZE ) ;
570
	bs = enc_int(bs,(unsigned long)BYTE_SIZE);
538
    bs = enc_int ( bs, n ) ;
571
	bs = enc_int(bs, n);
539
    bs = enc_ascii ( bs, n, s ) ;
572
	bs = enc_ascii(bs, n, s);
540
    return ( bs ) ;
573
	return (bs);
541
}
574
}
542
 
575
 
543
 
576
 
544
/*
577
/*
545
    ADD A TDF BITSTREAM TO A BITSTREAM
578
    ADD A TDF BITSTREAM TO A BITSTREAM
546
 
579
 
547
    This routine adds the bitstream ps, preceded by its length, to the
580
    This routine adds the bitstream ps, preceded by its length, to the
548
    bitstream bs.
581
    bitstream bs.
549
*/
582
*/
550
 
583
 
551
BITSTREAM *enc_bitstream
584
BITSTREAM *
552
    PROTO_N ( ( bs, ps ) )
-
 
553
    PROTO_T ( BITSTREAM *bs X BITSTREAM *ps )
585
enc_bitstream(BITSTREAM *bs, BITSTREAM *ps)
554
{
586
{
555
    unsigned n = length_bitstream ( ps ) ;
587
	unsigned n = length_bitstream(ps);
556
    bs = enc_int ( bs, ( unsigned long ) n ) ;
588
	bs = enc_int(bs,(unsigned long)n);
557
    bs = join_bitstreams ( bs, ps ) ;
589
	bs = join_bitstreams(bs, ps);
558
    return ( bs ) ;
590
	return (bs);
559
}
591
}