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-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
*/
Line 40... Line 70...
40
#include "fetch.h"
70
#include "fetch.h"
41
#include "file.h"
71
#include "file.h"
42
#include "table.h"
72
#include "table.h"
43
#include "utility.h"
73
#include "utility.h"
44
#include "write.h"
74
#include "write.h"
45
extern char *progname ;
75
extern char *progname;
46
extern char *capname ;
76
extern char *capname;
47
extern int decode_status ;
77
extern int decode_status;
48
extern int have_version ;
78
extern int have_version;
49
 
79
 
50
 
80
 
51
/*
81
/*
52
    ARE INPUT AND OUTPUT TEXT OR CODE?
82
    ARE INPUT AND OUTPUT TEXT OR CODE?
53
 
83
 
54
    The form of any error message depends on whether the input file
84
    The form of any error message depends on whether the input file
55
    is text or code.
85
    is text or code.
56
*/
86
*/
57
 
87
 
58
boolean text_input = 1 ;
88
boolean text_input = 1;
59
boolean text_output = 0 ;
89
boolean text_output = 0;
60
 
90
 
61
 
91
 
62
/*
92
/*
63
    EXIT STATUS
93
    EXIT STATUS
64
 
94
 
65
    The overall exit status of the program.
95
    The overall exit status of the program.
66
*/
96
*/
67
 
97
 
68
int exit_status = EXIT_SUCCESS ;
98
int exit_status = EXIT_SUCCESS;
69
 
99
 
70
 
100
 
71
/*
101
/*
72
    REPORT A FATAL ERROR
102
    REPORT A FATAL ERROR
73
 
103
 
74
    The error s is reported and the program exits.
104
    The error s is reported and the program exits.
75
*/
105
*/
76
 
106
 
77
void fatal_error
107
void
78
    PROTO_V ( ( char *s, ... ) ) /* VARARGS */
108
fatal_error(char *s, ...) /* VARARGS */
79
{
109
{
80
    va_list args ;
110
    va_list args;
81
#if FS_STDARG
111
#if FS_STDARG
82
    va_start ( args, s ) ;
112
    va_start(args, s);
83
#else
113
#else
84
    char *s ;
114
    char *s;
85
    va_start ( args ) ;
115
    va_start(args);
86
    s = va_arg ( args, char * ) ;
116
    s = va_arg(args, char *);
87
#endif
117
#endif
88
    if ( progname ) IGNORE fprintf ( stderr, "%s: ", progname ) ;
118
    if (progname)IGNORE fprintf(stderr, "%s: ", progname);
89
    IGNORE fprintf ( stderr, "Error: " ) ;
119
    IGNORE fprintf(stderr, "Error: ");
90
    IGNORE vfprintf ( stderr, s, args ) ;
120
    IGNORE vfprintf(stderr, s, args);
91
    IGNORE fprintf ( stderr, ".\n" ) ;
121
    IGNORE fprintf(stderr, ".\n");
92
    va_end ( args ) ;
122
    va_end(args);
93
    exit ( EXIT_FAILURE ) ;
123
    exit(EXIT_FAILURE);
94
}
124
}
95
 
125
 
96
 
126
 
97
/*
127
/*
98
    IS AN INPUT ERROR FATAL?
128
    IS AN INPUT ERROR FATAL?
99
 
129
 
100
    Not all input errors cause an immediate exit.  These should set
130
    Not all input errors cause an immediate exit.  These should set
101
    is_fatal to false before calling input_error.
131
    is_fatal to false before calling input_error.
102
*/
132
*/
103
 
133
 
104
boolean is_fatal = 1 ;
134
boolean is_fatal = 1;
105
 
135
 
106
 
136
 
107
/*
137
/*
108
    REPORT AN INPUT ERROR
138
    REPORT AN INPUT ERROR
109
 
139
 
110
    The input error s is reported and the program exits.
140
    The input error s is reported and the program exits.
111
*/
141
*/
112
 
142
 
113
void input_error
143
void
114
    PROTO_V ( ( char *s, ... ) ) /* VARARGS */
