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
Line 79... Line 109...
79
 
109
 
80
 
110
 
81
/* VARIABLES */
111
/* VARIABLES */
82
/* All variables are initialised, jmf */
112
/* All variables are initialised, jmf */
83
 
113
 
84
static char * crt_ptr;	/* initialised by init_reader */
114
static char *crt_ptr;	/* initialised by init_reader */
85
static char * end_ptr;	/* initialised by init_reader */
115
static char *end_ptr;	/* initialised by init_reader */
86
 
116
 
87
static int getcode_bitposn;
117
static int getcode_bitposn;
88
static union pun_u
118
static union pun_u
89
  {unsigned int intc;
119
  { unsigned int intc;
90
    struct pun_s {char a; char b; char c; char d;} chars;
120
    struct pun_s {char a; char b; char c; char d;} chars;
91
  } crt_bits;		/* set before use */
121
  } crt_bits;		/* set before use */
92
 
122
 
93
 
123
 
94
 
124
 
95
static char *crt_dot_t;	/* initialised by init_reader */
125
static char *crt_dot_t;		/* initialised by init_reader */
96
int  crt_lno;		/* initialised to -1 by init_reader */
126
int crt_lno;			/* initialised to -1 by init_reader */
97
int crt_charno;		/* only used if crt_lno != -1. No init needed */
127
int crt_charno;			/* only used if crt_lno != -1. No init needed */
98
char * crt_flnm;	/* only used if crt_lno != -1. No init needed */
128
char *crt_flnm;			/* only used if crt_lno != -1. No init needed */
99
static int  failer_count;	/* initialised by init_reader */
129
static int failer_count;	/* initialised by init_reader */
100
				/* number of failures so far. To allow for
130
				/* number of failures so far. To allow for
101
				   limiting error messages */
131
				   limiting error messages */
102
static int   pkt_index;	/* initialised by init_reader */
132
static int pkt_index;		/* initialised by init_reader */
103
				/* the index of the current packet in the
133
				/* the index of the current packet in the
104
				   file */
134
				   file */
105
static int table_flag;	/* initialised by init_reader */
135
static int table_flag;		/* initialised by init_reader */
106
				/* 1 if reading from memory, 0 if reading
136
				/* 1 if reading from memory, 0 if reading
107
				   from file buffer */
137
				   from file buffer */
108
static char *crt_line;	/* set before use */
138
static char *crt_line;		/* set before use */
109
				/* current line of encoding */
139
				/* current line of encoding */
110
static int   file_pkt;	/* initialised by init_reader */
140
static int file_pkt;		/* initialised by init_reader */
111
				/* holds the index of the packet in the
141
				/* holds the index of the packet in the file */
112
				   file */
-
 
113
static FILE * fpin;	/* initialised by init_reader */
142
static FILE *fpin;		/* initialised by init_reader */
114
				/* file pointer for input */
143
				/* file pointer for input */
115
static int  buff[64];	/* set by read_line */
144
static int buff[64];		/* set by read_line */
116
				/* file buffer for input */
145
				/* file buffer for input */
117
static place current_place;	/* set before use */
146
static place current_place;	/* set before use */
118
static place bytestream_pickup;	/* set before use */
147
static place bytestream_pickup;	/* set before use */
119
				/* records the end of a bytestream */
148
				/* records the end of a bytestream */
120
 
149
 
Line 138... Line 167...
138
   failer prints an error message on the standard output, and sets
167
   failer prints an error message on the standard output, and sets
139
   good_trans to 1 to indicate an error.
168
   good_trans to 1 to indicate an error.
140
 **********************************************************************/
169
 **********************************************************************/
141
 
170
 
142
/* fails, giving error message s */
171
/* fails, giving error message s */
143
void failer
172
void
144
    PROTO_N ( (s) )
-
 
145
    PROTO_T ( char *s )
173
failer(char *s)
146
{
174
{
147
 
175
 
148
  good_trans = 1;
176
  good_trans = 1;
149
  if (crt_lno != -1)
177
  if (crt_lno != -1) {
150
    IGNORE fprintf (stderr, "trans:%s: internal error: after line %d: char %d: %s\n", crt_flnm, crt_lno, crt_charno, s);
178
    IGNORE fprintf(stderr, "trans:%s: internal error: after line %d: char %d: %s\n", crt_flnm, crt_lno, crt_charno, s);
151
  else
179
  } else {
152
    IGNORE fprintf (stderr, "trans:%s: internal error: %s\n", crt_dot_t, s);
180
    IGNORE fprintf(stderr, "trans:%s: internal error: %s\n", crt_dot_t, s);
-
 
181
  }
153
  ++failer_count;
182
  ++failer_count;
154
  if (failer_count >= 15)	/* errors limited to 15 */
183
  /* errors limited to 15 */
-
 
184
  if (failer_count >= 15) {
155
    exit(EXIT_FAILURE);
185
    exit(EXIT_FAILURE);
-
 
186
  }
156
  return;
187
  return;
157
}
188
}
158
 
