| 1 | package Pointer; | ||
| 2 | use Spiffy 0.23 -Base; | ||
| 3 | use Carp; | ||
| 4 | use overload qw("" stringify + add - subtract); | ||
| 5 | use Config; | ||
| 6 | our $VERSION = '0.11'; | ||
| 7 | our @EXPORT = qw(pointer); | ||
| 8 | |||
| 9 | use constant POINTER => 'L!'; # XXX not totally portable | ||
| 10 | |||
| 11 | const type => 'void'; | ||
| 12 | const sizeof => 1; | ||
| 13 | const pack_template => 'b'; | ||
| 14 | field address => undef; | ||
| 15 | |||
| 16 | sub pointer() { | ||
| 17 | __PACKAGE__->new(@_); | ||
| 18 | } | ||
| 19 | |||
| 20 | my $type_map = {}; | ||
| 21 | sub get_class { | ||
| 22 | my $type = shift; | ||
| 23 | return $type_map->{$type} if defined $type_map->{$type}; | ||
| 24 | $type_map = {}; | ||
| 25 | for my $class (keys %INC) { | ||
| 26 | $class =~ s|/|::|g; | ||
| 27 | $class =~ s/\.pm$//; | ||
| 28 | next unless $class->isa('Pointer'); | ||
| 29 | my $class_type = $class->type; | ||
| 30 | if (not $type_map->{$class_type} or | ||
| 31 | $class->isa($type_map->{$class_type}) | ||
| 32 | ) { | ||
| 33 | $type_map->{$class_type} = $class; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | return $type_map->{$type} if defined $type_map->{$type}; | ||
| 37 | croak "No class to handle pointer type '$type'"; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub new { | ||
| 41 | my ($args, @values) = $self->parse_arguments(@_); | ||
| 42 | my ($type, $address) = @values; | ||
| 43 | $type ||= 'void'; | ||
| 44 | my $real_class = $self->get_class($type); | ||
| 45 | $self = bless {}, $real_class; | ||
| 46 | $self->address($address); | ||
| 47 | return $self; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub stringify { | ||
| 51 | overload::StrVal($self); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub add { | ||
| 55 | my $number = shift; | ||
| 56 | croak "Invalid pointer addition" if ref $number; | ||
| 57 | my $result = Pointer->new($self->type); | ||
| 58 | $result->address($self->address + $number * $self->sizeof); | ||
| 59 | return $result; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub subtract { | ||
| 63 | my ($number, $reverse) = @_; | ||
| 64 | croak "Invalid pointer subtraction" if $reverse or ref $number; | ||
| 65 | my $result = Pointer->new($self->type); | ||
| 66 | $result->address($self->address - $number * $self->sizeof); | ||
| 67 | return $result; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub ptrsize { | ||
| 71 | $Config::Config{ptrsize}; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub to { | ||
| 75 | my $address = shift; | ||
| 76 | $self->address($address); | ||
| 77 | return $self; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub of_scalar { | ||
| 81 | $self->address(hex $self->scalar_id($_[0])); | ||
| 82 | return $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub hex_address { | ||
| 86 | my $address = $self->assert_address; | ||
| 87 | sprintf '0x%x', $address; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub assert_pointer { | ||
| 91 | return pack(POINTER, $self->assert_address); | ||
| 92 | } | ||
| 93 | |||
| 94 | sub assert_address { | ||
| 95 | my $address = $self->address; | ||
| 96 | croak "Undefined pointer" | ||
| 97 | unless defined $address; | ||
| 98 | return $address; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub reverse_bytes { | ||
| 102 | local $_ = shift; | ||
| 103 | join '', reverse split; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub get { | ||
| 107 | my $count = shift || 1; | ||
| 108 | my $length = $count * $self->sizeof; | ||
| 109 | unpack $self->pack_template . $count, $self->get_raw($length); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub get_raw { | ||
| 113 | my $length = shift || 1; | ||
| 114 | unpack "P$length", $self->assert_pointer; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub get_hex { | ||
| 118 | my $count = shift || 1; | ||
| 119 | my $sizeof = $self->sizeof; | ||
| 120 | my $length = $count * $sizeof; | ||
| 121 | my @values = map { | ||
| 122 | unpack 'H' . $sizeof * 2, $_ | ||
| 123 | } $self->get_raw($length) =~ /(.{$sizeof})/g; | ||
| 124 | return wantarray ? @values : $values[0]; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub get_pointer { | ||
| 128 | my $pointer = Pointer->new(@_); | ||
| 129 | $pointer->address(unpack POINTER, $self->get_raw($self->ptrsize)); | ||
| 130 | return $pointer; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub get_string { | ||
| 134 | unpack 'p*', $self->assert_pointer; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub scalar_id { | ||
| 138 | return | ||
| 139 | overload::StrVal(\ $_[0]) =~ qr{^(?:.*\=)?[^=]*\(([^\(]*)\)$}o | ||
| 140 | ? $1 | ||
| 141 | : croak "Can't find id of scalar"; | ||
| 142 | } | ||
| 143 | |||
| 144 | __END__ |