summaryrefslogtreecommitdiffstats
path: root/converter/cNBTData.cpp
blob: 556960732890b9f58feb73f16202cad49a4d95f3 (plain) (blame)
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
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
#include "cNBTData.h"
#include <string> // memcpy
#include <stdio.h>
#include "zlib.h"
#include <assert.h>
#include <iostream>

#ifndef _WIN32
#include <cstring>
#include <netinet/in.h>
#endif

#ifdef _WIN32
#include <WinSock2.h>
#endif

cNBTData::~cNBTData()
{
    // TODO: Delete all compounds and stuff in it
    Clear();
}

cNBTData::cNBTData( char* a_Buffer, unsigned int a_BufferSize )
    : cNBTCompound( 0 )
{
    m_NumUnnamedElements = 0;
    for(int i = 0; i < TAG_NumTags; i++)
    {
        m_ParseFunctions[i] = 0;
    }
    m_ParseFunctions[TAG_Byte]      = &cNBTData::ParseByte;
    m_ParseFunctions[TAG_Short]     = &cNBTData::ParseShort;
    m_ParseFunctions[TAG_Int]       = &cNBTData::ParseInt;
    m_ParseFunctions[TAG_Long]      = &cNBTData::ParseLong;
    m_ParseFunctions[TAG_Double]    = &cNBTData::ParseDouble;
    m_ParseFunctions[TAG_Float]     = &cNBTData::ParseFloat;
    m_ParseFunctions[TAG_String]    = &cNBTData::ParseString;
    m_ParseFunctions[TAG_List]      = &cNBTData::ParseList;
    m_ParseFunctions[TAG_Compound]  = &cNBTData::ParseCompound;
    m_ParseFunctions[TAG_ByteArray] = &cNBTData::ParseByteArray;


    m_Buffer = a_Buffer;
    m_BufferSize = a_BufferSize;
    m_Index = 0;

    tm = false; //tm to true will print more information for test mode
    if (m_BufferSize == 82659) {
//	tm = true;
    }

    m_CurrentCompound = this;

    m_bDecompressed = true;
}

bool cNBTData::OpenCompound( std::string a_Name )
{
    cNBTCompound* Compound = GetCompound( a_Name );
    if( Compound )
    {
        m_CurrentCompound = Compound;
        return true;
    }
    printf("WARNING: Could not open NBT Compound %s\n", a_Name.c_str() );
    return false;
}

bool cNBTData::CloseCompound()
{
    if( m_CurrentCompound->GetParentCompound() )
    {
        m_CurrentCompound = m_CurrentCompound->GetParentCompound();
        return true;
    }
    printf("WARNING: Could not close NBT Compound, already at root!\n" );
    return false;
}

bool cNBTCompound::OpenList( std::string a_Name )
{
    if( GetList( a_Name ) )
    {
        m_CurrentList = GetList( a_Name );
        return true;
    }
    printf("WARNING: Could not open NBT List %s\n", a_Name.c_str() );
    return false;
}

bool cNBTCompound::CloseList()
{
    if( m_CurrentList )
    {
        m_CurrentList = m_CurrentList->GetParentList();
        return true;
    }
    printf("WARNING: Could not close NBT List, no list open!\n" );
    return false;
}

bool cNBTData::OpenList( std::string a_Name )
{
    return m_CurrentCompound->OpenList( a_Name );
}

bool cNBTData::CloseList()
{
    return m_CurrentCompound->CloseList();
}

