forked from ldmud/ldmud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkg-idna.c
225 lines (187 loc) · 5.48 KB
/
pkg-idna.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
/*------------------------------------------------------------------
* IDNA Efuns.
*
*------------------------------------------------------------------
* This file holds the efuns interfacing with the International
* Domainname library.
*
* efun: idna_to_ascii()
* efun: idna_to_unicode()
* efun: idna_stringprep()
*------------------------------------------------------------------
*/
#include "driver.h"
#ifdef HAS_IDN
#include "pkg-idna.h"
#include <idna.h>
#include <stringprep.h>
#include <stdlib.h>
#include "typedefs.h"
#include "interpret.h"
#include "mstrings.h"
#include "simulate.h"
#include "xalloc.h"
#include "../mudlib/sys/idn.h"
/*-------------------------------------------------------------------------*/
/*=========================================================================*/
/* EFUNS */
/*-------------------------------------------------------------------------*/
svalue_t *
f_idna_to_ascii (svalue_t *sp)
/* EFUN idna_to_ascii()
*
* string idna_to_ascii (string name)
*
* Converts string name from utf-8 to idna representation (8z punycode)
*/
{
char *out = NULL;
int rc;
rc = idna_to_ascii_8z(get_txt(sp->u.str), &out, 0);
free_svalue(sp);
if (rc == IDNA_SUCCESS)
{
put_c_string(sp, out);
free(out);
}
else
{
put_number(sp, -1);
errorf("idna_to_ascii(): Error %s", idna_strerror(rc));
/* NOTREACHED */
}
return sp;
} /* idna_to_ascii */
/*--------------------------------------------------------------------*/
svalue_t *
f_idna_to_unicode (svalue_t *sp)
/* EFUN idna_to_unicode()
*
* string idna_to_unicode (string input)
*
* Converts string input from idna (punycode) to a utf-8 string
*/
{
char *out = NULL;
int rc;
rc = idna_to_unicode_8z8z(get_txt(sp->u.str), &out, 0);
free_svalue(sp);
if (rc == IDNA_SUCCESS)
{
put_c_string(sp, out);
free(out);
}
else
{
put_number(sp, -1);
errorf("idna_to_unicode(): Error %s", idna_strerror(rc));
/* NOTREACHED */
}
return sp;
} /* idna_to_unicode() */
/*--------------------------------------------------------------------*/
svalue_t *
f_idna_stringprep (svalue_t *sp)
/* EFUN idna_stringprep()
*
* string idna_stringprep(string str, int profile, int flags = 0)
*
* Prepare the UTF-8 string str according to the stringprep profile
* see also libidn stringprep(3)
*
* profile is one of the stringprep profiles defined in ldmuds idn.h
* str is assumed to be in utf-8 charset (see convert_charset)
* flags is one of the stringprep flags defined in LDMud's idn.h .
*/
{
char *buf;
size_t size;
int flags = 0;
int prof = 0;
int ret;
const Stringprep_profile *profile;
string_t *s;
/* Get and check the flags. */
{
p_uint argflags = (p_uint)sp->u.number;
sp--;
if (argflags > (STRINGPREP_FLAG_MAX << 1)-1
|| argflags & (STRINGPREP_NO_NFKC_FLAG | STRINGPREP_NO_BIDI_FLAG)
)
{
errorf("idna_stringprep(): Unsupported flag value %ld\n", (long)argflags);
/* NOTREACHED */
return sp;
}
flags = 0;
if (argflags & STRINGPREP_NO_NFKC_FLAG) flags |= STRINGPREP_NO_NFKC;
if (argflags & STRINGPREP_NO_BIDI_FLAG) flags |= STRINGPREP_NO_BIDI;
if (argflags & STRINGPREP_NO_UNASSIGNED_FLAG) flags |= STRINGPREP_NO_UNASSIGNED;
}
/* Get and check the profile */
prof = (int)sp->u.number;
sp--;
/* select the profile */
switch(prof)
{
case STRINGPREP_NAMEPREP:
profile = stringprep_nameprep;
break;
case STRINGPREP_SASLPREP:
profile = stringprep_saslprep;
break;
case STRINGPREP_PLAIN:
profile = stringprep_plain;
break;
case STRINGPREP_TRACE:
profile = stringprep_trace;
break;
case STRINGPREP_KERBEROS5:
profile = stringprep_kerberos5;
break;
case STRINGPREP_XMPP_NODEPREP:
profile = stringprep_xmpp_nodeprep;
break;
case STRINGPREP_XMPP_RESOURCEPREP:
profile = stringprep_xmpp_resourceprep;
break;
case STRINGPREP_ISCSI:
profile = stringprep_iscsi;
break;
default:
/* unknown profile */
errorf("stringprep(): unknown profile %d.\n", prof);
break; /* NOTREACHED */
}
/* Get the string */
s = sp->u.str;
/* this assumes that most strings will pass stringprep unchanged */
size = mstrsize(s)+1;
memsafe(buf = xalloc(size), size, "stringprep buffer");
memcpy(buf, get_txt(s), mstrsize(s));
buf[mstrsize(s)] = 0;
ret = stringprep(buf, size, flags, profile);
while (ret == STRINGPREP_TOO_SMALL_BUFFER)
{
/* Increase the size until it fits. */
/* TODO: Same pattern as in convert_charset() - make it a utility function?
*/
size = size > 65536 ? (size + 33) : (2 * size);
memsafe(buf = rexalloc(buf, size), size, "stringprep buffer");
ret = stringprep(buf, size, flags, profile);
}
if (ret != STRINGPREP_OK)
{
errorf("stringprep(): Error %s", stringprep_strerror(ret));
/* NOTREACHED */
}
else
{
// free the string argument
free_svalue(sp);
put_c_string(sp, buf);
}
return sp;
} /* f_idna_stringprep */
/***************************************************************************/
#endif /* HAS_IDN */