blob: 691f8d43fa0397a1e41ad4b5d90d24db63331876 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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;
}
|