void cNBTData::Compress()
{
    //printf("Before Compress size: %i\n", m_BufferSize );//re
    const int MAXNBTSIZE = 1024 * 1024 * 120;

    int ret;
    unsigned have;
    z_stream strm;
    unsigned char* Compressed = new unsigned char[MAXNBTSIZE];

    /* allocate deflate state */
    strm.zalloc     = Z_NULL;
    strm.zfree      = Z_NULL;
    strm.opaque     = Z_NULL;
    strm.avail_in   = m_BufferSize;
    strm.avail_out  = MAXNBTSIZE;
    strm.next_in    =(Bytef*)m_Buffer;
    strm.next_out   = Compressed;
    strm.total_in   = 0;
    strm.total_out  = 0;
    ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15+MAX_WBITS, 8, Z_DEFAULT_STRATEGY);
    if (ret != Z_OK)
    {
        printf("deflateInit2 returned NOT OK\n");
        return;
    }



    /* run deflate() on input until output buffer not full, finish
       compression if all of source has been read in */

    ret = deflate(&strm, Z_FULL_FLUSH);    /* no bad return value */
    if( ret != Z_OK )
    {
        printf("WARNING: deflate returned NOT OK\n");
    }
    assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
    have = strm.total_out;

    assert(strm.avail_in == 0);     /* all input will be used */

    if( ret != Z_STREAM_END )
    {
        //printf("WARNING: Compressing didn't go to end of stream\n");//re
    }

    if(m_Buffer)
    {
        delete [] m_Buffer;
        m_Buffer = 0;
    }

    //printf("Compressed size: %i\n", have );//re

    m_BufferSize = have;
    m_Buffer = new char[ m_BufferSize ];
    memcpy( m_Buffer, Compressed, m_BufferSize );
    delete Compressed;

    /* clean up and return */
    deflateEnd(&strm);
    m_bDecompressed = false;
    return;
}

bool cNBTData::Decompress()
{
    if( m_bDecompressed )
    {
        printf("WARNING: Decompress called, while it has already been decompressed\n");
        return false;
    }
    if( m_BufferSize == 0 )
    {
        printf("WARNING: Decompress called, with m_BufferSize of 0\n");
        return false;
    }

    //printf("Before Decompress size: %i\n", m_BufferSize );//re

    const int MAXNBTSIZE = 1024 * 1024 * 120 ;

    int ret;
    z_stream strm;
    unsigned char* out = new unsigned char[MAXNBTSIZE];

    /* allocate inflate state */
    strm.zalloc     = Z_NULL;
    strm.zfree      = Z_NULL;
    strm.opaque     = Z_NULL;
    strm.avail_in   = Z_NULL;
    strm.next_in    = Z_NULL;
    strm.avail_in   = m_BufferSize;
    strm.avail_out  = Z_NULL;
    strm.next_in    = (Bytef*)m_Buffer;
    strm.next_out   = Z_NULL;
    strm.avail_out  = MAXNBTSIZE;
    strm.next_out   = out;
    strm.total_in   = 0;
    strm.total_out  = 0;

    ret = inflateInit2(&strm, 16+MAX_WBITS);
    if (ret != Z_OK)
    {
        printf("inflateInit2 returned NOT OK\n");
        delete out;
        return false;
    }

    if( (ret = inflate(&strm, Z_NO_FLUSH)) != Z_STREAM_END)
    {
        assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
        printf("ret != Z_STREAM_END\n");
    }
    unsigned UncompressedSize = strm.total_out; //MAXNBTSIZE - strm.avail_out;

    m_Buffer = new char[ UncompressedSize ];
    memcpy( m_Buffer, out, UncompressedSize );
    m_BufferSize = UncompressedSize;

    inflateEnd(&strm);
    delete [] out;

    if( ret != Z_STREAM_END )
    {
        printf("WARNING: NBT Data received was too big! (More than %i bytes)\n", MAXNBTSIZE);
    }

    //printf("Decompressed Size: %i\n", UncompressedSize );//re
    m_bDecompressed = true;
    return (ret == Z_STREAM_END) ? true : false;
}

void cNBTCompound::AppendShort( std::string & a_Buffer, short a_Value )
{
    a_Buffer.push_back( (char)((a_Value>>8)&0xff) );
    a_Buffer.push_back( (char)((a_Value)&0xff) );
}

void cNBTCompound::AppendInteger( std::string & a_Buffer, int a_Value )
{
    int NetVal = htonl( a_Value );
    a_Buffer.append( (char*)&NetVal, sizeof( int ) );
}

