forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnbt.cpp
More file actions
416 lines (373 loc) · 7.95 KB
/
nbt.cpp
File metadata and controls
416 lines (373 loc) · 7.95 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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
Copyright (c) 2013, Sean Kasun
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <QFile>
#include <QByteArray>
#include <QDebug>
#include "nbt.h"
#include <zlib.h>
// this handles decoding the gzipped level.dat
NBT::NBT(const QString level)
{
root=&NBT::Null; //just in case we die
QFile f(level);
f.open(QIODevice::ReadOnly);
QByteArray data=f.readAll();
f.close();
QByteArray nbt;
z_stream strm;
static const int CHUNK_SIZE = 8192;
char out[CHUNK_SIZE];
strm.zalloc=Z_NULL;
strm.zfree=Z_NULL;
strm.opaque=Z_NULL;
strm.avail_in=data.size();
strm.next_in=(Bytef*)data.data();
inflateInit2(&strm,15+32);
do {
strm.avail_out=CHUNK_SIZE;
strm.next_out=(Bytef*)out;
inflate(&strm,Z_NO_FLUSH);
nbt.append(out,CHUNK_SIZE-strm.avail_out);
} while (strm.avail_out==0);
inflateEnd(&strm);
TagDataStream s(nbt.constData(),nbt.size());
if (s.r8()==10) //compound
{
s.skip(s.r16()); //skip name
root=new Tag_Compound(s);
}
}
//this handles decoding a compressed() section of a region file
NBT::NBT(const uchar *chunk)
{
root=&NBT::Null; //just in case
// find chunk size
int length=(chunk[0]<<24)|(chunk[1]<<16)|(chunk[2]<<8)|chunk[3];
if (chunk[4]!=2) //rfc1950
return;
z_stream strm;
static const int CHUNK_SIZE = 8192;
char out[CHUNK_SIZE];
strm.zalloc=Z_NULL;
strm.zfree=Z_NULL;
strm.opaque=Z_NULL;
strm.avail_in=length-1;
strm.next_in=(Bytef*)chunk+5;
QByteArray nbt;
inflateInit(&strm);
do {
strm.avail_out=CHUNK_SIZE;
strm.next_out=(Bytef*)out;
inflate(&strm,Z_NO_FLUSH);
nbt.append(out,CHUNK_SIZE-strm.avail_out);
} while (strm.avail_out==0);
inflateEnd(&strm);
TagDataStream s(nbt.constData(),nbt.size());
if (s.r8()==10) //compound
{
s.skip(s.r16()); //skip name
root=new Tag_Compound(s);
}
}
Tag NBT::Null;
bool NBT::has(const QString key)
{
return root->has(key);
}
Tag *NBT::at(const QString key)
{
return root->at(key);
}
NBT::~NBT()
{
if (root!=&NBT::Null)
delete root;
}
/********** TAGS ****************/
Tag::Tag()
{
}
Tag::~Tag()
{
}
int Tag::length()
{
qWarning()<<"Unhandled length";
return 0;
}
bool Tag::has(const QString)
{
return false;
}
Tag *Tag::at(const QString)
{
return &NBT::Null;
}
Tag *Tag::at(int)
{
return &NBT::Null;
}
QString Tag::toString()
{
qWarning()<<"Unhandled toString";
return "";
}
qint32 Tag::toInt()
{
qWarning()<<"Unhandled toInt";
return 0;
}
double Tag::toDouble()
{
qWarning()<<"Unhandled toDouble";
return 0.0;
}
const quint8 *Tag::toByteArray()
{
qWarning()<<"Unhandled toByteArray";
return NULL;
}
const qint32 *Tag::toIntArray()
{
qWarning()<<"Unhandled toIntArray";
return NULL;
}
Tag_Byte::Tag_Byte(TagDataStream &s)
{
data=s.r8();
}
int Tag_Byte::toInt()
{
return data;
}
Tag_Short::Tag_Short(TagDataStream &s)
{
data=s.r16();
}
Tag_Int::Tag_Int(TagDataStream &s)
{
data=s.r32();
}
qint32 Tag_Int::toInt()
{
return data;
}
double Tag_Int::toDouble()
{
return (double)data;
}
Tag_Long::Tag_Long(TagDataStream &s)
{
data=s.r64();
}
Tag_Float::Tag_Float(TagDataStream &s)
{
union {qint32 d; float f;} fl;
fl.d=s.r32();
data=fl.f;
}
Tag_Double::Tag_Double(TagDataStream &s)
{
union {qint64 d; double f;} fl;
fl.d=s.r64();
data=fl.f;
}
double Tag_Double::toDouble()
{
return data;
}
Tag_Byte_Array::Tag_Byte_Array(TagDataStream &s)
{
len=s.r32();
data=s.r(len);
}
Tag_Byte_Array::~Tag_Byte_Array()
{
delete[] data;
}
int Tag_Byte_Array::length()
{
return len;
}
const quint8 *Tag_Byte_Array::toByteArray()
{
return data;
}
Tag_String::Tag_String(TagDataStream &s)
{
int len=s.r16();
data=s.utf8(len);
}
QString Tag_String::toString()
{
return data;
}
template <class T>
static void setListData(QList<Tag *> &data,int len,TagDataStream &s)
{
for (int i=0;i<len;i++)
data.append(new T(s));
}
Tag_List::Tag_List(TagDataStream &s)
{
quint8 type=s.r8();
int len=s.r32();
switch (type)
{
case 1: setListData<Tag_Byte>(data,len,s); break;
case 2: setListData<Tag_Short>(data,len,s); break;
case 3: setListData<Tag_Int>(data,len,s); break;
case 4: setListData<Tag_Long>(data,len,s); break;
case 5: setListData<Tag_Float>(data,len,s); break;
case 6: setListData<Tag_Double>(data,len,s); break;
case 7: setListData<Tag_Byte_Array>(data,len,s); break;
case 8: setListData<Tag_String>(data,len,s); break;
case 9: setListData<Tag_List>(data,len,s); break;
case 10: setListData<Tag_Compound>(data,len,s); break;
case 11: setListData<Tag_Int_Array>(data,len,s); break;
default:
throw "Unknown type";
}
}
Tag_List::~Tag_List()
{
QList<Tag *>::const_iterator i;
for (i=data.constBegin();i!=data.constEnd();i++)
delete *i;
}
int Tag_List::length()
{
return data.count();
}
Tag *Tag_List::at(int index)
{
return data[index];
}
Tag_Compound::Tag_Compound(TagDataStream &s)
{
quint8 type;
while ((type=s.r8())!=0) //until tag_end
{
quint16 len=s.r16();
QString key=s.utf8(len);
Tag *child;
switch (type)
{
case 1: child=new Tag_Byte(s); break;
case 2: child=new Tag_Short(s); break;
case 3: child=new Tag_Int(s); break;
case 4: child=new Tag_Long(s); break;
case 5: child=new Tag_Float(s); break;
case 6: child=new Tag_Double(s); break;
case 7: child=new Tag_Byte_Array(s); break;
case 8: child=new Tag_String(s); break;
case 9: child=new Tag_List(s); break;
case 10: child=new Tag_Compound(s); break;
case 11: child=new Tag_Int_Array(s); break;
default: throw "Unknown tag";
}
children[key]=child;
}
}
Tag_Compound::~Tag_Compound()
{
QHash<QString, Tag*>::const_iterator i;
for (i=children.constBegin();i!=children.constEnd();i++)
delete i.value();
}
bool Tag_Compound::has(const QString key)
{
return children.contains(key);
}
Tag *Tag_Compound::at(const QString key)
{
if (!children.contains(key))
return &NBT::Null;
return children[key];
}
Tag_Int_Array::Tag_Int_Array(TagDataStream &s)
{
len=s.r32();
data=new qint32[len];
for (int i=0;i<len;i++)
data[i]=s.r32();
}
Tag_Int_Array::~Tag_Int_Array()
{
delete[] data;
}
const qint32 *Tag_Int_Array::toIntArray()
{
return data;
}
int Tag_Int_Array::length()
{
return len;
}
TagDataStream::TagDataStream(const char *data,int len)
{
this->data=(const quint8 *)data;
this->len=len;
pos=0;
}
quint8 TagDataStream::r8()
{
return data[pos++];
}
quint16 TagDataStream::r16()
{
quint16 r=data[pos++]<<8;
r|=data[pos++];
return r;
}
quint32 TagDataStream::r32()
{
quint32 r=data[pos++]<<24;
r|=data[pos++]<<16;
r|=data[pos++]<<8;
r|=data[pos++];
return r;
}
quint64 TagDataStream::r64()
{
quint64 r=(quint64)r32()<<32;
r|=r32();
return r;
}
quint8 *TagDataStream::r(int len) //you need to free anything read with this
{
quint8 *r=new quint8[len];
memcpy(r,data+pos,len);
pos+=len;
return r;
}
QString TagDataStream::utf8(int len)
{
int old=pos;
pos+=len;
return QString::fromUtf8((const char *)data+old,len);
}
void TagDataStream::skip(int len)
{
pos+=len;
}