void QTVList_Process_Full_List(vfsfile_t *f, sb_qtvlist_parse_state_t *sb_qtvparse) { XML_Parser parser = NULL; int len; enum XML_Status status; char buf[4096]; vfserrno_t err; // initialize XML parser parser = XML_ParserCreate(NULL); if (parser == NULL) { Com_Printf("Couldn't initialize XML parser\n"); return; } XML_SetStartElementHandler(parser, QTVList_Parse_StartElement); XML_SetCharacterDataHandler(parser, QTVList_Parse_CharacterData); XML_SetEndElementHandler(parser, QTVList_Parse_EndElement); XML_SetUserData(parser, (void *) sb_qtvparse); while ((len = VFS_READ(f, buf, 4096, &err)) > 0) { if ((status = XML_Parse(parser, buf, len, 0)) != XML_STATUS_OK) { enum XML_Error err = XML_GetErrorCode(parser); Com_Printf("XML parser error.\n%s\n", status, XML_ErrorString(err)); break; } } }
static size_t vorbis_callback_read (void *ptr, size_t size, size_t nmemb, void *datasource) { vfsfile_t *f = (vfsfile_t *) datasource; return VFS_READ(f, ptr, size*nmemb, NULL); }
xml_t * XSD_Variable_LoadFromHandle(FILE *f, int filelen) { #else xml_t * XSD_Variable_LoadFromHandle(vfsfile_t *v, int filelen) { vfserrno_t err; #endif xml_variable_t *document; XML_Parser parser = NULL; int len; int pos = 0; char buf[XML_READ_BUFSIZE]; xml_parser_stack_t parser_stack; // create blank document document = (xml_variable_t *) Q_malloc(sizeof(xml_variable_t)); XSD_Variable_Init(document); // initialize XML parser parser = XML_ParserCreate(NULL); if (parser == NULL) goto error; XML_SetStartElementHandler(parser, OnStartElement); XML_SetEndElementHandler(parser, OnEndElement); XML_SetCharacterDataHandler(parser, OnCharacterData); // prepare user data XSD_InitStack(&parser_stack); parser_stack.document = (xml_t *) document; XML_SetUserData(parser, &parser_stack); #ifndef WITH_FTE_VFS while ((len = fread(buf, 1, min(XML_READ_BUFSIZE, filelen-pos), f)) > 0) #else while ((len = VFS_READ(v, buf, min(XML_READ_BUFSIZE, filelen-pos), &err)) > 0) #endif { if (XML_Parse(parser, buf, len, 0) != XML_STATUS_OK) goto error; pos += len; } if (XML_Parse(parser, NULL, 0, 1) != XML_STATUS_OK) goto error; XML_ParserFree(parser); return (xml_t *) document; error: if (parser) XML_ParserFree(parser); XSD_Variable_Free((xml_t *)document); return NULL; }
qboolean Rank_OpenRankings(void) { char syspath[MAX_OSPATH]; size_t result; qboolean created; if (!rankfile) { if (!*rank_filename.string) { return false; } if (!FS_NativePath(rank_filename.string, FS_GAMEONLY, syspath, sizeof(syspath))) return false; rankfile = FS_OpenVFS(rank_filename.string, "r+b", FS_GAMEONLY); if (!rankfile) //hmm... try creating { rankfile = FS_OpenVFS(rank_filename.string, "w+b", FS_GAMEONLY); created = true; } else created = false; if (!rankfile) return false; //couldn't open file. memset(&rankfileheader, 0, sizeof(rankfileheader)); VFS_SEEK(rankfile, 0); result = VFS_READ(rankfile, &rankfileheader, sizeof(rankfileheader_t)); if (result != sizeof(rankfileheader_t)) Con_Printf("Rank_OpenRankings() fread: expected %lu, result was %u (%s)\n",(long unsigned int)sizeof(rankfileheader_t),(unsigned int)result, rank_filename.string); rankfileheader.version = swaplong(rankfileheader.version); rankfileheader.usedslots = swaplong(rankfileheader.usedslots); rankfileheader.leader = swaplong(rankfileheader.leader); rankfileheader.freeslot = swaplong(rankfileheader.freeslot); if (!created && (rankfileheader.version != RANKFILE_VERSION || rankfileheader.ident != RANKFILE_IDENT)) { Con_Printf("Rank file is version %i not %i\nEither delete the file or use an equivelent version of " FULLENGINENAME "\n", rankfileheader.version, RANKFILE_VERSION); VFS_CLOSE(rankfile); rankfile = NULL; return false; } return true; //success. } return true; //already open }
static void FSGZIP_ReadFile(void *handle, flocation_t *loc, char *buffer) { gzipfile_t *gzip = handle; int err; VFS_SEEK(gzip->handle, gzip->file.filepos, SEEK_SET); err = VFS_READ(gzip->handle, buffer, gzip->file.filelen, NULL); if (err!=gzip->file.filelen) { Com_Printf ("Can't extract file \"%s:%s\" (corrupt)\n", gzip->filename, gzip->file.name); return; } return; }
static void READ_PLAYERHEADER(int x, rankheader_t *oh) { size_t result; VFS_SEEK(rankfile, sizeof(rankfileheader_t)+((x-1)*sizeof(rankinfo_t))); result = VFS_READ(rankfile, oh, sizeof(rankheader_t)); if (result != sizeof(rankheader_t)) Con_Printf("READ_PLAYERHEADER() fread: expected %lu, result was %u\n",(long unsigned int)sizeof(rankheader_t),(unsigned int)result); oh->prev = swaplong(oh->prev); //score is held for convineance. oh->next = swaplong(oh->next); // strcpy(oh->name, oh->name); oh->pwd = swaplong(oh->pwd); oh->score = swapfloat(oh->score); }
static void READ_PLAYERSTATS(int x, rankstats_t *os) { int i; size_t result; VFS_SEEK(rankfile, sizeof(rankfileheader_t)+sizeof(rankheader_t)+((x-1)*sizeof(rankinfo_t))); result = VFS_READ(rankfile, os, sizeof(rankstats_t)); if (result != sizeof(rankstats_t)) Con_Printf("READ_PLAYERSTATS() fread: expected %lu, result was %u\n",(long unsigned int)sizeof(rankstats_t),(unsigned int)result); os->kills = swaplong(os->kills); os->deaths = swaplong(os->deaths); for (i = 0; i < NUM_RANK_SPAWN_PARMS; i++) os->parm[i] = swapfloat(os->parm[i]); os->timeonserver = swapfloat(os->timeonserver); // os->flags1 = (os->flags1); // os->trustlevel = (os->trustlevel); // os->pad2 = (os->pad2); // os->pad3 = (os->pad3); }
//===================================== //PACK files (*.pak) - VFS functions //===================================== static int VFSPAK_ReadBytes (struct vfsfile_s *vfs, void *buffer, int bytestoread, vfserrno_t *err) { vfspack_t *vfsp = (vfspack_t*)vfs; int read; if (vfsp->currentpos - vfsp->startpos + bytestoread > vfsp->length) bytestoread = vfsp->length - (vfsp->currentpos - vfsp->startpos); if (bytestoread <= 0) return -1; if (vfsp->parentpak->filepos != vfsp->currentpos) { VFS_SEEK(vfsp->parentpak->handle, vfsp->currentpos, SEEK_SET); } read = VFS_READ(vfsp->parentpak->handle, buffer, bytestoread, err); vfsp->currentpos += read; vfsp->parentpak->filepos = vfsp->currentpos; // VFS-FIXME: Need to handle errors return read; }
static void Log_Stop(void) { int read; vfserrno_t err; char buf[1024]; int size; if (!Log_IsLogging()) return; size = (int) VFS_TELL(memlogfile); VFS_SEEK(memlogfile, 0, SEEK_SET); while (size > 0 && (read = VFS_READ(memlogfile, buf, 1024, &err))) { fwrite(buf, 1, min(read, size), logfile); size -= read; } VFS_CLOSE(memlogfile); fclose(logfile); logfile = NULL; memlogfile = NULL; }
// load document, auto-recognizing its type, returns NULL in case of failure xml_t * XSD_LoadDocument(char *filename) { xml_t *ret = NULL; int i; vfsfile_t *f; XML_Parser parser = NULL; int len; int filelen; char buf[XML_READ_BUFSIZE]; char document_type[1024]; // FIXME: D-Kure, does FS_ANY handle both the above cases if (!(f = FS_OpenVFS(filename, "rb", FS_ANY))) { return NULL; } filelen = VFS_GETLEN(f); // initialize XML parser parser = XML_ParserCreate(NULL); if (parser == NULL) { Com_Printf("could not open2\n"); goto error; } XML_SetStartElementHandler(parser, XSD_DetectType_OnStartElement); XML_SetUserData(parser, document_type); document_type[0] = 0; while (document_type[0] == 0 && (len = VFS_READ(f, buf, XML_READ_BUFSIZE, NULL)) > 0) { if (XML_Parse(parser, buf, len, 0) != XML_STATUS_OK) { Com_Printf("could not open3\n"); goto error; } } if (document_type[0] == 0) { Com_Printf("could not open4\n"); goto error; } // now we know what document type it is... // parser is no more needed XML_ParserFree(parser); parser = NULL; // fseek to the beginning of the file VFS_SEEK(f, 0, SEEK_SET); // execute loading parser i = 0; while (xsd_mappings[i].document_type != NULL) { if (!strcmp(xsd_mappings[i].document_type, document_type)) { //??? ret = xsd_mappings[i].load_function(f, filelen); break; } i++; } if (ret) { VFS_CLOSE(f); return ret; } error: if (f) VFS_CLOSE(f); if (parser) XML_ParserFree(parser); return NULL; }
/* ======== LoadPCX ======== */ byte* LoadPCX (vfsfile_t *f) { pcx_t *pcx, pcxbuf; byte palette[768]; byte *pix, *image_rgba; int x, y; int dataByte, runLength; int count; // // parse the PCX file // VFS_READ (f, &pcxbuf, sizeof(pcxbuf)); pcx = &pcxbuf; if (pcx->manufacturer != 0x0a || pcx->version != 5 || pcx->encoding != 1 || pcx->bits_per_pixel != 8 || pcx->xmax > 320 || pcx->ymax > 256) { Con_Printf ("Bad pcx file\n"); VFS_CLOSE(f); return NULL; } // seek to palette VFS_SEEK(f, VFS_GETLEN(f)-768); VFS_READ(f, palette, 768); VFS_SEEK(f, sizeof(pcxbuf) - 4); count = (pcx->xmax+1) * (pcx->ymax+1); image_rgba = malloc( count * 4); for (y=0 ; y<=pcx->ymax ; y++) { pix = image_rgba + 4*y*(pcx->xmax+1); for (x=0 ; x<=pcx->xmax ; ) { dataByte = VFS_GETC(f); if((dataByte & 0xC0) == 0xC0) { runLength = dataByte & 0x3F; dataByte = VFS_GETC(f); } else runLength = 1; while(runLength-- > 0) { pix[0] = palette[dataByte*3]; pix[1] = palette[dataByte*3+1]; pix[2] = palette[dataByte*3+2]; pix[3] = 255; pix += 4; x++; } } } VFS_CLOSE(f); image_width = pcx->xmax+1; image_height = pcx->ymax+1; return image_rgba; }
qbyte *LoadJPG(vfsfile_t *fin) { qbyte *mem=NULL, *in, *out; int i; /* This struct contains the JPEG decompression parameters and pointers to * working space (which is allocated as needed by the JPEG library). */ struct jpeg_decompress_struct cinfo; /* We use our private extension JPEG error handler. * Note that this struct must live as long as the main JPEG parameter * struct, to avoid dangling-pointer problems. */ struct my_error_mgr jerr; /* More stuff */ JSAMPARRAY buffer; /* Output row buffer */ int size_stride; /* physical row width in output buffer */ qbyte *infile; int length; length = VFS_GETLEN(fin); infile = malloc(length); VFS_READ(fin, infile, length); VFS_CLOSE(fin); /* Step 1: allocate and initialize JPEG decompression object */ /* We set up the normal JPEG error routines, then override error_exit. */ cinfo.err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = my_error_exit; /* Establish the setjmp return context for my_error_exit to use. */ if (setjmp(jerr.setjmp_buffer)) { // If we get here, the JPEG code has signaled an error. jpeg_destroy_decompress(&cinfo); if (mem) free(mem); return 0; } jpeg_create_decompress(&cinfo); jpeg_mem_src(&cinfo, infile, length); (void) jpeg_read_header(&cinfo, TRUE); (void) jpeg_start_decompress(&cinfo); if (cinfo.output_components!=3) Sys_Error("Bad number of componants in jpeg"); size_stride = cinfo.output_width * cinfo.output_components; /* Make a one-row-high sample array that will go away when done with image */ buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, size_stride, 1); out=mem=malloc(cinfo.output_height*cinfo.output_width*4); memset(out, 0, cinfo.output_height*cinfo.output_width*4); while (cinfo.output_scanline < cinfo.output_height) { (void) jpeg_read_scanlines(&cinfo, buffer, 1); in = buffer[0]; for (i = 0; i < cinfo.output_width; i++) {//rgb to rgba *out++ = *in++; *out++ = *in++; *out++ = *in++; *out++ = 255; } } (void) jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); image_width = cinfo.output_width; image_height = cinfo.output_height; return mem; }
/* ================= FSPAK_LoadPackFile Takes an explicit (not game tree related) path to a pak file. Loads the header and directory, adding the files at the beginning of the list so they override previous pack files. ================= */ static void *FSPAK_LoadPackFile (vfsfile_t *file, const char *desc) { dpackheader_t header; int i; packfile_t *newfiles; int numpackfiles; pack_t *pack; vfsfile_t *packhandle; dpackfile_t info; vfserrno_t err; packhandle = file; if (packhandle == NULL) return NULL; VFS_READ(packhandle, &header, sizeof(header), &err); if (header.id[0] != 'P' || header.id[1] != 'A' || header.id[2] != 'C' || header.id[3] != 'K') { return NULL; } header.dirofs = LittleLong (header.dirofs); header.dirlen = LittleLong (header.dirlen); numpackfiles = header.dirlen / sizeof(dpackfile_t); // if (numpackfiles != PAK0_COUNT) // com_modified = true; // not the original file newfiles = (packfile_t*)Q_malloc (numpackfiles * sizeof(packfile_t)); VFS_SEEK(packhandle, header.dirofs, SEEK_SET); // fread (&info, 1, header.dirlen, packhandle); // crc the directory to check for modifications // crc = QCRC_Block((qbyte *)info, header.dirlen); // QCRC_Init (&crc); pack = (pack_t *)Q_calloc(1, sizeof (pack_t)); // parse the directory for (i=0 ; i<numpackfiles ; i++) { *info.name = '\0'; // FIXME return value (void) VFS_READ(packhandle, &info, sizeof(info), &err); /* for (j=0 ; j<sizeof(info) ; j++) CRC_ProcessByte(&crc, ((qbyte *)&info)[j]); */ strlcpy (newfiles[i].name, info.name, MAX_QPATH); newfiles[i].filepos = LittleLong(info.filepos); newfiles[i].filelen = LittleLong(info.filelen); } /* if (crc != PAK0_CRC) com_modified = true; */ strlcpy (pack->filename, desc, sizeof (pack->filename)); pack->handle = packhandle; pack->numfiles = numpackfiles; pack->files = newfiles; pack->filepos = 0; VFS_SEEK(packhandle, pack->filepos, SEEK_SET); pack->references++; return pack; }