void AddSwizzleUsingElementCount(HLSLCrossCompilerContext* psContext, uint32_t count) { bstring glsl = *psContext->currentGLSLString; if(count) { bcatcstr(glsl, "."); bcatcstr(glsl, "x"); count--; } if(count) { bcatcstr(glsl, "y"); count--; } if(count) { bcatcstr(glsl, "z"); count--; } if(count) { bcatcstr(glsl, "w"); count--; } }
void taskmain(int argc, char *argv[]) { dbg_set_log(stderr); int i = 0; bstring arguments = bfromcstr(argv[0]); for(i = 1; i < argc; i++) { bstring a = bfromcstr(argv[i]); // compensate for quotes getting taken off by the shell // TODO: also need to escape " to bring back that if(bstrchr(a, ' ') != -1) { bcatcstr(arguments, " \""); bconcat(arguments, a); bcatcstr(arguments, "\""); } else { bcatcstr(arguments, " "); bconcat(arguments, a); } bdestroy(a); } debug("RUNNING: %s", bdata(arguments)); taskexitall(Command_run(arguments)); }
bool do_install(CURL* curl, bstring name) { FILE* fp; CURLcode res; long httpcode = 0; struct stat buffer; bstring url = bfromcstr("http://dms.dcputoolcha.in/modules/download?name="); bstring modpath = osutil_getmodulepath(); // Append the file name. bconchar(modpath, '/'); bconcat(modpath, name); bcatcstr(modpath, ".lua"); bconcat(url, name); bcatcstr(url, ".lua"); // Check to see if the module is already installed. if (stat(modpath->data, &buffer) == 0) { if (unlink(modpath->data) == 0) printd(LEVEL_WARNING, "removed existing %s module.\n", name->data); else { printd(LEVEL_ERROR, "unable to remove existing %s module.\n", name->data); return 1; } } // Open the file and do the cURL transfer. printd(LEVEL_DEFAULT, "querying module repository...\n"); fp = fopen(modpath->data, "wb"); curl_easy_setopt(curl, CURLOPT_URL, url->data); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); res = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpcode); if (res != 0 && httpcode != 200) { bdestroy(url); bdestroy(name); bdestroy(modpath); printd(LEVEL_ERROR, "curl failed with error code %i, HTTP error code %i.\n", res, httpcode); return 1; } fclose(fp); printd(LEVEL_DEFAULT, "module %s installed.\n", name->data); return 0; }
bool do_uninstall(CURL* curl, bstring name) { bstring modpath = osutil_getmodulepath(); struct stat buffer; // Append the file name. bconchar(modpath, '/'); bconcat(modpath, name); bcatcstr(modpath, ".lua"); // Check to see if the module is installed. if (stat(modpath->data, &buffer) == 0) { if (unlink(modpath->data) == 0) printd(LEVEL_DEFAULT, "removed existing %s module.\n", name->data); else { printd(LEVEL_ERROR, "unable to remove existing %s module.\n", name->data); return 1; } } else printd(LEVEL_WARNING, "module %s is not installed.\n", name->data); return 0; }
/** * Take the request header and adds it to a magic GUID. * Which is returned in a SHA1-hash * @param req - Mongrel2 request data * @return 0 on success */ static int mongrel2_ws_08_calculate_accept(mongrel2_request *req, bstring *ptr){ int retval; bstring key = mongrel2_request_get_header(req,WEBSOCKET_08_KEY); if(key == NULL){ return -1; } retval = bcatcstr(key,WEBSOCKET_08_GUID); if(retval != 0){ return -1; } void* buf = calloc(sizeof(char),SHA1_LEN); if(buf == NULL){ return -1; } sha1((const unsigned char*)bdata(key),blength(key),buf); bstring accept = blk2bstr(buf,SHA1_LEN); bstring accept_encoded = bBase64Encode(accept); bdestroy(accept); free(buf); bdestroy(key); *ptr = accept_encoded; return 0; }
static bstring ssh_arg_to_privkey(const bstring argkey) { struct tagbstring tmp; bstring privkey; int i, len; /* * A simple routine to convert from the private key format suitable for * inclusion in a Torrc back to PEM. */ len = strlen(OBFSPROXYSSH_PEM_HDR) + blength(argkey) + blength(argkey) /64 + 1 + strlen(OBFSPROXYSSH_PEM_FTR); privkey = bfromcstralloc(len, OBFSPROXYSSH_PEM_HDR); for (i = 0; i < blength(argkey); i += 64) { bmid2tbstr(tmp, argkey, i, 64); bconcat(privkey, &tmp); bconchar(privkey, '\n'); } bcatcstr(privkey, OBFSPROXYSSH_PEM_FTR); return privkey; }
void Handler_notify_credits(Handler *handler, int id, int credits) { void *socket = handler->send_socket; assert(socket && "Socket can't be NULL"); tns_outbuf outbuf = {.buffer = NULL}; bstring payload = NULL; if(handler->protocol == HANDLER_PROTO_TNET) { int header_start = tns_render_request_start(&outbuf); bstring creditsstr = bformat("%d", credits); tns_render_hash_pair(&outbuf, &HTTP_METHOD, &JSON_METHOD); tns_render_hash_pair(&outbuf, &DOWNLOAD_CREDITS, creditsstr); bdestroy(creditsstr); tns_outbuf_clamp(&outbuf, header_start); payload = bformat("%s %d @* %s%d:%s,", bdata(handler->send_ident), id, bdata(tns_outbuf_to_bstring(&outbuf)), blength(&CREDITS_MSG), bdata(&CREDITS_MSG)); } else { bstring headers = bfromcstralloc(PAYLOAD_GUESS, "{"); bcatcstr(headers, "\"METHOD\":\"JSON\",\""); bconcat(headers, &DOWNLOAD_CREDITS); bcatcstr(headers, "\":\""); bformata(headers, "%d", credits); bcatcstr(headers, "\"}"); payload = bformat("%s %d @* %d:%s,%d:%s,", bdata(handler->send_ident), id, blength(headers), bdata(headers), blength(&CREDITS_MSG), bdata(&CREDITS_MSG)); bdestroy(headers); } check(payload != NULL, "Failed to make the payload for credits."); if(Handler_deliver(socket, bdata(payload), blength(payload)) == -1) { log_err("Can't tell handler %d giving credits.", id); } error: //fallthrough if(payload) free(payload); if(outbuf.buffer) free(outbuf.buffer); }
bstring getWhitespace(int spaces) { bstring whitespace = bfromcstralloc(spaces * 5, ""); while (whitespace->slen < spaces * 5) { bcatcstr(whitespace, " "); } return whitespace; }
int TryCompileShader(GLenum eGLSLShaderType, char* inFilename, char* shader, double* pCompileTime) { GLint iCompileStatus; GLuint hShader; Timer_t timer; InitTimer(&timer); InitOpenGL(); hShader = glCreateShaderObjectARB(eGLSLShaderType); glShaderSourceARB(hShader, 1, (const char **)&shader, NULL); ResetTimer(&timer); glCompileShaderARB(hShader); *pCompileTime = ReadTimer(&timer); /* Check it compiled OK */ glGetObjectParameterivARB (hShader, GL_OBJECT_COMPILE_STATUS_ARB, &iCompileStatus); if (iCompileStatus != GL_TRUE) { FILE* errorFile; GLint iInfoLogLength = 0; char* pszInfoLog; bstring filename = bfromcstr(inFilename); char* cstrFilename; glGetObjectParameterivARB (hShader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &iInfoLogLength); pszInfoLog = malloc(iInfoLogLength); printf("Error: Failed to compile GLSL shader\n"); glGetInfoLogARB (hShader, iInfoLogLength, NULL, pszInfoLog); printf(pszInfoLog); bcatcstr(filename, "_compileErrors.txt"); cstrFilename = bstr2cstr(filename, '\0'); //Dump to file errorFile = fopen(cstrFilename, "w"); fprintf(errorFile, pszInfoLog); fclose(errorFile); bdestroy(filename); free(cstrFilename); free(pszInfoLog); return 0; } return 1; }
void AddIndentation(HLSLCrossCompilerContext* psContext) { int i; int indent = psContext->indent; bstring glsl = *psContext->currentGLSLString; for(i=0; i < indent; ++i) { bcatcstr(glsl, " "); } }
void TranslateOperandIndexMAD(HLSLCrossCompilerContext* psContext, const Operand* psOperand, int index, uint32_t multiply, uint32_t add) { int i = index; int isGeoShader = psContext->psShader->eShaderType == GEOMETRY_SHADER ? 1 : 0; bstring glsl = *psContext->currentGLSLString; ASSERT(index < psOperand->iIndexDims); switch(psOperand->eIndexRep[i]) { case OPERAND_INDEX_IMMEDIATE32: { if(i > 0 || isGeoShader) { bformata(glsl, "[%d*%d+%d]", psOperand->aui32ArraySizes[i], multiply, add); } else { bformata(glsl, "%d*%d+%d", psOperand->aui32ArraySizes[i], multiply, add); } break; } case OPERAND_INDEX_RELATIVE: { bcatcstr(glsl, "[int("); //Indexes must be integral. TranslateOperand(psContext, psOperand->psSubOperand[i], TO_FLAG_NONE); bformata(glsl, ")*%d+%d]", multiply, add); break; } case OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE: { bcatcstr(glsl, "[(int("); //Indexes must be integral. TranslateOperand(psContext, psOperand->psSubOperand[i], TO_FLAG_NONE); bformata(glsl, ") + %d)*%d+%d]", psOperand->aui32ArraySizes[i], multiply, add); break; } default: { break; } } }
static inline void B(bstring headers, const bstring k, const bstring v, int *first) { if(v) { if(*first) { bcatcstr(headers, "\""); *first = 0; } else { bcatcstr(headers, ",\""); } bconcat(headers, k); bconcat(headers, &JSON_OBJSEP); bstring vstr = json_escape(v); bconcat(headers, vstr); bcatcstr(headers, "\""); bdestroy(vstr); } }
void update_config(char *token, char *username) { bstring file = locate_config_file(); bstring tmp = bstrcpy(file); bcatcstr(tmp, ".tmp_XXXXXX"); int fd = mkstemp((char *) tmp->data); if (fd < 0) { perror("mkstemp"); exit(1); } FILE *fp = fdopen(fd, "w"); if (fp == 0) { perror("fdopen"); exit(1); } dictionary *config; struct stat statbuf; int rc = stat((char *) file->data, &statbuf); if (rc < 0 || statbuf.st_size == 0) { /* create a new empty dictionary */ config = dictionary_new(0); dictionary_set(config, "authentication", 0); } else { config = iniparser_load(bdata(file)); } iniparser_set(config, "authentication:token", token); if (username) iniparser_set(config, "authentication:user_id", username); iniparser_dump_ini(config, fp); iniparser_freedict(config); fclose(fp); if (rename(bdata(tmp), bdata(file)) < 0) { fprintf(stderr, "Error rename %s to %s: %s\n", bdata(tmp), bdata(file), strerror(errno)); exit(1); } bdestroy(tmp); bdestroy(file); }
static bstring inject_fmt(redir_request *req, struct redir_conn_t *conn) { char *url = req->inject_url; if (req->ibuf->slen) return req->ibuf; if (!url || !*url) url = _options.inject; if (!url || !*url) return 0; bassigncstr(req->ibuf, ""); if (conn) { char hexchal[1+(2*REDIR_MD5LEN)]; extern void redir_wispr2_reply (struct redir_t *redir, struct redir_conn_t *conn, int res, long int timeleft, char* hexchal, char* reply, char* redirurl, bstring b); redir_chartohex(conn->s_state.redir.uamchal, hexchal, REDIR_MD5LEN); redir_wispr2_reply(req->parent, conn, REDIR_NOTYET, 0, hexchal, 0, 0, req->ibuf); } if (_options.inject_ext) { bcatcstr(req->ibuf, "<script type=\"text/javascript\">"); bcatcstr(req->ibuf, _options.inject_ext); bcatcstr(req->ibuf, "</script>"); } bcatcstr(req->ibuf, "<script src='"); bcatcstr(req->ibuf, url); bcatcstr(req->ibuf, "'></script>"); return req->ibuf; }
void TextureName(HLSLCrossCompilerContext* psContext, const uint32_t ui32RegisterNumber, const int bZCompare) { bstring glsl = *psContext->currentGLSLString; ResourceBinding* psBinding = 0; int found; found = GetResourceFromBindingPoint(RGROUP_TEXTURE, ui32RegisterNumber, &psContext->psShader->sInfo, &psBinding); if(bZCompare) { bcatcstr(glsl, "hlslcc_zcmp_"); } if(found) { int i = 0; char name[MAX_REFLECT_STRING_LENGTH]; uint32_t ui32ArrayOffset = ui32RegisterNumber - psBinding->ui32BindPoint; while(psBinding->Name[i] != '\0' && i < (MAX_REFLECT_STRING_LENGTH-1)) { name[i] = psBinding->Name[i]; //array syntax [X] becomes _0_ //Otherwise declarations could end up as: //uniform sampler2D SomeTextures[0]; //uniform sampler2D SomeTextures[1]; if(name[i] == '[' || name[i] == ']') name[i] = '_'; ++i; } name[i] = '\0'; if(ui32ArrayOffset) { bformata(glsl, "%s%d", name, ui32ArrayOffset); } else { bformata(glsl, "%s", name); } } else { bformata(glsl, "UnknownResource%d", ui32RegisterNumber); } }
char * test_update() { Snippet * oldSnippet = DB_get(1); Snippet * newSnippet = DB_get(1); bcatcstr(newSnippet->code,"i've updated the code !"); DB_update(newSnippet); snippet_destroy(newSnippet); newSnippet = NULL; newSnippet = DB_get(1); mu_assert(biseq(oldSnippet->code, newSnippet->code) == 0, "The code should be different."); snippet_destroy(newSnippet); snippet_destroy(oldSnippet); return NULL; }
bstring locate_config_file() { /* * Find the config file in $HOME/.kbase_config */ char *h = getenv("HOME"); if (!h) { fprintf(stderr, "home not found in env\n"); return 0; } bstring home = bfromcstr(h); bcatcstr(home, "/.kbase_config"); return home; }
bstring build_oauth_header(bKeyValues *keyvalues) { bstring buffer = bfromcstr("Authorization: OAuth "); int i = 0; int first = 1; for(i = 0; i < keyvalues->qty; i++) { if(first) { first = 0; } else { bconchar(buffer, ','); } bconcat(buffer, keyvalues->entry[i].key); bcatcstr(buffer, "=\""); bconcat(buffer, keyvalues->entry[i].value); bconchar(buffer, '\"'); } return buffer; }
// Append the contents of the AST node to the string. // // node - The node to dump. // ret - A pointer to the bstring to concatenate to. // // Return 0 if successful, otherwise returns -1.s int qip_ast_freturn_dump(qip_ast_node *node, bstring ret) { int rc; check(node != NULL, "Node required"); check(ret != NULL, "String required"); // Append dump. check(bcatcstr(ret, "<freturn>\n") == BSTR_OK, "Unable to append dump"); // Recursively dump children. if(node->freturn.value != NULL) { rc = qip_ast_node_dump(node->freturn.value, ret); check(rc == 0, "Unable to dump return value"); } return 0; error: return -1; }
/** * The bstring library is a alternative to c-strings. * It makes appending strings together a lot easier. * * @param busname * @param objectpath * @param interface * @param member * @return A managed string object */ static bstring createRule( char* busname, char* objectpath, char* interface, char* member) { bstring rule = bfromcstr("type='signal'"); if(busname && *busname) { bcatcstr(rule,",sender='"); bcatcstr(rule,busname); bconchar(rule,'\''); } if(objectpath && *objectpath) { bcatcstr(rule,",path='"); bcatcstr(rule,objectpath); bconchar(rule,'\''); } if(interface && *interface) { bcatcstr(rule,",interface='"); bcatcstr(rule,interface); bconchar(rule,'\''); } if(member && *member) { bcatcstr(rule,",member='"); bcatcstr(rule,member); bconchar(rule,'\''); } return rule; }
static int chilli_sessions(bstring b) { struct dhcp_conn_t *conn = dhcp->firstusedconn; char *state = "<font color=green>Authorized</font>"; bstring s = bfromcstr(""); bcatcstr(b, "{\"service\":[{ \""); bcatcstr(b, getenv("CAP_table")); bcatcstr(b, "\":[ "); while (conn) { struct app_conn_t *appconn = (struct app_conn_t *)conn->peer; if (appconn && appconn->s_state.authenticated) { } else { state = "<font color=red>Redirect</font>"; } bassignformat(s, "{" "\"state\":\"%s\"," "\"macAddress\":\"%.2X-%.2X-%.2X-%.2X-%.2X-%.2X\"," "\"ipAddress\":\"%s\",", state, conn->hismac[0], conn->hismac[1], conn->hismac[2], conn->hismac[3], conn->hismac[4], conn->hismac[5], inet_ntoa(conn->hisip) ); conn = conn->next; if (appconn) { session_json_params(&appconn->s_state, &appconn->s_params, s, 0); bcatcstr(s, ","); session_json_acct(&appconn->s_state, &appconn->s_params, s, 0); } bcatcstr(s, "},"); bconcat(b, s); } bcatcstr(b, "]} ]}"); return 0; }
struct lua_hardware* vm_hw_lua_load(vm_t* vm, bstring name) { bstring path, modtype; struct lua_hardware* hw; int module, hwinfo; // Calculate full path to file. path = osutil_getarg0path(); #ifdef _WIN32 bcatcstr(path, "modules\\"); #else bcatcstr(path, "/modules/"); #endif bconcat(path, name); // Create the new lua hardware structure. hw = malloc(sizeof(struct lua_hardware)); hw->vm = vm; hw->state = lua_open(); assert(hw->state != NULL); luaL_openlibs(hw->state); luaX_loadexpressionlib(hw->state); // Execute the code in the new Lua context. if (luaL_dofile(hw->state, path->data) != 0) { printf("lua error was %s.\n", lua_tostring(hw->state, -1)); // Return NULL. lua_close(hw->state); free(hw); bdestroy(path); return NULL; } // Load tables. lua_getglobal(hw->state, "MODULE"); module = lua_gettop(hw->state); lua_getglobal(hw->state, "HARDWARE"); hwinfo = lua_gettop(hw->state); // Ensure both tables were provided. if (lua_isnoneornil(hw->state, module) || lua_isnoneornil(hw->state, hwinfo)) { printf("failed to load hardware from %s.\n", path->data); // Return NULL. lua_close(hw->state); free(hw); bdestroy(path); return NULL; } // Check to see whether the module is // a hardware module. lua_getfield(hw->state, module, "Type"); modtype = bfromcstr(lua_tostring(hw->state, -1)); if (!biseqcstrcaseless(modtype, "Hardware")) { // Return NULL. lua_pop(hw->state, 1); lua_close(hw->state); free(hw); bdestroy(modtype); bdestroy(path); return NULL; } lua_pop(hw->state, 1); bdestroy(modtype); // Store information into the Lua // hardware structure. lua_getfield(hw->state, hwinfo, "ID"); lua_getfield(hw->state, hwinfo, "Version"); lua_getfield(hw->state, hwinfo, "Manufacturer"); hw->device.id = (uint32_t)lua_tonumber(hw->state, lua_gettop(hw->state) - 2); hw->device.version = (uint16_t)lua_tonumber(hw->state, lua_gettop(hw->state) - 1); hw->device.manufacturer = (uint32_t)lua_tonumber(hw->state, lua_gettop(hw->state)); lua_pop(hw->state, 3); printf("hardware loaded ID: %u, Version: %u, Manufacturer: %u.\n", hw->device.id, hw->device.version, hw->device.manufacturer); // Register the hardware. hw->device.handler = &vm_hw_lua_interrupt; hw->device.userdata = hw; vm_hw_register(vm, hw->device); // Register the hooks. hw->cycle_id = vm_hook_register(vm, &vm_hw_lua_cycle, HOOK_ON_POST_CYCLE, hw); hw->write_id = vm_hook_register(vm, &vm_hw_lua_write, HOOK_ON_WRITE, hw); // Pop tables from stack. lua_pop(hw->state, 2); // Return new hardware. return hw; }
static int redir_conn_read(struct conn_t *conn, void *ctx) { redir_request *req = (redir_request *)ctx; uint8_t bb[PKT_MAX_LEN]; int s, r; if ((s=redir_cli_rewrite(req, conn)) != 0) return 0; r = safe_recv(conn->sock, bb, sizeof(bb)-1, 0); #if(_debug_) log_dbg("conn_read: %d clen=%d", r, req->clen); #endif if (r == 0) { if (req->read_closed && redir_cli_rewrite(req, conn) == 0) { log_dbg("done reading and writing"); redir_conn_finish(conn, req); return -1; } req->read_closed = 1; } else if (r < 0 && errno != EWOULDBLOCK && errno != EAGAIN) { log_dbg("ERRNO %d", errno); redir_conn_finish(conn, ctx); } else if (r > 0) { #ifdef ENABLE_REDIRINJECT bstring inject = inject_fmt(req, 0); #endif bb[r]=0; req->last_active = mainclock_tick(); #ifdef ENABLE_REDIRINJECT /** * */ if (inject && !req->headers) { char *newline = "\r\n\r\n"; char *eoh; bcatblk(req->hbuf, bb, r); if ((eoh = strstr((char *)req->hbuf->data, newline))) { int header_len = eoh - (char *)req->hbuf->data; bstring newhdr = bfromcstr(""); if (strncmp((char *)req->hbuf->data, "HTTP/1.", 7) || strncmp((char *)req->hbuf->data+8, " 2", 2)) { log_dbg("Not HTTP/1.X 2XX reply"); bcatblk(newhdr, req->hbuf->data, header_len + 4); } else { char *hdr, *p; int clen = 0; hdr = (char *)req->hbuf->data; while (hdr && *hdr) { int l; p = strstr(hdr, "\r\n"); if (p == hdr) { break; } else if (p) { l = (p - hdr); } else { l = (eoh - hdr); } if (!strncasecmp(hdr, "content-length:", 15)) { char c = hdr[l]; hdr[l] = 0; clen = req->clen = atoi(hdr+15); log_dbg("Detected Content Length %d", req->clen); hdr[l] = c; } else if (!strncasecmp(hdr, "content-type:", 13)) { if (strstr(hdr, "text/html")) { req->html = 1; } } else if (strcasestr(hdr, "content-encoding: gzip")) { req->gzip = 1; } else if (strcasestr(hdr, "transfer-encoding:") && strstr(hdr,"chunked")) { req->chunked = 1; } hdr += l + 2; if (!p) break; } hdr = (char *)req->hbuf->data; while (hdr && *hdr) { int l; int skip = 0; p = strstr(hdr, "\r\n"); if (p == hdr) { break; } else if (p) { l = (p - hdr); } else { l = (eoh - hdr); } if (req->html) { if (clen && !strncasecmp(hdr, "content-length:", 15)) { char tmp[128]; if (inject) clen += inject->slen; safe_snprintf(tmp, sizeof(tmp), "Content-Length: %d\r\n", clen); bcatcstr(newhdr, tmp); skip = 1; } else if (!strncasecmp(hdr, "connection:", 11)) { skip = 1; } else if (!strncasecmp(hdr, "accept-ranges:", 14)) { skip = 1; } } log_dbg("Resp Header [%d] %.*s%s", l, l, hdr, skip ? " [Skipped]" : ""); if (!skip) { bcatblk(newhdr, hdr, l + 2); } hdr += l + 2; if (!p) break; } bcatcstr(newhdr, "Connection: close\r\n"); /* process headers */ /* Is HTML */ /* Check content-encoding chunked */ /* Adjust content-length */ bcatblk(newhdr, newline, 2); if (req->html) { if (req->chunked) { char tmp[56]; safe_snprintf(tmp, sizeof(tmp), "%x\r\n", inject->slen); bcatcstr(newhdr, tmp); } bconcat(newhdr, inject); if (req->chunked) { bcatblk(newhdr, newline, 2); } } } bcatblk(newhdr, eoh + 4, req->hbuf->slen - header_len - 4); if (req->clen > 0) /* adjust clen */ req->clen -= (req->hbuf->slen - header_len - 4); redir_cli_write(req, newhdr->data, newhdr->slen); req->headers = 1; bdestroy(newhdr); } } else { #endif redir_cli_write(req, bb, r); if (req->clen > 0) req->clen -= r; #ifdef ENABLE_REDIRINJECT } #endif } /*log_dbg("leaving redir_conn_read()");*/ return 0; }
static void TranslateVariableName(HLSLCrossCompilerContext* psContext, const Operand* psOperand, uint32_t ui32TOFlag, uint32_t* pui32IgnoreSwizzle) { int integerConstructor = 0; bstring glsl = *psContext->currentGLSLString; *pui32IgnoreSwizzle = 0; if(psOperand->eType != OPERAND_TYPE_IMMEDIATE32 && psOperand->eType != OPERAND_TYPE_IMMEDIATE64 && psOperand->eType != OPERAND_TYPE_CONSTANT_BUFFER) { const uint32_t swizCount = psOperand->iNumComponents; SHADER_VARIABLE_TYPE eType = GetOperandDataType(psContext, psOperand); if( (ui32TOFlag & (TO_FLAG_INTEGER|TO_FLAG_UNSIGNED_INTEGER)) == (TO_FLAG_INTEGER|TO_FLAG_UNSIGNED_INTEGER)) { //Can be either int or uint if(eType != SVT_INT && eType != SVT_UINT) { if(swizCount == 1) bformata(glsl, "int("); else bformata(glsl, "ivec%d(", swizCount); integerConstructor = 1; } } else { if((ui32TOFlag & (TO_FLAG_INTEGER|TO_FLAG_DESTINATION))==TO_FLAG_INTEGER && eType != SVT_INT) { //Convert to int if(swizCount == 1) bformata(glsl, "int("); else bformata(glsl, "ivec%d(", swizCount); integerConstructor = 1; } if((ui32TOFlag & (TO_FLAG_UNSIGNED_INTEGER|TO_FLAG_DESTINATION))==TO_FLAG_UNSIGNED_INTEGER && eType != SVT_UINT) { //Convert to uint if(swizCount == 1) bformata(glsl, "uint("); else bformata(glsl, "uvec%d(", swizCount); integerConstructor = 1; } } } switch(psOperand->eType) { case OPERAND_TYPE_IMMEDIATE32: { if(psOperand->iNumComponents == 1) { if(ui32TOFlag & TO_FLAG_UNSIGNED_INTEGER) { bformata(glsl, "%uu", *((unsigned int*)(&psOperand->afImmediates[0]))); } else if((ui32TOFlag & TO_FLAG_INTEGER) || psOperand->iIntegerImmediate || fpcheck(psOperand->afImmediates[0])) { bformata(glsl, "%d", *((int*)(&psOperand->afImmediates[0]))); } else { bformata(glsl, "%f", psOperand->afImmediates[0]); } } else { if(ui32TOFlag & TO_FLAG_UNSIGNED_INTEGER) { bformata(glsl, "uvec4(%uu, %uu, %uu, %uu)", *(unsigned int*)&psOperand->afImmediates[0], *(unsigned int*)&psOperand->afImmediates[1], *(unsigned int*)&psOperand->afImmediates[2], *(unsigned int*)&psOperand->afImmediates[3]); } else if((ui32TOFlag & TO_FLAG_INTEGER) || psOperand->iIntegerImmediate || fpcheck(psOperand->afImmediates[0]) || fpcheck(psOperand->afImmediates[1]) || fpcheck(psOperand->afImmediates[2]) || fpcheck(psOperand->afImmediates[3])) { bformata(glsl, "ivec4(%d, %d, %d, %d)", *(int*)&psOperand->afImmediates[0], *(int*)&psOperand->afImmediates[1], *(int*)&psOperand->afImmediates[2], *(int*)&psOperand->afImmediates[3]); } else { bformata(glsl, "vec4(%f, %f, %f, %f)", psOperand->afImmediates[0], psOperand->afImmediates[1], psOperand->afImmediates[2], psOperand->afImmediates[3]); } if(psOperand->iNumComponents != 4) { AddSwizzleUsingElementCount(psContext, psOperand->iNumComponents); } } break; } case OPERAND_TYPE_IMMEDIATE64: { if(psOperand->iNumComponents == 1) { bformata(glsl, "%f", psOperand->adImmediates[0]); } else { bformata(glsl, "dvec4(%f, %f, %f, %f)", psOperand->adImmediates[0], psOperand->adImmediates[1], psOperand->adImmediates[2], psOperand->adImmediates[3]); if(psOperand->iNumComponents != 4) { AddSwizzleUsingElementCount(psContext, psOperand->iNumComponents); } } break; } case OPERAND_TYPE_INPUT: { switch(psOperand->iIndexDims) { case INDEX_2D: { if(psOperand->aui32ArraySizes[1] == 0)//Input index zero - position. { bcatcstr(glsl, "gl_in"); TranslateOperandIndex(psContext, psOperand, TO_FLAG_NONE);//Vertex index bcatcstr(glsl, ".gl_Position"); } else { const char* name = "Input"; if(ui32TOFlag & TO_FLAG_DECLARATION_NAME) { name = GetDeclaredInputName(psContext, psContext->psShader->eShaderType, psOperand); } bformata(glsl, "%s%d", name, psOperand->aui32ArraySizes[1]); TranslateOperandIndex(psContext, psOperand, TO_FLAG_NONE);//Vertex index } break; } default: { if(psOperand->eIndexRep[0] == OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE) { bformata(glsl, "Input%d[int(", psOperand->ui32RegisterNumber); TranslateOperand(psContext, psOperand->psSubOperand[0], TO_FLAG_NONE); bcatcstr(glsl, ")]"); } else { if(psContext->psShader->aIndexedInput[psOperand->ui32RegisterNumber] != 0) { const uint32_t parentIndex = psContext->psShader->aIndexedInputParents[psOperand->ui32RegisterNumber]; bformata(glsl, "Input%d[%d]", parentIndex, psOperand->ui32RegisterNumber - parentIndex); } else { if(ui32TOFlag & TO_FLAG_DECLARATION_NAME) { const char* name = GetDeclaredInputName(psContext, psContext->psShader->eShaderType, psOperand); bcatcstr(glsl, name); } else { bformata(glsl, "Input%d", psOperand->ui32RegisterNumber); } } } break; } } break; } case OPERAND_TYPE_OUTPUT: { bformata(glsl, "Output%d", psOperand->ui32RegisterNumber); if(psOperand->psSubOperand[0]) { bcatcstr(glsl, "[int("); //Indexes must be integral. TranslateOperand(psContext, psOperand->psSubOperand[0], TO_FLAG_NONE); bcatcstr(glsl, ")]"); } break; } case OPERAND_TYPE_OUTPUT_DEPTH: case OPERAND_TYPE_OUTPUT_DEPTH_GREATER_EQUAL: case OPERAND_TYPE_OUTPUT_DEPTH_LESS_EQUAL: { bcatcstr(glsl, "gl_FragDepth"); break; } case OPERAND_TYPE_TEMP: { SHADER_VARIABLE_TYPE eType = GetOperandDataType(psContext, psOperand); bcatcstr(glsl, "Temp"); if(eType == SVT_INT) { bcatcstr(glsl, "_int"); } else if(eType == SVT_UINT) { bcatcstr(glsl, "_uint"); } else if(eType == SVT_DOUBLE) { bcatcstr(glsl, "_double"); } else if(eType == SVT_VOID || (ui32TOFlag & TO_FLAG_DESTINATION)) { if(ui32TOFlag & TO_FLAG_INTEGER) { bcatcstr(glsl, "_int"); } else if(ui32TOFlag & TO_FLAG_UNSIGNED_INTEGER) { bcatcstr(glsl, "_uint"); } } bformata(glsl, "[%d]", psOperand->ui32RegisterNumber); break; } case OPERAND_TYPE_SPECIAL_IMMCONSTINT: { bformata(glsl, "IntImmConst%d", psOperand->ui32RegisterNumber); break; } case OPERAND_TYPE_SPECIAL_IMMCONST: { bformata(glsl, "ImmConst%d", psOperand->ui32RegisterNumber); break; } case OPERAND_TYPE_SPECIAL_OUTBASECOLOUR: { bcatcstr(glsl, "BaseColour"); break; } case OPERAND_TYPE_SPECIAL_OUTOFFSETCOLOUR: { bcatcstr(glsl, "OffsetColour"); break; } case OPERAND_TYPE_SPECIAL_POSITION: { bcatcstr(glsl, "gl_Position"); break; } case OPERAND_TYPE_SPECIAL_FOG: { bcatcstr(glsl, "Fog"); break; } case OPERAND_TYPE_SPECIAL_POINTSIZE: { bcatcstr(glsl, "gl_PointSize"); break; } case OPERAND_TYPE_SPECIAL_ADDRESS: { bcatcstr(glsl, "Address"); break; } case OPERAND_TYPE_SPECIAL_TEXCOORD: { bformata(glsl, "TexCoord%d", psOperand->ui32RegisterNumber); break; } case OPERAND_TYPE_CONSTANT_BUFFER: { const char* StageName = "VS"; ConstantBuffer* psCBuf = NULL; ShaderVarType* psVarType = NULL; int32_t index = -1; GetConstantBufferFromBindingPoint(RGROUP_CBUFFER, psOperand->aui32ArraySizes[0], &psContext->psShader->sInfo, &psCBuf); switch(psContext->psShader->eShaderType) { case PIXEL_SHADER: { StageName = "PS"; break; } case HULL_SHADER: { StageName = "HS"; break; } case DOMAIN_SHADER: { StageName = "DS"; break; } case GEOMETRY_SHADER: { StageName = "GS"; break; } case COMPUTE_SHADER: { StageName = "CS"; break; } default: { break; } } if(ui32TOFlag & TO_FLAG_DECLARATION_NAME) { pui32IgnoreSwizzle[0] = 1; } if((psContext->flags & HLSLCC_FLAG_UNIFORM_BUFFER_OBJECT)!=HLSLCC_FLAG_UNIFORM_BUFFER_OBJECT) { if(psCBuf) { //$Globals. if(psCBuf->Name[0] == '$') { bformata(glsl, "Globals%s", StageName); } else { bformata(glsl, "%s%s", psCBuf->Name, StageName); } if((ui32TOFlag & TO_FLAG_DECLARATION_NAME) != TO_FLAG_DECLARATION_NAME) { bcatcstr(glsl, "."); } } else { //bformata(glsl, "cb%d", psOperand->aui32ArraySizes[0]); } } if((ui32TOFlag & TO_FLAG_DECLARATION_NAME) != TO_FLAG_DECLARATION_NAME) { //Work out the variable name. Don't apply swizzle to that variable yet. int32_t rebase = 0; if(psCBuf) { GetShaderVarFromOffset(psOperand->aui32ArraySizes[1], psOperand->aui32Swizzle, psCBuf, &psVarType, &index, &rebase); bformata(glsl, "%s", psVarType->FullName); } else // We don't have a semantic for this variable, so try the raw dump appoach. { bformata(glsl, "cb%d.data", psOperand->aui32ArraySizes[0]);// index = psOperand->aui32ArraySizes[1]; } //Dx9 only? if(psOperand->psSubOperand[0] != NULL) { SHADER_VARIABLE_TYPE eType = GetOperandDataType(psContext, psOperand->psSubOperand[0]); if(eType != SVT_INT && eType != SVT_UINT) { bcatcstr(glsl, "[int("); //Indexes must be integral. TranslateOperand(psContext, psOperand->psSubOperand[0], TO_FLAG_NONE); bcatcstr(glsl, ")]"); } else { bcatcstr(glsl, "["); //Indexes must be integral. TranslateOperand(psContext, psOperand->psSubOperand[0], TO_FLAG_NONE); bcatcstr(glsl, "]"); } ASSERT(index == 0 || index == -1); } else if(index != -1 && psOperand->psSubOperand[1] != NULL) { //Array of matrices is treated as array of vec4s if(index != -1) { SHADER_VARIABLE_TYPE eType = GetOperandDataType(psContext, psOperand->psSubOperand[1]); if(eType != SVT_INT && eType != SVT_UINT) { bcatcstr(glsl, "[int("); TranslateOperand(psContext, psOperand->psSubOperand[1], TO_FLAG_NONE); bformata(glsl, ") + %d]", index); } else { bcatcstr(glsl, "["); TranslateOperand(psContext, psOperand->psSubOperand[1], TO_FLAG_NONE); bformata(glsl, " + %d]", index); } } } else if(index != -1) { bformata(glsl, "[%d]", index); } else if(psOperand->psSubOperand[1] != NULL) { SHADER_VARIABLE_TYPE eType = GetOperandDataType(psContext, psOperand->psSubOperand[1]); if(eType != SVT_INT && eType != SVT_UINT) { bcatcstr(glsl, "["); TranslateOperand(psContext, psOperand->psSubOperand[1], TO_FLAG_NONE); bcatcstr(glsl, "]"); } else { bcatcstr(glsl, "[int("); TranslateOperand(psContext, psOperand->psSubOperand[1], TO_FLAG_NONE); bcatcstr(glsl, ")]"); } } if(psVarType && psVarType->Class == SVC_VECTOR) { switch(rebase) { case 4: { if(psVarType->Columns == 2) { //.x(GLSL) is .y(HLSL). .y(GLSL) is .z(HLSL) bcatcstr(glsl, ".xxyx"); } else if(psVarType->Columns == 3) { //.x(GLSL) is .y(HLSL). .y(GLSL) is .z(HLSL) .z(GLSL) is .w(HLSL) bcatcstr(glsl, ".xxyz"); } break; } case 8: { if(psVarType->Columns == 2) { //.x(GLSL) is .z(HLSL). .y(GLSL) is .w(HLSL) bcatcstr(glsl, ".xxxy"); } break; } case 0: default: { //No rebase, but extend to vec4. if(psVarType->Columns == 2) { bcatcstr(glsl, ".xyxx"); } else if(psVarType->Columns == 3) { bcatcstr(glsl, ".xyzx"); } break; } } } if(psVarType && psVarType->Class == SVC_SCALAR) { *pui32IgnoreSwizzle = 1; } } break; } case OPERAND_TYPE_RESOURCE: { TextureName(psContext, psOperand->ui32RegisterNumber, 0); *pui32IgnoreSwizzle = 1; break; } case OPERAND_TYPE_SAMPLER: { bformata(glsl, "Sampler%d", psOperand->ui32RegisterNumber); *pui32IgnoreSwizzle = 1; break; } case OPERAND_TYPE_FUNCTION_BODY: { const uint32_t ui32FuncBody = psOperand->ui32RegisterNumber; const uint32_t ui32FuncTable = psContext->psShader->aui32FuncBodyToFuncTable[ui32FuncBody]; //const uint32_t ui32FuncPointer = psContext->psShader->aui32FuncTableToFuncPointer[ui32FuncTable]; const uint32_t ui32ClassType = psContext->psShader->sInfo.aui32TableIDToTypeID[ui32FuncTable]; const char* ClassTypeName = &psContext->psShader->sInfo.psClassTypes[ui32ClassType].Name[0]; const uint32_t ui32UniqueClassFuncIndex = psContext->psShader->ui32NextClassFuncName[ui32ClassType]++; bformata(glsl, "%s_Func%d", ClassTypeName, ui32UniqueClassFuncIndex); break; } case OPERAND_TYPE_INPUT_FORK_INSTANCE_ID: { bcatcstr(glsl, "forkInstanceID"); *pui32IgnoreSwizzle = 1; return; } case OPERAND_TYPE_IMMEDIATE_CONSTANT_BUFFER: { bcatcstr(glsl, "immediateConstBufferF"); if(psOperand->psSubOperand[0]) { bcatcstr(glsl, "(int("); //Indexes must be integral. TranslateOperand(psContext, psOperand->psSubOperand[0], TO_FLAG_NONE); bcatcstr(glsl, "))"); } break; } case OPERAND_TYPE_INPUT_DOMAIN_POINT: { bcatcstr(glsl, "gl_TessCoord"); break; } case OPERAND_TYPE_INPUT_CONTROL_POINT: { if(psOperand->aui32ArraySizes[1] == 0)//Input index zero - position. { bformata(glsl, "gl_in[%d].gl_Position", psOperand->aui32ArraySizes[0]); } else { bformata(glsl, "Input%d[%d]", psOperand->aui32ArraySizes[1], psOperand->aui32ArraySizes[0]); } break; } case OPERAND_TYPE_NULL: { // Null register, used to discard results of operations bcatcstr(glsl, "//null"); break; } case OPERAND_TYPE_OUTPUT_CONTROL_POINT_ID: { bcatcstr(glsl, "gl_InvocationID"); *pui32IgnoreSwizzle = 1; break; } case OPERAND_TYPE_OUTPUT_COVERAGE_MASK: { bcatcstr(glsl, "gl_SampleMask[0]"); *pui32IgnoreSwizzle = 1; break; } case OPERAND_TYPE_INPUT_COVERAGE_MASK: { bcatcstr(glsl, "gl_SampleMaskIn[0]"); //Skip swizzle on scalar types. *pui32IgnoreSwizzle = 1; break; } case OPERAND_TYPE_INPUT_THREAD_ID://SV_DispatchThreadID { bcatcstr(glsl, "gl_GlobalInvocationID"); break; } case OPERAND_TYPE_INPUT_THREAD_GROUP_ID://SV_GroupThreadID { bcatcstr(glsl, "gl_LocalInvocationID"); break; } case OPERAND_TYPE_INPUT_THREAD_ID_IN_GROUP://SV_GroupID { bcatcstr(glsl, "gl_WorkGroupID"); break; } case OPERAND_TYPE_INPUT_THREAD_ID_IN_GROUP_FLATTENED://SV_GroupIndex { bcatcstr(glsl, "gl_LocalInvocationIndex"); break; } case OPERAND_TYPE_UNORDERED_ACCESS_VIEW: { bformata(glsl, "UAV%d", psOperand->ui32RegisterNumber); break; } case OPERAND_TYPE_THREAD_GROUP_SHARED_MEMORY: { bformata(glsl, "TGSM%d", psOperand->ui32RegisterNumber); break; } case OPERAND_TYPE_INPUT_PRIMITIVEID: { bcatcstr(glsl, "gl_PrimitiveID"); break; } case OPERAND_TYPE_INDEXABLE_TEMP: { bformata(glsl, "TempArray%d", psOperand->aui32ArraySizes[0]); bformata(glsl, "[%d", psOperand->aui32ArraySizes[1]); if(psOperand->psSubOperand[1]) { bcatcstr(glsl, "+"); TranslateOperand(psContext, psOperand->psSubOperand[1], TO_FLAG_NONE); } bcatcstr(glsl, "]"); break; } case OPERAND_TYPE_STREAM: { bformata(glsl, "%d", psOperand->ui32RegisterNumber); break; } case OPERAND_TYPE_INPUT_GS_INSTANCE_ID: { bcatcstr(glsl, "gl_InvocationID"); break; } case OPERAND_TYPE_THIS_POINTER: { /* The "this" register is a register that provides up to 4 pieces of information: X: Which CB holds the instance data Y: Base element offset of the instance data within the instance CB Z: Base sampler index W: Base Texture index Can be different for each function call */ break; } default: { ASSERT(0); break; } } if(integerConstructor) { bcatcstr(glsl, ")"); } }
void TranslateOperandSwizzle(HLSLCrossCompilerContext* psContext, const Operand* psOperand) { bstring glsl = *psContext->currentGLSLString; if(psOperand->eType == OPERAND_TYPE_INPUT) { if(psContext->psShader->abScalarInput[psOperand->ui32RegisterNumber]) { return; } } if(psOperand->eType == OPERAND_TYPE_CONSTANT_BUFFER) { /*ConstantBuffer* psCBuf = NULL; ShaderVar* psVar = NULL; int32_t index = -1; GetConstantBufferFromBindingPoint(psOperand->aui32ArraySizes[0], &psContext->psShader->sInfo, &psCBuf); //Access the Nth vec4 (N=psOperand->aui32ArraySizes[1]) //then apply the sizzle. GetShaderVarFromOffset(psOperand->aui32ArraySizes[1], psOperand->aui32Swizzle, psCBuf, &psVar, &index); bformata(glsl, ".%s", psVar->Name); if(index != -1) { bformata(glsl, "[%d]", index); }*/ //return; } if(psOperand->iWriteMaskEnabled && psOperand->iNumComponents == 4) { //Comonent Mask if(psOperand->eSelMode == OPERAND_4_COMPONENT_MASK_MODE) { if(psOperand->ui32CompMask != 0 && psOperand->ui32CompMask != (OPERAND_4_COMPONENT_MASK_X|OPERAND_4_COMPONENT_MASK_Y|OPERAND_4_COMPONENT_MASK_Z|OPERAND_4_COMPONENT_MASK_W)) { bcatcstr(glsl, "."); if(psOperand->ui32CompMask & OPERAND_4_COMPONENT_MASK_X) { bcatcstr(glsl, "x"); } if(psOperand->ui32CompMask & OPERAND_4_COMPONENT_MASK_Y) { bcatcstr(glsl, "y"); } if(psOperand->ui32CompMask & OPERAND_4_COMPONENT_MASK_Z) { bcatcstr(glsl, "z"); } if(psOperand->ui32CompMask & OPERAND_4_COMPONENT_MASK_W) { bcatcstr(glsl, "w"); } } } else //Component Swizzle if(psOperand->eSelMode == OPERAND_4_COMPONENT_SWIZZLE_MODE) { if(psOperand->ui32Swizzle != (NO_SWIZZLE)) { uint32_t i; bcatcstr(glsl, "."); for(i=0; i< 4; ++i) { if(psOperand->aui32Swizzle[i] == OPERAND_4_COMPONENT_X) { bcatcstr(glsl, "x"); } else if(psOperand->aui32Swizzle[i] == OPERAND_4_COMPONENT_Y) { bcatcstr(glsl, "y"); } else if(psOperand->aui32Swizzle[i] == OPERAND_4_COMPONENT_Z) { bcatcstr(glsl, "z"); } else if(psOperand->aui32Swizzle[i] == OPERAND_4_COMPONENT_W) { bcatcstr(glsl, "w"); } } } } else if(psOperand->eSelMode == OPERAND_4_COMPONENT_SELECT_1_MODE) { bcatcstr(glsl, "."); if(psOperand->aui32Swizzle[0] == OPERAND_4_COMPONENT_X) { bcatcstr(glsl, "x"); } else if(psOperand->aui32Swizzle[0] == OPERAND_4_COMPONENT_Y) { bcatcstr(glsl, "y"); } else if(psOperand->aui32Swizzle[0] == OPERAND_4_COMPONENT_Z) { bcatcstr(glsl, "z"); } else if(psOperand->aui32Swizzle[0] == OPERAND_4_COMPONENT_W) { bcatcstr(glsl, "w"); } } //Component Select 1 } }
void TranslateOperand(HLSLCrossCompilerContext* psContext, const Operand* psOperand, uint32_t ui32TOFlag) { bstring glsl = *psContext->currentGLSLString; uint32_t ui32IgnoreSwizzle = 0; if(ui32TOFlag & TO_FLAG_NAME_ONLY) { TranslateVariableName(psContext, psOperand, ui32TOFlag, &ui32IgnoreSwizzle); return; } switch(psOperand->eModifier) { case OPERAND_MODIFIER_NONE: { break; } case OPERAND_MODIFIER_NEG: { bcatcstr(glsl, "-"); break; } case OPERAND_MODIFIER_ABS: { bcatcstr(glsl, "abs("); break; } case OPERAND_MODIFIER_ABSNEG: { bcatcstr(glsl, "-abs("); break; } } TranslateVariableName(psContext, psOperand, ui32TOFlag, &ui32IgnoreSwizzle); if(!ui32IgnoreSwizzle) { TranslateOperandSwizzle(psContext, psOperand); } switch(psOperand->eModifier) { case OPERAND_MODIFIER_NONE: { break; } case OPERAND_MODIFIER_NEG: { break; } case OPERAND_MODIFIER_ABS: { bcatcstr(glsl, ")"); break; } case OPERAND_MODIFIER_ABSNEG: { bcatcstr(glsl, ")"); break; } } }
static int redir_handle_url(struct redir_t *redir, struct redir_conn_t *conn, struct redir_httpreq_t *httpreq, struct redir_socket_t *socket, struct sockaddr_in *peer, redir_request *req) { int port = 80; int matches = 1; int i = -1; char *p = 0; #ifdef ENABLE_REDIRINJECT char hasInject = 0; if (conn->s_params.flags & UAM_INJECT_URL) { safe_strncpy((char *) req->inject_url, (char *) conn->s_params.url, REDIRINJECT_MAX); hasInject = 1; } else if (_options.inject && *_options.inject) { safe_strncpy((char *) req->inject_url, (char *) _options.inject, REDIRINJECT_MAX); hasInject = 1; } else { #endif for (i=0; i < MAX_REGEX_PASS_THROUGHS; i++) { if ( ! _options.regex_pass_throughs[i].inuse ) break; /* if ( ! _options.regex_pass_throughs[i].regex_host[0] && ! _options.regex_pass_throughs[i].regex_path[0] && ! _options.regex_pass_throughs[i].regex_qs[0] ) break; */ #if(_debug_) log_dbg("REGEX host=[%s] path=[%s] qs=[%s]", _options.regex_pass_throughs[i].regex_host, _options.regex_pass_throughs[i].regex_path, _options.regex_pass_throughs[i].regex_qs); log_dbg("Host %s", httpreq->host); #endif if (_options.regex_pass_throughs[i].regex_host[0]) { switch(check_regex(&_options.regex_pass_throughs[i].re_host, _options.regex_pass_throughs[i].regex_host, httpreq->host)) { case -1: return -1; case 1: matches = _options.regex_pass_throughs[i].neg_host; break; case 0: matches = !_options.regex_pass_throughs[i].neg_host; break; } } if (matches && _options.regex_pass_throughs[i].regex_path[0]) { switch(check_regex(&_options.regex_pass_throughs[i].re_path, _options.regex_pass_throughs[i].regex_path, httpreq->path)) { case -1: return -1; case 1: matches = _options.regex_pass_throughs[i].neg_path; break; case 0: matches = !_options.regex_pass_throughs[i].neg_path; break; } } if (matches && _options.regex_pass_throughs[i].regex_qs[0]) { switch(check_regex(&_options.regex_pass_throughs[i].re_qs, _options.regex_pass_throughs[i].regex_qs, httpreq->qs)) { case -1: return -1; case 1: matches = _options.regex_pass_throughs[i].neg_qs; break; case 0: matches = !_options.regex_pass_throughs[i].neg_qs; break; } } if (matches) break; } #ifdef ENABLE_REDIRINJECT } #endif if (i == 0) matches = 0; if (matches) { log_dbg("Matched for Host %s", httpreq->host); req->proxy = 1; #ifdef ENABLE_REDIRINJECT /* XXX */ /* Check for headers we wish to filter out */ if (hasInject) { bstring newhdr = bfromcstr(""); char *hdr = (char *)req->wbuf->data; if (_options.inject_wispr) (void) inject_fmt(req, conn); while (hdr && *hdr) { char *p = strstr(hdr, "\r\n"); int skip = 0; int l; if (p) { l = (p - hdr); } else { l = req->wbuf->slen - (hdr - (char*)req->wbuf->data); } if (!strncasecmp(hdr, "accept-encoding:", 16)) { bcatcstr(newhdr, "Accept-Encoding: identity\r\n"); skip = 1; } else if (!strncasecmp(hdr, "connection:", 11)) { bcatcstr(newhdr, "Connection: close\r\n"); skip = 1; } else if (!strncasecmp(hdr, "keep-alive:", 11)) { skip = 1; } if (!skip) bcatblk(newhdr, hdr, l); if (p) { if (!skip) bcatblk(newhdr, p, 2); hdr = p + 2; } else { hdr = 0; } } if (req->wbuf->slen != newhdr->slen) { log_dbg("Changed HTTP Headers"); } bassign(req->wbuf, newhdr); bdestroy(newhdr); } /* XXX */ #endif if ((p = strchr(httpreq->host, ':'))) { *p++ = 0; port = atoi(p); } if (conn_setup(&req->conn, httpreq->host, port, req->wbuf, req->dbuf)) { log_err(errno, "conn_setup()"); return -1; } req->state |= REDIR_CONN_FD; net_select_addfd(&sctx, req->conn.sock, SELECT_READ); return 0; } return 1; }
void AddVersionDependentCode(HLSLCrossCompilerContext* psContext) { bstring glsl = *psContext->currentGLSLString; if(psContext->psShader->ui32MajorVersion <= 3) { bcatcstr(glsl, "int RepCounter;\n"); if(psContext->psShader->eShaderType == VERTEX_SHADER) { uint32_t texCoord; bcatcstr(glsl, "ivec4 Address;\n"); if(InOutSupported(psContext->psShader->eTargetLanguage)) { bcatcstr(glsl, "out vec4 OffsetColour;\n"); bcatcstr(glsl, "out vec4 BaseColour;\n"); bcatcstr(glsl, "out vec4 Fog;\n"); for(texCoord=0; texCoord<8; ++texCoord) { bformata(glsl, "out vec4 TexCoord%d;\n", texCoord); } } else { bcatcstr(glsl, "varying vec4 OffsetColour;\n"); bcatcstr(glsl, "varying vec4 BaseColour;\n"); bcatcstr(glsl, "varying vec4 Fog;\n"); for(texCoord=0; texCoord<8; ++texCoord) { bformata(glsl, "varying vec4 TexCoord%d;\n", texCoord); } } } else { uint32_t renderTargets, texCoord; bcatcstr(glsl, "varying vec4 OffsetColour;\n"); bcatcstr(glsl, "varying vec4 BaseColour;\n"); bcatcstr(glsl, "varying vec4 Fog;\n"); for(texCoord=0; texCoord<8; ++texCoord) { bformata(glsl, "varying vec4 TexCoord%d;\n", texCoord); } for(renderTargets=0; renderTargets<8; ++renderTargets) { bformata(glsl, "#define Output%d gl_FragData[%d]\n", renderTargets, renderTargets); } } } if(!HaveCompute(psContext->psShader->eTargetLanguage)) { if(psContext->psShader->eShaderType == COMPUTE_SHADER) { bcatcstr(glsl,"#extension GL_ARB_compute_shader : enable\n"); bcatcstr(glsl,"#extension GL_ARB_shader_storage_buffer_object : enable\n"); } } if (!HaveAtomicMem(psContext->psShader->eTargetLanguage) || !HaveAtomicCounter(psContext->psShader->eTargetLanguage)) { if( psContext->psShader->aiOpcodeUsed[OPCODE_IMM_ATOMIC_ALLOC] || psContext->psShader->aiOpcodeUsed[OPCODE_IMM_ATOMIC_CONSUME] || psContext->psShader->aiOpcodeUsed[OPCODE_DCL_UNORDERED_ACCESS_VIEW_STRUCTURED]) { bcatcstr(glsl,"#extension GL_ARB_shader_atomic_counters : enable\n"); bcatcstr(glsl,"#extension GL_ARB_shader_storage_buffer_object : enable\n"); } } if(!HaveGather(psContext->psShader->eTargetLanguage)) { if(psContext->psShader->aiOpcodeUsed[OPCODE_GATHER4] || psContext->psShader->aiOpcodeUsed[OPCODE_GATHER4_PO_C] || psContext->psShader->aiOpcodeUsed[OPCODE_GATHER4_PO] || psContext->psShader->aiOpcodeUsed[OPCODE_GATHER4_C]) { bcatcstr(glsl,"#extension GL_ARB_texture_gather : enable\n"); } } if(!HaveGatherNonConstOffset(psContext->psShader->eTargetLanguage)) { if(psContext->psShader->aiOpcodeUsed[OPCODE_GATHER4_PO_C] || psContext->psShader->aiOpcodeUsed[OPCODE_GATHER4_PO]) { bcatcstr(glsl,"#extension GL_ARB_gpu_shader5 : enable\n"); } } if(!HaveQueryLod(psContext->psShader->eTargetLanguage)) { if(psContext->psShader->aiOpcodeUsed[OPCODE_LOD]) { bcatcstr(glsl,"#extension GL_ARB_texture_query_lod : enable\n"); } } if(!HaveQueryLevels(psContext->psShader->eTargetLanguage)) { if(psContext->psShader->aiOpcodeUsed[OPCODE_RESINFO]) { bcatcstr(glsl,"#extension GL_ARB_texture_query_levels : enable\n"); } } if(!HaveImageLoadStore(psContext->psShader->eTargetLanguage)) { if(psContext->psShader->aiOpcodeUsed[OPCODE_STORE_UAV_TYPED] || psContext->psShader->aiOpcodeUsed[OPCODE_STORE_RAW] || psContext->psShader->aiOpcodeUsed[OPCODE_STORE_STRUCTURED]) { bcatcstr(glsl,"#extension GL_ARB_shader_image_load_store : enable\n"); bcatcstr(glsl,"#extension GL_ARB_shader_bit_encoding : enable\n"); } else if(psContext->psShader->aiOpcodeUsed[OPCODE_LD_UAV_TYPED] || psContext->psShader->aiOpcodeUsed[OPCODE_LD_RAW] || psContext->psShader->aiOpcodeUsed[OPCODE_LD_STRUCTURED]) { bcatcstr(glsl,"#extension GL_ARB_shader_image_load_store : enable\n"); } } if((psContext->flags & HLSLCC_FLAG_ORIGIN_UPPER_LEFT) && (psContext->psShader->eTargetLanguage >= LANG_150)) { bcatcstr(glsl,"layout(origin_upper_left) in vec4 gl_FragCoord;\n"); } if((psContext->flags & HLSLCC_FLAG_PIXEL_CENTER_INTEGER) && (psContext->psShader->eTargetLanguage >= LANG_150)) { bcatcstr(glsl,"layout(pixel_center_integer) in vec4 gl_FragCoord;\n"); } /* For versions which do not support a vec1 (currently all versions) */ bcatcstr(glsl,"struct vec1 {\n"); bcatcstr(glsl,"\tfloat x;\n"); bcatcstr(glsl,"};\n"); if(HaveUVec(psContext->psShader->eTargetLanguage)) { bcatcstr(glsl,"struct uvec1 {\n"); bcatcstr(glsl,"\tuint x;\n"); bcatcstr(glsl,"};\n"); } bcatcstr(glsl,"struct ivec1 {\n"); bcatcstr(glsl,"\tint x;\n"); bcatcstr(glsl,"};\n"); /* OpenGL 4.1 API spec: To use any built-in input or output in the gl_PerVertex block in separable program objects, shader code must redeclare that block prior to use. */ if(psContext->psShader->eShaderType == VERTEX_SHADER && psContext->psShader->eTargetLanguage >= LANG_410) { bcatcstr(glsl, "out gl_PerVertex {\n"); bcatcstr(glsl, "vec4 gl_Position;\n"); bcatcstr(glsl, "float gl_PointSize;\n"); bcatcstr(glsl, "float gl_ClipDistance[];"); bcatcstr(glsl, "};\n"); } //The fragment language has no default precision qualifier for floating point types. if(psContext->psShader->eShaderType == PIXEL_SHADER && psContext->psShader->eTargetLanguage == LANG_ES_100 || psContext->psShader->eTargetLanguage == LANG_ES_300 ) { bcatcstr(glsl,"precision highp float;\n"); } /* There is no default precision qualifier for the following sampler types in either the vertex or fragment language: */ if(psContext->psShader->eTargetLanguage == LANG_ES_300 ) { bcatcstr(glsl,"precision lowp sampler3D;\n"); bcatcstr(glsl,"precision lowp samplerCubeShadow;\n"); bcatcstr(glsl,"precision lowp sampler2DShadow;\n"); bcatcstr(glsl,"precision lowp sampler2DArray;\n"); bcatcstr(glsl,"precision lowp sampler2DArrayShadow;\n"); bcatcstr(glsl,"precision lowp isampler2D;\n"); bcatcstr(glsl,"precision lowp isampler3D;\n"); bcatcstr(glsl,"precision lowp isamplerCube;\n"); bcatcstr(glsl,"precision lowp isampler2DArray;\n"); bcatcstr(glsl,"precision lowp usampler2D;\n"); bcatcstr(glsl,"precision lowp usampler3D;\n"); bcatcstr(glsl,"precision lowp usamplerCube;\n"); bcatcstr(glsl,"precision lowp usampler2DArray;\n"); } if(SubroutinesSupported(psContext->psShader->eTargetLanguage)) { bcatcstr(glsl, "subroutine void SubroutineType();\n"); } }
void TranslateToGLSL(HLSLCrossCompilerContext* psContext, GLLang* planguage,const GlExtensions *extensions) { bstring glsl; uint32_t i; Shader* psShader = psContext->psShader; GLLang language = *planguage; const uint32_t ui32InstCount = psShader->ui32InstCount; const uint32_t ui32DeclCount = psShader->ui32DeclCount; psContext->indent = 0; if(language == LANG_DEFAULT) { language = ChooseLanguage(psShader); *planguage = language; } glsl = bfromcstralloc (1024, GetVersionString(language)); psContext->glsl = glsl; psContext->earlyMain = bfromcstralloc (1024, ""); for(i=0; i<NUM_PHASES;++i) { psContext->postShaderCode[i] = bfromcstralloc (1024, ""); } psContext->currentGLSLString = &glsl; psShader->eTargetLanguage = language; psShader->extensions = (const struct GlExtensions*)extensions; psContext->currentPhase = MAIN_PHASE; if(extensions) { if(extensions->ARB_explicit_attrib_location) bcatcstr(glsl,"#extension GL_ARB_explicit_attrib_location : require\n"); if(extensions->ARB_explicit_uniform_location) bcatcstr(glsl,"#extension GL_ARB_explicit_uniform_location : require\n"); if(extensions->ARB_shading_language_420pack) bcatcstr(glsl,"#extension GL_ARB_shading_language_420pack : require\n"); } ClearDependencyData(psShader->eShaderType, psContext->psDependencies); AddVersionDependentCode(psContext); if(psContext->flags & HLSLCC_FLAG_UNIFORM_BUFFER_OBJECT) { bcatcstr(glsl, "layout(std140) uniform;\n"); } //Special case. Can have multiple phases. if(psShader->eShaderType == HULL_SHADER) { int haveInstancedForkPhase = 0; uint32_t forkIndex = 0; ConsolidateHullTempVars(psShader); for(i=0; i < psShader->ui32HSDeclCount; ++i) { TranslateDeclaration(psContext, psShader->psHSDecl+i); } //control psContext->currentPhase = HS_CTRL_POINT_PHASE; if(psShader->ui32HSControlPointDeclCount) { bcatcstr(glsl, "//Control point phase declarations\n"); for(i=0; i < psShader->ui32HSControlPointDeclCount; ++i) { TranslateDeclaration(psContext, psShader->psHSControlPointPhaseDecl+i); } } if(psShader->ui32HSControlPointInstrCount) { SetDataTypes(psContext, psShader->psHSControlPointPhaseInstr, psShader->ui32HSControlPointInstrCount); bcatcstr(glsl, "void control_point_phase()\n{\n"); psContext->indent++; for(i=0; i < psShader->ui32HSControlPointInstrCount; ++i) { TranslateInstruction(psContext, psShader->psHSControlPointPhaseInstr+i); } psContext->indent--; bcatcstr(glsl, "}\n"); } //fork psContext->currentPhase = HS_FORK_PHASE; for(forkIndex = 0; forkIndex < psShader->ui32ForkPhaseCount; ++forkIndex) { bcatcstr(glsl, "//Fork phase declarations\n"); for(i=0; i < psShader->aui32HSForkDeclCount[forkIndex]; ++i) { TranslateDeclaration(psContext, psShader->apsHSForkPhaseDecl[forkIndex]+i); if(psShader->apsHSForkPhaseDecl[forkIndex][i].eOpcode == OPCODE_DCL_HS_FORK_PHASE_INSTANCE_COUNT) { haveInstancedForkPhase = 1; } } bformata(glsl, "void fork_phase%d()\n{\n", forkIndex); psContext->indent++; SetDataTypes(psContext, psShader->apsHSForkPhaseInstr[forkIndex], psShader->aui32HSForkInstrCount[forkIndex]-1); if(haveInstancedForkPhase) { AddIndentation(psContext); bformata(glsl, "for(int forkInstanceID = 0; forkInstanceID < HullPhase%dInstanceCount; ++forkInstanceID) {\n", forkIndex); psContext->indent++; } //The minus one here is remove the return statement at end of phases. //This is needed otherwise the for loop will only run once. ASSERT(psShader->apsHSForkPhaseInstr[forkIndex][psShader->aui32HSForkInstrCount[forkIndex]-1].eOpcode == OPCODE_RET); for(i=0; i < psShader->aui32HSForkInstrCount[forkIndex]-1; ++i) { TranslateInstruction(psContext, psShader->apsHSForkPhaseInstr[forkIndex]+i); } if(haveInstancedForkPhase) { psContext->indent--; AddIndentation(psContext); bcatcstr(glsl, "}\n"); if(psContext->havePostShaderCode[psContext->currentPhase]) { #ifdef _DEBUG AddIndentation(psContext); bcatcstr(glsl, "//--- Post shader code ---\n"); #endif bconcat(glsl, psContext->postShaderCode[psContext->currentPhase]); #ifdef _DEBUG AddIndentation(psContext); bcatcstr(glsl, "//--- End post shader code ---\n"); #endif } } psContext->indent--; bcatcstr(glsl, "}\n"); } //join psContext->currentPhase = HS_JOIN_PHASE; if(psShader->ui32HSJoinDeclCount) { bcatcstr(glsl, "//Join phase declarations\n"); for(i=0; i < psShader->ui32HSJoinDeclCount; ++i) { TranslateDeclaration(psContext, psShader->psHSJoinPhaseDecl+i); } } if(psShader->ui32HSJoinInstrCount) { SetDataTypes(psContext, psShader->psHSJoinPhaseInstr, psShader->ui32HSJoinInstrCount); bcatcstr(glsl, "void join_phase()\n{\n"); psContext->indent++; for(i=0; i < psShader->ui32HSJoinInstrCount; ++i) { TranslateInstruction(psContext, psShader->psHSJoinPhaseInstr+i); } psContext->indent--; bcatcstr(glsl, "}\n"); } bcatcstr(glsl, "void main()\n{\n"); psContext->indent++; #ifdef _DEBUG AddIndentation(psContext); bcatcstr(glsl, "//--- Start Early Main ---\n"); #endif bconcat(glsl, psContext->earlyMain); #ifdef _DEBUG AddIndentation(psContext); bcatcstr(glsl, "//--- End Early Main ---\n"); #endif if(psShader->ui32HSControlPointInstrCount) { AddIndentation(psContext); bcatcstr(glsl, "control_point_phase();\n"); if(psShader->ui32ForkPhaseCount || psShader->ui32HSJoinInstrCount) { AddIndentation(psContext); bcatcstr(glsl, "barrier();\n"); } } for(forkIndex = 0; forkIndex < psShader->ui32ForkPhaseCount; ++forkIndex) { AddIndentation(psContext); bformata(glsl, "fork_phase%d();\n", forkIndex); if(psShader->ui32HSJoinInstrCount || (forkIndex+1 < psShader->ui32ForkPhaseCount)) { AddIndentation(psContext); bcatcstr(glsl, "barrier();\n"); } } if(psShader->ui32HSJoinInstrCount) { AddIndentation(psContext); bcatcstr(glsl, "join_phase();\n"); } psContext->indent--; bcatcstr(glsl, "}\n"); if(psContext->psDependencies) { //Save partitioning and primitive type for use by domain shader. psContext->psDependencies->eTessOutPrim = psShader->sInfo.eTessOutPrim; psContext->psDependencies->eTessPartitioning = psShader->sInfo.eTessPartitioning; } return; } if(psShader->eShaderType == DOMAIN_SHADER && psContext->psDependencies) { //Load partitioning and primitive type from hull shader. switch(psContext->psDependencies->eTessOutPrim) { case TESSELLATOR_OUTPUT_TRIANGLE_CW: { bcatcstr(glsl, "layout(cw) in;\n"); break; } case TESSELLATOR_OUTPUT_POINT: { bcatcstr(glsl, "layout(point_mode) in;\n"); break; } default: { break; } } switch(psContext->psDependencies->eTessPartitioning) { case TESSELLATOR_PARTITIONING_FRACTIONAL_ODD: { bcatcstr(glsl, "layout(fractional_odd_spacing) in;\n"); break; } case TESSELLATOR_PARTITIONING_FRACTIONAL_EVEN: { bcatcstr(glsl, "layout(fractional_even_spacing) in;\n"); break; } default: { break; } } } for(i=0; i < ui32DeclCount; ++i) { TranslateDeclaration(psContext, psShader->psDecl+i); } bcatcstr(glsl, "void main()\n{\n"); psContext->indent++; #ifdef _DEBUG AddIndentation(psContext); bcatcstr(glsl, "//--- Start Early Main ---\n"); #endif bconcat(glsl, psContext->earlyMain); #ifdef _DEBUG AddIndentation(psContext); bcatcstr(glsl, "//--- End Early Main ---\n"); #endif MarkIntegerImmediates(psContext); SetDataTypes(psContext, psShader->psInst, ui32InstCount); for(i=0; i < ui32InstCount; ++i) { TranslateInstruction(psContext, psShader->psInst+i); } psContext->indent--; bcatcstr(glsl, "}\n"); }
void vm_hw_lua_init(vm_t* vm) { DIR* dir; struct dirent* entry; bstring name = NULL; bstring hwpath = NULL; bstring ext = bfromcstr(".lua"); // Work out the name of the hw directory. hwpath = osutil_getarg0path(); #ifdef _WIN32 bcatcstr(hwpath, "\\modules"); #else bcatcstr(hwpath, "/modules"); #endif // Attempt to open the hw directory. dir = opendir(hwpath->data); if (dir == NULL) { // The directory does not exist, so we don't load // any custom hardware. vm_lua_online = false; bdestroy(hwpath); bdestroy(ext); return; } // Load each file from the hw directory. while ((entry = readdir(dir)) != NULL) { name = bfromcstr(&entry->d_name[0]); // Check to see whether it is a lua file. if (binstr(name, blength(name) - 4, ext) == BSTR_ERR) { // Not a Lua file, skip over and // then continue. bdestroy(name); continue; } // Check to see if it is a normal file. #if defined(DT_REG) if (entry->d_type != DT_REG) #elif defined(DT_DIR) if (entry->d_type == DT_DIR) #elif defined(DT_UNKNOWN) if (entry->d_type == DT_UNKNOWN) #else #error Build system must support DT_REG, DT_DIR or DT_UNKNOWN in dirent.h. #endif { // Not a file, skip over and then // continue. bdestroy(name); continue; } // Attempt to load the Lua file. printf("loading Lua hardware from: %s\n", name->data); vm_hw_lua_load(vm, name); // Free data. bdestroy(name); } // Free resources. closedir(dir); /* hw_t keyboard; keyboard.id = 0x30CF7406; keyboard.version = 0x1802; keyboard.manufacturer = 0x1C6C8B36; keyboard.handler = &vm_hw_lua_interrupt; // Register hooks. vm_cycle_update = vm_hook_register(vm, &vm_hw_lua_cycle, HOOK_ON_CYCLE); vm_write_update = vm_hook_register(vm, &vm_hw_lua_write, HOOK_ON_WRITE); vm_hw_register(vm, keyboard);*/ }