189
 
159
 
190
 
160
 
191
 
161
/**************************************************************
192
/**************************************************************
162
  read_line reads the next line from the file and
193
  read_line reads the next line from the file and
163
  updates pkt_index, file_pkt and crt_line.
194
  updates pkt_index, file_pkt and crt_line.
164
 **************************************************************/
195
 **************************************************************/
165
 
196
 
166
static void read_line
197
static void
167
    PROTO_N ( (complain) )
-
 
168
    PROTO_T ( int complain )
198
read_line(int complain)
169
{
199
{
170
 
200
 
171
  size_t   test = fread ((char *)buff, sizeof (char), cppkt, fpin);
201
  size_t test = fread((char *)buff, sizeof(char), cppkt, fpin);
172
 
202
 
173
  if (test == (size_t)0 && complain) {
203
  if (test == (size_t)0 && complain) {
174
    failer (READ_PAST_END);
204
    failer(READ_PAST_END);
175
    exit(EXIT_FAILURE);
205
    exit(EXIT_FAILURE);
176
  };
206
  }
177
  pkt_index++;
207
  pkt_index++;
178
  file_pkt++;
208
  file_pkt++;
179
  crt_line = (char *)buff;
209
  crt_line = (char *)buff;
180
}
210
}
181
 
211
 
182
/***************************************************************
212
/***************************************************************
183
  initreader opens the file called n and sets initial values
213
  initreader opens the file called n and sets initial values
184
  into variables.
214
  into variables.
185
 ***************************************************************/
215
 ***************************************************************/
186
void check_magic_no PROTO_S ( ( void ) ) ;
216
void check_magic_no(void);
187
 
217
 
188
bool initreader
218
bool
189
    PROTO_N ( (n) )
-
 
190
    PROTO_T ( char *n )
219
initreader(char *n)
191
{
220
{
192
  crt_dot_t = n;
221
  crt_dot_t = n;
193
  crt_lno = -1;
222
  crt_lno = -1;
194
  failer_count = 0;
223
  failer_count = 0;
195
 
224
 
196
  fpin = fopen (n, "rb");
225
  fpin = fopen(n, "rb");
197
  if (fpin == (FILE *) 0) {
226
  if (fpin == (FILE *)0) {
198
    failer (CANT_OPEN_FILE);
227
    failer(CANT_OPEN_FILE);
199
    return (0);
228
    return(0);
200
  };
229
  }
201
 
230
 
202
  pkt_index = -1;
231
  pkt_index = -1;
203
  file_pkt = -1;
232
  file_pkt = -1;
204
  table_flag = 0;
233
  table_flag = 0;
205
  getcode_bitposn = 0;
234
  getcode_bitposn = 0;
206
  read_line (1);
235
  read_line(1);
207
 
236
 
208
  crt_line = (char *)buff;
237
  crt_line = (char *)buff;
209
  crt_ptr = crt_line;
238
  crt_ptr = crt_line;
210
  end_ptr = crt_line + cppkt;
239
  end_ptr = crt_line + cppkt;
211
  check_magic_no();
240
  check_magic_no();
212
  return (1);
241
  return(1);
213
}
242
}
214
 
243
 
215
void endreader
244
void
216
    PROTO_Z ()
245
endreader(void)
217
{
246
{
218
  int   st = fclose (fpin);
247
  int st = fclose(fpin);
219
  if (st == EOF) {
248
  if (st == EOF) {
220
    failer ("failed to close file");
249
    failer("failed to close file");
221
    exit(EXIT_FAILURE);
250
    exit(EXIT_FAILURE);
222
  };
251
  }
223
}
252
}
224
 
253
 
225
int  getcode
254
int
226
    PROTO_N ( (np) )
-
 
227
    PROTO_T ( int np )
