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