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
|
#include "malloc.h" // realloc() and free()
#include <string.h> // strncasecmp()
#include "file.h"
// how is our flag organized?
#define FLAG ( 0xBF )
#define FLAG_VALID(flag) (flag == 0xBF)
static int do_resize(struct file_entry *e, const uint32_t name_len, const int zero_fill);
struct file_entry* new_file_entry(const uint32_t name_len) {
// Start my allocating memory
struct file_entry *e = (struct file_entry*)malloc(sizeof(struct file_entry));
if( e == NULL ) {
DEBUGF("new_file_entry: could not allocate memory\n");
return NULL;
}
// We begin empty
e->name = NULL;
e->size.name_len = 0;
e->hash = 0;
e->song = 0;
e->rundb = 0;
e->flag = FLAG;
// and resize to the requested size
if( do_resize(e, name_len, 1) ) {
free(e);
return NULL;
}
return e;
}
int file_entry_destruct(struct file_entry *e) {
assert(e != NULL);
assert(FLAG_VALID(e->flag));
free(e->name);
free(e);
return ERR_NONE;
}
static int do_resize(struct file_entry *e, const uint32_t name_len, const int zero_fill) {
void* temp;
assert(e != NULL);
assert(FLAG_VALID(e->flag));
if( name_len != e->size.name_len ) {
temp = realloc(e->name, name_len);
if(temp == NULL) {
DEBUGF("file_entry_resize: out of memory to resize name\n");
return ERR_MALLOC;
}
e->name = (char*)temp;
// if asked, fill it with zero's
if( zero_fill ) {
uint32_t i;
for(i=e->size.name_len; i<name_len; i++)
e->name[i] = (char)0x00;
}
e->size.name_len = name_len;
}
return ERR_NONE;
}
inline int file_entry_resize(struct file_entry *e, const uint32_t name_len) {
return do_resize(e, name_len, 1);
}
int file_entry_serialize(FILE *fd, const struct file_entry *e) {
assert(fd != NULL);
assert(e != NULL);
assert(FLAG_VALID(e->flag));
// First byte we write is a flag-byte to indicate this is a valid record
if( fwrite(&e->flag, 1, 1, fd) != 1 ) {
DEBUGF("file_entry_serialize: failed to write flag-byte\n");
return ERR_FILE;
}
// First we write the length of the name field
if( fwrite(&e->size.name_len, sizeof(e->size.name_len), 1, fd) != 1 ) {
DEBUGF("file_entry_serialize: failed to write name_len\n");
return ERR_FILE;
}
// now the name field itself
if( fwrite(e->name, 1, e->size.name_len, fd) != e->size.name_len ) {
DEBUGF("file_entry_serialize: failed to write name\n");
return ERR_FILE;
}
// hash field
if( fwrite(&e->hash, sizeof(e->hash), 1, fd) != 1 ) {
DEBUGF("file_entry_serialize: failed to write hash\n");
return ERR_FILE;
}
// song field
if( fwrite(&e->song, sizeof(e->song), 1, fd) != 1 ) {
DEBUGF("file_entry_serialize: failed to write song\n");
return ERR_FILE;
}
// rundb field
if( fwrite(&e->rundb, sizeof(e->rundb), 1, fd) != 1 ) {
DEBUGF("file_entry_serialize: failed to write rundb\n");
return ERR_FILE;
}
return ERR_NONE;
}
int file_entry_unserialize(struct file_entry **dest, FILE *fd) {
uint32_t length;
struct file_entry *e;
assert(dest != NULL);
assert(fd != NULL);
// Allocate memory
e = new_file_entry(0);
if( e == NULL ) {
DEBUGF("file_entry_unserialize: could not create new file_entry\n");
return ERR_MALLOC;
}
// First we read the length of the name field
if( fread(&length, sizeof(length), 1, fd) != 1 ) {
DEBUGF("file_entry_unserialize: failed to read name_len\n");
file_entry_destruct(e);
return ERR_FILE;
}
// allocate memory for the upcomming name-field
if( do_resize(e, length, 0) ) {
DEBUGF("file_entry_unserialize: failed to allocate memory for name\n");
file_entry_destruct(e);
return ERR_MALLOC;
}
// read it in
if( fread(e->name, 1, e->size.name_len, fd) != e->size.name_len ) {
DEBUGF("file_entry_unserialize: failed to read name\n");
file_entry_destruct(e);
return ERR_FILE;
}
// hash field
if( fread(&e->hash, sizeof(e->hash), 1, fd) != 1 ) {
DEBUGF("file_entry_unserialize: failed to read hash\n");
file_entry_destruct(e);
return ERR_FILE;
}
// song field
if( fread(&e->song, sizeof(e->song), 1, fd) != 1 ) {
DEBUGF("file_entry_unserialize: failed to read song\n");
file_entry_destruct(e);
return ERR_FILE;
}
// rundb field
if( fread(&e->rundb, sizeof(e->rundb), 1, fd) != 1 ) {
DEBUGF("file_entry_unserialize: failed to read rundb\n");
file_entry_destruct(e);
return ERR_FILE;
}
*dest = e;
return ERR_NONE;
}
int file_entry_write(FILE *fd, struct file_entry *e, struct file_size *s) {
uint32_t be32;
char pad = 0x00;
assert(fd != NULL);
assert(e != NULL);
assert(FLAG_VALID(e->flag));
// file name
if( fwrite(e->name, 1, e->size.name_len, fd) != e->size.name_len ) {
DEBUGF("file_entry_write: failed to write name\n");
return ERR_FILE;
}
// pad the rest
be32 = e->size.name_len; // abuse be32 as counter
while( s != NULL && s->name_len > be32) {
if( fwrite(&pad, 1, 1, fd) == 1 ) {
be32++;
} else {
DEBUGF("file_entry_write: failed to pad name\n");
return ERR_FILE;
}
}
// hash
be32 = BE32(e->hash);
if( fwrite(&be32, sizeof(be32), 1, fd) != 1 ) {
DEBUGF("file_entry_write: failed to write hash\n");
return ERR_FILE;
}
// song
be32 = BE32(e->song);
if( fwrite(&be32, sizeof(be32), 1, fd) != 1 ) {
DEBUGF("file_entry_write: failed to write song\n");
return ERR_FILE;
}
// rundb
be32 = BE32(e->rundb);
if( fwrite(&be32, sizeof(be32), 1, fd) != 1 ) {
DEBUGF("file_entry_write: failed to write rundb\n");
return ERR_FILE;
}
return ERR_NONE;
}
inline int file_entry_compare(const struct file_entry *a, const struct file_entry *b) {
assert(a != NULL);
assert(b != NULL);
return strncasecmp(a->name, b->name, (a->size.name_len <= b->size.name_len ? a->size.name_len : b->size.name_len) );
}
struct file_size* new_file_size() {
struct file_size *s;
s = (struct file_size*)malloc(sizeof(struct file_size));
if( s == NULL ) {
DEBUGF("new_file_size: failed to allocate memory\n");
return NULL;
}
s->name_len = 0;
return s;
}
inline uint32_t file_size_get_length(const struct file_size *size) {
assert(size != NULL);
return size->name_len + 12;
}
inline int file_size_max(struct file_size *s, const struct file_entry *e) {
assert(s != NULL);
assert(e != NULL);
assert(FLAG_VALID(e->flag));
s->name_len = ( s->name_len >= e->size.name_len ? s->name_len : e->size.name_len );
return ERR_NONE;
}
int file_size_destruct(struct file_size *s) {
assert(s != NULL);
// nothing to do...
free(s);
return ERR_NONE;
}
|