unsigned char *kGUI::LoadFile(const char *filename,unsigned long *filesize) { unsigned char *fm; unsigned long fs; DataHandle dh; if(filesize) filesize[0]=0; dh.SetFilename(filename); if(dh.Open()==false) { printf("Error: cannot open file '%s'\n",filename); return(0); /* file not found or empty file */ } fs=dh.GetLoadableSize(); /* allocate space for file to load info */ fm=new unsigned char[fs+1]; if(!fm) { dh.Close(); return(0); } fm[fs]=0; /* allocate an extra byte and put a null at the end */ dh.Read(fm,fs); dh.Close(); if(filesize) { //todo: if filesize>32 bits, then assert, else return ok filesize[0]=fs; } return(fm); }
bool kGUIProt::LoadSeed(kGUIRandom *r,unsigned int offset) { unsigned int i; unsigned char keychar; long long keyfilesize; long long keystartindex=m_keystart+offset; DataHandle dh; r->InitSeed(); if(!m_keyfile) { if(!m_keyfilename.GetLen()) return(false); /* no filename set */ dh.SetFilename(m_keyfilename.GetString()); if(dh.Open()==false) return(false); keyfilesize=dh.GetSize(); } else keyfilesize=m_keyfilesize; /* if start index>filesize then bring it back inside */ while(keystartindex>=keyfilesize) keystartindex-=keyfilesize; if(!m_keyfile) dh.Seek(keystartindex); for(i=0;i<m_keylen;++i) { /* read in bytes and set random key */ if(!m_keyfile) { /* read 1 byte from the keyfile */ dh.Read(&keychar,(unsigned long)1); } else keychar=m_keyfile[keystartindex]; r->AddEntropy(keychar); if(++keystartindex==keyfilesize) { keystartindex=0; if(!m_keyfile) dh.Seek((unsigned long long)0); } } if(!m_keyfile) dh.Close(); return(true); }
OZF2GPXMap::OZF2GPXMap(const char *fn) { kGUIString ozfn; DataHandle dh; int linenum; kGUIString line; kGUIStringSplit ss; int index; int z; int nf; unsigned char *fp; unsigned long filesize; OZ2FHEADER_DEF *oh; double maxlat,maxlon; double maxx,maxy; m_coords=0; dh.SetFilename(fn); if(dh.Open()==false) return; /* couldn't load file */ linenum=0; do { dh.ReadLine(&line); nf=ss.Split(&line,","); if(linenum==2) { int sl; static char dirchar[]={ DIRCHAR }; ozfn.SetString(fn); /* extract path from filename */ sl=ozfn.GetLen()-1; /* cut off last subdir */ while(sl>0) { if(ozfn.GetChar(sl)==dirchar[0]) { ++sl; break; } --sl; }; ozfn.Clip(sl); ozfn.Append(line.GetString()); } else if(linenum==4) { /* datum */ if(!strcmp(ss.GetWord(0)->GetString(),"WGS 84")) { /* ok */ } else { dh.Close(); return; } //if (! FindOziDatumToWGS84 (strDatum.c_str (), m_pDatumTransformation)) { // m_pDatumTransformation = NULL; // ReportWarning ("Some unsupported datum is used: '%s'. No datum conversion performed.", strDatum.c_str ()); } else { if(!strcmp(ss.GetWord(0)->GetString(),"Map Projection")) { /* todo */ } else if(!strcmp(ss.GetWord(0)->GetString(),"MMPNUM")) { m_coords=new OZF2Corners[ss.GetWord(1)->GetInt()]; } else if(!strcmp(ss.GetWord(0)->GetString(),"MMPXY")) { /* pixel position */ index=atoi(ss.GetWord(1)->GetString())-1; m_coords[index].x=atoi(ss.GetWord(2)->GetString()); m_coords[index].y=atoi(ss.GetWord(3)->GetString()); } else if(!strcmp(ss.GetWord(0)->GetString(),"MMPLL")) { /* latitude/longitude position */ index=atoi(ss.GetWord(1)->GetString())-1; m_coords[index].lon=atof(ss.GetWord(2)->GetString()); m_coords[index].lat=atof(ss.GetWord(3)->GetString()); } } ++linenum; }while(dh.Eof()==false); dh.Close(); /* calculate bounding rectanle for lat/lon */ m_minlat=m_coords[0].lat; maxlat=m_coords[0].lat; m_minlon=m_coords[0].lon; maxlon=m_coords[0].lon; m_minx=m_coords[0].x; maxx=m_coords[0].x; m_miny=m_coords[0].y; maxy=m_coords[0].y; for(index=1;index<4;++index) { if(m_coords[index].lat<m_minlat) { m_minlat=m_coords[index].lat; m_miny=m_coords[index].y; } if(m_coords[index].lat>maxlat) { maxlat=m_coords[index].lat; maxy=m_coords[index].y; } if(m_coords[index].lon<m_minlon) { m_minlon=m_coords[index].lon; m_minx=m_coords[index].x; } if(m_coords[index].lon>maxlon) { maxlon=m_coords[index].lon; maxx=m_coords[index].x; } } /* assume linear for now until I figure out the projection */ m_slx=(maxlon-m_minlon)/(maxx-m_minx); m_sly=(maxlat-m_minlat)/(maxy-m_miny); m_lsx=(maxx-m_minx)/(maxlon-m_minlon); m_lsy=(maxy-m_miny)/(maxlat-m_minlat); for(index=0;index<4;++index) { GPXCoord c; int sx,sy; c.Set(m_coords[index].lat,m_coords[index].lon); ToMap(&c,&sx,&sy); DebugPrint("Coord testing %f,%f %d,%d - %d,%d",m_coords[index].lat,m_coords[index].lon,m_coords[index].x,m_coords[index].y,sx,sy); } /* load the map file with the tiles */ fp=kGUI::LoadFile(ozfn.GetString(),&filesize); if(!fp) return; /* couldn't load file */ m_filedata=fp; /* save pointer to data so destructor will free it up upon exiting */ oh=(OZ2FHEADER_DEF *)fp; if(oh->wSignature!=0x7778) return; /* not valid format */ assert ((oh->wPlanes==1) && (oh->wBitsPerPixel==8),"Unsupported bitmap format" ); SetTileSize(64,64); /* parse the tile data */ const int dwMasterTblOffset = * reinterpret_cast<const int *> (fp + filesize - 4); const int * const pdwMasterTbl = reinterpret_cast<const int *> (fp + dwMasterTblOffset); const int zoomlevels = (filesize - 4 - dwMasterTblOffset)/4; SetZoomLevels(1,zoomlevels); m_bitmaps.Alloc(zoomlevels); z=zoomlevels-1; for (int zoomlevel = 0; zoomlevel < zoomlevels; ++ zoomlevel) { OZ2FBITMAP_DEF *ih = (OZ2FBITMAP_DEF *) (fp + pdwMasterTbl [zoomlevel]); m_bitmaps.SetEntry(z,ih); SetSize(z,ih->dwImageWidth,ih->dwImageHeight); --z; } }
int main(int argc, char* argv[]) { int i; int nums; FDATA fdata; Hash AddHash; BigFile DestBigFile; BigFile SourceBigFile; BigFileEntry *bfe; int numok,numnew,numupdated; int sourcetype; bool update; char *p1=0; char *p2=0; char *p3=0; int tosspath=0; DataHandle addhandle; int vargc; char **vargv; vargc=argc; vargv=argv; /* handle encrypted file */ kGUIProt DestProt; bool usedestprot; kGUIProt SourceProt; bool usesourceprot; kGUISystemBig sysbig; kGUI::SetSystem(&sysbig); signal(SIGINT, sigint_handler); optcompress=false; optrecursive=true; optverify=false; optdelete=false; optmissing=false; usedestprot=false; usesourceprot=false; #if 0 p1="/source/kgui/_data.big"; p2="/source/kgui/big"; p3="/source/kgui/big/"; optverify=true; #endif for(i=1; i<vargc; ++i) { if(vargv[i][0]=='-') { switch(vargv[i][1]) { case 'c': optcompress=true; break; case 'd': optdelete=true; break; case 'm': optmissing=true; /* delete missing files from subdir */ break; case 'v': optverify=true; break; case 'r': optrecursive=false; break; case 'k': if(vargv[i][2]=='d') /* encryption key on destination file */ { printf("using dest key\n"); if(DestProt.SetKey(vargv[i+1],atoi(vargv[i+2]),atoi(vargv[i+3]),true)==false) { printf("Error loading dest keyfile '%s'\n",vargv[i+1]); return(0); } usedestprot=true; i+=3; } else if(vargv[i][2]=='s') /* encryption key on source file */ { printf("using source key\n"); if(SourceProt.SetKey(vargv[i+1],atoi(vargv[i+2]),atoi(vargv[i+3]),true)==false) { printf("Error loading source keyfile '%s'\n",vargv[i+1]); return(0); } usesourceprot=true; i+=3; } optrecursive=false; break; default: printf("Unknown parm '%s'\n",vargv[i]); return(0); break; } } else { if(!p1) p1=vargv[i]; else if(!p2) p2=vargv[i]; else if(!p3) { p3=vargv[i]; tosspath=(int)strlen(p3); } else { printf("Unknown parm '%s'\n",vargv[i]); return(0); } } } /* need at least 1 parm */ if(!p1) { printf("kguibig: (c) 2005 Kevin Pickelll\n"); printf("usage: kguibig bigfile.big path [root]\n"); printf(" -c = compress\n"); printf(" -v = verify\n"); printf(" -r = don't recurse\n"); printf(" -k[d,s] = filename offset len // source/dest key\n"); return(0); } DestBigFile.SetFilename(p1); if(usedestprot==true) DestBigFile.SetEncryption(&DestProt); DestBigFile.Load(true); if(DestBigFile.IsBad()==true) { printf("Dest file exists and is not a bigfile, or decryption key is incorrect!\n"); return(0); } /* list, verify or compress ? */ if(!p2) { if(optcompress) { } else { unsigned long crc; unsigned long vfsize; BigFileEntry *sfe; unsigned char copybuf[65536]; DataHandle checkhandle; /* verify or list */ nums=DestBigFile.GetNumEntries(); for(i=0; ((i<nums) && (g_userabort==false)); ++i) { sfe=(BigFileEntry *)DestBigFile.GetEntry(i); if(optverify) { /* check crc and print if no match */ printf("%d%c",i,13); vfsize=sfe->m_size; crc=0; DestBigFile.CopyArea(&checkhandle,sfe->m_offset,sfe->m_size,sfe->m_time); checkhandle.Open(); while(vfsize>sizeof(copybuf)) { checkhandle.Read(©buf,(unsigned long)sizeof(copybuf)); crc=DestBigFile.CrcBuffer(crc,copybuf,sizeof(copybuf)); vfsize-=sizeof(copybuf); }; /* write remainder */ if(vfsize>0) { checkhandle.Read(©buf,vfsize); crc=DestBigFile.CrcBuffer(crc,copybuf,vfsize); } checkhandle.Close(); if(crc!=sfe->m_crc) printf("CRC Error on file '%s' %06x<>%06x\n",sfe->GetName()->GetString(),(int)crc,sfe->m_crc); } else /* assume list if verify is not set */ printf("%s, len=%d,crc=%06x\n",sfe->GetName()->GetString(),sfe->m_size,sfe->m_crc); } } return(0); } AddHash.Init(16,sizeof(FDATA)); /* is p2 a bigfile? */ if(strstr(p2,".big")) { SourceBigFile.SetFilename(p2); if(usesourceprot==true) SourceBigFile.SetEncryption(&SourceProt); SourceBigFile.Load(true); if(SourceBigFile.IsBad()==false) sourcetype=SOURCE_BIG; else { printf("Source file exists and is not a bigfile, or decryption key is incorrect!\n"); return(0); } } else if(kGUI::IsDir(p2)==false) { fdata.time=kGUI::SysFileTime(p2); //fdata.root=p3; AddHash.Add(p2,&fdata); sourcetype=SOURCE_FILE; } else { unsigned int df; const char *name; kGUIDir dir; printf("loading directory!\n"); dir.LoadDir(p2,true,true); for(df=0; df<dir.GetNumFiles(); ++df) { name=dir.GetFilename(df); fdata.time=kGUI::SysFileTime(name); AddHash.Add(name,&fdata); } // scandir(&AddHash,p2); sourcetype=SOURCE_DIR; } /* now, look for differences between bigfile and files in the addhash list */ numok=0; numnew=0; numupdated=0; /* todo: optdelete function */ /* add from source bigfile to dest bigfile will not work */ /* if source is encrypted so I need to rewrite addfile to use a datahandle */ /* instead of a filestream */ if(sourcetype==SOURCE_BIG) { BigFileEntry *sfe; nums=SourceBigFile.GetNumEntries(); for(i=0; ((i<nums) && (g_userabort==false)); ++i) { sfe=(BigFileEntry *)SourceBigFile.GetEntry(i); bfe=DestBigFile.Locate(sfe->GetName()->GetString()); if(!bfe) { // printf("File '%s' not in destination set!\n",she->m_string); update=true; ++numnew; } else { if(sfe->m_time==bfe->m_time) { update=false; ++numok; } else { int deltatime; deltatime=abs(sfe->m_time-bfe->m_time); if(deltatime==46400 || deltatime==3600) { update=false; ++numok; } else { printf("File '%s' %d,%d times are different!(%d)\n",sfe->GetName()->GetString(),sfe->m_time,bfe->m_time,deltatime); ++numupdated; update=true; } } } if(update==true) { SourceBigFile.CopyArea(&addhandle,sfe->m_offset,sfe->m_size,sfe->m_time); // addsize=sfe->m_size; // addtime=sfe->m_time; // fseek(addhandle,sfe->m_offset,SEEK_SET); /* add the file to the bigfile */ DestBigFile.AddFile(sfe->GetName()->GetString(),&addhandle,false); } } DestBigFile.UpdateDir(); } else { HashEntry *she; FDATA *sfdata; nums=AddHash.GetNum(); she=AddHash.GetFirst(); for(i=0; ((i<nums) && (g_userabort==false)); ++i) { sfdata=(FDATA *)she->m_data; bfe=DestBigFile.Locate(she->m_string+tosspath); if(!bfe) { // printf("File '%s' not in destination set!\n",she->m_string+tosspath); ++numnew; update=true; } else { if(sfdata->time==bfe->m_time) { update=false; ++numok; } else { int deltatime; deltatime=(abs((int)sfdata->time-bfe->m_time)); if(deltatime==46400 || deltatime==3600) { update=false; ++numok; } else { printf("File '%s' %d,%d times are different!(%d)\n",she->m_string+tosspath,(int)sfdata->time,bfe->m_time,deltatime); ++numupdated; update=true; } } } if(update==true) { addhandle.SetFilename(she->m_string); /* add the file to the bigfile */ DestBigFile.AddFile(she->m_string+tosspath,&addhandle,false); } she=she->m_next; } DestBigFile.UpdateDir(); } printf("numok=%d,numnew=%d,numupdated=%d\n",numok,numnew,numupdated); return 0; }