// // create a new AIVar (with a unique label) // // returns NULL if label isn't unique // AIVar *aivarCreate(char *label) { AIVar *var; if (aivarFind(label)) return NULL; // allocate the var var = memAlloc(sizeof(AIVar), "aivar", 0); aivarValueSet(var, 0); // default, just to be nice aivarLabelSet(var, label); // keep track of all allocations if (!vars) { vars = memAlloc(sizeof(AIVar*)*AIVAR_ALLOC_INITIAL, "aivarlist", 0); varsAllocated = AIVAR_ALLOC_INITIAL; varsUsed = 0; } if (varsUsed >= varsAllocated) { // allocate more if necessary vars = memRealloc(vars, sizeof(AIVar*)*(varsAllocated + AIVAR_ALLOC_INCREMENT), "aivarlist", 0); varsAllocated += AIVAR_ALLOC_INCREMENT; } vars[varsUsed++] = var; return var; }
/* returns LONG_MAX iff out of memory */ PQhandle pqInsert( PriorityQ *pq, PQkey keyNew ) { long curr; if( pq->initialized ) { return __gl_pqHeapInsert( pq->heap, keyNew ); } curr = pq->size; if( ++ pq->size >= pq->max ) { PQkey *saveKey= pq->keys; /* If the heap overflows, double its size. */ pq->max <<= 1; pq->keys = (PQHeapKey *)memRealloc( pq->keys, (size_t) (pq->max * sizeof( pq->keys[0] ))); if (pq->keys == NULL) { pq->keys = saveKey; /* restore ptr to free upon return */ return LONG_MAX; } } assert(curr != LONG_MAX); pq->keys[curr] = keyNew; /* Negative handles index the sorted array. */ return -(curr+1); }
/*----------------------------------------------------------------------------- Name : shGrowBuffers Description : possibly grow the normal & colour lists Inputs : n - number of vertices Outputs : Return : ----------------------------------------------------------------------------*/ void shGrowBuffers(sdword nVertices) { if (normalList == NULL) { nVerts = nVertices; normalList = (hvector*)memAlloc(nVerts * sizeof(hvector), "sh normal list", NonVolatile); colorList = (shColor*)memAlloc(nVerts * sizeof(shColor), "sh color list", NonVolatile); } if (nVerts < nVertices) { nVerts = nVertices; normalList = (hvector*)memRealloc(normalList, nVerts * sizeof(hvector), "sh normal list", NonVolatile); colorList = (shColor*)memRealloc(colorList, nVerts * sizeof(shColor), "sh color list", NonVolatile); } }
/*! * \brief Encode a path string to be OAuth complient. * * This is just a slight modification of oauth_url_escape from liboauth library: * -# '/' is not escaped anymore * -# memory allocation is checked * -# code aspect was modified to match the library coding convention * * \param path path to encode * \return encoded path (must be freed by caller) */ char* drbEncodePath(const char *path) { size_t alloc, newLength, length, i = 0; newLength = alloc = ( length = (path ? strlen(path) : 0) ) + 1; char *encPath = (char*) malloc(alloc); // loop while string end isnt reached and while ns is a valid pointer while(length-- && encPath) { unsigned char in = *path++; switch(in){ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z': case '_': case '~': case '.': case '-': case '/': encPath[i++] = in; break; default: newLength += 2; /* this'll become a %XX */ if(newLength > alloc) { alloc *= 2; if((encPath = memRealloc(encPath, alloc)) == NULL) continue; } snprintf(&encPath[i], 4, "%%%02X", in); i += 3; break; } } // Shrink and finilaze the new string if ns is a valid pointer if (encPath && (encPath = memRealloc(encPath, newLength)) != NULL) { encPath[i] = '\0'; } return encPath; }
/*----------------------------------------------------------------------------- Name : transGrowVertexLists Description : grows the vertex lists to accomodate an increasing number of vertices. performance is expected to be very poor for a frame, then stable afterwards. TODO: transGrowAndShrinkVertexLists, a more memory- conscious version of this pathetic fn Inputs : nVertices - number of vertices Outputs : preVertexList, xVertexList are re/allocated Return : ----------------------------------------------------------------------------*/ void transGrowVertexLists(sdword nVertices) { if (eyeVertexList == NULL) { nVerts = nVertices; eyeVertexList = (hvector*)memAlloc(nVerts * sizeof(hvector), "eye vertex list", NonVolatile); clipVertexList = (hvector*)memAlloc(nVerts * sizeof(hvector), "clip vertex list", NonVolatile); } if (nVerts < nVertices) { nVerts = nVertices; eyeVertexList = (hvector*)memRealloc( eyeVertexList, nVerts * sizeof(hvector), "eye vertex list", NonVolatile); clipVertexList = (hvector*)memRealloc( clipVertexList, nVerts * sizeof(hvector), "clip vertex list", NonVolatile); } }
/*----------------------------------------------------------------------------- Name : spMissionDescriptionSet Description : Script-parse callback for setting description text Inputs : Outputs : Return : ----------------------------------------------------------------------------*/ static void spMissionDescriptionSet(char *directory,char *field,void *dataToFillIn) { if (spDescription == NULL) { spDescription = memStringDupe(field); } else { spDescription = memRealloc(spDescription, strlen(spDescription) + strlen(field) + 3, "DescriptionString", 0); strcat(spDescription, "\\n"); //auto-newline strcat(spDescription, field); } }
void VMMessage::addParam( const SafeItem &itm ) { if ( m_params == 0 ) { m_params = (SafeItem *) memAlloc( sizeof( SafeItem ) * PARAMS_GROWTH ); m_allocated = PARAMS_GROWTH; } else if( m_pcount == m_allocated ) { m_allocated += PARAMS_GROWTH; m_params = (SafeItem *) memRealloc( m_params, sizeof( SafeItem ) * m_allocated ); } m_params[ m_pcount++ ].copy( itm ); }
/* ############################################################################# * * Description create a string from a searchpattern and path information * Author Harry Brueckner * Date 2005-04-12 * Arguments SEARCHPATTERN pattern - pattern to create * char** path - path information to full into the * pattern * char* string - result string which must be freed * by the caller * Return 1 on error, 0 if ok */ int getPatternString(SEARCHPATTERN* pattern, char** path, char** string) { SEARCHPATTERN* cpattern = pattern; int maxlevel = listCount(path); char* concat; TRACE(99, "getPatternString()", NULL); *string = NULL; while (cpattern) { switch (cpattern -> type) { case PATTERN_TEMPLATE: if (cpattern -> templateid > maxlevel) { return 1; } concat = path[cpattern -> templateid - 1]; break; case PATTERN_STRING: concat = cpattern -> string; break; default: concat = NULL; } if (concat) { /* we have a string to concat to the result */ if (!*string) { /* new string */ *string = memAlloc(__FILE__, __LINE__, strlen(concat) + 1); *string[0] = 0; } else { /* resize the string */ *string = memRealloc(__FILE__, __LINE__, *string, strlen(*string) + 1, strlen(*string) + strlen(concat) + 1); } strStrncat(*string, concat, strlen(concat) + 1); } cpattern = cpattern -> next; } return 0; }
static int make_unique_format_titles(DATA_BIN_PTR dbin) { PROCESS_INFO_LIST plist = NULL; PROCESS_INFO_PTR pinfo = NULL; int error = 0; int SCRATCH = strlen("Binary Output Separate Varied Record Header: ") + 1; /* Longest */ char *cp = NULL; FF_VALIDATE(dbin); db_ask(dbin, DBASK_PROCESS_INFO, 0, &plist); plist = dll_first(plist); pinfo = FF_PI(plist); while (pinfo && !error) { FF_VALIDATE(pinfo); cp = (char *)memRealloc(PINFO_FORMAT(pinfo)->name, strlen(PINFO_FORMAT(pinfo)->name) + SCRATCH + 1, "PINFO_FORMAT(pinfo)->name"); if (cp) { PINFO_FORMAT(pinfo)->name = cp; memmove(cp + SCRATCH, cp, strlen(cp) + 1); } else { error = err_push(ERR_MEM_LACK, ""); break; } error = get_format_type(PINFO_FORMAT(pinfo), cp); if (error) break; memmove(cp + strlen(cp), cp + SCRATCH, strlen(cp + SCRATCH) + 1); plist = dll_next(plist); pinfo = FF_PI(plist); } ff_destroy_process_info_list(plist); return error; }
void* memXRealloc (void *object, int numBytes) { if (numBytes == 0) return 0; else { object = memRealloc(object, numBytes); if (object == 0) { fprintf(stderr, "memory exhausted\n"); exit(1); } return object; } }
void AddFileOfMap(char *filedirname,char *filename,sdword fileSize) { sdword index = autodownloadmapInfo.numFilesOfMap++; if (autodownloadmapInfo.numFilesOfMap > autodownloadmapInfo.numFilesOfMapAllocated) { if (autodownloadmapInfo.fileOfMapInfo == NULL) { autodownloadmapInfo.fileOfMapInfo = memAlloc(sizeof(FileOfMapInfo)*NUM_FILES_OF_MAP_GROW,"fileofmap",0); } else { autodownloadmapInfo.fileOfMapInfo = memRealloc(autodownloadmapInfo.fileOfMapInfo,sizeof(FileOfMapInfo)*(autodownloadmapInfo.numFilesOfMapAllocated + NUM_FILES_OF_MAP_GROW),"rfileofmap",0); } autodownloadmapInfo.numFilesOfMapAllocated += NUM_FILES_OF_MAP_GROW; } strcpy(autodownloadmapInfo.fileOfMapInfo[index].filename,filename); strcpy(autodownloadmapInfo.fileOfMapInfo[index].filedirname,filedirname); autodownloadmapInfo.fileOfMapInfo[index].fileSize = fileSize; autodownloadmapInfo.fileOfMapInfo[index].fileCRC = 0; }
char* gpgData2Char(gpgme_data_t dh, int* newsize) { size_t tmpsize; char* newbuffer; char* tmpbuffer; TRACE(99, "gpgData2Char()", NULL); newbuffer = NULL; *newsize = 0; /* we have to rewind the buffer */ if (gpgme_data_seek(dh, 0, SEEK_SET)) { gpgError(gpgme_err_code_from_errno(errno)); return NULL; } tmpbuffer = memAlloc(__FILE__, __LINE__, BUFFERSIZE + 1); while ((tmpsize = gpgme_data_read(dh, tmpbuffer, BUFFERSIZE)) > 0) { newbuffer = memRealloc(__FILE__, __LINE__, newbuffer, *newsize, *newsize + tmpsize); /* Flawfinder: ignore */ memcpy(newbuffer + *newsize, tmpbuffer, tmpsize); *newsize += tmpsize; } memFree(__FILE__, __LINE__, tmpbuffer, BUFFERSIZE + 1); if (tmpsize < 0) { gpgError(gpgme_err_code_from_errno(errno)); return NULL; } return newbuffer; }
/** Allocates enough memory to hold size bytes. buf->used is not changed. \param buf Initialized binary buffer. \param size Total number of bytes. */ void ur_binReserve( UBuffer* buf, int size ) { uint8_t* mem; int avail; avail = ur_testAvail( buf ); if( size <= avail ) return; /* Double the buffer size (unless that is not big enough). */ avail *= 2; if( avail < size ) avail = (size < 8) ? 8 : size; if( buf->ptr.b ) mem = (uint8_t*) memRealloc( buf->ptr.b - FORWARD, avail + FORWARD ); else mem = (uint8_t*) memAlloc( avail + FORWARD ); assert( mem ); //printf( "realloc %d\n", mem == (buf->ptr.b - fsize) ); buf->ptr.b = mem + FORWARD; ur_avail(buf) = avail; }
int fileNameDistExpand ( char ** const nameptr, /*+ Pointer to name string pointer +*/ const int procnbr, /*+ Number of processes +*/ const int procnum, /*+ Number of current process +*/ const int protnum) /*+ Root process number +*/ { int namemax; int namenum; char * naexptr; int naexmax; int naexnum; int flagval; /* Flag set if expansion took place */ namemax = strlen (*nameptr); naexmax = namemax + FILENAMEDISTEXPANDNBR * 2; if ((naexptr = memAlloc ((naexmax + 1) * sizeof (char))) == NULL) /* "+ 1" for terminating character */ return (1); #ifdef COMMON_DEBUG sprintf (naexptr, FILENAMEDISTEXPANDSTR, procnbr); /* TRICK: Test if FILENAMEDISTEXPANDNBR is a size large enough */ if (atoi (naexptr) != procnbr) { errorPrint ("fileNameDistExpand: undersized integer string size"); return (1); } #endif /* COMMON_DEBUG */ for (namenum = naexnum = flagval = 0; namenum < namemax; ) { char charval; int dataval = 0; int datasiz; charval = (*nameptr)[namenum ++]; /* Get current characted */ datasiz = 1; /* Assume individual expanded character */ if (charval == '%') { char chnxval; /* Value of next char */ switch (chnxval = (*nameptr)[namenum ++]) { case 'p' : /* "%p" translates into number of processes */ flagval = 1; datasiz = FILENAMEDISTEXPANDNBR; dataval = procnbr; break; case 'r' : /* "%r" translates into process rank */ flagval = 1; datasiz = FILENAMEDISTEXPANDNBR; dataval = procnum; break; case '-' : /* "%-" translates into nothing but indicates distributed file */ datasiz = 0; flagval = 1; break; case '%' : /* "%%" translates into '%' */ break; default : charval = chnxval; /* Unrecognized character */ } } if (datasiz > 0) { if ((naexnum + datasiz) > naexmax) { char * nanwptr; naexmax += datasiz + FILENAMEDISTEXPANDNBR; if ((nanwptr = memRealloc (naexptr, (naexmax + 1) * sizeof (char))) == NULL) { /* "+ 1" for terminating character */ memFree (naexptr); return (1); } naexptr = nanwptr; } if (datasiz == 1) naexptr[naexnum ++] = charval; else { sprintf (&naexptr[naexnum], FILENAMEDISTEXPANDSTR, dataval); /* TRICK: Change format string if FILENAMEDISTEXPANDNBR changes */ naexptr[naexnum + FILENAMEDISTEXPANDNBR] = ' '; naexnum = strchr (&naexptr[naexnum], ' ') - naexptr; } } } naexptr[naexnum] = '\0'; /* Set terminating character as there is always room for it */ if ((flagval != 0) || (procnum == protnum)) /* If file name is a distributed one or we are the root process */ *nameptr = naexptr; /* Replace new string by old one */ else { memFree (naexptr); /* No need for the expanded string */ *nameptr = NULL; } return (0); }
/*----------------------------------------------------------------------------- Name : spTitleListLoad Description : Scans the missions directory and loads in the titles of all the mission files available. Inputs : void Outputs : Fills in spScenarios and spNumberScenarios Return : void ----------------------------------------------------------------------------*/ void spTitleListLoad(void) { #if !(defined(CGW) || defined (Downloadable) || defined(DLPublicBeta) || defined(OEM)) struct _finddata_t find; sdword handle, startHandle; #endif sdword index, numplayers; char fileName[_MAX_PATH], nameBuffer[_MAX_PATH]; #if MAIN_Password char upperNameBuffer[_MAX_PATH]; #endif char bitmapfileName[_MAX_PATH]; char *pString; char *title; filehandle scriptFile; #if defined(CGW) || defined(Downloadable) || defined(DLPublicBeta) scriptFile = fileOpen("DemoMissions.script", FF_TextMode); #else //oem has all missions! scriptFile = fileOpen("multiPlayerMissions.script", FF_TextMode); #endif dbgAssert(scriptFile != 0); while (fileLineRead(scriptFile, nameBuffer, _MAX_PATH) != FR_EndOfFile) { if (nameBuffer[0] == ';' || (nameBuffer[0] == '/' && nameBuffer[1] == '/')) { continue; } numplayers = 0; for (pString = nameBuffer; *pString != 0; pString++) { //search for a numeral character if (strchr("0123456789", *pString) != NULL) { //if this is a numeral numplayers = atoi(pString); memset(fileName, 0, _MAX_PATH); memStrncpy(fileName, nameBuffer, pString - nameBuffer + 1);//copy the start of the string memStrncpy(bitmapfileName, nameBuffer, pString - nameBuffer + 1);//copy the start of the string strcat(fileName, "%d"); //make something like: strcat(fileName, pString + 1); //'StdGame%d.level' } } if (numplayers == 0) { goto alreadyLoaded; } title = spTitleFind("MultiPlayer\\", nameBuffer); if (title == NULL) { goto alreadyLoaded; //break-continue } #if MAIN_Password if (!mainEnableSpecialMissions) { //if this is an "off-limits" mission strcpy(upperNameBuffer, title); _strupr(upperNameBuffer); if (strstr(upperNameBuffer, "ALL")) { memFree(title); goto alreadyLoaded; //break-continue } } #endif //MAIN_Password for (index = 0; index < spNumberScenarios; index++) { if (!_stricmp(spScenarios[index].fileSpec, fileName)) { //if matching file specs, // matches, but we should update max number of player if necessary if (numplayers > spScenarios[index].maxplayers) spScenarios[index].maxplayers = numplayers; if (numplayers < spScenarios[index].minplayers) spScenarios[index].minplayers = numplayers; memFree(title); goto alreadyLoaded; //break-continue } } if (spNumberScenarios >= spScenarioListLength) { spScenarioListLength += SP_ScenarioListGrowth; spScenarios = memRealloc(spScenarios, spScenarioListLength * sizeof(spscenario), "spScenarios", NonVolatile); } dbgAssert(spNumberScenarios < spScenarioListLength); spScenarios[spNumberScenarios].fileSpec = memStringDupe(fileName); spScenarios[spNumberScenarios].bitmapfileSpec = memStringDupe(bitmapfileName); spScenarios[spNumberScenarios].title = title; spScenarios[spNumberScenarios].maxplayers = numplayers; spScenarios[spNumberScenarios].minplayers = numplayers; spNumberScenarios++; alreadyLoaded:; } fileClose(scriptFile); #if !(defined(CGW) || defined (Downloadable) || defined(DLPublicBeta) || defined(OEM)) startHandle = handle = _findfirst(filePathPrepend("MultiPlayer\\*.", 0), &find); while (handle != -1) { if (find.name[0] == '.') { goto alreadyLoadedFromFileSystem; } fileName[0] = 0; numplayers = 0; for (pString = find.name; *pString != 0; pString++) { //search for a numeral character if (strchr("0123456789", *pString) != NULL) { //if this is a numeral numplayers = atoi(pString); memset(fileName, 0, _MAX_PATH); strncpy(fileName, find.name, pString - find.name);//copy the start of the string memStrncpy(bitmapfileName, find.name, pString - find.name + 1);//copy the start of the string strcat(fileName, "%d"); //make something like: strcat(fileName, pString + 1); //'StdGame%d.level' } } if (numplayers == 0) { goto alreadyLoadedFromFileSystem; } if (fileName[0] == 0) { goto alreadyLoadedFromFileSystem; } title = spTitleFind("MultiPlayer\\", find.name); if (title == NULL) { goto alreadyLoadedFromFileSystem; } #if MAIN_Password if (!mainEnableSpecialMissions) { //if this is an "off-limits" mission strcpy(upperNameBuffer, title); _strupr(upperNameBuffer); if (strstr(upperNameBuffer, "ALL")) { memFree(title); goto alreadyLoadedFromFileSystem; //break-continue } } #endif //MAIN_Password for (index = 0; index < spNumberScenarios; index++) { if (!_stricmp(spScenarios[index].fileSpec, fileName)) { //if matching file specs, // matches, but we should update max number of player if necessary if (numplayers > spScenarios[index].maxplayers) spScenarios[index].maxplayers = numplayers; if (numplayers < spScenarios[index].minplayers) spScenarios[index].minplayers = numplayers; memFree(title); goto alreadyLoadedFromFileSystem; //break-continue } } if (spNumberScenarios >= spScenarioListLength) { spScenarioListLength += SP_ScenarioListGrowth; spScenarios = memRealloc(spScenarios, spScenarioListLength * sizeof(spscenario), "spScenarios", NonVolatile); } dbgAssert(spNumberScenarios < spScenarioListLength); spScenarios[spNumberScenarios].fileSpec = memStringDupe(fileName); spScenarios[spNumberScenarios].bitmapfileSpec = memStringDupe(bitmapfileName); spScenarios[spNumberScenarios].title = title; spScenarios[spNumberScenarios].maxplayers = numplayers; spScenarios[spNumberScenarios].minplayers = numplayers; spNumberScenarios++; alreadyLoadedFromFileSystem:; handle = _findnext(startHandle, &find); } #endif //defined(CGW) || defined (Downloadable) || defined(DLPublicBeta) || defined(OEM) dbgAssert(spNumberScenarios > 0); if (spNumberScenarios > 1) { //alphabetically sort the scenario list qsort(&spScenarios[0],spNumberScenarios,sizeof(spscenario),compareScenariosCB); } //find the default scenario's index if (spCurrentSelected == 0) { //if it's not already selected spCurrentSelected = spScenarioFind(DefaultScenario); } }
/* ############################################################################# * * Description compress a buffer * Author Harry Brueckner * Date 2005-05-18 * Arguments char* srcbuffer - source buffer * int srclen - length of the source buffer * char** dstbuffer - compressed buffer * int* dstlen - length of the compressed buffer * Return 1 on error, 0 on success */ int zlibCompress(char* srcbuffer, int srclen, char** dstbuffer, int* dstlen, char** errormsg) { Byte* zbuffer; z_stream zh; int error, extrasize = srclen; TRACE(99, "zlibCompress()", NULL); *errormsg = NULL; /* we use this buffer for the compression */ zbuffer = memAlloc(__FILE__, __LINE__, srclen + extrasize); zh.zalloc = (alloc_func)0; zh.zfree = (free_func)0; zh.opaque = (voidpf)0; error = deflateInit2(&zh, config -> compression, Z_DEFLATED, 15 + 16, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY); if (error != Z_OK) { *errormsg = zh.msg; memFree(__FILE__, __LINE__, zbuffer, srclen + extrasize); return 1; } zh.next_out = zbuffer; zh.avail_out = (uInt)srclen + extrasize; zh.next_in = (Byte*)srcbuffer; zh.avail_in = (uInt)srclen; do { if (extrasize > srclen) { /* we increase the ouput buffer until it matches */ zbuffer = memRealloc(__FILE__, __LINE__, zbuffer, srclen + extrasize - BUFFERSIZE, srclen + extrasize); } error = deflate(&zh, Z_FINISH); extrasize += BUFFERSIZE; } while (error == Z_OK); /* readjust the extrasize */ extrasize -= BUFFERSIZE; if (error != Z_STREAM_END) { /* Z_STREAM_END means everything is ok */ *errormsg = zh.msg; memFree(__FILE__, __LINE__, zbuffer, srclen + extrasize); return 1; } /* we get the data back to the caller */ *dstlen = zh.total_out; *dstbuffer = memAlloc(__FILE__, __LINE__, zh.total_out); /* Flawfinder: ignore */ memcpy(*dstbuffer, zbuffer, zh.total_out); memFree(__FILE__, __LINE__, zbuffer, srclen + extrasize); error = deflateEnd(&zh); if (error != Z_OK) { *errormsg = zh.msg; return 1; } else { return 0; } }
/*----------------------------------------------------------------------------- Name : gpTitleListLoad Description : Scans the games directory and loads in the titles of all the game files available. Inputs : void Outputs : Fills in gpGames and gpNumberGames Return : void ----------------------------------------------------------------------------*/ void gpTitleListLoad(void) { struct _finddata_t find; sdword handle, startHandle; sdword index; char fileName[_MAX_PATH]; char fileSearch[100]; // char *pString; // char *title; for (index = 0; index < gpNumberGames; index++) { memFree(gpGames[index].fileSpec); memFree(gpGames[index].title); } if (gpNumberGames > 0) { memFree(gpGames); gpGames = NULL; gpNumberGames = 0; } strcpy(fileSearch,SavedGamesPath); strcat(fileSearch,"*.*"); startHandle = handle = _findfirst(filePathPrepend(fileSearch, 0), &find); while (handle != -1) { if (find.name[0] == '.') { goto alreadyLoaded; } fileName[0] = 0; strcpy(fileName, find.name); if (fileName[0] == 0) { goto alreadyLoaded; } if (strstr(fileName,PKTS_EXTENSION)) { goto alreadyLoaded; } for (index = 0; index < gpNumberGames; index++) { if (!_stricmp(gpGames[index].fileSpec, fileName)) { //if matching file specs, goto alreadyLoaded; //break-continue } } gpGames = memRealloc(gpGames, (gpNumberGames+1) * sizeof(gpgame), "gpGames", NonVolatile); gpGames[gpNumberGames].fileSpec = memStringDupe(fileName); //gpGames[gpNumberGames].title = title; gpGames[gpNumberGames].title = memStringDupe(fileName); gpNumberGames++; alreadyLoaded:; handle = _findnext(startHandle, &find); } if (gpNumberGames > 1) { //alphabetically sort the game list qsort(&gpGames[0],gpNumberGames,sizeof(gpgame),compareGamesCB); } gpCurrentSelected = gpCurrentGame = 0; //set default game }
/* ############################################################################# * * Description decompress a buffer * Author Harry Brueckner * Date 2005-05-18 * Arguments char* srcbuffer - source buffer * int srclen - length of the source buffer * char** dstbuffer - compressed buffer * int* dstlen - length of the compressed buffer * Return 1 on error, 0 on success */ int zlibDecompress(char* srcbuffer, int srclen, char** dstbuffer, int* dstlen, char** errormsg) { Byte* zbuffer = NULL; z_stream zh; int error, offset = 0; TRACE(99, "zlibDecompress()", NULL); *errormsg = NULL; *dstbuffer = NULL; zh.zalloc = (alloc_func)0; zh.zfree = (free_func)0; zh.opaque = (voidpf)0; zh.next_in = (Byte*)srcbuffer; zh.avail_in = (uInt)srclen; error = inflateInit2(&zh, 15 + 16); if (error != Z_OK) { *errormsg = zh.msg; return 1; } zbuffer = memAlloc(__FILE__, __LINE__, BUFFERSIZE); while (1) { /* reset the zbuffer */ zh.next_out = zbuffer; zh.avail_out = (uInt)BUFFERSIZE; error = inflate(&zh, Z_NO_FLUSH); if (zh.total_out) { if (*dstbuffer) { /* we must resize the buffer */ *dstbuffer = memRealloc(__FILE__, __LINE__, *dstbuffer, offset, zh.total_out); } else { /* we need a new buffer */ *dstbuffer = memAlloc(__FILE__, __LINE__, zh.total_out); } /* Flawfinder: ignore */ memcpy(*dstbuffer + offset, zbuffer, zh.total_out - offset); offset = zh.total_out; } if (error == Z_STREAM_END) { break; } if (error != Z_OK) { *errormsg = zh.msg; memFree(__FILE__, __LINE__, zbuffer, BUFFERSIZE); return 1; } } *dstlen = offset; memFree(__FILE__, __LINE__, zbuffer, BUFFERSIZE); error = inflateEnd(&zh); if (error != Z_OK) { *errormsg = zh.msg; return 1; } return 0; }
/*----------------------------------------------------------------------------- Name : gpTitleListLoad Description : Scans the games directory and loads in the titles of all the game files available. Inputs : void Outputs : Fills in gpGames and gpNumberGames Return : void ----------------------------------------------------------------------------*/ void gpTitleListLoad(void) { #ifdef _WIN32 struct _finddata_t find; sdword handle, startHandle; #else DIR* dp; struct dirent* dir_entry; #endif sdword index; char fileName[PATH_MAX] = ""; char fileSearch[PATH_MAX] = ""; // char *pString; // char *title; for (index = 0; index < gpNumberGames; index++) { memFree(gpGames[index].fileSpec); memFree(gpGames[index].title); } if (gpNumberGames > 0) { memFree(gpGames); gpGames = NULL; gpNumberGames = 0; } strcpy(fileSearch,SavedGamesPath); #ifdef _WIN32 strcat(fileSearch,"*.*"); startHandle = handle = _findfirst(filePathPrepend(fileSearch, FF_UserSettingsPath), &find); while (handle != -1) { if (find.name[0] == '.') { goto alreadyLoaded; } fileName[0] = 0; strcpy(fileName, find.name); if (fileName[0] == 0) { goto alreadyLoaded; } if (strstr(fileName,PKTS_EXTENSION)) { goto alreadyLoaded; } for (index = 0; index < gpNumberGames; index++) { if (!strcasecmp(gpGames[index].fileSpec, fileName)) { //if matching file specs, goto alreadyLoaded; //break-continue } } gpGames = memRealloc(gpGames, (gpNumberGames+1) * sizeof(gpgame), "gpGames", NonVolatile); gpGames[gpNumberGames].fileSpec = memStringDupe(fileName); //gpGames[gpNumberGames].title = title; gpGames[gpNumberGames].title = memStringDupe(fileName); gpNumberGames++; alreadyLoaded:; handle = _findnext(startHandle, &find); } #else dp = opendir(filePathPrepend(fileSearch, FF_UserSettingsPath)); if (dp) { while ((dir_entry = readdir(dp))) { if (dir_entry->d_name[0] == '.') continue; fileName[0] = 0; strcpy(fileName, dir_entry->d_name); if (fileName[0] == 0) continue; if (strstr(fileName,PKTS_EXTENSION)) continue; for (index = 0; index < gpNumberGames; index++) { if (!strcasecmp(gpGames[index].fileSpec, fileName)) { /*if matching file specs,*/ break; /*break-continue*/ } } if (index < gpNumberGames) continue; gpGames = memRealloc(gpGames, (gpNumberGames+1) * sizeof(gpgame), "gpGames", NonVolatile); gpGames[gpNumberGames].fileSpec = memStringDupe(fileName); //gpGames[gpNumberGames].title = title; gpGames[gpNumberGames].title = memStringDupe(fileName); gpNumberGames++; } closedir(dp); } #endif if (gpNumberGames > 1) { //alphabetically sort the game list qsort(&gpGames[0],gpNumberGames,sizeof(gpgame),compareGamesCB); } gpCurrentSelected = gpCurrentGame = 0; //set default game }