1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1991, 1993, 1994
5 *	The Regents of the University of California.  All rights reserved.
6 *
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.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
22 *
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
33 * SUCH DAMAGE.
34 */
35
36#ifndef lint
37#if 0
38static char sccsid[] = "@(#)conv.c	8.3 (Berkeley) 4/2/94";
39#endif
40#endif /* not lint */
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD$");
43
44#include <sys/param.h>
45
46#include <err.h>
47#include <inttypes.h>
48#include <string.h>
49
50#include "dd.h"
51#include "extern.h"
52
53/*
54 * def --
55 * Copy input to output.  Input is buffered until reaches obs, and then
56 * output until less than obs remains.  Only a single buffer is used.
57 * Worst case buffer calculation is (ibs + obs - 1).
58 */
59void
60def(void)
61{
62	u_char *inp;
63	const u_char *t;
64	size_t cnt;
65
66	if ((t = ctab) != NULL)
67		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
68			*inp = t[*inp];
69
70	/* Make the output buffer look right. */
71	out.dbp = in.dbp;
72	out.dbcnt = in.dbcnt;
73
74	if (in.dbcnt >= out.dbsz) {
75		/* If the output buffer is full, write it. */
76		dd_out(0);
77
78		/*
79		 * dd_out copies the leftover output to the beginning of
80		 * the buffer and resets the output buffer.  Reset the
81		 * input buffer to match it.
82	 	 */
83		in.dbp = out.dbp;
84		in.dbcnt = out.dbcnt;
85	}
86}
87
88void
89def_close(void)
90{
91	/* Just update the count, everything is already in the buffer. */
92	if (in.dbcnt)
93		out.dbcnt = in.dbcnt;
94}
95
96/*
97 * Copy variable length newline terminated records with a max size cbsz
98 * bytes to output.  Records less than cbs are padded with spaces.
99 *
100 * max in buffer:  MAX(ibs, cbsz)
101 * max out buffer: obs + cbsz
102 */
103void
104block(void)
105{
106	u_char *inp, *outp;
107	const u_char *t;
108	size_t cnt, maxlen;
109	static int intrunc;
110	int ch;
111
112	/*
113	 * Record truncation can cross block boundaries.  If currently in a
114	 * truncation state, keep tossing characters until reach a newline.
115	 * Start at the beginning of the buffer, as the input buffer is always
116	 * left empty.
117	 */
118	if (intrunc) {
119		for (inp = in.db, cnt = in.dbrcnt; cnt && *inp++ != '\n'; --cnt)
120			;
121		if (!cnt) {
122			in.dbcnt = 0;
123			in.dbp = in.db;
124			return;
125		}
126		intrunc = 0;
127		/* Adjust the input buffer numbers. */
128		in.dbcnt = cnt - 1;
129		in.dbp = inp + cnt - 1;
130	}
131
132	/*
133	 * Copy records (max cbsz size chunks) into the output buffer.  The
134	 * translation is done as we copy into the output buffer.
135	 */
136	ch = 0;
137	for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
138		maxlen = MIN(cbsz, (size_t)in.dbcnt);
139		if ((t = ctab) != NULL)
140			for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
141			    ++cnt)
142				*outp++ = t[ch];
143		else
144			for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
145			    ++cnt)
146				*outp++ = ch;
147		/*
148		 * Check for short record without a newline.  Reassemble the
149		 * input block.
150		 */
151		if (ch != '\n' && (size_t)in.dbcnt < cbsz) {
152			(void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
153			break;
154		}
155
156		/* Adjust the input buffer numbers. */
157		in.dbcnt -= cnt;
158		if (ch == '\n')
159			--in.dbcnt;
160
161		/* Pad short records with spaces. */
162		if (cnt < cbsz)
163			(void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
164		else {
165			/*
166			 * If the next character wouldn't have ended the
167			 * block, it's a truncation.
168			 */
169			if (!in.dbcnt || *inp != '\n')
170				++st.trunc;
171
172			/* Toss characters to a newline. */
173			for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt)
174				;
175			if (!in.dbcnt)
176				intrunc = 1;
177			else
178				--in.dbcnt;
179		}
180
181		/* Adjust output buffer numbers. */
182		out.dbp += cbsz;
183		if ((out.dbcnt += cbsz) >= out.dbsz)
184			dd_out(0);
185		outp = out.dbp;
186	}
187	in.dbp = in.db + in.dbcnt;
188}
189
190void
191block_close(void)
192{
193	/*
194	 * Copy any remaining data into the output buffer and pad to a record.
195	 * Don't worry about truncation or translation, the input buffer is
196	 * always empty when truncating, and no characters have been added for
197	 * translation.  The bottom line is that anything left in the input
198	 * buffer is a truncated record.  Anything left in the output buffer
199	 * just wasn't big enough.
200	 */
201	if (in.dbcnt) {
202		++st.trunc;
203		(void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
204		(void)memset(out.dbp + in.dbcnt, ctab ? ctab[' '] : ' ',
205		    cbsz - in.dbcnt);
206		out.dbcnt += cbsz;
207	}
208}
209
210/*
211 * Convert fixed length (cbsz) records to variable length.  Deletes any
212 * trailing blanks and appends a newline.
213 *
214 * max in buffer:  MAX(ibs, cbsz) + cbsz
215 * max out buffer: obs + cbsz
216 */
217void
218unblock(void)
219{
220	u_char *inp;
221	const u_char *t;
222	size_t cnt;
223
224	/* Translation and case conversion. */
225	if ((t = ctab) != NULL)
226		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
227			*inp = t[*inp];
228	/*
229	 * Copy records (max cbsz size chunks) into the output buffer.  The
230	 * translation has to already be done or we might not recognize the
231	 * spaces.
232	 */
233	for (inp = in.db; (size_t)in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
234		for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t)
235			;
236		if (t >= inp) {
237			cnt = t - inp + 1;
238			(void)memmove(out.dbp, inp, cnt);
239			out.dbp += cnt;
240			out.dbcnt += cnt;
241		}
242		*out.dbp++ = '\n';
243		if (++out.dbcnt >= out.dbsz)
244			dd_out(0);
245	}
246	if (in.dbcnt)
247		(void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
248	in.dbp = in.db + in.dbcnt;
249}
250
251void
252unblock_close(void)
253{
254	u_char *t;
255	size_t cnt;
256
257	if (in.dbcnt) {
258		warnx("%s: short input record", in.name);
259		for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t)
260			;
261		if (t >= in.db) {
262			cnt = t - in.db + 1;
263			(void)memmove(out.dbp, in.db, cnt);
264			out.dbp += cnt;
265			out.dbcnt += cnt;
266		}
267		++out.dbcnt;
268		*out.dbp++ = '\n';
269	}
270}
271