2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Keith Muller of the University of California, San Diego and Lance
9 * Visser of Convex Computer Corporation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * @(#)conv.c 8.3 (Berkeley) 4/2/94
36 * $FreeBSD: head/bin/dd/conv.c 326025 2017-11-20 19:49:47Z pfg $
39 #include <sys/param.h>
49 * Copy input to output. Input is buffered until reaches obs, and then
50 * output until less than obs remains. Only a single buffer is used.
51 * Worst case buffer calculation is (ibs + obs - 1).
60 if ((t
= ctab
) != NULL
)
61 for (inp
= in
.dbp
- (cnt
= in
.dbrcnt
); cnt
--; ++inp
)
64 /* Make the output buffer look right. */
68 if (in
.dbcnt
>= out
.dbsz
) {
69 /* If the output buffer is full, write it. */
73 * dd_out copies the leftover output to the beginning of
74 * the buffer and resets the output buffer. Reset the
75 * input buffer to match it.
85 /* Just update the count, everything is already in the buffer. */
91 * Copy variable length newline terminated records with a max size cbsz
92 * bytes to output. Records less than cbs are padded with spaces.
94 * max in buffer: MAX(ibs, cbsz)
95 * max out buffer: obs + cbsz
107 * Record truncation can cross block boundaries. If currently in a
108 * truncation state, keep tossing characters until reach a newline.
109 * Start at the beginning of the buffer, as the input buffer is always
113 for (inp
= in
.db
, cnt
= in
.dbrcnt
; cnt
&& *inp
++ != '\n'; --cnt
)
121 /* Adjust the input buffer numbers. */
123 in
.dbp
= inp
+ cnt
- 1;
127 * Copy records (max cbsz size chunks) into the output buffer. The
128 * translation is done as we copy into the output buffer.
131 for (inp
= in
.dbp
- in
.dbcnt
, outp
= out
.dbp
; in
.dbcnt
;) {
132 maxlen
= MIN(cbsz
, (size_t)in
.dbcnt
);
133 if ((t
= ctab
) != NULL
) {
134 for (cnt
= 0; cnt
< maxlen
&& (ch
= *inp
++) != '\n';
138 for (cnt
= 0; cnt
< maxlen
&& (ch
= *inp
++) != '\n';
143 * Check for short record without a newline. Reassemble the
146 if (ch
!= '\n' && (size_t)in
.dbcnt
< cbsz
) {
147 memmove(in
.db
, in
.dbp
- in
.dbcnt
, in
.dbcnt
);
151 /* Adjust the input buffer numbers. */
156 /* Pad short records with spaces. */
158 memset(outp
, ctab
? ctab
[' '] : ' ', cbsz
- cnt
);
161 * If the next character wouldn't have ended the
162 * block, it's a truncation.
164 if (!in
.dbcnt
|| *inp
!= '\n')
167 /* Toss characters to a newline. */
168 for (; in
.dbcnt
&& *inp
++ != '\n'; --in
.dbcnt
)
176 /* Adjust output buffer numbers. */
178 if ((out
.dbcnt
+= cbsz
) >= out
.dbsz
)
182 in
.dbp
= in
.db
+ in
.dbcnt
;
189 * Copy any remaining data into the output buffer and pad to a record.
190 * Don't worry about truncation or translation, the input buffer is
191 * always empty when truncating, and no characters have been added for
192 * translation. The bottom line is that anything left in the input
193 * buffer is a truncated record. Anything left in the output buffer
194 * just wasn't big enough.
198 memmove(out
.dbp
, in
.dbp
- in
.dbcnt
, in
.dbcnt
);
199 memset(out
.dbp
+ in
.dbcnt
, ctab
? ctab
[' '] : ' ',
206 * Convert fixed length (cbsz) records to variable length. Deletes any
207 * trailing blanks and appends a newline.
209 * max in buffer: MAX(ibs, cbsz) + cbsz
210 * max out buffer: obs + cbsz
219 /* Translation and case conversion. */
220 if ((t
= ctab
) != NULL
)
221 for (inp
= in
.dbp
- (cnt
= in
.dbrcnt
); cnt
--; ++inp
)
224 * Copy records (max cbsz size chunks) into the output buffer. The
225 * translation has to already be done or we might not recognize the
228 for (inp
= in
.db
; (size_t)in
.dbcnt
>= cbsz
; inp
+= cbsz
, in
.dbcnt
-= cbsz
) {
229 for (t
= inp
+ cbsz
- 1; t
>= inp
&& *t
== ' '; --t
)
233 memmove(out
.dbp
, inp
, cnt
);
238 if (++out
.dbcnt
>= out
.dbsz
)
242 memmove(in
.db
, in
.dbp
- in
.dbcnt
, in
.dbcnt
);
243 in
.dbp
= in
.db
+ in
.dbcnt
;
253 warnx("%s: short input record", in
.name
);
254 for (t
= in
.db
+ in
.dbcnt
- 1; t
>= in
.db
&& *t
== ' '; --t
)
258 memmove(out
.dbp
, in
.db
, cnt
);