summaryrefslogtreecommitdiff
path: root/sdar/all.h
blob: 6f2b2d4e7111195331b3e6c11b6c7f558470c560 (plain)
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
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include "mmh.h"

enum {
	Plnpad = 32,
	Cippad = 16,
	Cipovh = Plnpad - Cippad,
	Bufpad = 32,
	Prepsz = 32,
	Noncesz = 24,
	Hmacsz = 32,
	Keysz = 32,
	Segidsz = 16,

	Avglog = 20,
	Maxblk = 2 << 20,

	Entrysz = Hmacsz + 8,
	Lvlcap = Maxblk / Entrysz,
	Magicsz = 8,
	Segmetasz1 = 8 + 8 + 8 + 128,
	Segmetasz2 = 8 + 8,
	Seghdsz = Magicsz + Keysz + Cipovh,
	Segitemsz = Hmacsz + 4,

	Permf = 0666,
	Permd = 0777,
};

typedef unsigned char uchar;
typedef unsigned int uint;
typedef long long vlong;
typedef unsigned long long uvlong;

typedef struct Addr Addr;
typedef struct Arch Arch;
typedef struct Block Block;
typedef struct Cache Cache;
typedef struct Cachemeta Cachemeta;
typedef struct Chunker Chunker;
typedef struct Entry Entry;
typedef struct Key Key;
typedef struct Level Level;
typedef struct Loghd Loghd;
typedef struct Reader Reader;
typedef struct Segfile Segfile;
typedef struct Segctx Segctx;
typedef struct Segmeta Segmeta;
typedef struct Slice Slice;
typedef struct Writer Writer;

typedef uchar hmac_t[Hmacsz];

typedef int flushcb(void *, hmac_t, Slice);
typedef int blockcb(Block *, hmac_t, Segctx *, void *);


struct Slice {
	uchar *buf;
	long len;
};

struct Key {
	uchar *hmac;
	uchar *pub;
	uchar *sec;
};

struct Block {
	int seg;
	int len; /* length of the block in the segment
	            excluding the cipher overhead */
	vlong off;
	char lz4;
};

struct Cachemeta {
	uchar magic[4];
	int nseg;
};

struct Cache {
	Cachemeta *meta;
	Mmh hash; /* maps hmac to Block */
};

struct Entry {
	hmac_t hmac;
	vlong len;
};

struct Level {
	int nent;
	Entry ent[Lvlcap];
};

struct Segctx {
	uchar prep[Prepsz];
	int metasz;
};

struct Reader {
	Arch *arch;
	vlong pos;
	vlong pos1;
	hmac_t hmac0; /* hmac of level[0] */
	Level level[2];

	/* open segment */
	Segctx segc;
	FILE *segf;
	int segno;
};

struct Writer {
	flushcb *flush;
	void *arg;
	int bpos; /* position in buf[] */
	uint rhash;
	vlong vlen;
	vlong vlen0;
	Level level[2];
	uchar buf[Bufpad + 2*Maxblk];
};

struct Segmeta {
	vlong nitem;
	vlong dlen;
};

struct Segfile {
	uchar id[Segidsz];
	char here;
};

struct Loghd {
	uchar segk[Keysz];
	uchar segp[Prepsz];
	vlong nitem;
	vlong dlen;
};

struct Arch {
	int fd;
	Cache cache;
	Segfile *seg;
	int nseg;

	/* stash */
	FILE *log;
	FILE *data;
	Loghd *logh;
	Writer writer;
};

struct Addr {
	hmac_t hmac;
	int level;
};

#define MAKESURE(what, x) _Static_assert(x, #what)
#define min(a, b) ((a) > (b) ? (b) : (a))

/* util.c */
extern FILE *flog;
void logs(char *, ...);
int shex(char *, int, uchar *);
char *hexs(uchar *, int);
char *segs(uchar[Segidsz]);
char *hmacs(hmac_t);
void hmaccpy(hmac_t, hmac_t);
FILE *fopenat(int, int, char *, ...);
int flushsync(FILE *);
void enc32be(uchar *, uvlong);
void enc32le(uchar *, uvlong);
void enc64be(uchar *, uvlong);
uvlong dec32be(uchar *);
uvlong dec32le(uchar *);
uvlong dec64be(uchar *);
void encmeta(Slice *, Segmeta *);
void decmeta(Slice, Segmeta *);
void encitem(uchar *, hmac_t, int, long);
void decitem(uchar *, Block *);
void addentry(Level *, hmac_t, vlong);
void enclevel(Slice *, Level *);
int declevel(Slice, Level *);

/* slice.c */
Slice mksl(uchar *, long);
Slice newsl();
void freesl(Slice);
void sldeflate(Slice *, Slice);
int slinflate(Slice *, Slice);
void slencrypt(Slice *, Slice, uchar[Prepsz], vlong);
int decrypt(Slice *, FILE *, long, uchar[Prepsz], vlong);
void slhmac(Slice, hmac_t);
int slwrite(Slice, FILE *);
int slread(Slice *, long, FILE *);

/* key.c */
extern Key key;
void keygen(void);
void keydump(void);
int keywrite(char *, long, FILE *);
char *keyreadpub(FILE *);
char *keyreadsec(char *, long, FILE *);

/* cache.c */
int cinit(Cache *, int, char *, int);
void creset(Cache *);
int csync(Cache *);
int cput(Cache *, hmac_t, Block *);
int cputseg(Cache *, uchar[Segidsz], int);
int cget(Cache *, hmac_t, Block *);
int cgetseg(Cache *, uchar[Segidsz], int *);
void cdumphashes(Cache *, int);
void cfree(Cache *);

/* arch.c */
extern uchar segmagic2[Magicsz];
int archinit(Arch *, char *);
void archsync(Arch *);
int archhas(Arch *, hmac_t);
void readerinit(Reader *, Arch *);
int readerstart(Reader *, Addr *, vlong);
void readerdone(Reader *);
int readerseek(Reader *, vlong);
int reader(Reader *, Slice *);
int walkblocks(FILE *, blockcb, void *);

/* stash.c */
int stashinit(Arch *);
int stashadd(Arch *, uchar *, vlong);
int stashblk(Arch *, hmac_t, int, Slice);
int stashdone(Arch *, Addr *);
int stashsync(Arch *);
int stashcommit(Arch *, uchar[Segidsz]);

/* write.c */
void writerinit(Writer *, flushcb *, void *);
int writer(Writer *, uchar *, vlong);
int writerdone(Writer *, Addr *);