255
getcode(int np)
228
{
256
{
229
  /* np = no of bits to read, np is >= 1 */
257
  /* np = no of bits to read, np is >= 1 */
230
  {
258
  {
231
    unsigned int m;
259
    unsigned int m;
232
    int p;
260
    int p;
233
    {
261
    {
234
      int n = np;
262
      int n = np;
235
      p = getcode_bitposn - n;
263
      p = getcode_bitposn - n;
236
      m = mask[n];
264
      m = mask[n];
237
    };
265
    }
238
    if (p >= 0) {
266
    if (p >= 0) {
239
      getcode_bitposn = p;
267
      getcode_bitposn = p;
240
      return (int)((crt_bits.intc >> p) & m);
268
      return(int)((crt_bits.intc >> p) & m);
241
    };
269
    }
242
  };
270
  }
243
 
271
 
244
  {
272
  {
245
    int p = getcode_bitposn;
273
    int p = getcode_bitposn;
246
    int q;
274
    int q;
247
    unsigned int m;
275
    unsigned int m;
248
    {
276
    {
249
      int n = np - p;
277
      int n = np - p;
250
      m = mask[n];
278
      m = mask[n];
251
      q = (int)((crt_bits.intc & mask[p]) << n);
279
      q = (int)((crt_bits.intc & mask[p]) << n);
252
      p = 32 - n;
280
      p = 32 - n;
253
    };
281
    }
254
 
282
 
255
    if (crt_ptr == end_ptr) {
283
    if (crt_ptr == end_ptr) {
256
      read_line (1);
284
      read_line(1);
257
      crt_ptr = crt_line;
285
      crt_ptr = crt_line;
258
    };
286
    }
259
 
287
 
260
#ifndef FS_LITTLE_ENDIAN
288
#ifndef FS_LITTLE_ENDIAN
261
!!!!!!!!!!!!!  /* define FS_LITTLE_ENDIAN in config.h */
289
!!!!!!!!!!!!!  /* define FS_LITTLE_ENDIAN in config.h */
262
#else
290
#else
263
#if FS_LITTLE_ENDIAN
291
#if FS_LITTLE_ENDIAN
Line 266... Line 294...
266
    crt_bits.chars.b = crt_ptr[2];
294
    crt_bits.chars.b = crt_ptr[2];
267
    crt_bits.chars.a = crt_ptr[3];
295
    crt_bits.chars.a = crt_ptr[3];
268
#else
296
#else
269
    crt_bits.intc = ((unsigned int*)crt_ptr)[0];
297
    crt_bits.intc = ((unsigned int*)crt_ptr)[0];
270
#endif
298
#endif
271
#endif
299
#endif
272
    crt_ptr += 4;
300
    crt_ptr += 4;
273
    getcode_bitposn = p;
301
    getcode_bitposn = p;
274
    return q + (int)((crt_bits.intc >> p) & m);
302
    return q + (int)((crt_bits.intc >> p) & m);
275
  }
303
  }
276
}
304
}
277
 
305
 
278
int get_big_code
306
int
279
    PROTO_N ( (n) )
-
 
280
    PROTO_T ( int n )
307
get_big_code(int n)
281
{
308
{
282
  int t;
309
  int t;
283
  int res = 0;
310
  int res = 0;
284
 
311
 
285
  while (1)
312
  while (1) {
286
   {
-
 
287
     t = getcode(n);
313
     t = getcode(n);
288
     if (t == 0)
314
     if (t == 0) {
289
       res += (int)(mask[n]);
315
       res += (int)(mask[n]);
290
     else
316
     } else {
291
       return (res + t);
317
       return(res + t);
-
 
318
     }
292
   };
319
  }
293
}
320
}
294
 
321
 
295
/********************************************************************
322
/********************************************************************
296
   keep_place records the present state of the getcode variables
323
   keep_place records the present state of the getcode variables
297
   in a place. It condenses the position variables into the
324
   in a place. It condenses the position variables into the
298
   bits_on field, measured from the start of the recorded line.
325
   bits_on field, measured from the start of the recorded line.
299
 ********************************************************************/
326
 ********************************************************************/
300
 
327
 
301
place keep_place
328
place
302
    PROTO_Z ()
329
keep_place(void)
303
{
330
{
304
  place new_pl;
331
  place new_pl;
305
  new_pl.flag = table_flag;
332
  new_pl.flag = table_flag;
306
  if (table_flag)
333
  if (table_flag) {
307
    new_pl.pl_mem = crt_line;
334
    new_pl.pl_mem = crt_line;
-
 
335
  }
308
  new_pl.bits_on = (int)(crt_ptr - crt_line) * 8 - getcode_bitposn;
336
  new_pl.bits_on = (int)(crt_ptr - crt_line) * 8 - getcode_bitposn;
309
  if (!table_flag)
337
  if (!table_flag) {
310
    new_pl.bits_on += pkt_index * bppkt;
338
    new_pl.bits_on += pkt_index * bppkt;
-
 
339
  }
311
  return (new_pl);
340
  return(new_pl);
312
}
341
}
313
 
