-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert-uri-escape.c.inl
166 lines (155 loc) · 5.4 KB
/
convert-uri-escape.c.inl
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Copyright (c) 2019-2025 Griefer@Work *
* *
* This software is provided 'as-is', without any express or implied *
* warranty. In no event will the authors be held liable for any damages *
* arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute it *
* freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must not *
* claim that you wrote the original software. If you use this software *
* in a product, an acknowledgement (see the following) in the product *
* documentation is required: *
* Portions Copyright (c) 2019-2025 Griefer@Work *
* 2. Altered source versions must be plainly marked as such, and must not be *
* misrepresented as being the original software. *
* 3. This notice may not be removed or altered from any source distribution. *
*/
#ifdef __INTELLISENSE__
#include "convert.c"
#endif /* __INTELLISENSE__ */
DECL_BEGIN
INTERN NONNULL((1, 2)) ssize_t FORMATPRINTER_CC
libiconv_uri_escape_encode(struct iconv_encode *__restrict self,
/*bytes*/ char const *__restrict data,
size_t size) {
ssize_t temp, result = 0;
char const *end, *flush_start;
flush_start = data;
end = data + size;
while (data < end) {
unsigned char ch;
ch = (unsigned char)*data;
/* Only the bytes associated with the following
* ASCI characters don't need to be escaped:
* A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
* a b c d e f g h i j k l m n o p q r s t u v w x y z
* 0 1 2 3 4 5 6 7 8 9 - _ . ~ */
switch (ch) {
case 'A' ... 'Z':
case 'a' ... 'z':
case '0' ... '9':
case '-':
case '_':
case '.':
case '~':
/* Don't need to escape this! */
break;
default: {
char esc[3];
DO_encode_output(flush_start, (size_t)(data - flush_start));
esc[0] = '%';
esc[1] = _itoa_upper_digits[(ch & 0xf0) >> 4];
esc[2] = _itoa_upper_digits[ch & 0xf];
DO_encode_output(esc, 3);
flush_start = data + 1;
} break;
}
++data;
}
DO_encode_output(flush_start, (size_t)(end - flush_start));
return result;
err:
return temp;
}
INTERN NONNULL((1, 2)) ssize_t FORMATPRINTER_CC
libiconv_uri_escape_decode(struct iconv_decode *__restrict self,
/*ascii*/ char const *__restrict data,
size_t size) {
ssize_t temp, result = 0;
char const *end, *flush_start;
flush_start = data;
end = data + size;
if unlikely(self->icd_flags & ICONV_HASERR)
goto err_ilseq;
while (data < end) {
unsigned char ch = (unsigned char)*data;
if unlikely(self->icd_data.idd_uri.ue_mode != _ICONV_DECODE_URI_TXT) {
uint8_t nibble;
if (ch >= '0' && ch <= '9') {
nibble = ch - '0';
} else if (ch >= 'A' && ch <= 'F') {
nibble = 10 + ch - 'A';
} else if (ch >= 'a' && ch <= 'f') {
nibble = 10 + ch - 'a';
} else {
if (IS_ICONV_ERR_ERROR_OR_ERRNO(self->icd_flags))
goto err_ilseq;
if (!IS_ICONV_ERR_DISCARD(self->icd_flags)) {
if (IS_ICONV_ERR_REPLACE(self->icd_flags))
ch = '?';
DO_decode_output((char const *)&ch, 1);
}
goto next_data;
}
if (self->icd_data.idd_uri.ue_mode == _ICONV_DECODE_URI_PCT) {
self->icd_data.idd_uri.ue_chr = nibble << 4;
self->icd_data.idd_uri.ue_mode = _ICONV_DECODE_URI_PCT_1;
} else {
nibble |= self->icd_data.idd_uri.ue_chr;
/* Output the fully decoded character. */
DO_decode_output((char const *)&nibble, 1);
self->icd_data.idd_uri.ue_mode = _ICONV_DECODE_URI_TXT;
flush_start = data + 1;
}
} else {
/* Only the bytes associated with the following
* ASCI characters don't need to be escaped:
* A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
* a b c d e f g h i j k l m n o p q r s t u v w x y z
* 0 1 2 3 4 5 6 7 8 9 - _ . ~ */
switch (ch) {
case 'A' ... 'Z':
case 'a' ... 'z':
case '0' ... '9':
case '-':
case '_':
case '.':
case '~':
/* Allowed as-is. */
break;
case '%':
/* Enter escape mode. */
DO_decode_output(flush_start, (size_t)(data - flush_start));
self->icd_data.idd_uri.ue_mode = _ICONV_DECODE_URI_PCT;
break;
default:
/* Illegal character! */
if (IS_ICONV_ERR_ERROR_OR_ERRNO(self->icd_flags))
goto err_ilseq;
if (!IS_ICONV_ERR_IGNORE(self->icd_flags)) {
DO_decode_output(flush_start, (size_t)(data - flush_start));
if (IS_ICONV_ERR_REPLACE(self->icd_flags))
DO_decode_output("?", 1);
flush_start = data + 1;
}
break;
}
}
next_data:
++data;
}
if likely(self->icd_data.idd_uri.ue_mode == _ICONV_DECODE_URI_TXT)
DO_decode_output(flush_start, (size_t)(end - flush_start));
return result;
err:
return temp;
err_ilseq:
self->icd_flags |= ICONV_HASERR;
if (IS_ICONV_ERR_ERRNO(self->icd_flags))
errno = EILSEQ;
return -(ssize_t)(size_t)(end - data);
}
DECL_END