Difference between revisions of "Gzcompat"

From Computer History Wiki
Jump to: navigation, search
(update gzcompat to version 5.2)
Line 5: Line 5:
 
<pre>
 
<pre>
 
#ifndef lint
 
#ifndef lint
static char sccsid[] = "@(#)gzcompat.c 5.1 (Berkeley) 1/21/99";
+
static char sccsid[] = "@(#)gzcompat.c 5.2 (Berkeley) 12/21/00";
 
#endif
 
#endif
  
Line 16: Line 16:
  
 
struct gzheader {
 
struct gzheader {
        unsigned char cm;
+
unsigned char cm;
        unsigned char flg;
+
unsigned char flg;
        unsigned char mtime[4];
+
unsigned char mtime[4];
        unsigned char xfl;
+
unsigned char xfl;
        unsigned char os;
+
unsigned char os;
 
} gzheader;
 
} gzheader;
  
Line 34: Line 34:
 
#define OS_UNIX 0x03
 
#define OS_UNIX 0x03
  
int main()
+
int main(argc, argv)
 +
char **argv;
 
{
 
{
        char buf[4096];
+
FILE *infile;
        int len;
+
char *inname;
        int mkgzip;
+
char buf[4096];
 +
int len;
 +
int mkgzip;
  
        /* First read the input magic number. */
+
if (argc == 1) {
        len = fread(buf, 1, 2, stdin);
+
infile = stdin;
        if (len < 0) {
+
inname = "stdin";
                perror("stdin");
+
} else if (argc == 2) {
                return 1;
+
inname = argv[1];
        }
+
infile = fopen(inname, "r");
        if (len != 2) {
+
if (infile == NULL) {
                fprintf(stderr, "stdin: not in compress -s or gzip format\n");
+
perror(inname);
                return 1;
+
exit(1);
        }
+
}
        if (buf[0] == magic_strong[0] && buf[1] == magic_strong[1])
+
} else {
                mkgzip = 1;
+
fprintf(stderr, "usage: %s [infile]\n", argv[0]);
        else if (buf[0] == magic_gzip[0] && buf[1] == magic_gzip[1])
+
exit(1);
                mkgzip = 0;
+
}
        else {
 
                fprintf(stderr, "stdin: not in compress -s or gzip format\n");
 
                return 1;
 
        }
 
  
        /* Now read and check the gzip header if necessary. */
+
/* First read the input magic number. */
        if (!mkgzip) {
+
len = fread(buf, 1, 2, infile);
                len = fread(&gzheader, sizeof(struct gzheader), 1, stdin);
+
if (len < 0) {
                if (len < 0) {
+
perror(inname);
                        perror("stdin");
+
exit(1);
                        return 1;
+
}
                }
+
if (len != 2) {
                if (len != 1) {
+
fprintf(stderr, "%s: not in compress -s or gzip format\n",
                        fprintf(stderr, "stdin: invalid gzip header\n");
+
inname);
                        return 1;
+
exit(1);
                }
+
}
                if (gzheader.cm != CM_DEFLATE || gzheader.flg & FRSVD) {
+
if (buf[0] == magic_strong[0] && buf[1] == magic_strong[1])
                        fprintf(stderr, "stdin: invalid gzip header\n");
+
mkgzip = 1;
                        return 1;
+
else if (buf[0] == magic_gzip[0] && buf[1] == magic_gzip[1])
                }
+
mkgzip = 0;
                if (gzheader.flg & FEXTRA) {
+
else {
                        int count;
+
fprintf(stderr, "%s: not in compress -s or gzip format\n",
 +
inname);
 +
exit(1);
 +
}
  
                        count = getchar();
+
/* Now read and check the gzip header if necessary. */
                        if (ferror(stdin)) {
+
if (!mkgzip) {
                                perror("stdin");
+
len = fread(&gzheader, sizeof(struct gzheader), 1, infile);
                                return 1;
+
if (len < 0) {
                        }
+
perror(inname);
                        if (feof(stdin)) {
+
exit(1);
                                fprintf(stderr, "stdin: invalid gzip header\n");
+
}
 +
if (len != 1) {
 +
fprintf(stderr, "%s: invalid gzip header\n", inname);
 +
exit(1);
 +
}
 +
if (gzheader.cm != CM_DEFLATE || gzheader.flg & FRSVD) {
 +
fprintf(stderr, "%s: invalid gzip header\n", inname);
 +
exit(1);
 +
}
 +
if (gzheader.flg & FEXTRA) {
 +
int count;
  
                                return 1;
+
count = getc(infile);
                        }
+
if (ferror(infile)) {
                        while (count) {
+
perror(inname);
                                getchar();
+
exit(1);
                                if (ferror(stdin)) {
+
}
                                        perror("stdin");
+
if (feof(infile)) {
                                        return 1;
+
fprintf(stderr, "%s: invalid gzip header\n",
                                }
+
inname);
                                if (feof(stdin)) {
+
exit(1);
                                        fprintf(stderr, "stdin: invalid gzip header\n");
+
}
                                        return 1;
+
while (count) {
                                }
+
getc(infile);
                                count--;
+
if (ferror(infile)) {
                        }
+
perror(inname);
                }
+
exit(1);
                if (gzheader.flg & FNAME) {
+
}
                        int ch;
+
if (feof(infile)) {
 +
fprintf(stderr, "%s: invalid gzip header\n", inname);
 +
exit(1);
 +
}
 +
count--;
 +
}
 +
}
 +
if (gzheader.flg & FNAME) {
 +
int ch;
  
                        do {
+
do {
                                ch = getchar();
+
ch = getc(infile);
                                if (ferror(stdin)) {
+
if (ferror(infile)) {
                                        perror("stdin");
+
perror(inname);
                                        return 1;
+
exit(1);
                                }
+
}
                                if (feof(stdin)) {
+
if (feof(infile)) {
                                        fprintf(stderr, "stdin: invalid gzip header\n");
+
fprintf(stderr, "%s: invalid gzip header\n", inname);
                                        return 1;
+
exit(1);
                                }
+
}
                        }
+
}
                        while (ch);
+
while (ch);
                }
+
}
                if (gzheader.flg & FCOMMENT) {
+
if (gzheader.flg & FCOMMENT) {
                        int ch;
+
int ch;
  
                        do {
+
do {
                                ch = getchar();
+
ch = getc(infile);
                                if (ferror(stdin)) {
+
if (ferror(infile)) {
                                        perror("stdin");
+
perror(inname);
                                        return 1;
+
exit(1);
                                }
+
}
                                if (feof(stdin)) {
+
if (feof(infile)) {
                                        fprintf(stderr, "stdin: invalid gzip header\n");
+
fprintf(stderr, "%s: invalid gzip header\n", inname);
                                        return 1;
+
exit(1);
                                }
+
}
                        }
+
}
                        while (ch);
+
while (ch);
                }
+
}
                if (gzheader.flg & FHCRC) {
+
if (gzheader.flg & FHCRC) {
                        len = fread(buf, 1, 2, stdin);
+
len = fread(buf, 1, 2, infile);
                        if (len < 0) {
+
if (len < 0) {
                                perror("stdin");
+
perror(inname);
                                return 1;
+
exit(1);
                        }
+
}
                        if (len != 2) {
+
if (len != 2) {
                                fprintf(stderr, "stdin: invalid gzip header\n");
+
fprintf(stderr, "%s: invalid gzip header\n",
 +
inname);
 +
exit(1);
 +
}
 +
}
 +
}
  
                                return 1;
+
/* Now write the output magic number. */
                        }
+
if (mkgzip) {
                }
+
if (fwrite(magic_gzip, 1, 2, stdout) != 2) {
        }
+
perror("stdout");
 +
exit(1);
 +
}
 +
}
 +
else {
 +
if (fwrite(magic_strong, 1, 2, stdout) != 2) {
 +
perror("stdout");
 +
exit(1);
 +
}
 +
}
  
        /* Now write the output magic number. */
+
/* Now write the gzip header if necessary. */
        if (mkgzip) {
+
if (mkgzip) {
                if (fwrite(magic_gzip, 1, 2, stdout) != 2) {
+
gzheader.cm = CM_DEFLATE;
                        perror("stdout");
+
gzheader.flg = 0;
                        return 1;
+
gzheader.mtime[0] = 0;
                }
+
gzheader.mtime[1] = 0;
        }
+
gzheader.mtime[2] = 0;
        else {
+
gzheader.mtime[3] = 0;
                if (fwrite(magic_strong, 1, 2, stdout) != 2) {
+
gzheader.xfl = 0;
                        perror("stdout");
+
gzheader.os = OS_UNIX;
                        return 1;
+
if (fwrite(&gzheader, sizeof(struct gzheader), 1, stdout) != 1) {
                }
+
perror("stdout");
        }
+
exit(1);
 +
}
 +
}
  
        /* Now write the gzip header if necessary. */
+
/* Now actually copy the data! */
        if (mkgzip) {
+
for (;;) {
                gzheader.cm = CM_DEFLATE;
+
len = fread(buf, 1, sizeof(buf), infile);
                gzheader.flg = 0;
+
if (len < 0) {
                gzheader.mtime[0] = 0;
+
perror(inname);
                gzheader.mtime[1] = 0;
+
exit(1);
                gzheader.mtime[2] = 0;
+
}
                gzheader.mtime[3] = 0;
+
if (len == 0)
                gzheader.xfl = 0;
+
break;
                gzheader.os = OS_UNIX;
+
if (fwrite(buf, 1, len, stdout) != len) {
                if (fwrite(&gzheader, sizeof(struct gzheader), 1, stdout) != 1)
+
perror("stdout");
{
+
exit(1);
                        perror("stdout");
+
}
                        return 1;
+
}
                }
 
        }
 
 
 
        /* Now actually copy the data! */
 
        for (;;) {
 
                len = fread(buf, 1, sizeof(buf), stdin);
 
                if (len < 0) {
 
                        perror("stdin");
 
                        return 1;
 
                }
 
                if (len == 0)
 
                        break;
 
                if (fwrite(buf, 1, len, stdout) != len) {
 
                        perror("stdout");
 
                        return 1;
 
                }
 
        }
 
  
        /* I can't believe we're done! */
+
/* I can't believe we're done! */
        return 0;
+
if (argc == 2)
 +
fclose(infile);
 +
return(0);
 
}
 
}
 
