xref: /freebsd/bin/dd/conv.c (revision e043f37205ffbde5627ff299ad25cd532f2956f0)
14b88c807SRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993, 1994
54b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
64b88c807SRodney W. Grimes  *
74b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
84b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego and Lance
94b88c807SRodney W. Grimes  * Visser of Convex Computer Corporation.
104b88c807SRodney W. Grimes  *
114b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
124b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
134b88c807SRodney W. Grimes  * are met:
144b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
164b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
174b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
184b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
204b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
214b88c807SRodney W. Grimes  *    without specific prior written permission.
224b88c807SRodney W. Grimes  *
234b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334b88c807SRodney W. Grimes  * SUCH DAMAGE.
344b88c807SRodney W. Grimes  */
354b88c807SRodney W. Grimes 
364b88c807SRodney W. Grimes #include <sys/param.h>
374b88c807SRodney W. Grimes 
384b88c807SRodney W. Grimes #include <err.h>
397503d74fSMark Murray #include <inttypes.h>
404b88c807SRodney W. Grimes #include <string.h>
414b88c807SRodney W. Grimes 
424b88c807SRodney W. Grimes #include "dd.h"
434b88c807SRodney W. Grimes #include "extern.h"
444b88c807SRodney W. Grimes 
454b88c807SRodney W. Grimes /*
464b88c807SRodney W. Grimes  * def --
474b88c807SRodney W. Grimes  * Copy input to output.  Input is buffered until reaches obs, and then
484b88c807SRodney W. Grimes  * output until less than obs remains.  Only a single buffer is used.
494b88c807SRodney W. Grimes  * Worst case buffer calculation is (ibs + obs - 1).
504b88c807SRodney W. Grimes  */
514b88c807SRodney W. Grimes void
def(void)52f9bcb0beSWarner Losh def(void)
534b88c807SRodney W. Grimes {
5458687472SBrian Feldman 	u_char *inp;
5558687472SBrian Feldman 	const u_char *t;
5658687472SBrian Feldman 	size_t cnt;
574b88c807SRodney W. Grimes 
58ad66f7eeSPoul-Henning Kamp 	if ((t = ctab) != NULL)
594b88c807SRodney W. Grimes 		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
604b88c807SRodney W. Grimes 			*inp = t[*inp];
614b88c807SRodney W. Grimes 
624b88c807SRodney W. Grimes 	/* Make the output buffer look right. */
634b88c807SRodney W. Grimes 	out.dbp = in.dbp;
644b88c807SRodney W. Grimes 	out.dbcnt = in.dbcnt;
654b88c807SRodney W. Grimes 
664b88c807SRodney W. Grimes 	if (in.dbcnt >= out.dbsz) {
674b88c807SRodney W. Grimes 		/* If the output buffer is full, write it. */
684b88c807SRodney W. Grimes 		dd_out(0);
694b88c807SRodney W. Grimes 
704b88c807SRodney W. Grimes 		/*
7166d5f719SThomas Quinot 		 * dd_out copies the leftover output to the beginning of
724b88c807SRodney W. Grimes 		 * the buffer and resets the output buffer.  Reset the
734b88c807SRodney W. Grimes 		 * input buffer to match it.
744b88c807SRodney W. Grimes 	 	 */
754b88c807SRodney W. Grimes 		in.dbp = out.dbp;
764b88c807SRodney W. Grimes 		in.dbcnt = out.dbcnt;
774b88c807SRodney W. Grimes 	}
784b88c807SRodney W. Grimes }
794b88c807SRodney W. Grimes 
804b88c807SRodney W. Grimes void
def_close(void)81f9bcb0beSWarner Losh def_close(void)
824b88c807SRodney W. Grimes {
834b88c807SRodney W. Grimes 	/* Just update the count, everything is already in the buffer. */
844b88c807SRodney W. Grimes 	if (in.dbcnt)
854b88c807SRodney W. Grimes 		out.dbcnt = in.dbcnt;
864b88c807SRodney W. Grimes }
874b88c807SRodney W. Grimes 
884b88c807SRodney W. Grimes /*
894b88c807SRodney W. Grimes  * Copy variable length newline terminated records with a max size cbsz
904b88c807SRodney W. Grimes  * bytes to output.  Records less than cbs are padded with spaces.
914b88c807SRodney W. Grimes  *
924b88c807SRodney W. Grimes  * max in buffer:  MAX(ibs, cbsz)
934b88c807SRodney W. Grimes  * max out buffer: obs + cbsz
944b88c807SRodney W. Grimes  */
954b88c807SRodney W. Grimes void
block(void)96f9bcb0beSWarner Losh block(void)
974b88c807SRodney W. Grimes {
9858687472SBrian Feldman 	u_char *inp, *outp;
9958687472SBrian Feldman 	const u_char *t;
10058687472SBrian Feldman 	size_t cnt, maxlen;
1014b88c807SRodney W. Grimes 	static int intrunc;
1027599187eSBrian Feldman 	int ch;
1034b88c807SRodney W. Grimes 
1044b88c807SRodney W. Grimes 	/*
1054b88c807SRodney W. Grimes 	 * Record truncation can cross block boundaries.  If currently in a
1064b88c807SRodney W. Grimes 	 * truncation state, keep tossing characters until reach a newline.
1074b88c807SRodney W. Grimes 	 * Start at the beginning of the buffer, as the input buffer is always
1084b88c807SRodney W. Grimes 	 * left empty.
1094b88c807SRodney W. Grimes 	 */
1104b88c807SRodney W. Grimes 	if (intrunc) {
111767bc8adSBrian Feldman 		for (inp = in.db, cnt = in.dbrcnt; cnt && *inp++ != '\n'; --cnt)
112767bc8adSBrian Feldman 			;
1134b88c807SRodney W. Grimes 		if (!cnt) {
1144b88c807SRodney W. Grimes 			in.dbcnt = 0;
1154b88c807SRodney W. Grimes 			in.dbp = in.db;
1164b88c807SRodney W. Grimes 			return;
1174b88c807SRodney W. Grimes 		}
1184b88c807SRodney W. Grimes 		intrunc = 0;
1194b88c807SRodney W. Grimes 		/* Adjust the input buffer numbers. */
1204b88c807SRodney W. Grimes 		in.dbcnt = cnt - 1;
1214b88c807SRodney W. Grimes 		in.dbp = inp + cnt - 1;
1224b88c807SRodney W. Grimes 	}
1234b88c807SRodney W. Grimes 
1244b88c807SRodney W. Grimes 	/*
1254b88c807SRodney W. Grimes 	 * Copy records (max cbsz size chunks) into the output buffer.  The
1264b88c807SRodney W. Grimes 	 * translation is done as we copy into the output buffer.
1274b88c807SRodney W. Grimes 	 */
12878b09ffeSSteve Price 	ch = 0;
1294b88c807SRodney W. Grimes 	for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
13077a798b3SAlan Somers 		maxlen = MIN(cbsz, (size_t)in.dbcnt);
131ad66f7eeSPoul-Henning Kamp 		if ((t = ctab) != NULL)
132767bc8adSBrian Feldman 			for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
133767bc8adSBrian Feldman 			    ++cnt)
1344b88c807SRodney W. Grimes 				*outp++ = t[ch];
1354b88c807SRodney W. Grimes 		else
136767bc8adSBrian Feldman 			for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
137767bc8adSBrian Feldman 			    ++cnt)
1384b88c807SRodney W. Grimes 				*outp++ = ch;
1394b88c807SRodney W. Grimes 		/*
1404b88c807SRodney W. Grimes 		 * Check for short record without a newline.  Reassemble the
1414b88c807SRodney W. Grimes 		 * input block.
1424b88c807SRodney W. Grimes 		 */
14377a798b3SAlan Somers 		if (ch != '\n' && (size_t)in.dbcnt < cbsz) {
14458687472SBrian Feldman 			(void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
1454b88c807SRodney W. Grimes 			break;
1464b88c807SRodney W. Grimes 		}
1474b88c807SRodney W. Grimes 
1484b88c807SRodney W. Grimes 		/* Adjust the input buffer numbers. */
1494b88c807SRodney W. Grimes 		in.dbcnt -= cnt;
1504b88c807SRodney W. Grimes 		if (ch == '\n')
1514b88c807SRodney W. Grimes 			--in.dbcnt;
1524b88c807SRodney W. Grimes 
1534b88c807SRodney W. Grimes 		/* Pad short records with spaces. */
1544b88c807SRodney W. Grimes 		if (cnt < cbsz)
1554b88c807SRodney W. Grimes 			(void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
1564b88c807SRodney W. Grimes 		else {
1574b88c807SRodney W. Grimes 			/*
1584b88c807SRodney W. Grimes 			 * If the next character wouldn't have ended the
1594b88c807SRodney W. Grimes 			 * block, it's a truncation.
1604b88c807SRodney W. Grimes 			 */
1614b88c807SRodney W. Grimes 			if (!in.dbcnt || *inp != '\n')
1624b88c807SRodney W. Grimes 				++st.trunc;
1634b88c807SRodney W. Grimes 
1644b88c807SRodney W. Grimes 			/* Toss characters to a newline. */
1657503d74fSMark Murray 			for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt)
1667503d74fSMark Murray 				;
1674b88c807SRodney W. Grimes 			if (!in.dbcnt)
1684b88c807SRodney W. Grimes 				intrunc = 1;
1694b88c807SRodney W. Grimes 			else
1704b88c807SRodney W. Grimes 				--in.dbcnt;
1714b88c807SRodney W. Grimes 		}
1724b88c807SRodney W. Grimes 
1734b88c807SRodney W. Grimes 		/* Adjust output buffer numbers. */
1744b88c807SRodney W. Grimes 		out.dbp += cbsz;
1754b88c807SRodney W. Grimes 		if ((out.dbcnt += cbsz) >= out.dbsz)
1764b88c807SRodney W. Grimes 			dd_out(0);
1774b88c807SRodney W. Grimes 		outp = out.dbp;
1784b88c807SRodney W. Grimes 	}
1794b88c807SRodney W. Grimes 	in.dbp = in.db + in.dbcnt;
1804b88c807SRodney W. Grimes }
1814b88c807SRodney W. Grimes 
1824b88c807SRodney W. Grimes void
block_close(void)183f9bcb0beSWarner Losh block_close(void)
1844b88c807SRodney W. Grimes {
1854b88c807SRodney W. Grimes 	/*
1864b88c807SRodney W. Grimes 	 * Copy any remaining data into the output buffer and pad to a record.
1874b88c807SRodney W. Grimes 	 * Don't worry about truncation or translation, the input buffer is
1884b88c807SRodney W. Grimes 	 * always empty when truncating, and no characters have been added for
1894b88c807SRodney W. Grimes 	 * translation.  The bottom line is that anything left in the input
1904b88c807SRodney W. Grimes 	 * buffer is a truncated record.  Anything left in the output buffer
1914b88c807SRodney W. Grimes 	 * just wasn't big enough.
1924b88c807SRodney W. Grimes 	 */
1934b88c807SRodney W. Grimes 	if (in.dbcnt) {
1944b88c807SRodney W. Grimes 		++st.trunc;
19558687472SBrian Feldman 		(void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
196767bc8adSBrian Feldman 		(void)memset(out.dbp + in.dbcnt, ctab ? ctab[' '] : ' ',
197767bc8adSBrian Feldman 		    cbsz - in.dbcnt);
1984b88c807SRodney W. Grimes 		out.dbcnt += cbsz;
1994b88c807SRodney W. Grimes 	}
2004b88c807SRodney W. Grimes }
2014b88c807SRodney W. Grimes 
2024b88c807SRodney W. Grimes /*
2034b88c807SRodney W. Grimes  * Convert fixed length (cbsz) records to variable length.  Deletes any
2044b88c807SRodney W. Grimes  * trailing blanks and appends a newline.
2054b88c807SRodney W. Grimes  *
2064b88c807SRodney W. Grimes  * max in buffer:  MAX(ibs, cbsz) + cbsz
2074b88c807SRodney W. Grimes  * max out buffer: obs + cbsz
2084b88c807SRodney W. Grimes  */
2094b88c807SRodney W. Grimes void
unblock(void)210f9bcb0beSWarner Losh unblock(void)
2114b88c807SRodney W. Grimes {
21258687472SBrian Feldman 	u_char *inp;
21358687472SBrian Feldman 	const u_char *t;
21458687472SBrian Feldman 	size_t cnt;
2154b88c807SRodney W. Grimes 
2164b88c807SRodney W. Grimes 	/* Translation and case conversion. */
217ad66f7eeSPoul-Henning Kamp 	if ((t = ctab) != NULL)
2187503d74fSMark Murray 		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
2197503d74fSMark Murray 			*inp = t[*inp];
2204b88c807SRodney W. Grimes 	/*
2214b88c807SRodney W. Grimes 	 * Copy records (max cbsz size chunks) into the output buffer.  The
2224b88c807SRodney W. Grimes 	 * translation has to already be done or we might not recognize the
2234b88c807SRodney W. Grimes 	 * spaces.
2244b88c807SRodney W. Grimes 	 */
22577a798b3SAlan Somers 	for (inp = in.db; (size_t)in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
226767bc8adSBrian Feldman 		for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t)
227767bc8adSBrian Feldman 			;
2284b88c807SRodney W. Grimes 		if (t >= inp) {
2294b88c807SRodney W. Grimes 			cnt = t - inp + 1;
23058687472SBrian Feldman 			(void)memmove(out.dbp, inp, cnt);
2314b88c807SRodney W. Grimes 			out.dbp += cnt;
2324b88c807SRodney W. Grimes 			out.dbcnt += cnt;
2334b88c807SRodney W. Grimes 		}
2344b88c807SRodney W. Grimes 		*out.dbp++ = '\n';
235767bc8adSBrian Feldman 		if (++out.dbcnt >= out.dbsz)
2364b88c807SRodney W. Grimes 			dd_out(0);
2374b88c807SRodney W. Grimes 	}
2384b88c807SRodney W. Grimes 	if (in.dbcnt)
23958687472SBrian Feldman 		(void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
2404b88c807SRodney W. Grimes 	in.dbp = in.db + in.dbcnt;
2414b88c807SRodney W. Grimes }
2424b88c807SRodney W. Grimes 
2434b88c807SRodney W. Grimes void
unblock_close(void)244f9bcb0beSWarner Losh unblock_close(void)
2454b88c807SRodney W. Grimes {
2464b88c807SRodney W. Grimes 	u_char *t;
24758687472SBrian Feldman 	size_t cnt;
2484b88c807SRodney W. Grimes 
2494b88c807SRodney W. Grimes 	if (in.dbcnt) {
2504b88c807SRodney W. Grimes 		warnx("%s: short input record", in.name);
251767bc8adSBrian Feldman 		for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t)
252767bc8adSBrian Feldman 			;
2534b88c807SRodney W. Grimes 		if (t >= in.db) {
2544b88c807SRodney W. Grimes 			cnt = t - in.db + 1;
25558687472SBrian Feldman 			(void)memmove(out.dbp, in.db, cnt);
2564b88c807SRodney W. Grimes 			out.dbp += cnt;
2574b88c807SRodney W. Grimes 			out.dbcnt += cnt;
2584b88c807SRodney W. Grimes 		}
2594b88c807SRodney W. Grimes 		++out.dbcnt;
2604b88c807SRodney W. Grimes 		*out.dbp++ = '\n';
2614b88c807SRodney W. Grimes 	}
2624b88c807SRodney W. Grimes }
263