-
Notifications
You must be signed in to change notification settings - Fork 24
/
node-stringprep.cc
319 lines (286 loc) · 8.07 KB
/
node-stringprep.cc
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
#include <nan.h>
#include <unicode/unistr.h>
#include <unicode/usprep.h>
#include <unicode/uidna.h>
#include <cstring>
#include <exception>
using namespace v8;
using namespace node;
/* supports return of just enum */
class UnknownProfileException : public std::exception {
};
// protect constructor from GC
static Nan::Persistent<FunctionTemplate> stringprep_constructor;
class StringPrep : public Nan::ObjectWrap {
public:
static void Initialize(Handle<Object> target)
{
Nan::HandleScope scope;
Local<FunctionTemplate> t = Nan::New<FunctionTemplate>(New);
stringprep_constructor.Reset(t);
t->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeMethod(t, "prepare", Prepare);
target->Set(Nan::New<String>("StringPrep").ToLocalChecked(), t->GetFunction());
}
bool good() const
{
return U_SUCCESS(error);
}
const char *errorName() const
{
return u_errorName(error);
}
protected:
/*** Constructor ***/
static NAN_METHOD(New)
{
if (info.Length() >= 1 && info[0]->IsString())
{
Nan::Utf8String arg0(info[0]->ToString());
UStringPrepProfileType profileType;
try
{
profileType = parseProfileType(arg0);
}
catch (UnknownProfileException &)
{
Nan::ThrowTypeError("Unknown StringPrep profile");
return;
}
StringPrep *self = new StringPrep(profileType);
if (self->good())
{
self->Wrap(info.This());
info.GetReturnValue().Set(info.This());
return;
}
else
{
const char* err = self->errorName();
delete self;
Nan::ThrowError(err);
return;
}
}
else {
Nan::ThrowTypeError("Bad argument.");
return;
}
}
StringPrep(const UStringPrepProfileType profileType)
: error(U_ZERO_ERROR)
{
profile = usprep_openByType(profileType, &error);
}
/*** Destructor ***/
~StringPrep()
{
if (profile)
usprep_close(profile);
}
/*** Prepare ***/
static NAN_METHOD(Prepare)
{
if (info.Length() >= 1 && info[0]->IsString())
{
StringPrep *self = Nan::ObjectWrap::Unwrap<StringPrep>(info.This());
String::Value arg0(info[0]->ToString());
info.GetReturnValue().Set(self->prepare(arg0));
return;
}
else {
Nan::ThrowTypeError("Bad argument.");
return;
}
}
Local<Value> prepare(String::Value &str)
{
Nan::EscapableHandleScope scope;
size_t destLen = str.length() + 1;
UChar *dest = NULL;
while(!dest)
{
error = U_ZERO_ERROR;
dest = new UChar[destLen];
size_t w = usprep_prepare(profile,
*str, str.length(),
dest, destLen,
USPREP_DEFAULT, NULL, &error);
if (error == U_BUFFER_OVERFLOW_ERROR)
{
// retry with a dest buffer twice as large
destLen *= 2;
delete[] dest;
dest = NULL;
}
else if (!good())
{
// other error, just bail out
delete[] dest;
Nan::ThrowError(errorName());
return scope.Escape(Nan::Undefined());
}
else
destLen = w;
}
Local<Value> result = Nan::New<String>(dest, destLen).ToLocalChecked();
delete[] dest;
return scope.Escape(result);
}
private:
UStringPrepProfile *profile;
UErrorCode error;
static enum UStringPrepProfileType parseProfileType(Nan::Utf8String &profile)
throw(UnknownProfileException)
{
if (strcasecmp(*profile, "nameprep") == 0)
return USPREP_RFC3491_NAMEPREP;
if (strcasecmp(*profile, "nfs4_cs_prep") == 0)
return USPREP_RFC3530_NFS4_CS_PREP;
if (strcasecmp(*profile, "nfs4_cs_prep") == 0)
return USPREP_RFC3530_NFS4_CS_PREP_CI;
if (strcasecmp(*profile, "nfs4_cis_prep") == 0)
return USPREP_RFC3530_NFS4_CIS_PREP;
if (strcasecmp(*profile, "nfs4_mixed_prep prefix") == 0)
return USPREP_RFC3530_NFS4_MIXED_PREP_PREFIX;
if (strcasecmp(*profile, "nfs4_mixed_prep suffix") == 0)
return USPREP_RFC3530_NFS4_MIXED_PREP_SUFFIX;
if (strcasecmp(*profile, "iscsi") == 0)
return USPREP_RFC3722_ISCSI;
if (strcasecmp(*profile, "nodeprep") == 0)
return USPREP_RFC3920_NODEPREP;
if (strcasecmp(*profile, "resourceprep") == 0)
return USPREP_RFC3920_RESOURCEPREP;
if (strcasecmp(*profile, "mib") == 0)
return USPREP_RFC4011_MIB;
if (strcasecmp(*profile, "saslprep") == 0)
return USPREP_RFC4013_SASLPREP;
if (strcasecmp(*profile, "trace") == 0)
return USPREP_RFC4505_TRACE;
if (strcasecmp(*profile, "ldap") == 0)
return USPREP_RFC4518_LDAP;
if (strcasecmp(*profile, "ldapci") == 0)
return USPREP_RFC4518_LDAP_CI;
throw UnknownProfileException();
}
};
/*** IDN support ***/
NAN_METHOD(ToUnicode)
{
if (info.Length() >= 2 && info[0]->IsString() && info[1]->IsInt32())
{
String::Value str(info[0]->ToString());
int32_t options = info[1]->ToInt32()->Value();
UErrorCode error = U_ZERO_ERROR;
UIDNA *uidna = uidna_openUTS46(options, &error);
if (U_FAILURE(error))
{
Nan::ThrowError(u_errorName(error));
return;
}
// ASCII encoding (xn--*--*) should be longer than Unicode
size_t destLen = str.length() + 1;
UChar *dest = NULL;
while(!dest)
{
dest = new UChar[destLen];
UIDNAInfo uinfo = UIDNA_INFO_INITIALIZER;
size_t w = uidna_nameToUnicode(
uidna,
*str, str.length(),
dest, destLen,
&uinfo, &error);
if (error == U_BUFFER_OVERFLOW_ERROR)
{
// retry with a dest buffer twice as large
destLen *= 2;
delete[] dest;
dest = NULL;
}
else if (U_FAILURE(error))
{
// other error, just bail out
delete[] dest;
uidna_close(uidna);
Nan::ThrowError(u_errorName(error));
return;
}
else
destLen = w;
}
Local<String> result = Nan::New<String>(dest, destLen).ToLocalChecked();
delete[] dest;
uidna_close(uidna);
info.GetReturnValue().Set(result);
return;
}
else {
Nan::ThrowTypeError("Bad argument.");
return;
}
}
NAN_METHOD(ToASCII)
{
if (info.Length() >= 2 && info[0]->IsString() && info[1]->IsInt32())
{
String::Value str(info[0]->ToString());
int32_t options = info[1]->ToInt32()->Value();
UErrorCode error = U_ZERO_ERROR;
UIDNA *uidna = uidna_openUTS46(options, &error);
if (U_FAILURE(error))
{
Nan::ThrowError(u_errorName(error));
return;
}
// find out length first
size_t strLen = str.length();
UIDNAInfo uinfo1 = UIDNA_INFO_INITIALIZER;
size_t destLen = uidna_nameToASCII(
uidna,
*str, strLen,
NULL, 0,
&uinfo1, &error);
UChar *dest = NULL;
if (error == U_BUFFER_OVERFLOW_ERROR)
{
// now try with dest buffer
error = U_ZERO_ERROR;
dest = new UChar[destLen];
UIDNAInfo uinfo2 = UIDNA_INFO_INITIALIZER;
uidna_nameToASCII(
uidna,
*str, strLen,
dest, destLen,
&uinfo2, &error);
if (U_SUCCESS(error) && uinfo2.errors > 0)
{
error = U_INVALID_CHAR_FOUND;
}
}
if (U_FAILURE(error))
{
// other error, just bail out
delete[] dest;
uidna_close(uidna);
Nan::ThrowError(u_errorName(error));
return;
}
Local<String> result = Nan::New<String>(dest, destLen).ToLocalChecked();
delete[] dest;
uidna_close(uidna);
info.GetReturnValue().Set(result);
return;
}
else {
Nan::ThrowTypeError("Bad argument.");
return;
}
}
/*** Initialization ***/
NAN_MODULE_INIT(init)
{
StringPrep::Initialize(target);
Nan::SetMethod(target, "toUnicode", ToUnicode);
Nan::SetMethod(target, "toASCII", ToASCII);
}
NODE_MODULE(node_stringprep, init)