I think you're looking at the problem from the wrong angle by focusing on the "bit" part. If you look at the problem from more distance, you see that you have 50 bits that must be zero, so that gives you 3 bytes (36 bit) that will always be zero. Looking for these three zero bytes is easy by using regular expressions. If you look left and right of these three bytes, you can then determine if these three zero bytes form part of a correct 128-bit frame.

Some untested code that should capture a "interesting" frame candidate, supposing that you can read the whole file into memory:

my $old_buffer; my $offset = 0; while (read(FH,65536,\$buffer)) { # Append the last 8 bytes of the buffer if ($old_buffer) { $buffer = substr( $old_buffer, -8 ) . $buffer; $offset -= 8; }; if ($buffer =~ /(......\0\0\0........)/sm) { printf "Found possible candidate at %s : $1\n", $offset + pos; }; $offset += len $buffer; $old_buffer = $buffer; };

If you can't read the whole file into memory, read smaller chunks of the file into memory and then paste the last 16 bytes (== 128 bits) from the end of the last buffer to the front of the next buffer. That way, you'll also catch frames that fall on a buffer boundary.

Update: As liz tells me, you can even look for four consecutive zeroes by the following logic:

You have 50 zero bits and at most 7 junk bits in a byte. The 50 bits span 6 bytes, so you will have (50-7*2)/8 clean bytes, which is (36 bits)/8, which makes 4 consecutive zero bytes. If your data is MPEG data, I think I remember that the MPEG frames always start at the byte boundary, so then you could even look for the exact sequence with a carefully crafted RE.

perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web

In reply to Re: perl bitology - breaking into bytes by Corion
in thread perl bitology - breaking into bytes by spurperl

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.