xref: /netbsd/bin/dd/conv.c (revision bf9ec67e)
1 /*	$NetBSD: conv.c,v 1.14 2001/11/26 00:13:23 lukem Exp $	*/
2 
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. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)conv.c	8.3 (Berkeley) 4/2/94";
44 #else
45 __RCSID("$NetBSD: conv.c,v 1.14 2001/11/26 00:13:23 lukem Exp $");
46 #endif
47 #endif /* not lint */
48 
49 #include <sys/param.h>
50 #include <sys/time.h>
51 
52 #include <err.h>
53 #include <string.h>
54 
55 #include "dd.h"
56 #include "extern.h"
57 
58 /*
59  * def --
60  * Copy input to output.  Input is buffered until reaches obs, and then
61  * output until less than obs remains.  Only a single buffer is used.
62  * Worst case buffer calculation is (ibs + obs - 1).
63  */
64 void
65 def(void)
66 {
67 	uint64_t cnt;
68 	u_char *inp;
69 	const u_char *t;
70 
71 	if ((t = ctab) != NULL)
72 		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
73 			*inp = t[*inp];
74 
75 	/* Make the output buffer look right. */
76 	out.dbp = in.dbp;
77 	out.dbcnt = in.dbcnt;
78 
79 	if (in.dbcnt >= out.dbsz) {
80 		/* If the output buffer is full, write it. */
81 		dd_out(0);
82 
83 		/*
84 		 * Ddout copies the leftover output to the beginning of
85 		 * the buffer and resets the output buffer.  Reset the
86 		 * input buffer to match it.
87 	 	 */
88 		in.dbp = out.dbp;
89 		in.dbcnt = out.dbcnt;
90 	}
91 }
92 
93 void
94 def_close(void)
95 {
96 
97 	/* Just update the count, everything is already in the buffer. */
98 	if (in.dbcnt)
99 		out.dbcnt = in.dbcnt;
100 }
101 
102 #ifdef	NO_CONV
103 /* Build a smaller version (i.e. for a miniroot) */
104 /* These can not be called, but just in case...  */
105 static const char no_block[] = "unblock and -DNO_CONV?";
106 void block(void)		{ errx(1, "%s", no_block + 2); }
107 void block_close(void)		{ errx(1, "%s", no_block + 2); }
108 void unblock(void)		{ errx(1, "%s", no_block); }
109 void unblock_close(void)	{ errx(1, "%s", no_block); }
110 #else	/* NO_CONV */
111 
112 /*
113  * Copy variable length newline terminated records with a max size cbsz
114  * bytes to output.  Records less than cbs are padded with spaces.
115  *
116  * max in buffer:  MAX(ibs, cbsz)
117  * max out buffer: obs + cbsz
118  */
119 void
120 block(void)
121 {
122 	static int intrunc;
123 	int ch = 0;	/* pacify gcc */
124 	uint64_t cnt, maxlen;
125 	u_char *inp, *outp;
126 	const u_char *t;
127 
128 	/*
129 	 * Record truncation can cross block boundaries.  If currently in a
130 	 * truncation state, keep tossing characters until reach a newline.
131 	 * Start at the beginning of the buffer, as the input buffer is always
132 	 * left empty.
133 	 */
134 	if (intrunc) {
135 		for (inp = in.db, cnt = in.dbrcnt;
136 		    cnt && *inp++ != '\n'; --cnt);
137 		if (!cnt) {
138 			in.dbcnt = 0;
139 			in.dbp = in.db;
140 			return;
141 		}
142 		intrunc = 0;
143 		/* Adjust the input buffer numbers. */
144 		in.dbcnt = cnt - 1;
145 		in.dbp = inp + cnt - 1;
146 	}
147 
148 	/*
149 	 * Copy records (max cbsz size chunks) into the output buffer.  The
150 	 * translation is done as we copy into the output buffer.
151 	 */
152 	for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
153 		maxlen = MIN(cbsz, in.dbcnt);
154 		if ((t = ctab) != NULL)
155 			for (cnt = 0;
156 			    cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
157 				*outp++ = t[ch];
158 		else
159 			for (cnt = 0;
160 			    cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
161 				*outp++ = ch;
162 		/*
163 		 * Check for short record without a newline.  Reassemble the
164 		 * input block.
165 		 */
166 		if (ch != '\n' && in.dbcnt < cbsz) {
167 			(void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
168 			break;
169 		}
170 
171 		/* Adjust the input buffer numbers. */
172 		in.dbcnt -= cnt;
173 		if (ch == '\n')
174 			--in.dbcnt;
175 
176 		/* Pad short records with spaces. */
177 		if (cnt < cbsz)
178 			(void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
179 		else {
180 			/*
181 			 * If the next character wouldn't have ended the
182 			 * block, it's a truncation.
183 			 */
184 			if (!in.dbcnt || *inp != '\n')
185 				++st.trunc;
186 
187 			/* Toss characters to a newline. */
188 			for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
189 			if (!in.dbcnt)
190 				intrunc = 1;
191 			else
192 				--in.dbcnt;
193 		}
194 
195 		/* Adjust output buffer numbers. */
196 		out.dbp += cbsz;
197 		if ((out.dbcnt += cbsz) >= out.dbsz)
198 			dd_out(0);
199 		outp = out.dbp;
200 	}
201 	in.dbp = in.db + in.dbcnt;
202 }
203 
204 void
205 block_close(void)
206 {
207 
208 	/*
209 	 * Copy any remaining data into the output buffer and pad to a record.
210 	 * Don't worry about truncation or translation, the input buffer is
211 	 * always empty when truncating, and no characters have been added for
212 	 * translation.  The bottom line is that anything left in the input
213 	 * buffer is a truncated record.  Anything left in the output buffer
214 	 * just wasn't big enough.
215 	 */
216 	if (in.dbcnt) {
217 		++st.trunc;
218 		(void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
219 		(void)memset(out.dbp + in.dbcnt,
220 		    ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
221 		out.dbcnt += cbsz;
222 	}
223 }
224 
225 /*
226  * Convert fixed length (cbsz) records to variable length.  Deletes any
227  * trailing blanks and appends a newline.
228  *
229  * max in buffer:  MAX(ibs, cbsz) + cbsz
230  * max out buffer: obs + cbsz
231  */
232 void
233 unblock(void)
234 {
235 	uint64_t cnt;
236 	u_char *inp;
237 	const u_char *t;
238 
239 	/* Translation and case conversion. */
240 	if ((t = ctab) != NULL)
241 		for (cnt = in.dbrcnt, inp = in.dbp - 1; cnt--; inp--)
242 			*inp = t[*inp];
243 	/*
244 	 * Copy records (max cbsz size chunks) into the output buffer.  The
245 	 * translation has to already be done or we might not recognize the
246 	 * spaces.
247 	 */
248 	for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
249 		for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
250 		if (t >= inp) {
251 			cnt = t - inp + 1;
252 			(void)memmove(out.dbp, inp, cnt);
253 			out.dbp += cnt;
254 			out.dbcnt += cnt;
255 		}
256 		++out.dbcnt;
257 		*out.dbp++ = '\n';
258 		if (out.dbcnt >= out.dbsz)
259 			dd_out(0);
260 	}
261 	if (in.dbcnt)
262 		(void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
263 	in.dbp = in.db + in.dbcnt;
264 }
265 
266 void
267 unblock_close(void)
268 {
269 	uint64_t cnt;
270 	u_char *t;
271 
272 	if (in.dbcnt) {
273 		warnx("%s: short input record", in.name);
274 		for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t);
275 		if (t >= in.db) {
276 			cnt = t - in.db + 1;
277 			(void)memmove(out.dbp, in.db, cnt);
278 			out.dbp += cnt;
279 			out.dbcnt += cnt;
280 		}
281 		++out.dbcnt;
282 		*out.dbp++ = '\n';
283 	}
284 }
285 
286 #endif	/* NO_CONV */
287