| 1 | |||
| 2 | package XPT::File; | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use base qw(XPT); | ||
| 8 | |||
| 9 | use Carp; | ||
| 10 | |||
| 11 | use constant MAGIC => "XPCOM\nTypeLib\r\n\032"; | ||
| 12 | |||
| 13 | sub demarshal { | ||
| 14 | my ($r_buffer, $r_offset) = @_; | ||
| 15 | |||
| 16 | my $magic = XPT::ReadBuffer($r_buffer, $r_offset, length(MAGIC)); | ||
| 17 | die "libxpt: bad magic header in input file; found '",$magic,"', expected '",MAGIC,"'\n" | ||
| 18 | unless ($magic eq MAGIC); | ||
| 19 | |||
| 20 | my $major_version = XPT::Read8($r_buffer, $r_offset); | ||
| 21 | my $minor_version = XPT::Read8($r_buffer, $r_offset); | ||
| 22 | die "libxpt: newer version ",$major_version,".",$minor_version,"\n" | ||
| 23 | unless ($major_version == 1); | ||
| 24 | |||
| 25 | my $num_interfaces = XPT::Read16($r_buffer, $r_offset); | ||
| 26 | my $file_length = XPT::Read32($r_buffer, $r_offset); | ||
| 27 | |||
| 28 | die "libxpt: File length in header does not match actual length. File may be corrupt\n" | ||
| 29 | if ($file_length != length $$r_buffer); | ||
| 30 | |||
| 31 | my $interface_directory_offset = XPT::Read32($r_buffer, $r_offset); | ||
| 32 | $XPT::data_pool_offset = XPT::Read32($r_buffer, $r_offset); | ||
| 33 | |||
| 34 | my @annotations = (); | ||
| 35 | my %interface_iid = (); | ||
| 36 | my %interface_iid_nul = (); | ||
| 37 | eval { | ||
| 38 | my $annotation = XPT::Annotation::demarshal($r_buffer, $r_offset); | ||
| 39 | push @annotations, $annotation; | ||
| 40 | while (!$annotation->{is_last}) { | ||
| 41 | $annotation = XPT::Annotation::demarshal($r_buffer, $r_offset); | ||
| 42 | push @annotations, $annotation; | ||
| 43 | } | ||
| 44 | |||
| 45 | my $offset = $interface_directory_offset - 1; | ||
| 46 | while ($num_interfaces --) { | ||
| 47 | my $entry = XPT::InterfaceDirectoryEntry::demarshal($r_buffer, \$offset); | ||
| 48 | if ($entry->{iid}->_is_nul()) { | ||
| 49 | my $fullname = ($entry->{name_space} || q{}) . '::' . $entry->{name}; | ||
| 50 | $interface_iid_nul{$fullname} = $entry; | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | $interface_iid{$entry->{iid}->stringify()} = $entry; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | }; | ||
| 57 | if ($@) { | ||
| 58 | $XPT::demarshal_retcode = 1; | ||
| 59 | if ($XPT::demarshal_not_abort) { | ||
| 60 | warn $@; | ||
| 61 | } | ||
| 62 | else { | ||
| 63 | die $@; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | return new XPT::File( | ||
| 68 | magic => $magic, | ||
| 69 | major_version => $major_version, | ||
| 70 | minor_version => $minor_version, | ||
| 71 | interface_iid_nul => \%interface_iid_nul, | ||
| 72 | interface_iid => \%interface_iid, | ||
| 73 | annotations => \@annotations, | ||
| 74 | file_length => $file_length, | ||
| 75 | data_pool_offset => $XPT::data_pool_offset, | ||
| 76 | )->_revolve(); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _interface_directory { | ||
| 80 | my $self = shift; | ||
| 81 | my @list = (); | ||
| 82 | foreach (sort keys %{$self->{interface_iid_nul}}) { | ||
| 83 | my $entry = $self->{interface_iid_nul}->{$_}; | ||
| 84 | push @list, $entry; | ||
| 85 | } | ||
| 86 | foreach (sort keys %{$self->{interface_iid}}) { | ||
| 87 | my $entry = $self->{interface_iid}->{$_}; | ||
| 88 | push @list, $entry; | ||
| 89 | } | ||
| 90 | return @list; | ||
| 91 | } | ||
| 92 | |||
| 93 | sub _revolve { | ||
| 94 | my $self = shift; | ||
| 95 | my @interface_directory = $self->_interface_directory(); | ||
| 96 | foreach my $itf (values %{$self->{interface_iid}}) { | ||
| 97 | next unless (defined $itf->{interface_descriptor}); # ISupport | ||
| 98 | my $desc = $itf->{interface_descriptor}; | ||
| 99 | my $idx_parent = $desc->{parent_interface_index}; | ||
| 100 | if ($idx_parent) { | ||
| 101 | if ($idx_parent > scalar(@interface_directory)) { | ||
| 102 | warn "parent_interface_index out of range! ($idx_parent)\n"; | ||
| 103 | $XPT::demarshal_retcode = 1; | ||
| 104 | } | ||
| 105 | $desc->{parent_interface} = $interface_directory[$idx_parent - 1]; | ||
| 106 | } | ||
| 107 | foreach my $method (@{$desc->{method_descriptors}}) { | ||
| 108 | foreach my $param (@{$method->{params}}) { | ||
| 109 | my $type = $param->{type}; | ||
| 110 | if ($type->{tag} == XPT::InterfaceTypeDescriptor) { | ||
| 111 | my $idx = $type->{interface_index}; | ||
| 112 | if ($idx > scalar(@interface_directory)) { | ||
| 113 | warn "interface_index out of range! ($idx)\n"; | ||
| 114 | next; | ||
| 115 | } | ||
| 116 | $type->{interface} = $interface_directory[$idx - 1]; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | return $self; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub marshal { | ||
| 125 | my $self = shift; | ||
| 126 | |||
| 127 | my $header_size = length(MAGIC) + 1 + 1 + 2 + 4 + 4 + 4; | ||
| 128 | my $annotations = q{}; | ||
| 129 | foreach (@{$self->{annotations}}) { | ||
| 130 | $annotations .= $_->marshal(); | ||
| 131 | } | ||
| 132 | # while ( ($header_size + length($annotations)) % 4) { | ||
| 133 | # $annotations .= "\0"; | ||
| 134 | # } | ||
| 135 | $header_size += length($annotations); | ||
| 136 | my $interface_directory_offset = $header_size + 1; | ||
| 137 | my @interface_directory = $self->_interface_directory(); | ||
| 138 | $XPT::data_pool = q{}; | ||
| 139 | my $interface_directory = q{}; | ||
| 140 | foreach (@interface_directory) { | ||
| 141 | $interface_directory .= $_->marshal(); | ||
| 142 | } | ||
| 143 | |||
| 144 | my $data_pool_offset = $header_size + length($interface_directory); | ||
| 145 | my $file_length = $header_size + length($interface_directory) + length($XPT::data_pool); | ||
| 146 | my $buffer = $self->{magic}; | ||
| 147 | $buffer .= XPT::Write8($self->{major_version}); | ||
| 148 | $buffer .= XPT::Write8($self->{minor_version}); | ||
| 149 | $buffer .= XPT::Write16(scalar(@interface_directory)); | ||
| 150 | $buffer .= XPT::Write32($file_length); | ||
| 151 | $buffer .= XPT::Write32($interface_directory_offset); | ||
| 152 | $buffer .= XPT::Write32($data_pool_offset); | ||
| 153 | $buffer .= $annotations; | ||
| 154 | $buffer .= $interface_directory; | ||
| 155 | $buffer .= $XPT::data_pool; | ||
| 156 | return $buffer; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub stringify { | ||
| 160 | my $self = shift; | ||
| 161 | my ($indent) = @_; | ||
| 162 | $indent = q{} unless (defined $indent); | ||
| 163 | my $new_indent = $indent . q{ } x 3; | ||
| 164 | my $more_indent = $new_indent . q{ } x 3; | ||
| 165 | |||
| 166 | my @interface_directory = $self->_interface_directory(); | ||
| 167 | my $str = $indent . "Header:\n"; | ||
| 168 | if ($XPT::stringify_verbose) { | ||
| 169 | $str .= $new_indent . "Magic beans: "; | ||
| 170 | foreach (split //, $self->{magic}) { | ||
| 171 | $str .= sprintf("%02x", ord($_)); | ||
| 172 | } | ||
| 173 | $str .= "\n"; | ||
| 174 | if ($self->{magic} eq MAGIC) { | ||
| 175 | $str .= $new_indent . " PASSED\n"; | ||
| 176 | } | ||
| 177 | else { | ||
| 178 | $str .= $new_indent . " FAILED\n"; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | $str .= $new_indent . "Major version: " . $self->{major_version} . "\n"; | ||
| 182 | $str .= $new_indent . "Minor version: " . $self->{minor_version} . "\n"; | ||
| 183 | $str .= $new_indent . "Number of interfaces: " . scalar(@interface_directory) . "\n"; | ||
| 184 | if ($XPT::stringify_verbose) { | ||
| 185 | $str .= $new_indent . "File length: " . $self->{file_length} . "\n" | ||
| 186 | if (exists $self->{file_length}); | ||
| 187 | $str .= $new_indent . "Data pool offset: " . $self->{data_pool_offset} . "\n" | ||
| 188 | if (exists $self->{data_pool_offset}); | ||
| 189 | $str .= "\n"; | ||
| 190 | } | ||
| 191 | |||
| 192 | my $nb = -1; | ||
| 193 | $str .= $new_indent . "Annotations:\n"; | ||
| 194 | foreach (@{$self->{annotations}}) { | ||
| 195 | $nb ++; | ||
| 196 | $str .= $more_indent . "Annotation #" . $nb; | ||
| 197 | $str .= $_->stringify($new_indent); | ||
| 198 | } | ||
| 199 | if ($XPT::stringify_verbose) { | ||
| 200 | $str .= $more_indent . "Annotation #" . $nb . " is the last annotation.\n"; | ||
| 201 | } | ||
| 202 | |||
| 203 | $XPT::param_problems = 0; | ||
| 204 | $nb = 0; | ||
| 205 | $str .= "\n"; | ||
| 206 | $str .= $indent . "Interface Directory:\n"; | ||
| 207 | foreach my $entry (@interface_directory) { | ||
| 208 | if ($XPT::stringify_verbose) { | ||
| 209 | $str .= $new_indent . "Interface #" . $nb ++ . ":\n"; | ||
| 210 | $str .= $entry->stringify($new_indent . $new_indent, $self); | ||
| 211 | } | ||
| 212 | else { | ||
| 213 | $str .= $entry->stringify($new_indent, $self); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | if ($XPT::param_problems) { | ||
| 217 | $str .= "\nWARNING: ParamDescriptors are present with " | ||
| 218 | . "bad in/out/retval flag information.\n" | ||
| 219 | . "These have been marked with 'XXX'.\n" | ||
| 220 | . "Remember, retval params should always be marked as out!\n"; | ||
| 221 | } | ||
| 222 | |||
| 223 | return $str; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub add_annotation { | ||
| 227 | my $self = shift; | ||
| 228 | my ($annotation) = @_; | ||
| 229 | $annotation->{is_last} = 0; | ||
| 230 | $self->{annotations} = [] unless (exists $self->{annotations}); | ||
| 231 | push @{$self->{annotations}}, $annotation; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub terminate_annotations { | ||
| 235 | my $self = shift; | ||
| 236 | if (exists $self->{annotations}) { | ||
| 237 | ${$self->{annotations}}[-1]->{is_last} = 1; | ||
| 238 | } | ||
| 239 | else { | ||
| 240 | my $annotation = new XPT::Annotation( | ||
| 241 | is_last => 1, | ||
| 242 | tag => 0, | ||
| 243 | ); | ||
| 244 | $self->{annotations} = [ $annotation ]; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | sub add_interface { | ||
| 249 | my $self = shift; | ||
| 250 | my ($entry) = @_; | ||
| 251 | $self->{interface_iid_nul} = {} unless (exists $self->{interface_iid_nul}); | ||
| 252 | $self->{interface_iid} = {} unless (exists $self->{interface_iid}); | ||
| 253 | my $fullname = $entry->{name_space} . '::' . $entry->{name}; | ||
| 254 | if ($entry->{iid}->_is_nul()) { | ||
| 255 | return if (exists $self->{interface_iid_nul}->{$fullname}); | ||
| 256 | foreach (values %{$self->{interface_iid}}) { | ||
| 257 | return if ($fullname eq $_->{name_space} . '::' . $_->{name}); | ||
| 258 | } | ||
| 259 | $self->{interface_iid_nul}->{$fullname} = $entry; | ||
| 260 | } | ||
| 261 | else { | ||
| 262 | my $iid = $entry->{iid}->stringify(); | ||
| 263 | if (exists $self->{interface_iid}->{$iid}) { | ||
| 264 | return if (defined $self->{interface_iid}->{$iid}->{interface_descriptor}); | ||
| 265 | } | ||
| 266 | else { | ||
| 267 | delete $self->{interface_iid_nul}->{$fullname} | ||
| 268 | if (exists $self->{interface_iid_nul}->{$fullname}); | ||
| 269 | foreach (values %{$self->{interface_iid}}) { | ||
| 270 | croak "ERROR: found duplicate definition of interface $fullname with iids \n" | ||
| 271 | if ($fullname eq $_->{name_space} . '::' . $_->{name}); | ||
| 272 | } | ||
| 273 | } | ||
| 274 | $self->{interface_iid}->{$iid} = $entry; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | sub indexe { | ||
| 279 | my $self = shift; | ||
| 280 | foreach my $itf (values %{$self->{interface_iid}}) { | ||
| 281 | next unless (defined $itf->{interface_descriptor}); # ISupport | ||
| 282 | my $desc = $itf->{interface_descriptor}; | ||
| 283 | $desc->{parent_interface_index} = $self->_find_itf($desc->{parent_interface}); | ||
| 284 | foreach my $method (@{$desc->{method_descriptors}}) { | ||
| 285 | foreach my $param (@{$method->{params}}) { | ||
| 286 | my $type = $param->{type}; | ||
| 287 | if ($type->{tag} == XPT::InterfaceTypeDescriptor) { | ||
| 288 | $type->{interface_index} = $self->_find_itf($type->{interface}); | ||
| 289 | } | ||
| 290 | } | ||
| 291 | } | ||
| 292 | } | ||
| 293 | } | ||
| 294 | |||
| 295 | sub _find_itf { | ||
| 296 | my $self = shift; | ||
| 297 | my ($itf) = @_; | ||
| 298 | return 0 unless (defined $itf); | ||
| 299 | my @interface_directory = $self->_interface_directory(); | ||
| 300 | my $idx = 1; | ||
| 301 | foreach (@interface_directory) { | ||
| 302 | if (ref $itf) { | ||
| 303 | if ( $_->{name_space} eq $itf->{name_space} | ||
| 304 | and $_->{name} eq $itf->{name} ) { | ||
| 305 | return $idx; | ||
| 306 | } | ||
| 307 | } | ||
| 308 | else { | ||
| 309 | if ($itf eq $_->{name_space} . '::' . $_->{name}) { | ||
| 310 | return $idx; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | $idx ++; | ||
| 314 | } | ||
| 315 | if (ref $itf) { | ||
| 316 | croak "ERROR: interface $itf->{name_space}::$itf->{name} not found\n"; | ||
| 317 | } | ||
| 318 | else { | ||
| 319 | croak "ERROR: interface $itf not found\n"; | ||
| 320 | } | ||
| 321 | } | ||
| 322 | |||
| 323 | 1; | ||
| 324 |