#!/usr/bin/perl -w use strict; use Carp; my $SYNCCODE_CHAR_ONE = 0xFF; # 11111111 in binary my $SYNCCODE_CHAR_TWO = 0xF8; # 11111000 in binary # Notice the complet lack of error checking # loop over all filenames supplied on command-line for my $filename (@ARGV) { open my $file, '<', $filename or croak "Can't open '$filename': $!"; binmode($file); my $SyncCode_Char_One_Found = 0; my $SyncCode_Position = 0; READBYTE: while ( sysread($file, my $byte, 1) ) { if ($SyncCode_Char_One_Found) { if (ord($byte) == $SYNCCODE_CHAR_TWO) { last READBYTE; } else { $SyncCode_Char_One_Found = 0; $SyncCode_Position++; } }; if (ord($byte)== $SYNCCODE_CHAR_ONE) { $SyncCode_Char_One_Found = 1; next READBYTE; } else { $SyncCode_Position++; } } close $file; truncate ($filename, $SyncCode_Position) or croak "Couldn't truncate file '$filename': $!"; print "File: $filename truncated to $SyncCode_Position bytes\n"; }