line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package FFI::Platypus::Type::WideString; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
1168
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
59
|
|
4
|
2
|
|
|
2
|
|
13
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
50
|
|
5
|
2
|
|
|
2
|
|
35
|
use 5.008004; |
|
2
|
|
|
|
|
7
|
|
6
|
2
|
|
|
2
|
|
703
|
use FFI::Platypus; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
57
|
|
7
|
2
|
|
|
2
|
|
456
|
use FFI::Platypus::Memory qw( memcpy ); |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
162
|
|
8
|
2
|
|
|
2
|
|
895
|
use FFI::Platypus::Buffer qw( buffer_to_scalar scalar_to_pointer scalar_to_buffer ); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
137
|
|
9
|
2
|
|
|
2
|
|
13
|
use Encode qw( decode encode find_encoding ); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
108
|
|
10
|
2
|
|
|
2
|
|
12
|
use Carp (); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1975
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# ABSTRACT: Platypus custom type for Unicode "wide" strings |
13
|
|
|
|
|
|
|
our $VERSION = '2.06_01'; # TRIAL VERSION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my @stack; # To keep buffer alive. |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub _compute_wide_string_encoding |
19
|
|
|
|
|
|
|
{ |
20
|
6
|
|
|
6
|
|
125
|
foreach my $need (qw( wcslen wcsnlen )) |
21
|
|
|
|
|
|
|
{ |
22
|
12
|
50
|
|
|
|
87
|
die "This type plugin needs $need from libc, and cannot find it" |
23
|
|
|
|
|
|
|
unless FFI::Platypus::Memory->can("_$need"); |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
6
|
|
|
|
|
31
|
my $ffi = FFI::Platypus->new( api => 2, lib => [undef] ); |
27
|
|
|
|
|
|
|
|
28
|
6
|
|
|
|
|
15
|
my $size = eval { $ffi->sizeof('wchar_t') }; |
|
6
|
|
|
|
|
23
|
|
29
|
6
|
50
|
|
|
|
20
|
die 'no wchar_t' if $@; |
30
|
|
|
|
|
|
|
|
31
|
6
|
|
|
|
|
49
|
my %orders = ( |
32
|
|
|
|
|
|
|
join('', 1..$size) => 'BE', |
33
|
|
|
|
|
|
|
join('', reverse 1..$size) => 'LE', |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
6
|
|
|
|
|
15
|
my $byteorder = join '', @{ $ffi->cast( "wchar_t*", "uint8[$size]", \hex(join '', map { "0$_" } 1..$size) ) }; |
|
6
|
|
|
|
|
22
|
|
|
24
|
|
|
|
|
81
|
|
37
|
|
|
|
|
|
|
|
38
|
6
|
|
|
|
|
20
|
my $encoding; |
39
|
|
|
|
|
|
|
|
40
|
6
|
50
|
|
|
|
24
|
if($size == 2) |
|
|
50
|
|
|
|
|
|
41
|
|
|
|
|
|
|
{ |
42
|
0
|
|
|
|
|
0
|
$encoding = 'UTF-16'; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
elsif($size == 4) |
45
|
|
|
|
|
|
|
{ |
46
|
6
|
|
|
|
|
14
|
$encoding = 'UTF-32'; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
else |
49
|
|
|
|
|
|
|
{ |
50
|
0
|
|
|
|
|
0
|
die "not sure what encoding to use for size $size"; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
6
|
50
|
|
|
|
17
|
if(defined $orders{$byteorder}) |
54
|
|
|
|
|
|
|
{ |
55
|
6
|
|
|
|
|
14
|
$encoding .= $orders{$byteorder}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
else |
58
|
|
|
|
|
|
|
{ |
59
|
0
|
|
|
|
|
0
|
die "odd byteorder $byteorder not (yet) supported"; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
6
|
50
|
|
|
|
26
|
die "Perl doesn't recognize $encoding as an encoding" |
63
|
|
|
|
|
|
|
unless find_encoding($encoding); |
64
|
|
|
|
|
|
|
|
65
|
6
|
|
|
|
|
5675
|
return ($encoding, $size); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub ffi_custom_type_api_1 |
69
|
|
|
|
|
|
|
{ |
70
|
4
|
|
|
4
|
0
|
16
|
my %args = @_; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# TODO: it wold be nice to allow arbitrary encodings, but we are |
73
|
|
|
|
|
|
|
# relying on a couple of wcs* functions to compute the string, so |
74
|
|
|
|
|
|
|
# we will leave that for future development. |
75
|
4
|
|
|
|
|
16
|
my($encoding, $width) = __PACKAGE__->_compute_wide_string_encoding(); |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# it is hard to come up with a default size for write buffers |
78
|
|
|
|
|
|
|
# but 2048 is a multiple of 1024 that is large enough to fit |
79
|
|
|
|
|
|
|
# any Windows PATH (260*4)+2 = 1042 |
80
|
|
|
|
|
|
|
# |
81
|
|
|
|
|
|
|
# (assuming all characters in the PATH are in the BMP, which is |
82
|
|
|
|
|
|
|
# admitedly unlikely, possilby impossible (?) and and a null |
83
|
|
|
|
|
|
|
# termination of two bytes). |
84
|
|
|
|
|
|
|
# |
85
|
|
|
|
|
|
|
# it is arbitrary and based on a platform specific windows |
86
|
|
|
|
|
|
|
# thing, but windows is where wide strings are most likely |
87
|
|
|
|
|
|
|
# to be found, so seems good as anything. |
88
|
4
|
|
50
|
|
|
24
|
my $size = $args{size} || 2048; |
89
|
4
|
|
100
|
|
|
17
|
my $access = $args{access} || 'read'; |
90
|
|
|
|
|
|
|
|
91
|
4
|
|
|
|
|
13
|
my %ct = ( |
92
|
|
|
|
|
|
|
native_type => 'opaque', |
93
|
|
|
|
|
|
|
); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
$ct{native_to_perl} = sub { |
96
|
8
|
100
|
|
8
|
|
29
|
return undef unless defined $_[0]; |
97
|
7
|
|
|
|
|
77
|
return decode($encoding, |
98
|
|
|
|
|
|
|
buffer_to_scalar( |
99
|
|
|
|
|
|
|
$_[0], |
100
|
|
|
|
|
|
|
FFI::Platypus::Memory::_wcslen($_[0])*$width, |
101
|
|
|
|
|
|
|
) |
102
|
|
|
|
|
|
|
); |
103
|
4
|
|
|
|
|
21
|
}; |
104
|
|
|
|
|
|
|
|
105
|
4
|
100
|
|
|
|
22
|
if($access eq 'read') |
|
|
50
|
|
|
|
|
|
106
|
|
|
|
|
|
|
{ |
107
|
|
|
|
|
|
|
$ct{perl_to_native} = sub { |
108
|
28
|
100
|
|
28
|
|
1212
|
if(defined $_[0]) |
109
|
|
|
|
|
|
|
{ |
110
|
27
|
|
|
|
|
97
|
my $buf = encode($encoding, $_[0]."\0"); |
111
|
27
|
|
|
|
|
1184
|
push @stack, \$buf; |
112
|
27
|
|
|
|
|
67
|
return scalar_to_pointer $buf; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
else |
115
|
|
|
|
|
|
|
{ |
116
|
1
|
|
|
|
|
4
|
push @stack, undef; |
117
|
1
|
|
|
|
|
5
|
return undef; |
118
|
|
|
|
|
|
|
} |
119
|
2
|
|
|
|
|
10
|
}; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
$ct{perl_to_native_post} = sub { |
122
|
28
|
|
|
28
|
|
53
|
pop @stack; |
123
|
28
|
|
|
|
|
109
|
return; |
124
|
2
|
|
|
|
|
8
|
}; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
elsif($access eq 'write') |
128
|
|
|
|
|
|
|
{ |
129
|
2
|
|
|
|
|
5
|
my @stack; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
$ct{perl_to_native} = sub { |
132
|
17
|
|
|
17
|
|
6090
|
my $ref = shift; |
133
|
17
|
100
|
|
|
|
58
|
if(ref($ref) eq 'ARRAY') |
|
|
100
|
|
|
|
|
|
134
|
|
|
|
|
|
|
{ |
135
|
8
|
100
|
|
|
|
14
|
${ $ref->[0] } = "\0" x $size unless defined ${ $ref->[0] }; |
|
4
|
|
|
|
|
10
|
|
|
8
|
|
|
|
|
41
|
|
136
|
8
|
|
|
|
|
13
|
my $ptr = scalar_to_pointer ${ $ref->[0] }; |
|
8
|
|
|
|
|
24
|
|
137
|
8
|
50
|
|
|
|
21
|
if(defined $ref->[0]) |
138
|
|
|
|
|
|
|
{ |
139
|
8
|
|
|
|
|
25
|
my $init = encode($encoding, $ref->[1]); |
140
|
8
|
|
|
|
|
353
|
my($sptr, $ssize) = scalar_to_buffer($init); |
141
|
8
|
|
|
|
|
41
|
memcpy($ptr, $sptr, $ssize); |
142
|
|
|
|
|
|
|
} |
143
|
8
|
|
|
|
|
15
|
push @stack, \${ $ref->[0] }; |
|
8
|
|
|
|
|
20
|
|
144
|
8
|
|
|
|
|
32
|
return $ptr; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
elsif(ref($ref) eq 'SCALAR') |
147
|
|
|
|
|
|
|
{ |
148
|
8
|
|
|
|
|
18
|
push @stack, $ref; |
149
|
8
|
100
|
|
|
|
102
|
$$ref = "\0" x $size unless defined $$ref; |
150
|
8
|
|
|
|
|
24
|
return scalar_to_pointer $$ref; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
else |
153
|
|
|
|
|
|
|
{ |
154
|
1
|
|
|
|
|
2
|
push @stack, $ref; |
155
|
1
|
|
|
|
|
5
|
return undef; |
156
|
|
|
|
|
|
|
} |
157
|
2
|
|
|
|
|
10
|
}; |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
$ct{perl_to_native_post} = sub { |
160
|
17
|
|
|
17
|
|
29
|
my $ref = pop @stack; |
161
|
17
|
100
|
|
|
|
54
|
return unless defined $ref; |
162
|
16
|
|
|
|
|
25
|
my $len = length $$ref; |
163
|
16
|
|
|
|
|
65
|
$len = FFI::Platypus::Memory::_wcsnlen($$ref, $len); |
164
|
16
|
|
|
|
|
59
|
$$ref = decode($encoding, substr($$ref, 0, $len*$width)); |
165
|
2
|
|
|
|
|
11
|
}; |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
else |
169
|
|
|
|
|
|
|
{ |
170
|
0
|
|
|
|
|
0
|
Carp::croak("Unknown access type $access"); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
4
|
|
|
|
|
14
|
return \%ct; |
174
|
|
|
|
|
|
|
} |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
1; |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
__END__ |