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