📄 zeropadbin.pl
字号:
#!/bin/perl -w
#
## zeropadbin.pl
##
## Opens the input file for writing and expects to find the following
## header at the beginning of the file:
##
## +0 reserved
## +4 reserved
## +8 uint32 version of header (0x00000001)
## +12 uint32 RO_Base
## +16 uint32 RO_Limit
## +20 uint32 RW_Base
## +24 uint32 ZI_Base
## +28 uint32 ZI_Limit
##
## Total file size should be given by RO_Limit - RO_Base + ZI_Limit - RW_Base
## If the file is shorter than that, it needs to be zero padded.
## As a sanity check, it should be RO_Limit - RO_Base + ZI_Base - RW_Base in
## size before zero padding with ZI_Limit - ZI_Base bytes of zeros.
#
$header = "";
$#ARGV == 0 or die "Usage: zeropadbin file.bin\n";
open(BINFILE, "+<$ARGV[0]") or die "zeropadbin: Cannot open file \"$ARGV[0]\" for RW\n";
binmode(BINFILE);
(undef, undef, undef, undef, undef, undef, undef, $fsize, undef) = stat(BINFILE);
$fsize >= 32 or die "zeropadbin: File too short to be valid\n";
read(BINFILE, $header, 32) or die "zeropadbin: Invalid format\n";
# Read the header
($vers, $robase, $rolimit, $rwbase, $zibase, $zilimit) =
unpack('x4x4V6', $header);
#print length($header), " ", join(" ", unpack('x4x4V6', $header)), "\n";
# Check header version
$vers == 1 || $vers == 2 or die "zeropadbin: Unsupported header version $vers in file \"$ARGV[0]\"\n";
# Some sanity validation -- require a non-empty RO section
$robase < $rolimit && $rwbase <= $zibase && $zibase <= $zilimit or
die "zeropadbin: Invalid header values in file \"$ARGV[0]\"\n";
# Okay, see if it's one of the magic sizes
$nozeropadsize = $rolimit - $robase + $zibase - $rwbase;
$zeropadsize = $nozeropadsize + $zilimit - $zibase;
#print "File size is $fsize\n";
#print "Nozeropad size would be $nozeropadsize\n";
#print "Zeropad size would be $zeropadsize\n";
$fsize >= $nozeropadsize or die "zeropadbin: file too short \"$ARGV[0]\"\n";
$fsize <= $zeropadsize or die "zeropadbin: file too long \"$ARGV[0]\"\n";
$fsize == $nozeropadsize || $fsize == $zeropadsize or
die "zeropadbin: Invalid partial zero padding present in file \"$ARGV[0]\"\n";
# Okay, passed all the sanity checks see if we need to add the padding or not
if ($fsize == $zeropadsize) {
close(BINFILE);
print "zeropadbin: doing nothing, zero padding already correct in \"$ARGV[0]\"\n";
exit 0;
}
# Must add zero padding now
seek(BINFILE, 0, 2) or die "zeropadbin: seek EOF failed on file \"$ARGV[0]\"\n";
$padding = $zilimit - $zibase;
print(BINFILE scalar(chr(0) x $padding));
close(BINFILE);
print "zeropadbin: added $padding bytes of zeros to \"$ARGV[0]\"\n";
exit 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -