//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 "<<SourceFileName;fclose(f);returnfalse;}
if(fread(&byte2,sizeof(byte2),1,f)!=1){std::cout<<"ERROR ks93 READING FROM FILE "<<SourceFileName;fclose(f);returnfalse;}
if(fread(&byte3,sizeof(byte3),1,f)!=1){std::cout<<"ERROR 2s5f READING FROM FILE "<<SourceFileName;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 "<<SourceFileName;fclose(f);returnfalse;}//we don't need to use this byte right now.
Quick.quicksort(toffarr,0,1023);//sort the array from smallest to largest 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 "<<SourceFileName;fclose(f);returnfalse;}
if(fread(&byte2,sizeof(byte2),1,f)!=1){std::cout<<"ERROR 2y51 READING FROM FILE "<<SourceFileName;fclose(f);returnfalse;}
if(fread(&byte3,sizeof(byte3),1,f)!=1){std::cout<<"ERROR 3424 READING FROM FILE "<<SourceFileName;fclose(f);returnfalse;}
if(fread(&byte4,sizeof(byte4),1,f)!=1){std::cout<<"ERROR sd22 READING FROM FILE "<<SourceFileName;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 "<<SourceFileName;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 "<<SourceFileName;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.