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