-
Notifications
You must be signed in to change notification settings - Fork 1
/
meta.c
315 lines (278 loc) · 5.52 KB
/
meta.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <stdio.h>
#include "dr.h"
#include "meta.h"
#define HASH_LEN uint8
#define NAME_LEN uint16
#define HASH_LEN_SIZE (sizeof(uint8_t))
#define NAME_LEN_SIZE (sizeof(uint16_t))
static void
writeact(drb_t *drb, int act)
{
uint8_t *p = drb_check(drb, 1);
*p = (uint8_t)act; p += 1;
drb_end(drb, p);
}
static dr_t
readname(const uint8_t **pp)
{
int size;
dr_t name;
const uint8_t *p = *pp;
size = NAME_LEN(p); p += NAME_LEN_SIZE;
name = dr_new(size, p); p += size;
*pp = p;
return name;
}
static void
writename(drb_t *drb, dr_t name)
{
uint8_t *p;
int size;
p = drb_check(drb, NAME_LEN_SIZE + name->size);
NAME_LEN(p) = size = name->size; p += NAME_LEN_SIZE;
memcpy(p, name->buf, size); p += size;
drb_end(drb, p);
}
static dr_t
readdata(const uint8_t **pp)
{
int size;
dr_t data;
const uint8_t *p = *pp;
size = uint32(p); p += 4;
data = dr_new(size, p); p += size;
*pp = p;
return data;
}
static void
writedata(drb_t *drb, dr_t data)
{
int size;
uint8_t *p;
p = drb_check(drb, data->size);
uint32(p) = size = data->size; p += 4;
memcpy(p, data->buf, size); p += size;
drb_end(drb, p);
return ;
}
void
ctrl_new(drb_t *drb, struct NEW *c)
{
writeact(drb, CTRL_NEW);
writename(drb, c->name);
writedata(drb, c->data);
printf(".NEW %s size:%.3f KiB\n",
c->name->buf, c->data->size/1024.f);
return ;
}
static const uint8_t *
ctrl_new_read(const uint8_t *p, struct NEW *c)
{
c->name = readname(&p);
c->data = readdata(&p);
printf(".NEW %s size:%.3f KiB\n",
c->name->buf, c->data->size/1024.f);
return p;
}
static void
ctrl_new_free(struct NEW *n)
{
dr_unref(n->name);
dr_unref(n->data);
}
void
ctrl_dff(drb_t *drb, struct DFF *c)
{
writeact(drb, CTRL_DFF);
writename(drb, c->name);
writedata(drb, c->patch);
printf(".DFF %s patch:%d Byte\n",
c->name->buf, c->patch->size);
return ;
}
static const uint8_t *
ctrl_dff_read(const uint8_t *p, struct DFF *c)
{
c->name = readname(&p);
c->patch = readdata(&p);
printf(".DFF %s patch:%d Byte\n",
c->name->buf, c->patch->size);
return p;
}
static void
ctrl_dff_free(struct DFF *n)
{
dr_unref(n->name);
dr_unref(n->patch);
}
void
ctrl_dfx(drb_t *drb, struct DFX *c)
{
writeact(drb, CTRL_DFX);
writename(drb, c->name);
writename(drb, c->namea);
writedata(drb, c->patch);
printf(".DFX %s -> %s patch:%d Byte\n",
c->namea->buf, c->name->buf, c->patch->size);
return ;
}
static const uint8_t *
ctrl_dfx_read(const uint8_t *p, struct DFX *c)
{
c->name = readname(&p);
c->namea = readname(&p);
c->patch = readdata(&p);
printf(".DFX %s -> %s patch:%d Byte\n",
c->namea->buf, c->name->buf, c->patch->size);
return p;
}
static void
ctrl_dfx_free(struct DFX *n)
{
dr_unref(n->name);
dr_unref(n->namea);
dr_unref(n->patch);
}
void
ctrl_del(drb_t *drb, struct DEL *c)
{
writeact(drb, CTRL_DEL);
writename(drb, c->name);
printf(".DEL %s\n", c->name->buf);
}
static const uint8_t *
ctrl_del_read(const uint8_t *p, struct DEL *n)
{
n->name = readname(&p);
printf(".DEL %s\n", n->name->buf);
return p;
}
static void
ctrl_del_free(struct DEL *n)
{
dr_unref(n->name);
}
void
ctrl_cpy(drb_t *drb, struct CPY *c)
{
writeact(drb, CTRL_CPY);
writename(drb, c->name);
writename(drb, c->namea);
printf(".CPY %s => %s\n", c->namea->buf, c->name->buf);
}
static const uint8_t *
ctrl_cpy_read(const uint8_t *p, struct CPY *n)
{
n->name = readname(&p);
n->namea = readname(&p);
return p;
}
static void
ctrl_cpy_free(struct CPY *n)
{
dr_unref(n->name);
dr_unref(n->namea);
}
const uint8_t *
ctrl_read(const uint8_t *p, struct CTRL *ctrl)
{
ctrl->act = uint8(p); p += 1;
switch (ctrl->act) {
case CTRL_NEW:
return ctrl_new_read(p, &ctrl->u.new);
case CTRL_DFF:
return ctrl_dff_read(p, &ctrl->u.dff);
case CTRL_DFX:
return ctrl_dfx_read(p, &ctrl->u.dfx);
case CTRL_CPY:
return ctrl_cpy_read(p, &ctrl->u.cpy);
case CTRL_DEL:
return ctrl_del_read(p, &ctrl->u.del);
default:
assert(0);
}
return NULL;
}
void
ctrl_destroy(struct CTRL *ctrl)
{
switch (ctrl->act) {
case CTRL_NEW:
ctrl_new_free(&ctrl->u.new);
break;
case CTRL_DFF:
ctrl_dff_free(&ctrl->u.dff);
break;
case CTRL_DFX:
ctrl_dfx_free(&ctrl->u.dfx);
break;
case CTRL_CPY:
ctrl_cpy_free(&ctrl->u.cpy);
break;
case CTRL_DEL:
ctrl_del_free(&ctrl->u.del);
break;
default:
assert(0);
}
return ;
}
void
patch_total(drb_t *drb, int size)
{
uint8_t *p = drb_check(drb, 4);
uint32(p) = size; p += 4;
drb_end(drb, p);
}
const uint8_t *
patch_total_read(const uint8_t *p, uint32_t *size)
{
*size = uint32(p); p += 4;
return p;
}
void
patch_copy(drb_t *drb, struct COPY *copy)
{
uint8_t *p = drb_check(drb, 1+4+4);
uint8(p) = PATCH_COPY; p += 1;
uint32(p) = copy->pos; p += 4;
uint32(p) = copy->size; p += 4;
drb_end(drb, p);
return ;
}
void
patch_insert(drb_t *drb, struct INSERT *insert)
{
int size = insert->size;
uint8_t *p = drb_check(drb, 1+4+size);
uint8(p) = PATCH_INSERT; p += 1;
uint32(p) = size; p += 4;
memcpy(p, insert->p, size); p += size;
drb_end(drb, p);
}
static const uint8_t *
patch_copy_read(const uint8_t *p, struct COPY *copy)
{
copy->pos = uint32(p); p += 4;
copy->size = uint32(p); p += 4;
return p;
}
static const uint8_t *
patch_insert_read(const uint8_t *p, struct INSERT *insert)
{
insert->size = uint32(p); p += 4;
insert->p = p; p += insert->size;
return p;
}
const uint8_t *
patch_read(const uint8_t *p, struct PATCH *patch)
{
patch->act = uint8(p); p += 1;
if (patch->act == PATCH_COPY)
return patch_copy_read(p, &patch->u.copy);
else if (patch->act == PATCH_INSERT)
return patch_insert_read(p, &patch->u.insert);
else
assert(0);
return NULL;
}