int ttvfs_stdio_fsize(VFILE *f, size_t *sizep) { long int sz = 0; if ( vfseek(f, 0, SEEK_END) != 0 || (sz = vftell(f)) < 0 || vfseek(f, 0, SEEK_SET) != 0) { return -1; } *sizep = sz; return 0; }
uint32 zz_vfs_pkg::read_ (char * buf, const uint32 size) { assert(size < MAX_FILESIZE); uint32 read_size = 0; if (fp_) { #define KEYSIZE 0x1000 long Mpointer = vftell(fp_) % KEYSIZE; read_size = vfread(buf, 1, size, fp_); if(fp_->btEncrypted==0x90) { size_t spt; spt = fp_->sFileName.find_last_of("/\\"); std::string hashstring1 = fp_->sFileName.substr(spt+1, (fp_->sFileName.length()-spt)); unsigned long key = StrToHashKey(hashstring1.c_str()); char crypttable[KEYSIZE]; DWORD *EAX=reinterpret_cast<DWORD*>(crypttable); DWORD EDI=0; DWORD ECX = key; DWORD EDX = key+1; ECX = ECX*4+1; for(int i = 0; i < 0x400; i++) { EDI=ECX*EDX; *((DWORD*)EAX) = EDI; EAX++; ECX+=4; EDX++; } for(size_t k=0;k<read_size;k++) { *((char*)buf+k)^= crypttable[Mpointer]; Mpointer++; Mpointer %= KEYSIZE; } } } else { ZZ_LOG("vfs_pkg: read_() failed. invalid fp_.\n"); return 0; } assert(read_size < MAX_FILESIZE); if (read_size >= MAX_FILESIZE) { ZZ_LOG("vfs_pkg: read_(%s) failed. read_size = %d.\n", filename_.get(), read_size); read_size = 0; } return read_size; }
BGBBTJ_API byte *BGBBTJ_JPG_Load(VFILE *fd, int *xs, int *ys) { byte *buf, *obuf; int fsz; vfseek(fd, 0, 2); fsz=vftell(fd); vfseek(fd, 0, 0); buf=malloc(fsz+256); vfread(buf, 1, fsz, fd); obuf=BGBBTJ_JPG_Decode(buf, fsz, xs, ys); free(buf); return(obuf); }
char *bgbcc_loadfile(char *name, int *rsz) { VFILE *fd; void *buf; int sz; fd=vffopen(name, "rb"); if(!fd)return(NULL); vfseek(fd, 0, 2); sz=vftell(fd); vfseek(fd, 0, 0); buf=bgbcc_malloc(sz+16); memset(buf, 0, sz+16); vfread(buf, 1, sz, fd); vfclose(fd); if(rsz)*rsz=sz; return(buf); #if 0 char *buf, *buf1; int i, sz; buf=basm_loadfile(name, &sz); if(buf) { buf1=bgbcc_malloc(sz); memcpy(buf1, buf, sz); basm_free(buf); if(rsz)*rsz=sz; return(buf1); } return(NULL); #endif }
BSP_Model *BSP_ReadModel(VFILE *fd) { char buf[64], buf2[256]; BSP_Model *tmp; BSP_LM *ltmp, *llst; float org[3]; int i, j, k, l; tmp=gctalloc("bsp_model_t", sizeof(BSP_Model)); // memset(tmp, 0, sizeof(BSP_Model)); // BSP_ReadFourcc(fd, FCC_Mdl); // BSP_ReadFourcc(fd, FCC_hEad); // BSP_ReadInt32(fd, 18); l=0; llst=NULL; while(1) { i=BSP_ReadFourcc(fd); if(i==FCC_enD_)break; if(i==FCC_hEad) { j=BSP_ReadInt32(fd); k=vftell(fd)+j; BSP_ReadVectorI(fd, org); //mins BSP_ReadVectorI(fd, org); //maxs BSP_ReadVectorI(fd, org); //org vfseek(fd, k, 0); continue; } if(i==FCC_tExi) { j=BSP_ReadInt32(fd); k=vftell(fd)+j; tmp->num_tex=j/64; tmp->texnames=gcalloc(tmp->num_tex*sizeof(char *)); tmp->tex_xs=gcatomic(tmp->num_tex*sizeof(int)); tmp->tex_ys=gcatomic(tmp->num_tex*sizeof(int)); tmp->tex_fl=gcatomic(tmp->num_tex*sizeof(int)); tmp->tex_num=gcatomic(tmp->num_tex*sizeof(int)); tmp->tex_num2=gcatomic(tmp->num_tex*sizeof(int)); for(j=0; j<tmp->num_tex; j++) { memset(buf, 0, 64); vfread(buf, 1, 56, fd); tmp->texnames[j]=kstrdup(buf); tmp->tex_xs[j]=BSP_ReadInt16(fd); tmp->tex_ys[j]=BSP_ReadInt16(fd); BSP_ReadInt16(fd); BSP_ReadInt16(fd); tmp->tex_num[j]=-1; tmp->tex_num2[j]=-1; sprintf(buf2, "textures/%s", buf); tmp->tex_num[j]=LBXGL_Texture_LoadImage(buf2); sprintf(buf2, "textures/%s_s", buf); tmp->tex_num2[j]=LBXGL_Texture_LoadImage(buf2); tmp->tex_fl[j]= LBXGL_Texture_GetImageFlags( tmp->tex_num[j]); } vfseek(fd, k, 0); continue; } if(i==FCC_lMi) { ltmp=BSP_ReadLM(fd); ltmp->num=l++; if(llst) { llst->next=ltmp; llst=ltmp; }else { tmp->lmroot=ltmp; llst=ltmp; } continue; } if(i==FCC_Node) { lbxgl_bsp_nodestackpos=0; tmp->root=BSP_ReadNode(fd, tmp); continue; } if(i==FCC_Leaf) { lbxgl_bsp_nodestackpos=0; tmp->root=BSP_ReadLeaf(fd, tmp); continue; } BSP_SkipTag(fd, i); } return(tmp); }
BSP_LM *BSP_ReadLM(VFILE *fd) { static byte *buf2=NULL; BSP_LM *tmp; byte *buf; // byte *buf, *buf2; int i, j, k, l; tmp=gcalloc(sizeof(BSP_LM)); memset(tmp, 0, sizeof(BSP_LM)); // BSP_ReadFourcc(fd); i=BSP_ReadInt32(fd)+vftell(fd); BSP_ReadNormal(fd, tmp->norm); tmp->xs=BSP_ReadInt16(fd); tmp->ys=BSP_ReadInt16(fd); tmp->fl=BSP_ReadInt16(fd); tmp->dn=BSP_ReadInt16(fd); tmp->dxs=tmp->xs>>2; tmp->dys=tmp->ys>>2; if(tmp->dxs<1)tmp->dxs=1; if(tmp->dys<1)tmp->dys=1; BSP_ReadVector2_14(fd, tmp->udir); BSP_ReadVector2_14(fd, tmp->vdir); tmp->min[0]=BSP_ReadFixed(fd, 65536); tmp->min[1]=BSP_ReadFixed(fd, 65536); tmp->max[0]=BSP_ReadFixed(fd, 65536); tmp->max[1]=BSP_ReadFixed(fd, 65536); tmp->num_vecs=BSP_ReadInt16(fd); tmp->vec_xyz=gcatomic(tmp->num_vecs*3*sizeof(float)); tmp->vec_uv=gcatomic(tmp->num_vecs*2*sizeof(float)); for(j=0; j<tmp->num_vecs; j++) { BSP_ReadVector(fd, tmp->vec_xyz+j*3); BSP_ReadVector2D(fd, tmp->vec_uv+j*2); } // vfseek(fd, i, 0); // return(tmp); // printf("Read LM %dx%d\n", tmp->xs, tmp->ys); if(tmp->fl&BSPLM_FL_MONO) { tmp->lmbuf=gcatomic((tmp->xs+1)*(tmp->ys+1)); BSP_ReadImgMono(fd, tmp->lmbuf, tmp->xs, tmp->ys); tmp->texnum=Tex_LoadTextureMono(tmp->xs, tmp->ys, tmp->lmbuf); for(j=0; j<tmp->dn; j++) { tmp->dlmstyle[j]=vfgetc(fd); tmp->dlmnum[j]==-1; tmp->dlmbuf[j]=gcatomic((tmp->dxs+1)*(tmp->dys+1)); BSP_ReadImgMono(fd, tmp->dlmbuf[j], tmp->dxs, tmp->dys); tmp->dlmnum[j]=Tex_LoadTextureMono(tmp->dxs, tmp->dys, tmp->dlmbuf[j]); } }else { tmp->lmbuf=gcatomic((tmp->xs+1)*(tmp->ys+1)*4); BSP_ReadImg(fd, tmp->lmbuf, tmp->xs, tmp->ys); tmp->texnum=Tex_LoadTexture(tmp->xs, tmp->ys, tmp->lmbuf, 1); for(j=0; j<tmp->dn; j++) { tmp->dlmstyle[j]=vfgetc(fd); tmp->dlmnum[j]==-1; tmp->dlmbuf[j]=gcatomic((tmp->dxs+1)*(tmp->dys+1)*4); BSP_ReadImg(fd, tmp->dlmbuf[j], tmp->dxs, tmp->dys); tmp->dlmnum[j]=Tex_LoadTexture(tmp->dxs, tmp->dys, tmp->dlmbuf[j], 1); } } #if 0 tmp->lmbuf=gcatomic(tmp->xs*tmp->ys*4); if(tmp->fl&1)BSP_ReadImgMono(fd, tmp->lmbuf, tmp->xs, tmp->ys); else BSP_ReadImg(fd, tmp->lmbuf, tmp->xs, tmp->ys); tmp->texnum=-1; buf=tmp->lmbuf; // buf2=gcatomic(tmp->xs*tmp->ys); if(!buf2)buf2=gcatomic(2048*2048); #if 0 l=0; for(j=0; j<(tmp->xs*tmp->ys); j++) { k=(buf[j*4+0]+buf[j*4+1]*2+buf[j*4+2])/4; // k=(buf[j*4+0]+buf[j*4+1]+buf[j*4+2])/3; buf2[j]=k; if(abs(buf[j*4+0]-k)>8)break; if(abs(buf[j*4+1]-k)>8)break; if(abs(buf[j*4+2]-k)>8)break; // if(abs(buf[j*4+0]-k)>8)l=1; // if(abs(buf[j*4+1]-k)>8)l=1; // if(abs(buf[j*4+2]-k)>8)l=1; } #endif if(l) // if(1) { tmp->texnum=Tex_LoadTexture(tmp->xs, tmp->ys, tmp->lmbuf, 1); }else { tmp->texnum=Tex_LoadTextureMono(tmp->xs, tmp->ys, buf2); } // gcfree(buf2); // tmp->texnum=Tex_LoadTexture(tmp->xs, tmp->ys, tmp->lmbuf, 1); // tmp->texnum=Tex_LoadTextureMono(tmp->xs, tmp->ys, tmp->lmbuf); for(j=0; j<tmp->dn; j++) { tmp->dlmstyle[j]=vfgetc(fd); tmp->dlmnum[j]==-1; tmp->dlmbuf[j]=gcatomic(tmp->dxs*tmp->dys*4); if(tmp->fl&1) BSP_ReadImgMono(fd, tmp->dlmbuf[j], tmp->dxs, tmp->dys); else BSP_ReadImg(fd, tmp->dlmbuf[j], tmp->dxs, tmp->dys); tmp->dlmnum[j]=Tex_LoadTexture(tmp->dxs, tmp->dys, tmp->dlmbuf[j], 1); } #endif vfseek(fd, i, 0); return(tmp); }
BSP_Node *BSP_ReadNode(VFILE *fd, BSP_Model *mdl) { BSP_Node *tmp, *ntmp; BSP_Face *ftmp, *ffst, *flst; BSP_Face *ptmp, *pfst, *plst; BSP_Face *ttmp, *tfst, *tlst; int i, j, k; tmp=gcalloc(sizeof(BSP_Node)); memset(tmp, 0, sizeof(BSP_Node)); // BSP_ReadFourcc(fd, FCC_Node); lbxgl_bsp_nodestack[lbxgl_bsp_nodestackpos++]=tmp; ffst=NULL; flst=NULL; pfst=NULL; plst=NULL; tfst=NULL; tlst=NULL; while(1) { i=BSP_ReadFourcc(fd); if(i==FCC_enD_)break; if(i==FCC_nOde) { j=BSP_ReadInt32(fd); k=vftell(fd)+j; BSP_ReadNormal(fd, tmp->norm); BSP_ReadVectorI(fd, tmp->mins); BSP_ReadVectorI(fd, tmp->maxs); tmp->flags=BSP_ReadInt16(fd); tmp->num=BSP_ReadInt16(fd); vfseek(fd, k, 0); continue; } if(i==FCC_fAce) { ftmp=BSP_ReadNodeFace(fd, NULL, mdl); if(ffst) { flst->next=ftmp; flst=ftmp; } else { ffst=ftmp; flst=ftmp; } continue; } if(i==FCC_pRt) { ptmp=BSP_ReadPortal(fd, tmp); if(pfst) { plst->next=ptmp; plst=ptmp; } else { pfst=ptmp; plst=ptmp; } continue; } if(i==FCC_tSrf) { ttmp=BSP_ReadTraceSurf(fd, tmp); if(tfst) { tlst->next=ttmp; tlst=ttmp; } else { tfst=ttmp; tlst=ttmp; } continue; } if(i==FCC_Node) { ntmp=BSP_ReadNode(fd, mdl); if(!tmp->lnode) { tmp->lnode=ntmp; }else if(!tmp->rnode) { tmp->rnode=ntmp; } continue; } if(i==FCC_Leaf) { ntmp=BSP_ReadLeaf(fd, mdl); if(!tmp->lnode) { tmp->lnode=ntmp; }else if(!tmp->rnode) { tmp->rnode=ntmp; } continue; } BSP_SkipTag(fd, i); } // tmp->face=ffst; tmp->prt=pfst; tmp->tsurf=tfst; V3F_SCALEADDSCALE(tmp->mins, 0.5, tmp->maxs, 0.5, tmp->org); tmp->radius=BSP_BoundNodeSphere(tmp, tmp->org); lbxgl_bsp_nodestackpos--; return(tmp); }
BSP_Node *BSP_ReadLeaf(VFILE *fd, BSP_Model *mdl) { BSP_Node *tmp; BSP_Face *ftmp, *ffst, *flst, *tfst, *tlst; int i, j, k; tmp=gcalloc(sizeof(BSP_Node)); memset(tmp, 0, sizeof(BSP_Node)); // BSP_ReadFourcc(fd, FCC_Leaf); ffst=NULL; flst=NULL; tfst=NULL; tlst=NULL; while(1) { i=BSP_ReadFourcc(fd); if(i==FCC_enD_)break; if(i==FCC_lEaf) { j=BSP_ReadInt32(fd); k=vftell(fd)+j; BSP_ReadVectorI(fd, tmp->mins); BSP_ReadVectorI(fd, tmp->maxs); tmp->flags=BSP_ReadInt16(fd); tmp->num=BSP_ReadInt16(fd); vfseek(fd, k, 0); continue; } if(i==FCC_vIs) { j=BSP_ReadInt32(fd); tmp->szvis=j; tmp->vis=gcatomic(j); vfread(tmp->vis, 1, j, fd); continue; } if(i==FCC_fAc1) { ftmp=BSP_ReadLeafFace(fd); if(tfst) { tlst->next=ftmp; tlst=ftmp; }else { tfst=ftmp; tlst=ftmp; } continue; } if(i==FCC_fAce) { ftmp=BSP_ReadNodeFace(fd, tmp, mdl); if(ffst) { flst->next=ftmp; flst=ftmp; }else { ffst=ftmp; flst=ftmp; } continue; } BSP_SkipTag(fd, i); } tmp->face=ffst; tmp->tsurf=tfst; V3F_SCALEADDSCALE(tmp->mins, 0.5, tmp->maxs, 0.5, tmp->org); tmp->radius=BSP_BoundNodeSphere(tmp, tmp->org); return(tmp); }
bool zz_vfs_pkg::open (const char * filename_in, const zz_vfs_mode mode) { if(mode==ZZ_VFS_WRITE) return false; //ZZ_LOG("vfs_pkg:open(%s)\n", filename); //ZZ_PROFILER_INSTALL(vfs_pkg_open); zz_assert(filename_in); zz_assert(filename_in[0]); zz_slash_converter filename(filename_in); if (fp_) { close(); } assert(!fp_); if (!pkg_system_) { ZZ_LOG("vfs_pkg: open(%s) failed. invalid pkg_system_.\n", filename.get()); return false; } //ZZ_LOG("vfs_pkg: open(%s)\n", filename); VHANDLE fsystem = pkg_system_->get_filesystem(); // switch (mode) { // case zz_vfs::ZZ_VFS_READ: fp_ = VOpenFile(filename, fsystem); zz_assertf( fp_, "vfs_pkg: open(%s) failed.", filename.get() ); filename_.set(filename); //check if it is encrypted fp_->btEncrypted=0; //if(fp_->btFileType==0){ size_t spt; spt = fp_->sFileName.find_last_of("/\\"); std::string hashstring1 = fp_->sFileName.substr(spt+1, (fp_->sFileName.length()-spt)); //OutputDebugString(hashstring1.c_str()); unsigned long key = StrToHashKey(hashstring1.c_str()); char crypttable[16]; DWORD *EAX=reinterpret_cast<DWORD*>(crypttable); DWORD EDI=0; DWORD ECX = key; DWORD EDX = key+1; ECX = ECX*4+1; for(int i = 0; i < 4; i++) { EDI=ECX*EDX; *((DWORD*)EAX) = EDI; EAX++; ECX+=4; EDX++; } vfseek(fp_, 0, VFSEEK_END); long off_set = vftell(fp_); if(off_set>16) { off_set-=16; vfseek(fp_,off_set, VFSEEK_SET); char buffer[16]; vfread(&buffer,1,16,fp_); if(buffer[0]==crypttable[0]){ if(buffer[1]==crypttable[1]){ if(buffer[2]==crypttable[2]){ if(buffer[3]==crypttable[3]){ if(buffer[4]==crypttable[4]){ if(buffer[5]==crypttable[5]){ if(buffer[6]==crypttable[6]){ if(buffer[7]==crypttable[7]){ if(buffer[8]==crypttable[8]){ if(buffer[9]==crypttable[9]){ if(buffer[10]==crypttable[10]){ if(buffer[11]==crypttable[11]){ if(buffer[12]==crypttable[12]){ if(buffer[13]==crypttable[13]){ if(buffer[14]==crypttable[14]){ if(buffer[15]==crypttable[15]){ fp_->btEncrypted=0x90; fp_->lEndOff-=16; //MessageBox(0,fp_->sFileName.c_str()," is encrypted",0); } } } } } } } } } } } } } } } } } vfseek(fp_,0, VFSEEK_SET); // break; // case zz_vfs::ZZ_VFS_WRITE: // not implemented yet!!! // break; // } if (!fp_) { return false; } return true; }
PDGL_Sample *PDGL_Sound_LoadMP3(char *name) { VFILE *fd; PDGL_Sample *tmp; struct mpstr mp; int i, j; byte *buf, *s, *ibuf, *obuf; int chan, rate, bits, bitrate, lty; int len, ret, size, bufsize, ofs; kprint("PDGL_Sound_LoadMP3: Loading sample '%s'\n", name); fd=vffopen(name, "rb"); if(!fd) { kprint("PDGL_Sound_LoadMP3: Failed open sample '%s'\n", name); return(NULL); } InitMP3(&mp); buf=malloc(10<<20); s=buf; ibuf=kalloc(16384); obuf=kalloc(16384); len=vfread(ibuf, 1, 16384, fd); ofs=statMP3(ibuf, &rate, &chan, &bitrate, <y); bits=16; kprint("PDGL_Sound_LoadMP3: %d Hz, %d chan, %d bps, layer %d, ofs %d\n", rate, chan, bitrate, lty, ofs); vfseek(fd, 0, 2); size=vftell(fd); len=(size+((bitrate/8)-1))/(bitrate/8); bufsize=len*rate*chan*4; //2x est stream len kprint("PDGL_Sound_LoadMP3: aprox %d seconds, buf %d bytes\n", len, bufsize); vfseek(fd, ofs, 0); buf=malloc(bufsize); s=buf; while(!vfeof(fd)) { len=vfread(ibuf, 1, 16384, fd); if(len<=0)break; ret = decodeMP3(&mp,ibuf,len,obuf,16384,&size); while(ret == MP3_OK) { memcpy(s, obuf, size); s+=size; ret = decodeMP3(&mp,NULL,0,obuf,16384,&size); } if((s-buf)>((bufsize*2)/3)) { kprint("PDGL_Sound_LoadMP3: overflow\n"); break; } } len=s-buf; kprint("PDGL_Sound_LoadMP3: decoded %f seconds\n", (float)len/(rate*chan*2)); tmp=PDGL_Sound_SampleFromBuf(name, buf, chan, rate, bits, len); kfree(ibuf); kfree(obuf); free(buf); return(tmp); }