xref: /freebsd/bin/dd/conv.c (revision 77dfecce)
1 /*-
2  * Copyright (c) 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Keith Muller of the University of California, San Diego and Lance
7  * Visser of Convex Computer Corporation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	$Id: conv.c,v 1.3 1994/09/24 02:54:44 davidg Exp $
38  */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)conv.c	8.3 (Berkeley) 4/2/94";
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 
46 #include <err.h>
47 #include <string.h>
48 #include <sys/time.h>
49 
50 #include "dd.h"
51 #include "extern.h"
52 
53 /*
54  * def --
55  * Copy input to output.  Input is buffered until reaches obs, and then
56  * output until less than obs remains.  Only a single buffer is used.
57  * Worst case buffer calculation is (ibs + obs - 1).
58  */
59 void
60 def()
61 {
62 	int cnt;
63 	u_char *inp, *t;
64 
65 	if ((t = ctab))
66 		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
67 			*inp = t[*inp];
68 
69 	/* Make the output buffer look right. */
70 	out.dbp = in.dbp;
71 	out.dbcnt = in.dbcnt;
72 
73 	if (in.dbcnt >= out.dbsz) {
74 		/* If the output buffer is full, write it. */
75 		dd_out(0);
76 
77 		/*
78 		 * Ddout copies the leftover output to the beginning of
79 		 * the buffer and resets the output buffer.  Reset the
80 		 * input buffer to match it.
81 	 	 */
82 		in.dbp = out.dbp;
83 		in.dbcnt = out.dbcnt;
84 	}
85 }
86 
87 void
88 def_close()
89 {
90 	/* Just update the count, everything is already in the buffer. */
91 	if (in.dbcnt)
92 		out.dbcnt = in.dbcnt;
93 }
94 
95 /*
96  * Copy variable length newline terminated records with a max size cbsz
97  * bytes to output.  Records less than cbs are padded with spaces.
98  *
99  * max in buffer:  MAX(ibs, cbsz)
100  * max out buffer: obs + cbsz
101  */
102 void
103 block()
104 {
105 	static int intrunc;
106 	int ch, cnt, maxlen;
107 	u_char *inp, *outp, *t;
108 
109         ch = 0;
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;
118 		    cnt && *inp++ != '\n'; --cnt);
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 	for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
135 		maxlen = MIN(cbsz, in.dbcnt);
136 		if ((t = ctab))
137 			for (cnt = 0;
138 			    cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
139 				*outp++ = t[ch];
140 		else
141 			for (cnt = 0;
142 			    cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
143 				*outp++ = ch;
144 		/*
145 		 * Check for short record without a newline.  Reassemble the
146 		 * input block.
147 		 */
148 		if (ch != '\n' && in.dbcnt < cbsz) {
149 			memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
150 			break;
151 		}
152 
153 		/* Adjust the input buffer numbers. */
154 		in.dbcnt -= cnt;
155 		if (ch == '\n')
156 			--in.dbcnt;
157 
158 		/* Pad short records with spaces. */
159 		if (cnt < cbsz)
160 			(void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
161 		else {
162 			/*
163 			 * If the next character wouldn't have ended the
164 			 * block, it's a truncation.
165 			 */
166 			if (!in.dbcnt || *inp != '\n')
167 				++st.trunc;
168 
169 			/* Toss characters to a newline. */
170 			for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
171 			if (!in.dbcnt)
172 				intrunc = 1;
173 			else
174 				--in.dbcnt;
175 		}
176 
177 		/* Adjust output buffer numbers. */
178 		out.dbp += cbsz;
179 		if ((out.dbcnt += cbsz) >= out.dbsz)
180 			dd_out(0);
181 		outp = out.dbp;
182 	}
183 	in.dbp = in.db + in.dbcnt;
184 }
185 
186 void
187 block_close()
188 {
189 	/*
190 	 * Copy any remaining data into the output buffer and pad to a record.
191 	 * Don't worry about truncation or translation, the input buffer is
192 	 * always empty when truncating, and no characters have been added for
193 	 * translation.  The bottom line is that anything left in the input
194 	 * buffer is a truncated record.  Anything left in the output buffer
195 	 * just wasn't big enough.
196 	 */
197 	if (in.dbcnt) {
198 		++st.trunc;
199 		memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
200 		(void)memset(out.dbp + in.dbcnt,
201 		    ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
202 		out.dbcnt += cbsz;
203 	}
204 }
205 
206 /*
207  * Convert fixed length (cbsz) records to variable length.  Deletes any
208  * trailing blanks and appends a newline.
209  *
210  * max in buffer:  MAX(ibs, cbsz) + cbsz
211  * max out buffer: obs + cbsz
212  */
213 void
214 unblock()
215 {
216 	int cnt;
217 	u_char *inp, *t;
218 
219 	/* Translation and case conversion. */
220 	if ((t = ctab))
221 		for (cnt = in.dbrcnt, inp = in.dbp; cnt--;)
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; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
229 		for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
230 		if (t >= inp) {
231 			cnt = t - inp + 1;
232 			memmove(out.dbp, inp, cnt);
233 			out.dbp += cnt;
234 			out.dbcnt += cnt;
235 		}
236 		++out.dbcnt;
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()
248 {
249 	int cnt;
250 	u_char *t;
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 		if (t >= in.db) {
256 			cnt = t - in.db + 1;
257 			memmove(out.dbp, in.db, cnt);
258 			out.dbp += cnt;
259 			out.dbcnt += cnt;
260 		}
261 		++out.dbcnt;
262 		*out.dbp++ = '\n';
263 	}
264 }
265