| 1 | ##################################################################### | ||
| 2 | # Library Include.pm | ||
| 3 | # Title C::Include | ||
| 4 | # Version 1.30 | ||
| 5 | # Author Albert N. MICHEEV [Albert@f80.n5049.z2.fidonet.org] | ||
| 6 | # Description Miscelaneous routines for working with C/C++ like | ||
| 7 | # header files as native Perl deep structs. | ||
| 8 | ##################################################################### | ||
| 9 | # REVISION HISTORY | ||
| 10 | # | ||
| 11 | # 1.00 (01 Mar 2000) First pilot variant | ||
| 12 | # | ||
| 13 | # 1.10 (21 Apr 2000) Extended module call method & added func INC | ||
| 14 | # use C::Include qw(headers/config.h -cache); | ||
| 15 | # Fixed for compatibility with Perl v5.6 | ||
| 16 | # | ||
| 17 | # 1.20 (04 Jun 2000) Fixed for compatibility with ++ 8-) | ||
| 18 | # | ||
| 19 | # 1.30 (24 Jun 2000) make_struct modificated | ||
| 20 | # | ||
| 21 | # 1.40 (30 May 2001) allowed anonymous enum | ||
| 22 | # allowed synonym defining via 'typedef' keyword | ||
| 23 | |||
| 24 | package C::Include::Struct; | ||
| 25 | package C::Include; | ||
| 26 | |||
| 27 | use 5.005; | ||
| 28 | use strict; | ||
| 29 | use IO::File; | ||
| 30 | use Storable 0.6.7.1; | ||
| 31 | use vars qw/$VERSION %TYPES %ALIASES %INC $TEST/; | ||
| 32 | |||
| 33 | $VERSION = 1.40; | ||
| 34 | |||
| 35 | $TEST = 0; | ||
| 36 | |||
| 37 | %TYPES = ( | ||
| 38 | 'string', ['a',1], | ||
| 39 | 'sstring', ['A',1], | ||
| 40 | 'zstring', ['Z',1], | ||
| 41 | 'char', ['c',1], | ||
| 42 | 'unsigned char', ['C',1], | ||
| 43 | 'short', ['s',2], | ||
| 44 | 'unsigned short', ['S',2], | ||
| 45 | 'int', ['i',4], | ||
| 46 | 'unsigned int', ['I',4], | ||
| 47 | 'long', ['l',4], | ||
| 48 | 'unsigned long', ['L',4], | ||
| 49 | 'long long', ['q',8], | ||
| 50 | 'unsigned long long', ['Q',8], | ||
| 51 | 'float', ['f',4], | ||
| 52 | 'double', ['d',8], | ||
| 53 | 'pointer', ['p',4], | ||
| 54 | 'nullbyte', ['x',1], | ||
| 55 | 'neganull', ['X',1] | ||
| 56 | ); | ||
| 57 | |||
| 58 | %ALIASES = ( | ||
| 59 | 'short int' => 'short', | ||
| 60 | 'long int' => 'long', | ||
| 61 | 'long long int' => 'long long', | ||
| 62 | 'unsigned' => 'unsigned long', | ||
| 63 | 'unsigned long int' => 'unsigned long', | ||
| 64 | 'unsigned short int' => 'unsigned short', | ||
| 65 | 'unsigned long long int'=> 'unsigned long long', | ||
| 66 | 'byte' => 'unsigned char', | ||
| 67 | 'word' => 'unsigned short', | ||
| 68 | 'dword' => 'unsigned long', | ||
| 69 | ); | ||
| 70 | |||
| 71 | |||
| 72 | sub new { | ||
| 73 | my $class = shift; | ||
| 74 | my $self = {}; | ||
| 75 | my %param; | ||
| 76 | |||
| 77 | # Params processing | ||
| 78 | { | ||
| 79 | local $_; my $param; | ||
| 80 | |||
| 81 | while( $_ = shift ){ | ||
| 82 | $param{ $param=$1 } = '', next if /^\-(.+)$/; | ||
| 83 | |||
| 84 | $param = 'src' unless $param; | ||
| 85 | die "Invalid parametr: '$param'" if $param ne 'src' and $param ne 'cache'; | ||
| 86 | $param{$param} = $_; | ||
| 87 | undef $param; | ||
| 88 | } | ||
| 89 | |||
| 90 | $param{cache} = $1.'.hp' if exists $param{cache} and | ||
| 91 | not $param{cache} and | ||
| 92 | not defined fileno( $param{src} ) and | ||
| 93 | $param{src} =~ /^(.*)\.h$/io; | ||
| 94 | } | ||
| 95 | |||
| 96 | # Reading compiled from cache file | ||
| 97 | if( $param{cache} and -f $param{cache} and -M $param{cache} < -M $param{src} ){ | ||
| 98 | $self = Storable::retrieve( $param{cache} ); | ||
| 99 | die "Version error!" unless $self->{''}{VERSION} == $VERSION; | ||
| 100 | |||
| 101 | # Parsing new source and store compiled to cache file | ||
| 102 | }else{ | ||
| 103 | unless( defined fileno $param{src} ){ | ||
| 104 | my $file = $param{src}; | ||
| 105 | $param{src} = new IO::File( $file ) or die "Can't open file '$file'!\n"; | ||
| 106 | } | ||
| 107 | read $param{src}, $self->{source}, (stat $param{src})[7], 0; | ||
| 108 | parse( $self ); | ||
| 109 | Storable::store($self, $param{cache}) if $param{cache}; | ||
| 110 | } | ||
| 111 | |||
| 112 | bless $self, $class; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | ########################### | ||
| 117 | # TEXT PARSER | ||
| 118 | sub parse() { | ||
| 119 | my $self = shift; | ||
| 120 | my %define; | ||
| 121 | |||
| 122 | # Define defaults | ||
| 123 | $self->{''}{VERSION} = $VERSION; | ||
| 124 | $define{__PERL__} = ''; | ||
| 125 | %{$self->{typedef}} = %TYPES; | ||
| 126 | $self->{typedef}{$_[0]} = $self->{typedef}{$_[1]} while @_ = each %ALIASES; | ||
| 127 | |||
| 128 | # Kill comments | ||
| 129 | $self->{source} =~ tr/\x0a\x0d/\x00/d; | ||
| 130 | $self->{source} =~ s{//.*?\x00|/\*.*?\*/}{\x00}go; | ||
| 131 | |||
| 132 | # Split to lines | ||
| 133 | my @lines = grep { /\S/ } split /\x00+/, $self->{source}; | ||
| 134 | delete $self->{source}; | ||
| 135 | |||
| 136 | |||
| 137 | # Local evalution func | ||
| 138 | local *evalute = sub($){ | ||
| 139 | my ($code, $key, $value) = shift; | ||
| 140 | $code =~ s/ \b (0x[0-9a-f]+) [ul]{0,2} \b /$1/gix; | ||
| 141 | while( ($key, $value) = each %define ){ | ||
| 142 | $value = 1 unless defined $value; # $value ??= 1; | ||
| 143 | $code =~ s/\b$key\b/$value/g; | ||
| 144 | } | ||
| 145 | return eval $code; | ||
| 146 | }; | ||
| 147 | |||
| 148 | |||
| 149 | # Local structs allocation func | ||
| 150 | local *alloc_structs = sub($$$){ | ||
| 151 | my ($template, $parent, $count, $result) = @_; | ||
| 152 | |||
| 153 | if( $count == 1 ){ | ||
| 154 | $result = $parent ? | ||
| 155 | Storable::dclone( $template ) : | ||
| 156 | new C::Include::Struct( $template ); | ||
| 157 | |||
| 158 | if( $parent ){ | ||
| 159 | push @{$$parent{''}{buffers}}, @{$$result{''}{buffers}}; | ||
| 160 | push @{$$parent{''}{bitsets}}, @{$$result{''}{bitsets}} if $$result{''}{bitsets}; | ||
| 161 | delete $$result{''} ; | ||
| 162 | } | ||
| 163 | }else{ | ||
| 164 | for ( 0..($count-1) ){ | ||
| 165 | $$result[$_] = $parent ? | ||
| 166 | Storable::dclone( $template ) : | ||
| 167 | new C::Include::Struct( $template ); | ||
| 168 | |||
| 169 | next unless $parent; | ||
| 170 | push @{$$parent{''}{buffers}}, @{$$result[$_]{''}{buffers}}; | ||
| 171 | push @{$$parent{''}{bitsets}}, @{$$result[$_]{''}{bitsets}} | ||
| 172 | if $$result[$_]{''}{bitsets}; | ||
| 173 | delete $$result[$_]{''}; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | if( $parent ){ | ||
| 178 | $$parent{''}{mask} .= $$template{''}{mask} x $count; | ||
| 179 | $$parent{''}{length} += $$template{''}{length} * $count; | ||
| 180 | } | ||
| 181 | |||
| 182 | return $result; | ||
| 183 | }; | ||
| 184 | |||
| 185 | local $\ = "\n" if $TEST; | ||
| 186 | |||
| 187 | # Parsing | ||
| 188 | my ($skip, $code); | ||
| 189 | |||
| 190 | # Evaluating prepocessor directives | ||
| 191 | while ( my $line = shift @lines ) { | ||
| 192 | $line =~ s/^\s+//o; $line =~ s/\s+$//o; | ||
| 193 | |||
| 194 | if ($line =~ /^\#\s*(\w+)\s*(\S.*?)?\s*$/o){ | ||
| 195 | my ($word, $value) = ($1, $2); | ||
| 196 | |||
| 197 | if ($word eq 'define') { | ||
| 198 | next if $skip; | ||
| 199 | |||
| 200 | ($word, $value) = split /\s+/, $value, 2; | ||
| 201 | if( $value and exists $self->{struct}{$value} ){ | ||
| 202 | $self->{struct}{$word} = $self->{struct}{$value}; | ||
| 203 | }elsif( $value and exists $self->{typedef}{$value} ){ | ||
| 204 | $self->{typedef}{$word} = $self->{typedef}{$value}; | ||
| 205 | }else{ | ||
| 206 | $define{$word} = defined $value ? evalute($value) : undef; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | elsif ($word eq 'pragma') { | ||
| 210 | } | ||
| 211 | elsif ($word eq 'ifdef') { | ||
| 212 | $skip++, next if $skip; | ||
| 213 | $skip++ unless exists $define{$value} or exists $self->{typedef}{$value}; | ||
| 214 | } | ||
| 215 | elsif ($word eq 'ifndef') { | ||
| 216 | $skip++, next if $skip; | ||
| 217 | $skip++ if exists $define{$value} or exists $self->{typedef}{$value}; | ||
| 218 | } | ||
| 219 | elsif ($word eq 'endif') { | ||
| 220 | $skip-- if $skip; | ||
| 221 | } | ||
| 222 | elsif ($word eq 'else') { | ||
| 223 | $skip += $skip ? -1 : 1; | ||
| 224 | } | ||
| 225 | |||
| 226 | next; | ||
| 227 | } | ||
| 228 | |||
| 229 | $code .= $line unless $skip; | ||
| 230 | } | ||
| 231 | |||
| 232 | @lines = split /;|(?<=\{)/, $code; | ||
| 233 | undef $code; | ||
| 234 | |||
| 235 | my @structs; | ||
| 236 | |||
| 237 | while( my $line = shift @lines ){ | ||
| 238 | $line =~ s/^\s+//o; $line =~ s/\s+$//o; | ||
| 239 | |||
| 240 | if( $line =~ /^typedef \s+ (.+?) \s+ (\w+) $ /xo ){ | ||
| 241 | print 'TYPEDEF: ', $2, ' == ', $1 if $TEST; | ||
| 242 | |||
| 243 | if( exists $self->{struct}{$1} ){ | ||
| 244 | $self->{struct}{$2} = $self->{struct}{$1}; | ||
| 245 | }elsif( exists $self->{typedef}{$1} ){ | ||
| 246 | $self->{typedef}{$2} = $self->{typedef}{$1}; | ||
| 247 | } | ||
| 248 | } | ||
| 249 | |||
| 250 | # ENUM definition | ||
| 251 | elsif( $line =~ /^ (?:typedef \s+)? enum (?: \s+(\w+) )? \s* \{$ /xo ){ | ||
| 252 | print 'ENUM: ', $1||'<anonymous>' if $TEST; | ||
| 253 | |||
| 254 | $self->{typedef}{$1} = $self->{typedef}{int} if $1; | ||
| 255 | |||
| 256 | my $items = shift @lines; | ||
| 257 | $items =~ s/\}\s*$//o; $items =~ s/\s+//go; | ||
| 258 | |||
| 259 | my $counter = 0; | ||
| 260 | foreach my $item ( split ',', $items ){ | ||
| 261 | $counter = evalute($1) if $item =~ s/=(.+)$//o; | ||
| 262 | print 'ENUM VALUE: ', $item, '=', $counter if $TEST; | ||
| 263 | $define{$item} = $counter++; | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | # BEGIN STRUCT definition | ||
| 268 | elsif( $line =~ /^ (typedef \s+)? struct (?: \s+(\w+) )? \s* \{$ /xo ){ | ||
| 269 | unshift @structs, {}; | ||
| 270 | $structs[0]{''}{typedef}++ if $1; | ||
| 271 | $structs[0]{''}{tag} = $2 if $2; | ||
| 272 | print 'STRUCT BEGIN: ', $line if $TEST; | ||
| 273 | } | ||
| 274 | |||
| 275 | # END STRUCT definition | ||
| 276 | elsif( $line =~ s/^\}//o ){ | ||
| 277 | print 'STRUCT END: ', $line if $TEST; | ||
| 278 | die "WARNING: found end of struct without begin" unless @structs; | ||
| 279 | |||
| 280 | my ($struct, @items, $name, $repeat) = shift @structs; | ||
| 281 | |||
| 282 | $line =~ s/\s+//go; | ||
| 283 | @items = map { | ||
| 284 | $repeat = s/\[(.+?)\]$//o ? evalute($1) : 1; | ||
| 285 | [$_, $repeat]; | ||
| 286 | } grep { !/\*/ } split /,/, $line; | ||
| 287 | |||
| 288 | # Compacting mask by counting repeated chars | ||
| 289 | 1 while $$struct{''}{mask} =~ s/(?<=([b-y]))(\d+)?(\1+)(\d+)?/($2?$2:1)+length($3)+($4?$4-1:0)/geix; | ||
| 290 | |||
| 291 | if( $$struct{''}{typedef} ){ | ||
| 292 | die "WARNING: Unnamed typedef found!\n" unless @items; | ||
| 293 | |||
| 294 | $name = @{shift @items}[0]; | ||
| 295 | if( $$struct{''}{tag} ){ | ||
| 296 | $self->{struct}{ $name } = $struct; | ||
| 297 | }else{ | ||
| 298 | $$struct{''}{tag} = $name; | ||
| 299 | } | ||
| 300 | |||
| 301 | splice @items; | ||
| 302 | delete $$struct{''}{typedef}; | ||
| 303 | } | ||
| 304 | |||
| 305 | if( $$struct{''}{tag} ){ | ||
| 306 | #$name = join '::', map{ $$_{''}{tag} }(reverse(@structs), $struct); | ||
| 307 | #die "WARNING: Can't define struct while anonymous parent exists '$name'!" | ||
| 308 | # if index($name, ':::') > -1; | ||
| 309 | #$self->{struct}{$name} = $struct; | ||
| 310 | $self->{struct}{ $$struct{''}{tag} } = $struct; | ||
| 311 | } | ||
| 312 | |||
| 313 | while( @items and ($name, $repeat) = @{shift @items} ){ | ||
| 314 | my $item; | ||
| 315 | |||
| 316 | if( $name ){ | ||
| 317 | if( scalar @structs ){ | ||
| 318 | $structs[0]{$name} = alloc_structs($struct, $structs[0], $repeat); | ||
| 319 | }else{ | ||
| 320 | $$self{$name} = alloc_structs($struct, undef, $repeat); | ||
| 321 | } | ||
| 322 | }else{ | ||
| 323 | die "WARNING: Parent struct not fount for aggregating anonymous struct: '$name'!" | ||
| 324 | unless scalar @structs; | ||
| 325 | die "WARNING: Can't aggregate to parent more than 1 anonymous struct: '$name' - $repeat!" | ||
| 326 | if $repeat > 1; | ||
| 327 | |||
| 328 | die "WARNING: In current anonymous struct aggregation not supported!"; | ||
| 329 | } | ||
| 330 | |||
| 331 | print "STRUCT: name:$name \t repeat:$repeat \t tag:$$struct{''}{tag} \t length:$$struct{''}{length} \t mask:$$struct{''}{mask}" if $TEST; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | # STRUCT ITEM definition | ||
| 336 | elsif( @structs ){ | ||
| 337 | print 'STRUCT ITEM: ', $line if $TEST; | ||
| 338 | |||
| 339 | # Bitfields | ||
| 340 | if( $line =~ /:/o ){ | ||
| 341 | my $empty = 0; | ||
| 342 | while( $line =~ /^(.+?) (?: \s+ (\w+) )? \s* : \s* (\d+)$/xo ){ | ||
| 343 | my ($type, $name, $len) = ($1, $2, $3); | ||
| 344 | |||
| 345 | if( !$len or (defined $len and $len > $empty) ){ | ||
| 346 | push @{$structs[0]{''}{bitsets}[0]{mask}}, 0-$empty if $empty; | ||
| 347 | |||
| 348 | unshift @{$structs[0]{''}{bitsets}}, {}; | ||
| 349 | #$structs[0]{''}{bitsets}[0]{buffer} = ''; # !* Storable bug fixer | ||
| 350 | push @{$structs[0]{''}{buffers}}, \$structs[0]{''}{bitsets}[0]{buffer}; | ||
| 351 | |||
| 352 | $type = $self->{typedef}{$type}; | ||
| 353 | $structs[0]{''}{mask} .= $$type[0]; | ||
| 354 | $structs[0]{''}{length} += $$type[1]; | ||
| 355 | |||
| 356 | $empty = $$type[1] * 8; | ||
| 357 | } | ||
| 358 | next unless $len; | ||
| 359 | |||
| 360 | #$structs[0]{$name} = ''; # !* Storable bug fixer | ||
| 361 | push @{$structs[0]{''}{bitsets}[0]{fields}}, \$structs[0]{$name}; | ||
| 362 | push @{$structs[0]{''}{bitsets}[0]{mask}}, $len; | ||
| 363 | |||
| 364 | $empty -= $len; | ||
| 365 | }continue{ | ||
| 366 | $line = shift @lines; | ||
| 367 | unshift(@lines,$line), last if $line =~ /^\}/o; | ||
| 368 | } | ||
| 369 | |||
| 370 | @{$structs[0]{''}{bitsets}} = reverse @{$structs[0]{''}{bitsets}}; | ||
| 371 | } | ||
| 372 | |||
| 373 | # Normal item | ||
| 374 | elsif( $line =~ s/^(?: struct \s+)? (.+?) \s+ (?= \w+ (?: \s* \[ \s* (.+?) \s* \] )? \s* (?:,|$) )//ox ){ | ||
| 375 | my ($type, $name, $repeat) = $1; | ||
| 376 | $line =~ s/\s+//go; | ||
| 377 | |||
| 378 | my @items = map { | ||
| 379 | $repeat = s/\[(.+?)\]$//o ? evalute($1) : 1; | ||
| 380 | [$_, $repeat]; | ||
| 381 | } split /,/, $line; | ||
| 382 | |||
| 383 | # sample type | ||
| 384 | if( exists $self->{typedef}{$type} ){ | ||
| 385 | my $typedef = $self->{typedef}{$type}; | ||
| 386 | my $strdef = $type eq 'char' ? $self->{typedef}{zstring}[0] : undef; | ||
| 387 | $strdef = $$typedef[0] if not $strdef and ( $$typedef[0] eq 'a' or | ||
| 388 | $$typedef[0] eq 'A' or | ||
| 389 | $$typedef[0] eq 'Z' ); | ||
| 390 | |||
| 391 | while( @items and ($name, $repeat) = @{shift @items} ){ | ||
| 392 | my $typedef = $typedef; | ||
| 393 | $structs[0]{''}{length} += $$typedef[1] * $repeat; | ||
| 394 | $typedef = [$strdef.$repeat, 1], $repeat=1 if $repeat>1 and $strdef; | ||
| 395 | $structs[0]{''}{mask} .= $$typedef[0]; | ||
| 396 | $structs[0]{''}{mask} .= $repeat if $repeat>1; | ||
| 397 | next if $$typedef[0] eq 'x' or $$typedef[0] eq 'X'; | ||
| 398 | if( $repeat > 1 ){ | ||
| 399 | #$structs[0]{$name}[$_] = '', # !* Storable bug fixer | ||
| 400 | push @{$structs[0]{''}{buffers}}, \$structs[0]{$name}[$_] for 0..($repeat-1); | ||
| 401 | }else{ | ||
| 402 | #$structs[0]{$name} = ''; # !* Storable bug fixer | ||
| 403 | push @{$structs[0]{''}{buffers}}, \$structs[0]{$name}; | ||
| 404 | } | ||
| 405 | } | ||
| 406 | } | ||
| 407 | |||
| 408 | # struct type | ||
| 409 | elsif( exists $self->{struct}{$type} ){ | ||
| 410 | while( @items and ($name, $repeat) = @{shift @items} ){ | ||
| 411 | $structs[0]{$name} = alloc_structs( $self->{struct}{$type}, | ||
| 412 | $structs[0], | ||
| 413 | $repeat ); | ||
| 414 | } | ||
| 415 | } | ||
| 416 | |||
| 417 | # unknown item type | ||
| 418 | else{ die "WARNING: Unknown struct item type '$type'\n" } | ||
| 419 | } | ||
| 420 | |||
| 421 | # Unknown struct item | ||
| 422 | else{ die "WARNING: Unknown struct item '$line'\n" } | ||
| 423 | } | ||
| 424 | |||
| 425 | # Unreashed code | ||
| 426 | else{ die "WARNING: Unreashed code '$line'\n" } | ||
| 427 | } | ||
| 428 | |||
| 429 | @$self{ keys %define } = values %define; | ||
| 430 | } | ||
| 431 | |||
| 432 | |||
| 433 | sub sizeof($){ | ||
| 434 | my ($self, $op) = @_; | ||
| 435 | |||
| 436 | return $self->{typedef}{$op}[1] if exists $self->{typedef}{$op}; | ||
| 437 | return $self->{struct}{$op}{''}{length} if exists $self->{struct}{$op}; | ||
| 438 | return 0; | ||
| 439 | } | ||
| 440 | |||
| 441 | |||
| 442 | sub make_struct($){ | ||
| 443 | my ($self, $struct) = @_; | ||
| 444 | die "WARNING: Can't make struct with unknown type: '$struct'!\n" | ||
| 445 | unless exists $self->{struct}{$struct}; | ||
| 446 | return new C::Include::Struct( $self->{struct}{$struct} ); | ||
| 447 | } | ||
| 448 | |||
| 449 | |||
| 450 | sub defined($){ return exists shift->{$_[0]}; } | ||
| 451 | sub INC{ return exists($INC{scalar($_[0] || caller)}) ? $INC{scalar($_[0] || caller)} : undef; } | ||
| 452 | sub import{ shift; $INC{scalar(caller)} = new C::Include( @_ ) if @_; } | ||
| 453 | |||
| 454 | |||
| 455 | ################################ | ||
| 456 | ## package C::Include::Struct ## | ||
| 457 | ################################ | ||
| 458 | package C::Include::Struct; | ||
| 459 | |||
| 460 | use 5.005; | ||
| 461 | use strict; | ||
| 462 | use Storable; | ||
| 463 | |||
| 464 | sub new($){ | ||
| 465 | my ($class, $self) = @_; | ||
| 466 | $self = Storable::dclone( $self ); | ||
| 467 | bless $self, $class; | ||
| 468 | } | ||
| 469 | |||
| 470 | sub pack(;$){ | ||
| 471 | my ($self, $FILE, $data) = (shift, shift); | ||
| 472 | |||
| 473 | if( $$self{''}{bitsets} ){ | ||
| 474 | for my $item( @{$$self{''}{bitsets}} ){ | ||
| 475 | my $i = $#{$$item{fields}}; | ||
| 476 | my $data; | ||
| 477 | for my $size ( reverse @{$$item{mask}} ){ | ||
| 478 | $data <<= $size<1 ? 1-$size : $size; | ||
| 479 | $data += ${$$item{fields}[$i--]} unless $size < 1; | ||
| 480 | } | ||
| 481 | $$item{buffer} = $data; | ||
| 482 | } | ||
| 483 | } | ||
| 484 | $data = pack $$self{''}{mask}, map{ $$_ }@{$$self{''}{buffers}}; | ||
| 485 | |||
| 486 | print $FILE $data if $FILE and defined fileno $FILE; | ||
| 487 | |||
| 488 | return $data; | ||
| 489 | } | ||
| 490 | |||
| 491 | sub unpack($;$$){ | ||
| 492 | my $self = shift; | ||
| 493 | my $data = shift; | ||
| 494 | |||
| 495 | if( defined fileno $data ){ | ||
| 496 | my( $pos, $len ) = ( shift || 0, shift || $$self{''}{length} ); | ||
| 497 | my $readed = read $data, $data, $len, $pos; | ||
| 498 | return 0 if $readed < $len; | ||
| 499 | } | ||
| 500 | |||
| 501 | my $i = 0; | ||
| 502 | ${$$self{''}{buffers}[$i++]} = $_ for unpack $$self{''}{mask}, $data; | ||
| 503 | |||
| 504 | if( $$self{''}{bitsets} ){ | ||
| 505 | for my $item( @{$$self{''}{bitsets}} ){ | ||
| 506 | my $data = $$item{buffer} || 0; | ||
| 507 | my $prev = $data; | ||
| 508 | my $i = 0; | ||
| 509 | for my $size ( @{$$item{mask}} ){ | ||
| 510 | $data >>= $size<1 ? 1-$size : $size; | ||
| 511 | ${$$item{fields}[$i++]} = $prev - ($data << $size) unless $size < 1; | ||
| 512 | $prev = $data; | ||
| 513 | } | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | return 1; | ||
| 518 | } | ||
| 519 | |||
| 520 | sub link($$){ | ||
| 521 | my ($self, $old, $new) = @_; | ||
| 522 | |||
| 523 | $old = \$self->{$old} unless ref $old; | ||
| 524 | $_ == $old and return $_ = $new for @{ $self->{''}{buffers} }; | ||
| 525 | die "Can't relink!"; | ||
| 526 | } | ||
| 527 | |||
| 528 | sub size(){ return shift->{''}{length} } | ||
| 529 | |||
| 530 | 1; | ||
| 531 | |||
| 532 | __END__ |