void cNBTCompound::Serialize(std::string & a_Buffer)
{
    //printf("cNBTCompound::Serialize()\n");//re
    for( CompoundMap::iterator itr = m_Compounds.begin(); itr != m_Compounds.end(); itr++ )
    {
        if( itr->second == 0 ) continue;
        a_Buffer.push_back( TAG_Compound );
        AppendShort( a_Buffer, (short)itr->first.size() );
        if( itr->first.size() > 0 )
        {
            a_Buffer.append( itr->first.c_str(), itr->first.size() );
        }
        itr->second->Serialize( a_Buffer );
        a_Buffer.push_back( TAG_End );
    }

    for( ListMap::iterator itr = m_Lists.begin(); itr != m_Lists.end(); itr++ )
    {
        if( itr->second == 0 ) continue;
        a_Buffer.push_back( TAG_List );
        AppendShort( a_Buffer, (short)itr->first.size() );
        if( itr->first.size() > 0 )
        {
            a_Buffer.append( itr->first.c_str(), itr->first.size() );
        }
        a_Buffer.push_back( (char)itr->second->GetType() );
        AppendInteger( a_Buffer, itr->second->GetSize() );
        itr->second->Serialize( a_Buffer );
    }

    for( IntegerMap::iterator itr = m_Integers.begin(); itr != m_Integers.end(); itr++ )
    {
        a_Buffer.push_back( TAG_Int );
        AppendShort( a_Buffer, (short)itr->first.size() );
        if( itr->first.size() > 0 )
        {
            a_Buffer.append( itr->first.c_str(), itr->first.size() );
        }
        AppendInteger( a_Buffer, itr->second );
    }

    for( ShortMap::iterator itr = m_Shorts.begin(); itr != m_Shorts.end(); itr++ )
    {
        a_Buffer.push_back( TAG_Short );
        AppendShort( a_Buffer, (short)itr->first.size() );
        if( itr->first.size() > 0 )
        {
            a_Buffer.append( itr->first.c_str(), itr->first.size() );
        }
        AppendShort( a_Buffer, itr->second );
    }

    for( ByteMap::iterator itr = m_Bytes.begin(); itr != m_Bytes.end(); itr++ )
    {
        a_Buffer.push_back( TAG_Byte );
        AppendShort( a_Buffer, (short)itr->first.size() );
        if( itr->first.size() > 0 )
        {
            a_Buffer.append( itr->first.c_str(), itr->first.size() );
        }
        a_Buffer.push_back( itr->second );
    }

    for( DoubleMap::iterator itr = m_Doubles.begin(); itr != m_Doubles.end(); itr++ )
    {
        a_Buffer.push_back( TAG_Double );
        AppendShort( a_Buffer, (short)itr->first.size() );
        if( itr->first.size() > 0 )
        {
            a_Buffer.append( itr->first.c_str(), itr->first.size() );
        }
        a_Buffer.push_back( itr->second );
    }

    for( FloatMap::iterator itr = m_Floats.begin(); itr != m_Floats.end(); itr++ )
    {
        a_Buffer.push_back( TAG_Float );
        AppendShort( a_Buffer, (short)itr->first.size() );
        if( itr->first.size() > 0 )
        {
            a_Buffer.append( itr->first.c_str(), itr->first.size() );
        }
        a_Buffer.push_back( itr->second );
    }

    for( LongMap::iterator itr = m_Longs.begin(); itr != m_Longs.end(); itr++ )
    {
        a_Buffer.push_back( TAG_Long );
        AppendShort( a_Buffer, (short)itr->first.size() );
        if( itr->first.size() > 0 )
        {
            a_Buffer.append( itr->first.c_str(), itr->first.size() );
        }
        a_Buffer.push_back( itr->second );
    }


}