144
input_error(char *s, ...) /* VARARGS */
115
{
145
{
116
    va_list args ;
146
    va_list args;
117
#if FS_STDARG
147
#if FS_STDARG
118
    va_start ( args, s ) ;
148
    va_start(args, s);
119
#else
149
#else
120
    char *s ;
150
    char *s;
121
    va_start ( args ) ;
151
    va_start(args);
122
    s = va_arg ( args, char * ) ;
152
    s = va_arg(args, char *);
123
#endif
153
#endif
124
    if ( progname ) IGNORE fprintf ( stderr, "%s: ", progname ) ;
154
    if (progname)IGNORE fprintf(stderr, "%s: ", progname);
125
    IGNORE fprintf ( stderr, "Error: " ) ;
155
    IGNORE fprintf(stderr, "Error: ");
126
    IGNORE vfprintf ( stderr, s, args ) ;
156
    IGNORE vfprintf(stderr, s, args);
127
    if ( input_file ) {
157
    if (input_file) {
128
	IGNORE fprintf ( stderr, ", %s", input_file ) ;
158
	IGNORE fprintf(stderr, ", %s", input_file);
129
	if ( text_input ) {
159
	if (text_input) {
130
	    IGNORE fprintf ( stderr, ", line %ld", line_no ) ;
160
	    IGNORE fprintf(stderr, ", line %ld", line_no);
131
	} else {
161
	} else {
132
	    long b = input_posn () ;
162
	    long b = input_posn();
133
	    if ( capname ) {
163
	    if (capname) {
134
		IGNORE fprintf ( stderr, ", capsule %s", capname ) ;
164
		IGNORE fprintf(stderr, ", capsule %s", capname);
135
	    }
165
	    }
136
	    switch ( decode_status ) {
166
	    switch (decode_status) {
137
		case 0 : {
167
		case 0: {
138
		    IGNORE fprintf ( stderr, " (at outermost level)" ) ;
168
		    IGNORE fprintf(stderr, " (at outermost level)");
139
		    break ;
169
		    break;
140
		}
170
		}
141
		case 1 : {
171
		case 1: {
142
		    IGNORE fprintf ( stderr, " (in linking information)" ) ;
172
		    IGNORE fprintf(stderr, " (in linking information)");
143
		    break ;
173
		    break;
144
		}
174
		}
145
		case 2 : {
175
		case 2: {
146
		    IGNORE fprintf ( stderr, " (in unit body)" ) ;
176
		    IGNORE fprintf(stderr, " (in unit body)");
147
		    break ;
177
		    break;
148
		}
178
		}
149
	    }
179
	    }
150
	    IGNORE fprintf ( stderr, ", byte %ld, bit %ld", b / 8, b % 8 ) ;
180
	    IGNORE fprintf(stderr, ", byte %ld, bit %ld", b / 8, b % 8);
151
	    if ( decode_status == 0 ) {
181
	    if (decode_status == 0) {
152
		IGNORE fprintf ( stderr, " (Illegal TDF capsule?)" ) ;
182
		IGNORE fprintf(stderr, " (Illegal TDF capsule?)");
153
	    }
183
	    }
154
	    if ( decode_status >= 1 && !have_version ) {
184
	    if (decode_status >= 1 && !have_version) {
155
		IGNORE fprintf ( stderr, " (TDF version error?)" ) ;
185
		IGNORE fprintf(stderr, " (TDF version error?)");
156
	    }
186
	    }
157
	}
187
	}
158
    }
188
    }
159
    IGNORE fprintf ( stderr, ".\n" ) ;
189
    IGNORE fprintf(stderr, ".\n");
160
    va_end ( args ) ;
190
    va_end(args);
161
    if ( is_fatal ) {
191
    if (is_fatal) {
162
	if ( text_output ) {
192
	if (text_output) {
163
	    sort_all () ;
193
	    sort_all();
164
	    print_capsule () ;
194
	    print_capsule();
165
	    IGNORE fputs ( "# TERMINATED ON INPUT ERROR\n", output ) ;
195
	    IGNORE fputs("# TERMINATED ON INPUT ERROR\n", output);
166
	}
196
	}
167
	exit ( EXIT_FAILURE ) ;
197
	exit(EXIT_FAILURE);
168
    }
198
    }
169
    is_fatal = 1 ;
199
    is_fatal = 1;
170
    exit_status = EXIT_FAILURE ;
200
    exit_status = EXIT_FAILURE;
171
    return ;
201
    return;
172
}
202
}
173
 
203
 
174
 
204
 
175
/*
205
/*
176
    ISSUE A WARNING
206
    ISSUE A WARNING
177
 
207
 
178
    The warning message s is printed.
208
    The warning message s is printed.
179
*/
209
*/
180
 
210
 
181
void warning
211
void
182
    PROTO_V ( ( char *s, ... ) ) /* VARARGS */
212
warning(char *s, ...) /* VARARGS */
183
{
213
{
184
    va_list args ;
214
    va_list args;
185
#if FS_STDARG
215
#if FS_STDARG
186
    va_start ( args, s ) ;
216
    va_start(args, s);
187
#else
217
#else
188
    char *s ;
218
    char *s;
189
    va_start ( args ) ;
219
    va_start(args);
190
    s = va_arg ( args, char * ) ;
220
    s = va_arg(args, char *);
191
#endif
221
#endif
192
    if ( progname ) IGNORE fprintf ( stderr, "%s: ", progname ) ;
222
    if (progname)IGNORE fprintf(stderr, "%s: ", progname);
193
    IGNORE fprintf ( stderr, "Warning: " ) ;
223
    IGNORE fprintf(stderr, "Warning: ");
194
    IGNORE vfprintf ( stderr, s, args ) ;
224
    IGNORE vfprintf(stderr, s, args);
195
    IGNORE fprintf ( stderr, ".\n" ) ;
225
    IGNORE fprintf(stderr, ".\n");
196
    va_end ( args ) ;
226
    va_end(args);
197
    return ;
227
    return;
198
}
228
}
199
 
