aboutsummaryrefslogtreecommitdiff
path: root/Src/auth/decode.cpp
diff options
context:
space:
mode:
authorJef <jef@targetspot.com>2024-09-24 08:54:57 -0400
committerJef <jef@targetspot.com>2024-09-24 08:54:57 -0400
commit20d28e80a5c861a9d5f449ea911ab75b4f37ad0d (patch)
tree12f17f78986871dd2cfb0a56e5e93b545c1ae0d0 /Src/auth/decode.cpp
parent537bcbc86291b32fc04ae4133ce4d7cac8ebe9a7 (diff)
downloadwinamp-20d28e80a5c861a9d5f449ea911ab75b4f37ad0d.tar.gz
Initial community commit
Diffstat (limited to 'Src/auth/decode.cpp')
-rw-r--r--Src/auth/decode.cpp78
1 files changed, 78 insertions, 0 deletions
diff --git a/Src/auth/decode.cpp b/Src/auth/decode.cpp
new file mode 100644
index 00000000..691f8d43
--- /dev/null
+++ b/Src/auth/decode.cpp
@@ -0,0 +1,78 @@
+#include <stdio.h>
+#include <bfc/platform/types.h>
+#include <strsafe.h>
+
+static uint8_t quickhex(char c)
+{
+ int hexvalue = c;
+ if (hexvalue & 0x10)
+ hexvalue &= ~0x30;
+ else
+ {
+ hexvalue &= 0xF;
+ hexvalue += 9;
+ }
+ return hexvalue;
+}
+
+static uint8_t DecodeEscape(const char *&str)
+{
+ uint8_t a = quickhex(*++str);
+ uint8_t b = quickhex(*++str);
+ str++;
+ return a * 16 + b;
+}
+
+static void DecodeEscapedUTF8(char *&output, const char *&input)
+{
+ uint8_t utf8_data[1024] = {0}; // hopefully big enough!!
+ int num_utf8_words=0;
+ bool error=false;
+
+ while (input && *input && *input == '%' && num_utf8_words < sizeof(utf8_data))
+ {
+ if (isxdigit(input[1]) && isxdigit(input[2]))
+ {
+ utf8_data[num_utf8_words++]=DecodeEscape(input);
+ }
+ else if (input[1] == '%')
+ {
+ input+=2;
+ utf8_data[num_utf8_words++]='%';
+ }
+ else
+ {
+ error = true;
+ break;
+ }
+ }
+
+ memcpy(output, utf8_data, num_utf8_words);
+ output+=num_utf8_words;
+ //StringCchCopyExA(output, num_utf8_words, (char *)utf8_data, &output, 0, 0);
+
+ if (error)
+ {
+ *output++ = *input++;
+ }
+}
+
+// benski> We have the luxury of knowing that decoding will ALWAYS produce smaller strings
+// so we can do it in-place
+void UrlDecode(char *str)
+{
+ const char *itr = str;
+ while (itr && *itr)
+ {
+ switch (*itr)
+ {
+ case '%':
+ DecodeEscapedUTF8(str, itr);
+ break;
+ default:
+ *str++ = *itr++;
+ break;
+ }
+ }
+ *str = 0;
+} \ No newline at end of file