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