void cNBTCompound::PrintData( int a_Depth, std::string a_Name )
{
    char* Prefix = new char[a_Depth*4+1];
    for(int i = 0; i < a_Depth*4; i++)
        Prefix[i] = ' ';
    Prefix[ a_Depth*4 ] = 0;

    if( a_Name.size() > 0 )
        printf("%sCOMPOUND (%s)\n", Prefix, a_Name.c_str() );
    else
        printf("%sCOMPOUND (...)\n", Prefix );

    delete Prefix;
    a_Depth++;
    Prefix = new char[a_Depth*4];
    for(int i = 0; i < a_Depth*4; i++)
        Prefix[i] = ' ';
    Prefix[ a_Depth*4-1 ] = 0;

    for( CompoundMap::iterator itr = m_Compounds.begin(); itr != m_Compounds.end(); itr++ )
    {
        if( itr->second == 0 ) continue;
        itr->second->PrintData( a_Depth, itr->first );
    }

    for( ListMap::iterator itr = m_Lists.begin(); itr != m_Lists.end(); itr++)
    {
        if( itr->second == 0 ) continue;
        itr->second->PrintData( a_Depth, itr->first );
    }

	for( StringMap::iterator itr = m_Strings.begin(); itr != m_Strings.end(); itr++ )
	{
		printf("%s STRING %s (%s)\n", Prefix, itr->first.c_str(), itr->second.c_str() );
	}

    for( IntegerMap::iterator itr = m_Integers.begin(); itr != m_Integers.end(); itr++ )
    {
        printf("%s INTEGER %s (%i)\n", Prefix, itr->first.c_str(), itr->second );
    }

    for( ShortMap::iterator itr = m_Shorts.begin(); itr != m_Shorts.end(); itr++ )
    {
        printf("%s SHORT %s (%i)\n", Prefix, itr->first.c_str(), itr->second );
    }

    for( FloatMap::iterator itr = m_Floats.begin(); itr != m_Floats.end(); itr++ )
    {
        printf("%s FLOAT %s (%f)\n", Prefix, itr->first.c_str(), itr->second );
    }

    for( LongMap::iterator itr = m_Longs.begin(); itr != m_Longs.end(); itr++ )
    {
        printf("%s LONG %s (%lli)\n", Prefix, itr->first.c_str(), itr->second );
    }

    for( DoubleMap::iterator itr = m_Doubles.begin(); itr != m_Doubles.end(); itr++ )
    {
        printf("%s Double %s (%f)\n", Prefix, itr->first.c_str(), itr->second );
    }

    for( ByteMap::iterator itr = m_Bytes.begin(); itr != m_Bytes.end(); itr++ )
    {
        printf("%s BYTE %s (%i)\n", Prefix, itr->first.c_str(), itr->second );
    }

    for( ByteArrayMap::iterator itr = m_ByteArrays.begin(); itr != m_ByteArrays.end(); itr++ )
    {
	printf("%s BYTE ARRAY %s (length: %li)\n", Prefix, itr->first.c_str(), sizeof(itr->second) );
    }

    delete Prefix;
}

void cNBTData::PrintData()
{
    printf("==== STRUCTURED NBT DATA ====\n");
    m_CurrentCompound->PrintData( 0, " " );
    printf("=============================\n");
}

void cNBTData::Serialize()
{
    std::string Buffer;
    m_CurrentCompound->Serialize( Buffer );

    if( m_Buffer )
        delete m_Buffer;
    m_Buffer = new char[Buffer.size()];
    memcpy( m_Buffer, Buffer.c_str(), Buffer.size() );
    m_BufferSize = Buffer.size();

    //printf("m_BufferSize1: %i\n", m_BufferSize);//re

     //for(unsigned int i = 0; i < m_BufferSize; i++)//re
     //{//re
     //    printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] );//re
     //}//re
}

void cNBTData::ParseData()
{
    if(!m_bDecompressed)
    {
        printf("WARNING: ParseData() called while data was not decompressed\n");
        return;
    }

     m_Index = 0;
     //printf("m_BufferSize2: %i\n", m_BufferSize);//re
     //printf("cNBTData::ParseData()\n");//re
     //for(unsigned int i = 0; i < m_BufferSize; i++)//re
     //for(unsigned int i = 0; i < 70; i++)//re
     //{//re
     //    printf("echo%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] );//re
     //}//re

    while( m_Index < m_BufferSize )
    {
	if (tm) {
		printf("m_BufferSize3: %i\n", m_BufferSize);
		printf("m_Index: %i\n", m_Index);
	}
        ParseTags();
    }
}

