int32_t SCRIPT_Load(char * filename) { int32_t s,h,l; char *b; h = SafeOpenRead(filename, filetype_binary); l = SafeFileLength(h)+1; b = (char *)Bmalloc(l); SafeRead(h,b,l-1); b[l-1] = '\n'; // JBF 20040111: evil nasty hack to trick my evil nasty parser SafeClose(h); s = SCRIPT_Init(filename); if (s<0) { Bfree(b); return -1; } SCRIPT_ParseBuffer(s,b,l); Bfree(b); return s; }
/* ============= ReadFiles ============= */ int ReadFiles (void) { FILE *f; int i; f = SafeOpenRead ("files.dat"); fscanf (f, "%i\n", &numsounds); for (i=0 ; i<numsounds ; i++) fscanf (f, "%i %s\n", &precache_sounds_block[i], precache_sounds[i]); fscanf (f, "%i\n", &nummodels); for (i=0 ; i<nummodels ; i++) fscanf (f, "%i %s\n", &precache_models_block[i], precache_models[i]); fscanf (f, "%i\n", &numfiles); for (i=0 ; i<numfiles ; i++) fscanf (f, "%i %s\n", &precache_files_block[i], precache_files[i]); fclose (f); printf ("%3i sounds\n", numsounds); printf ("%3i models\n", nummodels); printf ("%3i files\n", numfiles); }
/* * ==================== * W_OpenWad * ==================== */ void W_OpenWad(char *filename) { lumpinfo_t *lump_p; unsigned i; int length; /* open the file and add to directory */ wadhandle = SafeOpenRead(filename); SafeRead(wadhandle, &header, sizeof(header)); if (strncmp(header.identification, "WAD2", 4)) Error("Wad file %s doesn't have WAD2 id\n", filename); header.numlumps = LittleLong(header.numlumps); header.infotableofs = LittleLong(header.infotableofs); numlumps = header.numlumps; length = numlumps * sizeof(lumpinfo_t); lumpinfo = malloc(length); lump_p = lumpinfo; fseek(wadhandle, header.infotableofs, SEEK_SET); SafeRead(wadhandle, lumpinfo, length); /* Fill in lumpinfo */ for (i = 0; i < numlumps; i++, lump_p++) { lump_p->filepos = LittleLong(lump_p->filepos); lump_p->size = LittleLong(lump_p->size); } }
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== int ReadQuakeFile(quakefile_t *qf, void *buffer, int length) { FILE *fp; unzFile zf; if (qf->zipfile) { //open the zip file zf = unzOpen(qf->pakfile); //set the file position in the zip file (also sets the current file info) unzSetOffset(zf, qf->pos); //open the Quake file in the zip file unzOpenCurrentFile(zf); //read the Quake file from the zip file length = unzReadCurrentFile(zf, buffer, length); //close the Quake file in the zip file unzCloseCurrentFile(zf); //close the zip file unzClose(zf); return length; } //end if else { fp = SafeOpenRead(qf->filename); SafeRead(fp, buffer, length); fclose(fp); return length; } //end else } //end of the function ReadQuakeFile
/* =========== PackFile Copy a file into the pak file =========== */ void PackFile (char *src, char *name) { FILE *in; int remaining, count; char buf[4096]; if ( (byte *)pf - (byte *)pfiles > sizeof(pfiles) ) Error ("Too many files in pak file"); in = SafeOpenRead (src); remaining = filelength (in); pf->filepos = LittleLong (ftell (packhandle)); pf->filelen = LittleLong (remaining); strcpy (pf->name, name); printf ("%64s : %7i\n", pf->name, remaining); packbytes += remaining; while (remaining) { if (remaining < sizeof(buf)) count = remaining; else count = sizeof(buf); SafeRead (in, buf, count); SafeWrite (packhandle, buf, count); remaining -= count; } fclose (in); pf++; }
/* ================= TEX_InitFromWad ================= */ void TEX_InitFromWad (char *path) { int i, j = 0; char *word, buf[2048]; while ((word = (char *) strsep(&path, ";")) && j < NUM_WADS) { // sprintf(buf, "%s/%s", gamedir, word); strcpy(buf, word); printf("reading wadfile: '%s'\n", buf); texfile[j] = SafeOpenRead (buf); SafeRead (texfile[j], &wadinfo[j], sizeof(wadinfo_t)); if (strncmp (wadinfo[j].identification, "WAD2", 4)) Error ("TEX_InitFromWad: %s isn't a wadfile",buf); wadinfo[j].numlumps = LittleLong(wadinfo[j].numlumps); wadinfo[j].infotableofs = LittleLong(wadinfo[j].infotableofs); fseek (texfile[j], wadinfo[j].infotableofs, SEEK_SET); lumpinfo[j] = malloc(wadinfo[j].numlumps*sizeof(lumpinfo_t)); SafeRead (texfile[j], lumpinfo[j], wadinfo[j].numlumps*sizeof(lumpinfo_t)); for (i=0 ; i<wadinfo[j].numlumps ; i++) { CleanupName (lumpinfo[j][i].name, lumpinfo[j][i].name); lumpinfo[j][i].filepos = LittleLong(lumpinfo[j][i].filepos); lumpinfo[j][i].disksize = LittleLong(lumpinfo[j][i].disksize); } j++; } }
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== int ReadQuakeFile(quakefile_t *qf, void *buffer, int offset, int length) { FILE *fp; int read; unzFile zf; char tmpbuf[1024]; if(qf->zipfile) { //open the zip file zf = unzOpen(qf->pakfile); //set the file pointer qf->zipinfo.file = ((unz_s *) zf)->file; //open the Quake file in the zip file unzOpenCurrentFile(&qf->zipinfo); // while(offset > 0) { read = offset; if(read > sizeof(tmpbuf)) { read = sizeof(tmpbuf); } unzReadCurrentFile(&qf->zipinfo, tmpbuf, read); offset -= read; } //end while //read the Quake file from the zip file length = unzReadCurrentFile(&qf->zipinfo, buffer, length); //close the Quake file in the zip file unzCloseCurrentFile(&qf->zipinfo); //close the zip file unzClose(zf); return length; } //end if else { fp = SafeOpenRead(qf->filename); if(qf->offset) { fseek(fp, qf->offset, SEEK_SET); } if(offset) { fseek(fp, offset, SEEK_CUR); } SafeRead(fp, buffer, length); fclose(fp); return length; } //end else } //end of the function ReadQuakeFile
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== int LoadQuakeFile(quakefile_t *qf, void **bufferptr) { FILE *fp; void *buffer; int length; unzFile zf; if(qf->zipfile) { //open the zip file zf = unzOpen(qf->pakfile); //set the file pointer qf->zipinfo.file = ((unz_s *) zf)->file; //open the Quake file in the zip file unzOpenCurrentFile(&qf->zipinfo); //allocate memory for the buffer length = qf->length; buffer = GetMemory(length + 1); //read the Quake file from the zip file length = unzReadCurrentFile(&qf->zipinfo, buffer, length); //close the Quake file in the zip file unzCloseCurrentFile(&qf->zipinfo); //close the zip file unzClose(zf); *bufferptr = buffer; return length; } //end if else { fp = SafeOpenRead(qf->filename); if(qf->offset) { fseek(fp, qf->offset, SEEK_SET); } length = qf->length; if(!length) { length = Q_filelength(fp); } buffer = GetMemory(length + 1); ((char *)buffer)[length] = 0; SafeRead(fp, buffer, length); fclose(fp); *bufferptr = buffer; return length; } //end else } //end of the function LoadQuakeFile
void SB_LoadFile (swappedbuffer_t *sbuf, char *filename) { FILE *f; f = SafeOpenRead (filename); sbuf->maxsize = Q_filelength (f); sbuf->start = qmalloc (sbuf->maxsize + 1); sbuf->start[sbuf->maxsize] = 0; SafeRead (f, sbuf->start, sbuf->maxsize); fclose (f); sbuf->index = sbuf->start; sbuf->initialized = true; }
long LoadFile (char *filename, void **bufferptr) { int handle; long length; void *buffer; handle = SafeOpenRead (filename); length = filelength (handle); buffer = SafeMalloc (length); SafeRead (handle, buffer, length); close (handle); *bufferptr = buffer; return length; }
/* * ============== * LoadFile * ============== */ int LoadFile(const char* const filename, char** bufferptr) { FILE* f; int length; char* buffer; f = SafeOpenRead(filename); length = q_filelength(f); buffer = (char*)Alloc(length + 1); SafeRead(f, buffer, length); fclose(f); *bufferptr = buffer; return length; }
void RTS_AddFile (char *filename) { wadinfo_t header; lumpinfo_t *lump_p; uint32 i; int32 handle, length; int32 startlump; filelump_t *fileinfo; // // read the entire file in // FIXME: shared opens handle = SafeOpenRead( filename, filetype_binary ); startlump = numlumps; // WAD file printf(" Adding %s.\n",filename); SafeRead( handle, &header, sizeof( header ) ); if (strncmp(header.identification,"IWAD",4)) Error ("RTS file %s doesn't have IWAD id\n",filename); header.numlumps = IntelLong(header.numlumps); header.infotableofs = IntelLong(header.infotableofs); length = header.numlumps*sizeof(filelump_t); fileinfo = alloca (length); if (!fileinfo) Error ("RTS file could not allocate header info on stack"); lseek (handle, header.infotableofs, SEEK_SET); SafeRead (handle, fileinfo, length); numlumps += header.numlumps; // // Fill in lumpinfo // SafeRealloc(&lumpinfo,numlumps*sizeof(lumpinfo_t)); lump_p = &lumpinfo[startlump]; for (i=startlump ; i<numlumps ; i++,lump_p++, fileinfo++) { lump_p->handle = handle; lump_p->position = IntelLong(fileinfo->filepos); lump_p->size = IntelLong(fileinfo->size); strncpy (lump_p->name, fileinfo->name, 8); } }
/* ============== LoadFile ============== */ int LoadFile (const char *filename, void **bufferptr) { FILE *f; size_t length; void *buffer; f = SafeOpenRead (filename); length = (size_t) Q_filelength (f); buffer = malloc (length + 1); if (!buffer) COM_Error ("%s failed for %lu bytes.", __thisfunc__, (unsigned long)length); ((char *)buffer)[length] = 0; SafeRead(f, buffer, length); fclose (f); *bufferptr = buffer; return length; }
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== int LoadQuakeFile(quakefile_t *qf, void **bufferptr) { FILE *fp; void *buffer; int length; unzFile zf; if (qf->zipfile) { //open the zip file zf = unzOpen(qf->pakfile); //set the file position in the zip file (also sets the current file info) unzSetOffset(zf, qf->pos); //open the Quake file in the zip file unzOpenCurrentFile(zf); //allocate memory for the buffer length = qf->length; buffer = GetMemory(length+1); //read the Quake file from the zip file length = unzReadCurrentFile(zf, buffer, length); //close the Quake file in the zip file unzCloseCurrentFile(zf); //close the zip file unzClose(zf); *bufferptr = buffer; return length; } //end if else { fp = SafeOpenRead(qf->filename); length = qf->length; if (!length) length = Q_filelength(fp); buffer = GetMemory(length+1); ((char *)buffer)[length] = 0; SafeRead(fp, buffer, length); fclose(fp); *bufferptr = buffer; return length; } //end else } //end of the function LoadQuakeFile
/* ================= TEX_InitFromWad ================= */ void TEX_InitFromWad (char *path) { int i; texfile = SafeOpenRead (path); SafeRead (texfile, &wadinfo, sizeof(wadinfo)); if (strncmp (wadinfo.identification, "WAD2", 4)) Error ("TEX_InitFromWad: %s isn't a wadfile",path); wadinfo.numlumps = LittleLong(wadinfo.numlumps); wadinfo.infotableofs = LittleLong(wadinfo.infotableofs); fseek (texfile, wadinfo.infotableofs, SEEK_SET); lumpinfo = malloc(wadinfo.numlumps*sizeof(lumpinfo_t)); SafeRead (texfile, lumpinfo, wadinfo.numlumps*sizeof(lumpinfo_t)); for (i=0 ; i<wadinfo.numlumps ; i++) { CleanupName (lumpinfo[i].name, lumpinfo[i].name); lumpinfo[i].filepos = LittleLong(lumpinfo[i].filepos); lumpinfo[i].disksize = LittleLong(lumpinfo[i].disksize); } }
/* ============== LoadFile ============== */ int LoadFile (char *filename, void **bufferptr) { int length = 0; void *buffer; FileHandle_t f = SafeOpenRead (filename); if ( FILESYSTEM_INVALID_HANDLE != f ) { length = Q_filelength (f); buffer = malloc (length+1); ((char *)buffer)[length] = 0; SafeRead (f, buffer, length); g_pFileSystem->Close (f); *bufferptr = buffer; } else { *bufferptr = NULL; } return length; }
/* ==================== W_OpenWad ==================== */ void W_OpenWad (char *filename) { lumpinfo_t *lump_p; unsigned i; int length; // // open the file and add to directory // wadhandle = SafeOpenRead (filename); SafeRead (wadhandle, &header, sizeof(header)); if (!STRING_MATCHES_ID(header.identification,WAD_ID)) Error ("Wad file %s doesn't have %s identifier\n",filename, WAD_IDNAME); header.numlumps = LittleLong(header.numlumps); header.infotableofs = LittleLong(header.infotableofs); numlumps = header.numlumps; length = numlumps*sizeof(lumpinfo_t); lumpinfo = malloc (length); lump_p = lumpinfo; fseek (wadhandle, header.infotableofs, SEEK_SET); SafeRead (wadhandle, lumpinfo, length); // // Fill in lumpinfo // for (i=0 ; i<numlumps ; i++,lump_p++) { lump_p->filepos = LittleLong(lump_p->filepos); lump_p->size = LittleLong(lump_p->size); } }
int main (int argc, char **argv) { char filename[128]; int handle; wadinfo_t wad; lumpinfo_t *lumps, *lump_p; int i,j; char name[9]; uint32_t datasize, wadsize, infosize; char *lzss; printf ("SPITWAD "VERSION" by John Carmack, copyright (c) 1992 Id Software\n"); if (CheckParm ("?") || (argc != 2 && argc != 4)) { printf ("SPITWAD filename<.wad> [start end]\n"); exit (1); } strcpy (filename, argv[1]); DefaultExtension (filename, ".wad"); handle = SafeOpenRead (filename); wadsize = filelength(handle); datasize = 0; SafeRead (handle, &wad, sizeof(wad)); if (strncmp(wad.identification,"IWAD",4) && strncmp(wad.identification,"PWAD",4)) Error ("Wad file %s doesn't have IWAD or PWAD id\n",filename); wad.numlumps = LittleLong(wad.numlumps); wad.infotableofs = LittleLong(wad.infotableofs); infosize = wad.numlumps*sizeof(lumpinfo_t); lumps = lump_p = SafeMalloc(infosize); lseek (handle, wad.infotableofs, SEEK_SET); SafeRead (handle, lumps, infosize); infosize += sizeof(wadinfo_t); printf ("\n"); printf ("WAD file : %s\n",filename); printf ("numlumps : %u\n",wad.numlumps); printf ("infotableofs: %u\n",wad.infotableofs); printf ("\n"); printf ("lump name size filepos 0xsize 0xfpos\n"); printf ("---- -------- ------ -------- ------ ------\n"); name[8] = 0; for (i=0 ; i<wad.numlumps ; i++, lump_p++) { datasize += LittleLong (lump_p->size); strncpy (name,lump_p->name,8); for (j=7 ; j>=0 ; j--) if (!name[j]) name[j] = ' '; else break; if (name[0] & 0x80) { lzss = "(lzss)"; name[0] &= 0x7f; } else lzss = ""; printf ("%4i %s %6i %8i %6x %6x %s\n", i, name , LittleLong(lump_p->size), LittleLong(lump_p->filepos) , LittleLong(lump_p->size), LittleLong(lump_p->filepos), lzss ); } printf ("\n"); printf ("File size:%9i\n", wadsize); printf ("Data size:%9i\n", datasize); printf ("Info size:%9i\n", infosize); printf ("Wasted :%9i\n", wadsize-(datasize+infosize)); return 0; }