cryptutils

Cryptographic Software Utilities
git clone http://git.omkov.net/cryptutils
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2021-02-15 12:38:55
Commit174660fc18f20c41d5f472d695cf714aa5ca78c4
Parent595bd6b4b1ce42ed8dba1c5e2ac092a0cbedd6c6

crc32: Use BSD or GNU style CRCs

Use the BSD or GNU checksum style when printing CRC values.

Diffstat

M man/crc32.1 | 13 ++++++++-----
M src/crc32.c | 49 ++++++++++++++++++++++++++++++-------------------

2 files changed, 38 insertions, 24 deletions

diff --git a/man/crc32.1 b/man/crc32.1
index 70d1730..d419f5b 100644
--- a/man/crc32.1
+++ b/man/crc32.1
@@ -1,13 +1,16 @@
-.TH CRC32 1 2020-08-14 "OMKOV cryptutils" "General Commands Manual"
+.TH CRC32 1 2021-02-16 "OMKOV cryptutils" "General Commands Manual"
 .SH NAME
 crc32 \(em compute the CRC-32 for a file
 .SH SYNOPSYS
 \fBcrc32\fR [\fIfile\fR...]
 .SH DESCRIPTION
-Compute and print a CRC-32 for each input file, as well as the file size in
-bytes.
+Compute and print a CRC-32 for each input file.
 .SH OPTIONS
 The following options are supported:
+.IP "\fB--bsd\fR" 8
+Use BSD style CRCs.
+.IP "\fB--gnu\fR" 8
+Use GNU style CRCs.
 .IP "\fB--help\fR" 8
 Display help information.
 .IP "\fB--version\fR" 8
@@ -15,8 +18,8 @@ Display version information.
 .SH OPERANDS
 The following operand is supported:
 .IP "\fIfile\fR" 8
-A pathname of an input file. If no \fIfile\fR operands are specified, or
-\fIfile\fR is a '\fB-\fR', \fIcrc32\fR will read from standard input.
+A pathname of an input file. If no \fIfile\fR operands are specified then
+\fIcrc32\fR will read from standard input.
 .SH EXIT STATUS
 The following exit values will be returned:
 .IP "\ 0" 8
diff --git a/src/crc32.c b/src/crc32.c
index 0937f2f..e89032a 100644
--- a/src/crc32.c
+++ b/src/crc32.c
@@ -1,4 +1,4 @@
-// crc32.c, version 1.0.6
+// crc32.c, version 1.1.0
 // OMKOV cryptutils crc32
 // Copyright (C) 2020, Jakob Wakeling
 // All rights reserved.
@@ -37,67 +37,78 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 #include <stdint.h>
 #include <stdio.h>
 
-#define VERSION "1.0.6"
+#define VERSION "1.1.0"
 
 static struct lop lops[] = {
 	{ "help",    ARG_NUL, 256 },
 	{ "version", ARG_NUL, 257 },
+	{ "bsd",     ARG_NUL, 258 },
+	{ "gnu",     ARG_NUL, 259 },
 	{ NULL, 0, 0 }
 };
 
 static const uint32_t CRC32[];
 
-static inline int crc32(const char *file);
+static bool qflag;
+static int form;
+
+static inline void crc32(const char *file);
 
 static void hlp(void);
 static void ver(void);
 
 int main(int ac, char *av[]) { A0 = av[0];
-	struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops;
+	struct opt opt = OPTGET_INIT; opt.str = "q"; opt.lops = lops;
 	for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) {
+	case 'q': { qflag = true; break; }
 	case 256: { hlp(); return 0; }
 	case 257: { ver(); return 0; }
+	case 258: { form = 1; break; }
+	case 259: { form = 0; break; }
 	default: { return 1; }
 	}
 
-	bool warned = false;
-	
 	if (opt.ind == ac) { crc32(NULL); }
-	else for (char **p = &av[opt.ind]; *p; ++p) if (crc32(*p)) {
-		warn("%s: %s", *p, serr()); warned = true;
-	}
+	else for (char **p = &av[opt.ind]; *p; ++p) { crc32(*p); }
 
 	return warned;
 }
 
-static inline int crc32(const char *file) {
-	uint8_t buf[BUFSIZ * 16]; FILE *fi;
-	register size_t fl = 0; register uint32_t crc = (uint32_t)~0;
+static inline void crc32(const char *file) {
+	uint8_t buf[BUFSIZ * 16]; FILE *fi; register uint32_t crc = (uint32_t)~0;
 
-	if (!file || (file[0] == '-' && file[1] == 0)) { fi = stdin; }
-	else if (!(fi = fopen(file, "r"))) { return 1; }
+	if (!file) { fi = stdin; } else { fi = fopen(file, "rb"); }
+	if (!fi) { warn("%s: %s", file, serr()); return; }
 
-	for (size_t c; (c = fread(buf, 1, sizeof (buf), fi)); fl += c) {
+	for (size_t c; (c = fread(buf, 1, sizeof (buf), fi));) {
 		for (register size_t i = 0; i != c; ++i) {
-			crc = (crc >> 8) ^ CRC32[(crc ^ (unsigned int)buf[i]) & 0xFF];
+			crc = (crc >> 8) ^ CRC32[(crc ^ (uint32_t)buf[i]) & 0xFF];
 		}
 	} crc = ~crc;
 
-	printf("%X %zu", crc, fl);
-	if (file) { printf(" %s", file); } fputc('\n', stdout);
+	if (ferror(fi)) { fclose(fi); warn("%s: %s", file, serr()); return; }
+	
+	if (!file || qflag) { printf("%08X\n", crc); } else switch (form) {
+	case 0: { printf("%08X *%s\n", crc, file); break; }
+	case 1: { printf("CRC32 (%s) = %08X\n", file, crc); break; }
+	}
 
-	if (fi != stdin) { fclose(fi); } return 0;
+	if (fi != stdin) { fclose(fi); } return;
 }
 
+/* Print help information */
 static void hlp(void) {
 	puts("crc32 - compute the CRC-32 for a file\n");
 	puts("usage: crc32 [file...]\n");
 	puts("options:");
+	puts("  --bsd      Use BSD style CRCs");
+	puts("  --gnu      Use GNU style CRCs");
 	puts("  --help     Display help information");
 	puts("  --version  Display version information");
 	return;
 }
 
+/* Print version information */
 static void ver(void) {
 	puts("OMKOV cryptutils crc32, version " VERSION);
 	puts("Copyright (C) 2020, Jakob Wakeling");