xref: /netbsd-src/bin/dd/conv.c (revision 4b30c543a0b21e3ba94f2c569e9a82b4fdb2075f)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * 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 
38 #ifndef lint
39 /*static char sccsid[] = "from: @(#)conv.c	5.6 (Berkeley) 4/28/93";*/
40 static char rcsid[] = "$Id: conv.c,v 1.2 1993/08/01 19:00:11 mycroft Exp $";
41 #endif /* not lint */
42 
43 #include <sys/param.h>
44 
45 #include <string.h>
46 
47 #include "dd.h"
48 #include "extern.h"
49 
50 /*
51  * def --
52  * Copy input to output.  Input is buffered until reaches obs, and then
53  * output until less than obs remains.  Only a single buffer is used.
54  * Worst case buffer calculation is (ibs + obs - 1).
55  */
56 void
57 def()
58 {
59 	register int cnt;
60 	register u_char *inp, *t;
61 
62 	if (t = ctab)
63 		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
64 			*inp = t[*inp];
65 
66 	/* Make the output buffer look right. */
67 	out.dbp = in.dbp;
68 	out.dbcnt = in.dbcnt;
69 
70 	if (in.dbcnt >= out.dbsz) {
71 		/* If the output buffer is full, write it. */
72 		dd_out(0);
73 
74 		/*
75 		 * Ddout copies the leftover output to the beginning of
76 		 * the buffer and resets the output buffer.  Reset the
77 		 * input buffer to match it.
78 	 	 */
79 		in.dbp = out.dbp;
80 		in.dbcnt = out.dbcnt;
81 	}
82 }
83 
84 void
85 def_close()
86 {
87 	/* Just update the count, everything is already in the buffer. */
88 	if (in.dbcnt)
89 		out.dbcnt = in.dbcnt;
90 }
91 
92 /*
93  * Copy variable length newline terminated records with a max size cbsz
94  * bytes to output.  Records less than cbs are padded with spaces.
95  *
96  * max in buffer:  MAX(ibs, cbsz)
97  * max out buffer: obs + cbsz
98  */
99 void
100 block()
101 {
102 	static int intrunc;
103 	register int ch, cnt;
104 	register u_char *inp, *outp, *t;
105 	int maxlen;
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;
115 		    cnt && *inp++ != '\n'; --cnt);
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 	for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
132 		maxlen = MIN(cbsz, in.dbcnt);
133 		if (t = ctab)
134 			for (cnt = 0;
135 			    cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
136 				*outp++ = t[ch];
137 		else
138 			for (cnt = 0;
139 			    cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
140 				*outp++ = ch;
141 		/*
142 		 * Check for short record without a newline.  Reassemble the
143 		 * input block.
144 		 */
145 		if (ch != '\n' && in.dbcnt < cbsz) {
146 			memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
147 			break;
148 		}
149 
150 		/* Adjust the input buffer numbers. */
151 		in.dbcnt -= cnt;
152 		if (ch == '\n')
153 			--in.dbcnt;
154 
155 		/* Pad short records with spaces. */
156 		if (cnt < cbsz)
157 			(void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
158 		else {
159 			/*
160 			 * If the next character wouldn't have ended the
161 			 * block, it's a truncation.
162 			 */
163 			if (!in.dbcnt || *inp != '\n')
164 				++st.trunc;
165 
166 			/* Toss characters to a newline. */
167 			for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
168 			if (!in.dbcnt)
169 				intrunc = 1;
170 			else
171 				--in.dbcnt;
172 		}
173 
174 		/* Adjust output buffer numbers. */
175 		out.dbp += cbsz;
176 		if ((out.dbcnt += cbsz) >= out.dbsz)
177 			dd_out(0);
178 		outp = out.dbp;
179 	}
180 	in.dbp = in.db + in.dbcnt;
181 }
182 
183 void
184 block_close()
185 {
186 	/*
187 	 * Copy any remaining data into the output buffer and pad to a record.
188 	 * Don't worry about truncation or translation, the input buffer is
189 	 * always empty when truncating, and no characters have been added for
190 	 * translation.  The bottom line is that anything left in the input
191 	 * buffer is a truncated record.  Anything left in the output buffer
192 	 * just wasn't big enough.
193 	 */
194 	if (in.dbcnt) {
195 		++st.trunc;
196 		memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
197 		(void)memset(out.dbp + in.dbcnt,
198 		    ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
199 		out.dbcnt += cbsz;
200 	}
201 }
202 
203 /*
204  * Convert fixed length (cbsz) records to variable length.  Deletes any
205  * trailing blanks and appends a newline.
206  *
207  * max in buffer:  MAX(ibs, cbsz) + cbsz
208  * max out buffer: obs + cbsz
209  */
210 void
211 unblock()
212 {
213 	register int cnt;
214 	register u_char *inp, *t;
215 
216 	/* Translation and case conversion. */
217 	if (t = ctab)
218 		for (cnt = in.dbrcnt, inp = in.dbp; cnt--;)
219 			*--inp = t[*inp];
220 	/*
221 	 * Copy records (max cbsz size chunks) into the output buffer.  The
222 	 * translation has to already be done or we might not recognize the
223 	 * spaces.
224 	 */
225 	for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
226 		for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
227 		if (t >= inp) {
228 			cnt = t - inp + 1;
229 			memmove(out.dbp, inp, cnt);
230 			out.dbp += cnt;
231 			out.dbcnt += cnt;
232 		}
233 		++out.dbcnt;
234 		*out.dbp++ = '\n';
235 		if (out.dbcnt >= out.dbsz)
236 			dd_out(0);
237 	}
238 	if (in.dbcnt)
239 		memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
240 	in.dbp = in.db + in.dbcnt;
241 }
242 
243 void
244 unblock_close()
245 {
246 	register int cnt;
247 	register u_char *t;
248 
249 	if (in.dbcnt) {
250 		warn("%s: short input record", in.name);
251 		for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t);
252 		if (t >= in.db) {
253 			cnt = t - in.db + 1;
254 			memmove(out.dbp, in.db, cnt);
255 			out.dbp += cnt;
256 			out.dbcnt += cnt;
257 		}
258 		++out.dbcnt;
259 		*out.dbp++ = '\n';
260 	}
261 }
262