conv.c revision 19720
1/*-
2 * Copyright (c) 1991, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Keith Muller of the University of California, San Diego and Lance
7 * Visser of Convex Computer Corporation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 *    must display the following acknowledgement:
19 *	This product includes software developed by the University of
20 *	California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 *    may be used to endorse or promote products derived from this software
23 *    without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 *	$Id: conv.c,v 1.4 1996/11/12 23:09:04 phk Exp $
38 */
39
40#ifndef lint
41static char sccsid[] = "@(#)conv.c	8.3 (Berkeley) 4/2/94";
42#endif /* not lint */
43
44#include <sys/param.h>
45
46#include <err.h>
47#include <string.h>
48
49#include "dd.h"
50#include "extern.h"
51
52/*
53 * def --
54 * Copy input to output.  Input is buffered until reaches obs, and then
55 * output until less than obs remains.  Only a single buffer is used.
56 * Worst case buffer calculation is (ibs + obs - 1).
57 */
58void
59def()
60{
61	int cnt;
62	u_char *inp, *t;
63
64	if ((t = ctab) != NULL)
65		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
66			*inp = t[*inp];
67
68	/* Make the output buffer look right. */
69	out.dbp = in.dbp;
70	out.dbcnt = in.dbcnt;
71
72	if (in.dbcnt >= out.dbsz) {
73		/* If the output buffer is full, write it. */
74		dd_out(0);
75
76		/*
77		 * Ddout copies the leftover output to the beginning of
78		 * the buffer and resets the output buffer.  Reset the
79		 * input buffer to match it.
80	 	 */
81		in.dbp = out.dbp;
82		in.dbcnt = out.dbcnt;
83	}
84}
85
86void
87def_close()
88{
89	/* Just update the count, everything is already in the buffer. */
90	if (in.dbcnt)
91		out.dbcnt = in.dbcnt;
92}
93
94/*
95 * Copy variable length newline terminated records with a max size cbsz
96 * bytes to output.  Records less than cbs are padded with spaces.
97 *
98 * max in buffer:  MAX(ibs, cbsz)
99 * max out buffer: obs + cbsz
100 */
101void
102block()
103{
104	static int intrunc;
105	int ch, cnt, maxlen;
106	u_char *inp, *outp, *t;
107
108	/*
109	 * Record truncation can cross block boundaries.  If currently in a
110	 * truncation state, keep tossing characters until reach a newline.
111	 * Start at the beginning of the buffer, as the input buffer is always
112	 * left empty.
113	 */
114	if (intrunc) {
115		for (inp = in.db, cnt = in.dbrcnt;
116		    cnt && *inp++ != '\n'; --cnt);
117		if (!cnt) {
118			in.dbcnt = 0;
119			in.dbp = in.db;
120			return;
121		}
122		intrunc = 0;
123		/* Adjust the input buffer numbers. */
124		in.dbcnt = cnt - 1;
125		in.dbp = inp + cnt - 1;
126	}
127
128	/*
129	 * Copy records (max cbsz size chunks) into the output buffer.  The
130	 * translation is done as we copy into the output buffer.
131	 */
132	ch = 0;			/* Help the compiler. */
133	for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
134		maxlen = MIN(cbsz, in.dbcnt);
135		if ((t = ctab) != NULL)
136			for (cnt = 0;
137			    cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
138				*outp++ = t[ch];
139		else
140			for (cnt = 0;
141			    cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
142				*outp++ = ch;
143		/*
144		 * Check for short record without a newline.  Reassemble the
145		 * input block.
146		 */
147		if (ch != '\n' && in.dbcnt < cbsz) {
148			memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
149			break;
150		}
151
152		/* Adjust the input buffer numbers. */
153		in.dbcnt -= cnt;
154		if (ch == '\n')
155			--in.dbcnt;
156
157		/* Pad short records with spaces. */
158		if (cnt < cbsz)
159			(void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
160		else {
161			/*
162			 * If the next character wouldn't have ended the
163			 * block, it's a truncation.
164			 */
165			if (!in.dbcnt || *inp != '\n')
166				++st.trunc;
167
168			/* Toss characters to a newline. */
169			for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
170			if (!in.dbcnt)
171				intrunc = 1;
172			else
173				--in.dbcnt;
174		}
175
176		/* Adjust output buffer numbers. */
177		out.dbp += cbsz;
178		if ((out.dbcnt += cbsz) >= out.dbsz)
179			dd_out(0);
180		outp = out.dbp;
181	}
182	in.dbp = in.db + in.dbcnt;
183}
184
185void
186block_close()
187{
188	/*
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.
195	 */
196	if (in.dbcnt) {
197		++st.trunc;
198		memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
199		(void)memset(out.dbp + in.dbcnt,
200		    ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
201		out.dbcnt += cbsz;
202	}
203}
204
205/*
206 * Convert fixed length (cbsz) records to variable length.  Deletes any
207 * trailing blanks and appends a newline.
208 *
209 * max in buffer:  MAX(ibs, cbsz) + cbsz
210 * max out buffer: obs + cbsz
211 */
212void
213unblock()
214{
215	int cnt;
216	u_char *inp, *t;
217
218	/* Translation and case conversion. */
219	if ((t = ctab) != NULL)
220		for (cnt = in.dbrcnt, inp = in.dbp; cnt--;)
221			*--inp = t[*inp];
222	/*
223	 * Copy records (max cbsz size chunks) into the output buffer.  The
224	 * translation has to already be done or we might not recognize the
225	 * spaces.
226	 */
227	for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
228		for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
229		if (t >= inp) {
230			cnt = t - inp + 1;
231			memmove(out.dbp, inp, cnt);
232			out.dbp += cnt;
233			out.dbcnt += cnt;
234		}
235		++out.dbcnt;
236		*out.dbp++ = '\n';
237		if (out.dbcnt >= out.dbsz)
238			dd_out(0);
239	}
240	if (in.dbcnt)
241		memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
242	in.dbp = in.db + in.dbcnt;
243}
244
245void
246unblock_close()
247{
248	int cnt;
249	u_char *t;
250
251	if (in.dbcnt) {
252		warnx("%s: short input record", in.name);
253		for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t);
254		if (t >= in.db) {
255			cnt = t - in.db + 1;
256			memmove(out.dbp, in.db, cnt);
257			out.dbp += cnt;
258			out.dbcnt += cnt;
259		}
260		++out.dbcnt;
261		*out.dbp++ = '\n';
262	}
263}
264