package RDA::Macro::Windows::Local; # $Id: Local.pm,v 1.28 2009/08/14 14:32:02 mschenke Exp $ # ARCS: $Header: /u02/webapps/workbench/cvs/RDA_4/src/scripting/lib/RDA/Macro/Windows/Local.pm,v 1.28 2009/08/14 14:32:02 mschenke Exp $ # # Change History # 20090814 MSC Extend quoting for interfacing with the registry. =head1 NAME RDA::Macro::Windows::Local - Class Used for Microsoft Windows Local Macros =head1 SYNOPSIS require RDA::Macro::Windows::Local; =head1 DESCRIPTION The objects of the C class are used to interface with Microsoft Windows-related macros. The following methods are available: =cut use strict; BEGIN { use Exporter; use File::Copy; use File::Spec::Win32; use IO::File; use RDA::Object::Buffer; } # Define the global public variables use vars qw($VERSION); $VERSION = sprintf("%d.%02d", q$Revision: 1.28 $ =~ /(\d+)\.(\d+)/); # Define the global private variables my %tb_cat = ( 0 => 'None', 1 => 'General', 2 => 'Disk', 8 => 'Installation', ); my %tb_fct = ( 'getCodePage' => [\&_m_get_code_page, 'T'], 'getRegValue' => [\&_m_get_reg_value, 'T'], 'getVersionInfo' => [\&_m_get_ver_info, 'L'], 'grepRegValue' => [\&_m_grep_reg_value, 'L'], 'writeEvents' => [\&_m_write_events, 'N'], 'writeFirewall' => [\&_m_write_firewall, 'N'], 'writeMsinfo' => [\&_m_write_msinfo, 'N'], 'writeRegistry' => [\&_m_write_registry, 'N'], 'writeSysinfo' => [\&_m_write_sysinfo, 'N'], 'writeWinmsd' => [\&_m_write_winmsd, 'N'], ); my %tb_msi = ( 'ComponentsDisplay' => ['Display' => 1], 'ComponentsStorageDrives' => ['Disks' => 1, 'Drives' => 1], 'ComponentsNetworkProtocol' => ['Protocol' => 1], 'Odbc' => ['ODBC Drivers' => 2], 'ResourcesMemory' => ['Memory' => 1], 'SWEnvServices' => ['Services' => 1], 'SWEnvDrivers' => ['System Drivers' => 1], 'SWEnvEnvVars' => ['Environment Variables' => 1], 'SystemSummary' => ['System Summary' => 1], ); my %tb_reg = ( buf => [\&_get_buf, \&_grep_buf, \&_write_buf], reg => [\&_get_reg, \&_grep_reg, \&_write_reg], ); # Report the module version number sub Version { $VERSION; } =head2 S<$h = RDA::Macro::Windows::Local-Enew($agt)> The object constructor. It takes the agent object reference as an argument. C is represented by a blessed hash reference. The following special keys are used: =over 16 =item S< B<'_agt' > > Reference to the agent object =item S< B<'_buf' > > Registry hash =item S< B<'_cyg' > > Cygwin indicator =item S< B<'_msi' > > Msinfo32 report =item S< B<'_reg' > > Registry access method =item S< B<'_tmp' > > Temporary file =item S< B<'_utb' > > UTF16 to UTF8 / begin of file indicator =item S< B<'_utf' > > UTF16 to UTF8 / unpack format =item S< B<'_uth' > > UTF16 to UTF8 / input file handle =item S< B<'_uti' > > UTF16 to UTF8 / input array =item S< B<'_utl' > > UTF16 to UTF8 / character left from previous read =back Internal keys are prefixed by an underscore. =cut sub new { my $slf = shift; my $class = ref($slf) || $slf; my $agt = shift; # Abort if not executed in a Windows environment return unless $agt->is_windows; # Create the macro object $slf = bless { _agt => $agt, _cyg => $^O eq 'cygwin', }, $class; # Setup some parameters by default $slf->{'_tmp'} = File::Spec::Win32->catfile( $agt->get_setting('RPT_DIRECTORY', File::Spec::Win32->curdir), 'tmp_'.$agt->get_setting('RPT_GROUP')."_$$.txt" ); # Determine how access the registry $slf->{'_buf'} = {}; $slf->{'_reg'} = $tb_reg{_get_reg($slf, 'HKLM\Software\Microsoft\Windows NT\CurrentVersion','ProductName') ? 'reg' : 'buf'}; # Determine how access the msinfo32 $slf->{'_msi'} = File::Spec::Win32->catfile( $agt->get_setting('RPT_DIRECTORY', File::Spec::Win32->curdir), $agt->get_setting('RPT_GROUP')."_INI_msi.txt") if &{$slf->{'_reg'}->[0]}($slf, 'HKLM\Software\Microsoft\Windows NT\CurrentVersion', 'CurrentVersion', '5.1') =~ m/^6\./; # Register the macros $agt->register($slf, [keys(%tb_fct)], qw(thread)); # Return the object reference $slf; } =head2 S<$h-Ecall($name,...)> This method executes the macro code. =cut sub call { my ($slf, $nam, @arg) = @_; &{$tb_fct{$nam}->[0]}($slf, @arg); } =head2 S<$h-Ereset> This method resets the object for its new environment to allow a thread-save execution. =cut sub reset { my ($slf) = @_; my $agt = $slf->{'_agt'}; $slf->{'_tmp'} = File::Spec::Win32->catfile( $agt->get_setting('RPT_DIRECTORY', File::Spec::Win32->curdir), 'tmp_'.$agt->get_setting('RPT_GROUP')."_$$.txt" ); } =head2 S<$h-Erun($name,$arg,$ctx)> This method executes the macro with the specified argument list in a given context. =cut sub run { my ($slf, $nam, $arg, $ctx) = @_; my ($fct, $ret, $typ); $fct = $tb_fct{$nam}; $typ = $fct->[1]; # Treat an array context return RDA::Value::new_list(&{$fct->[0]}($slf, $ctx, $arg->eval_as_array)) if $typ eq 'L'; # Treat a scalar context $ret = &{$fct->[0]}($slf, $ctx, $arg->eval_as_array); $typ = 'U' unless defined($ret); return RDA::Value->new($typ, $ret); } =head1 WINDOWS MACROS =head2 S This macro returns the current code page. =cut sub _m_get_code_page { my ($slf, $ctx) = @_; my ($set); ($set) = `cmd /c chcp`; ($set =~ m/\s(\d+)[\n\r\s]*$/) ? $1 : undef; } =head2 S This macro returns the value of a registry key name. It returns the default value when not found. =cut sub _m_get_reg_value { my ($slf, $ctx, $key, $nam, $val) = @_; return $val unless $key && $nam; &{$slf->{'_reg'}->[0]}($slf, $key, $nam, $val); } =head2 S This macro returns the version information of the specified file as a hash content. It returns an empty list when problems are encountered. =cut sub _m_get_ver_info { my ($slf, $ctx, $fil) = @_; my (%inf); if ($fil && open(FIL, "<$fil")) { binmode(FIL); _get_version(\%inf); close(FIL); } return %inf; } sub _get_version { my $inf = shift; my ($bas, $buf, $cnt, $nxt, $off, $siz, @tbl); # Read the first block return unless sysread(FIL, $buf, 4096) == 4096; ($nxt) = unpack('L', substr($buf, 0, 4)); return unless $nxt == 0x905a4d; # List the sections ($off) = unpack('L', substr($buf, 60, 4)); ($cnt) = unpack('S', substr($buf, $off + 6, 2)); @tbl = unpack('L3', substr($buf, 16, 12)); for ($off += $tbl[0] + $tbl[2] ; $cnt-- > 0 ; $off += 40) { @tbl = unpack('Z8L4', substr($buf, $off, 40)); ($nxt, $siz, $bas) = ($tbl[4], $tbl[3], $tbl[2]) if $tbl[0] eq '.rsrc'; } # Read the resource block return unless $nxt && $siz && seek(FIL, $nxt, 0) && sysread(FIL, $buf, $siz) == $siz; # Find the version information ($siz, $cnt) = unpack('S2', substr($buf, 12, 4)); for ($off = 16 + $siz * 8 , $nxt = 0 ; $cnt-- > 0 ; $off += 8) { @tbl = unpack('S3', substr($buf, $off, 6)); $nxt = $tbl[2] if $tbl[0] == 16; } return unless $nxt; ($off) = unpack('S', substr($buf, $nxt + 20, 2)); ($off) = unpack('S', substr($buf, $off + 20, 2)); ($off, $siz) = unpack('L2', substr($buf, $off, 8)); # Decode the file information @tbl = unpack('S*', substr($buf, $off - $bas, $siz)); _ext_version($inf, \@tbl, 0, 0) if $siz == $tbl[0]; } sub _ext_version { my ($inf, $tbl, $off) = @_; my ($beg, $dat, $end, $key, $siz, $typ, @str); $beg = $off; $siz = $tbl->[$off++]; $end = $beg + $siz / 2; $dat = $tbl->[$off++]; $typ = $tbl->[$off++]; # Extract the associated key push(@str, $tbl->[$off++]) while $tbl->[$off]; ++$off if ++$off & 1; $key = _ext_ver_str(\@str); # Extract the value if ($typ == 0) { $off += int(($dat + 1) / 2); ++$off if $off & 1; } elsif ($dat) { @str = (); push(@str, $tbl->[$off++]) while $tbl->[$off]; ++$off if ++$off & 1; $inf->{$key} = _ext_ver_str(\@str); return $off; } $off = _ext_version($inf, $tbl, $off) while $off < $end; return $off; } sub _ext_ver_str { my ($buf, $chr, @inp); _cnv_utf16(\@inp, shift); $buf = ''; $buf .= chr($chr) while defined($chr = shift(@inp)) && $chr; $buf; } =head2 S This macro returns the list of registry keys that contain the specified value name. When the flag is set, it returns C pairs. =cut sub _m_grep_reg_value { my ($slf, $ctx, $key, $nam, $flg) = @_; return () unless $key && $nam; &{$slf->{'_reg'}->[1]}($slf, $key, $nam, $flg); } =head2 S This macro extracts events from the specified event log and writes them to the output file. You can filter events by using a regular expression to indicate which sources are relevant. When a number greater than zero is specified as age, then only the events more recent than that number of days are considered. By default, it includes main fields only in the report. This is controlled by the last argument. It returns the number of the events written. =cut sub _m_write_events { my ($slf, $ctx, $fil, $flt, $age, $all) = @_; my ($cnt, $psx); # Initialize the filter $flt = qr#$flt# if $flt; $age = ($age && $age > 0) ? time - 86400 * $age : 0; # Load the POSIX module when possible eval "require POSIX"; $psx = $@; # Treat the event log file $cnt = 0; if (open(EVT, "<$fil")) { my ($buf, $cmp, $dat, $evt, $lgt, $nxt, $off, $ofh, $siz, $src); # Load the file content binmode(EVT); $off = 0; $off += $lgt while ($lgt = read(EVT, $dat, 65536, $off)); close(EVT); # Create the circular buffer $evt = RDA::Object::Buffer->new(\$dat); $evt->setlim(unpack('L', $dat)); # Find and load the end record next unless _find_end_evt($evt, \$dat); $lgt = $evt->sysread($buf, 4); $evt->sysseek($off = unpack('L',$buf), 0); # Treat all events for ($nxt = 4; ($lgt = $nxt) && $evt->sysread($buf, $lgt) == $lgt ;) { # Determine the size of the next record ($nxt) = unpack('L', substr($buf,-4)); next if $lgt == 4; # Analyze the header my ($sig, $rec, $tmc, $tmw, $eid, $typ, $num, $flg, $cat, $end, $off1, $lgt2, $off2, $lgt3, $off3) = unpack("L4S4L7", $buf); last unless $sig == 0x654c664c; # Filter the event on its age next if $age && $tmc < $age; # Filter the event on its source if (($siz = $off1 - 56) > 0) { my (@src, @tbl); @src = unpack('v*', substr($buf, 52, $siz)); _cnv_utf16(\@tbl, \@src); $src = _ext_evt_txt(\@tbl); $cmp = _ext_evt_txt(\@tbl); } else { $cmp = $src = ''; } next if $flt && $src !~ $flt; # Get the output file handle on first event if ($cnt++) { print $ofh "| ||\n"; } else { $ofh = $ctx->get_handle; } # Print the event print $ofh "|*Record Number*|".$rec." |\n" if $all; print $ofh "|*Event Id*|".$eid, " |\n|*Created*|"._fmt_evt_time($tmc), " |\n"; print $ofh "|*Written*|"._fmt_evt_time($tmw), " |\n|*Type*|".sprintf("0x%04x", $typ), " |\n|*Flag*|".sprintf("0x%04x", $flg), " |\n" if $all; print $ofh "|*Category*|".($tb_cat{$cat} || "($cat)")." |\n"; print $ofh "|*Source*|".$src." |\n|*Computer*|".$cmp." |\n" if $src || $cmp; if (($siz = $off3 - $off1) > 0) { my (@src, @tbl); @src = unpack('v*', substr($buf, $off1 - 4, $siz)); _cnv_utf16(\@tbl, \@src); print $ofh "|*Description*|"._ext_evt_txt(\@tbl)." |\n" } print $ofh "|*String*|"._ext_evt_str(substr($buf, $off3 - 4, $lgt3)) ." |\n" if $lgt3; if ($lgt2) { my $str = substr($buf, $off2 - 4, $lgt2); print $ofh "|*SID*|"._dump_evt_data($str)." |\n"; } } $evt->close; } $cnt; } sub _dump_evt_data { my $str = shift; my ($buf, $sep); $sep = $buf = ''; foreach my $chr (split(//, $str)) { $buf .= $sep.sprintf('%02x', ord($chr)); $sep = ' '; } "``$buf``"; } sub _ext_evt_str { my $str = shift; # Detect a string in UTF16 if ($str =~ m/^\r\000\n\000/) { my (@src, @tbl); @src = unpack('v*', $str); _cnv_utf16(\@tbl, \@src); return '``'._ext_evt_txt(\@tbl).'``'; } # Detect a binary string return _dump_evt_data($str) if $str =~ m/^.?[\000-\037]/; # Treat a string _fmt_evt_str($str); } sub _ext_evt_txt { my $src = shift; my $buf = ''; my $chr; while (defined($chr = shift(@$src))) { last unless $chr; $buf .= chr($chr); } _fmt_evt_str($buf); } sub _find_end_evt { my ($evt, $dat) = @_; my ($buf, $off, $sig1, $sig2); $sig1 = pack('H*',"11111111"); $sig2 = pack('H*',"11111111222222223333333344444444"); for ($off = 0 ; ($off = index($$dat, $sig1, $off)) > 0 ; ++$off) { unless ($off & 3) { $evt->sysseek($off, 0); return 0 unless $evt->sysread($buf, 16) == 16; return $off if $buf eq $sig2; } } 0; } sub _fmt_evt_str { my ($str) = @_; $str =~ s/^\r//g; $str =~ s/^[\n\s]+//; $str =~ s/[\n\s]+$//; $str =~ s/\n+/\%BR\%/g; $str =~ s/\'/'/g; $str =~ s/\*/*/g; $str =~ s/\`/`/g; $str =~ s/\|/|/g; $str; } sub _fmt_evt_time { my ($tim, $flg) = @_; $flg ? scalar(gmtime($tim)) : &POSIX::strftime('%d-%b-%Y %H:%M:%S UTC', gmtime($tim)); } =head2 S This macro writes the firewall configuration to the output file. It returns the number of the lines written. When the flag is set, the sub sections do not contribute to the table of contents. =cut sub _m_write_firewall { my ($slf, $ctx, $toc) = @_; my ($cnt, $flg, $lvl, $ofh, $scp); $toc = $toc ? '+!!' : ''; $cnt = 0; if (open(IN, "netsh firewall show state verbose=enable |")) { # Get the output file handle $ofh = $ctx->get_handle; # Reformat the command output while () { s/[\s\n\r]+$//; if (m/^(.*):$/) { print $ofh "\n---+$toc $1"; ++$cnt; $flg = $lvl = 2; $scp = ($1 =~ m/ICMP settings/) ? 0 : -1; } elsif (s/\s+=\s/ |/) { print $ofh "\n|$_ |"; ++$cnt; } elsif ($flg && s/\s{2,}/* |*/g) { $lvl = tr/\|/\|/; print $ofh "\n|*$_* |"; ++$cnt; if ($scp) { print $ofh "*Scope* |"; $scp = 1; } } elsif (s/\s{2,}/ |/g) { print $ofh "\n|$_ |"; ++$cnt; } elsif (m/^-+$/) { $flg = 0; } elsif (m/^\s+Scope:\s+(.*)/) { print $ofh "Scope: " if $scp < 0; print $ofh "$1 |"; } else { print $ofh "\n$_"; ++$cnt; } } print $ofh "\n"; ++$cnt; close(IN); } # Return the number of lines written $cnt; } =head2 S This macro writes the result of C categories to the output file. It returns the number of the lines written. =cut sub _m_write_msinfo { my $slf = shift; my $ctx = shift; my $ttl = shift; my ($cat, $cnt, $flg, $hdr, $nxt, $ofh, $pgm, $skp, $tbl, $tmp); if (exists($slf->{'_msi'})) { # Determine the category list $tbl = (scalar @_) ? {map {@{exists($tb_msi{$_}) ? $tb_msi{$_} : []}} @_} : {'System Summary' => 1}; # Get the msinfo32 report on the first call unless (-f ($tmp = $slf->{'_msi'})) { $pgm = File::Spec->catfile($ENV{'COMMONPROGRAMFILES'}, 'Microsoft Shared', 'MSInfo', 'msinfo32.exe'); $pgm = '"'.$pgm.'"' unless $^O eq 'cygwin'; eval { local $SIG{'__WARN__'} = sub { }; system($pgm, '/report', $tmp); }; return 0 if $@; } } else { # Determine the category list $cat = (scalar @_) ? join('+', @_) : 'SystemSummary'; # Get the information in a temporary file $tmp = $slf->{'_tmp'}; $pgm = File::Spec->catfile($ENV{'COMMONPROGRAMFILES'}, 'Microsoft Shared', 'MSInfo', 'msinfo32.exe'); return 0 unless -x $pgm; eval { local $SIG{'__WARN__'} = sub { }; system($pgm, '/categories', "+$cat", '/report', $tmp); }; return 0 if $@; } # Open the file and get the output file handle return 0 unless $slf->_open_utf16("<$tmp"); $ofh = $ctx->get_handle; # Reformat the information print $ofh "---+ $ttl\n"; $cnt = $flg = $skp = 1; $nxt = 0; $hdr = ''; while (defined($_ = $slf->_getl_utf16)) { s//>/g; if (s/ *\t */ |/g) { next if $skp; s/^ \| \|/ ||/g; s/^/|/; s/([^\|])$/$1|/; s/^(\|[^\|]+\|)$/$1|/; if ($flg) { s/\|/*|*/g; s/^\*//; s/\*$//; } } elsif (m/^\[(.*)\]$/) { if (!$tbl) { $skp = ($1 eq 'System Summary' && $cat ne 'SystemSummary'); } elsif (exists($tbl->{$1})) { $skp = 0; $nxt = $tbl->{$1}; } elsif ($nxt) { $skp = (--$nxt) ? 0 : 1; } else { $skp = 1; } $hdr = ($1 eq $ttl) ? '' : "---++!! $1\n"; next; } else { $flg = 1; next; } if ($hdr) { print $ofh $hdr; ++$cnt; $hdr = ''; } print $ofh $_, "\n"; ++$cnt; $flg = 0; } $slf->_close_utf16; unless ($tbl) { 1 while unlink($tmp); } # Return the number of lines written $cnt; } =head2 S This macro writes the registry key to the output file. The level (starting from 0) indicates the highest branch level to include in the report table of contents. By default, it takes one more than the level of the specified key. It returns the number of the lines written. =cut sub _m_write_registry { my ($slf, $ctx, $key, $lvl) = @_; my ($cnt, $flg, $nam, $ofh, $toc, $typ, $val); # Determine the indexation level $lvl = 1 + ($key =~ tr/\\/\\/) unless defined($lvl); # Write the registry key and return the number of lines written &{$slf->{'_reg'}->[2]}($slf, $ctx, $key, $lvl); } =head2 S This macro writes the C information to the output file. =cut sub _m_write_sysinfo { my ($slf, $ctx) = @_; my ($cnt, $itm, $ofh, $val, @tbl, @tb_itm, @tb_val); # Get the systeminfo output $cnt = 0; if (open(IN, "systeminfo /FO CSV 2>&1 |")) { while () { s/[\s\n\r]+$//; push(@tbl, $1) if m/^"(.*)"$/; } close(IN); } return 0 unless scalar(@tbl) == 2; # Get the output file handle $ofh = $ctx->get_handle; # Decode the information @tb_itm = split(/","/, $tbl[0]); @tb_val = split(/","/, $tbl[1]); # Output the information while (defined($itm = shift(@tb_itm)) && defined($val = shift(@tb_val))) { $val =~ s/,/\%BR\%/g if $itm =~ m/^(Hotfix|NetWork|Processor)/i; $val =~ s/^\s+/    /g; print $ofh "|$itm |$val |\n"; ++$cnt; } # Return the number of lines written $cnt; } =head2 S This macro writes the result of C to the output file. It returns the number of the lines written. =cut sub _m_write_winmsd { my ($slf, $ctx) = @_; my ($cnt, $flg, $hdr, $ofh, $skp, $tmp); # Get the information in a temporary file $tmp = $ENV{'COMPUTERNAME'}.'.txt'; eval { local $SIG{'__WARN__'} = sub { }; system("winmsd /a /f"); }; return 0 if $@; # Open the file and get the output file handle return 0 unless $slf->_open_utf16("<$tmp"); $ofh = $ctx->get_handle; # Reformat the information $cnt = 0; $flg = 1; $hdr = ''; while (defined($_ = $slf->_getl_utf16)) { s/[\s]+$//; if ($hdr) { next if $flg && m/^(-+)?$/; if (m/^$hdr\s/) { print $ofh "\n" unless $flg; print $ofh "---++ ", $_, "\n"; $flg = 1; } elsif (m/^$/) { print $ofh "\n" unless $skp; $skp = 1; } else { print $ofh "\n" if $flg; print $ofh $_, "\n"; $skp = $flg = 0; } ++$cnt; } else { $hdr = $1 if m/^(\w+)\s/; } } $slf->_close_utf16; print $ofh "\n" unless $flg; 1 while unlink($tmp); # Return the number of lines written $cnt; } #--- UTF16 to UTF8 conversion methods ---------------------------------------- sub _open_utf16 { my ($slf, $fil) = @_; my ($cnt); $slf->{'_utb'} = 1; $slf->{'_utf'} = 'v*'; $slf->{'_uth'} = IO::File->new; $slf->{'_uti'} = []; $slf->{'_utl'} = ''; $cnt = 10; while (!open($slf->{'_uth'}, $fil)) { return 0 unless $cnt--; sleep(1); } 1; } sub _close_utf16 { my ($slf) = @_; close($slf->{'_uth'}); } sub _getl_utf16 { my ($slf) = @_; my ($chr, @lin); while (defined($chr = $slf->_getc_utf16)) { return pack('C*', @lin) if $chr == 10; push(@lin, $chr) unless $chr == 13; } scalar(@lin) ? pack('C*', @lin) : undef; } sub _getc_utf16 { my ($slf) = @_; my ($buf, $chr, $inp, $lgt, @src); # Return a character from the input buffer $inp = $slf->{'_uti'}; return $chr if defined($chr = shift(@$inp)); # Read a block and convert it while ($lgt = sysread($slf->{'_uth'}, $buf, 2048)) { # Get an even number of characters $lgt = length($buf = $slf->{'_utl'}.$buf); if ($lgt & 1) { $slf->{'_utl'} = substr($buf, -1); next unless --$lgt; $buf = substr($buf, 0, $lgt); } else { $slf->{'_utl'} = ''; } # Determine the byte order at the beginning of the file if ($slf->{'_utb'}) { $slf->{'_utb'} = 0; if ($buf =~ m/^\377\376/) { $slf->{'_utf'} = 'v*'; $buf = substr($buf, 2); next if $lgt < 2; } elsif ($buf =~ m/^\376\377/) { $slf->{'_utf'} = 'n*'; $buf = substr($buf, 2); next if $lgt < 2; } } # Decode the buffer @src = unpack($slf->{'_utf'}, $buf); _cnv_utf16($inp, \@src); return $chr if defined($chr = shift(@$inp)); } # Indicate the end of the file undef; } sub _cnv_utf16 { my ($inp, $src) = @_; my ($chr, $low); while (defined($chr = shift(@$src))) { # Detect surrogate if ($chr >= 0xD800 && $chr <= 0xDFFF) { $low = shift(@$src) || 0; if ($chr >= 0xDC00 || $low < 0xDC00 || $low > 0xDFFF) { unshift(@$src, $low); next; } else { $chr = ($chr - 0xD800) * 0x400 + ($low - 0xDC00) + 0x10000; } } # Convert the character if ($chr < 0x80) { push(@$inp, $chr); } elsif ($chr < 0x800) { push(@$inp, (($chr >> 6) | 0300), (($chr & 0077) | 0200)); } elsif ($chr < 0x10000) { push(@$inp, (( $chr >> 12) | 0340), ((($chr >> 6) & 0077) | 0200), (( $chr & 0077) | 0200)); } elsif ($chr < 0x200000) { push(@$inp, (( $chr >> 18) | 0360), ((($chr >> 12) & 0077) | 0200), ((($chr >> 6) & 0077) | 0200), (( $chr & 0077) | 0200)); } } } #--- Reg query methods ------------------------------------------------------- # Get a registry value sub _get_reg { my ($slf, $key, $nam, $val) = @_; if (open(REG, "reg query \"$key\" /v \"$nam\" 2>&1 |")) { while () { if (m/^\s+\Q$nam\E\s/i) { s/[\s\n\r]+$//; my @tbl = split(/\s+/, $_, 4); $val = $tbl[3] if defined($tbl[3]); } } close(REG); } $val; } # Grep all registry keys containing a name sub _grep_reg { my ($slf, $key, $nam, $flg) = @_; my ($lst, @tbl); if (open(REG, "reg query \"$key\" /s 2>&1 |")) { while () { s/[\s\n\r]+$//; if ($lst && m/^\s+($nam)\s/i) { push(@tbl, $flg ? "$lst|$1" : $lst); } elsif (m/^[A-Z]/) { $lst = $_; } } close(REG); } @tbl; } sub _write_reg { my ($slf, $ctx, $key, $lvl) = @_; my ($cnt, $cod, $nam, $ofh, $toc, $typ, $val); # Write the registry key $cnt = 0; if (open(IN, "reg query \"$key\" /s 2>&1 |")) { # Get the output file handle $ofh = $ctx->get_handle; # Reformat the command output while () { s/[\s\n\r]+$//; next if m/^$/ || m/^!/ || m/^Error:/; if (m/^[A-Z]/) { $toc = tr/\\/\\/; $toc = ($toc > $lvl) ? '+!!' : ''; print $ofh "\n" if $cod; print $ofh "\n---++$toc ``[$_]``\n"; ++$cnt; $cod = 0; } elsif (m/^\s+(.*\S)\s+REG_([A-Z_]+)\s+(.*)$/) { ($nam, $typ, $val) = ($1, $2, $3); print $ofh "\n" unless $cod++; $nam = '(Default)' if $nam eq ''; if ($typ =~ m/SZ$/) { print $ofh "$nam = \"$val\"\n"; } else { $typ = lc($typ); print $ofh "$nam = $typ:$val\n"; } ++$cnt; } } close(IN); } print $ofh "\n" if $cod; # Return the number of lines written $cnt; } #--- Regedit methods --------------------------------------------------------- # Get a registry value sub _get_buf { my ($slf, $key, $nam, $val) = @_; my ($flg, $reg); ($key, $reg) = _key_buf($key); for (@{$slf->_sel_buf($key)}) { if ($flg && m/^\s+$nam\s/i) { my @tbl = split(/\s+/, $_, 4); $val = $tbl[3] if defined($tbl[3]); last; } elsif (m/^[A-Z]/) { last if $flg; $flg = $_ =~ m/^$reg$/i; } } $val; } # Grep all registry keys containing a name sub _grep_buf { my ($slf, $key, $nam, $flg) = @_; my ($lst, $reg, @tbl); ($key, $reg) = _key_buf($key); for (@{$slf->_sel_buf($key)}) { if ($lst && m/^\s+($nam)\s/i) { push(@tbl, $flg ? "$lst|$1" : $lst); } elsif (m/^[A-Z]/) { if (m/^$reg/i) { $lst = $_; } elsif ($lst) { last; } } } @tbl; } # Reformat the key for searching in buffer sub _key_buf { my ($key) = @_; my ($reg); $key =~ s/^HKLM/HKEY_LOCAL_MACHINE/; $reg = $key; $reg =~ s/\\/\\\\/g; ($key, $reg); } # Load a registry key in memory sub _load_buf { my ($slf, $key) = @_; my ($buf, $flg, $lin, $nam, $typ); # Export the registry key $slf->{'_buf'}->{uc($key)} = $buf = []; system('regedit /e "'.$slf->{'_tmp'}.'" "'.$key.'"'); unless ($?) { # Load the registry key $flg = 0; $lin = ''; if ($slf->_open_utf16('<'.$slf->{'_tmp'})) { while (defined($_ = $slf->_getl_utf16)) { s/\s+$//; s/^\s+//; $lin .= $_; next if $lin =~ s/\\$//; $lin =~ s/\s+$//; next if $lin =~ m/^$/; if ($lin =~ m/^\[([^\]]+)/) { push(@$buf, $1); $flg = 1; } elsif ($flg) { $nam = ($lin =~ s/^"([^"]*)"=//) ? $1 : ($lin =~ s/@=//) ? '' : '?'; if ($lin =~ s/^"([^"]*)"/$1/) { $typ = 'REG_SZ '; $lin =~ s/\\(.)/$1/g; } elsif ($lin =~ s/^hex\(0\)://i) { $typ = 'REG_NONE'; } elsif ($lin =~ s/^hex://i) { $typ = 'REG_BINARY '; $lin =~ s/,//g; $lin = uc($lin); } elsif ($lin =~ s/^hex\(2\)://i) { $typ = 'REG_EXPAND_SZ '; $lin =~ s/00,?//g; $lin =~ s/[\dA-Fa-f]{2},?/chr(oct('0x'.substr($&,0,2)))/eg; } elsif ($lin =~ s/^dword://i) { $typ = 'REG_DWORD 0x'; $lin =~ s/^0+(\d)/$1/; } else { $typ = '?'; } push(@$buf, " $nam $typ$lin"); } $lin = ''; } $slf->_close_utf16; } } # Delete the temporary file 1 while unlink($slf->{'_tmp'}); # Return the buffer $buf; } # Select the buffer sub _sel_buf { my ($slf, $key) = @_; my $ref = uc($key); foreach my $cur (sort {length($a) <=> length($b)} keys(%{$slf->{'_buf'}})) { return $slf->{'_buf'}->{$cur} if $cur eq substr($ref, 0, length($cur)); } return $slf->_load_buf($key); } sub _write_buf { my ($slf, $ctx, $key, $lvl) = @_; my ($buf, $cnt, $cod, $flg, $nam, $ofh, $reg, $toc, $typ, $val); # Write the registry key ($key, $reg) = _key_buf($key); $buf = $slf->_sel_buf($key); $cnt = 0; if (scalar(@$buf)) { # Get the output file handle $ofh = $ctx->get_handle; # Reformat the command output for (@$buf) { if (m/^[A-Z]/) { if (m/^$reg/i) { $flg = 1; } elsif ($flg) { last; } $toc = tr/\\/\\/; $toc = ($toc > $lvl) ? '+!!' : ''; print $ofh "\n" if $cod; print $ofh "\n---++$toc ``[$_]``\n"; ++$cnt; $cod = 0; } elsif ($flg && m/^\s+(.*\S)\s+REG_([A-Z_]+)\s+(.*)$/) { ($nam, $typ, $val) = ($1, $2, $3); print $ofh "\n" unless $cod++; $nam = '(Default)' if $nam eq ''; if ($typ =~ m/SZ$/) { print $ofh "$nam = \"$val\"\n"; } else { $typ = lc($typ); print $ofh "$nam = $typ:$val\n"; } ++$cnt; } } } print $ofh "\n" if $cod; # Return the number of lines written $cnt; } 1; __END__ =head1 SEE ALSO L, L, L, L =head1 COPYRIGHT NOTICE Copyright (c) 2002-2009, Oracle. All rights reserved. =head1 TRADEMARK NOTICE Oracle, JD Edwards, PeopleSoft, and Siebel are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. =cut