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