229
 
200
 
230
 
201
/*
231
/*
202
    ALLOCATE A SECTION OF MEMORY
232
    ALLOCATE A SECTION OF MEMORY
203
 
233
 
204
    This routine allocates n bytes of memory.
234
    This routine allocates n bytes of memory.
205
*/
235
*/
206
 
236
 
207
pointer xalloc
237
pointer
208
    PROTO_N ( ( n ) )
-
 
209
    PROTO_T ( int n )
238
xalloc(int n)
210
{
239
{
211
    pointer ptr ;
240
    pointer ptr;
212
    if ( n == 0 ) return ( null ) ;
241
    if (n == 0) return(null);
213
    ptr = ( pointer ) malloc ( ( size_t ) n ) ;
242
    ptr = (pointer)malloc((size_t)n);
214
    if ( ptr == null ) {
243
    if (ptr == null) {
215
	if ( !text_input && decode_status == 0 ) {
244
	if (!text_input && decode_status == 0) {
216
	    fatal_error ( "Memory allocation error (Illegal TDF capsule?)" ) ;
245
	    fatal_error("Memory allocation error (Illegal TDF capsule?)");
217
	}
246
	}
218
	fatal_error ( "Memory allocation error" ) ;
247
	fatal_error("Memory allocation error");
219
    }
248
    }
220
    return ( ptr ) ;
249
    return(ptr);
221
}
250
}
222
 
251
 
223
 
252
 
224
/*
253
/*
225
    REALLOCATE A SECTION OF MEMORY
254
    REALLOCATE A SECTION OF MEMORY
226
 
255
 
227
    This routine reallocates n bytes of memory for the pointer p.
256
    This routine reallocates n bytes of memory for the pointer p.
228
*/
257
*/
229
 
258
 
230
pointer xrealloc
259
pointer
231
    PROTO_N ( ( p, n ) )
-
 
232
    PROTO_T ( pointer p X int n )
260
xrealloc(pointer p, int n)
233
{
261
{
234
    pointer ptr ;
262
    pointer ptr;
235
    if ( n == 0 ) return ( null ) ;
263
    if (n == 0) return(null);
236
    if ( p == null ) return ( xalloc ( n ) ) ;
264
    if (p == null) return(xalloc(n));
237
    ptr = ( pointer ) realloc ( p, ( size_t ) n ) ;
265
    ptr = (pointer)realloc(p,(size_t)n);
238
    if ( ptr == null ) fatal_error ( "Memory allocation error" ) ;
266
    if (ptr == null)fatal_error("Memory allocation error");
239
    return ( ptr ) ;
267
    return(ptr);
240
}
268
}
241
 
269
 
242
 
270
 
243
/*
271
/*
244
    MAKE A COPY OF A STRING
272
    MAKE A COPY OF A STRING
245
 
273
 
246
    This routine makes a permanent copy of the string s of length n.
274
    This routine makes a permanent copy of the string s of length n.
247
*/
275
*/
248
 
276
 
249
char *string_copy
277
char *
250
    PROTO_N ( ( s, n ) )
-
 
251
    PROTO_T ( char *s X int n )
278
string_copy(char *s, int n)
252
{
279
{
253
    int m = ( n + 1 ) * ( int ) sizeof ( char ) ;
280
    int m = (n + 1)*(int)sizeof(char);
254
    char *p = ( char * ) xalloc ( m ) ;
281
    char *p = (char *)xalloc(m);
255
    IGNORE strncpy ( p, s, ( size_t ) n ) ;
282
    IGNORE strncpy(p, s,(size_t)n);
256
    p [n] = 0 ;
283
    p[n] = 0;
257
    return ( p ) ;
284
    return(p);
258
}
285
}
259
 
286
 
260
 
287
 
261
/*
288
/*
262
    MAKE A TEMPORARY COPY OF A STRING
289
    MAKE A TEMPORARY COPY OF A STRING
263
 
290
 
264
    This routine copies a string into a temporary buffer.
291
    This routine copies a string into a temporary buffer.
265
*/
292
*/
266
 
293
 
267
char *temp_copy
294
char *
268
    PROTO_N ( ( s ) )
-
 
269
    PROTO_T ( char *s )
