CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
sysread and Little Endian

 



ric79
New User

Oct 4, 2013, 1:34 AM

Post #1 of 6 (850 views)
sysread and Little Endian Can't Post

Hi all,
my file, in HEX, is like this:


Code
hexdump -C mytest.pcap -n 16 
00000018 63 1e 0f 4f b5 a2 0e 00 2a 05 00 00 2a 05 00 00 |c..O....*...*...|
00000028


I need to sysread the last 4 bytes (2a 05 00 00) which indicates the total file length. Beware that the number is written in Little Endian and must be read as 0x0000052a --> 1322 Bytes


Code
sysread($global_fh, $buffer, 12) > 0 || die "couldn't read byte from $filename: $!"; 
sysread($global_fh, $buffer, 4) > 0 || die "couldn't read byte from $filename: $!";
$buf = unpack('H*',$buffer);
print "-". $buf ."-\n";


-2a050000-


How to convert now in decimal?
Help is appreciated!

Riccardo


(This post was edited by ric79 on Oct 6, 2013, 11:37 PM)
Attachments: mytest.pcap (1.33 KB)


Laurent_R
Veteran / Moderator

Oct 4, 2013, 3:16 PM

Post #2 of 6 (831 views)
Re: [ric79] sysread and Little Endian [In reply to] Can't Post

I would probably first try to use the read and seek functions, rather than sysread, to read the required bytes.

This is a basic syntax example I gave on this forum a few weeks ago, you can adapt it to your needs:


Code
  DB<1> $c = "The quick brown fox jumps over the lazy dog."  

DB<2> open $fh, "<", \$c or die "could not open $c $!"; # opens a reference to the $c variable as a filehandler

DB<3> seek $fh, 10, "00"; # positions the filehandler on the 10th byte

DB<4> read $fh, $out, 10; # reads 10 bytes from position 10

DB<5> print $out;
brown fox


For the Little Endian/Big Endian thing, hmm, we would probably need to know more about your machine architecture (64 bits, 48 bits, 64 bits, more ?), I am not even sure you need to care about it if you use the read function (last time I had to deal with such problems was at least 6 years ago, I don't remember well enough what I had to do, except that I had to make a bit of experiment to get it right, but it was not so complicated to get it right once I understood the format). One possible clue: the h and H templates of the pack and unpack function might be what you are looking for.

I might have a quick look at it next Monday when I am back to work, where I have access to a Little Endian platform.


Chris Charley
User

Oct 4, 2013, 7:34 PM

Post #3 of 6 (827 views)
Re: [ric79] sysread and Little Endian [In reply to] Can't Post

I'm not sure this is what you want, but it seems to give the correct number, 1322.


Code
#!/usr/bin/perl 
use strict;
use warnings;

$_ = '2a050000';

print unpack "N", pack "V", hex $_;



ric79
New User

Oct 6, 2013, 11:29 PM

Post #4 of 6 (800 views)
Re: [Laurent_R] sysread and Little Endian [In reply to] Can't Post

Thanks, I switched to your syntax but now I'm still on the same problem

$out contains, i think, 0x2a 05 00 00.

How can I do the convertion to decimal? I attached in the first question the binary file. I'm trying to decode the bytes in [20-23] range.

Thanks to all!


(This post was edited by ric79 on Oct 6, 2013, 11:38 PM)


BillKSmith
Veteran

Oct 8, 2013, 7:36 AM

Post #5 of 6 (772 views)
Re: [ric79] sysread and Little Endian [In reply to] Can't Post

I had to change the number of bytes in the first sysread to read your sample file. This code runs on perl v5.16.1 under windows xp.

Code
open my $global_fh, '<', 'mytest.pcap', or die "cannot open $!"; 
binmode $global_fh;
sysread( $global_fh, $buffer, 0x20 ) > 0
|| die "couldn't read byte from $filename: $!";
sysread( $global_fh, $buffer, 4 ) > 0
|| die "couldn't read byte from $filename: $!";

$buf = unpack( 'H8', $buffer );
print "-" . $buf . "-\n";
$buf = unpack( 'V', $buffer );
print "-" . $buf . "-\n";


OUTPUT:

Code
-2a050000- 
-1322-


Update: Corrected first sysread. Note that second syread reads bytes 0x20 thru 0x23.
Good Luck,
Bill

(This post was edited by BillKSmith on Oct 8, 2013, 10:12 AM)


ric79
New User

Oct 8, 2013, 10:34 PM

Post #6 of 6 (756 views)
Re: [BillKSmith] sysread and Little Endian [In reply to] Can't Post

Thanks to all!
It work

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives