| 1 | package Encode::Win32; | ||
| 2 | use Win32::API; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | BEGIN { | ||
| 6 | if ($^O ne 'MSWin32') | ||
| 7 | { die "__PACKAGE__ is a Win32 only package"; } | ||
| 8 | } | ||
| 9 | |||
| 10 | our $fdec = Win32::API->new('Kernel32', 'MultiByteToWideChar', [qw(N N P N P N)], 'N'); | ||
| 11 | our $fenc = Win32::API->new('Kernel32', 'WideCharToMultiByte', [qw(N N P N P N P P)], 'N'); | ||
| 12 | |||
| 13 | |||
| 14 | sub new | ||
| 15 | { | ||
| 16 | my ($class, $enc) = @_; | ||
| 17 | my ($self) = {'name' => $enc}; | ||
| 18 | |||
| 19 | return bless $self, ref $class || $class; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub name | ||
| 23 | { return $_[0]->{'name'}; } | ||
| 24 | |||
| 25 | sub encode | ||
| 26 | { | ||
| 27 | my ($self, $str, $check) = @_; | ||
| 28 | my ($instr) = pack('S*', unpack('U*', $str), 0); | ||
| 29 | my ($spaces, $len, $res); | ||
| 30 | |||
| 31 | # $len = $fenc->Call($self->{'name'}, 0, $instr, -1, 0, 0, 0, 0) | ||
| 32 | # || return undef; | ||
| 33 | |||
| 34 | $len = length($str) * 1.1; | ||
| 35 | |||
| 36 | $spaces = ' ' x ($len + 2); | ||
| 37 | $fenc->Call($self->{'name'}, 0, $instr, -1, $spaces, length($spaces), 0, 0) | ||
| 38 | || return undef; | ||
| 39 | |||
| 40 | $spaces =~ s/\000.*$//o; | ||
| 41 | $res = pack('U0a*', $spaces); | ||
| 42 | return $spaces; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub decode | ||
| 46 | { | ||
| 47 | my ($self, $str, $check) = @_; | ||
| 48 | $str .= "\000"; | ||
| 49 | my ($spaces, $res, $len); | ||
| 50 | |||
| 51 | # $len = $fdec->Call($self->{'name'}, 0, $str, length($str), 0, 0) | ||
| 52 | # || return undef; | ||
| 53 | |||
| 54 | $len = length($str) * 2.1; | ||
| 55 | |||
| 56 | $spaces = ' ' x ($len + 2); | ||
| 57 | $fdec->Call($self->{'name'}, 0, $str, length($str), $spaces, length($spaces)) | ||
| 58 | || return undef; | ||
| 59 | $res = pack('U0U*', unpack('S*', $spaces)); | ||
| 60 | $res =~ s/\000.*$//o; | ||
| 61 | undef $spaces; | ||
| 62 | return $res; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub new_sequence | ||
| 66 | { return $_[0]; } | ||
| 67 |