#!/usr/bin/perl -w # $Id: swap_endian,v 1.8 2004/04/23 16:29:45 braup Exp $ # Copyright 2004 Bruce Raup (http://cires.colorado.edu/~braup/) # This software is distributed under the GNU General Public License. See # http://www.gnu.org/licenses/gpl.txt for the text of this license. # In short: # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. $| = 1; ($progname = $0) =~ s!^.*/!!; # get basename of program use Getopt::Std; $version = '$Revision: 1.8 $'; ($version) = $version =~ /^\$Revision:\s*(\d+\.\d*)/; # Defaults $bytes = 4; # number of bytes to swap $usage = "$progname version $version Program to swap bytes (endianness). Usage: $progname -h (prints this help message and exits) OR $progname [-q] [-bNum] infile outfile where -b specifies Num bytes to swap [default: $bytes] -q specifies quiet mode Examples: swap_endian -b2 little big gzip -dc little.gz | swap_endian -b2 - - | gzip -c > big.gz "; $opt_q = $opt_h = $opt_b = ''; if (! getopts('hqb:') ) { die "$usage\n"; } warn "$progname version $version\n" unless $opt_q; die "$usage\n" if $opt_h; die "$usage\n" if @ARGV != 2; $bytes = $opt_b if $opt_b; ($infile, $outfile) = @ARGV; if (-e $outfile) { warn "$outfile already exists. Overwrite? "; $answer = ; unless ($answer =~ /^y/i) { warn "Okay, we'll abort then. Exiting.\n"; exit 1; } } warn "Opening files ... " unless $opt_q; open IN, "$infile" or die "Couldn't open $infile for reading $!"; open OUT, ">$outfile" or die "Couldn't open $outfile for writing $!"; warn "Done.\nStarting to swap, $bytes bytes at a time ... " unless $opt_q; $template = 'c' x $bytes; while (read IN, $in, $bytes) { $out = pack( $template, reverse( unpack( $template, $in ) ) ); print OUT $out; } close IN; close OUT; warn "Done.\n" unless $opt_q;