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 42... Line 72...
42
/*
72
/*
43
    FILL A BITSTREAM
73
    FILL A BITSTREAM
44
 
74
 
45
    This routine fills the bitstream bs from its associated file.  It
75
    This routine fills the bitstream bs from its associated file.  It
46
    returns the number of bytes read.
76
    returns the number of bytes read.
47
*/
77
*/
48
 
78
 
49
static unsigned fill_bitstream
79
static unsigned
50
    PROTO_N ( ( bs ) )
-
 
51
    PROTO_T ( BITSTREAM *bs )
80
fill_bitstream(BITSTREAM *bs)
52
{
81
{
53
    unsigned sz ;
82
	unsigned sz;
54
    FILE *f = bs->file ;
83
	FILE *f = bs->file;
55
    string text = bs->text ;
84
	string text = bs->text;
56
    sz = ( unsigned ) fread ( ( gen_ptr ) text, sizeof ( character ),
85
	sz = (unsigned)fread((gen_ptr)text, sizeof(character),
57
			      ( size_t ) CHUNK_SIZE, f ) ;
86
			     (size_t)CHUNK_SIZE, f);
58
    if ( sz == 0 ) {
87
	if (sz == 0) {
59
	/* No more characters in file */
88
		/* No more characters in file */
60
	bs->link = ( gen_ptr ) text ;
89
		bs->link = (gen_ptr)text;
61
	while ( sz < CHUNK_SIZE ) {
90
		while (sz < CHUNK_SIZE) {
62
	    /* Fill buffer with all ones */
91
			/* Fill buffer with all ones */
63
	    text [ sz ] = ( character ) BYTE_MASK ;
92
			text[sz] = (character)BYTE_MASK;
64
	    sz++ ;
93
			sz++;
65
	}
94
		}
66
    }
95
	}
67
    bs->size = sz ;
96
	bs->size = sz;
68
    return ( sz ) ;
97
	return (sz);
69
}
98
}
70
 
99
 
71
 
100
 
72
/*
101
/*
73
    CHECK FOR END OF FILE
102
    CHECK FOR END OF FILE
74
 
103
 
75
    This routine checks whether the end of the file associated with the
104
    This routine checks whether the end of the file associated with the
76
    bitstream bs has been precisely reached.  Note that the link field
105
    bitstream bs has been precisely reached.  Note that the link field
77
    is set to a non-null value by fill_bitstream if the end of file is
106
    is set to a non-null value by fill_bitstream if the end of file is
78
    encountered.
107
    encountered.
79
*/
108
*/
80
 
109
 
81
int de_eof
110
int
82
    PROTO_N ( ( bs ) )
-
 
83
    PROTO_T ( BITSTREAM *bs )
111
de_eof(BITSTREAM *bs)
84
{
112
{
85
    if ( bs->link ) return ( 0 ) ;
113
	if (bs->link) {
-
 
114
		return (0);
-
 
115
	}
86
    if ( bs->bytes >= bs->size ) {
116
	if (bs->bytes >= bs->size) {
87
	IGNORE fill_bitstream ( bs ) ;
117
		IGNORE fill_bitstream(bs);
88
	if ( bs->link ) return ( 1 ) ;
118
		if (bs->link) {
-
 
119
			return (1);
89
    }
120
		}
-
 
121
	}
90
    return ( 0 ) ;
122
	return (0);
91
}
123
}
92
 
124
 
93
 
125
 
94
/*
126
/*
95
    TABLE OF BIT MASKS
127
    TABLE OF BIT MASKS
96
 
128
 
97
    This array gives the bit masks for values up to 16 bits.
129
    This array gives the bit masks for values up to 16 bits.
98
*/
130
*/
99
 
131
 
100
static unsigned long bit_mask [17] = {
132
static unsigned long bit_mask[17] = {
101
    0x0000,
133
	0x0000,
102
#if FS_NUMBER_SUFFIX
134
#if FS_NUMBER_SUFFIX
103
    0x0001UL, 0x0003UL, 0x0007UL, 0x000fUL,
135
	0x0001UL, 0x0003UL, 0x0007UL, 0x000fUL,
104
    0x001fUL, 0x003fUL, 0x007fUL, 0x00ffUL,
136
	0x001fUL, 0x003fUL, 0x007fUL, 0x00ffUL,
105
    0x01ffUL, 0x03ffUL, 0x07ffUL, 0x0fffUL,
137
	0x01ffUL, 0x03ffUL, 0x07ffUL, 0x0fffUL,
106
    0x1fffUL, 0x3fffUL, 0x7fffUL, 0xffffUL
138
	0x1fffUL, 0x3fffUL, 0x7fffUL, 0xffffUL
107
#else
139
#else
108
    0x0001, 0x0003, 0x0007, 0x000f,
140
	0x0001, 0x0003, 0x0007, 0x000f,
109
    0x001f, 0x003f, 0x007f, 0x00ff,
141
	0x001f, 0x003f, 0x007f, 0x00ff,
110
    0x01ff, 0x03ff, 0x07ff, 0x0fff,
142
	0x01ff, 0x03ff, 0x07ff, 0x0fff,
111
    0x1fff, 0x3fff, 0x7fff, 0xffff
143
	0x1fff, 0x3fff, 0x7fff, 0xffff
112
#endif
144
#endif
113
} ;
145
};
114
 