void cNBTData::ParseTags()
{
    if( m_Index < m_BufferSize )
    {
        //printf("ParseTags idx:%02i %02x %3i %c\n", m_Index, (unsigned char)m_Buffer[m_Index], (unsigned char)m_Buffer[m_Index], m_Buffer[m_Index] );//re
        ENUM_TAG Tag = (ENUM_TAG)m_Buffer[m_Index];
        if( Tag > 0 && m_ParseFunctions[ Tag ] )
        {
        //printf("m_BufferSize4: %i\n", m_BufferSize);
        //printf("m_Index1: %i\n\n\n\n", m_Index);

            m_Index++;
	    if (tm) {
	        printf("Tag: %i\n", Tag);
	    }
            (*this.*m_ParseFunctions[ Tag ])(true);
        }
        else if( Tag == TAG_End )
        {
	    if (tm) {
                printf("Tag End\n");
                int n;
		std::cin >> n;
	    }
            m_Index++;
        }
        else
        {
            printf("UNKNOWN TAG %x\n", m_Buffer[m_Index] );
            for(unsigned int i = (m_Index-10 > 0)?m_Index-10:0 ; i < m_Index+10 && i < m_BufferSize; i++)
            {
                printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] );
            }
            m_Index = m_BufferSize;
            return;
        }
    }
}

void cNBTData::ParseCompound( bool a_bNamed )
{
    std::string Name;
    if( a_bNamed ) Name = ReadName();
    //printf("OPEN COMPOUND: %s\n", Name.c_str() );//re

    PutCompound( Name );
    OpenCompound( Name );
    while( m_Index < m_BufferSize && m_Buffer[ m_Index ] != TAG_End )
    {
        ParseTags();
    }
    CloseCompound();
	m_Index++;
    //printf("CLOSE COMPOUND\n");//re
}

void cNBTData::ParseList( bool a_bNamed )
{
    std::string Name;
    if( a_bNamed ) Name = ReadName();
    ENUM_TAG TagType = (ENUM_TAG)ReadByte();
    int Length = ReadInt();
    //printf("LIST: %s Type: %02x Length: %i\n", Name.c_str(), TagType, Length );//re

     //for(unsigned int i = (m_Index-10 > 0)?m_Index-10:0 ; i < m_Index+10 && i < m_BufferSize; i++)//re
     //{//re
         //printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] );//re
     //}//re

    if (tm) {
        printf("List Name, tag, length: %s, %i, %i\n", Name.c_str(), (int)TagType, Length); 
    }

    PutList( Name, TagType );
    OpenList( Name );
    for(int i = 0; i < Length && m_Index < m_BufferSize; i++)
    {
        if( m_ParseFunctions[ TagType ] )
        {
            (*this.*m_ParseFunctions[ TagType ] )(false);
        }
    }
    if (tm) {
        printf("List Done Name, tag, length: %s, %i, %i\n", Name.c_str(), (int)TagType, Length);
    }

    CloseList();

}

void cNBTData::ParseByte( bool a_bNamed )
{
    std::string Name;
    if( a_bNamed ) Name = ReadName();
    char Value = ReadByte();

    PutByte( Name, Value );
    if (tm) {
      printf("BYTE: %s %i\n", Name.c_str(), Value );//re
    }
}

void cNBTData::ParseShort( bool a_bNamed )
{
    std::string Name;
    if( a_bNamed ) Name = ReadName();
    short Value = ReadShort();

    PutShort( Name, Value );
    if (tm) {
      printf("SHORT: %s %i\n", Name.c_str(), Value );//re
    }
}

void cNBTData::ParseInt( bool a_bNamed )
{
    std::string Name;
    if( a_bNamed ) Name = ReadName();
    int Value = ReadInt();

    PutInteger( Name, Value );
    if (tm) {
      printf("INT: %s %i\n", Name.c_str(), Value );//re
    }
}

