Skip to content

Commit

Permalink
unicode fix around Win32::OLE
Browse files Browse the repository at this point in the history
We configured Win32::OLE to use UTF8 with the CP => CP_UTF8 parameter.
The string are stored as Perl internal string but without the utf8 flag.

Since we know it's UTF8, we call utf8::upgrade to add the internal UTF8
flag on them.

Win32::OLE may returns non "string" item like array so we need to be
cleaver a bit.

This changes also add a fix for the case Win32::TieRegistry returns string with the UTF8 flag. In this case we won't reencode them.
  • Loading branch information
Gonéri Le Bouder committed May 17, 2012
1 parent 0414f17 commit 31a1636
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/FusionInventory/Agent/Tools/Win32.pm
Expand Up @@ -18,6 +18,7 @@ use Win32::TieRegistry (
qw/KEY_READ/
);

use utf8;
use File::Temp ();
use File::Temp qw(:seekable);
use Win32::Job;
Expand Down Expand Up @@ -66,7 +67,9 @@ sub encodeFromRegistry {
## no critic (ExplicitReturnUndef)
return undef unless $string;

return encode("UTF-8", decode(getLocalCodepage(), $string));
return $string if Encode::is_utf8($string);

return decode(getLocalCodepage(), $string);
}

sub getWmiObjects {
Expand All @@ -84,7 +87,17 @@ sub getWmiObjects {
)) {
my $object;
foreach my $property (@{$params{properties}}) {
$object->{$property} = $instance->{$property};
if (!ref($instance->{$property}) && $instance->{$property}) {
# cast the Win32::OLE object in string
$object->{$property} = sprintf("%s", $instance->{$property});

# because of the Win32::OLE->Option(CP => Win32::OLE::CP_UTF8);
# we know it's UTF8, let's flag the string according because
# Win32::OLE don't do it
utf8::upgrade($object->{$property});
} else {
$object->{$property} = $instance->{$property};
}
}
push @objects, $object;
}
Expand Down

0 comments on commit 31a1636

Please sign in to comment.