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
|
/* btoa.c */
/* Written by Paul Rutter, Joe Orost & Stefan Parmark. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "btoa.h"
#if USE_MACROS
#include "chksum.h"
#endif USE_MACROS
#define VERSION "5.2"
LONG Ceor, Csum, Crot; /* Checksums to verify archive validity. */
BYTE new_version, openoutput, buffer[BUFSIZE];
FILE *outfile;
void main(argc, argv)
int argc;
BYTE **argv;
{
register BYTE openinput, error, ch, a_to_b, diagnosis, repair;
register BYTE *infilename, *text;
register FILE *infile;
extern BYTE new_version, openoutput;
extern FILE *outfile;
#ifdef AMIGA
extern int _bufsiz;
/* Change file buffer size. */
_bufsiz = 10240;
#endif AMIGA
error = openinput = openoutput = a_to_b = diagnosis = repair = FALSE;
new_version = TRUE;
infilename = NULL;
/* Assume a_to_b = TRUE if we're called 'atob' */
if (!strncmp(argv[0] + strlen(argv[0]) - 4, "atob", 4)) {
a_to_b = TRUE;
}
/* Scan for '-' options. The rest must be file names. */
while (!error && argc > 1 && *argv[1] == '-')
{
text = &argv[1][1];
while (!error && (ch = *text++) != 0)
{
switch(ch)
{
case 'a' : /* Activate atob. */
a_to_b = TRUE;
break;
case 'd' : /* Extract missing part from undamaged archive. */
diagnosis = TRUE;
break;
case 'h' : /* Print help and abort execution. */
error = TRUE;
break;
case 'o' : /* Use old btoa format. */
new_version = FALSE;
break;
case 'r' : /* Repair damaged archive. */
repair = TRUE;
break;
default : error = TRUE;
}
}
argv++;
argc--;
}
if (argc > 3)
error = TRUE;
if (error)
printhelp();
else
{
/* If file name was given, try to open file. Otherwise use stdin. */
if (argc > 1)
{
infilename = argv[1];
if ((infile = fopen_read(infilename)) == NULL)
error = TRUE;
else
openinput = TRUE;
}
else
infile = stdin;
}
if (!error)
{
/* If file name was given, try to open file. Otherwise use stdout. */
if (argc > 2 && !diagnosis && !repair)
{
if ((outfile = fopen_write(argv[2])) == NULL)
error = TRUE;
else
openoutput = TRUE;
}
else
outfile = stdout;
}
if (!error)
{
if (diagnosis)
error = producerepair(infile);
else if (repair)
error = performrepair(infile);
else if (a_to_b)
error = atob(infile);
else
error = btoa(infile, infilename);
}
/* Close all opened files. */
if (openinput)
fclose(infile);
if (openoutput)
fclose(outfile);
if (error)
exit(1);
}
BYTE btoa(infile, infilename)
register FILE *infile;
register BYTE *infilename;
{
register LONG codeword, filesize;
register int ch1, ch2, ch3, ch4, readbytes;
extern FILE *outfile;
extern BYTE new_version, buffer[BUFSIZE];
extern LONG Ceor, Csum, Crot;
Ceor = Csum = Crot = 0;
/* Write archive header. */
if (new_version)
{
fprintf(outfile, "xbtoa5 %d %s Begin\n", MAXPERLINE,
(infilename == NULL) ? "-" : truncname(infilename));
}
else
fprintf(outfile, "xbtoa Begin\n");
/* Encode entire input file. */
filesize = 0;
do
{
readbytes = fread(buffer, 1, 4, infile);
if (readbytes < 4)
{
ch1 = (readbytes > 0) ? ((int)buffer[0] & 0xFF) : 0;
ch2 = (readbytes > 1) ? ((int)buffer[1] & 0xFF) : 0;
ch3 = (readbytes > 2) ? ((int)buffer[2] & 0xFF) : 0;
ch4 = 0;
}
else
{
ch1 = (int)buffer[0] & 0xFF;
ch2 = (int)buffer[1] & 0xFF;
ch3 = (int)buffer[2] & 0xFF;
ch4 = (int)buffer[3] & 0xFF;
}
if (readbytes > 0)
{
if (!new_version)
{
calcchecksum(ch1);
calcchecksum(ch2);
calcchecksum(ch3);
calcchecksum(ch4);
}
codeword = (ch1 << 8) | ch2;
codeword = (((codeword << 8) | ch3) << 8) | ch4;
wordout(codeword);
filesize += readbytes;
}
}
while (readbytes == 4);
asciiout(EOF); /* Flush buffer. */
/* Filesize is written twice as crude cross check. */
fprintf(outfile, "xbtoa End N %ld %lx E %lx S %lx R %lx\n",
filesize, filesize, Ceor, Csum, Crot);
return(FALSE); /* No errors discovered. */
}
/* Print help on how to use btoa. */
void printhelp()
{
fprintf(stderr, " Btoa version %s\n", VERSION);
fprintf(stderr, "Written by Paul Rutter, Joe Orost & Stefan Parmark.\n");
fprintf(stderr, "\nUsage: btoa [-{adhor}] [input file] [output file]\n");
fprintf(stderr, "\nOptions:\n");
fprintf(stderr, "-h Shows this help list.\n");
fprintf(stderr, "-a Use atob rather than btoa.\n");
fprintf(stderr, "-o Use old version of btoa.\n");
fprintf(stderr, "-d Extract repair file from diagnosis file.\n");
fprintf(stderr, "-r Repair archive from repair file.\n");
fprintf(stderr, "\nExamples:\n");
fprintf(stderr, " btoa -h\n");
fprintf(stderr, " btoa [input binary file] [output archive file]\n");
fprintf(stderr, " btoa -o [input binary file] [output archive file]\n");
fprintf(stderr, " btoa -a [input archive file] [output binary file]\n");
fprintf(stderr, " btoa -d [undamaged archive file]\n");
fprintf(stderr, " btoa -r [damaged archive file]\n");
}
#if !USE_MACROS
/* Update file checksums. */
void calcchecksum(ch)
register int ch;
{
extern LONG Ceor, Csum, Crot;
Ceor ^= ch;
Csum += ch + 1;
if (Crot & 0x80000000L)
ch ++;
Crot <<= 1;
Crot += ch;
}
#endif !USE_MACROS
/* Encode 4 binary bytes to 5 ascii bytes. */
void wordout(codeword)
register LONG codeword;
{
register int tmp, quote;
extern BYTE new_version;
if (codeword == 0)
/* Encode 4 zeros as a 'z'. */
asciiout('z');
else if (new_version && codeword == 0x20202020)
/* Encode 4 spaces as a 'y'. */
asciiout('y');
else
{
tmp = 0;
/* Extra calculations because some machines don't support */
/* unsigned longwords. */
if (codeword < 0)
{
tmp = 32;
codeword -= (LONG)(85L * 85 * 85 * 85 * 32);
}
if (codeword < 0)
{
tmp = 64;
codeword -= (LONG)(85L * 85 * 85 * 85 * 32);
}
/* Write 5 ascii bytes representing 4 binary bytes. */
quote = codeword / (LONG)(85L * 85 * 85 * 85);
codeword -= quote * (LONG)(85L * 85 * 85 * 85);
asciiout(ENCODE(quote + tmp));
quote = codeword / (LONG)(85L * 85 * 85);
codeword -= quote * (LONG)(85L * 85 * 85);
asciiout(ENCODE(quote));
quote = codeword / (LONG)(85L * 85);
codeword -= quote * (LONG)(85L * 85);
asciiout(ENCODE(quote));
quote = (int)codeword / 85;
codeword -= quote * 85;
asciiout(ENCODE(quote));
asciiout(ENCODE((int)codeword));
}
}
/* Write ch to outfile. Write '\n' for every line. */
void asciiout(ch)
register int ch;
{
static WORD linepos = 0;
extern FILE *outfile;
extern LONG Csum;
extern BYTE new_version;
if (ch == EOF) /* Signal to flush buffer. */
{
/* Linepos == 0 means '\n' just written in asciiout(). This */
/* avoids bug in BITNET, which changes blank line to spaces. */
if (linepos != 0)
{
if (new_version)
fputc(ENCODE(Csum % 85), outfile); /* Checksum for every line. */
fputc('\n', outfile);
}
}
else
{
fputc(ch, outfile);
linepos ++;
if (new_version)
{
calcchecksum(ch);
if (linepos >= (MAXPERLINE-1))
{
fputc(ENCODE(Csum % 85), outfile); /* Checksum for every line. */
fputc('\n', outfile);
linepos = 0;
}
}
else /* Old version */
if (linepos >= MAXPERLINE)
{
fputc('\n', outfile);
linepos = 0;
}
}
}
/* Remove paths from a file name. */
BYTE *truncname(name)
register BYTE *name;
{
register BYTE ch, *newname;
newname = name;
while ((ch = *name++) != 0)
if (ch == '/' || ch == ':')
newname = name;
return(newname);
}
|