146
 
115
 
147
 
116
/*
148
/*
117
    READ A NUMBER OF BITS FROM A BITSTREAM
149
    READ A NUMBER OF BITS FROM A BITSTREAM
118
 
150
 
119
    This routine reads the next n bits from the bitstream bs.  n will be
151
    This routine reads the next n bits from the bitstream bs.  n will be
120
    at most 16.
152
    at most 16.
121
*/
153
*/
122
 
154
 
123
unsigned de_bits
155
unsigned
124
    PROTO_N ( ( bs, n ) )
-
 
125
    PROTO_T ( BITSTREAM *bs X unsigned n )
156
de_bits(BITSTREAM *bs, unsigned n)
126
{
157
{
127
    unsigned long d = 0 ;
158
	unsigned long d = 0;
128
    string text = bs->text ;
159
	string text = bs->text;
129
    unsigned sz = bs->size ;
160
	unsigned sz = bs->size;
130
    unsigned bytes = bs->bytes ;
161
	unsigned bytes = bs->bytes;
131
    unsigned bits = 0 ;
162
	unsigned bits = 0;
132
 
163
 
133
    /* Build up result */
164
	/* Build up result */
134
    unsigned m = n + bs->bits ;
165
	unsigned m = n + bs->bits;
135
    while ( m ) {
166
	while (m) {
136
	unsigned b ;
167
		unsigned b;
137
	unsigned long c ;
168
		unsigned long c;
138
 
169
 
139
	/* Refill buffer from file if necessary */
170
		/* Refill buffer from file if necessary */
140
	if ( bytes >= sz ) {
171
		if (bytes >= sz) {
141
	    sz = fill_bitstream ( bs ) ;
172
			sz = fill_bitstream(bs);
142
	    bytes = 0 ;
173
			bytes = 0;
-
 
174
			bits = 0;
-
 
175
		}
-
 
176
 
-
 
177
		/* Get next character */
-
 
178
		c = (unsigned long)text[bytes];
-
 
179
		d = ((d << BYTE_SIZE) | c);
-
 
180
		b = BYTE_SIZE - bits;
-
 
181
		if (m < b) {
-
 
182
			d >>= (b - m);
-
 
183
			bits += m;
-
 
184
			m = 0;
-
 
185
		} else {
-
 
186
			bits += b;
-
 
187
			m -= b;
-
 
188
		}
-
 
189
		if (bits == BYTE_SIZE) {
143
	    bits = 0 ;
190
			bits = 0;
-
 
191
			bytes++;
-
 
192
		}
144
	}
193
	}
145
 
-
 
146
	/* Get next character */
-
 
147
	c = ( unsigned long ) text [ bytes ] ;
-
 
148
	d = ( ( d << BYTE_SIZE ) | c ) ;
-
 
149
	b = BYTE_SIZE - bits ;
-
 
150
	if ( m < b ) {
-
 
151
	    d >>= ( b - m ) ;
-
 
152
	    bits += m ;
-
 
153
	    m = 0 ;
-
 
154
	} else {
-
 
155
	    bits += b ;
-
 
156
	    m -= b ;
-
 
157
	}
-
 
158
	if ( bits == BYTE_SIZE ) {
-
 
159
	    bits = 0 ;
-
 
160
	    bytes++ ;
-
 
161
	}
-
 
162
    }
-
 
163
    bs->bytes = bytes ;
194
	bs->bytes = bytes;
164
    bs->bits = bits ;
195
	bs->bits = bits;
165
    d &= bit_mask [n] ;
196
	d &= bit_mask[n];
166
    return ( ( unsigned ) d ) ;
197
	return ((unsigned)d);
167
}
198
}
168
 
199
 
169
 
200
 
170
/*
201
/*
171
    READ A LARGE NUMBER OF BITS FROM A BITSTREAM
202
    READ A LARGE NUMBER OF BITS FROM A BITSTREAM
172
 
203
 
173
    This routine is identical to de_bits except that it works for up to
204
    This routine is identical to de_bits except that it works for up to
174
    32 bits.
205
    32 bits.
175
*/
206
*/
176
 
-
 
177
unsigned long de_long_bits
-
 
178
    PROTO_N ( ( bs, n ) )
-
 
179
    PROTO_T ( BITSTREAM *bs X unsigned n )
-
 
180
{
-
 
181
    unsigned long a, b ;
-
 
182
    if ( n > 16 ) {
-
 
183
	a = ( unsigned long ) de_bits ( bs, n - 16 ) ;
-
 
184
	n = 16 ;
-
 
185
    } else {
-
 
186
	a = 0 ;
-
 
187
    }
-
 
188
    b = ( unsigned long ) de_bits ( bs, n ) ;
-
 
189
    return ( ( a << 16 ) | b ) ;
-
 
190
}
-
 
