+// #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);
+}
+
+// #502 string(string filename) whichpack (DP_QC_WHICHPACK)
+// returns the name of the pack containing a file, or "" if it is not in any pack (but local or non-existant)
+void VM_whichpack (void)
+{
+ const char *fn, *pack;
+
+ VM_SAFEPARMCOUNT(1, VM_whichpack);
+ fn = PRVM_G_STRING(OFS_PARM0);
+ pack = FS_WhichPack(fn);
+
+ PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(pack ? pack : "");
+}
+
+typedef struct
+{
+ int prognr;
+ double starttime;
+ float id;
+ char buffer[MAX_INPUTLINE];
+}
+uri_to_prog_t;
+
+static void uri_to_string_callback(int status, size_t length_received, unsigned char *buffer, void *cbdata)
+{
+ uri_to_prog_t *handle = (uri_to_prog_t *) cbdata;
+
+ if(!PRVM_ProgLoaded(handle->prognr))
+ {
+ // curl reply came too late... so just drop it
+ Z_Free(handle);
+ return;
+ }
+
+ PRVM_SetProg(handle->prognr);
+ PRVM_Begin;
+ if((prog->starttime == handle->starttime) && (prog->funcoffsets.URI_Get_Callback))
+ {
+ if(length_received >= sizeof(handle->buffer))
+ length_received = sizeof(handle->buffer) - 1;
+ handle->buffer[length_received] = 0;
+
+ PRVM_G_FLOAT(OFS_PARM0) = handle->id;
+ PRVM_G_FLOAT(OFS_PARM1) = status;
+ PRVM_G_INT(OFS_PARM2) = PRVM_SetTempString(handle->buffer);
+ PRVM_ExecuteProgram(prog->funcoffsets.URI_Get_Callback, "QC function URI_Get_Callback is missing");
+ }
+ PRVM_End;
+
+ Z_Free(handle);
+}
+
+// uri_get() gets content from an URL and calls a callback "uri_get_callback" with it set as string; an unique ID of the transfer is returned
+// returns 1 on success, and then calls the callback with the ID, 0 or the HTTP status code, and the received data in a string
+void VM_uri_get (void)
+{
+ const char *url;
+ float id;
+ qboolean ret;
+ uri_to_prog_t *handle;
+
+ if(!prog->funcoffsets.URI_Get_Callback)
+ PRVM_ERROR("uri_get called by %s without URI_Get_Callback defined", PRVM_NAME);
+
+ VM_SAFEPARMCOUNT(2, VM_uri_get);
+
+ url = PRVM_G_STRING(OFS_PARM0);
+ id = PRVM_G_FLOAT(OFS_PARM1);
+ handle = (uri_to_prog_t *) Z_Malloc(sizeof(*handle)); // this can't be the prog's mem pool, as curl may call the callback later!
+
+ handle->prognr = PRVM_GetProgNr();
+ handle->starttime = prog->starttime;
+ handle->id = id;
+ ret = Curl_Begin_ToMemory(url, (unsigned char *) handle->buffer, sizeof(handle->buffer), uri_to_string_callback, handle);
+ if(ret)
+ {
+ PRVM_G_INT(OFS_RETURN) = 1;
+ }
+ else
+ {
+ Z_Free(handle);
+ PRVM_G_INT(OFS_RETURN) = 0;
+ }
+}
+
+void VM_netaddress_resolve (void)
+{
+ const char *ip;
+ char normalized[128];
+ int port;
+ lhnetaddress_t addr;
+
+ VM_SAFEPARMCOUNTRANGE(1, 2, VM_netaddress_resolve);
+
+ ip = PRVM_G_STRING(OFS_PARM0);
+ port = 0;
+ if(prog->argc > 1)
+ port = (int) PRVM_G_FLOAT(OFS_PARM1);
+
+ if(LHNETADDRESS_FromString(&addr, ip, port) && LHNETADDRESS_ToString(&addr, normalized, sizeof(normalized), prog->argc > 1))
+ PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString(normalized);
+ else
+ PRVM_G_INT(OFS_RETURN) = PRVM_SetTempString("");
+}
+
+//string(void) getextresponse = #624; // returns the next extResponse packet that was sent to this client
+void VM_getextresponse (void)
+{
+ VM_SAFEPARMCOUNT(0,VM_argv);
+
+ if (net_extresponse_count <= 0)
+ PRVM_G_INT(OFS_RETURN) = OFS_NULL;
+ else
+ {
+ int first;
+ --net_extresponse_count;
+ first = (net_extresponse_last + NET_EXTRESPONSE_MAX - net_extresponse_count) % NET_EXTRESPONSE_MAX;
+ PRVM_G_INT(OFS_RETURN) = PRVM_SetEngineString(net_extresponse[first]);
+ }
+}
+
+/*
+=========
+VM_M_callfunction
+
+ callfunction(...,string function_name)
+Extension: pass
+=========
+*/
+mfunction_t *PRVM_ED_FindFunction (const char *name);
+void VM_callfunction(void)
+{
+ mfunction_t *func;
+ const char *s;
+
+ VM_SAFEPARMCOUNTRANGE(1, 8, VM_callfunction);
+
+ s = PRVM_G_STRING(OFS_PARM0+(prog->argc - 1)*3);
+
+ VM_CheckEmptyString(s);
+
+ func = PRVM_ED_FindFunction(s);
+
+ if(!func)
+ PRVM_ERROR("VM_callfunciton: function %s not found !", s);
+ else if (func->first_statement < 0)
+ {
+ // negative statements are built in functions
+ int builtinnumber = -func->first_statement;
+ prog->xfunction->builtinsprofile++;
+ if (builtinnumber < prog->numbuiltins && prog->builtins[builtinnumber])
+ prog->builtins[builtinnumber]();
+ else
+ PRVM_ERROR("No such builtin #%i in %s; most likely cause: outdated engine build. Try updating!", builtinnumber, PRVM_NAME);
+ }
+ else if(func - prog->functions > 0)
+ {
+ prog->argc--;
+ PRVM_ExecuteProgram(func - prog->functions,"");
+ prog->argc++;
+ }
+}
+
+/*
+=========
+VM_isfunction
+
+float isfunction(string function_name)
+=========
+*/
+mfunction_t *PRVM_ED_FindFunction (const char *name);
+void VM_isfunction(void)
+{
+ mfunction_t *func;
+ const char *s;
+
+ VM_SAFEPARMCOUNT(1, VM_isfunction);
+
+ s = PRVM_G_STRING(OFS_PARM0);
+
+ VM_CheckEmptyString(s);
+
+ func = PRVM_ED_FindFunction(s);
+
+ if(!func)
+ PRVM_G_FLOAT(OFS_RETURN) = false;
+ else
+ PRVM_G_FLOAT(OFS_RETURN) = true;
+}