-
Notifications
You must be signed in to change notification settings - Fork 2
/
scdbinempty
executable file
·71 lines (61 loc) · 2.26 KB
/
scdbinempty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl
###############################################################################
# Copyright (c) 2014 by bgvanbur
#
# 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.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
###############################################################################
# replace part of a binary file with the contents of another binary file
###############################################################################
use strict;
use warnings;
if ( $#ARGV != 2 ) {
&Help();
}
my $type = $ARGV[0];
my $fileName = $ARGV[1];
my $length = $ARGV[2];
if ( $length =~ m/^(\$|0x)([0-9A-F]+)$/ ) {
$length = hex($2);
}
my $data;
if ( $type =~ m/^00$/i ) {
$data = chr(0x00) x $length;
} elsif ( $type =~ m/^ff$/i ) {
$data = chr(0xFF) x $length;
} elsif ( $type =~ m/^([0-9A-F]){2}$/i ) {
$data = chr(hex($type)) x $length;
} elsif ( $type =~ m/^([0-9A-F]){4}$/i ) {
$data = substr(((chr(hex(substr($type,0,2))).chr(hex(substr($type,2,2)))) x (($length+1)>>1)),0,$length);
} elsif ( $type =~ m/^tilepattern$/i ) {
$data = substr(((chr(0x01).chr(0x23).chr(0x45).chr(0x67).chr(0x89).chr(0xAB).chr(0xCD).chr(0xEF)) x (($length+7)>>3)),0,$length);
} else {
die "invalid type: $type\n";
}
open( FILE, ">$fileName" ) or die "Cannot write $fileName\n";
binmode FILE;
print FILE $data;
close FILE;
sub Help {
die '
scdbinempty <type> <file> <length>
[description]
Make a file with zeros of size length
<type> 00 fill with 0x00
ff fill with 0xFF
xx fill with hexadecimal byte
xxxx fill with hexadecimal byte pair
tilepattern fill with 0x0123456789ABCDEF
';
}