191
 
-
 
192
 
-
 
193
/*
-
 
194
    READ AN INTEGER FROM A BITSTREAM
-
 
195
 
207
 
196
    This routine reads an integer as a series of octal digits from the
-
 
197
    bitstream bs.
-
 
198
*/
-
 
199
 
-
 
200
unsigned long de_int
208
unsigned long
201
    PROTO_N ( ( bs ) )
-
 
202
    PROTO_T ( BITSTREAM *bs )
209
de_long_bits(BITSTREAM *bs, unsigned n)
203
{
210
{
204
    unsigned long d, n = 0 ;
211
	unsigned long a, b;
205
    for ( ; ; ) {
212
	if (n > 16) {
206
	d = ( unsigned long ) de_bits ( bs, ( unsigned ) 4 ) ;
213
		a = (unsigned long)de_bits(bs, n - 16);
-
 
214
		n = 16;
207
	if ( d & 0x8 ) break ;
215
	} else {
208
	n = ( ( n << 3 ) | d ) ;
216
		a = 0;
209
    }
217
	}
210
    n = ( ( n << 3 ) | ( d & 0x7 ) ) ;
218
	b = (unsigned long)de_bits(bs, n);
211
    return ( n ) ;
219
	return ((a << 16) | b);
212
}
220
}
213
 
221
 
214
 
222
 
215
/*
223
/*
-
 
224
    READ AN INTEGER FROM A BITSTREAM
-
 
225
 
-
 
226
    This routine reads an integer as a series of octal digits from the
-
 
227
    bitstream bs.
-
 
228
*/
-
 
229
 
-
 
230
unsigned long
-
 
231
de_int(BITSTREAM *bs)
-
 
232
{
-
 
233
	unsigned long d, n = 0;
-
 
234
	for (;;) {
-
 
235
		d = (unsigned long)de_bits(bs,(unsigned)4);
-
 
236
		if (d & 0x8) {
-
 
237
			break;
-
 
238
		}
-
 
239
		n = ((n << 3) | d);
-
 
240
	}
-
 
241
	n = ((n << 3) | (d & 0x7));
-
 
242
	return (n);
-
 
243
}
-
 
244
 
-
 
245
 
-
 
246
/*
216
    ALIGN A BITSTREAM TO A BYTE BOUNDARY
247
    ALIGN A BITSTREAM TO A BYTE BOUNDARY
217
 
248
 
218
    This routine reads enough bits from the bitstream bs to align it to
249
    This routine reads enough bits from the bitstream bs to align it to
219
    the next byte boundary.
250
    the next byte boundary.
220
*/
251
*/
221
 
252
 
222
void de_boundary
253
void
223
    PROTO_N ( ( bs ) )
-
 
224
    PROTO_T ( BITSTREAM *bs )
254
de_boundary(BITSTREAM *bs)
225
{
255
{
226
    unsigned bits = bs->bits ;
256
	unsigned bits = bs->bits;
-
 
257
	if (bits) {
227
    if ( bits ) IGNORE de_bits ( bs, BYTE_SIZE - bits ) ;
258
		IGNORE de_bits(bs, BYTE_SIZE - bits);
-
 
259
	}
228
    return ;
260
	return;
229
}
261
}
230
 
262
 
231
 
263
 
232
/*
264
/*
233
    READ A STRING FROM A BITSTREAM
265
    READ A STRING FROM A BITSTREAM
234
 
266
 
235
    This routine reads a string from the bitstream bs into the buffer bf.
267
    This routine reads a string from the bitstream bs into the buffer bf.
236
*/
268
*/
237
 
269
 
238
void de_tdfstring
270
void
239
    PROTO_N ( ( bs, bf ) )
-
 
240
    PROTO_T ( BITSTREAM *bs X BUFFER *bf )
271
de_tdfstring(BITSTREAM *bs, BUFFER *bf)
241
{
272
{
242
    unsigned m = ( unsigned ) de_int ( bs ) ;
273
	unsigned m = (unsigned)de_int(bs);
243
    unsigned long n = de_int ( bs ) ;
274
	unsigned long n = de_int(bs);
244
    string p = stretch_buffer ( bf, bf->posn, ( gen_size ) n ) ;
275
	string p = stretch_buffer(bf, bf->posn,(gen_size)n);
245
    bf->posn = p + n ;
276
	bf->posn = p + n;
246
    while ( n ) {
277
	while (n) {
247
	int ch = CHAR_SIMPLE ;
278
		int ch = CHAR_SIMPLE;
248
	unsigned long c = ( unsigned long ) de_bits ( bs, m ) ;
279
		unsigned long c = (unsigned long)de_bits(bs, m);
249
	c = from_ascii ( c, &ch ) ;
280
		c = from_ascii(c, &ch);
250
	*( p++ ) = ( character ) c ;
281
		*(p++) = (character)c;
251
	n-- ;
282
		n--;
252
    }
283
	}
253
    *p = 0 ;
284
	*p = 0;
254
    return ;
285
	return;
255
}
286
}