liboqs/tests/tmp_store.c
Michael Baentsch 2757d16854
Enabling out-of-source build-and-test (#1091)
* removes further fixed build directory dependencies

* formatting corrected

* remove cleanup code unsuitable for win

* remove include unknown in windows

* adapt mkdir to win API
2021-09-18 16:06:07 -04:00

64 lines
1.6 KiB
C

// SPDX-License-Identifier: MIT
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/stat.h>
#define OQS_STORE_DIR "tmp"
#define OQS_STORE_PREFIX "/oqs-temp-file-"
#define MAXPATHLEN 128
#if (defined(_WIN32) || defined(__WIN32__))
#define mkdir(A, B) mkdir(A)
#endif
static OQS_STATUS oqs_fstore_init(void) {
return mkdir(OQS_STORE_DIR, 0755);
}
static OQS_STATUS oqs_fstore(const char *fname, const char *mname, uint8_t *data, size_t len) {
char fpath[MAXPATHLEN];
strcpy(fpath, OQS_STORE_DIR);
strcat(fpath, OQS_STORE_PREFIX);
strcat(fpath, mname);
strcat(fpath, fname);
FILE *fp = fopen(fpath, "wb");
if (!fp) {
fprintf(stderr, "Couldn't open %s for writing.\n", fpath);
return OQS_ERROR;
}
fwrite(data, len, 1, fp);
fclose(fp);
return OQS_SUCCESS;
}
static OQS_STATUS oqs_fload(const char *fname, const char *mname, uint8_t *data, size_t len, size_t *rcvd) {
size_t len_read = 0, r = 0;
uint8_t *dr = NULL;
char fpath[MAXPATHLEN];
strcpy(fpath, OQS_STORE_DIR);
strcat(fpath, OQS_STORE_PREFIX);
strcat(fpath, mname);
strcat(fpath, fname);
FILE *fp = fopen(fpath, "rb");
if (!fp) {
fprintf(stderr, "Couldn't open %s for reading.\n", fpath);
return OQS_ERROR;
}
do { // assume some OSs don't deliver all data in one go...
dr = (uint8_t *)(data + len_read);
r = fread(dr, 1, len - len_read, fp);
len_read += r;
} while (r > 0);
*rcvd = len_read;
if (len_read <= 0) {
fprintf(stderr, "Error reading data (operations called in proper sequence?). Expecting %zu. Exiting.\n", len);
return OQS_ERROR;
}
fclose(fp);
return OQS_SUCCESS;
}