xref: /freebsd/bin/dd/conv.c (revision 78b09ffeaf738a9bdc32990fbdffd0f6ae0b0ca5)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
64b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego and Lance
74b88c807SRodney W. Grimes  * Visser of Convex Computer Corporation.
84b88c807SRodney W. Grimes  *
94b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
104b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
114b88c807SRodney W. Grimes  * are met:
124b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
134b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
144b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
164b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
174b88c807SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
184b88c807SRodney W. Grimes  *    must display the following acknowledgement:
194b88c807SRodney W. Grimes  *	This product includes software developed by the University of
204b88c807SRodney W. Grimes  *	California, Berkeley and its contributors.
214b88c807SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
224b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
234b88c807SRodney W. Grimes  *    without specific prior written permission.
244b88c807SRodney W. Grimes  *
254b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
264b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
274b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
284b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
294b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
304b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
314b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
324b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
334b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
344b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
354b88c807SRodney W. Grimes  * SUCH DAMAGE.
3689730b29SDavid Greenman  *
3778b09ffeSSteve Price  *	$Id: conv.c,v 1.5 1996/11/13 19:59:56 phk Exp $
384b88c807SRodney W. Grimes  */
394b88c807SRodney W. Grimes 
404b88c807SRodney W. Grimes #ifndef lint
4178b09ffeSSteve Price static char const sccsid[] = "@(#)conv.c	8.3 (Berkeley) 4/2/94";
424b88c807SRodney W. Grimes #endif /* not lint */
434b88c807SRodney W. Grimes 
444b88c807SRodney W. Grimes #include <sys/param.h>
454b88c807SRodney W. Grimes 
464b88c807SRodney W. Grimes #include <err.h>
474b88c807SRodney W. Grimes #include <string.h>
484b88c807SRodney W. Grimes 
494b88c807SRodney W. Grimes #include "dd.h"
504b88c807SRodney W. Grimes #include "extern.h"
514b88c807SRodney W. Grimes 
524b88c807SRodney W. Grimes /*
534b88c807SRodney W. Grimes  * def --
544b88c807SRodney W. Grimes  * Copy input to output.  Input is buffered until reaches obs, and then
554b88c807SRodney W. Grimes  * output until less than obs remains.  Only a single buffer is used.
564b88c807SRodney W. Grimes  * Worst case buffer calculation is (ibs + obs - 1).
574b88c807SRodney W. Grimes  */
584b88c807SRodney W. Grimes void
594b88c807SRodney W. Grimes def()
604b88c807SRodney W. Grimes {
614b88c807SRodney W. Grimes 	int cnt;
624b88c807SRodney W. Grimes 	u_char *inp, *t;
634b88c807SRodney W. Grimes 
64ad66f7eeSPoul-Henning Kamp 	if ((t = ctab) != NULL)
654b88c807SRodney W. Grimes 		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
664b88c807SRodney W. Grimes 			*inp = t[*inp];
674b88c807SRodney W. Grimes 
684b88c807SRodney W. Grimes 	/* Make the output buffer look right. */
694b88c807SRodney W. Grimes 	out.dbp = in.dbp;
704b88c807SRodney W. Grimes 	out.dbcnt = in.dbcnt;
714b88c807SRodney W. Grimes 
724b88c807SRodney W. Grimes 	if (in.dbcnt >= out.dbsz) {
734b88c807SRodney W. Grimes 		/* If the output buffer is full, write it. */
744b88c807SRodney W. Grimes 		dd_out(0);
754b88c807SRodney W. Grimes 
764b88c807SRodney W. Grimes 		/*
774b88c807SRodney W. Grimes 		 * Ddout copies the leftover output to the beginning of
784b88c807SRodney W. Grimes 		 * the buffer and resets the output buffer.  Reset the
794b88c807SRodney W. Grimes 		 * input buffer to match it.
804b88c807SRodney W. Grimes 	 	 */
814b88c807SRodney W. Grimes 		in.dbp = out.dbp;
824b88c807SRodney W. Grimes 		in.dbcnt = out.dbcnt;
834b88c807SRodney W. Grimes 	}
844b88c807SRodney W. Grimes }
854b88c807SRodney W. Grimes 
864b88c807SRodney W. Grimes void
874b88c807SRodney W. Grimes def_close()
884b88c807SRodney W. Grimes {
894b88c807SRodney W. Grimes 	/* Just update the count, everything is already in the buffer. */
904b88c807SRodney W. Grimes 	if (in.dbcnt)
914b88c807SRodney W. Grimes 		out.dbcnt = in.dbcnt;
924b88c807SRodney W. Grimes }
934b88c807SRodney W. Grimes 
944b88c807SRodney W. Grimes /*
954b88c807SRodney W. Grimes  * Copy variable length newline terminated records with a max size cbsz
964b88c807SRodney W. Grimes  * bytes to output.  Records less than cbs are padded with spaces.
974b88c807SRodney W. Grimes  *
984b88c807SRodney W. Grimes  * max in buffer:  MAX(ibs, cbsz)
994b88c807SRodney W. Grimes  * max out buffer: obs + cbsz
1004b88c807SRodney W. Grimes  */
1014b88c807SRodney W. Grimes void
1024b88c807SRodney W. Grimes block()
1034b88c807SRodney W. Grimes {
1044b88c807SRodney W. Grimes 	static int intrunc;
1054b88c807SRodney W. Grimes 	int ch, cnt, maxlen;
1064b88c807SRodney W. Grimes 	u_char *inp, *outp, *t;
1074b88c807SRodney W. Grimes 
1084b88c807SRodney W. Grimes 	/*
1094b88c807SRodney W. Grimes 	 * Record truncation can cross block boundaries.  If currently in a
1104b88c807SRodney W. Grimes 	 * truncation state, keep tossing characters until reach a newline.
1114b88c807SRodney W. Grimes 	 * Start at the beginning of the buffer, as the input buffer is always
1124b88c807SRodney W. Grimes 	 * left empty.
1134b88c807SRodney W. Grimes 	 */
1144b88c807SRodney W. Grimes 	if (intrunc) {
1154b88c807SRodney W. Grimes 		for (inp = in.db, cnt = in.dbrcnt;
1164b88c807SRodney W. Grimes 		    cnt && *inp++ != '\n'; --cnt);
1174b88c807SRodney W. Grimes 		if (!cnt) {
1184b88c807SRodney W. Grimes 			in.dbcnt = 0;
1194b88c807SRodney W. Grimes 			in.dbp = in.db;
1204b88c807SRodney W. Grimes 			return;
1214b88c807SRodney W. Grimes 		}
1224b88c807SRodney W. Grimes 		intrunc = 0;
1234b88c807SRodney W. Grimes 		/* Adjust the input buffer numbers. */
1244b88c807SRodney W. Grimes 		in.dbcnt = cnt - 1;
1254b88c807SRodney W. Grimes 		in.dbp = inp + cnt - 1;
1264b88c807SRodney W. Grimes 	}
1274b88c807SRodney W. Grimes 
1284b88c807SRodney W. Grimes 	/*
1294b88c807SRodney W. Grimes 	 * Copy records (max cbsz size chunks) into the output buffer.  The
1304b88c807SRodney W. Grimes 	 * translation is done as we copy into the output buffer.
1314b88c807SRodney W. Grimes 	 */
13278b09ffeSSteve Price 	ch = 0;
1334b88c807SRodney W. Grimes 	for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
1344b88c807SRodney W. Grimes 		maxlen = MIN(cbsz, in.dbcnt);
135ad66f7eeSPoul-Henning Kamp 		if ((t = ctab) != NULL)
1364b88c807SRodney W. Grimes 			for (cnt = 0;
1374b88c807SRodney W. Grimes 			    cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
1384b88c807SRodney W. Grimes 				*outp++ = t[ch];
1394b88c807SRodney W. Grimes 		else
1404b88c807SRodney W. Grimes 			for (cnt = 0;
1414b88c807SRodney W. Grimes 			    cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
1424b88c807SRodney W. Grimes 				*outp++ = ch;
1434b88c807SRodney W. Grimes 		/*
1444b88c807SRodney W. Grimes 		 * Check for short record without a newline.  Reassemble the
1454b88c807SRodney W. Grimes 		 * input block.
1464b88c807SRodney W. Grimes 		 */
1474b88c807SRodney W. Grimes 		if (ch != '\n' && in.dbcnt < cbsz) {
1484b88c807SRodney W. Grimes 			memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
1494b88c807SRodney W. Grimes 			break;
1504b88c807SRodney W. Grimes 		}
1514b88c807SRodney W. Grimes 
1524b88c807SRodney W. Grimes 		/* Adjust the input buffer numbers. */
1534b88c807SRodney W. Grimes 		in.dbcnt -= cnt;
1544b88c807SRodney W. Grimes 		if (ch == '\n')
1554b88c807SRodney W. Grimes 			--in.dbcnt;
1564b88c807SRodney W. Grimes 
1574b88c807SRodney W. Grimes 		/* Pad short records with spaces. */
1584b88c807SRodney W. Grimes 		if (cnt < cbsz)
1594b88c807SRodney W. Grimes 			(void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
1604b88c807SRodney W. Grimes 		else {
1614b88c807SRodney W. Grimes 			/*
1624b88c807SRodney W. Grimes 			 * If the next character wouldn't have ended the
1634b88c807SRodney W. Grimes 			 * block, it's a truncation.
1644b88c807SRodney W. Grimes 			 */
1654b88c807SRodney W. Grimes 			if (!in.dbcnt || *inp != '\n')
1664b88c807SRodney W. Grimes 				++st.trunc;
1674b88c807SRodney W. Grimes 
1684b88c807SRodney W. Grimes 			/* Toss characters to a newline. */
1694b88c807SRodney W. Grimes 			for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
1704b88c807SRodney W. Grimes 			if (!in.dbcnt)
1714b88c807SRodney W. Grimes 				intrunc = 1;
1724b88c807SRodney W. Grimes 			else
1734b88c807SRodney W. Grimes 				--in.dbcnt;
1744b88c807SRodney W. Grimes 		}
1754b88c807SRodney W. Grimes 
1764b88c807SRodney W. Grimes 		/* Adjust output buffer numbers. */
1774b88c807SRodney W. Grimes 		out.dbp += cbsz;
1784b88c807SRodney W. Grimes 		if ((out.dbcnt += cbsz) >= out.dbsz)
1794b88c807SRodney W. Grimes 			dd_out(0);
1804b88c807SRodney W. Grimes 		outp = out.dbp;
1814b88c807SRodney W. Grimes 	}
1824b88c807SRodney W. Grimes 	in.dbp = in.db + in.dbcnt;
1834b88c807SRodney W. Grimes }
1844b88c807SRodney W. Grimes 
1854b88c807SRodney W. Grimes void
1864b88c807SRodney W. Grimes block_close()
1874b88c807SRodney W. Grimes {
1884b88c807SRodney W. Grimes 	/*
1894b88c807SRodney W. Grimes 	 * Copy any remaining data into the output buffer and pad to a record.
1904b88c807SRodney W. Grimes 	 * Don't worry about truncation or translation, the input buffer is
1914b88c807SRodney W. Grimes 	 * always empty when truncating, and no characters have been added for
1924b88c807SRodney W. Grimes 	 * translation.  The bottom line is that anything left in the input
1934b88c807SRodney W. Grimes 	 * buffer is a truncated record.  Anything left in the output buffer
1944b88c807SRodney W. Grimes 	 * just wasn't big enough.
1954b88c807SRodney W. Grimes 	 */
1964b88c807SRodney W. Grimes 	if (in.dbcnt) {
1974b88c807SRodney W. Grimes 		++st.trunc;
1984b88c807SRodney W. Grimes 		memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
1994b88c807SRodney W. Grimes 		(void)memset(out.dbp + in.dbcnt,
2004b88c807SRodney W. Grimes 		    ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
2014b88c807SRodney W. Grimes 		out.dbcnt += cbsz;
2024b88c807SRodney W. Grimes 	}
2034b88c807SRodney W. Grimes }
2044b88c807SRodney W. Grimes 
2054b88c807SRodney W. Grimes /*
2064b88c807SRodney W. Grimes  * Convert fixed length (cbsz) records to variable length.  Deletes any
2074b88c807SRodney W. Grimes  * trailing blanks and appends a newline.
2084b88c807SRodney W. Grimes  *
2094b88c807SRodney W. Grimes  * max in buffer:  MAX(ibs, cbsz) + cbsz
2104b88c807SRodney W. Grimes  * max out buffer: obs + cbsz
2114b88c807SRodney W. Grimes  */
2124b88c807SRodney W. Grimes void
2134b88c807SRodney W. Grimes unblock()
2144b88c807SRodney W. Grimes {
2154b88c807SRodney W. Grimes 	int cnt;
2164b88c807SRodney W. Grimes 	u_char *inp, *t;
2174b88c807SRodney W. Grimes 
2184b88c807SRodney W. Grimes 	/* Translation and case conversion. */
219ad66f7eeSPoul-Henning Kamp 	if ((t = ctab) != NULL)
2204b88c807SRodney W. Grimes 		for (cnt = in.dbrcnt, inp = in.dbp; cnt--;)
2214b88c807SRodney W. Grimes 			*--inp = t[*inp];
2224b88c807SRodney W. Grimes 	/*
2234b88c807SRodney W. Grimes 	 * Copy records (max cbsz size chunks) into the output buffer.  The
2244b88c807SRodney W. Grimes 	 * translation has to already be done or we might not recognize the
2254b88c807SRodney W. Grimes 	 * spaces.
2264b88c807SRodney W. Grimes 	 */
2274b88c807SRodney W. Grimes 	for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
2284b88c807SRodney W. Grimes 		for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
2294b88c807SRodney W. Grimes 		if (t >= inp) {
2304b88c807SRodney W. Grimes 			cnt = t - inp + 1;
2314b88c807SRodney W. Grimes 			memmove(out.dbp, inp, cnt);
2324b88c807SRodney W. Grimes 			out.dbp += cnt;
2334b88c807SRodney W. Grimes 			out.dbcnt += cnt;
2344b88c807SRodney W. Grimes 		}
2354b88c807SRodney W. Grimes 		++out.dbcnt;
2364b88c807SRodney W. Grimes 		*out.dbp++ = '\n';
2374b88c807SRodney W. Grimes 		if (out.dbcnt >= out.dbsz)
2384b88c807SRodney W. Grimes 			dd_out(0);
2394b88c807SRodney W. Grimes 	}
2404b88c807SRodney W. Grimes 	if (in.dbcnt)
2414b88c807SRodney W. Grimes 		memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
2424b88c807SRodney W. Grimes 	in.dbp = in.db + in.dbcnt;
2434b88c807SRodney W. Grimes }
2444b88c807SRodney W. Grimes 
2454b88c807SRodney W. Grimes void
2464b88c807SRodney W. Grimes unblock_close()
2474b88c807SRodney W. Grimes {
2484b88c807SRodney W. Grimes 	int cnt;
2494b88c807SRodney W. Grimes 	u_char *t;
2504b88c807SRodney W. Grimes 
2514b88c807SRodney W. Grimes 	if (in.dbcnt) {
2524b88c807SRodney W. Grimes 		warnx("%s: short input record", in.name);
2534b88c807SRodney W. Grimes 		for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t);
2544b88c807SRodney W. Grimes 		if (t >= in.db) {
2554b88c807SRodney W. Grimes 			cnt = t - in.db + 1;
2564b88c807SRodney W. Grimes 			memmove(out.dbp, in.db, cnt);
2574b88c807SRodney W. Grimes 			out.dbp += cnt;
2584b88c807SRodney W. Grimes 			out.dbcnt += cnt;
2594b88c807SRodney W. Grimes 		}
2604b88c807SRodney W. Grimes 		++out.dbcnt;
2614b88c807SRodney W. Grimes 		*out.dbp++ = '\n';
2624b88c807SRodney W. Grimes 	}
2634b88c807SRodney W. Grimes }
264