home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Struct.pm < prev    next >
Encoding:
Perl POD Document  |  2003-03-07  |  15.0 KB  |  529 lines

  1. package Win32::API::Struct;
  2.  
  3. # See the bottom of this file for the POD documentation.  Search for the
  4. # string '=head'.
  5.  
  6. #######################################################################
  7. #
  8. # Win32::API::Struct - Perl Win32 API struct Facility
  9. # Version: 0.40 
  10. # Date: 07 Mar 2003
  11. # Author: Aldo Calpini <dada@perl.it>
  12. # $Id: Struct.pm,v 1.0 2001/10/30 13:57:31 dada Exp $
  13. #######################################################################
  14.  
  15. $VERSION = "0.40";
  16.  
  17. use Win32::API::Type;
  18.  
  19. use Carp;
  20.  
  21. require Exporter;       # to export the constants to the main:: space
  22. require DynaLoader;     # to dynuhlode the module.
  23. @ISA = qw( Exporter DynaLoader );
  24.  
  25. my %Known = ();
  26.  
  27. sub DEBUG { 
  28.     if ($Win32::API::DEBUG) { 
  29.         printf @_ if @_ or return 1; 
  30.     } else {
  31.         return 0;
  32.     }
  33. }
  34.  
  35. sub typedef {
  36.     my $class = shift;
  37.     my $struct = shift;
  38.     my($type, $name);
  39.     my $self = {
  40.         align     => undef,
  41.         typedef => [],
  42.     };
  43.     while(defined($type = shift)) {
  44.         $name = shift;
  45.         $name =~ s/;$//;
  46.         push( @{ $self->{typedef} }, [ recognize($type, $name) ] );
  47.     }
  48.  
  49.     $Known{$struct} = $self;
  50.     return 1;
  51. }
  52.  
  53.  
  54. sub recognize {
  55.     my($type, $name) = @_;
  56.     my($size, $packing);
  57.     if(exists $Known{$type}) {
  58.         $packing = ">";
  59.         return $name, $packing, $type;
  60.     } else {
  61.         $packing = Win32::API::Type::packing($type);
  62.         return undef unless defined $packing;           
  63.         if($name =~ s/\[(.*)\]$//) {
  64.             $size = $1;     
  65.             $packing = $packing."*".$size;  
  66.         }
  67.         DEBUG "(PM)Struct::recognize got '$name', '$type' -> '$packing'\n";
  68.         return $name, $packing, $type;
  69.     }   
  70. }
  71.  
  72. sub new {
  73.     my $class = shift;
  74.     my($type, $name);
  75.     my $self = {
  76.         typedef => [],
  77.     };
  78.     if($#_ == 0) {
  79.         if(exists $Known{$_[0]}) {
  80.             DEBUG "(PM)Struct::new: got '$_[0]'\n";
  81.             $self->{typedef} = $Known{$_[0]}->{typedef};
  82.             foreach my $member (@{ $self->{typedef} }) {
  83.                 ($name, $packing, $type) = @$member;
  84.                 if($packing eq '>') {
  85.                     $self->{$name} = Win32::API::Struct->new($type);
  86.                 }
  87.             }
  88.             $self->{__typedef__} = $_[0];
  89.         } else {
  90.             carp "Unknown Win32::API::Struct '$_[0]'";
  91.             return undef;
  92.         }
  93.     } else {
  94.         while(defined($type = shift)) {
  95.             $name = shift;
  96.             # print "new: found member $name ($type)\n";
  97.             if(not exists $Win32::API::Type::Known{$type}) {
  98.                 warn "Unknown Win32::API::Struct type '$type'";
  99.                 return undef;
  100.             } else {
  101.                 push( @{ $self->{typedef} }, [ $name, $Win32::API::Type::Known{$type}, $type ] );
  102.             }
  103.         }
  104.     }
  105.     return bless $self;
  106. }
  107.  
  108. sub members {
  109.     my $self = shift;
  110.     return map {$_->[0]} @{ $self->{typedef} };
  111. }
  112.  
  113. sub sizeof {
  114.     my $self = shift;
  115.     my $size = 0;
  116.     my $align = 0;
  117.     my $first = undef;
  118.     foreach my $member (@{ $self->{typedef} }) {
  119.         my($name, $packing, $type) = @$member;
  120.         
  121.         if(ref( $self->{$name} ) eq "Win32::API::Struct") {
  122.             $size += $self->{$name}->sizeof();
  123.             # $align = $self->{$name}->sizeof() if $self->{$name}->sizeof() > $align;
  124.         } else {        
  125.             if($packing =~ /\w\*(\d+)/) {           
  126.                 $size += Win32::API::Type::sizeof($type) * $1;
  127.                 $first = Win32::API::Type::sizeof($type) * $1 unless defined $first;
  128.                 DEBUG "(PM)Struct::sizeof: sizeof with member($name) now = ". $size. "\n";
  129.             } else {            
  130.                 $size += Win32::API::Type::sizeof($type);
  131.                 $first = Win32::API::Type::sizeof($type) unless defined $first;
  132.                 $align = Win32::API::Type::sizeof($type)
  133.                     if Win32::API::Type::sizeof($type) > $align;                
  134.                 DEBUG "(PM)Struct::sizeof: sizeof with member($name) now = ". $size. "\n";
  135.             }
  136.         }
  137.     }
  138.     DEBUG "(PM)Struct::sizeof first=$first align=$align\n";
  139.     #DEBUG "(PM)Struct::sizeof returning %d\n", $first + (scalar(@{ $self->{typedef} })-1) * $align;    
  140.     #return $first + (scalar(@{ $self->{typedef} })-1) * $align;    
  141.     DEBUG "(PM)Struct::sizeof returning %d\n", scalar(@{ $self->{typedef} }) * $align;    
  142.     if(defined $align and $align > 0) {
  143.         return scalar(@{ $self->{typedef} }) * $align;
  144.     } else {
  145.         return $size;
  146.     }
  147.     return $size;
  148. }
  149.  
  150. sub align {
  151.     my $self = shift;
  152.     my $align = shift;
  153.     
  154.     if(not defined $align) {
  155.         return $self->{align} unless $self->{align} eq 'auto';
  156.         $align = 0;
  157.         foreach my $member (@{ $self->{typedef} }) {
  158.             my($name, $packing, $type) = @$member;
  159.  
  160.             if(ref( $self->{$name} ) eq "Win32::API::Struct") {
  161.                  #### ????
  162.             } else {        
  163.                 if($packing =~ /\w\*(\d+)/) {           
  164.                     #### ????
  165.                 } else {            
  166.                     $align = Win32::API::Type::sizeof($type)
  167.                         if Win32::API::Type::sizeof($type) > $align;                
  168.                 }
  169.             }
  170.         }
  171.         return $align;    
  172.     } else {
  173.         $self->{align} = $align;    
  174.     
  175.     }
  176. }
  177.  
  178. sub getPack {
  179.     my $self = shift;
  180.     my $packing = "";
  181.     my($type, $name);
  182.     my @items = ();
  183.     my @recipients = ();    
  184.     
  185.     my $align = $self->align();
  186.     
  187.     foreach my $member (@{ $self->{typedef} }) {
  188.         ($name, $type, $orig) = @$member;
  189.         if($type eq '>') {
  190.             my($subpacking, $subitems, $subrecipients) = $self->{$name}->getPack();
  191.             
  192.             DEBUG "(PM)Struct::getPack($self->{__typedef__}) ++ $subpacking\n";
  193.             
  194.             $packing .= $subpacking;
  195.             push(@items, @$subitems);
  196.             push(@recipients, @$subrecipients);
  197.         } else {
  198.             if($type =~ /\w\*(\d+)/) {
  199.                 my $size = $1;
  200.                 $type = "a$size";
  201.             }
  202.             
  203.             DEBUG "(PM)Struct::getPack($self->{__typedef__}) ++ $type\n"; 
  204.             
  205.             if($type eq 'p') {
  206.                 $type = "L";
  207.                 push(@items, Win32::API::PointerTo($self->{$name}));
  208.             } else {
  209.                 push(@items, $self->{$name});
  210.             }
  211.             $packing .= $type;
  212.             
  213.             if($Win32::API::Type::PackSize{$type} < $align) {
  214.                 $packing .= ("x" x ($align - $Win32::API::Type::PackSize{$type}));
  215.             }
  216.             
  217.             push(@recipients, $self);
  218.         }
  219.     }
  220.     DEBUG "(PM)Struct::getPack: $self->{__typedef__}(buffer) = pack($packing, @items)\n";
  221.     return($packing, [@items], [@recipients]);
  222. }
  223.     
  224.  
  225. sub Pack {
  226.     my $self = shift;
  227.     my($packing, $items, $recipients) = $self->getPack();
  228.     DEBUG "(PM)Struct::Pack: $self->{__typedef__}(buffer) = pack($packing, @$items)\n";
  229.     $self->{buffer} = pack($packing, @$items);
  230.     if(DEBUG) {
  231.         for my $i (0..$self->sizeof-1) {
  232.             printf "    %3d: 0x%02x\n", $i, ord(substr($self->{buffer}, $i, 1));
  233.         }
  234.     }
  235.     $self->{buffer_recipients} = $recipients
  236. }
  237.  
  238. sub getUnpack {
  239.     my $self = shift;
  240.     my $packing = "";
  241.     my($type, $name);
  242.     my @items = ();
  243.     my $align = $self->align();
  244.     foreach my $member (@{ $self->{typedef} }) {
  245.         ($name, $type, $orig) = @$member;
  246.         if($type eq '>') {
  247.             my($subpacking, @subitems) = $self->{$name}->getUnpack();
  248.             
  249.             DEBUG "(PM)Struct::getUnpack($self->{__typedef__}) ++ $subpacking\n";
  250.             $packing .= $subpacking;
  251.             
  252.             
  253.             push(@items, @subitems);
  254.         } else {
  255.             if($type =~ /\w\*(\d+)/) {
  256.                 my $size = $1;
  257.                 $type = "Z$size";
  258.             }
  259.           
  260.               #if($type eq 'p') {
  261.             #    $packing .= 'Z*';
  262.             #    DEBUG "(PM)Struct::getUnpack($self->{__typedef__}) ++ Z*\n";
  263.             #} else {
  264.                 $packing .= $type;
  265.                 DEBUG "(PM)Struct::getUnpack($self->{__typedef__}) ++ $type\n";
  266.             #}
  267.             if($type ne 'p' and $type !~ /^Z(\d+)/ and $Win32::API::Type::PackSize{$type} < $align) {
  268.                 DEBUG "(PM)Struct::getUnpack %s(%d) < %d\n",
  269.                     $type, $Win32::API::Type::PackSize{$type}, $align
  270.                 ;
  271.                 $packing .= ("x" x ($align - $Win32::API::Type::PackSize{$type}));
  272.             }
  273.  
  274.             push(@items, $name);
  275.         }
  276.     }
  277.     DEBUG "(PM)Struct::getUnpack($self->{__typedef__}): unpack($packing, @items)\n";
  278.     return($packing, @items);
  279. }
  280.  
  281. sub Unpack {
  282.     my $self = shift;
  283.     my($packing, @items) = $self->getUnpack();
  284.     my @itemvalue = unpack($packing, $self->{buffer});
  285.     DEBUG "(PM)Struct::Unpack: unpack($packing, buffer) = @itemvalue\n";
  286.     foreach my $i (0..$#items) {
  287.         my $recipient = $self->{buffer_recipients}->[$i];
  288.         DEBUG "(PM)Struct::Unpack: %s(%s) = '%s' (0x%08x)\n",
  289.             $recipient->{__typedef__},
  290.             $items[$i],
  291.             $itemvalue[$i],
  292.             $itemvalue[$i],
  293.         ;       
  294.         $recipient->{$items[$i]} = $itemvalue[$i];
  295.         DEBUG "(PM)Struct::Unpack: self.$items[$i] = $self->{$items[$i]}\n";        
  296.     }
  297. }
  298.  
  299. sub FromMemory {
  300.     my($self, $addr) = @_;
  301.     DEBUG "(PM)Struct::FromMemory: doing Pack\n";
  302.     $self->Pack();
  303.     DEBUG "(PM)Struct::FromMemory: doing GetMemory( 0x%08x, %d )\n", $addr, $self->sizeof;
  304.     $self->{buffer} = Win32::API::ReadMemory( $addr, $self->sizeof );
  305.     $self->Unpack();
  306.     DEBUG "(PM)Struct::FromMemory: doing Unpack\n";
  307.     DEBUG "(PM)Struct::FromMemory: structure is now:\n";
  308.     $self->Dump() if DEBUG;
  309.     DEBUG "\n";
  310. }
  311.  
  312. sub Dump {
  313.     my $self = shift;
  314.     my $prefix = shift;
  315.     foreach my $member (@{ $self->{typedef} }) {
  316.         ($name, $packing, $type) = @$member;
  317.         if( ref($self->{$name}) ) {
  318.             $self->{$name}->Dump($name);
  319.         } else {        
  320.             printf "%-20s %-20s %-20s\n", $prefix, $name, $self->{$name};
  321.         }
  322.     }
  323. }   
  324.  
  325.  
  326. sub is_known {
  327.     my $name = shift;
  328.     if(exists $Known{ $name }) {
  329.         return 1;
  330.     } else {
  331.         if($name =~ s/^LP//) {
  332.             return exists $Known{ $name };
  333.         }
  334.         return 0;
  335.     }
  336. }
  337.  
  338. sub TIEHASH {
  339.     return Win32::API::Struct::new(@_);
  340. }
  341.  
  342. sub EXISTS {
  343.  
  344. }
  345.  
  346. sub FETCH {
  347.     my $self = shift;
  348.     my $key = shift;
  349.     
  350.     if($key eq 'sizeof') {
  351.         return $self->sizeof;
  352.     }
  353.     my @members = map { $_->[0] } @{ $self->{typedef} };
  354.     if(grep(/^\Q$key\E$/, @members)) {
  355.         return $self->{$key};
  356.     } else {
  357.         warn "'$key' is not a member of Win32::API::Struct $self->{__typedef__}";
  358.     }   
  359. }
  360.  
  361. sub STORE {
  362.     my $self = shift;
  363.     my($key, $val) = @_;
  364.     my @members = map { $_->[0] } @{ $self->{typedef} };
  365.     if(grep(/^\Q$key\E$/, @members)) {
  366.         $self->{$key} = $val;
  367.     } else {
  368.         warn "'$key' is not a member of Win32::API::Struct $self->{__typedef__}";
  369.     }
  370. }
  371.  
  372. sub FIRSTKEY {
  373.     my $self = shift;
  374.     my @members = map { $_->[0] } @{ $self->{typedef} };
  375.     return $members[0]; 
  376. }
  377.  
  378. sub NEXTKEY {
  379.     my $self = shift;
  380.     my $key = shift;
  381.     my @members = map { $_->[0] } @{ $self->{typedef} };
  382.     for my $i (0..$#members-1) {    
  383.         return $members[$i+1] if $members[$i] eq $key;
  384.     }
  385.     return undef;
  386. }
  387.  
  388. 1;
  389.  
  390. #######################################################################
  391. # DOCUMENTATION
  392. #
  393.  
  394. =head1 NAME
  395.  
  396. Win32::API::Struct - C struct support package for Win32::API
  397.  
  398. =head1 SYNOPSIS
  399.  
  400.   use Win32::API;
  401.   
  402.   Win32::API::Struct->typedef( 'POINT', qw(
  403.     LONG x; 
  404.     LONG y; 
  405.   ));
  406.   
  407.   my $Point = Win32::API::Struct->new( 'POINT' ); 
  408.   $Point->{x} = 1024;
  409.   $Point->{y} = 768;
  410.  
  411.   #### alternatively
  412.   
  413.   tie %Point, 'Win32::API::Struct', 'POINT';
  414.   $Point{x} = 1024;
  415.   $Point{y} = 768;
  416.  
  417.  
  418. =head1 ABSTRACT
  419.  
  420. This module enables you to define C structs for use with
  421. Win32::API. 
  422.  
  423. See L<Win32::API> for more info about its usage.
  424.  
  425. =head1 DESCRIPTION
  426.  
  427. This module is automatically imported by Win32::API, so you don't 
  428. need to 'use' it explicitly. The main methods are C<typedef> and
  429. C<new>, which are documented below.
  430.  
  431. =over 4
  432.  
  433. =item C<typedef NAME, TYPE, MEMBER, TYPE, MEMBER, ...>
  434.  
  435. This method defines a structure named C<NAME>. The definition consists
  436. of types and member names, just like in C. In fact, most of the
  437. times you can cut the C definition for a structure and paste it
  438. verbatim to your script, enclosing it in a C<qw()> block. The 
  439. function takes care of removing the semicolon after the member
  440. name.
  441.  
  442. The synopsis example could be written like this:
  443.  
  444.   Win32::API::Struct->typedef('POINT', 'LONG', 'x', 'LONG', 'y');
  445.  
  446. But it could also be written like this (note the indirect object
  447. syntax), which is pretty cool:
  448.  
  449.   typedef Win32::API::Struct POINT => qw{
  450.     LONG x; 
  451.     LONG y; 
  452.   };
  453.  
  454. Also note that C<typedef> automatically defines an 'LPNAME' type,
  455. which holds a pointer to your structure. In the example above,
  456. 'LPPOINT' is also defined and can be used in a call to a 
  457. Win32::API (in fact, this is what you're really going to use when
  458. doing API calls).
  459.  
  460. =item C<new NAME>
  461.  
  462. This creates a structure (a Win32::API::Struct object) of the
  463. type C<NAME> (it must have been defined with C<typedef>). In Perl,
  464. when you create a structure, all the members are undefined. But
  465. when you use that structure in C (eg. a Win32::API call), you
  466. can safely assume that they will be treated as zero (or NULL).
  467.  
  468. =item C<sizeof>
  469.  
  470. This returns the size, in bytes, of the structure. Acts just like
  471. the C function of the same name. It is particularly useful for 
  472. structures that need a member to be initialized to the structure's
  473. own size.
  474.  
  475. =item C<align [SIZE]>
  476.  
  477. Sets or returns the structure alignment (eg. how the structure is
  478. stored in memory). This is a very advanced option, and you normally 
  479. don't need to mess with it. 
  480. All structures in the Win32 Platform SDK should work without it. 
  481. But if you define your own structure, you may need to give it an 
  482. explicit alignment. In most cases, passing a C<SIZE> of 'auto' 
  483. should keep the world happy.
  484.  
  485. =back
  486.  
  487. =head2 THE C<tie> INTERFACE
  488.  
  489. Instead of creating an object with the C<new> method, you can
  490. tie a hash, which will hold the desired structure, using the 
  491. C<tie> builtin function:
  492.  
  493.   tie %structure, Win32::API::Struct => 'NAME';
  494.  
  495. The differences between the tied and non-tied approaches are:
  496.  
  497. =over 4
  498.  
  499. =item *
  500. with tied structures, you can access members directly as
  501. hash lookups, eg.
  502.  
  503.   # tied              # non-tied
  504.   $Point{x}    vs.    $Point->{x}
  505.  
  506. =item *
  507. with tied structures, when you try to fetch or store a
  508. member that is not part of the structure, it will result
  509. in a warning, eg.
  510.  
  511.   print $Point{z};
  512.   # this will warn: 'z' is not a member of Win32::API::Struct POINT
  513.  
  514. =item *
  515. when you pass a tied structure as a Win32::API parameter, 
  516. remember to backslash it, eg.
  517.  
  518.   # tied                            # non-tied
  519.   GetCursorPos( \%Point )    vs.    GetCursorPos( $Point )
  520.  
  521. =back
  522.  
  523. =head1 AUTHOR
  524.  
  525. Aldo Calpini ( I<dada@perl.it> ).
  526.  
  527. =cut
  528.