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

Warning: Attempt to read property "msg" on null in /usr/local/www/websvn.planix.org/blame.php on line 247
WebSVN – tendra.SVN – Blame – //branches/tendra4/src/producers/common/output/decode.c – Rev 2

Subversion Repositories tendra.SVN

Rev

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

Rev Author Line No. Line
2 7u83 1
/*
2
    		 Crown Copyright (c) 1997
3
 
4
    This TenDRA(r) Computer Program is subject to Copyright
5
    owned by the United Kingdom Secretary of State for Defence
6
    acting through the Defence Evaluation and Research Agency
7
    (DERA).  It is made available to Recipients with a
8
    royalty-free licence for its use, reproduction, transfer
9
    to other parties and amendment for any purpose not excluding
10
    product development provided that any such use et cetera
11
    shall be deemed to be acceptance of the following conditions:-
12
 
13
        (1) Its Recipients shall ensure that this Notice is
14
        reproduced upon any copies or amended versions of it;
15
 
16
        (2) Any amended version of it shall be clearly marked to
17
        show both the nature of and the organisation responsible
18
        for the relevant amendment or amendments;
19
 
20
        (3) Its onward transfer from a recipient to another
21
        party shall be deemed to be that party's acceptance of
22
        these conditions;
23
 
24
        (4) DERA gives no warranty or assurance as to its
25
        quality or suitability for any purpose and DERA accepts
26
        no liability whatsoever in relation to any use to which
27
        it may be put.
28
*/
29
 
30
 
31
#include "config.h"
32
#include "c_types.h"
33
#include "error.h"
34
#include "buffer.h"
35
#include "char.h"
36
#include "decode.h"
37
#include "encode.h"
38
#include "file.h"
39
#include "literal.h"
40
 
41
 
42
/*
43
    FILL A BITSTREAM
44
 
45
    This routine fills the bitstream bs from its associated file.  It
46
    returns the number of bytes read.
47
*/
48
 
49
static unsigned fill_bitstream
50
    PROTO_N ( ( bs ) )
51
    PROTO_T ( BITSTREAM *bs )
52
{
53
    unsigned sz ;
54
    FILE *f = bs->file ;
55
    string text = bs->text ;
56
    sz = ( unsigned ) fread ( ( gen_ptr ) text, sizeof ( character ),
57
			      ( size_t ) CHUNK_SIZE, f ) ;
58
    if ( sz == 0 ) {
59
	/* No more characters in file */
60
	bs->link = ( gen_ptr ) text ;
61
	while ( sz < CHUNK_SIZE ) {
62
	    /* Fill buffer with all ones */
63
	    text [ sz ] = ( character ) BYTE_MASK ;
64
	    sz++ ;
65
	}
66
    }
67
    bs->size = sz ;
68
    return ( sz ) ;
69
}
70
 
71
 
72
/*
73
    CHECK FOR END OF FILE
74
 
75
    This routine checks whether the end of the file associated with the
76
    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
78
    encountered.
79
*/
80
 
81
int de_eof
82
    PROTO_N ( ( bs ) )
83
    PROTO_T ( BITSTREAM *bs )
84
{
85
    if ( bs->link ) return ( 0 ) ;
86
    if ( bs->bytes >= bs->size ) {
87
	IGNORE fill_bitstream ( bs ) ;
88
	if ( bs->link ) return ( 1 ) ;
89
    }
90
    return ( 0 ) ;
91
}
92
 
93
 
94
/*
95
    TABLE OF BIT MASKS
96
 
97
    This array gives the bit masks for values up to 16 bits.
98
*/
99
 
