| 1 | #!/usr/local/bin/perl -w | ||||||||||
| 2 | ################################################################################ | ||||||||||
| 3 | # | ||||||||||
| 4 | # Copyright (c) 2002-2020 Marcus Holland-Moritz. All rights reserved. | ||||||||||
| 5 | # This program is free software; you can redistribute it and/or modify | ||||||||||
| 6 | # it under the same terms as Perl itself. | ||||||||||
| 7 | # | ||||||||||
| 8 | ################################################################################ | ||||||||||
| 9 | |||||||||||
| 10 | #=============================================================================== | ||||||||||
| 11 | # | ||||||||||
| 12 | # Parse a C struct and use 'pack', 'unpack', 'sizeof' and 'offsetof'. | ||||||||||
| 13 | # | ||||||||||
| 14 | #=============================================================================== | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use Convert::Binary::C; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | #-------------------------------------------------------------- | ||||||||||
| 21 | # Create an object, configure it, and parse some embedded code. | ||||||||||
| 22 | #-------------------------------------------------------------- | ||||||||||
| 23 | |||||||||||
| 24 | 1 | my $c = Convert::Binary::C->new( LongSize => 4, ShortSize => 2 ) | |||||||||
| 25 | ->Alignment( 4 ) | ||||||||||
| 26 | ->ByteOrder( 'BigEndian' ) | ||||||||||
| 27 | ->parse( <<'ENDC' ); | ||||||||||
| 28 | |||||||||||
| 29 | typedef signed long i_32; | ||||||||||
| 30 | typedef unsigned long u_32; | ||||||||||
| 31 | typedef signed short i_16; | ||||||||||
| 32 | typedef unsigned short u_16; | ||||||||||
| 33 | typedef signed char i_8; | ||||||||||
| 34 | typedef unsigned char u_8; | ||||||||||
| 35 | |||||||||||
| 36 | struct convert { | ||||||||||
| 37 | i_8 byte; | ||||||||||
| 38 | i_16 word[2]; | ||||||||||
| 39 | i_32 dword; | ||||||||||
| 40 | union { | ||||||||||
| 41 | u_32 dword; | ||||||||||
| 42 | u_8 bytes[ sizeof( u_32 ) ]; | ||||||||||
| 43 | } c32; | ||||||||||
| 44 | }; | ||||||||||
| 45 | |||||||||||
| 46 | ENDC | ||||||||||
| 47 | |||||||||||
| 48 | #----------------------------------------------------------- | ||||||||||
| 49 | # Print the offsets and sizes of some of the struct members. | ||||||||||
| 50 | #----------------------------------------------------------- | ||||||||||
| 51 | |||||||||||
| 52 | 1 | for( qw( byte word dword ) ) { | |||||||||
| 53 | 3 | print "offsetof( 'convert', '$_' ) = ", $c->offsetof( 'convert', $_ ); | |||||||||
| 54 | 3 | print ", sizeof( 'convert.$_' ) = ", $c->sizeof( "convert.$_" ), "\n"; | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | #------------------------------------------------- | ||||||||||
| 58 | # Pack a Perl data structure into a binary string. | ||||||||||
| 59 | # Note that not all members need to be specified. | ||||||||||
| 60 | #------------------------------------------------- | ||||||||||
| 61 | |||||||||||
| 62 | 1 | my $binary = $c->pack( 'convert', { | |||||||||
| 63 | word => [-30000, 4711], | ||||||||||
| 64 | c32 => { dword => 0x01020304 } | ||||||||||
| 65 | } ); | ||||||||||
| 66 | |||||||||||
| 67 | #------------------------------------------------------- | ||||||||||
| 68 | # Just a demonstration that pack does the right thing... | ||||||||||
| 69 | #------------------------------------------------------- | ||||||||||
| 70 | |||||||||||
| 71 | 1 | ▶ | if( $c->sizeof( 'convert' ) == length $binary ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 1 | print "\nYup, the size matches!\n"; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | #------------------------------------------------------- | ||||||||||
| 76 | # Hexdump the binary string. | ||||||||||
| 77 | # Note that all padding regions are initialized to zero. | ||||||||||
| 78 | #------------------------------------------------------- | ||||||||||
| 79 | |||||||||||
| 80 | 1 | print "\nBinary: ", hexdump( $binary ), "\n\n"; | |||||||||
| 81 | |||||||||||
| 82 | #--------------------------------------------------------------- | ||||||||||
| 83 | # Unpack the binary string and dump the returned data structure. | ||||||||||
| 84 | #--------------------------------------------------------------- | ||||||||||
| 85 | |||||||||||
| 86 | 1 | my $data = $c->unpack( 'convert', $binary ); | |||||||||
| 87 | 1 | print Data::Dumper->Dump( [$data], ['data'] ); | |||||||||
| 88 | |||||||||||
| 89 | #------------------------------------------------------ | ||||||||||
| 90 | # You can modify selected elements in the binary string | ||||||||||
| 91 | # using the 3-argument version of 'pack'. | ||||||||||
| 92 | #------------------------------------------------------ | ||||||||||
| 93 | |||||||||||
| 94 | # only 'dword' will be modified | ||||||||||
| 95 | 1 | $c->pack( 'convert', { dword => -559038737 }, $binary ); | |||||||||
| 96 | 1 | print "\nBinary: ", hexdump( $binary ), "\n\n"; | |||||||||
| 97 | 1 | print Dumper( $c->unpack( 'convert', $binary ) ); | |||||||||
| 98 | |||||||||||
| 99 | #-------------------------------------------------- | ||||||||||
| 100 | # You can also use pack/unpack on compound members. | ||||||||||
| 101 | #-------------------------------------------------- | ||||||||||
| 102 | |||||||||||
| 103 | 1 | my $array = $c->unpack( 'convert.c32.bytes', 'ABCD' ); | |||||||||
| 104 | 1 | print "\n\$array = [ @$array ]\n"; | |||||||||
| 105 | |||||||||||
| 106 | #========================================================== | ||||||||||
| 107 | # SUBROUTINES | ||||||||||
| 108 | #========================================================== | ||||||||||
| 109 | |||||||||||
| 110 | sub hexdump | ||||||||||
| 111 | { | ||||||||||
| 112 | 2 | ▶ | join ' ', map { sprintf "%02X", $_ } unpack "C*", $_[0]; | ||||||||
|
Subroutine
hexdump: called CC=1 SCAR=0.0
| |||||||||||
| 113 | } | ||||||||||