2003-09-26 20:55:01 +02:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2004-03-21 21:27:09 +01:00
|
|
|
#include <string.h>
|
2003-09-26 20:55:01 +02:00
|
|
|
#include <erl_driver.h>
|
|
|
|
#include <ei.h>
|
|
|
|
|
|
|
|
#include "uni_data.c"
|
2003-10-06 22:12:11 +02:00
|
|
|
#include "uni_norm.c"
|
2003-09-26 20:55:01 +02:00
|
|
|
|
2003-09-28 20:27:55 +02:00
|
|
|
#define NAMEPREP_COMMAND 1
|
|
|
|
#define NODEPREP_COMMAND 2
|
|
|
|
#define RESOURCEPREP_COMMAND 3
|
|
|
|
|
2003-09-26 20:55:01 +02:00
|
|
|
typedef struct {
|
|
|
|
ErlDrvPort port;
|
|
|
|
} stringprep_data;
|
|
|
|
|
|
|
|
|
|
|
|
static ErlDrvData stringprep_erl_start(ErlDrvPort port, char *buff)
|
|
|
|
{
|
|
|
|
stringprep_data* d = (stringprep_data*)driver_alloc(sizeof(stringprep_data));
|
|
|
|
d->port = port;
|
|
|
|
|
2003-09-28 20:27:55 +02:00
|
|
|
//set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY);
|
2003-09-26 20:55:01 +02:00
|
|
|
|
|
|
|
return (ErlDrvData)d;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void stringprep_erl_stop(ErlDrvData handle)
|
|
|
|
{
|
|
|
|
driver_free((char*)handle);
|
|
|
|
}
|
|
|
|
|
2003-10-07 22:31:44 +02:00
|
|
|
/*
|
|
|
|
* "canonical_ordering" and "compose" functions are based on nfkc.c from Gnome
|
|
|
|
* library
|
|
|
|
*/
|
2003-10-06 22:12:11 +02:00
|
|
|
|
2003-10-09 20:09:05 +02:00
|
|
|
static void canonical_ordering(int *str, int len)
|
2003-10-07 22:31:44 +02:00
|
|
|
{
|
|
|
|
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)
|
2003-10-06 22:12:11 +02:00
|
|
|
{
|
|
|
|
int info1, info2;
|
|
|
|
|
|
|
|
info1 = GetUniCharCompInfo(ch1);
|
|
|
|
if(info1 != -1) {
|
|
|
|
if(info1 & CompSingleMask) {
|
|
|
|
if (ch2 == compFirstList[info1 & CompMask][0]) {
|
|
|
|
return compFirstList[info1 & CompMask][1];
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
info2 = GetUniCharCompInfo(ch2);
|
|
|
|
if(info2 != -1) {
|
|
|
|
if (info2 & CompSingleMask) {
|
|
|
|
if (ch1 == compSecondList[info2 & CompMask][0]) {
|
|
|
|
return compSecondList[info2 & CompMask][1];
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return compBothList[info1][info2];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define ADD_UCHAR(ruc) \
|
|
|
|
if(ruc < 0x80) { \
|
|
|
|
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; \
|
|
|
|
}
|
|
|
|
|
2003-10-07 22:31:44 +02:00
|
|
|
#define ADD_UCHAR32(str, pos, len, ch) \
|
|
|
|
if(pos >= len) { \
|
|
|
|
len = 2*len + 1; \
|
|
|
|
str = driver_realloc(str, len * sizeof(int)); \
|
|
|
|
} \
|
|
|
|
str[pos] = ch; \
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
|
|
|
|
#define ADD_DECOMP(ruc) \
|
|
|
|
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); \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-06 22:12:11 +02:00
|
|
|
|
2003-09-26 20:55:01 +02:00
|
|
|
static int stringprep_erl_control(ErlDrvData drv_data,
|
|
|
|
unsigned int command,
|
|
|
|
char *buf, int len,
|
|
|
|
char **rbuf, int rlen)
|
|
|
|
{
|
2003-10-06 22:12:11 +02:00
|
|
|
int i, j, pos=1;
|
2003-09-26 20:55:01 +02:00
|
|
|
unsigned char c;
|
|
|
|
int bad = 0;
|
2003-10-08 20:27:48 +02:00
|
|
|
int uc = 0, ruc;
|
2003-09-26 20:55:01 +02:00
|
|
|
int size;
|
|
|
|
int info;
|
2003-10-08 20:27:48 +02:00
|
|
|
int prohibit = 0, tolower = 0;
|
2003-09-26 20:55:01 +02:00
|
|
|
char *rstring;
|
2003-10-06 22:12:11 +02:00
|
|
|
int *mc;
|
2003-10-07 22:31:44 +02:00
|
|
|
int *str32;
|
|
|
|
int str32len, str32pos = 0;
|
|
|
|
int decomp_len, decomp_shift;
|
2003-10-08 20:27:48 +02:00
|
|
|
int comp_pos, comp_starter_pos;
|
|
|
|
int cclass1, cclass2, cclass_prev;
|
2003-10-07 22:31:44 +02:00
|
|
|
int ch1, ch2;
|
|
|
|
|
2003-09-28 20:27:55 +02:00
|
|
|
size = len + 1;
|
|
|
|
|
|
|
|
rstring = driver_alloc(size);
|
|
|
|
rstring[0] = 0;
|
2003-09-26 20:55:01 +02:00
|
|
|
|
2003-10-07 22:31:44 +02:00
|
|
|
str32len = len + 1;
|
|
|
|
|
|
|
|
str32 = driver_alloc(str32len * sizeof(int));
|
|
|
|
|
2003-09-28 20:27:55 +02:00
|
|
|
switch (command)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
prohibit = ACMask;
|
|
|
|
tolower = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NAMEPREP_COMMAND:
|
|
|
|
prohibit = ACMask;
|
|
|
|
tolower = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NODEPREP_COMMAND:
|
|
|
|
prohibit = ACMask | C11Mask | C21Mask | XNPMask;
|
|
|
|
tolower = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RESOURCEPREP_COMMAND:
|
|
|
|
prohibit = ACMask | C21Mask;
|
|
|
|
tolower = 0;
|
|
|
|
break;
|
|
|
|
}
|
2003-09-26 20:55:01 +02:00
|
|
|
|
2003-10-07 22:31:44 +02:00
|
|
|
for(i = 0; i < len; i++)
|
2003-09-26 20:55:01 +02:00
|
|
|
{
|
|
|
|
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) {
|
2003-10-08 20:27:48 +02:00
|
|
|
uc = ((c & 0x0F) << 12) | ((buf[i+1] & 0x3F) << 6)
|
2003-09-26 20:55:01 +02:00
|
|
|
| (buf[i+2] & 0x3F);
|
|
|
|
i += 2;
|
|
|
|
} else {
|
|
|
|
bad = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO
|
|
|
|
bad = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(bad) {
|
2003-09-28 20:27:55 +02:00
|
|
|
*rbuf = rstring;
|
2003-10-07 22:31:44 +02:00
|
|
|
driver_free(str32);
|
2003-09-26 20:55:01 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
info = GetUniCharInfo(uc);
|
2003-09-28 20:27:55 +02:00
|
|
|
|
|
|
|
if(!(info & B1Mask))
|
|
|
|
{
|
|
|
|
if(tolower) {
|
2003-10-06 22:12:11 +02:00
|
|
|
if(!(info & MCMask))
|
|
|
|
{
|
|
|
|
ruc = uc + GetDelta(info);
|
2003-10-07 22:31:44 +02:00
|
|
|
ADD_DECOMP(ruc);
|
2003-10-06 22:12:11 +02:00
|
|
|
} else {
|
|
|
|
mc = GetMC(info);
|
|
|
|
for(j = 1; j <= mc[0]; j++) {
|
|
|
|
ruc = mc[j];
|
2003-10-07 22:31:44 +02:00
|
|
|
ADD_DECOMP(ruc);
|
2003-10-06 22:12:11 +02:00
|
|
|
}
|
|
|
|
}
|
2003-09-28 20:27:55 +02:00
|
|
|
} else {
|
|
|
|
ruc = uc;
|
2003-10-07 22:31:44 +02:00
|
|
|
ADD_DECOMP(ruc);
|
2003-09-26 20:55:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-10-06 22:12:11 +02:00
|
|
|
|
2003-10-07 22:31:44 +02:00
|
|
|
if (str32pos == 0) {
|
|
|
|
rstring[0] = 1;
|
|
|
|
*rbuf = rstring;
|
|
|
|
driver_free(str32);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
canonical_ordering(str32, str32pos);
|
|
|
|
|
2003-10-08 20:27:48 +02:00
|
|
|
comp_pos = 1;
|
|
|
|
comp_starter_pos = 0;
|
2003-10-07 22:31:44 +02:00
|
|
|
ch1 = str32[0];
|
|
|
|
cclass1 = GetUniCharCClass(ch1);
|
2003-10-08 20:27:48 +02:00
|
|
|
cclass_prev = cclass1;
|
2003-10-07 22:31:44 +02:00
|
|
|
for(i = 1; i < str32pos; i++)
|
|
|
|
{
|
|
|
|
ch2 = str32[i];
|
|
|
|
cclass2 = GetUniCharCClass(ch2);
|
2003-10-08 20:27:48 +02:00
|
|
|
if(cclass1 == 0 && cclass2 > cclass_prev && (ruc = compose(ch1, ch2))) {
|
2003-10-07 22:31:44 +02:00
|
|
|
ch1 = ruc;
|
|
|
|
} else {
|
2003-10-08 20:27:48 +02:00
|
|
|
if(cclass2 == 0) {
|
|
|
|
str32[comp_starter_pos] = ch1;
|
|
|
|
comp_starter_pos = comp_pos++;
|
|
|
|
ch1 = ch2;
|
|
|
|
cclass1 = cclass_prev = 0;
|
|
|
|
} else {
|
|
|
|
str32[comp_pos++] = ch2;
|
|
|
|
cclass_prev = cclass2;
|
|
|
|
}
|
2003-10-07 22:31:44 +02:00
|
|
|
}
|
|
|
|
}
|
2003-10-08 20:27:48 +02:00
|
|
|
str32[comp_starter_pos] = ch1;
|
|
|
|
str32pos = comp_pos;
|
2003-10-07 22:31:44 +02:00
|
|
|
|
|
|
|
for(i = 0; i < str32pos; i++)
|
|
|
|
{
|
|
|
|
ruc = str32[i];
|
2003-10-08 20:27:48 +02:00
|
|
|
info = GetUniCharInfo(ruc);
|
|
|
|
if(info & prohibit) {
|
|
|
|
*rbuf = rstring;
|
|
|
|
driver_free(str32);
|
|
|
|
return 1;
|
|
|
|
}
|
2003-10-07 22:31:44 +02:00
|
|
|
ADD_UCHAR(ruc);
|
|
|
|
}
|
|
|
|
|
2003-09-28 20:27:55 +02:00
|
|
|
rstring[0] = 1;
|
|
|
|
*rbuf = rstring;
|
2003-10-07 22:31:44 +02:00
|
|
|
driver_free(str32);
|
2003-09-26 20:55:01 +02:00
|
|
|
|
2003-10-06 22:12:11 +02:00
|
|
|
return pos;
|
2003-09-26 20:55:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ErlDrvEntry stringprep_driver_entry = {
|
2003-09-28 20:27:55 +02:00
|
|
|
NULL, /* F_PTR init, N/A */
|
|
|
|
stringprep_erl_start, /* L_PTR start, called when port is opened */
|
|
|
|
stringprep_erl_stop, /* F_PTR stop, called when port is closed */
|
|
|
|
NULL, /* F_PTR output, called when erlang has sent */
|
|
|
|
NULL, /* F_PTR ready_input, called when input descriptor ready */
|
|
|
|
NULL, /* F_PTR ready_output, called when output descriptor ready */
|
|
|
|
"stringprep_drv", /* char *driver_name, the argument to open_port */
|
|
|
|
NULL, /* F_PTR finish, called when unloaded */
|
|
|
|
NULL, /* handle */
|
|
|
|
stringprep_erl_control, /* F_PTR control, port_command callback */
|
|
|
|
NULL, /* F_PTR timeout, reserved */
|
|
|
|
NULL /* F_PTR outputv, reserved */
|
2003-09-26 20:55:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
DRIVER_INIT(stringprep_erl) /* must match name in driver_entry */
|
|
|
|
{
|
2004-04-15 21:55:38 +02:00
|
|
|
return &stringprep_driver_entry;
|
2003-09-26 20:55:01 +02:00
|
|
|
}
|
|
|
|
|