-
Notifications
You must be signed in to change notification settings - Fork 0
/
packrat.c
292 lines (222 loc) · 10.8 KB
/
packrat.c
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/file.h>
#include <unistd.h>
#include "packrat.h"
// Pack Rat v3 - File signature 'PR'
#define PACKRAT_OFFSET_BITSPOS 16
#define PACKRAT_OFFSET_BITSLEN 1
#define PACKRAT_HEADER_SIG 0x5250
#define PACKRAT_HEADER_LEN 8
union packrat_header {
uint64_t u64;
unsigned char chr[8];
struct {
uint64_t pr: 16;
uint64_t id: 32;
uint64_t bitsPos: 5; // 0-31 -> 16-47. 64 KiB - 128 TiB for total data
uint64_t bitsLen: 5; // 0-31 -> 1-32. 4 B - 4 GiB per file (Zero variant only)
uint64_t unused: 5;
uint64_t allowMod: 1; // If 0, the library refuses to modify existing data.
} fields;
};
static int64_t div_floor(const int64_t a, const int64_t b) {
return (a - (a % b)) / b;
}
static int64_t div_ceil(const int64_t a, const int64_t b) {
return (a % b == 0) ? a / b : div_floor(a, b) + 1;
}
// pruint: Pack Rat Unsigned Integer: 1-48 bits, little endian bit and byte order
static void appendPruint(unsigned char * const target, const int bitsTarget, const uint64_t new, const int bitsNew) {
for (int i = 0; i < bitsNew; i++) {
const int t = bitsTarget + i;
const int tRemainder = t % 8;
const int iRemainder = i % 8;
if (*((const unsigned char * const)&new + (i - iRemainder) / 8) & (1 << iRemainder)) target[(t - tRemainder) / 8] |= 1 << (7 - tRemainder);
}
}
static uint64_t extractPruint(unsigned char * const src, const int startBit, const int pruintBits) {
union {
uint64_t u64;
unsigned char c[8];
} r;
r.u64 = 0;
for (int i = 0; i < pruintBits; i++) {
const int t = startBit + i;
const int tRemainder = t % 8;
const int iRemainder = i % 8;
if ((src[(t - tRemainder) / 8] & (1 << (7 - tRemainder))) != 0) {
r.c[(i - iRemainder) / 8] |= 1 << iRemainder;
}
}
return r.u64;
}
static bool headersMatch(const union packrat_header pri_hdr, const int fd) {
union packrat_header prd_hdr;
if (read(fd, prd_hdr.chr, PACKRAT_HEADER_LEN) != PACKRAT_HEADER_LEN) return false;
if ((pri_hdr.fields.id & 1) != 1 && (prd_hdr.fields.id & 1) != 0) return false;
prd_hdr.fields.id |= 1;
return (pri_hdr.u64 == prd_hdr.u64) ? true : false;
}
static int pr_get_zero(const int pri, const int prd, const unsigned long fileNum, const int bitsLen, const int bitsPos, unsigned char ** const data) {
const int64_t priPos = PACKRAT_HEADER_LEN + div_floor((bitsLen + bitsPos) * fileNum, 8); // Byte number to start reading from
const int skipBits = ((bitsLen + bitsPos) * fileNum) % 8; // Number of irrelevant bits in the first byte, to be skipped
const int priBytes = div_ceil(skipBits + bitsLen + bitsPos, 8);
unsigned char raw[priBytes];
ssize_t r = pread(pri, raw, priBytes, priPos);
if (r < 0) return PACKRAT_ERROR_READ_PRI;
const uint64_t readLen = extractPruint(raw, skipBits, bitsLen);
if (readLen == 0) return 0; // Placeholder
const uint64_t readPos = extractPruint(raw, skipBits + bitsLen, bitsPos);
if (readLen > (1UL << bitsPos)) return PACKRAT_ERROR_TOOBIG;
*data = malloc(readLen);
if (*data == NULL) return PACKRAT_ERROR_ALLOC;
r = pread(prd, *data, readLen, PACKRAT_HEADER_LEN + readPos);
if (r != (ssize_t)readLen) {
free(*data);
*data = NULL;
return PACKRAT_ERROR_READ_PRD;
}
return readLen;
}
static int pr_get_compact(const int pri, const int prd, const unsigned long fileNum, const int bitsPos, unsigned char ** const data) {
const int64_t priPos = PACKRAT_HEADER_LEN + div_floor(bitsPos * fileNum, 8); // Byte number to start reading from
const int skipBits = (bitsPos * fileNum) % 8; // Number of irrelevant bits in the first byte, to be skipped
const int priBytes = div_ceil(skipBits + (bitsPos * 2), 8);
unsigned char raw[priBytes];
ssize_t r = pread(pri, raw, priBytes, priPos);
if (r < 0) return PACKRAT_ERROR_READ_PRI;
const uint64_t readPos = extractPruint(raw, skipBits, bitsPos);
size_t readLen = 0;
if (r == priBytes) {
const uint64_t readEnd = extractPruint(raw, skipBits + bitsPos, bitsPos);
if (readEnd < readPos) return PACKRAT_ERROR_FORMAT;
readLen = readEnd - readPos;
} else if (r == div_ceil(skipBits + bitsPos, 8)) {
const off_t prdSize = lseek(prd, 0, SEEK_END);
if (prdSize < 0) return PACKRAT_ERROR_READ_PRI;
readLen = (prdSize - PACKRAT_HEADER_LEN) - readPos;
} else {
return PACKRAT_ERROR_READ_PRI;
}
if (readLen > (1UL << bitsPos)) return PACKRAT_ERROR_TOOBIG;
*data = malloc(readLen);
if (*data == NULL) return PACKRAT_ERROR_ALLOC;
r = pread(prd, *data, readLen, PACKRAT_HEADER_LEN + readPos);
if (r != (ssize_t)readLen) {
free(*data);
*data = NULL;
return PACKRAT_ERROR_READ_PRD;
}
return readLen;
}
int packrat_get(const char * const pathPri, const char * const pathPrd, const unsigned long fileNum, unsigned char ** const data) {
if (pathPri == NULL || pathPrd == NULL || data == NULL) return -1;
const int pri = open(pathPri, O_RDONLY | O_NOCTTY);
if (pri < 0) return PACKRAT_ERROR_OPEN;
if (flock(pri, LOCK_EX | LOCK_NB) != 0) {close(pri); return PACKRAT_ERROR_LOCK;}
const int prd = open(pathPrd, O_RDONLY | O_NOCTTY);
if (prd < 0) return PACKRAT_ERROR_OPEN;
if (flock(prd, LOCK_EX | LOCK_NB) != 0) {close(pri); close(prd); return PACKRAT_ERROR_LOCK;}
union packrat_header header;
if (read(pri, header.chr, PACKRAT_HEADER_LEN) != PACKRAT_HEADER_LEN) {close(prd); close(pri); return PACKRAT_ERROR_READ_PRI;}
if (!headersMatch(header, prd)) return PACKRAT_ERROR_MISMATCH;
const int ret = (header.fields.bitsLen == 0) ? pr_get_compact(pri, prd, fileNum, header.fields.bitsPos + PACKRAT_OFFSET_BITSPOS, data) : pr_get_zero(pri, prd, fileNum, header.fields.bitsLen + PACKRAT_OFFSET_BITSLEN, header.fields.bitsPos + PACKRAT_OFFSET_BITSPOS, data);
flock(pri, LOCK_UN);
close(pri);
flock(prd, LOCK_UN);
close(prd);
return ret;
}
static int pr_add_zero(const int pri, const int prd, int bitsLen, int bitsPos, const unsigned char * const data, const int lenData) {
if (lenData >= (1L << bitsLen)) return PACKRAT_ERROR_TOOBIG;
const off_t priSize = lseek(pri, 0, SEEK_END);
if (priSize < PACKRAT_HEADER_LEN) return PACKRAT_ERROR_READ_PRI;
const int entries = div_floor((priSize - PACKRAT_HEADER_LEN) * 8, bitsLen + bitsPos);
const int usedBits = ((bitsLen + bitsPos) * entries) % 8; // Number of currently-used bits in the last byte
const int lenTbw = div_ceil(usedBits + bitsLen + bitsPos, 8);
unsigned char tbw[lenTbw];
bzero(tbw, lenTbw);
if (usedBits > 0 && pread(pri, tbw, 1, priSize - 1) != 1) return PACKRAT_ERROR_READ_PRI;
// Zero variant stores the length and position of each file
const uint64_t raw1 = lenData;
appendPruint(tbw, usedBits, raw1, bitsLen);
if (lenData > 0) {
// Not a placeholder
const off_t prdSize = lseek(prd, 0, SEEK_END);
if (prdSize < PACKRAT_HEADER_LEN) return PACKRAT_ERROR_READ_PRD;
if (prdSize - PACKRAT_HEADER_LEN >= (1L << bitsPos)) return PACKRAT_ERROR_TOOBIG;
const uint64_t raw2 = prdSize - PACKRAT_HEADER_LEN;
appendPruint(tbw, usedBits + bitsLen, raw2, bitsPos);
if (write(prd, data, lenData) != (ssize_t)lenData) return PACKRAT_ERROR_WRITE;
}
return (pwrite(pri, (unsigned char*)&tbw, lenTbw, (usedBits > 0) ? priSize - 1 : priSize) == lenTbw) ? PACKRAT_OK : PACKRAT_ERROR_WRITE;
}
static int pr_add_compact(const int pri, const int prd, int bitsPos, const unsigned char * const data, const int lenData) {
if (lenData < 1) return PACKRAT_ERROR_PARAM;
if (bitsPos > 47) return PACKRAT_ERROR_HEADER;
const off_t prdSize = lseek(prd, 0, SEEK_END);
if (prdSize < PACKRAT_HEADER_LEN) return PACKRAT_ERROR_READ_PRD;
if (prdSize - PACKRAT_HEADER_LEN >= (1L << bitsPos)) return PACKRAT_ERROR_TOOBIG;
const off_t priSize = lseek(pri, 0, SEEK_END);
if (priSize < PACKRAT_HEADER_LEN) return PACKRAT_ERROR_READ_PRI;
const int entries = div_floor((priSize - PACKRAT_HEADER_LEN) * 8, bitsPos);
const int usedBits = (bitsPos * entries) % 8; // Number of currently-used bits in the last byte
const int lenTbw = div_ceil(usedBits + bitsPos, 8);
unsigned char tbw[lenTbw];
bzero(tbw, lenTbw);
if (usedBits > 0 && pread(pri, tbw, 1, priSize - 1) != 1) return PACKRAT_ERROR_READ_PRI;
// Compact variant stores the position (starting byte) of each file
const uint64_t raw = prdSize - PACKRAT_HEADER_LEN;
appendPruint(tbw, usedBits, raw, bitsPos);
if (pwrite(pri, (unsigned char*)&tbw, lenTbw, (usedBits > 0) ? priSize - 1 : priSize) != lenTbw) return PACKRAT_ERROR_WRITE;
if (write(prd, data, lenData) != (ssize_t)lenData) return PACKRAT_ERROR_WRITE;
return PACKRAT_OK;
}
int packrat_add(const char * const pathPri, const char * const pathPrd, const unsigned char * const data, const int lenData) {
if (pathPri == NULL || pathPrd == NULL || lenData < 0) return PACKRAT_ERROR_PARAM;
const int pri = open(pathPri, O_RDWR | O_NOCTTY);
if (pri < 0) return PACKRAT_ERROR_OPEN;
if (flock(pri, LOCK_EX | LOCK_NB) != 0) {close(pri); return PACKRAT_ERROR_LOCK;}
const int prd = open(pathPrd, ((lenData > 0) ? O_RDWR : O_RDONLY) | O_NOCTTY | O_APPEND);
if (prd < 0) return PACKRAT_ERROR_OPEN;
if (flock(prd, LOCK_EX | LOCK_NB) != 0) {close(pri); close(prd); return PACKRAT_ERROR_LOCK;}
union packrat_header header;
if (read(pri, header.chr, PACKRAT_HEADER_LEN) != PACKRAT_HEADER_LEN) {close(prd); close(pri); return PACKRAT_ERROR_READ_PRI;}
if (!headersMatch(header, prd)) return PACKRAT_ERROR_MISMATCH;
const int ret = (header.fields.bitsLen == 0) ? pr_add_compact(pri, prd, header.fields.bitsPos + PACKRAT_OFFSET_BITSPOS, data, lenData) : pr_add_zero(pri, prd, header.fields.bitsLen + PACKRAT_OFFSET_BITSLEN, header.fields.bitsPos + PACKRAT_OFFSET_BITSPOS, data, lenData);
flock(pri, LOCK_UN);
close(pri);
flock(prd, LOCK_UN);
close(prd);
return ret;
}
int packrat_create(const char * const pathPri, const char * const pathPrd, const int bitsPos, const int bitsLen) {
if (pathPri == NULL || pathPrd == NULL || bitsPos < 16 || bitsPos > 47 || bitsLen == 1 || bitsLen > 32 || bitsPos < bitsLen) return PACKRAT_ERROR_PARAM;
const int prd = open(pathPrd, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (prd < 0) return PACKRAT_ERROR_OPEN;
if (flock(prd, LOCK_EX | LOCK_NB) != 0) {close(prd); return PACKRAT_ERROR_LOCK;}
const int pri = open(pathPri, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if (pri < 0) return PACKRAT_ERROR_OPEN;
if (flock(pri, LOCK_EX | LOCK_NB) != 0) {close(pri); return PACKRAT_ERROR_LOCK;}
union packrat_header header;
header.fields.pr = PACKRAT_HEADER_SIG;
header.fields.id = arc4random() | 1; // PRI: id & 1 == 1
header.fields.bitsPos = bitsPos - PACKRAT_OFFSET_BITSPOS;
header.fields.bitsLen = (bitsLen == 0) ? 0 : bitsLen - PACKRAT_OFFSET_BITSLEN;
header.fields.unused = 0;
header.fields.allowMod = 1;
bool ok = false;
if (write(pri, header.chr, PACKRAT_HEADER_LEN) == PACKRAT_HEADER_LEN) {
header.fields.id--; // PRD: id & 1 == 0
if (write(prd, header.chr, PACKRAT_HEADER_LEN) == PACKRAT_HEADER_LEN) {
ok = true;
}
}
flock(pri, LOCK_UN);
close(pri);
flock(prd, LOCK_UN);
close(prd);
return ok? 0 : -1;
}