342
 
314
/********************************************************************
343
/********************************************************************
315
  set_place resets the getcode variables from the place pl, which
344
  set_place resets the getcode variables from the place pl, which
316
  was produced by keep_place or add_place. If necessary it reads more
345
  was produced by keep_place or add_place. If necessary it reads more
317
  lines from the file.
346
  lines from the file.
318
 ********************************************************************/
347
 ********************************************************************/
319
 
348
 
320
 
349
 
321
void set_place
350
void
322
    PROTO_N ( (pl) )
-
 
323
    PROTO_T ( place pl )
351
set_place(place pl)
324
{
352
{
325
  int  new_pi;
353
  int new_pi;
326
  table_flag = pl.flag;
354
  table_flag = pl.flag;
327
  if (!table_flag) {
355
  if (!table_flag) {
328
    new_pi = pl.bits_on / bppkt;
356
    new_pi = pl.bits_on / bppkt;
329
    crt_line = (char *)buff;
357
    crt_line = (char *)buff;
330
    crt_ptr = crt_line + (pl.bits_on / 32) * 4 - new_pi * cppkt;
358
    crt_ptr = crt_line + (pl.bits_on / 32) * 4 - new_pi * cppkt;
331
    getcode_bitposn = 32 - pl.bits_on % 32;
359
    getcode_bitposn = 32 - pl.bits_on % 32;
332
    pkt_index = file_pkt;
360
    pkt_index = file_pkt;
333
    while (pkt_index < new_pi)
361
    while (pkt_index < new_pi)
334
      read_line (0);
362
      read_line(0);
335
  }
-
 
336
  else {
363
  } else {
337
    crt_line = pl.pl_mem;
364
    crt_line = pl.pl_mem;
338
    crt_ptr = crt_line + (pl.bits_on/32)*4;
365
    crt_ptr = crt_line + (pl.bits_on/32) * 4;
339
    getcode_bitposn = 32 - pl.bits_on % 32;
366
    getcode_bitposn = 32 - pl.bits_on % 32;
340
    current_place = pl;
367
    current_place = pl;
341
  };
368
  }
342
  if (getcode_bitposn == 32)
369
  if (getcode_bitposn == 32)
343
    getcode_bitposn = 0;
370
    getcode_bitposn = 0;
344
 
371
 
345
  if (getcode_bitposn > 0) {
372
  if (getcode_bitposn > 0) {
346
#if FS_LITTLE_ENDIAN
373
#if FS_LITTLE_ENDIAN
Line 350... Line 377...
350
    crt_bits.chars.a = crt_ptr[3];
377
    crt_bits.chars.a = crt_ptr[3];
351
#else
378
#else
352
    crt_bits.intc = ((unsigned int*)crt_ptr)[0];
379
    crt_bits.intc = ((unsigned int*)crt_ptr)[0];
353
#endif
380
#endif
354
    crt_ptr += 4;
381
    crt_ptr += 4;
355
  };
382
  }
356
  return;
383
  return;
357
}
384
}
358
 
385
 
359
/********************************************************************
386
/********************************************************************
360
   add_place produces a place n bits on from the place pl.
387
   add_place produces a place n bits on from the place pl.
361
 ********************************************************************/
388
 ********************************************************************/
362
 
389
 
363
 
390
 
364
place add_place
391
place
365
    PROTO_N ( (pl, n) )
-
 
366
    PROTO_T ( place pl X int n )
392
add_place(place pl, int n)
367
{
393
{
368
  place new_pl;
394
  place new_pl;
369
  new_pl.bits_on = pl.bits_on + n;
395
  new_pl.bits_on = pl.bits_on + n;
370
  new_pl.pl_mem = pl.pl_mem;
396
  new_pl.pl_mem = pl.pl_mem;
371
  new_pl.flag = pl.flag;
397
  new_pl.flag = pl.flag;
372
  return (new_pl);
398
  return(new_pl);
373
}
399
}
374
 
400
 
375
/**********************************************************************
401
/**********************************************************************
376
   new_place memorises a line starting from the current position
402
   new_place memorises a line starting from the current position
377
   and going on for bn bits. This may cause more lines to be read
403
   and going on for bn bits. This may cause more lines to be read
378
   from the file.
404
   from the file.
379
 **********************************************************************/
405
 **********************************************************************/
380
void add_capsule_frees
406
void
381
    PROTO_N ( (vp) )
-
 
382
    PROTO_T ( void * vp )
