-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.h
65 lines (58 loc) · 1.81 KB
/
base.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <stdbool.h>
#include "bdb_encrypt/FileEncrypter.h"
#define MAXFILENAME 256 // should be about 256 for most systems
#define TESTNAME "test.db" // use for testing this code
#define PATHSIZE ((MAXFILENAME*2)+1)
// Database Struct
typedef struct {
char tablename[MAXFILENAME];
long datasize;
} SetHeader;
#define HEADERSIZE sizeof(SetHeader)
// Other useful functions
FILE* open_file(PATH dir, PATH filename, const char* mode) {
//decrypt the file
if (mode != (char*)"w" && mode !=(char*)"wb"){
KEY privkey=load_privkey(dir);
if (decrypt_file(dir, filename, privkey) !=0){
fprintf(stderr, "Error decrypting the file: %s\n", filename);
exit(EXIT_FAILURE);
}
}
//open the file
PATH filepath = catpath(catpath(dir, "/"), filename);
FILE* file = fopen(filepath, mode);
if (!file) {
fprintf(stderr, "Error opening the file: %s\n", filename);
exit(EXIT_FAILURE);
}
return file;
}
int close_file(PATH dir, PATH filename, FILE *file){
fclose(file);
KEY privkey = load_privkey(dir);
if (encrypt_file(catpath(dir, "/"), filename, privkey)){
fprintf(stderr, "Error encrypting the file: %s\n", filename);
exit(EXIT_FAILURE);
}
return 0;
}
bool CheckDataSet(const char* tablename) {
FILE* file = fopen(tablename, "r");
if (file) {
fclose(file);
return true; // Success
} else {
return false;
}
}
size_t str_len(const char *str) {
const char *start = str; // Save start address
while (*str != '\0') {
str++; // loop through characters until teminator is found
}
return str - start; // return the difference in memory address for length
}