diff options
author | aiju <devnull@localhost> | 2014-09-30 13:07:36 +0200 |
---|---|---|
committer | aiju <devnull@localhost> | 2014-09-30 13:07:36 +0200 |
commit | 4f264cedfbb6a10a9a471c8a11b2a80489e4287f (patch) | |
tree | f5fa7dc76d6c7341d475f3de18c0c7312bd0a27f | |
parent | 4f4d71b941e568b158da73978e25105b94afd7fd (diff) |
added crc32
-rw-r--r-- | sys/src/cmd/crc32.c | 99 | ||||
-rw-r--r-- | sys/src/games/gba/gba.c | 4 | ||||
-rw-r--r-- | sys/src/games/snes/snes.c | 1 |
3 files changed, 102 insertions, 2 deletions
diff --git a/sys/src/cmd/crc32.c b/sys/src/cmd/crc32.c new file mode 100644 index 000000000..cc3f5f9f8 --- /dev/null +++ b/sys/src/cmd/crc32.c @@ -0,0 +1,99 @@ +#include <u.h> +#include <libc.h> +#include <libsec.h> + +u32int table[256]; +u32int xor = -1, init, poly = 0xedb88320; +char *error; + +static void +dotable(void) +{ + u32int c; + int n, k; + + for(n = 0; n < 256; n++){ + c = n; + for(k = 0; k < 8; k++) + if((c & 1) != 0) + c = poly ^ c >> 1; + else + c >>= 1; + table[n] = c; + } +} + +static u32int +calc(u32int init, uchar *buf, ulong len) +{ + u32int c; + + c = init; + while(len-- != 0) + c = table[(*buf++ ^ c) & 0xff] ^ c >> 8; + return c; +} + +static void +sum(int fd, char *name) +{ + int n; + uchar buf[8192]; + u32int crc; + + crc = init ^ xor; + while((n = read(fd, buf, sizeof buf)) > 0) + crc = calc(crc, buf, n); + if(n < 0){ + fprint(2, "reading %s: %r\n", name ? name : "stdin"); + error = "read"; + return; + } + crc ^= xor; + if(name == nil) + print("%ux\n", crc); + else + print("%ux\t%s\n", crc, name); +} + +void +usage(void) +{ + fprint(2, "usage: %s [-x xorval] [-i initial] [-p poly] [file...]\n", argv0); + exits("usage"); +} + +void +main(int argc, char *argv[]) +{ + int i, fd; + + ARGBEGIN{ + case 'x': + xor = strtol(EARGF(usage()), 0, 0); + break; + case 'i': + init = strtol(EARGF(usage()), 0, 0); + break; + case 'p': + poly = strtol(EARGF(usage()), 0, 0); + break; + default: + usage(); + }ARGEND + + dotable(); + if(argc == 0) + sum(0, nil); + else for(i = 0; i < argc; i++){ + fd = open(argv[i], OREAD); + if(fd < 0){ + fprint(2, "crc32: can't open %s: %r\n", argv[i]); + error = "open"; + continue; + } + sum(fd, argv[i]); + close(fd); + } + exits(error); +} diff --git a/sys/src/games/gba/gba.c b/sys/src/games/gba/gba.c index dd417f432..d7b889ff9 100644 --- a/sys/src/games/gba/gba.c +++ b/sys/src/games/gba/gba.c @@ -198,8 +198,8 @@ loadrom(char *file) if(fd < 0) sysfatal("open: %r"); sz = seek(fd, 0, 2); - if(sz <= 0 || sz >= 32*1024*1024) - sysfatal("nope.jpg"); + if(sz <= 0 || sz > 32*1024*1024) + sysfatal("invalid file size"); seek(fd, 0, 0); nrom = sz; rom = emalloc(nrom); diff --git a/sys/src/games/snes/snes.c b/sys/src/games/snes/snes.c index 9cd273200..ebb99d714 100644 --- a/sys/src/games/snes/snes.c +++ b/sys/src/games/snes/snes.c @@ -95,6 +95,7 @@ loadbat(char *file) char *s; if(battery && nsram != 0){ + buf[sizeof buf - 1] = 0; strncpy(buf, file, sizeof buf - 5); s = buf + strlen(buf) - 4; if(s < buf || strcmp(s, ".smc") != 0) |