407
add_capsule_frees(void *vp)
383
{
408
{
384
  capsule_frees * cf;
409
  capsule_frees *cf;
385
  cf = (capsule_frees*)xmalloc(sizeof(capsule_frees));
410
  cf = (capsule_frees*)xmalloc(sizeof(capsule_frees));
386
  cf->next = capsule_freelist;
411
  cf->next = capsule_freelist;
387
  cf->ptr = vp;
412
  cf->ptr = vp;
388
  capsule_freelist = cf;
413
  capsule_freelist = cf;
389
  return;
414
  return;
390
}
415
}
391
 
416
 
392
 
417
 
393
place new_place
418
place
394
    PROTO_N ( (bn) )
-
 
395
    PROTO_T ( int bn )
419
new_place(int bn)
396
{
420
{
397
  place pl;
421
  place pl;
398
  int   no_chars,
422
  int   no_chars, i;
399
        i;
-
 
400
  char *mem;
423
  char *mem;
401
  char  c;
424
  char  c;
402
  pl.flag = 1;
425
  pl.flag = 1;
403
 
426
 
404
  if (!table_flag)  {
427
  if (!table_flag) {
405
    pl.bits_on = 32 - getcode_bitposn;
428
    pl.bits_on = 32 - getcode_bitposn;
406
    if (getcode_bitposn == 0)
429
    if (getcode_bitposn == 0)
407
      pl.bits_on = 0;
430
      pl.bits_on = 0;
408
    no_chars = ((pl.bits_on + bn + 31) / 32) * 4;
431
    no_chars = ((pl.bits_on + bn + 31) / 32) * 4;
409
    if (getcode_bitposn > 0) {
432
    if (getcode_bitposn > 0) {
410
      crt_ptr -= 4;
433
      crt_ptr -= 4;
411
    };
434
    }
412
    mem = (char *) xcalloc (no_chars, sizeof (char));
435
    mem = (char *)xcalloc(no_chars, sizeof(char));
413
    for (i = 0; i < no_chars; ++i) {
436
    for (i = 0; i < no_chars; ++i) {
414
      if (crt_ptr == end_ptr) {
437
      if (crt_ptr == end_ptr) {
415
        read_line (1);
438
        read_line(1);
416
        crt_ptr = crt_line;
439
        crt_ptr = crt_line;
417
      };
440
      }
418
      c = *crt_ptr++;
441
      c = *crt_ptr++;
419
      mem[i] = c;
442
      mem[i] = c;
420
    };
443
    }
421
    pl.pl_mem = mem;
444
    pl.pl_mem = mem;
422
    add_capsule_frees((void*)mem);
445
    add_capsule_frees((void*)mem);
423
    return (pl);
446
    return(pl);
424
  };
447
  }
425
 
448
 
426
  pl.bits_on = (int)(crt_ptr - crt_line) * 8 - getcode_bitposn;
449
  pl.bits_on = (int)(crt_ptr - crt_line)* 8 - getcode_bitposn;
427
  pl.pl_mem = current_place.pl_mem;
450
  pl.pl_mem = current_place.pl_mem;
428
  return pl;
451
  return pl;
429
}
452
}
430
 
453
 
431
/*********************************************************************
454
/*********************************************************************
Line 433... Line 456...
433
  encoded as a number of octal digits, most significant first.
456
  encoded as a number of octal digits, most significant first.
434
  These octal digits are encoded in 4-bit chunks with 8 added on
457
  These octal digits are encoded in 4-bit chunks with 8 added on
435
  to the last digit only.
458
  to the last digit only.
436
 *********************************************************************/
459
 *********************************************************************/
437
 
460
 
438
int small_dtdfint
461
int
439
    PROTO_Z ()
462
small_dtdfint(void)
440
{
463
{
441
  int  digit;
464
  int digit;
442
  int total = 0;
465
  int total = 0;
443
  while (digit = getcode (4), digit < 8)
466
  while (digit = getcode(4), digit < 8) {
444
    total = 8 * total + digit;
467
    total = 8 * total + digit;
-
 
468
  }
445
  return (8 * total + (digit - 8));
469
  return(8 * total + (digit - 8));
446
}
470
}
-
 
471
 
-
 
472
 /* step the input stream on to the next byte boundary */
447
 
473
 
448
 /* step the input stream on to the next byte boundary */
-
 
449
 
474
void
450
void to_boundary
475
to_boundary(void)
451
    PROTO_Z ()
-
 
452
{
476
{
453
  getcode_bitposn = getcode_bitposn - getcode_bitposn % 8;
477
  getcode_bitposn = getcode_bitposn - getcode_bitposn % 8;
454
  return;
478
  return;
455
}
479
}
456
 
480
 
457
 
