in reply to perl bitology - breaking into bytes

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