void cNBTData::ParseLong( bool a_bNamed )
{
    if (tm) {
        for(unsigned int i = (m_Index-10 > 0)?m_Index-10:0 ; i < m_Index+30 && i < m_BufferSize; i++) {
            printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] );
        }
    }
    std::string Name;
    if( a_bNamed ) Name = ReadName();
    long long Value = ReadLong();

    //PutInteger( Name, (int)Value );
    PutLong( Name, Value );
    if (tm) {
      printf("LONG: %s %lli\n", Name.c_str(), Value );//re
    }
}

void cNBTData::ParseDouble( bool a_bNamed )
{
    if (tm) {
        for(unsigned int i = (m_Index-10 > 0)?m_Index-10:0 ; i < m_Index+30 && i < m_BufferSize; i++) {
            printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] );
        }
    }
    std::string Name;
    if( a_bNamed ) Name = ReadName();
    double Value = ReadDouble();

    //PutInteger( Name, (int)Value );
    PutDouble( Name, Value );
    if (tm) {
      printf("Double: %s %f\n", Name.c_str(), Value );//re
    }
}

void cNBTData::ParseFloat( bool a_bNamed )
{
    if (tm) {
        for(unsigned int i = (m_Index-10 > 0)?m_Index-10:0 ; i < m_Index+30 && i < m_BufferSize; i++) {
            printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] );
        }
    }
    std::string Name;
    if( a_bNamed ) Name = ReadName();
    float Value = ReadFloat();

    //PutInteger( Name, (int)Value );
    PutFloat( Name, Value );
    if (tm) {
      printf("Float: %s %f\n", Name.c_str(), Value );//re
    }
}

void cNBTData::ParseString( bool a_bNamed )
{
    std::string Name;
    if( a_bNamed ) Name = ReadName();
    std::string String = ReadName();

    PutString( Name, String );
    if (tm) {
      printf("STRING: %s (%s)\n", Name.c_str(), String.c_str() );//re
    }
}

void cNBTData::ParseByteArray( bool a_bNamed )
{

    std::string Name;
    if( a_bNamed ) Name = ReadName();

    int Length = ReadInt();
    std::string String;

    char* ByteArray = new char[ Length ];
    if( Length > 0 )
    {
	memcpy( ByteArray, &m_Buffer[ m_Index ], Length );
        m_Index += Length;
    }

    PutByteArray( Name, ByteArray );

    if (tm) {
        for(unsigned int i = (m_Index-10 > 0)?m_Index-10:0 ; i < m_Index+10 && i < m_BufferSize; i++) {
            printf("%02i %02x %3i %c\n", i, (unsigned char)m_Buffer[i], (unsigned char)m_Buffer[i], m_Buffer[i] );
        }
    }
}

std::string cNBTData::ReadName()
{
    //printf("crui1 \n");
    short Length = ReadShort();

    //printf("crui Length: %i\n", Length);
    std::string Name;
    if( Length > 0 )
    {
        for(int i = 0; i < Length; i++, m_Index++)
        {
            Name.push_back( m_Buffer[m_Index] );
        }
    }
    return Name;
}

char cNBTData::ReadByte()
{
    unsigned char Byte = m_Buffer[ m_Index ]; m_Index++;
    return Byte;
}

short cNBTData::ReadShort()
{
    short Length = 0;
    Length |= m_Buffer[ m_Index ] << 8; m_Index++;
    Length |= m_Buffer[ m_Index ]; m_Index++;
    return Length;
}

int cNBTData::ReadInt()
{
    int Value = 0;
    memcpy( &Value, m_Buffer+m_Index, sizeof(int) );
    m_Index+=sizeof(int);

    return ntohl( Value );
}

long long cNBTData::ReadLong()
{
	if (tm) {
		printf( "here1 : %i, m_Index: %i\n", (int)sizeof(long long), (int)m_Index );
	}
    long long Value = 0;
    memcpy( &Value, m_Buffer+m_Index, sizeof(long long) );
    m_Index+=sizeof(long long);
	if (tm) {
		printf( "here2 : %i, m_Index: %i\n", (int)sizeof(long long), (int)m_Index );
	}
    return Value;
}

double cNBTData::ReadDouble()
{
    double Value = 0;
    memcpy( &Value, m_Buffer+m_Index, sizeof(double) );
    m_Index+=sizeof(double);

    return Value;
}

