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