]> git.saurik.com Git - apple/file_cmds.git/blame - dd/conv.c
file_cmds-46.tar.gz
[apple/file_cmds.git] / dd / conv.c
CommitLineData
44a7a5ab
A
1/* $NetBSD: conv.c,v 1.8 1998/07/28 05:15:46 mycroft Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Keith Muller of the University of California, San Diego and Lance
9 * Visser of Convex Computer Corporation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40#include <sys/cdefs.h>
41#ifndef lint
42#if 0
43static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
44#else
45__RCSID("$NetBSD: conv.c,v 1.8 1998/07/28 05:15:46 mycroft Exp $");
46#endif
47#endif /* not lint */
48
49#include <sys/param.h>
50
51#include <err.h>
52#include <string.h>
53
54#include "dd.h"
55#include "extern.h"
56
57/*
58 * def --
59 * Copy input to output. Input is buffered until reaches obs, and then
60 * output until less than obs remains. Only a single buffer is used.
61 * Worst case buffer calculation is (ibs + obs - 1).
62 */
63void
64def()
65{
66 int cnt;
67 u_char *inp;
68 const u_char *t;
69
70 if ((t = ctab) != NULL)
71 for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
72 *inp = t[*inp];
73
74 /* Make the output buffer look right. */
75 out.dbp = in.dbp;
76 out.dbcnt = in.dbcnt;
77
78 if (in.dbcnt >= out.dbsz) {
79 /* If the output buffer is full, write it. */
80 dd_out(0);
81
82 /*
83 * Ddout copies the leftover output to the beginning of
84 * the buffer and resets the output buffer. Reset the
85 * input buffer to match it.
86 */
87 in.dbp = out.dbp;
88 in.dbcnt = out.dbcnt;
89 }
90}
91
92void
93def_close()
94{
95 /* Just update the count, everything is already in the buffer. */
96 if (in.dbcnt)
97 out.dbcnt = in.dbcnt;
98}
99
100#ifdef NO_CONV
101/* Build a smaller version (i.e. for a miniroot) */
102/* These can not be called, but just in case... */
103static char no_block[] = "unblock and -DNO_CONV?";
104void block() { errx(1, no_block + 2); }
105void block_close() { errx(1, no_block + 2); }
106void unblock() { errx(1, no_block); }
107void unblock_close() { errx(1, no_block); }
108#else /* NO_CONV */
109
110/*
111 * Copy variable length newline terminated records with a max size cbsz
112 * bytes to output. Records less than cbs are padded with spaces.
113 *
114 * max in buffer: MAX(ibs, cbsz)
115 * max out buffer: obs + cbsz
116 */
117void
118block()
119{
120 static int intrunc;
121 int ch = 0; /* pacify gcc */
122 int cnt, maxlen;
123 u_char *inp, *outp;
124 const u_char *t;
125
126 /*
127 * Record truncation can cross block boundaries. If currently in a
128 * truncation state, keep tossing characters until reach a newline.
129 * Start at the beginning of the buffer, as the input buffer is always
130 * left empty.
131 */
132 if (intrunc) {
133 for (inp = in.db, cnt = in.dbrcnt;
134 cnt && *inp++ != '\n'; --cnt);
135 if (!cnt) {
136 in.dbcnt = 0;
137 in.dbp = in.db;
138 return;
139 }
140 intrunc = 0;
141 /* Adjust the input buffer numbers. */
142 in.dbcnt = cnt - 1;
143 in.dbp = inp + cnt - 1;
144 }
145
146 /*
147 * Copy records (max cbsz size chunks) into the output buffer. The
148 * translation is done as we copy into the output buffer.
149 */
150 for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
151 maxlen = MIN(cbsz, in.dbcnt);
152 if ((t = ctab) != NULL)
153 for (cnt = 0;
154 cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
155 *outp++ = t[ch];
156 else
157 for (cnt = 0;
158 cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
159 *outp++ = ch;
160 /*
161 * Check for short record without a newline. Reassemble the
162 * input block.
163 */
164 if (ch != '\n' && in.dbcnt < cbsz) {
165 (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
166 break;
167 }
168
169 /* Adjust the input buffer numbers. */
170 in.dbcnt -= cnt;
171 if (ch == '\n')
172 --in.dbcnt;
173
174 /* Pad short records with spaces. */
175 if (cnt < cbsz)
176 (void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
177 else {
178 /*
179 * If the next character wouldn't have ended the
180 * block, it's a truncation.
181 */
182 if (!in.dbcnt || *inp != '\n')
183 ++st.trunc;
184
185 /* Toss characters to a newline. */
186 for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
187 if (!in.dbcnt)
188 intrunc = 1;
189 else
190 --in.dbcnt;
191 }
192
193 /* Adjust output buffer numbers. */
194 out.dbp += cbsz;
195 if ((out.dbcnt += cbsz) >= out.dbsz)
196 dd_out(0);
197 outp = out.dbp;
198 }
199 in.dbp = in.db + in.dbcnt;
200}
201
202void
203block_close()
204{
205 /*
206 * Copy any remaining data into the output buffer and pad to a record.
207 * Don't worry about truncation or translation, the input buffer is
208 * always empty when truncating, and no characters have been added for
209 * translation. The bottom line is that anything left in the input
210 * buffer is a truncated record. Anything left in the output buffer
211 * just wasn't big enough.
212 */
213 if (in.dbcnt) {
214 ++st.trunc;
215 (void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
216 (void)memset(out.dbp + in.dbcnt,
217 ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
218 out.dbcnt += cbsz;
219 }
220}
221
222/*
223 * Convert fixed length (cbsz) records to variable length. Deletes any
224 * trailing blanks and appends a newline.
225 *
226 * max in buffer: MAX(ibs, cbsz) + cbsz
227 * max out buffer: obs + cbsz
228 */
229void
230unblock()
231{
232 int cnt;
233 u_char *inp;
234 const u_char *t;
235
236 /* Translation and case conversion. */
237 if ((t = ctab) != NULL)
238 for (cnt = in.dbrcnt, inp = in.dbp; cnt--;)
239 *--inp = t[*inp];
240 /*
241 * Copy records (max cbsz size chunks) into the output buffer. The
242 * translation has to already be done or we might not recognize the
243 * spaces.
244 */
245 for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
246 for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
247 if (t >= inp) {
248 cnt = t - inp + 1;
249 (void)memmove(out.dbp, inp, cnt);
250 out.dbp += cnt;
251 out.dbcnt += cnt;
252 }
253 ++out.dbcnt;
254 *out.dbp++ = '\n';
255 if (out.dbcnt >= out.dbsz)
256 dd_out(0);
257 }
258 if (in.dbcnt)
259 (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
260 in.dbp = in.db + in.dbcnt;
261}
262
263void
264unblock_close()
265{
266 int cnt;
267 u_char *t;
268
269 if (in.dbcnt) {
270 warnx("%s: short input record", in.name);
271 for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t);
272 if (t >= in.db) {
273 cnt = t - in.db + 1;
274 (void)memmove(out.dbp, in.db, cnt);
275 out.dbp += cnt;
276 out.dbcnt += cnt;
277 }
278 ++out.dbcnt;
279 *out.dbp++ = '\n';
280 }
281}
282
283#endif /* NO_CONV */