-
Notifications
You must be signed in to change notification settings - Fork 1
/
object.c
202 lines (183 loc) · 4.36 KB
/
object.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
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "object.h"
#define MALLOC malloc
#define FREE free
static void
ref_destroy(struct ref *r)
{
dr_unref(r->hash);
dr_unref(r->name);
dr_unref(r->data);
};
static void
ref_clone(struct ref *to, struct ref *from)
{
to->hash = dr_ref(from->hash);
to->name = dr_ref(from->name);
to->data = dr_ref(from->data);
}
int
ref_hashcmp(const void *aa, const void *bb)
{
struct ref *a = (struct ref *)aa;
struct ref *b = (struct ref *)bb;
return strcmp((char *)a->hash->buf, (char *)b->hash->buf);
}
int
ref_namecmp(const void *aa, const void *bb)
{
struct ref *a = (struct ref *)aa;
struct ref *b = (struct ref *)bb;
return strcmp((char *)a->name->buf, (char *)b->name->buf);
}
dr_t
blob_marshal(struct blob *b)
{
return dr_ref(b->data);
}
void
blob_unmarshal(struct blob *b, dr_t data)
{
b->data = dr_ref(data);
return ;
}
void
blob_destroy(struct blob *b)
{
}
dr_t
tree_marshal(struct tree *t)
{
dr_t d;
int i, sz;
char *ptr;
struct ref *refs = t->refs;
sz = 4 + //type
4; //refn
for (i = 0; i < t->refn; i++) {
struct ref *r = &refs[i];
sz += 4 + r->hash->size + 4 + r->name->size;
}
d = dr_new(sz, NULL);
ptr = (char*)d->buf;
*(uint32_t *)ptr = TREE; ptr += 4;
*(uint32_t *)ptr = t->refn; ptr += 4;
for (i = 0; i < t->refn; i++) {
struct ref *r = &refs[i];
*(uint32_t *)ptr = r->hash->size; ptr += 4;
memcpy(ptr, r->hash->buf, r->hash->size);ptr += r->hash->size;
*(uint32_t *)ptr = r->name->size; ptr += 4;
memcpy(ptr, r->name->buf, r->name->size);ptr += r->name->size;
}
return d;
}
void
tree_unmarshal(struct tree *t, dr_t d)
{
int type, i, size;
uint8_t *ptr = d->buf;
type = *(uint32_t *)ptr; ptr += 4;
assert(type == TREE);
t->refn = *(uint32_t *)ptr; ptr += 4;
size = t->refn * sizeof(struct ref);
t->refs = MALLOC(size);
memset(t->refs, 0, size);
for (i = 0; i < t->refn; i++) {
struct ref *r = &t->refs[i];
size = *(uint32_t *)ptr; ptr += 4;
r->hash = dr_new(size, ptr); ptr += size;
size = *(uint32_t *)ptr; ptr += 4;
r->name = dr_new(size, ptr); ptr += size;
}
return ;
}
void
tree_destroy(struct tree *t)
{
int i;
for (i = 0; i < t->refn; i++)
ref_destroy(&t->refs[i]);
FREE(t->refs);
return ;
}
void
tree_sort(struct tree *t, compar_t cb)
{
qsort(t->refs, t->refn, sizeof(struct ref), cb);
}
struct ref *
tree_search(struct tree *t, struct ref *k, compar_t cb)
{
return bsearch(k, t->refs, t->refn, sizeof(*k), cb);
}
void
tree_clone(struct tree *to, struct tree *from)
{
int i;
to->refn = from->refn;
to->refs = MALLOC(from->refn * sizeof(struct ref));
for (i = 0; i < from->refn; i++)
ref_clone(&to->refs[i], &from->refs[i]);
return ;
}
dr_t
release_marshal(struct release *r)
{
dr_t d;
int sz;
uint8_t *ptr;
sz = 4 + //type
sizeof(uint64_t) + //time
4 + r->prev->size + //prev
4 + r->tree->size + //tree
4 + r->ver->size + //ver
4 + r->note->size + //note
4 + r->finger->size; //finger
d = dr_new(sz, NULL);
ptr = d->buf;
*(uint32_t *)ptr = RELEASE; ptr += 4;
*(uint64_t *)ptr = r->time; ptr += sizeof(uint64_t);
*(uint32_t *)ptr = r->prev->size; ptr += 4;
memcpy(ptr, r->prev->buf, r->prev->size); ptr += r->prev->size;
*(uint32_t *)ptr = r->tree->size; ptr += 4;
memcpy(ptr, r->tree->buf, r->tree->size); ptr += r->tree->size;
*(uint32_t *)ptr = r->ver->size; ptr += 4;
memcpy(ptr, r->ver->buf, r->ver->size); ptr += r->ver->size;
*(uint32_t *)ptr = r->note->size; ptr += 4;
memcpy(ptr, r->note->buf, r->note->size); ptr += r->note->size;
*(uint32_t *)ptr = r->finger->size; ptr += 4;
memcpy(ptr, r->finger->buf, r->finger->size);
return d;
}
void
release_unmarshal(struct release *r, dr_t d)
{
int type, size;
uint8_t *ptr = d->buf;
type = *(uint32_t *)ptr; ptr += 4;
assert(type == RELEASE);
r->time = *(uint64_t *)ptr; ptr += sizeof(uint64_t);
size = *(uint32_t *)ptr; ptr += 4;
r->prev = dr_new(size, ptr); ptr += size;
size = *(uint32_t *)ptr; ptr += 4;
r->tree = dr_new(size, ptr); ptr += size;
size = *(uint32_t *)ptr; ptr += 4;
r->ver = dr_new(size, ptr); ptr += size;
size = *(uint32_t *)ptr; ptr += 4;
r->note = dr_new(size, ptr); ptr += size;
size = *(uint32_t *)ptr; ptr += 4;
r->finger = dr_new(size, ptr);
return ;
}
void
release_destroy(struct release *r)
{
dr_unref(r->prev);
dr_unref(r->tree);
dr_unref(r->ver);
dr_unref(r->note);
return ;
}