100
static unsigned long bit_mask [17] = {
101
    0x0000,
102
#if FS_NUMBER_SUFFIX
103
    0x0001UL, 0x0003UL, 0x0007UL, 0x000fUL,
104
    0x001fUL, 0x003fUL, 0x007fUL, 0x00ffUL,
105
    0x01ffUL, 0x03ffUL, 0x07ffUL, 0x0fffUL,
106
    0x1fffUL, 0x3fffUL, 0x7fffUL, 0xffffUL
107
#else
108
    0x0001, 0x0003, 0x0007, 0x000f,
109
    0x001f, 0x003f, 0x007f, 0x00ff,
110
    0x01ff, 0x03ff, 0x07ff, 0x0fff,
111
    0x1fff, 0x3fff, 0x7fff, 0xffff
112
#endif
113
} ;
114
 
115
 
116
/*
117
    READ A NUMBER OF BITS FROM A BITSTREAM
118
 
119
    This routine reads the next n bits from the bitstream bs.  n will be
120
    at most 16.
121
*/
122
 
123
unsigned de_bits
124
    PROTO_N ( ( bs, n ) )
125
    PROTO_T ( BITSTREAM *bs X unsigned n )
126
{
127
    unsigned long d = 0 ;
128
    string text = bs->text ;
129
    unsigned sz = bs->size ;
130
    unsigned bytes = bs->bytes ;
131
    unsigned bits = 0 ;
132
 
133
    /* Build up result */
134
    unsigned m = n + bs->bits ;
135
    while ( m ) {
136
	unsigned b ;
137
	unsigned long c ;
138
 
139
	/* Refill buffer from file if necessary */
140
	if ( bytes >= sz ) {
141
	    sz = fill_bitstream ( bs ) ;
142
	    bytes = 0 ;
143
	    bits = 0 ;
144
	}
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 ;
164
    bs->bits = bits ;
165
    d &= bit_mask [n] ;
166
    return ( ( unsigned ) d ) ;
167
}
168
 
169
 
170
/*
171
    READ A LARGE NUMBER OF BITS FROM A BITSTREAM
172
 
173
    This routine is identical to de_bits except that it works for up to
174
    32 bits.
175
*/
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
 
196
    This routine reads an integer as a series of octal digits from the
197
    bitstream bs.
198
*/
199
 
200
unsigned long de_int
201
    PROTO_N ( ( bs ) )
202
    PROTO_T ( BITSTREAM *bs )
203
{
204
    unsigned long d, n = 0 ;
205
    for ( ; ; ) {
206
	d = ( unsigned long ) de_bits ( bs, ( unsigned ) 4 ) ;
207
	if ( d & 0x8 ) break ;
208
	n = ( ( n << 3 ) | d ) ;
209
    }
210
    n = ( ( n << 3 ) | ( d & 0x7 ) ) ;
211
    return ( n ) ;
212
}
213
 
214
 
215
/*
216
    ALIGN A BITSTREAM TO A BYTE BOUNDARY
217
 
218
    This routine reads enough bits from the bitstream bs to align it to
219
    the next byte boundary.
220
*/
221
 
222
void de_boundary
223
    PROTO_N ( ( bs ) )
224
    PROTO_T ( BITSTREAM *bs )
225
{
226
    unsigned bits = bs->bits ;
227
    if ( bits ) IGNORE de_bits ( bs, BYTE_SIZE - bits ) ;
228
    return ;
229
}
230
 
231
 
232
/*
233
    READ A STRING FROM A BITSTREAM
234
 
235
    This routine reads a string from the bitstream bs into the buffer bf.
236
*/
237
 
238
void de_tdfstring
239
    PROTO_N ( ( bs, bf ) )
240
    PROTO_T ( BITSTREAM *bs X BUFFER *bf )
241
{
242
    unsigned m = ( unsigned ) de_int ( bs ) ;
243
    unsigned long n = de_int ( bs ) ;
244
    string p = stretch_buffer ( bf, bf->posn, ( gen_size ) n ) ;
245
    bf->posn = p + n ;
246
    while ( n ) {
247
	int ch = CHAR_SIMPLE ;
248
	unsigned long c = ( unsigned long ) de_bits ( bs, m ) ;
249
	c = from_ascii ( c, &ch ) ;
250
	*( p++ ) = ( character ) c ;
251
	n-- ;
252
    }
253
    *p = 0 ;
254
    return ;
255
}