From: divverent Date: Wed, 27 Feb 2008 13:51:57 +0000 (+0000) Subject: DP_QC_URI_ESCAPE X-Git-Tag: xonotic-v0.1.0preview~2379 X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=commitdiff_plain;h=053e5e3f2137d880aaaa31ca8c5705c7edc15782;hp=21b8a56df6b442830f16d41d96d269c69b50cf58 DP_QC_URI_ESCAPE git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@8143 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/clvm_cmds.c b/clvm_cmds.c index cedb6e81..08bf6c8f 100644 --- a/clvm_cmds.c +++ b/clvm_cmds.c @@ -3359,10 +3359,30 @@ VM_gecko_resize, // #492 void gecko_resize( string name, float w, float h ) VM_gecko_get_texture_extent, // #493 vector gecko_get_texture_extent( string name ) VM_crc16, // #494 float(float caseinsensitive, string s, ...) crc16 = #494 (DP_QC_CRC16) VM_cvar_type, // #495 float(string name) cvar_type = #495; (DP_QC_CVAR_TYPE) -NULL, // #496 -NULL, // #497 +VM_uri_escape, // #496 string(string in) uri_escape = #496; +VM_uri_unescape, // #497 string(string in) uri_unescape = #497; NULL, // #498 NULL, // #499 +NULL, // #500 +NULL, // #501 +NULL, // #502 +NULL, // #503 +NULL, // #504 +NULL, // #505 +NULL, // #506 +NULL, // #507 +NULL, // #508 +NULL, // #509 +VM_uri_escape, // #510 string(string in) uri_escape = #510; +VM_uri_unescape, // #511 string(string in) uri_unescape = #511; +NULL, // #512 +NULL, // #513 +NULL, // #514 +NULL, // #515 +NULL, // #516 +NULL, // #517 +NULL, // #518 +NULL, // #519 }; const int vm_cl_numbuiltins = sizeof(vm_cl_builtins) / sizeof(prvm_builtin_t); diff --git a/mvm_cmds.c b/mvm_cmds.c index c92d5c27..05f50f9b 100644 --- a/mvm_cmds.c +++ b/mvm_cmds.c @@ -26,6 +26,7 @@ char *vm_m_extensions = "DP_QC_CRC16 " "FTE_STRINGS " "DP_QC_CVAR_TYPE " +"DP_QC_URI_ESCAPE " ; /* @@ -1297,8 +1298,8 @@ NULL, // #506 NULL, // #507 NULL, // #508 NULL, // #509 -NULL, // #510 -NULL, // #511 +VM_uri_escape, // #510 string(string in) uri_escape = #510; +VM_uri_unescape, // #511 string(string in) uri_unescape = #511; NULL, // #512 NULL, // #513 NULL, // #514 diff --git a/prvm_cmds.c b/prvm_cmds.c index 5a86819e..401c7f93 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -4580,3 +4580,83 @@ void VM_Cmd_Reset(void) // VM_BufStr_ShutDown(); } +// #510 string(string input, ...) uri_escape (DP_QC_URI_ESCAPE) +// does URI escaping on a string (replace evil stuff by %AB escapes) +void VM_uri_escape (void) +{ + char src[VM_STRINGTEMP_LENGTH]; + char dest[VM_STRINGTEMP_LENGTH]; + char *p, *q; + static const char *hex = "0123456789abcdef"; + + VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_escape); + VM_VarString(0, src, sizeof(src)); + + for(p = src, q = dest; *p && q < dest + sizeof(dest) - 3; ++p) + { + if((*p >= 'A' && *p <= 'Z') + || (*p >= 'a' && *p <= 'z') + || (*p >= '0' && *p <= '9') + || (*p == '-') || (*p == '_') || (*p == '.') + || (*p == '!') || (*p == '~') || (*p == '*') + || (*p == '\'') || (*p == '(') || (*p == ')')) + *q++ = *p; + else + { + *q++ = '%'; + *q++ = hex[(*(unsigned char *)p >> 4) & 0xF]; + *q++ = hex[ *(unsigned char *)p & 0xF]; + } + } + *q++ = 0; + + PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest); +} + +// #510 string(string input, ...) uri_unescape (DP_QC_URI_ESCAPE) +// does URI unescaping on a string (get back the evil stuff) +void VM_uri_unescape (void) +{ + char src[VM_STRINGTEMP_LENGTH]; + char dest[VM_STRINGTEMP_LENGTH]; + char *p, *q; + int hi, lo; + + VM_SAFEPARMCOUNTRANGE(1, 8, VM_uri_unescape); + VM_VarString(0, src, sizeof(src)); + + for(p = src, q = dest; *p; ) // no need to check size, because unescape can't expand + { + if(*p == '%') + { + if(p[1] >= '0' && p[1] <= '9') + hi = p[1] - '0'; + else if(p[1] >= 'a' && p[1] <= 'f') + hi = p[1] - 'a' + 10; + else if(p[1] >= 'A' && p[1] <= 'F') + hi = p[1] - 'A' + 10; + else + goto nohex; + if(p[2] >= '0' && p[2] <= '9') + lo = p[2] - '0'; + else if(p[2] >= 'a' && p[2] <= 'f') + lo = p[2] - 'a' + 10; + else if(p[2] >= 'A' && p[2] <= 'F') + lo = p[2] - 'A' + 10; + else + goto nohex; + if(hi != 0 || lo != 0) // don't unescape NUL bytes + *q++ = (char) (hi * 0x10 + lo); + p += 3; + continue; + } + +nohex: + // otherwise: + *q++ = *p++; + } + *q++ = 0; + + PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(dest); +} + diff --git a/prvm_cmds.h b/prvm_cmds.h index fdabb68d..b523e84f 100644 --- a/prvm_cmds.h +++ b/prvm_cmds.h @@ -412,3 +412,6 @@ void VM_SetTraceGlobals(const trace_t *trace); void VM_Cmd_Init(void); void VM_Cmd_Reset(void); + +void VM_uri_escape (void); +void VM_uri_unescape (void); diff --git a/svvm_cmds.c b/svvm_cmds.c index bd12a0a7..e2d1de3b 100644 --- a/svvm_cmds.c +++ b/svvm_cmds.c @@ -153,6 +153,7 @@ char *vm_sv_extensions = "DP_SV_SHUTDOWN " "DP_GECKO_SUPPORT " "DP_QC_GETSURFACEPOINTATTRIBUTE " +"DP_QC_URI_ESCAPE " ; /* @@ -3349,6 +3350,26 @@ NULL, // #496 NULL, // #497 NULL, // #498 NULL, // #499 +NULL, // #500 +NULL, // #501 +NULL, // #502 +NULL, // #503 +NULL, // #504 +NULL, // #505 +NULL, // #506 +NULL, // #507 +NULL, // #508 +NULL, // #509 +VM_uri_escape, // #510 string(string in) uri_escape = #510; +VM_uri_unescape, // #511 string(string in) uri_unescape = #511; +NULL, // #512 +NULL, // #513 +NULL, // #514 +NULL, // #515 +NULL, // #516 +NULL, // #517 +NULL, // #518 +NULL, // #519 }; const int vm_sv_numbuiltins = sizeof(vm_sv_builtins) / sizeof(prvm_builtin_t);