xref: /freebsd/bin/dd/conv.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
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 #endif /* not lint */
38 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 
41 #include <err.h>
42 #include <inttypes.h>
43 #include <string.h>
44 
45 #include "dd.h"
46 #include "extern.h"
47 
48 /*
49  * def --
50  * Copy input to output.  Input is buffered until reaches obs, and then
51  * output until less than obs remains.  Only a single buffer is used.
52  * Worst case buffer calculation is (ibs + obs - 1).
53  */
54 void
55 def(void)
56 {
57 	u_char *inp;
58 	const u_char *t;
59 	size_t cnt;
60 
61 	if ((t = ctab) != NULL)
62 		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
63 			*inp = t[*inp];
64 
65 	/* Make the output buffer look right. */
66 	out.dbp = in.dbp;
67 	out.dbcnt = in.dbcnt;
68 
69 	if (in.dbcnt >= out.dbsz) {
70 		/* If the output buffer is full, write it. */
71 		dd_out(0);
72 
73 		/*
74 		 * dd_out copies the leftover output to the beginning of
75 		 * the buffer and resets the output buffer.  Reset the
76 		 * input buffer to match it.
77 	 	 */
78 		in.dbp = out.dbp;
79 		in.dbcnt = out.dbcnt;
80 	}
81 }
82 
83 void
84 def_close(void)
85 {
86 	/* Just update the count, everything is already in the buffer. */
87 	if (in.dbcnt)
88 		out.dbcnt = in.dbcnt;
89 }
90 
91 /*
92  * Copy variable length newline terminated records with a max size cbsz
93  * bytes to output.  Records less than cbs are padded with spaces.
94  *
95  * max in buffer:  MAX(ibs, cbsz)
96  * max out buffer: obs + cbsz
97  */
98 void
99 block(void)
100 {
101 	u_char *inp, *outp;
102 	const u_char *t;
103 	size_t cnt, maxlen;
104 	static int intrunc;
105 	int ch;
106 
107 	/*
108 	 * Record truncation can cross block boundaries.  If currently in a
109 	 * truncation state, keep tossing characters until reach a newline.
110 	 * Start at the beginning of the buffer, as the input buffer is always
111 	 * left empty.
112 	 */
113 	if (intrunc) {
114 		for (inp = in.db, cnt = in.dbrcnt; cnt && *inp++ != '\n'; --cnt)
115 			;
116 		if (!cnt) {
117 			in.dbcnt = 0;
118 			in.dbp = in.db;
119 			return;
120 		}
121 		intrunc = 0;
122 		/* Adjust the input buffer numbers. */
123 		in.dbcnt = cnt - 1;
124 		in.dbp = inp + cnt - 1;
125 	}
126 
127 	/*
128 	 * Copy records (max cbsz size chunks) into the output buffer.  The
129 	 * translation is done as we copy into the output buffer.
130 	 */
131 	ch = 0;
132 	for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
133 		maxlen = MIN(cbsz, (size_t)in.dbcnt);
134 		if ((t = ctab) != NULL)
135 			for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
136 			    ++cnt)
137 				*outp++ = t[ch];
138 		else
139 			for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
140 			    ++cnt)
141 				*outp++ = ch;
142 		/*
143 		 * Check for short record without a newline.  Reassemble the
144 		 * input block.
145 		 */
146 		if (ch != '\n' && (size_t)in.dbcnt < cbsz) {
147 			(void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
148 			break;
149 		}
150 
151 		/* Adjust the input buffer numbers. */
152 		in.dbcnt -= cnt;
153 		if (ch == '\n')
154 			--in.dbcnt;
155 
156 		/* Pad short records with spaces. */
157 		if (cnt < cbsz)
158 			(void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
159 		else {
160 			/*
161 			 * If the next character wouldn't have ended the
162 			 * block, it's a truncation.
163 			 */
164 			if (!in.dbcnt || *inp != '\n')
165 				++st.trunc;
166 
167 			/* Toss characters to a newline. */
168 			for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt)
169 				;
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 
185 void
186 block_close(void)
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 		(void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
199 		(void)memset(out.dbp + in.dbcnt, ctab ? ctab[' '] : ' ',
200 		    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  */
212 void
213 unblock(void)
214 {
215 	u_char *inp;
216 	const u_char *t;
217 	size_t cnt;
218 
219 	/* Translation and case conversion. */
220 	if ((t = ctab) != NULL)
221 		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
222 			*inp = t[*inp];
223 	/*
224 	 * Copy records (max cbsz size chunks) into the output buffer.  The
225 	 * translation has to already be done or we might not recognize the
226 	 * spaces.
227 	 */
228 	for (inp = in.db; (size_t)in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
229 		for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t)
230 			;
231 		if (t >= inp) {
232 			cnt = t - inp + 1;
233 			(void)memmove(out.dbp, inp, cnt);
234 			out.dbp += cnt;
235 			out.dbcnt += cnt;
236 		}
237 		*out.dbp++ = '\n';
238 		if (++out.dbcnt >= out.dbsz)
239 			dd_out(0);
240 	}
241 	if (in.dbcnt)
242 		(void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
243 	in.dbp = in.db + in.dbcnt;
244 }
245 
246 void
247 unblock_close(void)
248 {
249 	u_char *t;
250 	size_t cnt;
251 
252 	if (in.dbcnt) {
253 		warnx("%s: short input record", in.name);
254 		for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t)
255 			;
256 		if (t >= in.db) {
257 			cnt = t - in.db + 1;
258 			(void)memmove(out.dbp, in.db, cnt);
259 			out.dbp += cnt;
260 			out.dbcnt += cnt;
261 		}
262 		++out.dbcnt;
263 		*out.dbp++ = '\n';
264 	}
265 }
266