481
 
458
 /* delivers a new place for the bitstream in the input stream and steps
482
 /* delivers a new place for the bitstream in the input stream and steps
459
    over it */
483
    over it */
460
 
484
 
461
bitstream d_bitstream
485
bitstream
462
    PROTO_Z ()
486
d_bitstream(void)
463
{
487
{
464
  bitstream crt_bitstream;
488
  bitstream crt_bitstream;
465
  place here;
489
  place here;
466
  int  length;
490
  int length;
467
  length = small_dtdfint ();
491
  length = small_dtdfint();
468
  here = keep_place ();
492
  here = keep_place();
469
  crt_bitstream = new_place (length);
493
  crt_bitstream = new_place(length);
470
  set_place (add_place (here, length));
494
  set_place(add_place(here, length));
471
  return crt_bitstream;
495
  return crt_bitstream;
472
}
496
}
473
 
497
 
474
 
498
 
475
 
499
 
476
 
500
 
477
bytestream d_bytestream
501
bytestream
478
    PROTO_Z ()
502
d_bytestream(void)
479
{
503
{
480
  return bytestream_pickup;
504
  return bytestream_pickup;
481
}
505
}
482
 
506
 
-
 
507
void
483
void ignore_bytestream
508
ignore_bytestream(void)
484
    PROTO_Z ()
-
 
485
{
509
{
486
  /* steps over a bytestream */
510
  /* steps over a bytestream */
487
  int  length;
511
  int length;
488
  place here;
512
  place here;
489
  length = small_dtdfint ();
513
  length = small_dtdfint();
490
  to_boundary ();
514
  to_boundary();
491
  here = keep_place ();
515
  here = keep_place();
492
  set_place (add_place (here, (length * 8)));
516
  set_place(add_place(here,(length * 8)));
493
  return;
517
  return;
494
}
518
}
495
 
519
 
496
 /* records in bytestream_pickup the end of a bytestream */
520
 /* records in bytestream_pickup the end of a bytestream */
497
 
521
 
-
 
522
void
498
void start_bytestream
523
start_bytestream(void)
499
    PROTO_Z ()
-
 
500
{
524
{
501
  int  length;
525
  int length;
502
  place here;
526
  place here;
503
  length = small_dtdfint ();
527
  length = small_dtdfint();
504
  to_boundary ();
528
  to_boundary();
505
  here = keep_place ();
529
  here = keep_place();
506
  bytestream_pickup = add_place (here, (length * 8));
530
  bytestream_pickup = add_place(here,(length * 8));
507
  return;
531
  return;
508
}
532
}
509
 
533
 
510
 
534
 
511
 /* resets the input stream from bytestream_pickup */
535
 /* resets the input stream from bytestream_pickup */
512
 
536
 
513
void end_bytestream
537
void
514
    PROTO_Z ()
538
end_bytestream(void)
515
{
539
{
516
  set_place (bytestream_pickup);
540
  set_place(bytestream_pickup);
517
  return;
541
  return;
518
}
542
}
519
 
543
 
520
tdfstring d_tdfstring
544
tdfstring
521
    PROTO_Z ()
