Deleted Added
full compact
conv.c (50471) conv.c (51208)
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 *

--- 26 unchanged lines hidden (view full) ---

35 * SUCH DAMAGE.
36 */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
41#endif
42static const char rcsid[] =
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 *

--- 26 unchanged lines hidden (view full) ---

35 * SUCH DAMAGE.
36 */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
41#endif
42static const char rcsid[] =
43 "$FreeBSD: head/bin/dd/conv.c 50471 1999-08-27 23:15:48Z peter $";
43 "$FreeBSD: head/bin/dd/conv.c 51208 1999-09-12 16:51:53Z green $";
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 */
60void
61def()
62{
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 */
60void
61def()
62{
63 int cnt;
64 u_char *inp, *t;
63 u_char *inp;
64 const u_char *t;
65 size_t cnt;
65
66 if ((t = ctab) != NULL)
67 for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
68 *inp = t[*inp];
69
70 /* Make the output buffer look right. */
71 out.dbp = in.dbp;
72 out.dbcnt = in.dbcnt;

--- 25 unchanged lines hidden (view full) ---

98 * bytes to output. Records less than cbs are padded with spaces.
99 *
100 * max in buffer: MAX(ibs, cbsz)
101 * max out buffer: obs + cbsz
102 */
103void
104block()
105{
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;

--- 25 unchanged lines hidden (view full) ---

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 */
104void
105block()
106{
107 u_char *inp, *outp;
108 const u_char *t;
109 size_t cnt, maxlen;
106 static int intrunc;
110 static int intrunc;
107 int ch, cnt, maxlen;
108 u_char *inp, *outp, *t;
111 int ch = -1;
109
110 /*
111 * Record truncation can cross block boundaries. If currently in a
112 * truncation state, keep tossing characters until reach a newline.
113 * Start at the beginning of the buffer, as the input buffer is always
114 * left empty.
115 */
116 if (intrunc) {

--- 25 unchanged lines hidden (view full) ---

142 for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
143 ++cnt)
144 *outp++ = ch;
145 /*
146 * Check for short record without a newline. Reassemble the
147 * input block.
148 */
149 if (ch != '\n' && in.dbcnt < cbsz) {
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) {

--- 25 unchanged lines hidden (view full) ---

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) {
150 memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
153 (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
151 break;
152 }
153
154 /* Adjust the input buffer numbers. */
155 in.dbcnt -= cnt;
156 if (ch == '\n')
157 --in.dbcnt;
158

--- 33 unchanged lines hidden (view full) ---

192 * Don't worry about truncation or translation, the input buffer is
193 * always empty when truncating, and no characters have been added for
194 * translation. The bottom line is that anything left in the input
195 * buffer is a truncated record. Anything left in the output buffer
196 * just wasn't big enough.
197 */
198 if (in.dbcnt) {
199 ++st.trunc;
154 break;
155 }
156
157 /* Adjust the input buffer numbers. */
158 in.dbcnt -= cnt;
159 if (ch == '\n')
160 --in.dbcnt;
161

--- 33 unchanged lines hidden (view full) ---

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;
200 memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
203 (void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
201 (void)memset(out.dbp + in.dbcnt, ctab ? ctab[' '] : ' ',
202 cbsz - in.dbcnt);
203 out.dbcnt += cbsz;
204 }
205}
206
207/*
208 * Convert fixed length (cbsz) records to variable length. Deletes any
209 * trailing blanks and appends a newline.
210 *
211 * max in buffer: MAX(ibs, cbsz) + cbsz
212 * max out buffer: obs + cbsz
213 */
214void
215unblock()
216{
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 */
217void
218unblock()
219{
217 int cnt;
218 u_char *inp, *t;
220 u_char *inp;
221 const u_char *t;
222 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 = t[*inp];
224 /*
225 * Copy records (max cbsz size chunks) into the output buffer. The
226 * translation has to already be done or we might not recognize the
227 * spaces.
228 */
229 for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
230 for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t)
231 ;
232 if (t >= inp) {
233 cnt = t - inp + 1;
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 /*
229 * Copy records (max cbsz size chunks) into the output buffer. The
230 * translation has to already be done or we might not recognize the
231 * spaces.
232 */
233 for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
234 for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t)
235 ;
236 if (t >= inp) {
237 cnt = t - inp + 1;
234 memmove(out.dbp, inp, cnt);
238 (void)memmove(out.dbp, inp, cnt);
235 out.dbp += cnt;
236 out.dbcnt += cnt;
237 }
238 *out.dbp++ = '\n';
239 if (++out.dbcnt >= out.dbsz)
240 dd_out(0);
241 }
242 if (in.dbcnt)
239 out.dbp += cnt;
240 out.dbcnt += cnt;
241 }
242 *out.dbp++ = '\n';
243 if (++out.dbcnt >= out.dbsz)
244 dd_out(0);
245 }
246 if (in.dbcnt)
243 memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
247 (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
244 in.dbp = in.db + in.dbcnt;
245}
246
247void
248unblock_close()
249{
248 in.dbp = in.db + in.dbcnt;
249}
250
251void
252unblock_close()
253{
250 int cnt;
251 u_char *t;
254 u_char *t;
255 size_t cnt;
252
253 if (in.dbcnt) {
254 warnx("%s: short input record", in.name);
255 for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t)
256 ;
257 if (t >= in.db) {
258 cnt = t - in.db + 1;
256
257 if (in.dbcnt) {
258 warnx("%s: short input record", in.name);
259 for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t)
260 ;
261 if (t >= in.db) {
262 cnt = t - in.db + 1;
259 memmove(out.dbp, in.db, cnt);
263 (void)memmove(out.dbp, in.db, cnt);
260 out.dbp += cnt;
261 out.dbcnt += cnt;
262 }
263 ++out.dbcnt;
264 *out.dbp++ = '\n';
265 }
266}
264 out.dbp += cnt;
265 out.dbcnt += cnt;
266 }
267 ++out.dbcnt;
268 *out.dbp++ = '\n';
269 }
270}