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