diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile | 8 | ||||
| -rw-r--r-- | src/encryption.c | 51 | ||||
| -rw-r--r-- | src/encryption.h | 7 | ||||
| -rw-r--r-- | src/main.c | 55 | ||||
| -rw-r--r-- | src/shared.c | 42 | ||||
| -rw-r--r-- | src/shared.h | 18 |
6 files changed, 119 insertions, 62 deletions
diff --git a/src/Makefile b/src/Makefile index d3798bb..a97848d 100644 --- a/src/Makefile +++ b/src/Makefile | |||
| @@ -3,15 +3,15 @@ SHELL = /usr/bin/bash | |||
| 3 | 3 | ||
| 4 | DEBUG_CFLAGS := -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -Og | 4 | DEBUG_CFLAGS := -fanalyzer -Wanalyzer-too-complex -ggdb -g3 -Og |
| 5 | RELEASE_CFLAGS := -O3 -fipa-pta -fipa-cp -fuse-linker-plugin -flto=auto | 5 | RELEASE_CFLAGS := -O3 -fipa-pta -fipa-cp -fuse-linker-plugin -flto=auto |
| 6 | CFLAGS = -Wall -Wextra -Wpedantic -pedantic-errors $(DEBUG_CFLAGS) | 6 | CFLAGS = -Wall -Wextra -Wpedantic -pedantic-errors $(DEBUG_CFLAGS) $$(pkg-config --cflags libsodium) |
| 7 | 7 | ||
| 8 | DEBUG_LDLIBS := | 8 | DEBUG_LDLIBS := |
| 9 | RELEASE_LDLIBS := | 9 | RELEASE_LDLIBS := |
| 10 | LDLIBS += $(DEBUG_LDLIBS) | 10 | LDLIBS += $(DEBUG_LDLIBS) $$(pkg-config --libs-only-l libsodium) |
| 11 | 11 | ||
| 12 | DEBUG_LDFLAGS := | 12 | DEBUG_LDFLAGS := |
| 13 | RELEASE_LDFLAGS := -fuse-linker-plugin -flto=auto | 13 | RELEASE_LDFLAGS := -fuse-linker-plugin -flto=auto |
| 14 | LDFLAGS += $(DEBUG_LDFLAGS) | 14 | LDFLAGS += $(DEBUG_LDFLAGS) $$(pkg-config --libs-only-L libsodium) |
| 15 | 15 | ||
| 16 | 16 | ||
| 17 | BINARIES := main | 17 | BINARIES := main |
| @@ -25,5 +25,7 @@ main: main.o shared.o | |||
| 25 | main.o: main.c shared.h | 25 | main.o: main.c shared.h |
| 26 | shared.o: shared.c shared.h | 26 | shared.o: shared.c shared.h |
| 27 | 27 | ||
| 28 | encryption: encryption.c encryption.h shared.o shared.h | ||
| 29 | |||
| 28 | c clean: # huh, didn't think that would work | 30 | c clean: # huh, didn't think that would work |
| 29 | rm -rvf $(BINARIES) $(wildcard *.o) \ No newline at end of file | 31 | rm -rvf $(BINARIES) $(wildcard *.o) \ No newline at end of file |
diff --git a/src/encryption.c b/src/encryption.c new file mode 100644 index 0000000..e92e4e7 --- /dev/null +++ b/src/encryption.c | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | #include "encryption.h" | ||
| 2 | #include "shared.h" | ||
| 3 | |||
| 4 | #include <sodium.h> | ||
| 5 | |||
| 6 | #include <stdarg.h> | ||
| 7 | #include <errno.h> | ||
| 8 | #include <error.h> | ||
| 9 | #include <stdio.h> | ||
| 10 | |||
| 11 | int checkSodium(void) { | ||
| 12 | int ret = sodium_init(); | ||
| 13 | if(ret < 0) | ||
| 14 | error(1, ENOTSUP, "Couldn't initialize sodium for some reason. Quitting..."); | ||
| 15 | |||
| 16 | return ret; | ||
| 17 | } | ||
| 18 | |||
| 19 | // To encrypt: | ||
| 20 | // 1- Create a temp file with the correct name in the root folder of the partition being encrypted | ||
| 21 | // 1.1- Detect the partition and find the root folder | ||
| 22 | // 1.2- Create the temp file with the correct name | ||
| 23 | // 2- Encrypt the file's contents to the temp file | ||
| 24 | // 2.1- Open the file | ||
| 25 | // 2.2- Stream the file's contents into some encryption algo | ||
| 26 | // 2.3- Pipe the output of the encryption into the temp file | ||
| 27 | // 3- Once the file has been encrypted, hard link it back to the original location | ||
| 28 | // 4- Delete the original file | ||
| 29 | // 5- Delete the temp file | ||
| 30 | |||
| 31 | |||
| 32 | int maketmp(const char *dest, const char *format, ...) { | ||
| 33 | va_list ap; | ||
| 34 | va_start(ap, format); | ||
| 35 | |||
| 36 | |||
| 37 | |||
| 38 | va_end(ap); | ||
| 39 | return 0; | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | |||
| 44 | int main() { | ||
| 45 | char *test = NULL; | ||
| 46 | |||
| 47 | saprintf(&test, "We do a little trolling %d", 900); | ||
| 48 | printf("%s\n", test); | ||
| 49 | |||
| 50 | return 0; | ||
| 51 | } \ No newline at end of file | ||
diff --git a/src/encryption.h b/src/encryption.h new file mode 100644 index 0000000..945f73d --- /dev/null +++ b/src/encryption.h | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | #ifndef __VXGG_REWRITE___ENCRYPTION_H___1481879318188___ | ||
| 2 | #define __VXGG_REWRITE___ENCRYPTION_H___1481879318188___ | ||
| 3 | |||
| 4 | // Checks if sodium is initialized. Initializes it if not | ||
| 5 | int checkSodium(void); | ||
| 6 | |||
| 7 | #endif \ No newline at end of file | ||
| @@ -4,64 +4,9 @@ | |||
| 4 | #include <error.h> | 4 | #include <error.h> |
| 5 | #include <stdio.h> | 5 | #include <stdio.h> |
| 6 | 6 | ||
| 7 | #include <sys/types.h> | ||
| 8 | #include <sys/stat.h> | ||
| 9 | #include <unistd.h> | ||
| 10 | #include <dirent.h> | ||
| 11 | #include <fcntl.h> | ||
| 12 | |||
| 13 | #include <regex.h> | ||
| 14 | |||
| 15 | #include <string.h> | ||
| 16 | |||
| 17 | regex_t* initfilterregex() { | ||
| 18 | static regex_t *reg = NULL; | ||
| 19 | |||
| 20 | if(reg != NULL) | ||
| 21 | return reg; | ||
| 22 | |||
| 23 | reg = xcalloc(1, sizeof(*reg)); | ||
| 24 | if(regcomp(reg, "^(.{1,2})$", REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0) | ||
| 25 | return NULL; | ||
| 26 | |||
| 27 | return reg; | ||
| 28 | } | ||
| 29 | |||
| 30 | int testfilter(const struct dirent *node) { | ||
| 31 | regex_t *reg = initfilterregex(); | ||
| 32 | |||
| 33 | if(regexec(reg, node->d_name, 0, NULL, 0) != (REG_NOMATCH | REG_NOERROR) && S_ISDIR(DTTOIF(node->d_type))) | ||
| 34 | return 0; | ||
| 35 | |||
| 36 | return 1; | ||
| 37 | } | ||
| 38 | |||
| 39 | int main() { | 7 | int main() { |
| 40 | // Alright, going to start simple. First: scanning for files. I want to do this quickly and in one motion | ||
| 41 | // No reason to do things in O(n2) time if I can do it in O(n) | ||
| 42 | |||
| 43 | int nnodes = -1; | ||
| 44 | struct dirent **nodes = NULL; | ||
| 45 | if((nnodes = scandir(".", &nodes, testfilter, alphasort)) < 0) | ||
| 46 | error(1, errno, "scandir broke"); | ||
| 47 | |||
| 48 | for(int i = 0; i < nnodes; i++) { | ||
| 49 | printf("\"%s\"", nodes[i]->d_name); | ||
| 50 | #if defined _DIRENT_HAVE_D_TYPE | ||
| 51 | int mode = DTTOIF(nodes[i]->d_type); | ||
| 52 | printf(", Type: %s%s%s", | ||
| 53 | S_ISREG(mode) ? "Regular file" : "",\ | ||
| 54 | S_ISDIR(mode) ? "Directory" : "",\ | ||
| 55 | (!S_ISREG(mode) && !S_ISDIR(mode)) ? "Unknown" : ""); | ||
| 56 | |||
| 57 | #endif | ||
| 58 | 8 | ||
| 59 | printf("\n"); | ||
| 60 | } | ||
| 61 | 9 | ||
| 62 | // Ok so scandir is annoying and doesn't create a linked list, but rather an array of dirent objects. This means that if I want | ||
| 63 | // to iterate over the list myself and it'll be a pain in the ass to do so. That, or I can implement a linked list, which would | ||
| 64 | // also be a pain in the ass. I'm starting to remember why I finished vxgg the way I did | ||
| 65 | 10 | ||
| 66 | return 0; | 11 | return 0; |
| 67 | } \ No newline at end of file | 12 | } \ No newline at end of file |
diff --git a/src/shared.c b/src/shared.c index 5e58de0..0c7c8e2 100644 --- a/src/shared.c +++ b/src/shared.c | |||
| @@ -1,8 +1,10 @@ | |||
| 1 | #include "shared.h" | 1 | #include "shared.h" |
| 2 | 2 | ||
| 3 | #include <stdarg.h> | ||
| 3 | #include <stdlib.h> | 4 | #include <stdlib.h> |
| 4 | #include <errno.h> | 5 | #include <errno.h> |
| 5 | #include <error.h> | 6 | #include <error.h> |
| 7 | #include <stdio.h> | ||
| 6 | 8 | ||
| 7 | void* xcalloc(size_t nmemb, size_t size) { | 9 | void* xcalloc(size_t nmemb, size_t size) { |
| 8 | void *mem = calloc(nmemb, size); | 10 | void *mem = calloc(nmemb, size); |
| @@ -17,4 +19,44 @@ void* xcalloc(size_t nmemb, size_t size) { | |||
| 17 | 19 | ||
| 18 | 20 | ||
| 19 | return mem; | 21 | return mem; |
| 22 | } | ||
| 23 | |||
| 24 | void* xreallocarray(void *ptr, size_t nmemb, size_t size) { | ||
| 25 | void *mem = reallocarray(ptr, nmemb, size); | ||
| 26 | if(mem == NULL) { | ||
| 27 | #if defined ___VXGG___XCALLOC_EXIT_ON_ERROR___ && ___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0 | ||
| 28 | error(1, errno, "<xreallocarray> Could not allocate memory"); | ||
| 29 | |||
| 30 | #endif | ||
| 31 | |||
| 32 | abort(); | ||
| 33 | } | ||
| 34 | |||
| 35 | return mem; | ||
| 36 | } | ||
| 37 | |||
| 38 | int vsaprintf(char **str, const char *format, va_list ap) { | ||
| 39 | va_list ap2; | ||
| 40 | va_copy(ap2, ap); | ||
| 41 | |||
| 42 | int length = vsnprintf(NULL, 0, format, ap2) + 1; // + 1 because sprintf does not count the null byte | ||
| 43 | char *temp = reallocarray(*str, length, sizeof(char)); | ||
| 44 | if(temp == NULL) | ||
| 45 | return -1; | ||
| 46 | |||
| 47 | int ret = vsnprintf(temp, length, format, ap); | ||
| 48 | *str = temp; | ||
| 49 | |||
| 50 | va_end(ap2); | ||
| 51 | return ret; | ||
| 52 | } | ||
| 53 | |||
| 54 | int saprintf(char **str, const char *format, ...) { | ||
| 55 | va_list ap; | ||
| 56 | va_start(ap, format); | ||
| 57 | |||
| 58 | int ret = vsaprintf(str, format, ap); | ||
| 59 | |||
| 60 | va_end(ap); | ||
| 61 | return ret; | ||
| 20 | } \ No newline at end of file | 62 | } \ No newline at end of file |
diff --git a/src/shared.h b/src/shared.h index ac214f7..fef19ca 100644 --- a/src/shared.h +++ b/src/shared.h | |||
| @@ -2,13 +2,23 @@ | |||
| 2 | #define __VXGG_REWRITE___SHARED_H___3880294315821___ | 2 | #define __VXGG_REWRITE___SHARED_H___3880294315821___ |
| 3 | 3 | ||
| 4 | #include <stddef.h> | 4 | #include <stddef.h> |
| 5 | #include <stdarg.h> | ||
| 5 | 6 | ||
| 6 | // Defines how `xcalloc()` should exit. `___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions registered with | 7 | // Defines how `x___alloc()` functions should exit. `___VXGG___XCALLOC_EXIT_ON_ERROR___ > 0` calls `error()`, and thus functions |
| 7 | // `atexit()` and `on_exit()`. `___VXGG___XCALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `xcalloc()` will ALWAYS 'abort', | 8 | // registered with `atexit()` and `on_exit()`. `___VXGG___XCALLOC_EXIT_ON_ERROR___ <= 0` calls `abort()` on error. `xcalloc()` |
| 8 | // doing otherwise defeats the purpose of the function | 9 | // will ALWAYS 'abort', doing otherwise defeats the purpose of the function |
| 9 | #define ___VXGG___XCALLOC_EXIT_ON_ERROR___ 1 | 10 | #define ___VXGG___XCALLOC_EXIT_ON_ERROR___ 1 |
| 10 | 11 | ||
| 11 | // `calloc()` with error checking. Calls `error()` or `abort()` on error depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___` | 12 | // `calloc()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___` |
| 12 | void* xcalloc(size_t nmemb, size_t size); | 13 | void* xcalloc(size_t nmemb, size_t size); |
| 13 | 14 | ||
| 15 | // `reallocarray()` with error checking. Calls `error()` or `abort()` on error, depending on the value of `___VXGG___XCALLOC_EXIT_ON_ERROR___` | ||
| 16 | void* xreallocarray(void *ptr, size_t nmemb, size_t size); | ||
| 17 | |||
| 18 | // `vsprintf()`, but reallocates enough room for the resulting string before writing to `str` | ||
| 19 | int vsaprintf(char **str, const char *format, va_list ap); | ||
| 20 | |||
| 21 | // `sprintf()`, but reallocates enough room for the resulting string before writing to `str` | ||
| 22 | int saprintf(char **str, const char *format, ...); | ||
| 23 | |||
| 14 | #endif \ No newline at end of file | 24 | #endif \ No newline at end of file |
