| 1 |
90 |
jeremybenn |
/* mycompress.c. Test of Or1ksim compression program
|
| 2 |
|
|
|
| 3 |
|
|
Copyright (C) 1999-2006 OpenCores
|
| 4 |
|
|
Copyright (C) 2010 Embecosm Limited
|
| 5 |
|
|
|
| 6 |
|
|
Contributors various OpenCores participants
|
| 7 |
|
|
Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
|
| 8 |
|
|
|
| 9 |
|
|
This file is part of OpenRISC 1000 Architectural Simulator.
|
| 10 |
|
|
|
| 11 |
|
|
This program is free software; you can redistribute it and/or modify it
|
| 12 |
|
|
under the terms of the GNU General Public License as published by the Free
|
| 13 |
|
|
Software Foundation; either version 3 of the License, or (at your option)
|
| 14 |
|
|
any later version.
|
| 15 |
|
|
|
| 16 |
|
|
This program is distributed in the hope that it will be useful, but WITHOUT
|
| 17 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
| 18 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
| 19 |
|
|
more details.
|
| 20 |
|
|
|
| 21 |
|
|
You should have received a copy of the GNU General Public License along
|
| 22 |
|
|
with this program. If not, see <http: www.gnu.org/licenses/>. */
|
| 23 |
|
|
|
| 24 |
|
|
/* ----------------------------------------------------------------------------
|
| 25 |
|
|
This code is commented throughout for use with Doxygen.
|
| 26 |
|
|
--------------------------------------------------------------------------*/
|
| 27 |
|
|
|
| 28 |
|
|
#include <stdarg.h>
|
| 29 |
|
|
#include "support.h"
|
| 30 |
|
|
|
| 31 |
|
|
#define BYTES_TO_COMPRESS 1000
|
| 32 |
|
|
|
| 33 |
|
|
/*
|
| 34 |
|
|
* Compress - data compression program
|
| 35 |
|
|
*/
|
| 36 |
|
|
#define min(a,b) ((a>b) ? b : a)
|
| 37 |
|
|
|
| 38 |
|
|
/*
|
| 39 |
|
|
* machine variants which require cc -Dmachine: pdp11, z8000, pcxt
|
| 40 |
|
|
*/
|
| 41 |
|
|
|
| 42 |
|
|
/*
|
| 43 |
|
|
* Set USERMEM to the maximum amount of physical user memory available
|
| 44 |
|
|
* in bytes. USERMEM is used to determine the maximum BITS that can be used
|
| 45 |
|
|
* for compression.
|
| 46 |
|
|
*
|
| 47 |
|
|
* SACREDMEM is the amount of physical memory saved for others; compress
|
| 48 |
|
|
* will hog the rest.
|
| 49 |
|
|
*/
|
| 50 |
|
|
#ifndef SACREDMEM
|
| 51 |
|
|
#define SACREDMEM 0
|
| 52 |
|
|
#endif
|
| 53 |
|
|
|
| 54 |
|
|
/* #ifndef USERMEM */
|
| 55 |
|
|
#define USERMEM 60000 /* default user memory */
|
| 56 |
|
|
/* #endif */
|
| 57 |
|
|
|
| 58 |
|
|
#ifdef interdata /* (Perkin-Elmer) */
|
| 59 |
|
|
#define SIGNED_COMPARE_SLOW /* signed compare is slower than unsigned */
|
| 60 |
|
|
#endif
|
| 61 |
|
|
|
| 62 |
|
|
#ifdef USERMEM
|
| 63 |
|
|
# if USERMEM >= (433484+SACREDMEM)
|
| 64 |
|
|
# define PBITS 16
|
| 65 |
|
|
# else
|
| 66 |
|
|
# if USERMEM >= (229600+SACREDMEM)
|
| 67 |
|
|
# define PBITS 15
|
| 68 |
|
|
# else
|
| 69 |
|
|
# if USERMEM >= (127536+SACREDMEM)
|
| 70 |
|
|
# define PBITS 14
|
| 71 |
|
|
# else
|
| 72 |
|
|
# if USERMEM >= (73464+SACREDMEM)
|
| 73 |
|
|
# define PBITS 13
|
| 74 |
|
|
# else
|
| 75 |
|
|
# define PBITS 12
|
| 76 |
|
|
# endif
|
| 77 |
|
|
# endif
|
| 78 |
|
|
# endif
|
| 79 |
|
|
# endif
|
| 80 |
|
|
# undef USERMEM
|
| 81 |
|
|
#endif /* USERMEM */
|
| 82 |
|
|
|
| 83 |
|
|
#ifdef PBITS /* Preferred BITS for this memory size */
|
| 84 |
|
|
# ifndef BITS
|
| 85 |
|
|
# define BITS PBITS
|
| 86 |
|
|
# endif /* BITS */
|
| 87 |
|
|
#endif /* PBITS */
|
| 88 |
|
|
|
| 89 |
|
|
#if BITS == 16
|
| 90 |
|
|
# define HSIZE 69001 /* 95% occupancy */
|
| 91 |
|
|
#endif
|
| 92 |
|
|
#if BITS == 15
|
| 93 |
|
|
# define HSIZE 35023 /* 94% occupancy */
|
| 94 |
|
|
#endif
|
| 95 |
|
|
#if BITS == 14
|
| 96 |
|
|
# define HSIZE 18013 /* 91% occupancy */
|
| 97 |
|
|
#endif
|
| 98 |
|
|
#if BITS == 13
|
| 99 |
|
|
# define HSIZE 9001 /* 91% occupancy */
|
| 100 |
|
|
#endif
|
| 101 |
|
|
#if BITS <= 12
|
| 102 |
|
|
# define HSIZE 5003 /* 80% occupancy */
|
| 103 |
|
|
#endif
|
| 104 |
|
|
|
| 105 |
|
|
/*
|
| 106 |
|
|
* a code_int must be able to hold 2**BITS values of type int, and also -1
|
| 107 |
|
|
*/
|
| 108 |
|
|
#if BITS > 15
|
| 109 |
|
|
typedef long int code_int;
|
| 110 |
|
|
#else
|
| 111 |
|
|
typedef int code_int;
|
| 112 |
|
|
#endif
|
| 113 |
|
|
|
| 114 |
|
|
#ifdef SIGNED_COMPARE_SLOW
|
| 115 |
|
|
typedef unsigned long int count_int;
|
| 116 |
|
|
typedef unsigned short int count_short;
|
| 117 |
|
|
#else
|
| 118 |
|
|
typedef long int count_int;
|
| 119 |
|
|
#endif
|
| 120 |
|
|
|
| 121 |
|
|
#ifdef NO_UCHAR
|
| 122 |
|
|
typedef char char_type;
|
| 123 |
|
|
#else
|
| 124 |
|
|
typedef unsigned char char_type;
|
| 125 |
|
|
#endif /* UCHAR */
|
| 126 |
|
|
char_type magic_header[] = { "\037\235" }; /* 1F 9D */
|
| 127 |
|
|
|
| 128 |
|
|
/* Defines for third byte of header */
|
| 129 |
|
|
#define BIT_MASK 0x1f
|
| 130 |
|
|
#define BLOCK_MASK 0x80
|
| 131 |
|
|
/* Masks 0x40 and 0x20 are free. I think 0x20 should mean that there is
|
| 132 |
|
|
a fourth header byte (for expansion).
|
| 133 |
|
|
*/
|
| 134 |
|
|
#define INIT_BITS 9 /* initial number of bits/code */
|
| 135 |
|
|
|
| 136 |
|
|
/*
|
| 137 |
|
|
* compress.c - File compression ala IEEE Computer, June 1984.
|
| 138 |
|
|
*
|
| 139 |
|
|
* Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas)
|
| 140 |
|
|
* Jim McKie (decvax!mcvax!jim)
|
| 141 |
|
|
* Steve Davies (decvax!vax135!petsd!peora!srd)
|
| 142 |
|
|
* Ken Turkowski (decvax!decwrl!turtlevax!ken)
|
| 143 |
|
|
* James A. Woods (decvax!ihnp4!ames!jaw)
|
| 144 |
|
|
* Joe Orost (decvax!vax135!petsd!joe)
|
| 145 |
|
|
*
|
| 146 |
|
|
*/
|
| 147 |
|
|
|
| 148 |
|
|
#if i386
|
| 149 |
|
|
#include <stdio.h>
|
| 150 |
|
|
#include <stdio.h>
|
| 151 |
|
|
#include <ctype.h>
|
| 152 |
|
|
#include <signal.h>
|
| 153 |
|
|
#include <sys/types.h>
|
| 154 |
|
|
#include <sys/stat.h>
|
| 155 |
|
|
#endif
|
| 156 |
|
|
|
| 157 |
|
|
#define ARGVAL() (*++(*argv) || (--argc && *++argv))
|
| 158 |
|
|
|
| 159 |
|
|
int n_bits; /* number of bits/code */
|
| 160 |
|
|
int maxbits = BITS; /* user settable max # bits/code */
|
| 161 |
|
|
code_int maxcode; /* maximum code, given n_bits */
|
| 162 |
|
|
code_int maxmaxcode = 1L << BITS; /* should NEVER generate this code */
|
| 163 |
|
|
#ifdef COMPATIBLE /* But wrong! */
|
| 164 |
|
|
# define MAXCODE(n_bits) (1L << (n_bits) - 1)
|
| 165 |
|
|
#else
|
| 166 |
|
|
# define MAXCODE(n_bits) ((1L << (n_bits)) - 1)
|
| 167 |
|
|
#endif /* COMPATIBLE */
|
| 168 |
|
|
|
| 169 |
|
|
# ifdef sel
|
| 170 |
|
|
/* support gould base register problems */
|
| 171 |
|
|
/*NOBASE*/
|
| 172 |
|
|
count_int htab [HSIZE];
|
| 173 |
|
|
unsigned short codetab [HSIZE];
|
| 174 |
|
|
/*NOBASE*/
|
| 175 |
|
|
# else /* !gould */
|
| 176 |
|
|
count_int htab [HSIZE];
|
| 177 |
|
|
unsigned short codetab [HSIZE];
|
| 178 |
|
|
# endif /* !gould */
|
| 179 |
|
|
#define htabof(i) htab[i]
|
| 180 |
|
|
#define codetabof(i) codetab[i]
|
| 181 |
|
|
code_int hsize = HSIZE; /* for dynamic table sizing */
|
| 182 |
|
|
count_int fsize;
|
| 183 |
|
|
|
| 184 |
|
|
/*
|
| 185 |
|
|
* To save much memory, we overlay the table used by compress() with those
|
| 186 |
|
|
* used by decompress(). The tab_prefix table is the same size and type
|
| 187 |
|
|
* as the codetab. The tab_suffix table needs 2**BITS characters. We
|
| 188 |
|
|
* get this from the beginning of htab. The output stack uses the rest
|
| 189 |
|
|
* of htab, and contains characters. There is plenty of room for any
|
| 190 |
|
|
* possible stack (stack used to be 8000 characters).
|
| 191 |
|
|
*/
|
| 192 |
|
|
|
| 193 |
|
|
#define tab_prefixof(i) codetabof(i)
|
| 194 |
|
|
#ifdef XENIX_16
|
| 195 |
|
|
# define tab_suffixof(i) ((char_type *)htab[(i)>>15])[(i) & 0x7fff]
|
| 196 |
|
|
# define de_stack ((char_type *)(htab2))
|
| 197 |
|
|
#else /* Normal machine */
|
| 198 |
|
|
# define tab_suffixof(i) ((char_type *)(htab))[i]
|
| 199 |
|
|
# define de_stack ((char_type *)&tab_suffixof(1<<BITS))
|
| 200 |
|
|
#endif /* XENIX_16 */
|
| 201 |
|
|
|
| 202 |
|
|
code_int free_ent = 0; /* first unused entry */
|
| 203 |
|
|
int exit_stat = 0;
|
| 204 |
|
|
|
| 205 |
|
|
code_int getcode();
|
| 206 |
|
|
|
| 207 |
|
|
int nomagic = 0; /* Use a 3-byte magic number header, unless old file */
|
| 208 |
|
|
int zcat_flg = 0; /* Write output on stdout, suppress messages */
|
| 209 |
|
|
int quiet = 1; /* don't tell me about compression */
|
| 210 |
|
|
|
| 211 |
|
|
/*
|
| 212 |
|
|
* block compression parameters -- after all codes are used up,
|
| 213 |
|
|
* and compression rate changes, start over.
|
| 214 |
|
|
*/
|
| 215 |
|
|
int block_compress = BLOCK_MASK;
|
| 216 |
|
|
int clear_flg = 0;
|
| 217 |
|
|
long int ratio = 0;
|
| 218 |
|
|
#define CHECK_GAP 10000 /* ratio check interval */
|
| 219 |
|
|
count_int checkpoint = CHECK_GAP;
|
| 220 |
|
|
/*
|
| 221 |
|
|
* the next two codes should not be changed lightly, as they must not
|
| 222 |
|
|
* lie within the contiguous general code space.
|
| 223 |
|
|
*/
|
| 224 |
|
|
#define FIRST 257 /* first free entry */
|
| 225 |
|
|
#define CLEAR 256 /* table clear output code */
|
| 226 |
|
|
|
| 227 |
|
|
int force = 0;
|
| 228 |
|
|
char ofname [100];
|
| 229 |
|
|
int (*bgnd_flag)();
|
| 230 |
|
|
|
| 231 |
|
|
/* reset code table */
|
| 232 |
|
|
void cl_hash(register count_int hsize);
|
| 233 |
|
|
/* table clear for block compress */
|
| 234 |
|
|
void cl_block ();
|
| 235 |
|
|
void output(code_int code);
|
| 236 |
|
|
|
| 237 |
|
|
|
| 238 |
|
|
int do_decomp = 0;
|
| 239 |
|
|
|
| 240 |
|
|
static int offset;
|
| 241 |
|
|
long int in_count = 1; /* length of input */
|
| 242 |
|
|
long int bytes_out; /* length of compressed output */
|
| 243 |
|
|
long int out_count = 0; /* # of codes output (for debugging) */
|
| 244 |
|
|
|
| 245 |
|
|
/*
|
| 246 |
|
|
* compress stdin to stdout
|
| 247 |
|
|
*
|
| 248 |
|
|
* Algorithm: use open addressing double hashing (no chaining) on the
|
| 249 |
|
|
* prefix code / next character combination. We do a variant of Knuth's
|
| 250 |
|
|
* algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
|
| 251 |
|
|
* secondary probe. Here, the modular division first probe is gives way
|
| 252 |
|
|
* to a faster exclusive-or manipulation. Also do block compression with
|
| 253 |
|
|
* an adaptive reset, whereby the code table is cleared when the compression
|
| 254 |
|
|
* ratio decreases, but after the table fills. The variable-length output
|
| 255 |
|
|
* codes are re-sized at this point, and a special CLEAR code is generated
|
| 256 |
|
|
* for the decompressor. Late addition: construct the table according to
|
| 257 |
|
|
* file size for noticeable speed improvement on small files. Please direct
|
| 258 |
|
|
* questions about this implementation to ames!jaw.
|
| 259 |
|
|
*/
|
| 260 |
|
|
|
| 261 |
|
|
int main() {
|
| 262 |
|
|
register long fcode;
|
| 263 |
|
|
register code_int i = 0;
|
| 264 |
|
|
register int c;
|
| 265 |
|
|
register code_int ent;
|
| 266 |
|
|
#ifdef XENIX_16
|
| 267 |
|
|
register code_int disp;
|
| 268 |
|
|
#else /* Normal machine */
|
| 269 |
|
|
register int disp;
|
| 270 |
|
|
#endif
|
| 271 |
|
|
register code_int hsize_reg;
|
| 272 |
|
|
register int hshift;
|
| 273 |
|
|
|
| 274 |
|
|
#ifndef COMPATIBLE
|
| 275 |
|
|
if (nomagic == 0) {
|
| 276 |
|
|
/* putchar(magic_header[0]); putchar(magic_header[1]);
|
| 277 |
|
|
putchar((char)(maxbits | block_compress)); */
|
| 278 |
|
|
}
|
| 279 |
|
|
#endif /* COMPATIBLE */
|
| 280 |
|
|
|
| 281 |
|
|
offset = 0;
|
| 282 |
|
|
bytes_out = 3; /* includes 3-byte header mojo */
|
| 283 |
|
|
out_count = 0;
|
| 284 |
|
|
clear_flg = 0;
|
| 285 |
|
|
ratio = 0;
|
| 286 |
|
|
in_count = 1;
|
| 287 |
|
|
|
| 288 |
|
|
printf("main: bytes_out %d... hsize %d\n", (int)bytes_out, (int)hsize);
|
| 289 |
|
|
|
| 290 |
|
|
checkpoint = CHECK_GAP;
|
| 291 |
|
|
maxcode = MAXCODE(n_bits = INIT_BITS);
|
| 292 |
|
|
free_ent = ((block_compress) ? FIRST : 256 );
|
| 293 |
|
|
|
| 294 |
|
|
|
| 295 |
|
|
ent = '\0'; /* getchar (); */
|
| 296 |
|
|
|
| 297 |
|
|
hshift = 0;
|
| 298 |
|
|
for ( fcode = (long) hsize; fcode < 65536L; fcode *= 2L )
|
| 299 |
|
|
hshift++;
|
| 300 |
|
|
hshift = 8 - hshift; /* set hash code range bound */
|
| 301 |
|
|
printf("main: hshift %d...\n", hshift);
|
| 302 |
|
|
|
| 303 |
|
|
hsize_reg = hsize;
|
| 304 |
|
|
cl_hash( (count_int) hsize_reg); /* clear hash table */
|
| 305 |
|
|
|
| 306 |
|
|
/*#ifdef SIGNED_COMPARE_SLOW
|
| 307 |
|
|
while ( (c = getchar()) != (unsigned) EOF ) {
|
| 308 |
|
|
#else
|
| 309 |
|
|
while ( (c = getchar()) != EOF ) {
|
| 310 |
|
|
#endif*/
|
| 311 |
|
|
printf("main: bytes_out %d...\n", (int)bytes_out);
|
| 312 |
|
|
printf("main: hsize_reg %d...\n", (int)hsize_reg);
|
| 313 |
|
|
printf("main: before compress %d...\n", (int)in_count);
|
| 314 |
|
|
while (in_count < BYTES_TO_COMPRESS) {
|
| 315 |
|
|
c = in_count % 255;
|
| 316 |
|
|
|
| 317 |
|
|
printf("main: compressing %d...\n", (int)in_count);
|
| 318 |
|
|
in_count++;
|
| 319 |
|
|
fcode = (long) (((long) c << maxbits) + ent);
|
| 320 |
|
|
i = (((long)c << hshift) ^ ent); /* xor hashing */
|
| 321 |
|
|
|
| 322 |
|
|
if ( htabof (i) == fcode ) {
|
| 323 |
|
|
ent = codetabof (i);
|
| 324 |
|
|
continue;
|
| 325 |
|
|
} else if ( (long)htabof (i) < 0 ) /* empty slot */
|
| 326 |
|
|
goto nomatch;
|
| 327 |
|
|
|
| 328 |
|
|
disp = hsize_reg - i; /* secondary hash (after G. Knott) */
|
| 329 |
|
|
if ( i == 0 )
|
| 330 |
|
|
disp = 1;
|
| 331 |
|
|
probe:
|
| 332 |
|
|
if ( (i -= disp) < 0 )
|
| 333 |
|
|
i += hsize_reg;
|
| 334 |
|
|
|
| 335 |
|
|
if ( htabof (i) == fcode ) {
|
| 336 |
|
|
ent = codetabof (i);
|
| 337 |
|
|
continue;
|
| 338 |
|
|
}
|
| 339 |
|
|
if ( (long)htabof (i) > 0 )
|
| 340 |
|
|
goto probe;
|
| 341 |
|
|
nomatch:
|
| 342 |
|
|
output ( (code_int) ent );
|
| 343 |
|
|
out_count++;
|
| 344 |
|
|
ent = c;
|
| 345 |
|
|
#ifdef SIGNED_COMPARE_SLOW
|
| 346 |
|
|
if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
|
| 347 |
|
|
#else
|
| 348 |
|
|
if ( free_ent < maxmaxcode ) {
|
| 349 |
|
|
#endif
|
| 350 |
|
|
codetabof (i) = free_ent++; /* code -> hashtable */
|
| 351 |
|
|
htabof (i) = fcode;
|
| 352 |
|
|
}
|
| 353 |
|
|
else if ( (count_int)in_count >= checkpoint && block_compress )
|
| 354 |
|
|
cl_block ();
|
| 355 |
|
|
}
|
| 356 |
|
|
/*
|
| 357 |
|
|
* Put out the final code.
|
| 358 |
|
|
*/
|
| 359 |
|
|
printf("main: output...\n");
|
| 360 |
|
|
output( (code_int)ent );
|
| 361 |
|
|
out_count++;
|
| 362 |
|
|
output( (code_int)-1 );
|
| 363 |
|
|
|
| 364 |
|
|
if(bytes_out > in_count) /* exit(2) if no savings */
|
| 365 |
|
|
exit_stat = 2;
|
| 366 |
|
|
printf("main: end...\n");
|
| 367 |
|
|
report (0xdeaddead);
|
| 368 |
|
|
return 0;
|
| 369 |
|
|
}
|
| 370 |
|
|
|
| 371 |
|
|
/*****************************************************************
|
| 372 |
|
|
* TAG( output )
|
| 373 |
|
|
*
|
| 374 |
|
|
* Output the given code.
|
| 375 |
|
|
* Inputs:
|
| 376 |
|
|
* code: A n_bits-bit integer. If == -1, then EOF. This assumes
|
| 377 |
|
|
* that n_bits =< (long)wordsize - 1.
|
| 378 |
|
|
* Outputs:
|
| 379 |
|
|
* Outputs code to the file.
|
| 380 |
|
|
* Assumptions:
|
| 381 |
|
|
* Chars are 8 bits long.
|
| 382 |
|
|
* Algorithm:
|
| 383 |
|
|
* Maintain a BITS character long buffer (so that 8 codes will
|
| 384 |
|
|
* fit in it exactly). Use the VAX insv instruction to insert each
|
| 385 |
|
|
* code in turn. When the buffer fills up empty it and start over.
|
| 386 |
|
|
*/
|
| 387 |
|
|
|
| 388 |
|
|
static char buf[BITS];
|
| 389 |
|
|
|
| 390 |
|
|
#ifndef vax
|
| 391 |
|
|
char_type lmask[9] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
|
| 392 |
|
|
char_type rmask[9] = {0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
|
| 393 |
|
|
#endif /* vax */
|
| 394 |
|
|
|
| 395 |
|
|
void output( code )
|
| 396 |
|
|
code_int code;
|
| 397 |
|
|
{
|
| 398 |
|
|
|
| 399 |
|
|
/*
|
| 400 |
|
|
* On the VAX, it is important to have the register declarations
|
| 401 |
|
|
* in exactly the order given, or the asm will break.
|
| 402 |
|
|
*/
|
| 403 |
|
|
register int r_off = offset, bits= n_bits;
|
| 404 |
|
|
register char * bp = buf;
|
| 405 |
|
|
|
| 406 |
|
|
if ( code >= 0 ) {
|
| 407 |
|
|
#ifdef vax
|
| 408 |
|
|
/* VAX DEPENDENT!! Implementation on other machines is below.
|
| 409 |
|
|
*
|
| 410 |
|
|
* Translation: Insert BITS bits from the argument starting at
|
| 411 |
|
|
* offset bits from the beginning of buf.
|
| 412 |
|
|
*/
|
| 413 |
|
|
0; /* Work around for pcc -O bug with asm and if stmt */
|
| 414 |
|
|
asm( "insv 4(ap),r11,r10,(r9)" );
|
| 415 |
|
|
#else /* not a vax */
|
| 416 |
|
|
/*
|
| 417 |
|
|
* byte/bit numbering on the VAX is simulated by the following code
|
| 418 |
|
|
*/
|
| 419 |
|
|
/*
|
| 420 |
|
|
* Get to the first byte.
|
| 421 |
|
|
*/
|
| 422 |
|
|
bp += (r_off >> 3);
|
| 423 |
|
|
r_off &= 7;
|
| 424 |
|
|
/*
|
| 425 |
|
|
* Since code is always >= 8 bits, only need to mask the first
|
| 426 |
|
|
* hunk on the left.
|
| 427 |
|
|
*/
|
| 428 |
|
|
*bp = (*bp & rmask[r_off]) | ((code << r_off) & lmask[r_off]);
|
| 429 |
|
|
bp++;
|
| 430 |
|
|
bits -= (8 - r_off);
|
| 431 |
|
|
code >>= 8 - r_off;
|
| 432 |
|
|
/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
|
| 433 |
|
|
if ( bits >= 8 ) {
|
| 434 |
|
|
*bp++ = code;
|
| 435 |
|
|
code >>= 8;
|
| 436 |
|
|
bits -= 8;
|
| 437 |
|
|
}
|
| 438 |
|
|
/* Last bits. */
|
| 439 |
|
|
if(bits)
|
| 440 |
|
|
*bp = code;
|
| 441 |
|
|
#endif /* vax */
|
| 442 |
|
|
offset += n_bits;
|
| 443 |
|
|
if ( offset == (n_bits << 3) ) {
|
| 444 |
|
|
bp = buf;
|
| 445 |
|
|
bits = n_bits;
|
| 446 |
|
|
bytes_out += bits;
|
| 447 |
|
|
/* do
|
| 448 |
|
|
putchar(*bp++); */
|
| 449 |
|
|
while(--bits);
|
| 450 |
|
|
offset = 0;
|
| 451 |
|
|
}
|
| 452 |
|
|
|
| 453 |
|
|
/*
|
| 454 |
|
|
* If the next entry is going to be too big for the code size,
|
| 455 |
|
|
* then increase it, if possible.
|
| 456 |
|
|
*/
|
| 457 |
|
|
if ( free_ent > maxcode || (clear_flg > 0))
|
| 458 |
|
|
{
|
| 459 |
|
|
/*
|
| 460 |
|
|
* Write the whole buffer, because the input side won't
|
| 461 |
|
|
* discover the size increase until after it has read it.
|
| 462 |
|
|
*/
|
| 463 |
|
|
if ( offset > 0 ) {
|
| 464 |
|
|
/* if( fwrite( buf, 1, n_bits, stdout ) != n_bits)
|
| 465 |
|
|
writeerr(); */
|
| 466 |
|
|
bytes_out += n_bits;
|
| 467 |
|
|
}
|
| 468 |
|
|
offset = 0;
|
| 469 |
|
|
|
| 470 |
|
|
if ( clear_flg ) {
|
| 471 |
|
|
maxcode = MAXCODE (n_bits = INIT_BITS);
|
| 472 |
|
|
clear_flg = 0;
|
| 473 |
|
|
}
|
| 474 |
|
|
else {
|
| 475 |
|
|
n_bits++;
|
| 476 |
|
|
if ( n_bits == maxbits )
|
| 477 |
|
|
maxcode = maxmaxcode;
|
| 478 |
|
|
else
|
| 479 |
|
|
maxcode = MAXCODE(n_bits);
|
| 480 |
|
|
}
|
| 481 |
|
|
}
|
| 482 |
|
|
} else {
|
| 483 |
|
|
/*
|
| 484 |
|
|
* At EOF, write the rest of the buffer.
|
| 485 |
|
|
*/
|
| 486 |
|
|
/* if ( offset > 0 )
|
| 487 |
|
|
fwrite( buf, 1, (offset + 7) / 8, stdout ); */
|
| 488 |
|
|
bytes_out += (offset + 7) / 8;
|
| 489 |
|
|
offset = 0;
|
| 490 |
|
|
/* fflush( stdout ); */
|
| 491 |
|
|
/* if( ferror( stdout ) )
|
| 492 |
|
|
writeerr(); */
|
| 493 |
|
|
}
|
| 494 |
|
|
}
|
| 495 |
|
|
|
| 496 |
|
|
/*
|
| 497 |
|
|
* Decompress stdin to stdout. This routine adapts to the codes in the
|
| 498 |
|
|
* file building the "string" table on-the-fly; requiring no table to
|
| 499 |
|
|
* be stored in the compressed file. The tables used herein are shared
|
| 500 |
|
|
* with those of the compress() routine. See the definitions above.
|
| 501 |
|
|
*/
|
| 502 |
|
|
|
| 503 |
|
|
void decompress() {
|
| 504 |
|
|
register char_type *stackp;
|
| 505 |
|
|
register int finchar;
|
| 506 |
|
|
register code_int code, oldcode, incode;
|
| 507 |
|
|
|
| 508 |
|
|
/*
|
| 509 |
|
|
* As above, initialize the first 256 entries in the table.
|
| 510 |
|
|
*/
|
| 511 |
|
|
maxcode = MAXCODE(n_bits = INIT_BITS);
|
| 512 |
|
|
for ( code = 255; code >= 0; code-- ) {
|
| 513 |
|
|
tab_prefixof(code) = 0;
|
| 514 |
|
|
tab_suffixof(code) = (char_type)code;
|
| 515 |
|
|
}
|
| 516 |
|
|
free_ent = ((block_compress) ? FIRST : 256 );
|
| 517 |
|
|
|
| 518 |
|
|
finchar = oldcode = getcode();
|
| 519 |
|
|
if(oldcode == -1) /* EOF already? */
|
| 520 |
|
|
return; /* Get out of here */
|
| 521 |
|
|
/* putchar( (char)finchar ); */ /* first code must be 8 bits = char */
|
| 522 |
|
|
/* if(ferror(stdout))
|
| 523 |
|
|
writeerr(); */
|
| 524 |
|
|
stackp = de_stack;
|
| 525 |
|
|
|
| 526 |
|
|
while ( (code = getcode()) > -1 ) {
|
| 527 |
|
|
|
| 528 |
|
|
if ( (code == CLEAR) && block_compress ) {
|
| 529 |
|
|
for ( code = 255; code >= 0; code-- )
|
| 530 |
|
|
tab_prefixof(code) = 0;
|
| 531 |
|
|
clear_flg = 1;
|
| 532 |
|
|
free_ent = FIRST - 1;
|
| 533 |
|
|
if ( (code = getcode ()) == -1 ) /* O, untimely death! */
|
| 534 |
|
|
break;
|
| 535 |
|
|
}
|
| 536 |
|
|
incode = code;
|
| 537 |
|
|
/*
|
| 538 |
|
|
* Special case for KwKwK string.
|
| 539 |
|
|
*/
|
| 540 |
|
|
if ( code >= free_ent ) {
|
| 541 |
|
|
*stackp++ = finchar;
|
| 542 |
|
|
code = oldcode;
|
| 543 |
|
|
}
|
| 544 |
|
|
|
| 545 |
|
|
/*
|
| 546 |
|
|
* Generate output characters in reverse order
|
| 547 |
|
|
*/
|
| 548 |
|
|
#ifdef SIGNED_COMPARE_SLOW
|
| 549 |
|
|
while ( ((unsigned long)code) >= ((unsigned long)256) ) {
|
| 550 |
|
|
#else
|
| 551 |
|
|
while ( code >= 256 ) {
|
| 552 |
|
|
#endif
|
| 553 |
|
|
*stackp++ = tab_suffixof(code);
|
| 554 |
|
|
code = tab_prefixof(code);
|
| 555 |
|
|
}
|
| 556 |
|
|
*stackp++ = finchar = tab_suffixof(code);
|
| 557 |
|
|
|
| 558 |
|
|
/*
|
| 559 |
|
|
* And put them out in forward order
|
| 560 |
|
|
*/
|
| 561 |
|
|
/* do
|
| 562 |
|
|
putchar ( *--stackp );
|
| 563 |
|
|
while ( stackp > de_stack );*/
|
| 564 |
|
|
|
| 565 |
|
|
/*
|
| 566 |
|
|
* Generate the new entry.
|
| 567 |
|
|
*/
|
| 568 |
|
|
if ( (code=free_ent) < maxmaxcode ) {
|
| 569 |
|
|
tab_prefixof(code) = (unsigned short)oldcode;
|
| 570 |
|
|
tab_suffixof(code) = finchar;
|
| 571 |
|
|
free_ent = code+1;
|
| 572 |
|
|
}
|
| 573 |
|
|
/*
|
| 574 |
|
|
* Remember previous code.
|
| 575 |
|
|
*/
|
| 576 |
|
|
oldcode = incode;
|
| 577 |
|
|
}
|
| 578 |
|
|
/* fflush( stdout ); */
|
| 579 |
|
|
/* if(ferror(stdout))
|
| 580 |
|
|
writeerr(); */
|
| 581 |
|
|
}
|
| 582 |
|
|
|
| 583 |
|
|
/*****************************************************************
|
| 584 |
|
|
* TAG( getcode )
|
| 585 |
|
|
*
|
| 586 |
|
|
* Read one code from the standard input. If EOF, return -1.
|
| 587 |
|
|
* Inputs:
|
| 588 |
|
|
* stdin
|
| 589 |
|
|
* Outputs:
|
| 590 |
|
|
* code or -1 is returned.
|
| 591 |
|
|
*/
|
| 592 |
|
|
|
| 593 |
|
|
code_int
|
| 594 |
|
|
getcode() {
|
| 595 |
|
|
/*
|
| 596 |
|
|
* On the VAX, it is important to have the register declarations
|
| 597 |
|
|
* in exactly the order given, or the asm will break.
|
| 598 |
|
|
*/
|
| 599 |
|
|
register code_int code;
|
| 600 |
|
|
static int offset = 0, size = 0;
|
| 601 |
|
|
static char_type buf[BITS];
|
| 602 |
|
|
register int r_off, bits;
|
| 603 |
|
|
register char_type *bp = buf;
|
| 604 |
|
|
|
| 605 |
|
|
if ( clear_flg > 0 || offset >= size || free_ent > maxcode ) {
|
| 606 |
|
|
/*
|
| 607 |
|
|
* If the next entry will be too big for the current code
|
| 608 |
|
|
* size, then we must increase the size. This implies reading
|
| 609 |
|
|
* a new buffer full, too.
|
| 610 |
|
|
*/
|
| 611 |
|
|
if ( free_ent > maxcode ) {
|
| 612 |
|
|
n_bits++;
|
| 613 |
|
|
if ( n_bits == maxbits )
|
| 614 |
|
|
maxcode = maxmaxcode; /* won't get any bigger now */
|
| 615 |
|
|
else
|
| 616 |
|
|
maxcode = MAXCODE(n_bits);
|
| 617 |
|
|
}
|
| 618 |
|
|
if ( clear_flg > 0) {
|
| 619 |
|
|
maxcode = MAXCODE (n_bits = INIT_BITS);
|
| 620 |
|
|
clear_flg = 0;
|
| 621 |
|
|
}
|
| 622 |
|
|
/* size = fread( buf, 1, n_bits, stdin ); */
|
| 623 |
|
|
if ( size <= 0 )
|
| 624 |
|
|
return -1; /* end of file */
|
| 625 |
|
|
offset = 0;
|
| 626 |
|
|
/* Round size down to integral number of codes */
|
| 627 |
|
|
size = (size << 3) - (n_bits - 1);
|
| 628 |
|
|
}
|
| 629 |
|
|
r_off = offset;
|
| 630 |
|
|
bits = n_bits;
|
| 631 |
|
|
#ifdef vax
|
| 632 |
|
|
asm( "extzv r10,r9,(r8),r11" );
|
| 633 |
|
|
#else /* not a vax */
|
| 634 |
|
|
/*
|
| 635 |
|
|
* Get to the first byte.
|
| 636 |
|
|
*/
|
| 637 |
|
|
bp += (r_off >> 3);
|
| 638 |
|
|
r_off &= 7;
|
| 639 |
|
|
/* Get first part (low order bits) */
|
| 640 |
|
|
#ifdef NO_UCHAR
|
| 641 |
|
|
code = ((*bp++ >> r_off) & rmask[8 - r_off]) & 0xff;
|
| 642 |
|
|
#else
|
| 643 |
|
|
code = (*bp++ >> r_off);
|
| 644 |
|
|
#endif /* NO_UCHAR */
|
| 645 |
|
|
bits -= (8 - r_off);
|
| 646 |
|
|
r_off = 8 - r_off; /* now, offset into code word */
|
| 647 |
|
|
/* Get any 8 bit parts in the middle (<=1 for up to 16 bits). */
|
| 648 |
|
|
if ( bits >= 8 ) {
|
| 649 |
|
|
#ifdef NO_UCHAR
|
| 650 |
|
|
code |= (*bp++ & 0xff) << r_off;
|
| 651 |
|
|
#else
|
| 652 |
|
|
code |= *bp++ << r_off;
|
| 653 |
|
|
#endif /* NO_UCHAR */
|
| 654 |
|
|
r_off += 8;
|
| 655 |
|
|
bits -= 8;
|
| 656 |
|
|
}
|
| 657 |
|
|
/* high order bits. */
|
| 658 |
|
|
code |= (*bp & rmask[bits]) << r_off;
|
| 659 |
|
|
#endif /* vax */
|
| 660 |
|
|
offset += n_bits;
|
| 661 |
|
|
|
| 662 |
|
|
return code;
|
| 663 |
|
|
}
|
| 664 |
|
|
|
| 665 |
|
|
/* For those who don't have it in libc.a */
|
| 666 |
|
|
char *
|
| 667 |
|
|
rindex(const char *s,
|
| 668 |
|
|
int c)
|
| 669 |
|
|
{
|
| 670 |
|
|
const char *p;
|
| 671 |
|
|
for (p = NULL; *s; s++)
|
| 672 |
|
|
if (*s == c)
|
| 673 |
|
|
p = s;
|
| 674 |
|
|
return((char *)p);
|
| 675 |
|
|
}
|
| 676 |
|
|
|
| 677 |
|
|
/*
|
| 678 |
|
|
writeerr()
|
| 679 |
|
|
{
|
| 680 |
|
|
perror ( ofname );
|
| 681 |
|
|
unlink ( ofname );
|
| 682 |
|
|
exit ( 1 );
|
| 683 |
|
|
}
|
| 684 |
|
|
*/
|
| 685 |
|
|
void cl_block () /* table clear for block compress */
|
| 686 |
|
|
{
|
| 687 |
|
|
register long int rat;
|
| 688 |
|
|
|
| 689 |
|
|
checkpoint = in_count + CHECK_GAP;
|
| 690 |
|
|
|
| 691 |
|
|
if(in_count > 0x007fffff) { /* shift will overflow */
|
| 692 |
|
|
rat = bytes_out >> 8;
|
| 693 |
|
|
if(rat == 0) { /* Don't divide by zero */
|
| 694 |
|
|
rat = 0x7fffffff;
|
| 695 |
|
|
} else {
|
| 696 |
|
|
rat = in_count / rat;
|
| 697 |
|
|
}
|
| 698 |
|
|
} else {
|
| 699 |
|
|
rat = (in_count << 8) / bytes_out; /* 8 fractional bits */
|
| 700 |
|
|
}
|
| 701 |
|
|
if ( rat > ratio ) {
|
| 702 |
|
|
ratio = rat;
|
| 703 |
|
|
} else {
|
| 704 |
|
|
ratio = 0;
|
| 705 |
|
|
cl_hash ( (count_int) hsize );
|
| 706 |
|
|
free_ent = FIRST;
|
| 707 |
|
|
clear_flg = 1;
|
| 708 |
|
|
output ( (code_int) CLEAR );
|
| 709 |
|
|
}
|
| 710 |
|
|
}
|
| 711 |
|
|
|
| 712 |
|
|
void cl_hash(hsize) /* reset code table */
|
| 713 |
|
|
register count_int hsize;
|
| 714 |
|
|
{
|
| 715 |
|
|
#ifndef XENIX_16 /* Normal machine */
|
| 716 |
|
|
register count_int *htab_p = htab+hsize;
|
| 717 |
|
|
#else
|
| 718 |
|
|
register j;
|
| 719 |
|
|
register long k = hsize;
|
| 720 |
|
|
register count_int *htab_p;
|
| 721 |
|
|
#endif
|
| 722 |
|
|
register long i;
|
| 723 |
|
|
register long m1 = -1;
|
| 724 |
|
|
|
| 725 |
|
|
#ifdef XENIX_16
|
| 726 |
|
|
for(j=0; j<=8 && k>=0; j++,k-=8192) {
|
| 727 |
|
|
i = 8192;
|
| 728 |
|
|
if(k < 8192) {
|
| 729 |
|
|
i = k;
|
| 730 |
|
|
}
|
| 731 |
|
|
htab_p = &(htab[j][i]);
|
| 732 |
|
|
i -= 16;
|
| 733 |
|
|
if(i > 0) {
|
| 734 |
|
|
#else
|
| 735 |
|
|
i = hsize - 16;
|
| 736 |
|
|
#endif
|
| 737 |
|
|
do { /* might use Sys V memset(3) here */
|
| 738 |
|
|
*(htab_p-16) = m1;
|
| 739 |
|
|
*(htab_p-15) = m1;
|
| 740 |
|
|
*(htab_p-14) = m1;
|
| 741 |
|
|
*(htab_p-13) = m1;
|
| 742 |
|
|
*(htab_p-12) = m1;
|
| 743 |
|
|
*(htab_p-11) = m1;
|
| 744 |
|
|
*(htab_p-10) = m1;
|
| 745 |
|
|
*(htab_p-9) = m1;
|
| 746 |
|
|
*(htab_p-8) = m1;
|
| 747 |
|
|
*(htab_p-7) = m1;
|
| 748 |
|
|
*(htab_p-6) = m1;
|
| 749 |
|
|
*(htab_p-5) = m1;
|
| 750 |
|
|
*(htab_p-4) = m1;
|
| 751 |
|
|
*(htab_p-3) = m1;
|
| 752 |
|
|
*(htab_p-2) = m1;
|
| 753 |
|
|
*(htab_p-1) = m1;
|
| 754 |
|
|
htab_p -= 16;
|
| 755 |
|
|
} while ((i -= 16) >= 0);
|
| 756 |
|
|
#ifdef XENIX_16
|
| 757 |
|
|
}
|
| 758 |
|
|
}
|
| 759 |
|
|
#endif
|
| 760 |
|
|
for ( i += 16; i > 0; i-- )
|
| 761 |
|
|
*--htab_p = m1;
|
| 762 |
|
|
}
|
| 763 |
|
|
|