xref: /dragonfly/bin/dd/conv.c (revision 5fb3968e)
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  * @(#)conv.c	8.3 (Berkeley) 4/2/94
36  * $FreeBSD: head/bin/dd/conv.c 326025 2017-11-20 19:49:47Z pfg $
37  */
38 
39 #include <sys/param.h>
40 
41 #include <err.h>
42 #include <string.h>
43 
44 #include "dd.h"
45 #include "extern.h"
46 
47 /*
48  * def --
49  * Copy input to output.  Input is buffered until reaches obs, and then
50  * output until less than obs remains.  Only a single buffer is used.
51  * Worst case buffer calculation is (ibs + obs - 1).
52  */
53 void
54 def(void)
55 {
56 	u_char *inp;
57 	const u_char *t;
58 	size_t cnt;
59 
60 	if ((t = ctab) != NULL)
61 		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
62 			*inp = t[*inp];
63 
64 	/* Make the output buffer look right. */
65 	out.dbp = in.dbp;
66 	out.dbcnt = in.dbcnt;
67 
68 	if (in.dbcnt >= out.dbsz) {
69 		/* If the output buffer is full, write it. */
70 		dd_out(0);
71 
72 		/*
73 		 * dd_out copies the leftover output to the beginning of
74 		 * the buffer and resets the output buffer.  Reset the
75 		 * input buffer to match it.
76 	 	 */
77 		in.dbp = out.dbp;
78 		in.dbcnt = out.dbcnt;
79 	}
80 }
81 
82 void
83 def_close(void)
84 {
85 	/* Just update the count, everything is already in the buffer. */
86 	if (in.dbcnt)
87 		out.dbcnt = in.dbcnt;
88 }
89 
90 /*
91  * Copy variable length newline terminated records with a max size cbsz
92  * bytes to output.  Records less than cbs are padded with spaces.
93  *
94  * max in buffer:  MAX(ibs, cbsz)
95  * max out buffer: obs + cbsz
96  */
97 void
98 block(void)
99 {
100 	u_char *inp, *outp;
101 	const u_char *t;
102 	size_t cnt, maxlen;
103 	static int intrunc;
104 	int ch;
105 
106 	/*
107 	 * Record truncation can cross block boundaries.  If currently in a
108 	 * truncation state, keep tossing characters until reach a newline.
109 	 * Start at the beginning of the buffer, as the input buffer is always
110 	 * left empty.
111 	 */
112 	if (intrunc) {
113 		for (inp = in.db, cnt = in.dbrcnt; cnt && *inp++ != '\n'; --cnt)
114 			;
115 		if (!cnt) {
116 			in.dbcnt = 0;
117 			in.dbp = in.db;
118 			return;
119 		}
120 		intrunc = 0;
121 		/* Adjust the input buffer numbers. */
122 		in.dbcnt = cnt - 1;
123 		in.dbp = inp + cnt - 1;
124 	}
125 
126 	/*
127 	 * Copy records (max cbsz size chunks) into the output buffer.  The
128 	 * translation is done as we copy into the output buffer.
129 	 */
130 	ch = 0;
131 	for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
132 		maxlen = MIN(cbsz, (size_t)in.dbcnt);
133 		if ((t = ctab) != NULL) {
134 			for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
135 			    ++cnt)
136 				*outp++ = t[ch];
137 		} else {
138 			for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
139 			    ++cnt)
140 				*outp++ = ch;
141 		}
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 			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 			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 		memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
199 		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 			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 		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 			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