/** * Function used to encode an url * * note: dst must be already allocated and have the required size */ qboolean url_encode(char *str, char *dst) { char *pstr = str; int i = 0; if (!str) { LDE("%s\n", "str is NULL"); return qfalse; } if (!dst) { LDE("%s\n", "dst is NULL"); return qfalse; } while (*pstr) { if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') { dst[i] = *pstr; } else { dst[i++] = '%'; dst[i++] = to_hex(*pstr >> 4); dst[i] = to_hex(*pstr & 15); } pstr++; i++; } dst[i] = '\0'; return qtrue; }
DWORD Disasm::_getOpcodeLength(void *pAddress) { #if defined _WIN64 return (DWORD)LDE(pAddress, 64); #else return (DWORD)LDE(pAddress, 0); #endif }
/** * Get config command */ qboolean G_API_getConfig(void) { char *buf = NULL; char net_port[8] = { 0 }; char cphysics[8] = { 0 }; char encodedMapName[255] = { 0 }; buf = malloc(LARGE_RESPONSE_MAX_SIZE * sizeof (char)); if (!buf) { LDE("%s\n", "failed to allocate memory"); return qfalse; } sprintf(net_port, "%d", trap_Cvar_VariableIntegerValue("net_port")); sprintf(cphysics, "%d", physics.integer); if (url_encode(level.rawmapname, encodedMapName) == qfalse) { free(buf); return qfalse; } return G_SyncAPICall("o", buf, NULL, 4, encodedMapName, GAME_VERSION_DATED, cphysics, net_port); }
void CCBuffer::readData(char* p_out_data, unsigned int u_len) { BEGIN_IF(isReadable(u_len)) memcpy(p_out_data, CA(_p_buffer, _u_read_pos), u_len); AA(_u_read_pos, u_len); BEGIN_ELSE_IF(LQ(LDE(CS(_u_content_size, _u_read_pos),0), LNE(u_len, 0))) DO_ASSIGN(u_len, CS(_u_content_size, _u_read_pos)); memcpy(p_out_data, CA(_p_buffer, _u_read_pos), u_len); AA(_u_read_pos, u_len); END_IF }
/** * Function used to check an user input string * * note: dst must be already allocated and have the required size */ static qboolean check_string(char *str) { char *pstr = str; if (!str) { LDE("%s\n", "str is NULL"); return qfalse; } while (*pstr) { if (*pstr == '/') { return qfalse; } pstr++; } return qtrue; }
void CCBuffer::moveLeft(unsigned int u_len) { BEGIN_IF(LE(_u_content_size, 0)) DO_RETURN; END_IF DO_ASSERT(LNE(u_len, 0), "LNE(u_len, 0)"); BEGIN_IF(LDE(u_len, _u_content_size)) clear(); BEGIN_ELSE BEGIN_FOR(DO_ASSIGN(unsigned int i, u_len), LX(i, _u_content_size), AAI(i)) DO_ASSIGN(QV(CA(_p_buffer, CS(i, u_len))), QV(CA(_p_buffer, i))); DO_ASSIGN(QV(CA(_p_buffer, i)), 0); END_FOR DO_ASSIGN(_u_write_pos, TO_UINT(MAX(0, CS(TO_INT(_u_write_pos), TO_INT(u_len))))); DO_ASSIGN(_u_read_pos, TO_UINT(MAX(0, CS(TO_INT(_u_read_pos), TO_INT(u_len))))); DO_ASSIGN(_u_mark_pos, TO_UINT(MAX(0, CS(TO_INT(_u_mark_pos), TO_INT(u_len))))); DO_ASSIGN(_u_content_size, CS(_u_content_size, u_len)); END_IF }
/** * Asynchronous (using pthreads) API call function * * command: must be a command in apiCommands * result: pointer to an *already allocated* buffer for storing result * ent: entity who made the request * count: number of variadic arguments */ qboolean G_AsyncAPICall(char *command, char *result, gentity_t *ent, int count, ...) { struct query_s *queryStruct; pthread_t thread; pthread_attr_t attr; int returnCode = 0; void *(*handler)(void *) = NULL; va_list ap; int i = 0; if (api_module == NULL || API_query == NULL) { LDE("%s\n", "API module is not loaded"); return qfalse; } // Check if thread limit is reached if (activeThreadsCounter >= THREADS_MAX) { LDE("threads limit (%d) reached: %d\n", THREADS_MAX, activeThreadsCounter); return qfalse; } // Check if we are allowed to start a new thread if (!threadingAllowed) { LDE("%s\n", "starting new threads is forbidden"); return qfalse; } // Check number of arguments in ... (=count) if (count <= 0) { LDE("invalid argument count %d\n", count); return qfalse; } queryStruct = malloc(sizeof (struct query_s)); if (queryStruct == NULL) { LDE("%s\n", "failed to allocate memory"); return qfalse; } va_start(ap, count); // Init query buffer memset(queryStruct->query, 0, QUERY_MAX_SIZE); for (i = 0; i < count; ++i) { char *arg; arg = va_arg(ap, char *); if (!arg) { LDE("empty argument %d with command '%s'\n", i, command); free(queryStruct); va_end(ap); return qfalse; } Q_strcat(queryStruct->query, QUERY_MAX_SIZE, arg); // No trailing / if (i + 1 < count) { Q_strcat(queryStruct->query, QUERY_MAX_SIZE, CHAR_SEPARATOR); } } va_end(ap); Q_strncpyz(queryStruct->cmd, command, sizeof (queryStruct->cmd)); queryStruct->result = result; queryStruct->ent = ent; // Get the command handler handler = getHandlerForCommand(command); if (!handler) { LDE("no handler for command '%s'\n", command); free(queryStruct); return qfalse; } LDI("asynchronous API call with command: '%s', query '%s'\n", command, queryStruct->query); // Create threads as detached as they will never be joined if (pthread_attr_init(&attr)) { LDE("%s\n", "error in pthread_attr_init"); free(queryStruct); return qfalse; } if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) { LDE("%s\n", "error in pthread_attr_setdetachstate"); free(queryStruct); return qfalse; } returnCode = pthread_create(&thread, &attr, handler, (void *)queryStruct); if (returnCode) { LDE("error in pthread_create, return value is %d\n", returnCode); free(queryStruct); return qfalse; } // Nico, increase active threads counter activeThreadsCounter++; G_DPrintf("%s: increasing threads counter to %d\n", GAME_VERSION, activeThreadsCounter); if (pthread_attr_destroy(&attr)) { LDE("%s\n", "error in pthread_attr_destroy"); // Nico, note: I don't free querystruct because it's used in the thread return qfalse; } return qtrue; }
/** * Synchronous API call function * * command: must be a command in apiCommands * result: pointer to an *already allocated* buffer for storing result * ent: entity who made the request * count: number of variadic arguments */ qboolean G_SyncAPICall(char *command, char *result, gentity_t *ent, int count, ...) { struct query_s *queryStruct; void *(*handler)(void *) = NULL; va_list ap; int i = 0; if (api_module == NULL || API_query == NULL) { LDE("%s\n", "API module is not loaded"); return qfalse; } // Check number of arguments in ... (=count) if (count <= 0) { LDE("invalid argument count %d\n", count); return qfalse; } queryStruct = malloc(sizeof (struct query_s)); if (queryStruct == NULL) { LDE("%s\n", "failed to allocate memory"); return qfalse; } va_start(ap, count); // Init query buffer memset(queryStruct->query, 0, QUERY_MAX_SIZE); for (i = 0; i < count; ++i) { char *arg; arg = va_arg(ap, char *); if (!arg) { LDE("empty argument %d with command '%s'\n", i, command); free(queryStruct); va_end(ap); return qfalse; } Q_strcat(queryStruct->query, QUERY_MAX_SIZE, arg); // No trailing / if (i + 1 < count) { Q_strcat(queryStruct->query, QUERY_MAX_SIZE, CHAR_SEPARATOR); } } va_end(ap); Q_strncpyz(queryStruct->cmd, command, sizeof (queryStruct->cmd)); queryStruct->result = result; queryStruct->ent = ent; // Get the command handler handler = getHandlerForCommand(command); if (!handler) { LDE("no handler for command '%s'\n", command); free(queryStruct); return qfalse; } LDI("synchronous API call with command: '%s', query '%s'\n", command, queryStruct->query); handler(queryStruct); return qtrue; }