From 84da1a1050ee0f0cf5b2ecfec78291424b648c30 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 5 Aug 2005 00:31:06 +0000 Subject: r9082: added the ECMA functions encodeURIComponent() and decodeURIComponent(), which I am using as part of my object linearisation code (This used to be commit f91f705a143b24c2e01b9176850a7089e1a99b86) --- source4/scripting/ejs/smbcalls_string.c | 81 +++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) (limited to 'source4/scripting') diff --git a/source4/scripting/ejs/smbcalls_string.c b/source4/scripting/ejs/smbcalls_string.c index cd5db1f574..b4c85dbd28 100644 --- a/source4/scripting/ejs/smbcalls_string.c +++ b/source4/scripting/ejs/smbcalls_string.c @@ -336,6 +336,85 @@ static int ejs_vsprintf(MprVarHandle eid, int argc, struct MprVar **argv) return ret; } + +/* + encode a string, replacing all non-alpha with %02x form +*/ +static int ejs_encodeURIComponent(MprVarHandle eid, int argc, char **argv) +{ + int i, j, count=0; + const char *s; + char *ret; + if (argc != 1) { + ejsSetErrorMsg(eid, "encodeURIComponent invalid arguments"); + return -1; + } + + s = argv[0]; + + for (i=0;s[i];i++) { + if (!isalnum(s[i])) count++; + } + + ret = talloc_size(mprMemCtx(), i + count*2 + 1); + if (ret == NULL) { + return -1; + } + for (i=j=0;s[i];i++,j++) { + if (!isalnum(s[i])) { + snprintf(ret+j, 4, "%%%02X", (unsigned)s[i]); + j += 2; + } else { + ret[j] = s[i]; + } + } + ret[j] = 0; + mpr_Return(eid, mprString(ret)); + talloc_free(ret); + return 0; +} + +/* + encode a string, replacing all non-alpha of %02x form +*/ +static int ejs_decodeURIComponent(MprVarHandle eid, int argc, char **argv) +{ + int i, j, count=0; + const char *s; + char *ret; + if (argc != 1) { + ejsSetErrorMsg(eid, "decodeURIComponent invalid arguments"); + return -1; + } + + s = argv[0]; + + ret = talloc_size(mprMemCtx(), strlen(s) + 1); + if (ret == NULL) { + return -1; + } + + for (i=j=0;s[i];i++,j++) { + if (s[i] == '%') { + unsigned c; + if (sscanf(s+i+1, "%02X", &c) != 1) { + ejsSetErrorMsg(eid, "decodeURIComponent bad format"); + return -1; + } + ret[j] = c; + i += 2; + } else { + ret[j] = s[i]; + } + if (!isalnum(s[i])) count++; + } + + ret[j] = 0; + mpr_Return(eid, mprString(ret)); + talloc_free(ret); + return 0; +} + /* initialise string ejs subsystem */ @@ -351,6 +430,8 @@ static int ejs_string_init(MprVarHandle eid, int argc, struct MprVar **argv) mprSetCFunction(obj, "join", ejs_join); mprSetCFunction(obj, "sprintf", ejs_sprintf); mprSetCFunction(obj, "vsprintf", ejs_vsprintf); + mprSetStringCFunction(obj, "encodeURIComponent", ejs_encodeURIComponent); + mprSetStringCFunction(obj, "decodeURIComponent", ejs_decodeURIComponent); return 0; } -- cgit