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 "file.h"
62
#include "file.h"
33
#include "types.h"
63
#include "types.h"
-
 
64
#include "fetch.h"
34
#include "utility.h"
65
#include "utility.h"
35
 
66
 
36
 
67
 
37
/*
68
/*
38
    CURRENT FILE POINTER
69
    CURRENT FILE POINTER
Line 40... Line 71...
40
    As they are read from the input file bits are stored in input_buff.
71
    As they are read from the input file bits are stored in input_buff.
41
    The current number of bits in the buffer is bits_in_buff.  bytes_read
72
    The current number of bits in the buffer is bits_in_buff.  bytes_read
42
    records the position in the input file.
73
    records the position in the input file.
43
*/
74
*/
44
 
75
 
45
unsigned bits_in_buff = 0 ;
76
unsigned bits_in_buff = 0;
46
long bytes_read = 0 ;
77
long bytes_read = 0;
47
static unsigned long input_buff = 0 ;
78
static unsigned long input_buff = 0;
48
 
79
 
49
 
80
 
50
/*
81
/*
51
    ALIGN TO NEXT BYTE BOUNDARY
82
    ALIGN TO NEXT BYTE BOUNDARY
52
 
83
 
53
    Any bits remaining in the current byte are ignored.
84
    Any bits remaining in the current byte are ignored.
54
*/
85
*/
55
 
86
 
56
void byte_align
87
void
57
    PROTO_Z ()
88
byte_align(void)
58
{
89
{
59
    bits_in_buff = 0 ;
90
    bits_in_buff = 0;
60
    return ;
91
    return;
61
}
92
}
62
 
93
 
63
 
94
 
64
/*
95
/*
65
    FETCH A NUMBER OF BITS
96
    FETCH A NUMBER OF BITS
66
 
97
 
67
    This routine reads the next n bits from the input file and returns
98
    This routine reads the next n bits from the input file and returns
68
    them as a long.
99
    them as a long.
69
*/
100
*/
70
 
101
 
71
long fetch
102
long
72
    PROTO_N ( ( n ) )
-
 
73
    PROTO_T ( int n )
103
fetch(int n)
74
{
104
{
75
    unsigned long s ;
105
    unsigned long s;
76
    unsigned long r = 0 ;
106
    unsigned long r = 0;
77
    unsigned b = ( unsigned ) n ;
107
    unsigned b = (unsigned)n;
78
    while ( b ) {
108
    while (b) {
79
	unsigned m ;
109
	unsigned m;
80
	if ( bits_in_buff == 0 ) {
110
	if (bits_in_buff == 0) {
81
	    int c = getc ( input ) ;
111
	    int c = getc(input);
82
	    bytes_read++ ;
112
	    bytes_read++;
83
	    if ( c == EOF ) {
113
	    if (c == EOF) {
84
		input_error ( "Premature end of file" ) ;
114
		input_error("Premature end of file");
85
		c = 0xff ;
115
		c = 0xff;
86
	    }
116
	    }
87
	    bits_in_buff = BYTESIZE ;
117
	    bits_in_buff = BYTESIZE;
88
	    input_buff = ( unsigned long ) ( c & 0xff ) ;
118
	    input_buff = (unsigned long)(c & 0xff);
89
	}
119
	}
90
	m = ( b <= bits_in_buff ? b : bits_in_buff ) ;
120
	m = (b <= bits_in_buff ? b : bits_in_buff);
91
	s = ( input_buff << m ) ;
121
	s = (input_buff << m);
92
	r = ( ( r << m ) | ( ( s >> BYTESIZE ) & 0xff ) ) ;
122
	r = ((r << m) | ((s >> BYTESIZE) & 0xff));
93
	b -= m ;
123
	b -= m;
94
	bits_in_buff -= m ;
124
	bits_in_buff -= m;
95
	input_buff = ( s & 0xff ) ;
125
	input_buff = (s & 0xff);
96
    }
126
    }
97
    return ( ( long ) r ) ;
127
    return((long)r);
98
}
128
}
99
 
129
 
100
 
130
 
101
/*
131
/*
102
    FIND CURRENT POSITION IN FILE
132
    FIND CURRENT POSITION IN FILE
103
 
133
 
104
    The current position in the input file (in bits) is returned.
134
    The current position in the input file (in bits) is returned.
105
*/
135
*/
106
 
136
 
107
long input_posn
137
long
108
    PROTO_Z ()
138
input_posn(void)
109
{
139
{
110
    return ( BYTESIZE * bytes_read - ( long ) bits_in_buff ) ;
140
    return(BYTESIZE * bytes_read - (long)bits_in_buff);
111
}
141
}
112
 
142
 
113
 
143
 
114
/*
144
/*
115
    GO TO A POSITION IN FILE
145
    GO TO A POSITION IN FILE
116
 
146
 
117
    The position in the input file is set to the nth bit.
147
    The position in the input file is set to the nth bit.
118
*/
148
*/
119
 
149
 
120
void input_goto
150
void
121
    PROTO_N ( ( n ) )
-
 
122
    PROTO_T ( long n )
151
input_goto(long n)
123
{
152
{
124
    int b = ( int ) ( n % BYTESIZE ) ;
153
    int b = (int)(n % BYTESIZE);
125
    bytes_read = ( n / BYTESIZE ) ;
154
    bytes_read = (n / BYTESIZE);
126
    bits_in_buff = 0 ;
155
    bits_in_buff = 0;
127
    if ( fseek ( input, bytes_read, SEEK_SET ) ) {
156
    if (fseek(input, bytes_read, SEEK_SET)) {
128
	bits_in_buff = ( unsigned ) b ;
157
	bits_in_buff = (unsigned)b;
129
	input_error ( "Illegal seek command" ) ;
158
	input_error("Illegal seek command");
130
	return ;
159
	return;
131
    }
160
    }
132
    if ( b ) IGNORE fetch ( b ) ;
161
    if (b)IGNORE fetch(b);
133
    return ;
162
    return;
134
}
163
}
135
 
164
 
136
 
165
 
137
/*
166
/*
138
    SKIP A NUMBER OF BITS
167
    SKIP A NUMBER OF BITS
139
 
168
 
140
    The next n bits in the input file are ignored.
169
    The next n bits in the input file are ignored.
141
*/
170
*/
142
 
171
 
143
void input_skip
172
void
144
    PROTO_N ( ( n ) )
-
 
145
    PROTO_T ( long n )
173
input_skip(long n)
146
{
174
{
147
    if ( n <= 4 * BYTESIZE ) {
175
    if (n <= 4 * BYTESIZE) {
148
	IGNORE fetch ( ( int ) n ) ;
176
	IGNORE fetch((int)n);
149
    } else {
177
    } else {
150
	long m = input_posn () + n ;
178
	long m = input_posn() + n;
151
	input_goto ( m ) ;
179
	input_goto(m);
152
    }
180
    }
153
    return ;
181
    return;
154
}
182
}