1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
|
/*
Encode or decode file as five letter code groups
by John Walker
http://www.fourmilab.ch/
This program is in the public domain.
December 1986: Original version
July 1995: Unified encoder and decoder in one program,
replaced ad-hoc checksum with CRC-16.
October 1998: HTML documentation, Zipped distribution archive
including ready-to-run MS-DOS program.
September 2008: Fixed stdin/stdout initialiser problems with
GCC 4.1.2, added const declarations.
*/
#define REVDATE "7th September 2008"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#endif
#define TRUE 1
#define FALSE 0
typedef unsigned char byte; /* Byte type */
#define EOS '\0'
#define GROUPLEN 5 /* Code group type */
#define LINELEN 64 /* Maximum line length */
#define ERRMAX 10 /* Maximum data lost messages to print */
static FILE *fi; /* Input file */
static FILE *fo; /* Output file */
static char groupbuf[GROUPLEN + 1]; /* Group assembly buffer */
static char linebuf[LINELEN + 4]; /* Line editing buffer */
static byte iobuf[256]; /* I/O buffer */
static byte obuf[32]; /* Output assembly buffer */
static long gcount = 0L; /* Groups sent count */
static long cksum = 0L; /* Data checksum */
static int gblen = 0; /* Group bytes used count */
static int linelen = 0; /* Bytes used in line */
static int gprefix; /* Prefix for data block group */
static int iolen = 0; /* Bytes left in I/O buffer */
static int iocp = 256; /* Character removal pointer */
static int ateof = FALSE; /* EOF encountered */
static int obnib = 0; /* Output nybble index */
static int obbyte = 0; /* Output byte index */
static int exitstat = 0; /* Exit status */
/* This is the precomputed remainder table for generating and
checking cyclic redundancy check characters. */
static const byte low8[] = {
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,
0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,
0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,
0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,
0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,
0x81,0x40,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,
0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,
0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
0x01,0xC0,0x80,0x41,0x00,0xC1,0x81,0x40,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40,0x00,0xC1,0x81,0x40,
0x01,0xC0,0x80,0x41,0x01,0xC0,0x80,0x41,0x00,0xC1,
0x81,0x40,0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,
0x00,0xC1,0x81,0x40,0x01,0xC0,0x80,0x41,0x01,0xC0,
0x80,0x41,0x00,0xC1,0x81,0x40
};
static const byte high8[] = {
0x00,0xC0,0xC1,0x01,0xC3,0x03,0x02,0xC2,0xC6,0x06,
0x07,0xC7,0x05,0xC5,0xC4,0x04,0xCC,0x0C,0x0D,0xCD,
0x0F,0xCF,0xCE,0x0E,0x0A,0xCA,0xCB,0x0B,0xC9,0x09,
0x08,0xC8,0xD8,0x18,0x19,0xD9,0x1B,0xDB,0xDA,0x1A,
0x1E,0xDE,0xDF,0x1F,0xDD,0x1D,0x1C,0xDC,0x14,0xD4,
0xD5,0x15,0xD7,0x17,0x16,0xD6,0xD2,0x12,0x13,0xD3,
0x11,0xD1,0xD0,0x10,0xF0,0x30,0x31,0xF1,0x33,0xF3,
0xF2,0x32,0x36,0xF6,0xF7,0x37,0xF5,0x35,0x34,0xF4,
0x3C,0xFC,0xFD,0x3D,0xFF,0x3F,0x3E,0xFE,0xFA,0x3A,
0x3B,0xFB,0x39,0xF9,0xF8,0x38,0x28,0xE8,0xE9,0x29,
0xEB,0x2B,0x2A,0xEA,0xEE,0x2E,0x2F,0xEF,0x2D,0xED,
0xEC,0x2C,0xE4,0x24,0x25,0xE5,0x27,0xE7,0xE6,0x26,
0x22,0xE2,0xE3,0x23,0xE1,0x21,0x20,0xE0,0xA0,0x60,
0x61,0xA1,0x63,0xA3,0xA2,0x62,0x66,0xA6,0xA7,0x67,
0xA5,0x65,0x64,0xA4,0x6C,0xAC,0xAD,0x6D,0xAF,0x6F,
0x6E,0xAE,0xAA,0x6A,0x6B,0xAB,0x69,0xA9,0xA8,0x68,
0x78,0xB8,0xB9,0x79,0xBB,0x7B,0x7A,0xBA,0xBE,0x7E,
0x7F,0xBF,0x7D,0xBD,0xBC,0x7C,0xB4,0x74,0x75,0xB5,
0x77,0xB7,0xB6,0x76,0x72,0xB2,0xB3,0x73,0xB1,0x71,
0x70,0xB0,0x50,0x90,0x91,0x51,0x93,0x53,0x52,0x92,
0x96,0x56,0x57,0x97,0x55,0x95,0x94,0x54,0x9C,0x5C,
0x5D,0x9D,0x5F,0x9F,0x9E,0x5E,0x5A,0x9A,0x9B,0x5B,
0x99,0x59,0x58,0x98,0x88,0x48,0x49,0x89,0x4B,0x8B,
0x8A,0x4A,0x4E,0x8E,0x8F,0x4F,0x8D,0x4D,0x4C,0x8C,
0x44,0x84,0x85,0x45,0x87,0x47,0x46,0x86,0x82,0x42,
0x43,0x83,0x41,0x81,0x80,0x40
};
static unsigned int crc1, crc2; /* CRC accumulation cells */
/* CRCCHAR -- Include byte in CRC. */
static void crcchar(const byte b)
{
unsigned int c;
crc1 = low8[c = 0xFF & (b ^ crc1)] ^ crc2;
crc2 = high8[c];
}
/* CRCINIT -- Initialise CRC computation. */
static void crcinit(void)
{
crc1 = crc2 = 0;
crcchar(1); /* Guarantee no leading zeroes */
}
/* OUTLINE -- Output next line */
static void outline(void)
{
linebuf[linelen++] = '\n';
fwrite(linebuf, linelen, 1, fo);
linelen = 0;
}
/* OUTGROUP -- Output next group to line buffer */
static void outgroup(void)
{
if ((linelen + GROUPLEN + 1) > LINELEN) {
outline();
}
if (linelen > 0) {
linebuf[linelen++] = ' ';
}
strcpy(linebuf + linelen, groupbuf);
linelen += GROUPLEN;
gblen = 0;
gcount++; /* Increment groups sent */
}
/* OUTCHAR -- Output next character to group */
static void outchar(const int c)
{
groupbuf[gblen++] = c;
if (gblen >= GROUPLEN) {
outgroup();
}
}
/* OUTBYTE -- Output next encoded byte as two group characters */
static void outbyte(int b)
{
crcchar(b);
outchar('A' + ((b & 0xF0) >> 4));
outchar('A' + (b & 0xF));
}
/* OUT32 -- Output 32 byte data block */
static void out32(const int code, const byte dbuf[32])
{
int i;
outchar(code);
for (i = 0; i < 32; i++) {
outbyte((int) dbuf[i]);
}
}
/* STORELONG -- Store long value in I/O buffer (byte-order independent) */
static void storelong(byte *cp, long l)
{
int i;
for (i = 0; i < 4; i++) {
*cp++ = l & 0xFF;
l >>= 8;
}
}
/* ENGROUP -- Encode binary file into code groups. */
static void engroup(void)
{
int i, l;
strcpy(groupbuf, "ZZZZZ"); /* Place start sentinel */
outgroup();
while (TRUE) {
l = fread(iobuf + 1, 1, 32, fi);
if (l <= 0) {
break;
}
if (l < 32) {
iobuf[0] = (byte) l;
out32('V', iobuf);
} else {
out32('Y', iobuf + 1);
}
}
/* Emit ending record with group count and checksum. */
storelong(iobuf, gcount);
cksum = (crc1 << 16) | crc2;
storelong(iobuf + 4, cksum);
for (i = 8; i < 32; i += 4) {
storelong(iobuf + i, cksum = (gcount ^ cksum) ^ (cksum >> 3)
^ (cksum << 3));
}
out32('U', iobuf);
strcpy(groupbuf, "WWWWW"); /* Place end sentinel */
outgroup();
outline();
}
/* INBUF -- Fill input buffer with data */
static int inbuf(void)
{
int l;
if (ateof) {
return FALSE;
}
l = fread(iobuf, 1, 256, fi); /* Read input buffer */
if (l <= 0) {
ateof = TRUE;
return FALSE;
}
iolen = l;
iocp = 0;
return TRUE;
}
/* INCHAR -- Return next character from input */
static int inchar(void)
{
if (iocp >= iolen) {
if (!inbuf()) {
return EOF;
}
}
return iobuf[iocp++];
}
/* INSIG -- Return next significant input */
static int insig(void)
{
int c;
static int skipws = FALSE; /* Skip white space flag */
if (skipws) {
while (TRUE) {
c = inchar();
if (c == EOF || (c > ' ')) {
skipws = FALSE;
return c;
}
}
}
c = inchar();
if (c <= ' ') {
c = ' ';
skipws = TRUE;
}
return c;
}
/* INSKERR -- Skip error. Ignores input until next white space or end of
file. */
static void inskerr(void)
{
int ch;
while (TRUE) {
ch = insig();
if (ch == EOF || ch == ' ') {
break;
}
}
}
/* INGROUP -- Scan next code group into group buffer. Returns FALSE
if an error is detected in the code group, EOF if the
end of file is hit, and TRUE if a valid group is assembled. */
static int ingroup(void)
{
int gp = 0; /* Group data pointer */
int ch;
while (TRUE) {
ch = insig();
if (ch == EOF) {
return EOF;
}
if (ch != ' ') {
if (ch < 'A' || ch > 'Z') {
inskerr();
return FALSE;
}
groupbuf[gp++] = ch;
break;
}
}
while (TRUE) {
ch = insig();
if (ch == EOF || ch == ' ') {
if (gp < 5) {
return FALSE; /* Short groups got no reason to live */
}
gcount++; /* Increment valid groups received */
return TRUE;
}
if (ch < 'A' || ch > 'Z') {
inskerr();
return FALSE;
}
if (gp >= 5) {
inskerr(); /* Group is too long */
return FALSE;
}
groupbuf[gp++] = ch;
}
/* NOTREACHED */
}
/* OSTORE -- Store data group starting at specified offset.
Verifies that this is not a control
group being misinterpreted. */
static int ostore(const int x)
{
int i, j;
for (i = x; i < 5; i++) {
if (groupbuf[i] < 'A' || groupbuf[i] > ('A' + 15)) {
return FALSE;
}
j = groupbuf[i] - 'A';
if (obnib) {
obuf[obbyte++] |= j;
obnib = FALSE;
} else {
obuf[obbyte] = j << 4;
obnib = TRUE;
}
}
return TRUE;
}
/* IN32 -- Input data record. Returns EOF if 'WWWWWW' terminator
group found, FALSE if an error is detected, and TRUE if
a valid group was decoded. If a valid group was found, its
data is stored in OBUF, with the prefix letter in GPREFIX.
If an error was previously reported, all data is ignored
until a valid start of record group is found. */
static int in32(void)
{
int inerr = FALSE;
int i, j;
if (inerr) {
while (TRUE) {
i = ingroup();
if (i == EOF) {
return EOF;
}
if (i == TRUE) {
if (groupbuf[0] == 'Y' || groupbuf[0] == 'V' ||
(strcmp(groupbuf, "WWWWW") == 0)) {
inerr = FALSE;
break;
}
}
}
} else {
i = ingroup();
}
if (i == EOF || i == FALSE) {
inerr = TRUE;
return FALSE;
}
if (strcmp(groupbuf, "WWWWW") == 0) {
return EOF;
}
gprefix = groupbuf[0];
if (gprefix == 'Y' || gprefix == 'V' || gprefix == 'U') {
obnib = 0;
obbyte = 0;
i = ostore(1);
if (i == FALSE) {
inerr = TRUE;
return FALSE;
}
for (j = 0; j < 12; j++) {
i = ingroup();
if (i == EOF || i == FALSE) {
inerr = TRUE;
return FALSE;
}
if (!ostore(0)) {
inerr = TRUE;
return FALSE;
}
}
for (j = 0; j < 32; j++) {
crcchar(obuf[j]);
}
return TRUE;
} else {
inerr = TRUE;
return FALSE;
}
}
/* GETLONG -- Load LONG from a byte stream in an order-independent
fashion. */
static long getlong(byte const *cp)
{
int i;
long l;
for (l = 0L, i = 0; i < 4; i++) {
l |= ((long) (*cp++)) << (i * 8);
}
return l;
}
/* UNGROUP -- Decode codegroups. */
static void ungroup(void)
{
int i, l, nerrs = 0;
long savegc, savecs;
groupbuf[GROUPLEN] = EOS; /* Set group terminator */
/* Ignore all text before initial ZZZZZ group. */
l = 0;
while (TRUE) {
i = insig();
if (i == EOF) {
break;
}
if (i == 'Z') {
l++;
if (l == 5) {
i = insig();
if (i == EOF) {
break;
}
if (i == ' ') {
break;
} else {
l = (i == 'Z' ? 5 : 0);
}
}
} else {
l = 0;
}
}
/* If we hit end of file before finding the first code group, issue
an error message. */
if (i == EOF) {
fprintf(stderr, "No code groups found in input.\n");
exitstat = 2;
return;
}
gcount = 1; /* Account for initial sentinel group */
while (TRUE) {
savegc = gcount;
cksum = (crc1 << 16) | crc2;
savecs = cksum;
i = in32();
if (i == EOF) {
fprintf(stderr, "Warning: count and checksum missing.\n");
fprintf(stderr, " File may be incomplete.\n");
exitstat = 1;
return;
}
if (i == FALSE) {
if (++nerrs <= ERRMAX) {
fprintf(stderr,
"Error: skipping to next group. Data lost.\n");
exitstat = 1;
}
} else {
if (gprefix == 'U') {
if (nerrs > ERRMAX) {
nerrs = nerrs - ERRMAX;
fprintf(stderr,
"Too many errors. %d additional data lost message%s suppressed.\n",
nerrs, nerrs > 1 ? "s" : "");
}
gcount = getlong(obuf);
cksum = getlong(obuf + 4);
if (gcount > savegc) {
fprintf(stderr, "Warning: groups missing from file.\n");
fprintf(stderr,
" Groups sent: %ld, Groups received %ld.\n",
gcount, savegc);
exitstat = 1;
} else if (gcount < savegc) {
fprintf(stderr, "Warning: extraneous groups in file.\n");
fprintf(stderr,
" Groups sent: %ld, Groups received %ld.\n",
gcount, savegc);
exitstat = 1;
}
if (cksum != savecs) {
fprintf(stderr,
"Warning: checksum error on file contents.\n");
#ifdef DUMPCKSUM
fprintf(stderr,
" Checksum sent: %lX, received: %lX\n",
cksum, savecs);
#endif
exitstat = 1;
}
if (in32() != EOF) {
fprintf(stderr,
"Warning: extraneous material after final data block.\n");
exitstat = 1;
}
return;
} else if (gprefix == 'Y') {
fwrite(obuf, 32, 1, fo);
} else {
fwrite(obuf + 1, 1, obuf[0], fo);
}
}
}
}
/* Main program */
int main(int argc, char *argv[])
{
int i, f = 0, decode = FALSE;
char *cp, opt;
fi = stdin;
fo = stdout;
for (i = 1; i < argc; i++) {
cp = argv[i];
if (*cp == '-') {
opt = *(++cp);
if (islower(opt)) {
opt = toupper(opt);
}
switch (opt) {
case 'D':
decode = TRUE;
break;
case 'E':
decode = FALSE;
break;
case 'U':
case '?':
fprintf(stderr,"\n%s -- Encode/decode file as code groups. Call:", argv[0]);
fprintf(stderr,
"\n %s [-e[ncode] / -d[ecode]] [infile] [outfile]", argv[0]);
fprintf(stderr,"\n");
return 0;
}
} else {
switch (f) {
/** Warning! On systems which distinguish text mode and
binary I/O (MS-DOS, Macintosh, etc.) the modes in these
open statements will have to be made conditional based
upon whether an encode or decode is being done, which
will have to be specified earlier. But it's worse: if
input or output is from standard input or output, the
mode will have to be changed on the fly, which is
generally system and compiler dependent. 'Twasn't me
who couldn't conform to Unix CR/LF convention, so
don't ask me to write the code to work around
Apple and Microsoft's incompatible standards.
This file contains code, conditional on _WIN32, which
sets binary mode using the method prescribed by
Microsoft Visual C 1.52 ("Monkey C"); this may
require modification if you're using a different
compiler or release of Monkey C. */
case 0:
if (strcmp(cp, "-") != 0) {
if ((fi = fopen(cp, "r")) == NULL) {
fprintf(stderr, "Cannot open input file %s\n", cp);
return 2;
}
}
f++;
break;
case 1:
if (strcmp(cp, "-") != 0) {
if ((fo = fopen(cp, "w")) == NULL) {
fprintf(stderr, "Cannot open output file %s\n", cp);
return 2;
}
}
f++;
break;
default:
fprintf(stderr, "Too many file names specified.\n");
return 2;
}
}
}
crcinit();
if (decode) {
#ifdef _WIN32
_setmode(_fileno(fo), _O_BINARY);
#endif
ungroup();
} else {
#ifdef _WIN32
_setmode(_fileno(fi), _O_BINARY);
#endif
engroup();
}
return exitstat;
}
|