1.\"	$OpenBSD: binedit.1,v 1.1 2016/07/30 10:56:13 schwarze Exp $
2.\"
3.\" Copyright (c) 2016 Ingo Schwarze <schwarze@openbsd.org>
4.\"
5.\" Permission to use, copy, modify, and distribute this software for any
6.\" purpose with or without fee is hereby granted, provided that the above
7.\" copyright notice and this permission notice appear in all copies.
8.\"
9.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16.\"
17.Dd $Mdocdate: July 30 2016 $
18.Dt BINEDIT 1
19.Os
20.Sh NAME
21.Nm binedit
22.Nd non-interactive binary file editor
23.Sh SYNOPSIS
24.Nm binedit
25.Ar command_string
26.Sh DESCRIPTION
27The
28.Nm
29utility reads a string of arbitrary bytes from the standard input,
30edits it according to the
31.Ar command_string ,
32and writes the resulting string of bytes to the standard output.
33.Pp
34The
35.Ar command_string
36consists of an arbitrary number of one-letter commands.
37Some commands take a numeric argument.
38No delimiters are used.
39.Pp
40Commands use two number registers:
41A
42.Va value
43set by
44.Ic r
45and
46.Ic w ,
47modfied by
48.Ic i ,
49and used by
50.Ic d ,
51and a
52.Va destination
53set by
54.Ic d
55and used by
56.Ic a .
57.Pp
58All numbers are signed 32-bit integers.
59On the command line, they are given in decimal notation,
60optionally preceded by a sign.
61In the input and output streams, they are represented in
62big endian (network) byte order.
63.Pp
64The commands are as follows:
65.Bl -tag -width Ds
66.It Ic a
67Advance: copy bytes up to and including the input
68.Va destination
69set with the
70.Ic d
71command.
72.It Ic c Ar bytes
73Copy the specified number of
74.Ar bytes .
75.It Ic d
76Set the input file
77.Va destination
78to the saved
79.Va value .
80Bytes are counted from 1.
81.It Ic f
82Finish: copy all remaining bytes.
83This command can only appear once at the end of the
84.Ar command_string .
85.It Ic i Op Ar amount
86Increment the saved
87.Va value
88by the given
89.Ar amount ,
90by default 1.
91.It Ic r
92Read one number from the standard input and save the
93.Va value .
94.It Ic s Ar bytes
95Skip the specified number of
96.Ar bytes
97on the standard input.
98.It Ic w Op Ar value
99Write the given
100.Ar value
101and also save it.
102If no argument is given, the saved
103.Va value
104is written.
105.El
106.Sh EXIT STATUS
107.Ex -std
108.Sh EXAMPLES
109See the file
110.Pa /usr/src/regress/usr.bin/mandoc/db/run/Makefile
111for several examples.
112For example, with the
113.Ar command_string
114.Ic c12rdwariwf ,
115.Nm
116reads a valid
117.Xr mandoc_db 5
118file from the standard input and prints a corrupted version to the
119standard output where the final magic is incremented by one.
120.Sh DIAGNOSTICS
121Error messages are formatted as follows:
122.Pp
123.D1 Ar command : message Ns Op Ns : Ar errno_message
124.Pp
125The
126.Ar message
127strings are as follows:
128.Bl -tag -width Ds
129.It Ar command : Sy EOF
130The input file ended prematurely.
131Can occur during the
132.Ic a ,
133.Ic c ,
134.Ic r ,
135and
136.Ic s
137commands.
138.It Ar command : Sy getchar : Ar errno_message
139The
140.Xr getchar 3
141function failed.
142Can occur during the
143.Ic a ,
144.Ic c ,
145.Ic f ,
146and
147.Ic s
148commands.
149.It Ic r : Sy fread : Ar errno_message
150The
151.Xr fread 3
152function failed while processing an
153.Ic r
154command.
155.It Ic w : Sy fwrite : Ar errno_message
156The
157.Xr fwrite 3
158function failed while processing a
159.Ic w
160command.
161.It Ic f Ns ... : Sy not the last command
162An
163.Ic f
164command was encountered before the end of the
165.Ar command_string .
166.It Ar command : Sy invalid command
167An invalid byte was found in the
168.Ar command_string ,
169or an argument was given after a command that does not accept one.
170.El
171.Sh AUTHORS
172.An Ingo Schwarze Aq Mt schwarze@openbsd.org
173