295
temp_copy(char *s)
270
{
296
{
271
    static char *buff = null ;
297
    static char *buff = null;
272
    static int bufflen = 0 ;
298
    static int bufflen = 0;
273
    int n = ( int ) strlen ( s ) + 1 ;
299
    int n = (int)strlen(s) + 1;
274
    if ( n >= bufflen ) {
300
    if (n >= bufflen) {
275
	bufflen = n + 100 ;
301
	bufflen = n + 100;
276
	buff = ( char * ) xrealloc ( ( pointer ) buff, bufflen ) ;
302
	buff = (char *)xrealloc((pointer)buff, bufflen);
277
    }
303
    }
278
    IGNORE strcpy ( buff, s ) ;
304
    IGNORE strcpy(buff, s);
279
    return ( buff ) ;
305
    return(buff);
280
}
306
}
281
 
307
 
282
 
308
 
283
/*
309
/*
284
    CONVERT AN UNSIGNED LONG TO OCTAL
310
    CONVERT AN UNSIGNED LONG TO OCTAL
285
 
311
 
286
    The result is returned as a string of octal digits.
312
    The result is returned as a string of octal digits.
287
*/
313
*/
288
 
314
 
289
char *ulong_to_octal
315
char *
290
    PROTO_N ( ( n ) )
-
 
291
    PROTO_T ( unsigned long n )
316
ulong_to_octal(unsigned long n)
292
{
317
{
293
    int i = 99 ;
318
    int i = 99;
294
    char buff [100] ;
319
    char buff[100];
295
    if ( n == 0 ) return ( "0" ) ;
320
    if (n == 0) return("0");
296
    buff [i] = 0 ;
321
    buff[i] = 0;
297
    while ( n ) {
322
    while (n) {
298
	buff [ --i ] = ( char ) ( '0' + ( n & 7 ) ) ;
323
	buff[--i] = (char)('0' + (n & 7));
299
	n >>= 3 ;
324
	n >>= 3;
300
    }
325
    }
301
    return ( string_copy ( buff + i, 99 - i ) ) ;
326
    return(string_copy(buff + i, 99 - i));
302
}
327
}
303
 
328
 
304
 
329
 
305
/*
330
/*
306
    CONVERT AN OCTAL STRING TO AN UNSIGNED LONG
331
    CONVERT AN OCTAL STRING TO AN UNSIGNED LONG
307
 
332
 
308
    Any overflow is ignored (but see below).
333
    Any overflow is ignored (but see below).
309
*/
334
*/
310
 
335
 
311
unsigned long octal_to_ulong
336
unsigned long
312
    PROTO_N ( ( num ) )
-
 
313
    PROTO_T ( char *num )
337
octal_to_ulong(char *num)
314
{
338
{
315
    unsigned long n = 0 ;
339
    unsigned long n = 0;
316
    for ( ; *num ; num++ ) {
340
    for (; *num; num++) {
317
	n = ( n << 3 ) + ( unsigned long ) ( *num - '0' ) ;
341
	n = (n << 3) + (unsigned long)(*num - '0');
318
    }
342
    }
319
    return ( n ) ;
343
    return(n);
320
}
344
}
321
 
345
 
322
 
346
 
323
/*
347
/*
324
    DOES THE GIVEN OCTAL STRING FIT INTO AN UNSIGNED LONG?
348
    DOES THE GIVEN OCTAL STRING FIT INTO AN UNSIGNED LONG?
Line 326... Line 350...
326
    The number of binary digits needed to represent the octal number
350
    The number of binary digits needed to represent the octal number
327
    is calculated and compared against the number of bits in an
351
    is calculated and compared against the number of bits in an
328
    unsigned long, minus sz (to allow for a sign bit).
352
    unsigned long, minus sz (to allow for a sign bit).
329
*/
353
*/
330
 
354
 
331
boolean fits_ulong
355
boolean
332
    PROTO_N ( ( num, sz ) )
-
 
333
    PROTO_T ( char *num X int sz )
356
fits_ulong(char *num, int sz)
334
{
357
{
335
    int n = 3 * ( int ) strlen ( num ) ;
358
    int n = 3 *(int)strlen(num);
336
    int m = BYTESIZE * ( int ) sizeof ( unsigned long ) - sz ;
359
    int m = BYTESIZE *(int)sizeof(unsigned long) - sz;
337
    switch ( *num ) {
360
    switch (*num) {
338
	case '0' : n -= 3 ; break ;
361
	case '0': n -= 3; break;
339
	case '1' : n -= 2 ; break ;
362
	case '1': n -= 2; break;
340
	case '2' : n -= 1 ; break ;
363
	case '2': n -= 1; break;
341
	case '3' : n -= 1 ; break ;
364
	case '3': n -= 1; break;
342
    }
365
    }
343
    if ( n <= m ) return ( 1 ) ;
366
    if (n <= m) return(1);
344
    return ( 0 ) ;
367
    return(0);
345
}
368
}