From 3141392cc1c2fa32851893236d8b0a8656cbe998 Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Sun, 12 Nov 2023 05:07:07 -0800 Subject: [PATCH] Fixes in 1998/tomtorfs, add try.sh & format fixes The author stated that the program assumes EOF is -1 but for systems that do not have this it will fail. This is indeed a valid concern as the standard mandates that it's a value < 0 but it doesn't require it to be -1. The author also stated that the program could fail if 1 is not a return value that indicates error so this was changed to EXIT_FAILURE as the suggested was made. It is possible of course that I missed a place where this should have been updated but I don't believe so. I am unaware of any system where this would be a problem and I don't know why it matters anyway but I made the change as the author stated it could be a problem with portability. The README.md file has been formatted and typo checked. Added try.sh script. This should complete this entry's review. --- 1998/tomtorfs/README.md | 53 ++++++++++++++++++++++++++-------------- 1998/tomtorfs/tomtorfs.c | 5 ++-- 1998/tomtorfs/try.sh | 23 +++++++++++++++++ thanks-for-fixes.md | 14 +++++++++++ 4 files changed, 74 insertions(+), 21 deletions(-) create mode 100755 1998/tomtorfs/try.sh diff --git a/1998/tomtorfs/README.md b/1998/tomtorfs/README.md index df679a159..63d64db21 100644 --- a/1998/tomtorfs/README.md +++ b/1998/tomtorfs/README.md @@ -8,26 +8,31 @@ make all ## To use: ```sh -./tomtorfs tomtorfs.c 32 04C11DB7 1 FFFFFFFF FFFFFFFF +./tomtorfs filename bitwidth polynom reflected init xor ``` +where: + +``` +filename : file to calculate the CRC of +bitwidth : width of the CRC in bits (decimal) +polynom : polynomial used for the CRC (hexadecimal) +reflected : 0 if the CRC is not reflected, 1 if it is +init : initial value for the CRC (hexadecimal) +xor : value to xor the final CRC with (hexadecimal) +``` ### Try: ```sh -echo "abscond conferrable" > file.A -echo "adorn condolence" > file.B -echo "adorn condolence too" > file.C -./tomtorfs file.A 32 04C11DB7 1 FFFFFFFF FFFFFFFF -./tomtorfs file.B 32 04C11DB7 1 FFFFFFFF FFFFFFFF -./tomtorfs file.C 32 04C11DB7 1 FFFFFFFF FFFFFFFF +./try.sh ``` ## Judges' remarks: If you don't believe the program is correct, try and compare the output -of the program with the value of CRC computed by PKZIP or any other +of the program with the value of CRCs computed by PKZIP or any other archiver that uses the CRC-32 algorithm. @@ -47,12 +52,14 @@ xor : value to xor the final CRC with (hexadecimal) ### Examples + #### CRC-16: ```sh ./tomtorfs filename 16 1021 0 FFFF 0000 ``` + #### CRC-32: @@ -60,6 +67,7 @@ xor : value to xor the final CRC with (hexadecimal) ./tomtorfs filename 32 04C11DB7 1 FFFFFFFF FFFFFFFF ``` + #### XMODEM: ```sh @@ -67,12 +75,14 @@ xor : value to xor the final CRC with (hexadecimal) ``` + #### ARC: ```sh ./tomtorfs filename 16 8005 1 0000 0000 ``` + ### Portability notes The program assumes `CHAR_BIT==8`. Results are undefined if `bitwidth > @@ -82,11 +92,13 @@ valid exit value to indicate failure (replace them with `EOF` resp. No special build options required. + ### Special notes Count the number of non-whitespace characters in the source file to find out what dark force drove me to write this ;-). + ### Spoiler First of all, if you run a C beautifier on the program (thereby of course losing @@ -99,26 +111,29 @@ following meanings: ``` a = argc A = argv + b = array of unsigned long variables - b[0] = bit-width of the CRC (argv[2]) - b[1] = CRC polynomial (argv[3]) - b[2] = nonzero if CRC is reflected (argv[4]) - b[3] = initial value of CRC (argv[5]) - b[4] = value final CRC is XORed with (argv[6]) - b[5] = CRC value - b[6] = byte from file - b[7] = counter + b[0] = bit-width of the CRC (argv[2]) + b[1] = CRC polynomial (argv[3]) + b[2] = nonzero if CRC is reflected (argv[4]) + b[3] = initial value of CRC (argv[5]) + b[4] = value final CRC is XORed with (argv[6]) + b[5] = CRC value + b[6] = byte from file + b[7] = counter + B = FILE * through which the file argv[1] is opened + C = typedef for unsigned long ``` -The first for loop simply reads the command-line arguments -in the `b[]` array. The third parameter to `strtoul()` causes +The first `for` loop simply reads the command-line arguments +in the `b[]` array. The third parameter to `strtoul(3)` causes all arguments to be in base-16 (hex) except the first one, which will be in base-10 (decimal). The rest of the program "simply" calculates the CRC in `b[5]` -and then prints it to stdout. The bit reflection loops may +and then prints it to `stdout`. The bit reflection loops may appear a bit tricky at first, but if you have a good look at them they should become obvious also. diff --git a/1998/tomtorfs/tomtorfs.c b/1998/tomtorfs/tomtorfs.c index c48a2cf08..b93e6a74f 100644 --- a/1998/tomtorfs/tomtorfs.c +++ b/1998/tomtorfs/tomtorfs.c @@ -1,11 +1,12 @@ #include #include +#define exit(a) return EXIT_FAILURE int main(int a,char **A){FILE*B;typedef unsigned long C;C b -[8]; if(!(a==7&&(B= fopen(1[A],"rb")))) return 1;for(7[b]=0 +[8]; if(!(a==7&&(B= fopen(1[A],"rb")))) exit(1); for(7[b]=0 ;7[b]<5;7[b]++)b[7[ b]]=strtoul(A[2+7[b ]],0,16-!7[b]*6);5[ b]=3[b] ; while ((6[b]= getc(B) -)!=(C)- 1){if(2 [b])for (7[b]=0 +) !=EOF ){if(2 [b])for (7[b]=0 ;7[b]<4 ;7[b]++ )if(((6 [b]>>7[ b])^(6[ b]>>(7-7[b])))&1)6[ b] ^=(1 <<7[b]) ^(1<<(7-7[b]));5[b] ^= 6[b] diff --git a/1998/tomtorfs/try.sh b/1998/tomtorfs/try.sh new file mode 100755 index 000000000..b3189d7c0 --- /dev/null +++ b/1998/tomtorfs/try.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +# +# script to demonstrate 1998/tomtorfs +# + +rm -f file.A file.B file.C + +echo "$ echo abscond conferrable > file.A" 1>&2 +echo "abscond conferrable" > file.A +echo "$ ./tomtorfs file.A 32 04C11DB7 1 FFFFFFFF FFFFFFFF" 1>&2 +./tomtorfs file.A 32 04C11DB7 1 FFFFFFFF FFFFFFFF + +echo "$ echo adorn condolence > file.B" 1>&2 +echo "adorn condolence" > file.B +echo "$ ./tomtorfs file.B 32 04C11DB7 1 FFFFFFFF FFFFFFFF" 1>&2 +./tomtorfs file.B 32 04C11DB7 1 FFFFFFFF FFFFFFFF + +echo "$ echo adorn condolence too > file.C" 1>&2 +echo "adorn condolence too" > file.C +echo "$ ./tomtorfs file.C 32 04C11DB7 1 FFFFFFFF FFFFFFFF" 1>&2 +./tomtorfs file.C 32 04C11DB7 1 FFFFFFFF FFFFFFFF + +rm -f file.A file.B file.C diff --git a/thanks-for-fixes.md b/thanks-for-fixes.md index 290a35864..ff3fee464 100644 --- a/thanks-for-fixes.md +++ b/thanks-for-fixes.md @@ -2167,6 +2167,20 @@ There actually is a web page for the tool and this was added to the author information for the entry. It has not been added to any JSON file. +## [1998/tomtorfs](1998/tomtorfs/tomtorfs.c) ([README.md](1998/tomtorfs/README.md])) + +Cody fixed the assumption that `EOF` is `-1` (the author noted that it assumes +it is `-1` but the standard only guarantees that it's a value `< 0`) and that 1 +is a valid code to return failure (it seems unlikely that it wouldn't be but +since the author suggested it it was changed to `EXIT_FAILURE`). To make it so +the lines end at the same columns as the original the `EXIT_FAILURE` change was +done by redefining `exit(3)` to be `exit(a) return EXIT_FAILURE` and `1` was +passed to it (`return` was used because the original program had `return 1`). + +Cody also added the [try.sh](1998/tomtorfs/try.sh) script to try out a few +commands that we recommended. + + ## [2000/anderson](2000/anderson/anderson.c) ([README.md](2000/anderson/README.md])) Cody changed this entry to use `fgets()` instead of `gets()` to make it safer