forked from nevali/liburi
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathunicode.c
More file actions
339 lines (316 loc) · 7.01 KB
/
unicode.c
File metadata and controls
339 lines (316 loc) · 7.01 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* Author: Mo McRoberts <mo.mcroberts@bbc.co.uk>
*
* Copyright 2015-2017 BBC
*/
/*
* Copyright 2012 Mo McRoberts.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "p_liburi.h"
static size_t uri_wctoutf8_(int *dest, wchar_t ch);
static size_t uri_encode_8bit_(char *dest, unsigned char ch);
static size_t uri_encode_wide_(char *dest, wchar_t ch);
static size_t uri_widebytes_(const char *uristr, size_t nbytes);
static int uri_preprocess_(char *restrict buf, const char *restrict uristr, size_t nbytes);
static int uri_preprocess_utf8_(char *restrict buf, const unsigned char *restrict uristr, size_t nbytes);
/* Create a URI from a wide-character Unicode string */
URI *
uri_create_wstr(const wchar_t *restrict wstr, const URI *restrict base)
{
const wchar_t *t;
char *buf, *bp;
size_t l, needed;
URI *uri;
l = wcslen(wstr);
needed = l + 1;
for(t = wstr; *t; t++)
{
if(*t < 33)
{
l += 2;
}
else if(*t > 127)
{
/* Wide characters may use up to 6 bytes when UTF-8-encoded,
* each of which is individually percent-encoded (so
* consumes 3 bytes in the destination string, minus the
* one byte we've already accounted for
*/
l += 17;
}
}
buf = (char *) calloc(1, needed);
if(!buf)
{
return NULL;
}
bp = buf;
for(t = wstr; *t; t++)
{
if(*t < 31 || *t > 127)
{
bp += uri_encode_wide_(bp, *t);
}
else
{
*bp = (char) *t;
bp++;
}
}
*bp = 0;
uri = uri_create_ascii(buf, base);
free(buf);
return uri;
}
/* Create a URI from a UTF-8-encoded string; any non-ASCII characters
* will be percent-encoded
*/
URI *
uri_create_ustr(const unsigned char *restrict ustr, const URI *restrict base)
{
const unsigned char *t;
char *buf;
size_t l, needed;
URI *uri;
/* Determine the required buffer size, accounting for percent-encoding
* of non-printable and non-ASCII characters
*/
l = strlen((const char *) ustr);
needed = l + 1;
for(t = ustr; *t; t++)
{
if(*t < 33 || *t > 127)
{
needed += 2;
}
}
buf = (char *) calloc(1, needed);
if(!buf)
{
return NULL;
}
if(uri_preprocess_utf8_(buf, ustr, l))
{
free(buf);
return NULL;
}
uri = uri_create_ascii(buf, base);
free(buf);
return uri;
}
/* Create a URI from a string in the current locale */
URI *
uri_create_str(const char *restrict uristr, const URI *restrict base)
{
char *buf;
URI *uri;
size_t l, numwide;
/* XXX We should do this via mbstowcs() and then uri_create_wstr() */
l = strlen(uristr) + 1;
numwide = uri_widebytes_(uristr, l);
buf = (char *) malloc(l + numwide * 3);
if(!buf)
{
return NULL;
}
if(uri_preprocess_(buf, uristr, l))
{
free(buf);
return NULL;
}
uri = uri_create_ascii(buf, base);
free(buf);
return uri;
}
/* Encode ch as UTF-8, storing it in dest[0..3] and returning the number
* of octets stored. Because this is a convenience function used by
* uri_encode_wide_(), dest is an array of ints, rather than unsigned chars.
*/
static size_t
uri_wctoutf8_(int *dest, wchar_t ch)
{
if(ch < 0x7f)
{
dest[0] = ch;
return 1;
}
if(ch < 0x07ff)
{
/* 110aaaaa 10bbbbbb */
dest[0] = 0xc0 | ((ch & 0x0007c0) >> 6);
dest[1] = 0x80 | (ch & 0x00003f);
return 2;
}
if(ch < 0xffff)
{
/* 1110aaaa 10bbbbbb 10cccccc */
dest[0] = 0xe0 | ((ch & 0x00f000) >> 12);
dest[1] = 0x80 | ((ch & 0x000fc0) >> 6);
dest[2] = 0x80 | (ch & 0x00003f);
return 3;
}
/* 11110aaa 10bbbbbb 10cccccc 10dddddd */
dest[0] = 0xf0 | ((ch & 0x1c0000) >> 18);
dest[1] = 0x80 | ((ch & 0x03f000) >> 12);
dest[2] = 0x80 | ((ch & 0x000fc0) >> 6);
dest[3] = 0x80 | (ch & 0x00003f);
return 4;
}
/* Encode an 8-bit character as a percent-encoded sequence */
static size_t
uri_encode_8bit_(char *dest, unsigned char ch)
{
static const char hexdig[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
*dest = '%';
dest++;
*dest = hexdig[ch >> 4];
dest++;
*dest = hexdig[ch & 15];
dest++;
return 3;
}
/* Encode a Unicode wide-character as a sequence of percent-encoded
* UTF-8.
*/
static size_t
uri_encode_wide_(char *dest, wchar_t ch)
{
static const char hexdig[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
int utf8[6];
size_t l, c;
l = uri_wctoutf8_(utf8, ch);
for(c = 0; c < l; c++)
{
*dest = '%';
dest++;
*dest = hexdig[utf8[c] >> 4];
dest++;
*dest = hexdig[utf8[c] & 15];
dest++;
}
return l * 3;
}
/* Scan the URI string for wide characters and return the maximum storage
* needed for their UTF-8 encoding
*/
static size_t
uri_widebytes_(const char *uristr, size_t nbytes)
{
wchar_t ch;
int r;
const char *p;
size_t numwide;
mbtowc(&ch, NULL, 0);
numwide = 0;
for(p = uristr; *p;)
{
r = mbtowc(&ch, p, nbytes);
if(r <= 0)
{
return (size_t) -1;
}
if(ch < 33 || ch > 127)
{
/* Account for the full 6 bytes of UTF-8: we can't assume that
* the source string (and hence the return value of mbtowc()) is
* itself UTF-8, as it's locale-dependent.
*/
numwide += 6;
}
p += r;
}
return numwide;
}
/*
* Map a potential IRI to a URI (see section 3.1 of RFC3987), converting
* from locale-specific multibyte encoding to wide characters as we do
*/
static int
uri_preprocess_(char *restrict buf, const char *restrict uristr, size_t nbytes)
{
wchar_t ch;
char *bp;
int r;
/* Reset the multibyte shift state */
mbtowc(&ch, NULL, 0);
r = 0;
for(bp = buf; nbytes && *uristr;)
{
/* Convert the next character sequence into a wide character */
r = mbtowc(&ch, uristr, nbytes);
if(r <= 0)
{
return -1;
}
if(ch < 33 || ch > 127)
{
/* If the character is outside of the ASCII printable range,
* replace it with a percent-encoded UTF-8 equivalent
*/
bp += uri_encode_wide_(bp, ch);
}
else
{
*bp = ch;
bp++;
}
uristr += r;
nbytes -= r;
}
*bp = 0;
return 0;
}
/*
* Map a potential IRI to a URI (see section 3.1 of RFC3987), percent-encoding
* UTF-8 characters as we do
*/
static int
uri_preprocess_utf8_(char *restrict buf, const unsigned char *restrict uristr, size_t nbytes)
{
unsigned char ch;
char *bp;
int r;
/* Reset the multibyte shift state */
r = 0;
for(bp = buf; nbytes && *uristr;)
{
/* Convert the next character sequence into a wide character */
ch = *uristr;
if(ch < 33 || ch > 127)
{
/* If the character is outside of the ASCII printable range,
* replace it with a percent-encoded UTF-8 equivalent
*/
bp += uri_encode_8bit_(bp, ch);
}
else
{
*bp = ch;
bp++;
}
uristr++;
nbytes--;
}
*bp = 0;
return 0;
}