Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mRemoteNG/PuTTYNG.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'utils/percent_encode.c')
-rw-r--r--utils/percent_encode.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/utils/percent_encode.c b/utils/percent_encode.c
new file mode 100644
index 00000000..ea04b0ac
--- /dev/null
+++ b/utils/percent_encode.c
@@ -0,0 +1,34 @@
+/*
+ * %-encoding in URL style.
+ *
+ * Defaults to escaping % itself (necessary for decoding to even
+ * work), and any C0 escape character. Further bad characters can be
+ * provided in 'badchars'.
+ */
+
+#include "misc.h"
+
+void percent_encode_bs(BinarySink *bs, ptrlen data, const char *badchars)
+{
+ for (const char *p = data.ptr, *e = ptrlen_end(data); p < e; p++) {
+ char c = *p;
+ if (c == '%' || c < ' ' || (badchars && strchr(badchars, c)))
+ put_fmt(bs, "%%%02X", (unsigned char)c);
+ else
+ put_byte(bs, c);
+ }
+}
+
+void percent_encode_fp(FILE *fp, ptrlen data, const char *badchars)
+{
+ stdio_sink ss;
+ stdio_sink_init(&ss, fp);
+ percent_encode_bs(BinarySink_UPCAST(&ss), data, badchars);
+}
+
+strbuf *percent_encode_sb(ptrlen data, const char *badchars)
+{
+ strbuf *sb = strbuf_new();
+ percent_encode_bs(BinarySink_UPCAST(sb), data, badchars);
+ return sb;
+}