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
|
/*
$Id: Unicode.xs,v 1.3 2002/04/20 23:43:47 dankogai Exp $
*/
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#define U8 U8
#include "../Encode/encode.h"
#define FBCHAR 0xFFFd
#define BOM_BE 0xFeFF
#define BOM16LE 0xFFFe
#define BOM32LE 0xFFFe0000
#define issurrogate(x) (0xD800 <= (x) && (x) <= 0xDFFF )
#define isHiSurrogate(x) (0xD800 <= (x) && (x) < 0xDC00 )
#define isLoSurrogate(x) (0xDC00 <= (x) && (x) <= 0xDFFF )
#define invalid_ucs2(x) ( issurrogate(x) || 0xFFFF < (x) )
static UV
enc_unpack(pTHX_ U8 **sp,U8 *e,STRLEN size,U8 endian)
{
U8 *s = *sp;
UV v = 0;
if (s+size > e) {
croak("Partial character %c",(char) endian);
}
switch(endian) {
case 'N':
v = *s++;
v = (v << 8) | *s++;
case 'n':
v = (v << 8) | *s++;
v = (v << 8) | *s++;
break;
case 'V':
case 'v':
v |= *s++;
v |= (*s++ << 8);
if (endian == 'v')
break;
v |= (*s++ << 16);
v |= (*s++ << 24);
break;
default:
croak("Unknown endian %c",(char) endian);
break;
}
*sp = s;
return v;
}
void
enc_pack(pTHX_ SV *result,STRLEN size,U8 endian,UV value)
{
U8 *d = (U8 *)SvGROW(result,SvCUR(result)+size);
switch(endian) {
case 'v':
case 'V':
d += SvCUR(result);
SvCUR_set(result,SvCUR(result)+size);
while (size--) {
*d++ = value & 0xFF;
value >>= 8;
}
break;
case 'n':
case 'N':
SvCUR_set(result,SvCUR(result)+size);
d += SvCUR(result);
while (size--) {
*--d = value & 0xFF;
value >>= 8;
}
break;
default:
croak("Unknown endian %c",(char) endian);
break;
}
}
MODULE = Encode::Unicode PACKAGE = Encode::Unicode
PROTOTYPES: DISABLE
void
decode_xs(obj, str, check = 0)
SV * obj
SV * str
IV check
CODE:
{
int size = SvIV(*hv_fetch((HV *)SvRV(obj),"size",4,0));
U8 endian = *((U8 *)SvPV_nolen(*hv_fetch((HV *)SvRV(obj),"endian",6,0)));
int ucs2 = SvTRUE(*hv_fetch((HV *)SvRV(obj),"ucs2",4,0));
SV *result = newSVpvn("",0);
STRLEN ulen;
U8 *s = (U8 *)SvPVbyte(str,ulen);
U8 *e = (U8 *)SvEND(str);
ST(0) = sv_2mortal(result);
SvUTF8_on(result);
if (!endian && s+size <= e) {
UV bom;
endian = (size == 4) ? 'N' : 'n';
bom = enc_unpack(aTHX_ &s,e,size,endian);
if (bom != BOM_BE) {
if (bom == BOM16LE) {
endian = 'v';
}
else if (bom == BOM32LE) {
endian = 'V';
}
else {
croak("%s:Unregognised BOM %"UVxf,
SvPV_nolen(*hv_fetch((HV *)SvRV(obj),"Name",4,0)),
bom);
}
}
#if 0
/* Update endian for this sequence */
hv_store((HV *)SvRV(obj),"endian",6,newSVpv((char *)&endian,1),0);
#endif
}
while (s < e && s+size <= e) {
UV ord = enc_unpack(aTHX_ &s,e,size,endian);
U8 *d;
if (size != 4 && invalid_ucs2(ord)) {
if (ucs2) {
if (check) {
croak("%s:no surrogates allowed %"UVxf,
SvPV_nolen(*hv_fetch((HV *)SvRV(obj),"Name",4,0)),
ord);
}
if (s+size <= e) {
/* skip the next one as well */
enc_unpack(aTHX_ &s,e,size,endian);
}
ord = FBCHAR;
}
else {
UV lo;
if (!isHiSurrogate(ord)) {
croak("%s:Malformed HI surrogate %"UVxf,
SvPV_nolen(*hv_fetch((HV *)SvRV(obj),"Name",4,0)),
ord);
}
if (s+size > e) {
/* Partial character */
s -= size; /* back up to 1st half */
break; /* And exit loop */
}
lo = enc_unpack(aTHX_ &s,e,size,endian);
if (!isLoSurrogate(lo)){
croak("%s:Malformed LO surrogate %"UVxf,
SvPV_nolen(*hv_fetch((HV *)SvRV(obj),"Name",4,0)),
ord);
}
ord = 0x10000 + ((ord - 0xD800) << 10) + (lo - 0xDC00);
}
}
d = (U8 *) SvGROW(result,SvCUR(result)+UTF8_MAXLEN+1);
d = uvuni_to_utf8_flags(d+SvCUR(result), ord, 0);
SvCUR_set(result,d - (U8 *)SvPVX(result));
}
if (s < e) {
Perl_warner(aTHX_ packWARN(WARN_UTF8),"%s:Partial character",
SvPV_nolen(*hv_fetch((HV *)SvRV(obj),"Name",4,0)));
}
if (check && !(check & ENCODE_LEAVE_SRC)){
if (s < e) {
Move(s,SvPVX(str),e-s,U8);
SvCUR_set(str,(e-s));
}
else {
SvCUR_set(str,0);
}
*SvEND(str) = '\0';
}
XSRETURN(1);
}
void
encode_xs(obj, utf8, check = 0)
SV * obj
SV * utf8
IV check
CODE:
{
int size = SvIV(*hv_fetch((HV *)SvRV(obj),"size",4,0));
U8 endian = *((U8 *)SvPV_nolen(*hv_fetch((HV *)SvRV(obj),"endian",6,0)));
int ucs2 = SvTRUE(*hv_fetch((HV *)SvRV(obj),"ucs2",4,0));
SV *result = newSVpvn("",0);
STRLEN ulen;
U8 *s = (U8 *)SvPVutf8(utf8,ulen);
U8 *e = (U8 *)SvEND(utf8);
ST(0) = sv_2mortal(result);
if (!endian) {
endian = (size == 4) ? 'N' : 'n';
enc_pack(aTHX_ result,size,endian,BOM_BE);
#if 0
/* Update endian for this sequence */
hv_store((HV *)SvRV(obj),"endian",6,newSVpv((char *)&endian,1),0);
#endif
}
while (s < e && s+UTF8SKIP(s) <= e) {
STRLEN len;
UV ord = utf8n_to_uvuni(s, e-s, &len, 0);
s += len;
if (size != 4 && invalid_ucs2(ord)) {
if (!issurrogate(ord)){
if (ucs2) {
if (check) {
croak("%s:code point \"\\x{"UVxf"}\" too high",
SvPV_nolen(
*hv_fetch((HV *)SvRV(obj),"Name",4,0))
,ord);
}
enc_pack(aTHX_ result,size,endian,FBCHAR);
}else{
UV hi = ((ord - 0x10000) >> 10) + 0xD800;
UV lo = ((ord - 0x10000) & 0x3FF) + 0xDC00;
enc_pack(aTHX_ result,size,endian,hi);
enc_pack(aTHX_ result,size,endian,lo);
}
}
else {
/* not supposed to happen */
enc_pack(aTHX_ result,size,endian,FBCHAR);
}
}
else {
enc_pack(aTHX_ result,size,endian,ord);
}
}
if (s < e) {
Perl_warner(aTHX_ packWARN(WARN_UTF8),"%s:Partial character",
SvPV_nolen(*hv_fetch((HV *)SvRV(obj),"Name",4,0)));
}
if (check && !(check & ENCODE_LEAVE_SRC)){
if (s < e) {
Move(s,SvPVX(utf8),e-s,U8);
SvCUR_set(utf8,(e-s));
}
else {
SvCUR_set(utf8,0);
}
*SvEND(utf8) = '\0';
}
XSRETURN(1);
}
|