545
d_tdfstring(void)
522
{
546
{
523
  /* reads a tdfstring from the input stream */
547
  /* reads a tdfstring from the input stream */
524
  int  bits = small_dtdfint ();
548
  int bits = small_dtdfint();
525
  int  n = small_dtdfint ();
549
  int n = small_dtdfint();
526
  tdfstring tdb;
550
  tdfstring tdb;
527
  int  i;
551
  int i;
528
  tdb.number = n;
552
  tdb.number = n;
529
  if (bits <= 8) {
553
  if (bits <= 8) {
530
    tdb.ints.chars = (char *) xcalloc (n + 1, sizeof (char));
554
    tdb.ints.chars = (char *)xcalloc(n + 1, sizeof(char));
531
    for (i = 0; i < n; ++i)
555
    for (i = 0; i < n; ++i) {
532
      tdb.ints.chars[i] = (char)getcode (bits);
556
      tdb.ints.chars[i] = (char)getcode(bits);
-
 
557
    }
533
    tdb.ints.chars[n] = 0;
558
    tdb.ints.chars[n] = 0;
534
    tdb.size = 8;
559
    tdb.size = 8;
535
    return tdb;
560
    return tdb;
536
  };
561
  }
537
  if (bits <= 16) {
562
  if (bits <= 16) {
538
    tdb.ints.shorts = (short *) xcalloc (n + 1, sizeof (short));
563
    tdb.ints.shorts = (short *)xcalloc(n + 1, sizeof(short));
539
    for (i = 0; i < n; ++i)
564
    for (i = 0; i < n; ++i) {
540
      tdb.ints.shorts[i] = (short)getcode (bits);
565
      tdb.ints.shorts[i] = (short)getcode(bits);
-
 
566
    }
541
    tdb.ints.shorts[n] = 0;
567
    tdb.ints.shorts[n] = 0;
542
    tdb.size = 16;
568
    tdb.size = 16;
543
    return tdb;
569
    return tdb;
544
  };
570
  }
545
  if (bits <= 32) {
571
  if (bits <= 32) {
546
    tdb.ints.longs = (int *) xcalloc (n + 1, sizeof (int));
572
    tdb.ints.longs = (int *)xcalloc(n + 1, sizeof(int));
547
    for (i = 0; i < n; ++i)
573
    for (i = 0; i < n; ++i) {
548
      tdb.ints.longs[i] = getcode (bits);
574
      tdb.ints.longs[i] = getcode(bits);
-
 
575
    }
549
    tdb.ints.longs[n] = 0;
576
    tdb.ints.longs[n] = 0;
550
    tdb.size = 32;
577
    tdb.size = 32;
551
    return tdb;
578
    return tdb;
552
  };
579
  }
553
  if (bits <= 64) {
580
  if (bits <= 64) {
554
    tdb.ints.longs = (int *) xcalloc (n + 1, sizeof (int));
581
    tdb.ints.longs = (int *)xcalloc(n + 1, sizeof(int));
555
    for (i = 0; i < n; ++i) {
582
    for (i = 0; i < n; ++i) {
556
      flt64 x;
583
      flt64 x;
557
      flpt f;
584
      flpt f;
558
      x.big = getcode(bits - 32);
585
      x.big = getcode(bits - 32);
559
      x.small = (unsigned int)getcode(32);
586
      x.small = (unsigned int)getcode(32);
560
      f = f64_to_flt(x, 0);
587
      f = f64_to_flt(x, 0);
561
      tdb.ints.longs[i] = f;
588
      tdb.ints.longs[i] = f;
562
    };
589
    }
563
    tdb.ints.longs[n] = 0;
590
    tdb.ints.longs[n] = 0;
564
    tdb.size = 64;
591
    tdb.size = 64;
565
    return tdb;
592
    return tdb;
566
  };
593
  }
567
  failer(NO_BIG_STRINGS);
594
  failer(NO_BIG_STRINGS);
568
  return tdb;
595
  return tdb;
569
}
596
}
570
 
597
 
571
tdfstring d_tdfident
598
tdfstring
572
    PROTO_Z ()
599
d_tdfident(void)
573
{
600
{
574
  /* reads a tdfident from the input stream */
601
  /* reads a tdfident from the input stream */
575
  int  bits = small_dtdfint ();
602
  int bits = small_dtdfint();
576
  int  n = small_dtdfint ();
603
  int n = small_dtdfint();
577
  tdfstring tdb;
604
  tdfstring tdb;
578
  int  i;
605
  int i;
579
  tdb.size = bits;
606
  tdb.size = bits;
580
  tdb.number = n;
607
  tdb.number = n;
581
  if (bits <= 8) {
608
  if (bits <= 8) {
582
    tdb.ints.chars = (char *) xcalloc (n + 1, sizeof (char));
609
    tdb.ints.chars = (char *)xcalloc(n + 1, sizeof(char));
583
    to_boundary ();
610
    to_boundary();
584
    for (i = 0; i < n; ++i)
611
    for (i = 0; i < n; ++i) {
585
      tdb.ints.chars[i] = (char)getcode (bits);
612
      tdb.ints.chars[i] = (char)getcode(bits);
-
 
613
    }
586
    tdb.ints.chars[n] = 0;
614
    tdb.ints.chars[n] = 0;
587
    to_boundary ();
615
    to_boundary();
588
    return tdb;
616
    return tdb;
589
  };
617
  }
590
  if (bits <= 16) {
618
  if (bits <= 16) {
591
    tdb.ints.shorts = (short *) xcalloc (n + 1, sizeof (short));
619
    tdb.ints.shorts = (short *)xcalloc(n + 1, sizeof(short));
592
    to_boundary ();
620
    to_boundary();
593
    for (i = 0; i < n; ++i)
621
    for (i = 0; i < n; ++i) {
594
      tdb.ints.shorts[i] = (short)getcode (bits);
622
      tdb.ints.shorts[i] = (short)getcode(bits);
-
 
623
    }
595
    tdb.ints.shorts[n] = 0;
624
    tdb.ints.shorts[n] = 0;
596
    to_boundary ();
625
    to_boundary();
597
    return tdb;
626
    return tdb;
598
  };
627
  }
599
  tdb.ints.longs = (int *) xcalloc (n + 1, sizeof (int));
628
  tdb.ints.longs = (int *)xcalloc(n + 1, sizeof(int));
600
  to_boundary ();
629
  to_boundary();
601
  for (i = 0; i < n; ++i)
630
  for (i = 0; i < n; ++i) {
602
    tdb.ints.longs[i] = getcode (bits);
631
    tdb.ints.longs[i] = getcode(bits);
-
 
632
  }
603
  tdb.ints.longs[n] = 0;
633
  tdb.ints.longs[n] = 0;
604
  to_boundary ();
634
  to_boundary();
605
  return tdb;
635
  return tdb;
606
}
636
}
607
 
