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