xref: /original-bsd/bin/dd/conv.c (revision 97bd5884)
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  * %sccs.include.redist.c%
10  */
11 
12 #ifndef lint
13 static char sccsid[] = "@(#)conv.c	5.4 (Berkeley) 08/14/91";
14 #endif /* not lint */
15 
16 #include <sys/param.h>
17 #include <string.h>
18 #include "dd.h"
19 #include "extern.h"
20 
21 /*
22  * def --
23  * Copy input to output.  Input is buffered until reaches obs, and then
24  * output until less than obs remains.  Only a single buffer is used.
25  * Worst case buffer calculation is (ibs + obs - 1).
26  */
27 void
28 def()
29 {
30 	register int cnt;
31 	register u_char *inp, *t;
32 
33 	if (t = ctab)
34 		for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
35 			*inp = t[*inp];
36 
37 	/* Make the output buffer look right. */
38 	out.dbp = in.dbp;
39 	out.dbcnt = in.dbcnt;
40 
41 	if (in.dbcnt >= out.dbsz) {
42 		/* If the output buffer is full, write it. */
43 		dd_out(0);
44 
45 		/*
46 		 * Ddout copies the leftover output to the beginning of
47 		 * the buffer and resets the output buffer.  Reset the
48 		 * input buffer to match it.
49 	 	 */
50 		in.dbp = out.dbp;
51 		in.dbcnt = out.dbcnt;
52 	}
53 }
54 
55 void
56 def_close()
57 {
58 	/* Just update the count, everything is already in the buffer. */
59 	if (in.dbcnt)
60 		out.dbcnt = in.dbcnt;
61 }
62 
63 /*
64  * Copy variable length newline terminated records with a max size cbsz
65  * bytes to output.  Records less than cbs are padded with spaces.
66  *
67  * max in buffer:  MAX(ibs, cbsz)
68  * max out buffer: obs + cbsz
69  */
70 void
71 block()
72 {
73 	static int intrunc;
74 	register int ch, cnt;
75 	register u_char *inp, *outp, *t;
76 	int maxlen;
77 
78 	/*
79 	 * Record truncation can cross block boundaries.  If currently in a
80 	 * truncation state, keep tossing characters until reach a newline.
81 	 * Start at the beginning of the buffer, as the input buffer is always
82 	 * left empty.
83 	 */
84 	if (intrunc) {
85 		for (inp = in.db, cnt = in.dbrcnt;
86 		    cnt && *inp++ != '\n'; --cnt);
87 		if (!cnt) {
88 			in.dbcnt = 0;
89 			in.dbp = in.db;
90 			return;
91 		}
92 		intrunc = 0;
93 		/* Adjust the input buffer numbers. */
94 		in.dbcnt = cnt - 1;
95 		in.dbp = inp + cnt - 1;
96 	}
97 
98 	/*
99 	 * Copy records (max cbsz size chunks) into the output buffer.  The
100 	 * translation is done as we copy into the output buffer.
101 	 */
102 	for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
103 		maxlen = MIN(cbsz, in.dbcnt);
104 		if (t = ctab)
105 			for (cnt = 0;
106 			    cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
107 				*outp++ = t[ch];
108 		else
109 			for (cnt = 0;
110 			    cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
111 				*outp++ = ch;
112 		/*
113 		 * Check for short record without a newline.  Reassemble the
114 		 * input block.
115 		 */
116 		if (ch != '\n' && in.dbcnt < cbsz) {
117 			bcopy(in.dbp - in.dbcnt, in.db, in.dbcnt);
118 			break;
119 		}
120 
121 		/* Adjust the input buffer numbers. */
122 		in.dbcnt -= cnt;
123 		if (ch == '\n')
124 			--in.dbcnt;
125 
126 		/* Pad short records with "spaces". */
127 		if (cnt < cbsz)
128 			(void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
129 		else {
130 			++st.trunc;
131 			/* Toss characters to a newline. */
132 			for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
133 			if (!in.dbcnt)
134 				intrunc = 1;
135 			else
136 				--in.dbcnt;
137 		}
138 
139 		/* Adjust output buffer numbers. */
140 		out.dbp += cbsz;
141 		if ((out.dbcnt += cbsz) >= out.dbsz)
142 			dd_out(0);
143 		outp = out.dbp;
144 	}
145 	in.dbp = in.db + in.dbcnt;
146 }
147 
148 void
149 block_close()
150 {
151 	/*
152 	 * Copy any remaining data into the output buffer and pad to a record.
153 	 * Don't worry about truncation or translation, the input buffer is
154 	 * always empty when truncating, and no characters have been added for
155 	 * translation.  The bottom line is that anything left in the input
156 	 * buffer is a truncated record.  Anything left in the output buffer
157 	 * just wasn't big enough.
158 	 */
159 	if (in.dbcnt) {
160 		++st.trunc;
161 		bcopy(in.dbp - in.dbcnt, out.dbp, in.dbcnt);
162 		(void)memset(out.dbp + in.dbcnt,
163 		    ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
164 		out.dbcnt += cbsz;
165 	}
166 }
167 
168 /*
169  * Convert fixed length (cbsz) records to variable length.  Deletes any
170  * trailing blanks and appends a newline.
171  *
172  * max in buffer:  MAX(ibs, cbsz) + cbsz
173  * max out buffer: obs + cbsz
174  */
175 void
176 unblock()
177 {
178 	register int cnt;
179 	register u_char *inp, *t;
180 
181 	/* Translation and case conversion. */
182 	if (t = ctab)
183 		for (cnt = in.dbrcnt, inp = in.dbp; cnt--;)
184 			*--inp = t[*inp];
185 	/*
186 	 * Copy records (max cbsz size chunks) into the output buffer.  The
187 	 * translation has to already be done or we might not recognize the
188 	 * spaces.
189 	 */
190 	for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
191 		for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
192 		if (t >= inp) {
193 			cnt = t - inp + 1;
194 			bcopy(inp, out.dbp, cnt);
195 			out.dbp += cnt;
196 			out.dbcnt += cnt;
197 		}
198 		++out.dbcnt;
199 		*out.dbp++ = '\n';
200 		if (out.dbcnt >= out.dbsz)
201 			dd_out(0);
202 	}
203 	if (in.dbcnt)
204 		bcopy(in.dbp - in.dbcnt, in.db, in.dbcnt);
205 	in.dbp = in.db + in.dbcnt;
206 }
207 
208 void
209 unblock_close()
210 {
211 	register int cnt;
212 	register u_char *t;
213 
214 	if (in.dbcnt) {
215 		warn("%s: short input record", in.name);
216 		for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t);
217 		if (t >= in.db) {
218 			cnt = t - in.db + 1;
219 			bcopy(in.db, out.dbp, cnt);
220 			out.dbp += cnt;
221 			out.dbcnt += cnt;
222 		}
223 		++out.dbcnt;
224 		*out.dbp++ = '\n';
225 	}
226 }
227