t_CompChunk=newchar[5*1024*1024];//5MB Temp Compressed Chunk Data array
intt_CompChunkSz=-1;//Size of data in array
charPakVersion=1;
charChunkVersion=1;
shortNumChunks=0;
unsignedcharbyte1=0;
unsignedcharbyte2=0;
unsignedcharbyte3=0;
unsignedcharbyte4=0;
unsignedcharbyte5=0;
unsignedchartrash=0;
unsignedintfrloc=0;
inttoffset=0;
intcompdlength=0;
inttoffarr[1024];
//loop through notch's header
for(shorti=0;i<1024;++i){//loop through first 4096 bytes of data, 4 bytes at a time
//Region files begin with an 8kiB header containing information about which chunks are present in the region file, when they were last updated, and where they can be found. The location in the region file of a chunk at (x, z) (in chunk coordinates) can be found at byte offset 4 * ((x mod 32) + (z mod 32) * 32) in its region file. Its timestamp can be found 4096 bytes later in the file. The remainder of the file consists of data for up to 1024 chunks, interspersed with an arbitrary amount of unused space.
//we are only using the first 4096 bytes. We don't need the timestamps right now.
if(fread(&byte1,sizeof(byte1),1,f)!=1){std::cout<<"ERROR 21hs READING FROM FILE "<<SourceFile;fclose(f);returnfalse;}
if(fread(&byte2,sizeof(byte2),1,f)!=1){std::cout<<"ERROR ks93 READING FROM FILE "<<SourceFile;fclose(f);returnfalse;}
if(fread(&byte3,sizeof(byte3),1,f)!=1){std::cout<<"ERROR 2s5f READING FROM FILE "<<SourceFile;fclose(f);returnfalse;}//first three bytes area big-endian representation of the chunk offsets in no particular order.
if(fread(&byte4,sizeof(byte4),1,f)!=1){std::cout<<"ERROR dhj3 READING FROM FILE "<<SourceFile;fclose(f);returnfalse;}//we don't need to use this byte right now.
toffset=4096*((byte1*256*256)+(byte2*256)+byte3);//find the chunk offsets using the first three bytes of each long;
toffarr[i]=toffset;//array of chunk offset locatiosn in the fle.
}
for(shorti=0;i<4096;i++){//loop through next 4096 bytes of the header.
//keeping this code here in case we need it later. not using it right now.
if(fread(&trash,sizeof(byte4),1,f)!=1){std::cout<<"ERROR 2jkd READING FROM FILE "<<SourceFile;fclose(f);returnfalse;}
}
frloc=8192;//current location of fread is at 4096+ 4096 since we read through and collected important info from the header.
cQuicksortQuick;
Quick.quicksort(toffarr,0,1023);//sort the array from smallest to larget offset locations so we only have to read through the file once.
for(shortia=0;ia<1024;ia++){//a region file can hold a maximum of 1024 chunks (32*32)
if(ia<3500){//only run chunk # 3
if(toffarr[ia]<8192){//offsets of less than 8192 are impossible. 0 means there is no chunk in a particular location.
if(toffarr[ia]>0){std::cout<<"ERROR 2s31 IN COLLECTED CHUNK OFFSETS "<<toffarr[ia];fclose(f);returnfalse;}//values between 0 and 8192 should be impossible.
//This file does not contain the max 1024 chunks, skip until we get to the first
}else{// found a chunk offset value
//Chunk data begins with a (big-endian) four-byte length field which indicates the exact length of the remaining chunk data in bytes. The following byte indicates the compression scheme used for chunk data, and the remaining (length-1) bytes are the compressed chunk data.
//printf("Working on chunk %i :: %i\n", ia, toffarr[ia]);
if(fread(&byte1,sizeof(byte1),1,f)!=1){std::cout<<"ERROR 2t32 READING FROM FILE "<<SourceFile;fclose(f);returnfalse;}
if(fread(&byte2,sizeof(byte2),1,f)!=1){std::cout<<"ERROR 2y51 READING FROM FILE "<<SourceFile;fclose(f);returnfalse;}
if(fread(&byte3,sizeof(byte3),1,f)!=1){std::cout<<"ERROR 3424 READING FROM FILE "<<SourceFile;fclose(f);returnfalse;}
if(fread(&byte4,sizeof(byte4),1,f)!=1){std::cout<<"ERROR sd22 READING FROM FILE "<<SourceFile;fclose(f);returnfalse;}
compdlength=((byte1*256*256*256)+(byte2*256*256)+(byte3*256)+byte4-0);//length of compressed chunk data
if(fread(&byte5,sizeof(byte5),1,f)!=1){std::cout<<"ERROR 2341 READING FROM FILE "<<SourceFile;fclose(f);returnfalse;}//compression type, 1 = GZip (RFC1952) (unused in practice) , 2 = Zlib (RFC1950)
frloc+=5;//moved ahead 5 bytes while reading data.
char*compBlockData=newchar[compdlength];
if(fread(compBlockData,compdlength,1,f)!=1){std::cout<<"ERROR rf22 READING FROM FILE "<<SourceFile;fclose(f);returnfalse;}
frloc=frloc+compdlength;
uLongfDestSize=128576;// uncompressed chunks should never be larger than this
char*BlockData=newchar[DestSize];
interrorcode=uncompress((Bytef*)BlockData,&DestSize,(Bytef*)compBlockData,compdlength);//DestSize will update to the actual uncompressed data size after this opperation.