summaryrefslogtreecommitdiff
path: root/src/stringprep/stringprep_drv.c
blob: 9d9f5dfe03d0524414993e86b1026272009e0d4c (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
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
/* $Id$ */

#include <stdio.h>
#include <erl_driver.h>
#include <ei.h>
#include <iconv.h>

#include "uni_data.c"
#include "uni_norm.c"

#define NAMEPREP_COMMAND 1
#define NODEPREP_COMMAND 2
#define RESOURCEPREP_COMMAND 3

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;

   //set_port_control_flags(port, PORT_CONTROL_FLAG_BINARY);
   
   return (ErlDrvData)d;
}

static void stringprep_erl_stop(ErlDrvData handle)
{
   driver_free((char*)handle);
}


static int combine(int ch1, int ch2)
{
   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;							\
	 }


static int stringprep_erl_control(ErlDrvData drv_data,
				  unsigned int command,
				  char *buf, int len,
				  char **rbuf, int rlen)
{
   int i, j, pos=1;
   unsigned char c;
   int bad = 0;
   int uc, ruc;
   int size;
   int info;
   int prohibit, tolower;
   char *rstring;
   int *mc;
   
   size = len + 1;

   rstring = driver_alloc(size);
   rstring[0] = 0;

   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;
   }

   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 & 0x1F) << 12) | ((buf[i+1] & 0x1F) << 6)
	       | (buf[i+2] & 0x3F);
	    i += 2;
	 } else {
	    bad = 1;
	 }
      } else {
	 // TODO
	 bad = 1;
      }

      if(bad) {
	 *rbuf = rstring;
	 return 1;
      }
      
      info = GetUniCharInfo(uc);
      if(info & prohibit) {
	 *rbuf = rstring;
	 return 1;
      }


      if(!(info & B1Mask)) 
      {
	 if(tolower) {
	    if(!(info & MCMask)) 
	    {
	       ruc = uc + GetDelta(info);

	       //info = GetUniCharDecompInfo(ruc);
	       //if(info >= 0) {
	       //   printf("Decomposition %x: ", ruc);
	       //   for(j = 0; j < GetDecompLen(info); j++) {
	       //      printf("%x ", decompList[GetDecompShift(info) + j]);
	       //   }
	       //   printf("\r\n");
	       //}
	       
	       ADD_UCHAR(ruc);
	    } else {
	       mc = GetMC(info);
	       for(j = 1; j <= mc[0]; j++) {
		  ruc = mc[j];
		  //printf("Char %x cclass %d\r\n", ruc, GetUniCharCClass(ruc));
		  ADD_UCHAR(ruc);
	       }
	    }
	 } else {
	    ruc = uc;
	    ADD_UCHAR(ruc);
	 }
      }
   }

   //printf("Combine: %x\r\n", combine(0x438, 0x301));

   rstring[0] = 1;
   *rbuf = rstring;
   
   return pos;
}



ErlDrvEntry stringprep_driver_entry = {
   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 */
};

#ifdef WIN32
__declspec(dllexport)
#endif
DRIVER_INIT(stringprep_erl) /* must match name in driver_entry */
{
   return &stringprep_driver_entry;
}