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