float cNBTData::ReadFloat()
{
    float Value = 0;
    memcpy( &Value, m_Buffer+m_Index, sizeof(float) );
    m_Index+=sizeof(float);

	return Value;
}

void cNBTCompound::PutList( std::string Name, ENUM_TAG Type )
{
    m_Lists[Name] = new cNBTList( m_CurrentList, Type );
}

void cNBTCompound::PutCompound( std::string Name )
{
    if( m_CurrentList )
    {
        m_CurrentList->AddToList( new cNBTCompound( this ) );
    }
    else
    {
        m_Compounds[Name] = new cNBTCompound( this );
    }
}

void cNBTCompound::PutFloat( std::string Name, float Value )
{
	if( m_CurrentList )
		m_CurrentList->AddToList( (void*)((unsigned int*)&Value) );
	else
		m_Floats[Name] = Value;
}

cNBTCompound*    cNBTCompound::GetCompound( std::string Name )
{
    if( m_CurrentList )
    {
        if( m_CurrentList->GetType() != TAG_Compound )
            return 0;

        return (cNBTCompound*)m_CurrentList->GetLastElement();
    }
    return m_Compounds[Name];
}

void cNBTList::PrintData(int a_Depth, std::string a_Name)
{
    char* Prefix = new char[a_Depth*4];
    for(int i = 0; i < a_Depth*4; i++)
        Prefix[i] = ' ';
    Prefix[ a_Depth*4-1 ] = 0;

    if( a_Name.size() > 0 )
        printf("%s LIST (%s)\n", Prefix, a_Name.c_str() );
    else
        printf("%s LIST\n", Prefix );

    delete [] Prefix;

    for( VoidList::iterator itr = m_List.begin(); itr != m_List.end(); itr++)
    {
        switch( m_Type )
        {
        case cNBTCompound::TAG_Compound:
            {
                ((cNBTCompound*)*itr)->PrintData(a_Depth+1, "...");
            }
            break;
        default:
            break;
        }
    }
}

void cNBTList::Serialize(std::string & a_Buffer)
{
    for( VoidList::iterator itr = m_List.begin(); itr != m_List.end(); itr++ )
    {
        switch( m_Type )
        {
        case cNBTCompound::TAG_Compound:
            {
                ((cNBTCompound*)(*itr))->Serialize( a_Buffer );
                a_Buffer.push_back( cNBTCompound::TAG_End );
            }
            break;
        default:
            break;
        }
    }
}

void cNBTData::Clear()
{
    while( m_CurrentCompound != this ) CloseCompound();
    m_CurrentCompound->Clear();

    if( m_Buffer )
    {
        delete m_Buffer;
        m_Buffer = 0;
    }
    m_BufferSize = 0;
}

void cNBTCompound::Clear()
{
    for( CompoundMap::iterator itr = m_Compounds.begin(); itr != m_Compounds.end(); itr++ )
    {
        if( itr->second == 0 ) continue;
        itr->second->Clear();
        delete itr->second;
        itr->second = 0;
    }
    m_Compounds.clear();

    for( ListMap::iterator itr = m_Lists.begin(); itr != m_Lists.end(); itr++ )
    {
        if( itr->second == 0 ) continue;
        itr->second->Clear();
        delete itr->second;
        itr->second = 0;
    }
    m_Lists.clear();
    m_Bytes.clear();
    m_Shorts.clear();
    m_Integers.clear();
    m_Strings.clear();
}

void cNBTList::Clear()
{
    for( VoidList::iterator itr = m_List.begin(); itr != m_List.end(); itr++)
    {
        switch( m_Type )
        {
        case cNBTCompound::TAG_Compound:
            {
                cNBTCompound* Compound = (cNBTCompound*)(*itr);
                Compound->Clear();
                delete Compound;
                *itr = 0;
            }
            break;
        case cNBTCompound::TAG_List:
            {
                cNBTList* List = (cNBTList*)(*itr);
                List->Clear();
                delete List;
                *itr = 0;
            }
            break;
        default:
            break;
        }
    }
    m_List.clear();
}