-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinbuild
More file actions
180 lines (156 loc) · 3.39 KB
/
binbuild
File metadata and controls
180 lines (156 loc) · 3.39 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/ruby
#*********************************************************************
# Binary file builder
#
# written by Tsuyoshi HASEGAWA
#*********************************************************************
#-------------------------------
# command handler
#-------------------------------
def exec_coms( coms )
if ( coms[0] == "name" )
if ( coms.length < 2 )
printf "Error: Illegal name command in %d\n", $infile.lineno
exit(-1)
end
if ( $outfile != nil )
$outfile.close
end
$outfile = open( coms[1], "wb" );
return
end
if ( coms[0] == "fill" )
if ( coms.length < 2 )
printf "Error: Illegal fill command in %d\n", $infile.lineno
exit(-1)
end
if ( coms.length >= 3 )
fillval = Integer(coms[2])
else
fillval = 0;
end
Integer(coms[1]).times {
$outfile.putc( fillval )
}
return
end
if ( coms[0] == "file" )
if ( coms.length < 2 )
printf "Error: Illegal file command in %d\n", $infile.lineno
exit(-1)
end
file = open( coms[1], "rb" )
$outfile.write file.read
file.close
return
end
if ( coms[0] == "fint" )
if ( coms.length < 3 )
printf "Error: Illegal file command in %d\n", $infile.lineno
exit(-1)
end
file0 = open( coms[1], "rb" )
file1 = open( coms[2], "rb" )
file0data = file0.read;
file1data = file1.read;
if ( file0data.length >= file1data.length )
fsize = file0data.length
fsize.times {|i|
$outfile.putc(file0data[i])
if ( i < file1data.length )
$outfile.putc(file1data[i])
else
$outfile.putc(0);
end
}
else
fsize = file1data.length
fsize.times {|i|
if ( i < file0data.length )
$outfile.putc(file0data[i])
else
$outfile.putc(0);
end
$outfile.putc(file1data[i])
}
end
file1.close
file0.close
return
end
if ( coms[0] == "offs" )
if ( coms.length < 2 )
printf "Error: Illegal offs command in %d\n", $infile.lineno
exit(-1)
end
$outfile.seek( Integer(coms[1]), IO::SEEK_SET )
return
end
if ( coms[0] == "byte" )
if ( coms.length < 2 )
printf "Error: Illegal byte command in %d\n", $infile.lineno
exit(-1)
end
(coms.length-1).times {|i|
$outfile.putc( Integer(coms[i+1]) )
}
return
end
if ( coms[0] == "adjs" )
if ( coms.length < 2 )
printf "Error: Illegal adjs command in %d\n", $infile.lineno
exit(-1)
end
n = Integer( coms[1] )
n = n - ( $outfile.pos % n );
if ( n > 0 )
$outfile.seek( n-1, IO::SEEK_CUR )
$outfile.putc( 0 );
end
return
end
if ( coms[0] == "disp" )
if ( coms.length < 2 )
printf "Error: Illegal disp command in %d\n", $infile.lineno
exit(-1)
end
printf " %20s - $%08X\n", coms[1], $outfile.pos
return
end
if ( coms[0] == "disp2" )
if ( coms.length < 2 )
printf "Error: Illegal disp2 command in %d\n", $infile.lineno
exit(-1)
end
printf " %20s - $%08X\n", coms[1], $outfile.pos/2
return
end
if ( coms[0] == "cout" )
if ( coms[1] != nil )
print coms[1]
end
print "\n"
return
end
printf "Error: Unknown command \"%s\" in %d\n", coms[0], $infile.lineno
exit(-1)
end
#-------------------------------
# main
#-------------------------------
def exec
if ( ARGV[0] == nil )
print("Usage: binbuild [infile]\n")
return
end
$infile = open( ARGV[0] )
while !$infile.eof? do
text = $infile.gets.chomp!
if ( text.index(/[A-Za-z]./) == 0 )
exec_coms( text.split() )
end
end
$infile.close
$outfile.close
end
exec