xref: /dragonfly/bin/dd/conv.c (revision aa8d5dcb)
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  * @(#)conv.c	8.3 (Berkeley) 4/2/94
38  * $FreeBSD: src/bin/dd/conv.c,v 1.15 1999/09/13 21:47:10 green Exp $
39  * $DragonFly: src/bin/dd/conv.c,v 1.4 2004/01/28 16:25:29 joerg Exp $
40  */
41 
42 #include <sys/param.h>
43 
44 #include <err.h>
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(void)
58 {
59 	u_char *inp;
60 	const u_char *t;
61 	size_t cnt;
62 
63 	if ((t = ctab) != NULL)
64 		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
65 			*inp = t[*inp];
66 
67 	/* Make the output buffer look right. */
68 	out.dbp = in.dbp;
69 	out.dbcnt = in.dbcnt;
70 
71 	if (in.dbcnt >= out.dbsz) {
72 		/* If the output buffer is full, write it. */
73 		dd_out(0);
74 
75 		/*
76 		 * Ddout copies the leftover output to the beginning of
77 		 * the buffer and resets the output buffer.  Reset the
78 		 * input buffer to match it.
79 	 	 */
80 		in.dbp = out.dbp;
81 		in.dbcnt = out.dbcnt;
82 	}
83 }
84 
85 void
86 def_close(void)
87 {
88 	/* Just update the count, everything is already in the buffer. */
89 	if (in.dbcnt)
90 		out.dbcnt = in.dbcnt;
91 }
92 
93 /*
94  * Copy variable length newline terminated records with a max size cbsz
95  * bytes to output.  Records less than cbs are padded with spaces.
96  *
97  * max in buffer:  MAX(ibs, cbsz)
98  * max out buffer: obs + cbsz
99  */
100 void
101 block(void)
102 {
103 	u_char *inp, *outp;
104 	const u_char *t;
105 	size_t cnt, maxlen;
106 	static int intrunc;
107 	int ch;
108 
109 	/*
110 	 * Record truncation can cross block boundaries.  If currently in a
111 	 * truncation state, keep tossing characters until reach a newline.
112 	 * Start at the beginning of the buffer, as the input buffer is always
113 	 * left empty.
114 	 */
115 	if (intrunc) {
116 		for (inp = in.db, cnt = in.dbrcnt; cnt && *inp++ != '\n'; --cnt)
117 			;
118 		if (!cnt) {
119 			in.dbcnt = 0;
120 			in.dbp = in.db;
121 			return;
122 		}
123 		intrunc = 0;
124 		/* Adjust the input buffer numbers. */
125 		in.dbcnt = cnt - 1;
126 		in.dbp = inp + cnt - 1;
127 	}
128 
129 	/*
130 	 * Copy records (max cbsz size chunks) into the output buffer.  The
131 	 * translation is done as we copy into the output buffer.
132 	 */
133 	ch = 0;
134 	for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
135 		maxlen = MIN(cbsz, in.dbcnt);
136 		if ((t = ctab) != NULL)
137 			for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
138 			    ++cnt)
139 				*outp++ = t[ch];
140 		else
141 			for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
142 			    ++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 			(void)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(void)
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 		(void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
200 		(void)memset(out.dbp + in.dbcnt, ctab ? ctab[' '] : ' ',
201 		    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(void)
215 {
216 	u_char *inp;
217 	const u_char *t;
218 	size_t cnt;
219 
220 	/* Translation and case conversion. */
221 	if ((t = ctab) != NULL)
222 		for (cnt = in.dbrcnt, inp = in.dbp; cnt--;) {
223 			inp--;
224 			*inp = t[*inp];
225 		}
226 	/*
227 	 * Copy records (max cbsz size chunks) into the output buffer.  The
228 	 * translation has to already be done or we might not recognize the
229 	 * spaces.
230 	 */
231 	for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
232 		for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t)
233 			;
234 		if (t >= inp) {
235 			cnt = t - inp + 1;
236 			(void)memmove(out.dbp, inp, cnt);
237 			out.dbp += cnt;
238 			out.dbcnt += cnt;
239 		}
240 		*out.dbp++ = '\n';
241 		if (++out.dbcnt >= out.dbsz)
242 			dd_out(0);
243 	}
244 	if (in.dbcnt)
245 		(void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
246 	in.dbp = in.db + in.dbcnt;
247 }
248 
249 void
250 unblock_close(void)
251 {
252 	u_char *t;
253 	size_t cnt;
254 
255 	if (in.dbcnt) {
256 		warnx("%s: short input record", in.name);
257 		for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t)
258 			;
259 		if (t >= in.db) {
260 			cnt = t - in.db + 1;
261 			(void)memmove(out.dbp, in.db, cnt);
262 			out.dbp += cnt;
263 			out.dbcnt += cnt;
264 		}
265 		++out.dbcnt;
266 		*out.dbp++ = '\n';
267 	}
268 }
269