forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbt.cpp
602 lines (529 loc) · 10.5 KB
/
nbt.cpp
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
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 <QStringList>
#include "nbt.h"
#include "zlib/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;
}
QVariant Tag::getData()
{
qWarning()<<"Unhandled getData";
return QVariant();
}
Tag_Byte::Tag_Byte(TagDataStream &s)
{
data=s.r8();
}
int Tag_Byte::toInt()
{
return data;
}
QString Tag_Byte::toString()
{
return QString::number(data);
}
QVariant Tag_Byte::getData()
{
return data;
}
Tag_Short::Tag_Short(TagDataStream &s)
{
data=s.r16();
}
int Tag_Short::toInt()
{
return data;
}
QString Tag_Short::toString()
{
return QString::number(data);
}
QVariant Tag_Short::getData()
{
return data;
}
Tag_Int::Tag_Int(TagDataStream &s)
{
data=s.r32();
}
qint32 Tag_Int::toInt()
{
return data;
}
QString Tag_Int::toString()
{
return QString::number(data);
}
QVariant Tag_Int::getData()
{
return data;
}
double Tag_Int::toDouble()
{
return (double)data;
}
Tag_Long::Tag_Long(TagDataStream &s)
{
data=s.r64();
}
double Tag_Long::toDouble()
{
return (double)data;
}
qint32 Tag_Long::toInt()
{
return (double)data;
}
QString Tag_Long::toString()
{
return QString::number(data);
}
QVariant Tag_Long::getData()
{
return data;
}
Tag_Float::Tag_Float(TagDataStream &s)
{
union {qint32 d; float f;} fl;
fl.d=s.r32();
data=fl.f;
}
double Tag_Float::toDouble()
{
return data;
}
QString Tag_Float::toString()
{
return QString::number(data);
}
QVariant Tag_Float::getData()
{
return data;
}
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;
}
QVariant Tag_Double::getData()
{
return data;
}
QString Tag_Double::toString()
{
return QString::number(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;
}
QVariant Tag_Byte_Array::getData()
{
return QByteArray(reinterpret_cast<const char*>(data), len);
}
QString Tag_Byte_Array::toString()
{
try
{
return QString::fromLatin1((char*)data);
}
catch(...)
{
}
return "<Binary data>";
}
Tag_String::Tag_String(TagDataStream &s)
{
int len=s.r16();
data=s.utf8(len);
}
QString Tag_String::toString()
{
return data;
}
QVariant Tag_String::getData()
{
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();
if (len==0) //empty list, type is invalid
return;
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];
}
QString Tag_List::toString()
{
QStringList ret;
ret << "[";
QList<Tag*>::iterator it, end = data.end();
for (it = data.begin(); it != end; ++it)
{
ret << (*it)->toString();
ret << ", ";
}
ret.last() = "]";
return ret.join("");
}
QVariant Tag_List::getData()
{
QList<QVariant> list;
QList<Tag*>::iterator it, end = data.end();
for (it = data.begin(); it != end; ++it)
{
list << (*it)->getData();
}
return list;
}
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];
}
QString Tag_Compound::toString()
{
QStringList ret;
ret << "{\n";
QHash<QString, Tag *>::iterator it, end = children.end();
for (it = children.begin(); it != end; ++it)
{
ret << "\t" << it.key() << " = '" << it.value()->toString() << "',\n";
}
ret.last() = "}";
return ret.join("");
}
QVariant Tag_Compound::getData()
{
QMap<QString, QVariant> map;
QHash<QString, Tag *>::iterator it, end = children.end();
for (it = children.begin(); it != end; ++it)
{
map.insert(it.key(), it.value()->getData());
}
return map;
}
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;
}
QString Tag_Int_Array::toString()
{
QStringList ret;
ret << "[";
for (int i = 0; i < len; ++i)
{
ret << QString::number(data[i]) << ",";
}
ret.last() = "]";
return ret.join("");
}
QVariant Tag_Int_Array::getData()
{
QList<QVariant> ret;
for (int i = 0; i < len; ++i)
{
ret.push_back(data[i]);
}
return ret;
}
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;
}