637
 
608
tdfbool d_tdfbool
638
tdfbool
609
    PROTO_Z ()
639
d_tdfbool(void)
610
{
640
{
611
  /* reads a tdfbool from the input stream */
641
  /* reads a tdfbool from the input stream */
612
  return (tdfbool)getcode (1);
642
  return (tdfbool)getcode(1);
613
}
643
}
614
 
644
 
615
 
645
 
616
 
646
 
617
tdfint d_tdfint
647
tdfint
618
    PROTO_Z ()
648
d_tdfint(void)
619
{
649
{
620
  /* reads a tdfint from the input stream */
650
  /* reads a tdfint from the input stream */
621
  nat n;
651
  nat n;
622
  unsigned int  digit;
652
  unsigned int  digit;
623
  unsigned int total = 0;
653
  unsigned int total = 0;
624
  int  small = 1;
654
  int small = 1;
625
  int goon = 1;
655
  int goon = 1;
626
  flpt f;
656
  flpt f;
627
 
657
 
628
  while (goon)
658
  while (goon)
629
   {
659
   {
630
     digit = (unsigned int)getcode(4);
660
     digit = (unsigned int)getcode(4);
631
     if (digit >= 8)
661
     if (digit >= 8) {
632
       { goon = 0;
662
	 goon = 0;
633
         digit -= 8;
663
	 digit -= 8;
634
       };
664
     }
635
     if (small)
665
     if (small) {
636
       { if (total > 0x1fffffff)
666
	 if (total > 0x1fffffff) {
637
           { small = 0;
667
	   small = 0;
638
	     f = floatrep_unsigned(total);
668
	   f = floatrep_unsigned(total);
639
             flpt_newdig(digit, &flptnos[f], 8);
669
	   flpt_newdig(digit, &flptnos[f], 8);
640
           }
-
 
641
         else
670
	 } else {
642
           total = (total << 3) + digit;
671
	   total = (total << 3) + digit;
643
       }
672
	 }
644
     else {
673
     } else {
645
       SET(f);
674
       SET(f);
646
       flpt_newdig(digit, &flptnos[f], 8);
675
       flpt_newdig(digit, &flptnos[f], 8);
647
     };
676
     }
648
   };
677
   }
649
  nat_issmall(n) = (bool)small;
678
  nat_issmall(n) = (bool)small;
650
  if (small)
679
  if (small) {
651
    natint(n) = (int)total;
680
    natint(n) = (int)total;
652
  else
681
  } else {
653
   {
-
 
654
     SET(f);
682
    SET(f);
655
     nat_issmall(n) = 0;
683
    nat_issmall(n) = 0;
656
     natbig(n) = f;
684
    natbig(n) = f;
657
   };
685
   }
658
  return n;
686
  return n;
659
}
687
}
660
 
688
 
661
void check_magic_no
689
void
662
    PROTO_Z ()
690
check_magic_no(void)
663
{
691
{
664
	tdfint maj;
692
	tdfint maj;
665
	tdfint min;
693
	tdfint min;
666
 
694
 
667
	if (getcode(8) != 'T' || getcode(8) != 'D' || getcode(8) != 'F' ||
695
	if (getcode(8) != 'T' || getcode(8) != 'D' || getcode(8) != 'F' ||
668
		getcode(8) != 'C') {
696
	    getcode(8) != 'C') {
669
		failer("This is not a TDF Version >= 4 capsule");
697
		failer("This is not a TDF Version >= 4 capsule");
670
		exit(EXIT_FAILURE);
698
		exit(EXIT_FAILURE);
671
	}
699
	}
672
	maj = d_tdfint();
700
	maj = d_tdfint();
673
	if (natint(maj) > MAJOR_VERSION) {
701
	if (natint(maj) > MAJOR_VERSION) {