This repository has been archived by the owner on Apr 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
idna.cpp
296 lines (244 loc) · 9.1 KB
/
idna.cpp
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
/* ====================================================================
* Copyright (c) 2004-2015 Open Source Applications Foundation.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
* ====================================================================
*/
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "structmember.h"
#include "bases.h"
#include "idna.h"
#include "macros.h"
#if U_ICU_VERSION_HEX >= VERSION_HEX(55, 1, 0)
/* IDNAInfo */
class t_idnainfo : public _wrapper {
public:
UIDNAInfo *object;
UIDNAInfo info;
};
static int t_idnainfo_init(t_idnainfo *self, PyObject *args, PyObject *kwds);
static PyObject *t_idnainfo_isTransitionalDifferent(t_idnainfo *self);
static PyObject *t_idnainfo_errors(t_idnainfo *self);
static PyMethodDef t_idnainfo_methods[] = {
DECLARE_METHOD(t_idnainfo, isTransitionalDifferent, METH_NOARGS),
DECLARE_METHOD(t_idnainfo, errors, METH_NOARGS),
{ NULL, NULL, 0, NULL }
};
static void t_idnainfo_dealloc(t_idnainfo *self)
{
Py_TYPE(self)->tp_free((PyObject *) self);
}
DECLARE_STRUCT(IDNAInfo, t_idnainfo, UIDNAInfo, t_idnainfo_init,
t_idnainfo_dealloc)
/* IDNA */
class t_idna : public _wrapper {
public:
UIDNA *object;
};
typedef int32_t (*idna_fn)(
const UIDNA *idna, const UChar *text, int32_t len,
UChar *dest, int capacity, UIDNAInfo *info,
UErrorCode *status);
static int t_idna_init(t_idna *self, PyObject *args, PyObject *kwds);
static PyObject *t_idna_labelToASCII(t_idna *self, PyObject *args);
static PyObject *t_idna_labelToUnicode(t_idna *self, PyObject *args);
static PyObject *t_idna_nameToASCII(t_idna *self, PyObject *args);
static PyObject *t_idna_nameToUnicode(t_idna *self, PyObject *args);
static PyMethodDef t_idna_methods[] = {
DECLARE_METHOD(t_idna, labelToASCII, METH_VARARGS),
DECLARE_METHOD(t_idna, labelToUnicode, METH_VARARGS),
DECLARE_METHOD(t_idna, nameToASCII, METH_VARARGS),
DECLARE_METHOD(t_idna, nameToUnicode, METH_VARARGS),
{ NULL, NULL, 0, NULL }
};
static void t_idna_dealloc(t_idna *self)
{
if (self->object != NULL)
{
uidna_close(self->object);
self->object = NULL;
}
Py_TYPE(self)->tp_free((PyObject *) self);
}
DECLARE_STRUCT(IDNA, t_idna, UIDNA, t_idna_init, t_idna_dealloc)
/* IDNAInfo */
static int t_idnainfo_init(t_idnainfo *self, PyObject *args, PyObject *kwds)
{
switch (PyTuple_Size(args)) {
case 0:
memset(&self->info, 0, sizeof(UIDNAInfo));
self->info.size = sizeof(UIDNAInfo);
self->object = &self->info;
self->flags = T_OWNED;
return 0;
default:
PyErr_SetArgsError((PyObject *) self, "__init__", args);
return -1;
}
}
static PyObject *t_idnainfo_isTransitionalDifferent(t_idnainfo *self)
{
Py_RETURN_BOOL(self->info.isTransitionalDifferent);
}
static PyObject *t_idnainfo_errors(t_idnainfo *self)
{
return PyInt_FromLong(self->info.errors);
}
/* IDNA */
static int t_idna_init(t_idna *self, PyObject *args, PyObject *kwds)
{
uint32_t options;
switch (PyTuple_Size(args)) {
case 0:
INT_STATUS_CALL(self->object = uidna_openUTS46(UIDNA_DEFAULT, &status));
self->flags = T_OWNED;
return 0;
case 1:
if (!parseArgs(args, "i", &options))
{
INT_STATUS_CALL(self->object = uidna_openUTS46(options, &status));
self->flags = T_OWNED;
return 0;
}
PyErr_SetArgsError((PyObject *) self, "__init__", args);
return -1;
default:
PyErr_SetArgsError((PyObject *) self, "__init__", args);
return -1;
}
}
static PyObject *apply(idna_fn fn, const char *fn_name,
t_idna *self, PyObject *args)
{
UnicodeString *u;
UnicodeString _u;
t_idnainfo *infoArg;
switch (PyTuple_Size(args)) {
case 1:
if (!parseArgs(args, "S", &u, &_u))
{
const int32_t len = u->length();
const int32_t capacity = len * 4 + 32;
UErrorCode status = U_ZERO_ERROR;
UChar *dest = new UChar[capacity];
UIDNAInfo info = UIDNA_INFO_INITIALIZER;
PyObject *result;
int32_t size;
if (!dest)
{
PyErr_SetNone(PyExc_MemoryError);
return NULL;
}
size = (*fn)(self->object, u->getBuffer(), len, dest, capacity,
&info, &status);
if (U_FAILURE(status))
{
delete[] dest;
return ICUException(status).reportError();
}
result = PyUnicode_FromUnicodeString(dest, size);
delete[] dest;
return result;
}
break;
case 2:
if (!parseArgs(args, "SO", &IDNAInfoType_, &u, &_u, &infoArg))
{
const int32_t len = u->length();
const int32_t capacity = len * 4 + 32;
UErrorCode status = U_ZERO_ERROR;
UChar *dest = new UChar[capacity];
PyObject *result;
int32_t size;
if (!dest)
{
PyErr_SetNone(PyExc_MemoryError);
return NULL;
}
size = (*fn)(self->object, u->getBuffer(), len, dest, capacity,
infoArg->object, &status);
if (U_FAILURE(status))
{
delete[] dest;
return ICUException(status).reportError();
}
result = PyUnicode_FromUnicodeString(dest, size);
delete[] dest;
return result;
}
break;
}
return PyErr_SetArgsError((PyObject *) self, fn_name, args);
}
static PyObject *t_idna_labelToASCII(t_idna *self, PyObject *args)
{
return apply(uidna_labelToASCII, "labelToASCII", self, args);
}
static PyObject *t_idna_labelToUnicode(t_idna *self, PyObject *args)
{
return apply(uidna_labelToUnicode, "labelToUnicode", self, args);
}
static PyObject *t_idna_nameToASCII(t_idna *self, PyObject *args)
{
return apply(uidna_nameToASCII, "nameToASCII", self, args);
}
static PyObject *t_idna_nameToUnicode(t_idna *self, PyObject *args)
{
return apply(uidna_nameToUnicode, "nameToUnicode", self, args);
}
#endif
void _init_idna(PyObject *m)
{
#if U_ICU_VERSION_HEX >= VERSION_HEX(55, 1, 0)
INSTALL_STRUCT(IDNAInfo, m);
INSTALL_STRUCT(IDNA, m);
INSTALL_ENUM(IDNAInfo, "ERROR_EMPTY_LABEL", UIDNA_ERROR_EMPTY_LABEL);
INSTALL_ENUM(IDNAInfo, "ERROR_LABEL_TOO_LONG", UIDNA_ERROR_LABEL_TOO_LONG);
INSTALL_ENUM(IDNAInfo, "ERROR_DOMAIN_NAME_TOO_LONG",
UIDNA_ERROR_DOMAIN_NAME_TOO_LONG);
INSTALL_ENUM(IDNAInfo, "ERROR_LEADING_HYPHEN", UIDNA_ERROR_LEADING_HYPHEN);
INSTALL_ENUM(IDNAInfo, "ERROR_TRAILING_HYPHEN",
UIDNA_ERROR_TRAILING_HYPHEN);
INSTALL_ENUM(IDNAInfo, "ERROR_HYPHEN_3_4", UIDNA_ERROR_HYPHEN_3_4);
INSTALL_ENUM(IDNAInfo, "ERROR_LEADING_COMBINING_MARK",
UIDNA_ERROR_LEADING_COMBINING_MARK);
INSTALL_ENUM(IDNAInfo, "ERROR_DISALLOWED", UIDNA_ERROR_DISALLOWED);
INSTALL_ENUM(IDNAInfo, "ERROR_PUNYCODE", UIDNA_ERROR_PUNYCODE);
INSTALL_ENUM(IDNAInfo, "ERROR_LABEL_HAS_DOT", UIDNA_ERROR_LABEL_HAS_DOT);
INSTALL_ENUM(IDNAInfo, "ERROR_INVALID_ACE_LABEL",
UIDNA_ERROR_INVALID_ACE_LABEL);
INSTALL_ENUM(IDNAInfo, "ERROR_BIDI", UIDNA_ERROR_BIDI);
INSTALL_ENUM(IDNAInfo, "ERROR_CONTEXTJ", UIDNA_ERROR_CONTEXTJ);
INSTALL_ENUM(IDNAInfo, "ERROR_CONTEXTO_PUNCTUATION",
UIDNA_ERROR_CONTEXTO_PUNCTUATION);
INSTALL_ENUM(IDNAInfo, "ERROR_CONTEXTO_DIGITS",
UIDNA_ERROR_CONTEXTO_DIGITS);
INSTALL_ENUM(IDNA, "DEFAULT", UIDNA_DEFAULT);
INSTALL_ENUM(IDNA, "USE_STD3_RULES", UIDNA_USE_STD3_RULES);
INSTALL_ENUM(IDNA, "CHECK_BIDI", UIDNA_CHECK_BIDI);
INSTALL_ENUM(IDNA, "CHECK_CONTEXTJ", UIDNA_CHECK_CONTEXTJ);
INSTALL_ENUM(IDNA, "CHECK_NONTRANSITIONAL_TO_ASCII",
UIDNA_NONTRANSITIONAL_TO_ASCII);
INSTALL_ENUM(IDNA, "CHECK_NONTRANSITIONAL_TO_UNICODE",
UIDNA_NONTRANSITIONAL_TO_UNICODE);
INSTALL_ENUM(IDNA, "CHECK_CONTEXT0", UIDNA_CHECK_CONTEXTO);
#endif
}