Subversion Repositories tendra.SVN

Rev

Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
7 7u83 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
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
7 7u83 33
 
2 7u83 34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
7 7u83 42
 
2 7u83 43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
7 7u83 45
 
2 7u83 46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
7 7u83 49
 
2 7u83 50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
7 7u83 53
 
2 7u83 54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
#include "config.h"
62
#include "file.h"
63
#include "types.h"
7 7u83 64
#include "fetch.h"
2 7u83 65
#include "utility.h"
66
 
67
 
68
/*
69
    CURRENT FILE POINTER
70
 
71
    As they are read from the input file bits are stored in input_buff.
72
    The current number of bits in the buffer is bits_in_buff.  bytes_read
73
    records the position in the input file.
74
*/
75
 
7 7u83 76
unsigned bits_in_buff = 0;
77
long bytes_read = 0;
78
static unsigned long input_buff = 0;
2 7u83 79
 
80
 
81
/*
82
    ALIGN TO NEXT BYTE BOUNDARY
83
 
84
    Any bits remaining in the current byte are ignored.
85
*/
86
 
7 7u83 87
void
88
byte_align(void)
2 7u83 89
{
7 7u83 90
    bits_in_buff = 0;
91
    return;
2 7u83 92
}
93
 
94
 
95
/*
96
    FETCH A NUMBER OF BITS
97
 
98
    This routine reads the next n bits from the input file and returns
99
    them as a long.
100
*/
101
 
7 7u83 102
long
103
fetch(int n)
2 7u83 104
{
7 7u83 105
    unsigned long s;
106
    unsigned long r = 0;
107
    unsigned b = (unsigned)n;
108
    while (b) {
109
	unsigned m;
110
	if (bits_in_buff == 0) {
111
	    int c = getc(input);
112
	    bytes_read++;
113
	    if (c == EOF) {
114
		input_error("Premature end of file");
115
		c = 0xff;
2 7u83 116
	    }
7 7u83 117
	    bits_in_buff = BYTESIZE;
118
	    input_buff = (unsigned long)(c & 0xff);
2 7u83 119
	}
7 7u83 120
	m = (b <= bits_in_buff ? b : bits_in_buff);
121
	s = (input_buff << m);
122
	r = ((r << m) | ((s >> BYTESIZE) & 0xff));
123
	b -= m;
124
	bits_in_buff -= m;
125
	input_buff = (s & 0xff);
2 7u83 126
    }
7 7u83 127
    return((long)r);
2 7u83 128
}
129
 
130
 
131
/*
132
    FIND CURRENT POSITION IN FILE
133
 
134
    The current position in the input file (in bits) is returned.
135
*/
136
 
7 7u83 137
long
138
input_posn(void)
2 7u83 139
{
7 7u83 140
    return(BYTESIZE * bytes_read - (long)bits_in_buff);
2 7u83 141
}
142
 
143
 
144
/*
145
    GO TO A POSITION IN FILE
146
 
147
    The position in the input file is set to the nth bit.
148
*/
149
 
7 7u83 150
void
151
input_goto(long n)
2 7u83 152
{
7 7u83 153
    int b = (int)(n % BYTESIZE);
154
    bytes_read = (n / BYTESIZE);
155
    bits_in_buff = 0;
156
    if (fseek(input, bytes_read, SEEK_SET)) {
157
	bits_in_buff = (unsigned)b;
158
	input_error("Illegal seek command");
159
	return;
2 7u83 160
    }
7 7u83 161
    if (b)IGNORE fetch(b);
162
    return;
2 7u83 163
}
164
 
165
 
166
/*
167
    SKIP A NUMBER OF BITS
168
 
169
    The next n bits in the input file are ignored.
170
*/
171
 
7 7u83 172
void
173
input_skip(long n)
2 7u83 174
{
7 7u83 175
    if (n <= 4 * BYTESIZE) {
176
	IGNORE fetch((int)n);
2 7u83 177
    } else {
7 7u83 178
	long m = input_posn() + n;
179
	input_goto(m);
2 7u83 180
    }
7 7u83 181
    return;
2 7u83 182
}