I am writing a UT2K4 Query script in perl. The problem I am having is that the data that is returned is in a binary structure. My goal is to use unpack($template,$query); where $template is the pack/unpack template, and $query is a string of data from the query. format details can be found at http://unreal.student.utwente.nl/UT2003-queryspec.html
The format of this structure is as follows:
4 bytes -- (int) $id
unknown bytes -- (null terminated string) $player
4 bytes -- (int) $ping
4 bytes -- (int) $score
4 bytes -- (int) $stats
this block is repeated for each player in the list. My goal is to use unpack($template,$query); where $template is the pack/unpack template, and $query is a string of data from the query. my main issue is that the ints are in little-endian and the string is in the correct endian. I have tried some different templates and have gotten nothing but junk out. I used pack('H*',$query) to get a hex dump to see what the data should be. more details can be found: http://unreal.student.utwente.nl/UT2003-queryspec.html Any suggestions on how to go about this? I have looked at the resources on the net, but they don't seem to be able to handle the null terminated string. I am begining to wonder if unpack can handle this. I might be able to break each data type into a string and use unpack on just that chunk to get the ints.
Some Example Code to monkey with:
#!/bin/perl -w #A product from the bordom of the BioVorE #UT2K4 Player Server Query -- test use strict; #example player status return from UT2K4 server my $packet = "\x0d\x0a\x80\x0d\x0a\x6a\x69\x6d\x6d\x79\x43\x48\x55\x5 +b\x43\x4f"; $packet .= "\x53\x42\x59\x5d\x2c\x0d\x0a\x0d\x0a\x6a\x61\x57\x41\x7c\x +44\x72"; $packet .= "\x2e\x50\x68\x69\x6c\x30\x20\x4a\x61\x64\x65\x2d\x63\x6c\x +61\x6e"; $packet .= "\x6c\x65\x73\x73\x2c\x40\x20\x20\x20\x6b\x31\x3a\x3a\x73\x +70\x6c"; $packet .= "\x69\x66\x66\x6d\x61\x73\x74\x61\x3a\x3a\x30\x29\x40\x0d\x +0a\xbb"; $packet .= "\xab\x4b\x65\x76\x69\x6e\xbb\xab\x38\x20\x20\x0d\x0a\x20\x +20\x20"; $packet .= "\x20\x20\x20\x20\x20\x20\x20\x20\x20\x41\x79\x65\x43\x61\x +72\x61"; $packet .= "\x6d\x62\x61\x21\x44\x22\x40\x20\x20\x20\x20\x20\x20\xbb\x +62\x53"; $packet .= "\xb0\xc0\xee\x4d\xab\x48\x21\x20\x20\x20\x20\x20\x20\x3d\x +6f\x4e"; $packet .= "\x65\x3d\x46\x6c\x75\x38\x20\x3d\x6f\x4e\x65\x3d\xed\x6c\x +6c\xf9"; $packet .= "\x53\xef\xf8\xf1\x24\x27\xab\xfb\x2e\x4e\x65\x30\xbb\x52\x +65\x64"; $packet .= "\x42\x6c\x75\x65\x0d\x0a\x0d\x0a\x0d\x0a"; #drop first byte.. always 0d $packet = substr($packet,1); #print out the packet in hex chars my $data=0; $data = unpack('H*', $packet ); print $data; print "\n"; # parse out the $packet into varbles # structure format # int -- ID # string -- Player # int -- ping # int -- score # int -- stat # @player_info=unpack($template,$packet)
Peter Fetterer biovore biovore net

In reply to parsing Binary Structures using unpack by biovore

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.