Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Binary buffer - substr

by highland7 (Novice)
on Jan 13, 2013 at 22:29 UTC ( #1013138=perlquestion: print w/ replies, xml ) Need Help??
highland7 has asked for the wisdom of the Perl Monks concerning the following question:

Hello PerlMonks

I read/create IP packets using Net::Frame::Layer.
I already have raw packet dump in $packet_dump.
I need to search for 4 consecutive bytes with values 0x16 0x3 0x1 0x1 and make a new binary buffer which will be send.
That new binary buffer will start from that 4 bytes (to the end of $packet_dump).
How can i do it ?
I have tried it with qq, pack, unpack but without suceess :(
Thanks,

Comment on Binary buffer - substr
Re: Binary buffer - substr
by dave_the_m (Parson) on Jan 13, 2013 at 22:53 UTC
    (my $out_packet = $packet_dump) =~ s/^.*?(?=\x16\x03\x01\x01)//s;
    edit: added /s modifier as pointed out by 7stud

    Dave.

      Thanks, it's working !!!
      One more question: How can i find index number for specific binary string ? I would like to find out from which byte that specific 4 bytes appear (/x16/x03/0x1/0x0) Thanks
        index($packet_dump, "\x16\x03\x01\x01")

        Dave.

Re: Binary buffer - substr
by 7stud (Deacon) on Jan 14, 2013 at 06:21 UTC
    Binary data can contain newline characters, which cannot be matched by the default dot in a regex.
    use strict; use warnings; use 5.012; my $str = "\x16-\x03\x01\x01-\x16\x03\x01\x01PAY\nLOAD"; if ($str =~ /(\x16\x03\x01\x01 .*)/xms) { say $1; printf "%vd \n", $1; say length($str) - length($1); } --output:-- PAY LOAD 22.3.1.1.80.65.89.10.76.79.65.68 6
      Binary data can contain newline characters, which cannot be matched by the default dot in a regex
      Good point. I've updated my original post to include /s

      Dave.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1013138]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2015-07-03 08:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (50 votes), past polls