</pre>
 
</pre>

Revision as of 03:16, 16 July 2019

gzcompat is a utility to convert from Sokolov compression into gzip. The primary use is to convert the Quasijarus install files into gzip, so that they can easily be decompressed. This is for the 4.3 BSD Quasijarus Operating System.

source code

#ifndef lint
static char sccsid[] = "@(#)gzcompat.c	5.2 (Berkeley) 12/21/00";
#endif

/* gzcompat converts between compress -s and gzip formats. */

#include <stdio.h>

char magic_strong[2] = {037, 0241};
char magic_gzip[2]   = {037, 0213};

struct gzheader {
	unsigned char cm;
	unsigned char flg;
	unsigned char mtime[4];
	unsigned char xfl;
	unsigned char os;
} gzheader;

#define CM_DEFLATE 0x08

#define FTEXT    0x01
#define FHCRC    0x02
#define FEXTRA   0x04
#define FNAME    0x08
#define FCOMMENT 0x10
#define FRSVD    0xE0

#define OS_UNIX 0x03

int main(argc, argv)
	char **argv;
{
	FILE *infile;
	char *inname;
	char buf[4096];
	int len;
	int mkgzip;

	if (argc == 1) {
		infile = stdin;
		inname = "stdin";
	} else if (argc == 2) {
		inname = argv[1];
		infile = fopen(inname, "r");
		if (infile == NULL) {
			perror(inname);
			exit(1);
		}
	} else {
		fprintf(stderr, "usage: %s [infile]\n", argv[0]);
		exit(1);
	}

	/* First read the input magic number. */
	len = fread(buf, 1, 2, infile);
	if (len < 0) {
		perror(inname);
		exit(1);
	}
	if (len != 2) {
		fprintf(stderr, "%s: not in compress -s or gzip format\n",
			inname);
		exit(1);
	}
	if (buf[0] == magic_strong[0] && buf[1] == magic_strong[1])
		mkgzip = 1;
	else if (buf[0] == magic_gzip[0] && buf[1] == magic_gzip[1])
		mkgzip = 0;
	else {
		fprintf(stderr, "%s: not in compress -s or gzip format\n",
			inname);
		exit(1);
	}

	/* Now read and check the gzip header if necessary. */
	if (!mkgzip) {
		len = fread(&gzheader, sizeof(struct gzheader), 1, infile);
		if (len < 0) {
			perror(inname);
			exit(1);
		}
		if (len != 1) {
			fprintf(stderr, "%s: invalid gzip header\n", inname);
			exit(1);
		}
		if (gzheader.cm != CM_DEFLATE || gzheader.flg & FRSVD) {
			fprintf(stderr, "%s: invalid gzip header\n", inname);
			exit(1);
		}
		if (gzheader.flg & FEXTRA) {
			int count;

			count = getc(infile);
			if (ferror(infile)) {
				perror(inname);
				exit(1);
			}
			if (feof(infile)) {
				fprintf(stderr, "%s: invalid gzip header\n",
					inname);
				exit(1);
			}
			while (count) {
				getc(infile);
				if (ferror(infile)) {
					perror(inname);
					exit(1);
				}
				if (feof(infile)) {
					fprintf(stderr, "%s: invalid gzip header\n", inname);
					exit(1);
				}
				count--;
			}
		}
		if (gzheader.flg & FNAME) {
			int ch;

			do {
				ch = getc(infile);
				if (ferror(infile)) {
					perror(inname);
					exit(1);
				}
				if (feof(infile)) {
					fprintf(stderr, "%s: invalid gzip header\n", inname);
					exit(1);
				}
			}
			while (ch);
		}
		if (gzheader.flg & FCOMMENT) {
			int ch;

			do {
				ch = getc(infile);
				if (ferror(infile)) {
					perror(inname);
					exit(1);
				}
				if (feof(infile)) {
					fprintf(stderr, "%s: invalid gzip header\n", inname);
					exit(1);
				}
			}
			while (ch);
		}
		if (gzheader.flg & FHCRC) {
			len = fread(buf, 1, 2, infile);
			if (len < 0) {
				perror(inname);
				exit(1);
			}
			if (len != 2) {
				fprintf(stderr, "%s: invalid gzip header\n",
					inname);
				exit(1);
			}
		}
	}

	/* Now write the output magic number. */
	if (mkgzip) {
		if (fwrite(magic_gzip, 1, 2, stdout) != 2) {
			perror("stdout");
			exit(1);
		}
	}
	else {
		if (fwrite(magic_strong, 1, 2, stdout) != 2) {
			perror("stdout");
			exit(1);
		}
	}

	/* Now write the gzip header if necessary. */
	if (mkgzip) {
		gzheader.cm = CM_DEFLATE;
		gzheader.flg = 0;
		gzheader.mtime[0] = 0;
		gzheader.mtime[1] = 0;
		gzheader.mtime[2] = 0;
		gzheader.mtime[3] = 0;
		gzheader.xfl = 0;
		gzheader.os = OS_UNIX;
		if (fwrite(&gzheader, sizeof(struct gzheader), 1, stdout) != 1) {
			perror("stdout");
			exit(1);
		}
	}

	/* Now actually copy the data! */
	for (;;) {
		len = fread(buf, 1, sizeof(buf), infile);
		if (len < 0) {
			perror(inname);
			exit(1);
		}
		if (len == 0)
			break;
		if (fwrite(buf, 1, len, stdout) != len) {
			perror("stdout");
			exit(1);
		}
	}

	/* I can't believe we're done! */
	if (argc == 2)
		fclose(infile);
	return(0);
}