Golf...

P

Peter Wyzl

I have binary data representing an IP address (among other things) in $data.
(part of a libpcap file, read in binary mode)

The 4 bytes of $data repreenting the IP address start at offset 26. I am
looking for a good solution to represent this in dotted quad. Since there
are many repetitions of this manipulation, efficiency is important.

What I have so far is:

(assume $sourceip has been pre-declared and $data contains binary data)

$sourceip = join '.', map {ord}(split '', substr $data,26,4,);

Shorter solutions are also of interest to me.

BTW, this is on a windows platform so the data is in little endian order in
the headers, and network order in the raw IP packet (no reason to make life
simple...) This IP is therefore stored in network order. I am
contemplating a solution using unpack, but am still learing all the
different templates, and am unsure whether it is more efficient than substr
(or enough so to be worth trying). I have not yet benchmarked a regex
solution, having a hunch that it would be less efficient.
 
U

Uri Guttman

PW> (assume $sourceip has been pre-declared and $data contains binary data)

PW> $sourceip = join '.', map {ord}(split '', substr $data,26,4,);

PW> Shorter solutions are also of interest to me.

unpack is your friend. no need for both split and substr. sprintf can do
the ord and join in one.

<untested>

my $ip = sprintf '%c.%c.%c.%c', unpack 'X26CCCC', $data ;

uri
 
P

Peter Wyzl

:
: PW> (assume $sourceip has been pre-declared and $data contains binary
data)
:
: PW> $sourceip = join '.', map {ord}(split '', substr $data,26,4,);
:
: PW> Shorter solutions are also of interest to me.
:
: unpack is your friend. no need for both split and substr. sprintf can do
: the ord and join in one.
:
: <untested>

Thats OK, I'm happy to do the testing and playing around...

: my $ip = sprintf '%c.%c.%c.%c', unpack 'X26CCCC', $data ;

$sourceip = sprintf '%s.%s.%s.%s', unpack'x26CCCC', $data;

works (the unpack C makes it a char already and %c messes with that....

which means I can dispense with sprintf and use join again to get

$sourceip = join '.', unpack 'x26CCCC', $data;

12 strokes less!

Also you meant x not X in the template for unpack (forward null bytes not
backwards....)

But now that does what I need, in a nice elegant way. I will attempt a
benchmark shortly, but I suspect anything with less function calls must be
better. 2 v 5 is a good saving.

Incidentally, I have been able to process 5Mb (sysreading 1 packet at a
time) in less than 3 seconds before this improvement, so this looks hopeful
for reasonable performance on larger files..

Thanks Uri. That was just the mental poke I needed to move forward with
this little project.

:)
 
U

Uri Guttman

PW> :
PW> : <untested>

PW> Thats OK, I'm happy to do the testing and playing around...

want to do more testing and fixing of my code? :)

PW> : my $ip = sprintf '%c.%c.%c.%c', unpack 'X26CCCC', $data ;

PW> $sourceip = sprintf '%s.%s.%s.%s', unpack'x26CCCC', $data;

PW> works (the unpack C makes it a char already and %c messes with that....

d'oh! like i said, untested. and i was loopy then (all day in fact. :( ).

PW> which means I can dispense with sprintf and use join again to get

PW> $sourceip = join '.', unpack 'x26CCCC', $data;

PW> 12 strokes less!

and fewer calls which is more important.

PW> Also you meant x not X in the template for unpack (forward null bytes not
PW> backwards....)

well i always have case issues :)

PW> But now that does what I need, in a nice elegant way. I will
PW> attempt a benchmark shortly, but I suspect anything with less
PW> function calls must be better. 2 v 5 is a good saving.

that is the general rule of thumb in perl but there are many
counterintuitive examples especially with regexes.

PW> Incidentally, I have been able to process 5Mb (sysreading 1 packet
PW> at a time) in less than 3 seconds before this improvement, so this
PW> looks hopeful for reasonable performance on larger files..

it should be much faster IMO. post the results.

PW> Thanks Uri. That was just the mental poke I needed to move
PW> forward with this little project.

i will sharpen my poker for the next time! :)

uri
 
P

Peter Wyzl

: ::

<snip>:

: $sourceip = join '.', unpack 'x26CCCC', $data;

$sourceip = join '.', unpack 'x26C4', $data;

Is even better again...

(guess who is finally starting to understand this function? :) And that's
even after reading MJD's excellent tutorial at I know not where (can't seem
to find it again)
 
U

Uri Guttman

PW> PW> : PW> ::

PW> <snip>:

PW> : $sourceip = join '.', unpack 'x26CCCC', $data;

PW> $sourceip = join '.', unpack 'x26C4', $data;

PW> Is even better again...

i should have done that too. pack/unpack are very amazing but they scare
too many. coming from an assembler/c background, they make perfect sense
to me.

PW> (guess who is finally starting to understand this function? :) And
PW> that's even after reading MJD's excellent tutorial at I know not
PW> where (can't seem to find it again)

sure to be on plover.com somewhere. google his site.

uri
 
M

Michele Dondi

Subject: Golf... ^^^^
^^^^
[snip]
looking for a good solution to represent this in dotted quad. Since there
are many repetitions of this manipulation, efficiency is important.
^^^^^^^^^^
^^^^^^^^^^

Please note that in (Perl-)golfing efficiency is not an issue. The
subject may have been more accurate...


Michele
 
M

Michele Dondi

(guess who is finally starting to understand this function? :) And that's
even after reading MJD's excellent tutorial at I know not where (can't seem
to find it again)

I didn't know about that tutorial. Probably I'll search it @
plover.com, however out of curiosity can you please anticipate how it
compares in your opinion to 'perldoc perlpacktut'?


Just curious,
Michele
 
P

Peter Wyzl

: On Sun, 05 Dec 2004 06:10:23 GMT, "Peter Wyzl" <[email protected]>
: wrote:
:
: >(guess who is finally starting to understand this function? :) And that's
: >even after reading MJD's excellent tutorial at I know not where (can't
seem
: >to find it again)
:
: I didn't know about that tutorial. Probably I'll search it @
: plover.com, however out of curiosity can you please anticipate how it
: compares in your opinion to 'perldoc perlpacktut'?

Having a quick look...

seems they are one and the same... maybe I was reading too much of MJD's web
site at the same time and confused them...

My apologies to Simon and Wolfgang, and thanks to you for reminding where
the tut was...

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top