src/lib/cheapcgi.c 1.126
1.126 2009/12/09 19:24:35 galt
added new func javaScriptLiteralEncode
Index: src/lib/cheapcgi.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/cheapcgi.c,v
retrieving revision 1.125
retrieving revision 1.126
diff -b -B -U 4 -r1.125 -r1.126
--- src/lib/cheapcgi.c 20 Oct 2009 22:50:44 -0000 1.125
+++ src/lib/cheapcgi.c 9 Dec 2009 19:24:35 -0000 1.126
@@ -632,8 +632,66 @@
cgiBadVar(varName);
return res;
}
+char *javaScriptLiteralEncode(char *inString)
+/* Use backslash escaping on newline *
+ * and quote chars, backslash and others. *
+ * Intended that the encoded string will be *
+ * put between quotes at a higher level and *
+ * then interpreted by Javascript. */
+{
+char c;
+int outSize = 0;
+char *outString, *out, *in;
+
+if (inString == NULL)
+ return(cloneString(""));
+
+/* Count up how long it will be */
+in = inString;
+while ((c = *in++) != 0)
+ {
+ if (c == '\''
+ || c == '\"'
+ || c == '&'
+ || c == '\\'
+ || c == '\n'
+ || c == '\r'
+ || c == '\t'
+ || c == '\b'
+ || c == '\f'
+ )
+ outSize += 2;
+ else
+ outSize += 1;
+ }
+outString = needMem(outSize+1);
+
+/* Encode string */
+in = inString;
+out = outString;
+while ((c = *in++) != 0)
+ {
+ if (c == '\''
+ || c == '\"'
+ || c == '&'
+ || c == '\\'
+ || c == '\n'
+ || c == '\r'
+ || c == '\t'
+ || c == '\b'
+ || c == '\f'
+ )
+ *out++ = '\\';
+ *out++ = c;
+ }
+*out++ = 0;
+return outString;
+
+}
+
+
void cgiDecode(char *in, char *out, int inLength)
/* Decode from cgi pluses-for-spaces format to normal.
* Out will be a little shorter than in typically, and
* can be the same buffer. */