-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeydb.c
More file actions
188 lines (149 loc) · 4.21 KB
/
keydb.c
File metadata and controls
188 lines (149 loc) · 4.21 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "keydb.h"
#include "noisegatepad.h"
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <glib.h>
const char *LOG_DOMAIN="KeyDb";
// the primary hash table:
GHashTable *table = NULL;
typedef struct value_struct {
// any of these could be '0' meaning 'always valid'
time_t start_valid;
time_t end_valid;
} value_struct;
int part_count(gchar **v) {
int c = 0;
while(*v++ != NULL) {
c++;
}
return c;
}
time_t parse_time(const gchar *s) {
if (s == NULL) return -1;
if (*s == '\0') return -1; // nothing
// returns -1 if nothing there.
gchar * end;
time_t r = strtol(s, &end, 10);
if (*end == '\0' || *end == '\n') return r;
return -1;
}
int is_str_digits(const gchar *s) {
if (s == NULL) return 0;
if (*s == '\0') return 0;
do {
if (!g_ascii_isdigit(*s))
return 0;
} while (*++s != '\0');
return 1;
}
int reload_db_from_file(const char * fname) {
FILE * file = fopen(fname, "r");
if (file == NULL) {
g_warning("Unable to open db file: (%d): %s", errno, strerror(errno));
return -1;
}
GHashTable * new_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
char readbuf[1024];
int cnt = 0;
gchar ** parts = NULL;
while (!feof(file)) {
if (fgets(readbuf, 1024, file)) {
cnt++;
g_strfreev(parts);
// ignore all-comment lines and empty lines.
if (readbuf[0] == '#' || readbuf[0] == '\n') continue;
gchar **parts = g_strsplit(readbuf, ",", 4);
// check to see how many parts:
if (part_count(parts) != 4) {
g_warning("Line %d didn't have 4 parts, %d", cnt, part_count(parts));
continue;
}
time_t start_valid = parse_time(parts[1]);
time_t end_valid = parse_time(parts[2]);
int disabled = parse_time(parts[3]);
int failed = 0;
if (strlen(parts[0]) > MAX_DIGITS ||
strlen(parts[0]) < MIN_DIGITS) {
//g_warning("Line %d, code was wrong length: %ld",
// cnt, strlen(parts[0]));
failed = 1;
}
if (!is_str_digits(parts[0])) {
//g_warning("Line %d, code was not all digits: \"%s\"",
// cnt, parts[0]);
failed = 1;
}
if (start_valid == -1) {
g_warning("Line %d, start time was invalid: \"%s\"",
cnt, parts[1]);
failed = 1;
}
if (end_valid == -1) {
g_warning("Line %d, end time was invalid: \"%s\"",
cnt, parts[2]);
failed = 1;
}
//if (disabled != 0 || disabled != 1) {
if ( ! (disabled == 0 || disabled == 1)) {
g_warning("Line %d, disabled field wasnt 0 or 1: %s, %d",
cnt, parts[3], disabled);
failed = 1;
}
if (failed) continue;
if (disabled) {
g_message("Line %d: ignoring disabled code %s",
cnt, parts[0]);
continue;
}
gchar *code = g_strdup(parts[0]);
// populate hashdb:
value_struct * val = g_new(value_struct,1);
val->start_valid = start_valid;
val->end_valid = end_valid;
g_hash_table_insert(new_table, code, val);
//g_message("Line %d: Added code %s, validity range %ld -> %ld",
// cnt, code, start_valid, end_valid);
} else {
g_warning("Unable to fgets line (%d): %s", cnt, strerror(errno));
}
}
fclose(file);
guint newsize = g_hash_table_size(new_table);
if (table == NULL) {
table = new_table;
return newsize;
}
guint oldsize = g_hash_table_size(table);
if (newsize < oldsize/2) {
// dont update!
g_warning("New database is %ud which is much smaller than %ud of existing! Not updating!",
newsize, oldsize);
return -1;
}
table = new_table;
return newsize;
}
int check_code(const char * code) {
if (table == NULL) {
g_critical("Check_code called on null database - please init db first with reload_db_from_file!");
}
value_struct * value = (value_struct *)g_hash_table_lookup(table, code);
if (value == NULL)
return 0;
time_t now = time(NULL);
if (value->start_valid != 0) {
if (now < value->start_valid) {
g_warning("Code %s used before valid start time of %ld", code, now);
return 0;
}
}
if (value->end_valid != 0) {
if (now > value->end_valid) {
g_warning("Code %s used after valid end time of %ld", code, now);
return 0;
}
}
g_warning("Code %s is valid", code);
return 1;
}