xref: /freebsd/bin/dd/conv.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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
38 static char sccsid[] = "@(#)conv.c	8.3 (Berkeley) 4/2/94";
39 #endif
40 #endif /* not lint */
41 #include <sys/cdefs.h>
42 #include <sys/param.h>
43 
44 #include <err.h>
45 #include <inttypes.h>
46 #include <string.h>
47 
48 #include "dd.h"
49 #include "extern.h"
50 
51 /*
52  * def --
53  * Copy input to output.  Input is buffered until reaches obs, and then
54  * output until less than obs remains.  Only a single buffer is used.
55  * Worst case buffer calculation is (ibs + obs - 1).
56  */
57 void
58 def(void)
59 {
60 	u_char *inp;
61 	const u_char *t;
62 	size_t cnt;
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 		 * dd_out 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 
86 void
87 def_close(void)
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  */
101 void
102 block(void)
103 {
104 	u_char *inp, *outp;
105 	const u_char *t;
106 	size_t cnt, maxlen;
107 	static int intrunc;
108 	int ch;
109 
110 	/*
111 	 * Record truncation can cross block boundaries.  If currently in a
112 	 * truncation state, keep tossing characters until reach a newline.
113 	 * Start at the beginning of the buffer, as the input buffer is always
114 	 * left empty.
115 	 */
116 	if (intrunc) {
117 		for (inp = in.db, cnt = in.dbrcnt; cnt && *inp++ != '\n'; --cnt)
118 			;
119 		if (!cnt) {
120 			in.dbcnt = 0;
121 			in.dbp = in.db;
122 			return;
123 		}
124 		intrunc = 0;
125 		/* Adjust the input buffer numbers. */
126 		in.dbcnt = cnt - 1;
127 		in.dbp = inp + cnt - 1;
128 	}
129 
130 	/*
131 	 * Copy records (max cbsz size chunks) into the output buffer.  The
132 	 * translation is done as we copy into the output buffer.
133 	 */
134 	ch = 0;
135 	for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
136 		maxlen = MIN(cbsz, (size_t)in.dbcnt);
137 		if ((t = ctab) != NULL)
138 			for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
139 			    ++cnt)
140 				*outp++ = t[ch];
141 		else
142 			for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
143 			    ++cnt)
144 				*outp++ = ch;
145 		/*
146 		 * Check for short record without a newline.  Reassemble the
147 		 * input block.
148 		 */
149 		if (ch != '\n' && (size_t)in.dbcnt < cbsz) {
150 			(void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
151 			break;
152 		}
153 
154 		/* Adjust the input buffer numbers. */
155 		in.dbcnt -= cnt;
156 		if (ch == '\n')
157 			--in.dbcnt;
158 
159 		/* Pad short records with spaces. */
160 		if (cnt < cbsz)
161 			(void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
162 		else {
163 			/*
164 			 * If the next character wouldn't have ended the
165 			 * block, it's a truncation.
166 			 */
167 			if (!in.dbcnt || *inp != '\n')
168 				++st.trunc;
169 
170 			/* Toss characters to a newline. */
171 			for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt)
172 				;
173 			if (!in.dbcnt)
174 				intrunc = 1;
175 			else
176 				--in.dbcnt;
177 		}
178 
179 		/* Adjust output buffer numbers. */
180 		out.dbp += cbsz;
181 		if ((out.dbcnt += cbsz) >= out.dbsz)
182 			dd_out(0);
183 		outp = out.dbp;
184 	}
185 	in.dbp = in.db + in.dbcnt;
186 }
187 
188 void
189 block_close(void)
190 {
191 	/*
192 	 * Copy any remaining data into the output buffer and pad to a record.
193 	 * Don't worry about truncation or translation, the input buffer is
194 	 * always empty when truncating, and no characters have been added for
195 	 * translation.  The bottom line is that anything left in the input
196 	 * buffer is a truncated record.  Anything left in the output buffer
197 	 * just wasn't big enough.
198 	 */
199 	if (in.dbcnt) {
200 		++st.trunc;
201 		(void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
202 		(void)memset(out.dbp + in.dbcnt, ctab ? ctab[' '] : ' ',
203 		    cbsz - in.dbcnt);
204 		out.dbcnt += cbsz;
205 	}
206 }
207 
208 /*
209  * Convert fixed length (cbsz) records to variable length.  Deletes any
210  * trailing blanks and appends a newline.
211  *
212  * max in buffer:  MAX(ibs, cbsz) + cbsz
213  * max out buffer: obs + cbsz
214  */
215 void
216 unblock(void)
217 {
218 	u_char *inp;
219 	const u_char *t;
220 	size_t cnt;
221 
222 	/* Translation and case conversion. */
223 	if ((t = ctab) != NULL)
224 		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
225 			*inp = t[*inp];
226 	/*
227 	 * Copy records (max cbsz size chunks) into the output buffer.  The
228 	 * translation has to already be done or we might not recognize the
229 	 * spaces.
230 	 */
231 	for (inp = in.db; (size_t)in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
232 		for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t)
233 			;
234 		if (t >= inp) {
235 			cnt = t - inp + 1;
236 			(void)memmove(out.dbp, inp, cnt);
237 			out.dbp += cnt;
238 			out.dbcnt += cnt;
239 		}
240 		*out.dbp++ = '\n';
241 		if (++out.dbcnt >= out.dbsz)
242 			dd_out(0);
243 	}
244 	if (in.dbcnt)
245 		(void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
246 	in.dbp = in.db + in.dbcnt;
247 }
248 
249 void
250 unblock_close(void)
251 {
252 	u_char *t;
253 	size_t cnt;
254 
255 	if (in.dbcnt) {
256 		warnx("%s: short input record", in.name);
257 		for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t)
258 			;
259 		if (t >= in.db) {
260 			cnt = t - in.db + 1;
261 			(void)memmove(out.dbp, in.db, cnt);
262 			out.dbp += cnt;
263 			out.dbcnt += cnt;
264 		}
265 		++out.dbcnt;
266 		*out.dbp++ = '\n';
267 	}
268 }
269