diff --git a/base16384.h b/base16384.h index c24a05b..6a83278 100644 --- a/base16384.h +++ b/base16384.h @@ -84,8 +84,8 @@ union base16384_io_function_t { typedef union base16384_io_function_t base16384_io_function_t; struct base16384_stream_t { - const base16384_io_function_t f; - const void *client_data; + base16384_io_function_t f; + void *client_data; }; /** * @brief for stream encode/decode diff --git a/file.c b/file.c index 71c125b..3d9d872 100644 --- a/file.c +++ b/file.c @@ -248,10 +248,10 @@ base16384_err_t base16384_encode_stream_detailed(base16384_stream_t* input, base return base16384_err_ok; } -#define rm_head(fp) {\ - int ch = fgetc(fp);\ - if(ch == 0xFE) fgetc(fp);\ - else ungetc(ch, fp);\ +static inline void rm_head(FILE* fp) { + int ch = fgetc(fp); + if(ch == 0xFE) fgetc(fp); + else ungetc(ch, fp); } #define skip_offset(input_file) ((input_file[0]==(char)0xFE)?2:0) @@ -299,9 +299,11 @@ base16384_err_t base16384_decode_file_detailed(const char* input, const char* ou goto_base16384_file_detailed_cleanup(decode, base16384_err_fopen_input_file, {}); } rm_head(fp); + #ifndef _WIN32 // windows is crazy and always throws EINVAL if(errno) { goto_base16384_file_detailed_cleanup(decode, base16384_err_read_file, {}); } + #endif int cnt, last_encbuf_cnt = 0, last_decbuf_cnt = 0, offset = 0; size_t total_decoded_len = 0; while((cnt = fread(decbuf, sizeof(char), inputsize, fp)) > 0) { @@ -316,7 +318,9 @@ base16384_err_t base16384_decode_file_detailed(const char* input, const char* ou decbuf[cnt++] = '='; decbuf[cnt++] = end; } + #ifndef _WIN32 // windows is crazy and always throws EINVAL if(errno) goto_base16384_file_detailed_cleanup(decode, base16384_err_read_file, {}); + #endif offset = decbuf[cnt-1]; last_decbuf_cnt = cnt; cnt = base16384_decode_unsafe(decbuf, cnt, encbuf); @@ -376,9 +380,11 @@ base16384_err_t base16384_decode_fp_detailed(FILE* input, FILE* output, char* en off_t inputsize = _BASE16384_DECBUFSZ; uint32_t sum = BASE16384_SIMPLE_SUM_INIT_VALUE; rm_head(input); + #ifndef _WIN32 // windows is crazy and always throws EINVAL if(errno) { return base16384_err_read_file; } + #endif int cnt, last_encbuf_cnt = 0, last_decbuf_cnt = 0, offset = 0; size_t total_decoded_len = 0; while((cnt = fread(decbuf, sizeof(char), inputsize, input)) > 0) { @@ -393,7 +399,9 @@ base16384_err_t base16384_decode_fp_detailed(FILE* input, FILE* output, char* en decbuf[cnt++] = '='; decbuf[cnt++] = end; } + #ifndef _WIN32 // windows is crazy and always throws EINVAL if(errno) return base16384_err_read_file; + #endif offset = decbuf[cnt-1]; last_decbuf_cnt = cnt; cnt = base16384_decode_unsafe(decbuf, cnt, encbuf); @@ -464,9 +472,11 @@ base16384_err_t base16384_decode_fd_detailed(int input, int output, char* encbuf else break; } uint16_t next = is_next_end_fd(input); + #ifndef _WIN32 // windows is crazy and always throws EINVAL if(errno) { return base16384_err_read_file; } + #endif if((uint16_t)(~next)) { if(next&0xff00) { decbuf[n++] = '='; @@ -543,9 +553,11 @@ base16384_err_t base16384_decode_stream_detailed(base16384_stream_t* input, base else break; } uint16_t next = is_next_end_stream(input); + #ifndef _WIN32 // windows is crazy and always throws EINVAL if(errno) { return base16384_err_read_file; } + #endif if((uint16_t)(~next)) { if(next&0xff00) { decbuf[n++] = '='; diff --git a/test/file_test.c b/test/file_test.c index 754cd72..2558118 100644 --- a/test/file_test.c +++ b/test/file_test.c @@ -97,14 +97,14 @@ static char tstbuf[BASE16384_ENCBUFSZ]; for(i = TEST_SIZE; i > 0; i--) { \ reset_and_truncate(fd, i); \ \ - int fdout = open(TEST_OUTPUT_FILENAME, O_RDWR|O_TRUNC|O_CREAT|O_APPEND); \ + int fdout = open(TEST_OUTPUT_FILENAME, O_RDWR|O_TRUNC|O_CREAT|O_APPEND, 0644); \ loop_ok(!fdout, i, "open"); \ \ err = base16384_encode_fd_detailed(fd, fdout, encbuf, decbuf, flag); \ base16384_loop_ok(err); \ loop_ok(close(fd), i, "close"); \ \ - int fdval = open(TEST_VALIDATE_FILENAME, O_WRONLY|O_TRUNC|O_CREAT); \ + int fdval = open(TEST_VALIDATE_FILENAME, O_WRONLY|O_TRUNC|O_CREAT, 0644); \ loop_ok(!fdval, i, "open"); \ \ loop_ok(lseek(fdout, 0, SEEK_SET), i, "lseek"); \ @@ -124,7 +124,7 @@ static char tstbuf[BASE16384_ENCBUFSZ]; for(i = TEST_SIZE; i > 0; i--) { \ reset_and_truncate(fd, i); \ \ - int fdout = open(TEST_OUTPUT_FILENAME, O_RDWR|O_TRUNC|O_CREAT|O_APPEND); \ + int fdout = open(TEST_OUTPUT_FILENAME, O_RDWR|O_TRUNC|O_CREAT|O_APPEND, 0644); \ loop_ok(!fdout, i, "open"); \ \ err = base16384_encode_stream_detailed(&(base16384_stream_t){ \ @@ -137,7 +137,7 @@ static char tstbuf[BASE16384_ENCBUFSZ]; base16384_loop_ok(err); \ loop_ok(close(fd), i, "close"); \ \ - int fdval = open(TEST_VALIDATE_FILENAME, O_WRONLY|O_TRUNC|O_CREAT); \ + int fdval = open(TEST_VALIDATE_FILENAME, O_WRONLY|O_TRUNC|O_CREAT, 0644); \ loop_ok(!fdval, i, "open"); \ \ loop_ok(lseek(fdout, 0, SEEK_SET), i, "lseek"); \ @@ -171,12 +171,6 @@ static char tstbuf[BASE16384_ENCBUFSZ]; \ test_##name##_detailed(BASE16384_FLAG_NOHEADER|BASE16384_FLAG_SUM_CHECK_ON_REMAIN|BASE16384_FLAG_DO_SUM_CHECK_FORCELY); - -#define remove_test_files() \ - remove(TEST_INPUT_FILENAME); \ - remove(TEST_OUTPUT_FILENAME); \ - remove(TEST_VALIDATE_FILENAME); - int main() { srand(time(NULL)); @@ -184,6 +178,8 @@ int main() { int fd, i; base16384_err_t err; + init_test_files(); + test_detailed(file); test_detailed(fp); test_detailed(fd); diff --git a/test/file_test.h b/test/file_test.h index b44fe8b..4fc165c 100644 --- a/test/file_test.h +++ b/test/file_test.h @@ -19,6 +19,8 @@ * along with this program. If not, see . */ +#include + #define ok(has_failed, reason) \ if (has_failed) { \ perror(reason); \ @@ -83,8 +85,9 @@ } #define init_input_file() \ - for(i = 0; i < BASE16384_ENCBUFSZ; i += sizeof(int)) { \ - *(int*)(&encbuf[i]) = rand(); \ + fprintf(stderr, "fill encbufsz: %d\n", BASE16384_ENCBUFSZ);\ + for(i = 0; i < BASE16384_ENCBUFSZ/sizeof(uint16_t); i++) { \ + ((uint16_t*)encbuf)[i] = (uint16_t)rand(); \ } \ fp = fopen(TEST_INPUT_FILENAME, "wb"); \ ok(!fp, "fopen"); \ @@ -92,6 +95,23 @@ ok(fclose(fp), "fclose"); \ fputs("input file created.\n", stderr); +#define init_test_files() {\ + fd = open(TEST_INPUT_FILENAME, O_RDWR|O_TRUNC|O_CREAT, 0644); \ + ok(fd<0, "open"); \ + ok(close(fd), "close"); \ + fd = open(TEST_OUTPUT_FILENAME, O_RDWR|O_TRUNC|O_CREAT, 0644); \ + ok(fd<0, "open"); \ + ok(close(fd), "close"); \ + fd = open(TEST_VALIDATE_FILENAME, O_RDWR|O_TRUNC|O_CREAT, 0644); \ + ok(fd<0, "open"); \ + ok(close(fd), "close"); + +#define remove_test_files() \ + remove(TEST_INPUT_FILENAME); \ + remove(TEST_OUTPUT_FILENAME); \ + remove(TEST_VALIDATE_FILENAME); \ +} + #include #include #include diff --git a/test/wrap_test.c b/test/wrap_test.c index e2345b4..7c9f4e0 100644 --- a/test/wrap_test.c +++ b/test/wrap_test.c @@ -50,6 +50,8 @@ int main() { int fd, i; base16384_err_t err; + init_test_files(); + fputs("testing base16384_en/decode_file...\n", stderr); init_input_file(); for(i = TEST_SIZE; i > 0; i--) { @@ -101,14 +103,14 @@ int main() { for(i = TEST_SIZE; i > 0; i--) { reset_and_truncate(fd, i); - int fdout = open(TEST_OUTPUT_FILENAME, O_RDWR|O_TRUNC|O_CREAT|O_APPEND); + int fdout = open(TEST_OUTPUT_FILENAME, O_RDWR|O_TRUNC|O_CREAT|O_APPEND, 0644); loop_ok(!fdout, i, "open"); err = base16384_encode_fd(fd, fdout, encbuf, decbuf); base16384_loop_ok(err); loop_ok(close(fd), i, "close"); - int fdval = open(TEST_VALIDATE_FILENAME, O_WRONLY|O_TRUNC|O_CREAT); + int fdval = open(TEST_VALIDATE_FILENAME, O_WRONLY|O_TRUNC|O_CREAT, 0644); loop_ok(!fdval, i, "open"); loop_ok(lseek(fdout, 0, SEEK_SET), i, "lseek"); @@ -127,7 +129,7 @@ int main() { for(i = TEST_SIZE; i > 0; i--) { reset_and_truncate(fd, i); - int fdout = open(TEST_OUTPUT_FILENAME, O_RDWR|O_TRUNC|O_CREAT|O_APPEND); + int fdout = open(TEST_OUTPUT_FILENAME, O_RDWR|O_TRUNC|O_CREAT|O_APPEND, 0644); loop_ok(!fdout, i, "open"); err = base16384_encode_stream(&(base16384_stream_t){ @@ -140,7 +142,7 @@ int main() { base16384_loop_ok(err); loop_ok(close(fd), i, "close"); - int fdval = open(TEST_VALIDATE_FILENAME, O_WRONLY|O_TRUNC|O_CREAT); + int fdval = open(TEST_VALIDATE_FILENAME, O_WRONLY|O_TRUNC|O_CREAT, 0644); loop_ok(!fdval, i, "open"); loop_ok(lseek(fdout, 0, SEEK_SET), i, "lseek"); @@ -160,9 +162,7 @@ int main() { validate_result(); } - remove(TEST_INPUT_FILENAME); - remove(TEST_OUTPUT_FILENAME); - remove(TEST_VALIDATE_FILENAME); + remove_test_files(); return 0; }