nbt.c
6.87 KB
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
#include "nbt.h"
#include "compression.h"
#include "chunk.h"
ssize_t loadDB(const char* filename, void** data);
void destroyTag(Tag* t);
void destroyTagList(TagList* l);
void destroyTagCompound(TagCompound* tc);
size_t getTypeSize(uint8_t type);
unsigned int parseList(void* addr, TagList* tl, uint8_t type);
unsigned int parseCompound(void* addr, TagCompound* tc);
unsigned int parsePayload(void* addr,Tag* t);
unsigned int parseTag(void* addr, Tag* t);
ssize_t loadDB(const char* filename, void** data) {
if(access(filename,R_OK) == -1) {
perror("Can't access file");
return -1;
}
struct stat sb;
unsigned int pos = 0;
int fd = open(filename,O_RDONLY);
if(fd == -1) {
perror("Can't open file");
return -2;
}
fstat(fd, &sb);
void* filedata;
ssize_t filesize = sb.st_size;
filedata = malloc(filesize);
ssize_t nRead = 0;
size_t totalRead = 0;
while((nRead = read(fd,filedata+totalRead,filesize-totalRead))) {
if(nRead == -1) {
if(errno == EINTR) {
continue;
}
perror("Error reading file");
return -7;
}
totalRead += nRead;
}
if(*(uint16_t*)filedata == GZIP_MAGIC) {
void* decompressedFileData;
filesize = inflateGzip(filedata,filesize,&decompressedFileData,0);
free(filedata);
filedata = decompressedFileData;
}
*data = filedata;
return filesize;
}
void destroyTag(Tag* t) {
if(t->nameLength) {
free(t->name);
}
if(t->payloadLength) {
if(t->type == TAG_BYTEARRAY || t->type == TAG_INTARRAY || t->type == TAG_LIST) {
destroyTagList((TagList*)t->payload);
} else if(t->type == TAG_COMPOUND) {
destroyTagCompound((TagCompound*)t->payload);
}
free(t->payload);
}
}
void destroyTagList(TagList* l) {
for(int i = 0; i < l->size; ++i) {
destroyTag(&l->list[i]);
}
free(l->list);
}
void destroyTagCompound(TagCompound* tc) {
for(int i = 0; i < tc->numTags; ++i) {
destroyTag(&tc->list[i]);
}
free(tc->list);
}
size_t getTypeSize(uint8_t type) {
switch(type) {
case TAG_BYTE:
return sizeof(uint8_t);
break;
case TAG_SHORT:
return sizeof(uint16_t);
break;
case TAG_INT:
return sizeof(uint32_t);
break;
case TAG_LONG:
return sizeof(uint64_t);
break;
case TAG_FLOAT:
return sizeof(float);
break;
case TAG_DOUBLE:
return sizeof(double);
break;
default:
break;
}
return 0;
}
unsigned int parseList(void* addr, TagList* tl, uint8_t type) {
void* pos = addr;
if(type == TAG_LIST) {
tl->type = *((uint8_t*)pos);
pos += sizeof(uint8_t);
} else if(type == TAG_BYTEARRAY) {
tl->type = TAG_BYTE;
} else if(type == TAG_INTARRAY) {
tl->type = TAG_INT;
}
tl->size = __bswap_32(*((uint32_t*)pos));
pos += sizeof(uint32_t);
tl->list = NULL;
if(tl->type != TAG_END) {
tl->list = calloc(tl->size,sizeof(Tag));
for(int i = 0; i < tl->size; ++i) {
Tag *t = &tl->list[i];
t->type = tl->type;
t->name = NULL;
t->nameLength = 0;
pos += parsePayload(pos,t);
}
}
return pos - addr;
}
unsigned int parseCompound(void* addr, TagCompound* tc) {
void* pos = addr;
unsigned int numTags = 0;
Tag* list = calloc(REALLOC_SIZE,sizeof(Tag));
do {
if(numTags && !(numTags % REALLOC_SIZE)) {
void* newptr = reallocarray(list, numTags + REALLOC_SIZE, sizeof(Tag));
if(!newptr) {
fprintf(stderr,"Unable to request memory realloc\n");
break;
}
list = newptr;
}
pos += parseTag(pos,&list[numTags]);
} while(list[numTags++].type != TAG_END);
void* newptr = reallocarray(list, numTags, sizeof(Tag));
if(!newptr) {
fprintf(stderr,"Unable to request memory realloc\n");
}
list = newptr;
tc->list = list;
tc->numTags = numTags-1;
return pos - addr;
}
unsigned int parsePayload(void* addr,Tag* t) {
void* pos = addr;
t->payloadLength = getTypeSize(t->type); // initially, then particularly for lists/compounds/strings
TagCompound* tc;
TagList *tl;
uint16_t u16 = 0;
uint32_t u32 = 0;
uint64_t u64 = 0;
switch(t->type) {
case TAG_BYTE:
t->payload = calloc(1,t->payloadLength);
memcpy(t->payload,pos,t->payloadLength);
pos += t->payloadLength;
break;
case TAG_SHORT:
u16 = __bswap_16(*(uint16_t*)pos);
t->payload = calloc(1,t->payloadLength);
memcpy(t->payload,&u16,t->payloadLength);
pos += t->payloadLength;
break;
case TAG_INT:
case TAG_FLOAT:
u32 = __bswap_32(*(uint32_t*)pos);
t->payload = calloc(1,t->payloadLength);
memcpy(t->payload,&u32,t->payloadLength);
pos += t->payloadLength;
break;
case TAG_LONG:
case TAG_DOUBLE:
u64 = __bswap_64(*(uint64_t*)pos);
t->payload = calloc(1,t->payloadLength);
memcpy(t->payload,&u64,t->payloadLength);
pos += t->payloadLength;
break;
case TAG_STRING:
t->payloadLength = __bswap_16(*((uint16_t*)pos));
t->payload = NULL;
if(t->payloadLength) {
t->payload = calloc(t->payloadLength,sizeof(char));
memcpy(t->payload,pos+sizeof(uint16_t),t->payloadLength);
}
pos += sizeof(uint16_t) + t->payloadLength;
break;
case TAG_COMPOUND:
tc = (TagCompound*)calloc(1,sizeof(TagCompound));
t->payloadLength = sizeof(sizeof(TagCompound));
t->payload = tc;
pos += parseCompound(pos,tc);
break;
case TAG_LIST:
case TAG_BYTEARRAY:
case TAG_INTARRAY:
tl = (TagList*)calloc(1,sizeof(TagList));
t->payloadLength = sizeof(sizeof(TagList));
t->payload = tl;
pos += parseList(pos,tl,t->type);
break;
}
return pos - addr;
}
unsigned int parseTag(void* addr, Tag* t) {
void* pos = addr;
t->type = *((uint8_t*)pos);
t->nameLength = 0;
t->payloadLength = 0;
pos += sizeof(uint8_t);
if(t->type != TAG_END) {
t->nameLength = __bswap_16(*((uint16_t*)pos));
t->name = NULL;
if(t->nameLength) {
t->name = calloc(t->nameLength,sizeof(char));
memcpy(t->name,pos+sizeof(uint16_t),t->nameLength);
}
pos += sizeof(uint16_t) + t->nameLength;
}
pos += parsePayload(pos,t);
return pos-addr;
}