Subversion Repositories tendra.SVN

Rev

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