diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/akfs.c | 261 | ||||
-rwxr-xr-x | src/build.sh | 12 | ||||
-rw-r--r-- | src/include/akfs.h | 25 | ||||
-rw-r--r-- | src/tests/test_akfs.c | 201 |
4 files changed, 499 insertions, 0 deletions
diff --git a/src/akfs.c b/src/akfs.c new file mode 100644 index 0000000..d8f8955 --- /dev/null +++ b/src/akfs.c @@ -0,0 +1,261 @@ +#include <stdio.h> +#include <stdbool.h> +#include <assert.h> +#include <stdlib.h> +#include <akfs.h> + +char* ak_fs_return_hash_path(char* string) +{ + if ( ak_fs_verify_input_is_hash(string) ) + { + unsigned int i = 0; + char *result = malloc((128*2)+1); + while ( string[i] != '\0' ) + { + result[i*2] = string[i]; + if ( (i*2) + 1 <= 254 ) + { + result[(i*2)+1] = '/'; + } + else + { + result[(i*2)+1] = '\0'; + } + ++i; + } + return result; + } + else + { + return NULL; + } +} + +char* ak_fs_return_hash_dir(char* string) +{ + if ( ak_fs_verify_input_is_hash(string) ) + { + unsigned int i = 0; + char *result = malloc((128*2)+1); + while ( string[i] != '\0' ) + { + result[i*2] = string[i]; + if ( (i*2) + 1 <= 254-2 ) + { + result[(i*2)+1] = '/'; + } + else + { + result[(i*2)+1] = '\0'; + } + ++i; + } + return result; + } + else + { + return NULL; + } +} + +bool ak_fs_verify_input_is_hash(char* string) +{ + unsigned int i = 0; + while ( string[i] != '\0' ) + { + if ( + i < 128 && + !( + ( string[i] >= 0x30 ) && + (( string[i] <= 0x39) || ( string[i] >= 0x61 )) && + ( string[i] <= 0x66 ) + ) + ) + { + return false; + } + else { + i++; + } + } + if ( i > 128 ) + { + return false; + } + return true; +} + +int ak_fs_create_dir_for_hash(char* string) +{ + (void) string; + return -1; +} + +sha512sum ak_fs_sha512sum_string_to_struct(char* string) +{ + sha512sum hash = {0}; + if ( ak_fs_verify_input_is_hash(string) ) + { + for (size_t l = 0; l < 8; ++l) + { + hash.sum[l]=0; + } + unsigned int i = 0; + unsigned int j = 0; + unsigned int k = 4; + while ( string[i] != '\0' ) + { + assert( i < 128 && "Length exceeded limit"); + if ( i % 16 == 0 ) j = i / 16; + switch (string[i]) + { + case 0x30: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0x0; + break; + case 0x31: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0x1; + break; + case 0x32: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0x2; + break; + case 0x33: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0x3; + break; + case 0x34: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0x4; + break; + case 0x35: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0x5; + break; + case 0x36: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0x6; + break; + case 0x37: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0x7; + break; + case 0x38: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0x8; + break; + case 0x39: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0x9; + break; + case 0x61: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0xa; + break; + case 0x62: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0xb; + break; + case 0x63: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0xc; + break; + case 0x64: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0xd; + break; + case 0x65: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0xe; + break; + case 0x66: + hash.sum[j] = hash.sum[j] << k; + hash.sum[j] += 0xf; + break; + default: + assert(0 && "Character out of range"); + } + i++; + } + if ( i != 128 ) + { + sha512sum hash0 = {0}; + return hash0; + } + return hash; + } + else + { + return hash; + } +} + +char* ak_fs_sha512sum_struct_to_string(sha512sum hash) +{ + char *string = malloc(129); + int counter = 0; + for (size_t i = 0; i < 8; ++i) + { + for (size_t j = 0; j < 16; ++j) + { + long unsigned first = hash.sum[i]/0xfffffffffffffff; + switch(first){ + case 0: + string[counter] = '0'; + break; + case 1: + string[counter] = '1'; + break; + case 2: + string[counter] = '2'; + break; + case 3: + string[counter] = '3'; + break; + case 4: + string[counter] = '4'; + break; + case 5: + string[counter] = '5'; + break; + case 6: + string[counter] = '6'; + break; + case 7: + string[counter] = '7'; + break; + case 8: + string[counter] = '8'; + break; + case 9: + string[counter] = '9'; + break; + case 0xa: + string[counter] = 'a'; + break; + case 0xb: + string[counter] = 'b'; + break; + case 0xc: + string[counter] = 'c'; + break; + case 0xd: + string[counter] = 'd'; + break; + case 0xe: + string[counter] = 'e'; + break; + case 0xf: + string[counter] = 'f'; + break; + default: + assert(0 && "Should be unreachable"); + } + counter++; + hash.sum[i] = hash.sum[i] << 4; + } + } + string[128] = '\0'; + return string; +} diff --git a/src/build.sh b/src/build.sh new file mode 100755 index 0000000..cccc9e6 --- /dev/null +++ b/src/build.sh @@ -0,0 +1,12 @@ +#!/bin/bash +if [ ! -d $PWD/lib ] +then + mkdir $PWD/lib +fi + +echo "Building lib/akfs.so" && \ +gcc -c -shared -Wextra -Wall -Werror -pedantic -ggdb -fPIC -I./include akfs.c -o lib/akfs.so && \ +echo "Building tests/test_akfs" && \ +gcc -Wextra -Wall -Werror -pedantic -ggdb -Wl,-rpath=lib -I./include tests/test_akfs.c lib/akfs.so -o tests/test_akfs && \ +echo "Running test_akfs" && \ +time ./tests/test_akfs diff --git a/src/include/akfs.h b/src/include/akfs.h new file mode 100644 index 0000000..1a574af --- /dev/null +++ b/src/include/akfs.h @@ -0,0 +1,25 @@ +#ifndef AKFS +#define AKFS + +#include <stdbool.h> + +typedef struct { + long unsigned int sum[8]; +} sha512sum; + +//typedef char[64] sha512sum_as_string; + +char* ak_fs_return_hash_path(char*); + +char* ak_fs_return_hash_dir(char*); + +bool ak_fs_verify_input_is_hash(char*); + +int ak_fs_create_dir_for_hash(char*); + +sha512sum ak_fs_sha512sum_string_to_struct(char*); + +char* ak_fs_sha512sum_struct_to_string(sha512sum); + +#endif // AKFS + diff --git a/src/tests/test_akfs.c b/src/tests/test_akfs.c new file mode 100644 index 0000000..e95508b --- /dev/null +++ b/src/tests/test_akfs.c @@ -0,0 +1,201 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <akfs.h> + +void correct_string_correct_length() +{ + printf("%s\t", __func__); + char queried_string[] = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9a7"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + sha512sum resulted_hash = ak_fs_sha512sum_string_to_struct(queried_string); + char* resulted_string = ak_fs_sha512sum_struct_to_string(resulted_hash); + // printf("Hash returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) == 0 ) + { + printf("PASS!\n"); + } + else + { + printf("NO PASS :(\n"); + } + free(resulted_string); +} + +void bad_string_correct_length() +{ + printf("%s\t", __func__); + char queried_string[] = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9az"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + sha512sum resulted_hash = ak_fs_sha512sum_string_to_struct(queried_string); + char* resulted_string = ak_fs_sha512sum_struct_to_string(resulted_hash); + // printf("Hash returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) != 0 ) + { + printf("PASS!\n"); + } + else + { + printf("NO PASS :(\n"); + } + free(resulted_string); +} + +void less_than_length() +{ + printf("%s\t", __func__); + char queried_string[] = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + sha512sum resulted_hash = ak_fs_sha512sum_string_to_struct(queried_string); + char* resulted_string = ak_fs_sha512sum_struct_to_string(resulted_hash); + // printf("Hash returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) != 0 ) + { + printf("\tPASS!\n"); + } + else + { + printf("\tNO PASS :(\n"); + } + free(resulted_string); +} + +void more_than_length() +{ + printf("%s\t", __func__); + char queried_string[] = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9a7aaa"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + sha512sum resulted_hash = ak_fs_sha512sum_string_to_struct(queried_string); + char* resulted_string = ak_fs_sha512sum_struct_to_string(resulted_hash); + // printf("Hash returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) != 0 ) + { + printf("\tPASS!\n"); + } + else + { + printf("\tNO PASS :(\n"); + } + free(resulted_string); +} + +void string_is_empty() +{ + printf("%s\t", __func__); + char queried_string[128]; + // printf("Hash given:\t%s\n", queried_string); + //printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + sha512sum resulted_hash = ak_fs_sha512sum_string_to_struct(queried_string); + char* resulted_string = ak_fs_sha512sum_struct_to_string(resulted_hash); + // printf("Hash returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) != 0 ) + { + printf("\t\tPASS!\n"); + } + else + { + printf("\t\tNO PASS :(\n"); + } + free(resulted_string); +} + +void hash_path_test() +{ + printf("%s\t", __func__); + char queried_string[] = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9a7"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + char* resulted_string = ak_fs_return_hash_path(queried_string); + // printf("Path returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) != 0 ) + { + printf("\t\tPASS!\n"); + } + else + { + printf("\t\tNO PASS :(\n"); + } + free(resulted_string); +} + +void hash_dir_test() +{ + printf("%s\t", __func__); + char queried_string[] = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9a7"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + char* resulted_string = ak_fs_return_hash_dir(queried_string); + // printf("Path returned:\t%s\n", resulted_string); + if ( strcmp(queried_string, resulted_string) != 0 ) + { + printf("\t\tPASS!\n"); + } + else + { + printf("\t\tNO PASS :(\n"); + } + free(resulted_string); +} + +void hash_save_to_file() +{ + printf("%s\t", __func__); + char queried_string[] = "921618bc6d9f8059437c5e0397b13f973ab7c7a7b81f0ca31b70bf448fd800a460b67efda0020088bc97bf7d9da97a9e2ce7b20d46e066462ec44cf60284f9a7"; + // printf("Hash given:\t%s\n", queried_string); + // printf("Is a hash: %s\n", ak_fs_verify_input_is_hash(queried_string) ? "true": "false"); + sha512sum resulted_hash = ak_fs_sha512sum_string_to_struct(queried_string); + FILE* fd = fopen("tmpfile", "wb"); + if ( fd == NULL ) + { + printf("Some error occured"); + } + fwrite(&resulted_hash, sizeof(sha512sum),1,fd); + fclose(fd); + + sha512sum readone = {0}; + fd = fopen("tmpfile", "rb"); + fread (&readone, sizeof(sha512sum),1,fd); + char* resulted_string = ak_fs_sha512sum_struct_to_string(readone); + + if ( strcmp(queried_string, resulted_string) == 0 ) + { + printf("\tPASS!\n"); + } + else + { + printf("\tNO PASS :(\n"); + } + free(resulted_string); +} + +int main(void) +{ + // Correct one + correct_string_correct_length(); + + // Supposingly a bad string but in correct length + bad_string_correct_length(); + + // Less than must be length + less_than_length(); + + // More than must be length + more_than_length(); + + // Empty string + string_is_empty(); + + // Hash path + hash_path_test(); + + // Hash dir + hash_dir_test(); + + // Tempfile test read-write + hash_save_to_file(); + return 0; +} |