commit 9059a38bbbf2014ba005277214f2eb3788817485 Author: Unbewohnte Date: Sun Apr 9 18:40:33 2023 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c22858a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +testfile +corrupt \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7e6b19d --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0932666 --- /dev/null +++ b/Makefile @@ -0,0 +1,11 @@ +all: + gcc -Wall -Werror -O2 -static corrupt.c -o corrupt + +clean: + rm -f corrupt + +install: all + mv corrupt /usr/local/bin/ + +uninstall: + rm -f /usr/local/bin/corrupt \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..674e6a8 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# corrupt +## a utility to corrupt FILE streams without changes in file size. + +### Usage + +`corrupt -[hvm] FILE...` + +Flags + +- `-h` to see the relevant usage information +- `-v` to get version information +- `-m METHOD (bit)` to specify corruption method (MORE TO BE ADDED) + +### Examples + +- `corrupt testfile.txt` - corrupt `testfile.txt` with default corruption method +- `corrupt -m bit testfile.txt` - corrupt `testfile.txt` with `bit` corruption method + +### Build + +Simply `make` or if you don't have it - compile `corrupt.c` with the compiler of choice. The only dependency is the () standart library + +### License + +MIT \ No newline at end of file diff --git a/corrupt.c b/corrupt.c new file mode 100644 index 0000000..4ebcb79 --- /dev/null +++ b/corrupt.c @@ -0,0 +1,143 @@ +/* +The MIT License (MIT) + +Copyright © 2023 Kasyanov Nikolay Alexeyevich (Unbewohnte) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +*/ + +/* +corrupt.c - a utility to corrupt FILE streams +*/ + +#include +#include +#include + +// Corruption method. TODO(add more) +typedef enum Corruption { + CORRUPTION_BITMAGIC, // Corrupt each byte via bit manipulation +} Corruption; + +// Possible funcion errors +typedef enum CorruptError { + SUCCESS, + ERR_NULLPTR, +} CorruptError; + + +// Corrupts byte via bit manipulation +int corrupt_char(int byte) { + static int last; + int corrupted_byte = ((byte << 1) | last) + (byte ^ last); + last = byte; + return corrupted_byte; +} + +/* +Corrupts the file with specified corruption method. Returns 0 if +successful, 1 - if FILE is NULL. +*/ +CorruptError corrupt(FILE* file, Corruption corruption) { + if (!file) { + return ERR_NULLPTR; + } else if (feof(file)) { + rewind(file); + } + + int character; + while ((character = fgetc(file)) != EOF) { + fseek(file, -1, SEEK_CUR); + + if (corruption == CORRUPTION_BITMAGIC) { + fputc(corrupt_char(character), file); + } + } + + return SUCCESS; +} + +int main(int argc, char** argv) { + char* file_paths[argc-1]; + unsigned int file_path_index = 0; + FILE* file = NULL; + Corruption corruption_method = CORRUPTION_BITMAGIC; + CorruptError result; + + unsigned int i = 1; + while (i < argc) { + // handle specified flags + if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { + fprintf( + stdout, + "corrupt -[hvm] FILE...\n\n-h --help -> Print this message and exit\n-v --version -> Print version information and exit\n-m --method METHOD (bit) -> Specify corruption method\n" + ); + return SUCCESS; + } else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) { + fprintf( + stdout, + "corrupt v0.1.0 - a utility to corrupt FILE streams\n(c) Kasyanov Nikolay Alexeyevich (Unbewohnte)\n" + ); + return SUCCESS; + } else if (strcmp(argv[i], "-m") == 0 || strcmp(argv[i], "--method") == 0) { + i++; + if (strcmp(argv[i], "bit") == 0) { + corruption_method = CORRUPTION_BITMAGIC; + } else { + corruption_method = CORRUPTION_BITMAGIC; + fprintf( + stdout, + "Invalid corruption method \"%s\". Defaulting to \"bit\"\n", + argv[i] + ); + } + } else { + // remember this path + file_paths[file_path_index] = argv[i]; + file_path_index++; + } + + i++; + } + + if (argc == 1 || file_path_index == 0) { + fprintf(stdout, "No FILEs were specified. Run \"corrupt -h\" to get help\n"); + return SUCCESS; + } + + for (unsigned int i = 0; i < file_path_index; i++) { + // open stream + file = fopen(file_paths[i], "r+"); + + fprintf( + stdout, + "Corrupting \"%s\" ... ", + file_paths[i] + ); + + // corrupt the whole stream + result = corrupt(file, corruption_method); + if (result == ERR_NULLPTR) { + fprintf( + stdout, + "error: %s\n", + strerror(errno) + ); + continue; + } + + // close stream + fclose(file); + + fprintf( + stdout, + "done\n" + ); + } + + return SUCCESS; +} \ No newline at end of file