Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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,

Replies are listed 'Best First'.
Re: Binary buffer - substr
by dave_the_m (Prior) 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?
Corion watches from the sidelines. Or rather, from behind, as my system only gets output from that process and my programs adhere strictly to the GIGO design principle.
[erix]: ah, that's nice to hear Corion :)
[Corion]: erix: Yeah, the sad thing is that all I can do is document things, so I can point fingers when the auditors come :-/
[Corion]: "I'm here to open tickets and point fingers. And I'm all out of tickets."
[erix]: didn't Sybase have pretty good auditing? :) (this is a vague memory)
[erix]: (culprits often are upstream of db of course)
[Corion]: Ah, how I missed it. After some years, I revisit slashdot on a click-bait link, and it provides the usual humor instantly: "I didn't know Drupal had rules for sex. It must be a plug-in"
[Corion]: erix: This is not for sybase, but for the input data files, resp. their contents.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (8)
As of 2017-03-28 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (328 votes). Check out past polls.