This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
/
exmpp_stringprep.c
386 lines (341 loc) · 8.44 KB
/
exmpp_stringprep.c
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <stdio.h>
#include <string.h>
#include "exmpp_driver.h"
#include "stringprep_uni_data.h"
#include "stringprep_uni_norm.h"
#define DRIVER_NAME exmpp_stringprep
/* Hangul constants */
#define SBase 0xAC00
#define LBase 0x1100
#define VBase 0x1161
#define TBase 0x11A7
#define LCount 19
#define VCount 21
#define TCount 28
#define NCount (VCount * TCount)
#define SCount (LCount * NCount)
/* Control operations. */
enum {
COMMAND_LOWERCASE = 0,
COMMAND_NAMEPREP,
COMMAND_NODEPREP,
COMMAND_RESOURCEPREP,
COMMAND_PORT_REVISION
};
static void canonical_ordering(int *str, int len);
static int compose(int ch1, int ch2);
#define ADD_UCHAR(ruc) do { \
if (ruc <= 0x7F) { \
if (pos >= size) { \
size = 2 * size + 1; \
rstring = driver_realloc(rstring, size); \
} \
rstring[pos] = (char)ruc; \
pos++; \
} else if (ruc <= 0x7FF) { \
if (pos + 1 >= size) { \
size = 2 * size + 2; \
rstring = driver_realloc(rstring, size); \
} \
rstring[pos] = (char)((ruc >> 6) | 0xC0); \
rstring[pos + 1] = (char)((ruc | 0x80) & 0xBF); \
pos += 2; \
} else if (ruc <= 0xFFFF) { \
if (pos + 2 >= size) { \
size = 2 * size + 3; \
rstring = driver_realloc(rstring, size); \
} \
rstring[pos] = (char)((ruc >> 12) | 0xE0); \
rstring[pos + 1] = (char)(((ruc >> 6) | 0x80) & 0xBF); \
rstring[pos + 2] = (char)((ruc | 0x80) & 0xBF); \
pos += 3; \
} else if (ruc <= 0x1FFFFF) { \
if (pos + 3 >= size) { \
size = 2 * size + 4; \
rstring = driver_realloc(rstring, size); \
} \
rstring[pos] = (char)((ruc >> 18) | 0xF0); \
rstring[pos + 1] = (char)(((ruc >> 12) | 0x80) & 0xBF); \
rstring[pos + 2] = (char)(((ruc >> 6) | 0x80) & 0xBF); \
rstring[pos + 3] = (char)((ruc | 0x80) & 0xBF); \
pos += 4; \
} \
} while (0)
#define ADD_UCHAR32(str, pos, len, ch) do { \
if (pos >= len) { \
len = 2*len + 1; \
str = driver_realloc(str, len * sizeof(int)); \
} \
str[pos] = ch; \
pos++; \
} while (0)
#define ADD_DECOMP(ruc) do { \
info = GetUniCharDecompInfo(ruc); \
if (info >= 0) { \
decomp_len = GetDecompLen(info); \
decomp_shift = GetDecompShift(info); \
for (j = 0; j < decomp_len; j++) { \
ADD_UCHAR32(str32, str32pos, str32len, \
decompList[decomp_shift + j]); \
} \
} else { \
ADD_UCHAR32(str32, str32pos, str32len, ruc); \
} \
} while (0)
/* -------------------------------------------------------------------
* Erlang port driver callbacks.
* ------------------------------------------------------------------- */
static int
exmpp_stringprep_control(ErlDrvData drv_data, unsigned int command,
char *buf, int len, char **rbuf, int rlen)
{
int i, j, pos;
unsigned char c;
int bad;
int uc, ruc;
int size;
int info;
int prohibit, tolower;
char *rstring;
int *mc;
int *str32;
int str32len, str32pos;
int decomp_len, decomp_shift;
int comp_pos, comp_starter_pos;
int cclass_prev, cclass2;
int ch1, ch2;
int first_ral, last_ral, have_ral, have_l;
pos = 1;
bad = 0;
uc = 0;
prohibit = 0;
tolower = 0;
str32pos = 0;
size = len + 1;
rstring = driver_alloc(size);
rstring[0] = 0;
str32len = len + 1;
str32 = driver_alloc(str32len * sizeof(int));
switch (command) {
case COMMAND_LOWERCASE:
prohibit = ACMask;
tolower = 1;
break;
case COMMAND_NAMEPREP:
prohibit = ACMask;
tolower = 1;
break;
case COMMAND_NODEPREP:
prohibit = ACMask | C11Mask | C21Mask | XNPMask;
tolower = 1;
break;
case COMMAND_RESOURCEPREP:
prohibit = ACMask | C21Mask;
tolower = 0;
break;
case COMMAND_PORT_REVISION:
rstring[0] = 1;
rstring[1] = 0;
#if defined(_MSC_VER)
strcat_s(rstring, size, "$Revision$");
#else
strcat(rstring, "$Revision$");
#endif
*rbuf = rstring;
driver_free(str32);
return (strlen(rstring));
}
for (i = 0; i < len; ++i) {
c = buf[i];
if (c < 0x80) {
uc = c;
} else if (c < 0xC0) {
bad = 1;
} else if (c < 0xE0) {
if (i + 1 < len && (buf[i + 1] & 0xC0) == 0x80) {
uc = ((c & 0x1F) << 6) | (buf[i + 1] & 0x3F);
i++;
} else {
bad = 1;
}
} else if (c < 0xF0) {
if (i+2 < len && (buf[i + 1] & 0xC0) == 0x80 &&
(buf[i + 2] & 0xC0) == 0x80) {
uc = ((c & 0x0F) << 12)
| ((buf[i + 1] & 0x3F) << 6)
| (buf[i + 2] & 0x3F);
i += 2;
} else {
bad = 1;
}
} else if (c < 0xF8) {
if (i + 3 < len &&
(buf[i + 1] & 0xC0) == 0x80 &&
(buf[i + 2] & 0xC0) == 0x80 &&
(buf[i + 3] & 0xC0) == 0x80) {
uc = ((c & 0x07) << 18)
| ((buf[i + 1] & 0x3F) << 12)
| ((buf[i + 2] & 0x3F) << 6)
| (buf[i + 3] & 0x3F);
i += 3;
if (uc > 0x10FFFF)
bad = 1;
} else {
bad = 1;
}
} else {
bad = 1;
}
if (bad) {
*rbuf = rstring;
driver_free(str32);
return (1);
}
info = GetUniCharInfo(uc);
if (!(info & B1Mask)) {
if (tolower) {
if (!(info & MCMask)) {
ruc = uc + GetDelta(info);
ADD_DECOMP(ruc);
} else {
mc = GetMC(info);
for (j = 1; j <= mc[0]; ++j) {
ruc = mc[j];
ADD_DECOMP(ruc);
}
}
} else {
ruc = uc;
ADD_DECOMP(ruc);
}
}
}
if (str32pos == 0) {
rstring[0] = 1;
*rbuf = rstring;
driver_free(str32);
return (1);
}
canonical_ordering(str32, str32pos);
comp_pos = 1;
comp_starter_pos = 0;
ch1 = str32[0];
cclass_prev = GetUniCharCClass(ch1);
for (i = 1; i < str32pos; ++i) {
ch2 = str32[i];
cclass2 = GetUniCharCClass(ch2);
if ((cclass_prev == 0 || cclass2 > cclass_prev) &&
(ruc = compose(ch1, ch2))) {
ch1 = ruc;
} else {
if (cclass2 == 0) {
str32[comp_starter_pos] = ch1;
comp_starter_pos = comp_pos++;
ch1 = ch2;
cclass_prev = 0;
} else {
str32[comp_pos++] = ch2;
cclass_prev = cclass2;
}
}
}
str32[comp_starter_pos] = ch1;
str32pos = comp_pos;
last_ral = have_ral = have_l = 0;
info = GetUniCharInfo(str32[0]);
first_ral = info & D1Mask;
for (i = 0; i < str32pos; ++i) {
ruc = str32[i];
info = GetUniCharInfo(ruc);
if (info & prohibit) {
*rbuf = rstring;
driver_free(str32);
return (1);
}
last_ral = info & D1Mask;
have_ral = have_ral || last_ral;
have_l = info & D2Mask;
ADD_UCHAR(ruc);
}
if (have_ral && (!first_ral || !last_ral || have_l)) {
*rbuf = rstring;
driver_free(str32);
return (1);
}
rstring[0] = 1;
*rbuf = rstring;
driver_free(str32);
return (pos);
}
/* -------------------------------------------------------------------
* Internal functions.
* ------------------------------------------------------------------- */
/* "canonical_ordering" and "compose" functions are based on nfkc.c from
* Gnome library. */
static void
canonical_ordering(int *str, int len)
{
int i, j, t;
int last, next;
last = GetUniCharCClass(str[0]);
for (i = 0; i < len - 1; ++i) {
next = GetUniCharCClass(str[i + 1]);
if (next != 0 && last > next) {
for (j = i; j >= 0; --j) {
if (GetUniCharCClass(str[j]) <= next)
break;
t = str[j + 1];
str[j + 1] = str[j];
str[j] = t;
}
next = last;
}
last = next;
}
}
static int
compose(int ch1, int ch2)
{
int info1, info2;
if (LBase <= ch1 && ch1 < LBase + LCount &&
VBase <= ch2 && ch2 < VBase + VCount) {
return (SBase + ((ch1 - LBase) * VCount + (ch2 - VBase)) *
TCount);
}
if (SBase <= ch1 && ch1 < SBase + SCount &&
((ch1 - SBase) % TCount) == 0 &&
TBase <= ch2 && ch2 < TBase + TCount) {
return (ch1 + ch2 - TBase);
}
info1 = GetUniCharCompInfo(ch1);
if (info1 != -1 && info1 & CompSingleMask) {
if (!(info1 & CompSecondMask) &&
ch2 == compFirstList[info1 & CompMask][0]) {
return (compFirstList[info1 & CompMask][1]);
} else
return (0);
}
info2 = GetUniCharCompInfo(ch2);
if (info2 != -1 && info2 & CompSingleMask) {
if ((info2 & CompSecondMask) &&
ch1 == compSecondList[info2 & CompMask][0]) {
return (compSecondList[info2 & CompMask][1]);
} else
return (0);
}
if (info1 != -1 && info2 != -1 &&
!(info1 & CompSecondMask) && (info2 & CompSecondMask))
return (compBothList[info1][info2 & CompMask]);
else
return (0);
}
/* -------------------------------------------------------------------
* Driver declaration.
* ------------------------------------------------------------------- */
static ErlDrvEntry driver_entry;
DRIVER_INIT(DRIVER_NAME)
{
driver_entry.driver_name = S(DRIVER_NAME);
driver_entry.control = exmpp_stringprep_control;
return (&driver_entry);
}