14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1991, 1993, 1994 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * This code is derived from software contributed to Berkeley by 64b88c807SRodney W. Grimes * Keith Muller of the University of California, San Diego and Lance 74b88c807SRodney W. Grimes * Visser of Convex Computer Corporation. 84b88c807SRodney W. Grimes * 94b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 104b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 114b88c807SRodney W. Grimes * are met: 124b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 134b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 144b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 154b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 164b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 174b88c807SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 184b88c807SRodney W. Grimes * must display the following acknowledgement: 194b88c807SRodney W. Grimes * This product includes software developed by the University of 204b88c807SRodney W. Grimes * California, Berkeley and its contributors. 214b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 224b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 234b88c807SRodney W. Grimes * without specific prior written permission. 244b88c807SRodney W. Grimes * 254b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 264b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 274b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 284b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 294b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 304b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 314b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 324b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 334b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 344b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 354b88c807SRodney W. Grimes * SUCH DAMAGE. 364b88c807SRodney W. Grimes */ 374b88c807SRodney W. Grimes 384b88c807SRodney W. Grimes #ifndef lint 39cbf6f7d3SPhilippe Charnier #if 0 401ba0e048SPhilippe Charnier static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94"; 41cbf6f7d3SPhilippe Charnier #endif 424b88c807SRodney W. Grimes #endif /* not lint */ 435eb43ac2SDavid E. O'Brien #include <sys/cdefs.h> 445eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$"); 454b88c807SRodney W. Grimes 464b88c807SRodney W. Grimes #include <sys/param.h> 474b88c807SRodney W. Grimes 484b88c807SRodney W. Grimes #include <err.h> 497503d74fSMark Murray #include <inttypes.h> 504b88c807SRodney W. Grimes #include <string.h> 514b88c807SRodney W. Grimes 524b88c807SRodney W. Grimes #include "dd.h" 534b88c807SRodney W. Grimes #include "extern.h" 544b88c807SRodney W. Grimes 554b88c807SRodney W. Grimes /* 564b88c807SRodney W. Grimes * def -- 574b88c807SRodney W. Grimes * Copy input to output. Input is buffered until reaches obs, and then 584b88c807SRodney W. Grimes * output until less than obs remains. Only a single buffer is used. 594b88c807SRodney W. Grimes * Worst case buffer calculation is (ibs + obs - 1). 604b88c807SRodney W. Grimes */ 614b88c807SRodney W. Grimes void 62f9bcb0beSWarner Losh def(void) 634b88c807SRodney W. Grimes { 6458687472SBrian Feldman u_char *inp; 6558687472SBrian Feldman const u_char *t; 6658687472SBrian Feldman size_t cnt; 674b88c807SRodney W. Grimes 68ad66f7eeSPoul-Henning Kamp if ((t = ctab) != NULL) 694b88c807SRodney W. Grimes for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp) 704b88c807SRodney W. Grimes *inp = t[*inp]; 714b88c807SRodney W. Grimes 724b88c807SRodney W. Grimes /* Make the output buffer look right. */ 734b88c807SRodney W. Grimes out.dbp = in.dbp; 744b88c807SRodney W. Grimes out.dbcnt = in.dbcnt; 754b88c807SRodney W. Grimes 764b88c807SRodney W. Grimes if (in.dbcnt >= out.dbsz) { 774b88c807SRodney W. Grimes /* If the output buffer is full, write it. */ 784b88c807SRodney W. Grimes dd_out(0); 794b88c807SRodney W. Grimes 804b88c807SRodney W. Grimes /* 814b88c807SRodney W. Grimes * Ddout copies the leftover output to the beginning of 824b88c807SRodney W. Grimes * the buffer and resets the output buffer. Reset the 834b88c807SRodney W. Grimes * input buffer to match it. 844b88c807SRodney W. Grimes */ 854b88c807SRodney W. Grimes in.dbp = out.dbp; 864b88c807SRodney W. Grimes in.dbcnt = out.dbcnt; 874b88c807SRodney W. Grimes } 884b88c807SRodney W. Grimes } 894b88c807SRodney W. Grimes 904b88c807SRodney W. Grimes void 91f9bcb0beSWarner Losh def_close(void) 924b88c807SRodney W. Grimes { 934b88c807SRodney W. Grimes /* Just update the count, everything is already in the buffer. */ 944b88c807SRodney W. Grimes if (in.dbcnt) 954b88c807SRodney W. Grimes out.dbcnt = in.dbcnt; 964b88c807SRodney W. Grimes } 974b88c807SRodney W. Grimes 984b88c807SRodney W. Grimes /* 994b88c807SRodney W. Grimes * Copy variable length newline terminated records with a max size cbsz 1004b88c807SRodney W. Grimes * bytes to output. Records less than cbs are padded with spaces. 1014b88c807SRodney W. Grimes * 1024b88c807SRodney W. Grimes * max in buffer: MAX(ibs, cbsz) 1034b88c807SRodney W. Grimes * max out buffer: obs + cbsz 1044b88c807SRodney W. Grimes */ 1054b88c807SRodney W. Grimes void 106f9bcb0beSWarner Losh block(void) 1074b88c807SRodney W. Grimes { 10858687472SBrian Feldman u_char *inp, *outp; 10958687472SBrian Feldman const u_char *t; 11058687472SBrian Feldman size_t cnt, maxlen; 1114b88c807SRodney W. Grimes static int intrunc; 1127599187eSBrian Feldman int ch; 1134b88c807SRodney W. Grimes 1144b88c807SRodney W. Grimes /* 1154b88c807SRodney W. Grimes * Record truncation can cross block boundaries. If currently in a 1164b88c807SRodney W. Grimes * truncation state, keep tossing characters until reach a newline. 1174b88c807SRodney W. Grimes * Start at the beginning of the buffer, as the input buffer is always 1184b88c807SRodney W. Grimes * left empty. 1194b88c807SRodney W. Grimes */ 1204b88c807SRodney W. Grimes if (intrunc) { 121767bc8adSBrian Feldman for (inp = in.db, cnt = in.dbrcnt; cnt && *inp++ != '\n'; --cnt) 122767bc8adSBrian Feldman ; 1234b88c807SRodney W. Grimes if (!cnt) { 1244b88c807SRodney W. Grimes in.dbcnt = 0; 1254b88c807SRodney W. Grimes in.dbp = in.db; 1264b88c807SRodney W. Grimes return; 1274b88c807SRodney W. Grimes } 1284b88c807SRodney W. Grimes intrunc = 0; 1294b88c807SRodney W. Grimes /* Adjust the input buffer numbers. */ 1304b88c807SRodney W. Grimes in.dbcnt = cnt - 1; 1314b88c807SRodney W. Grimes in.dbp = inp + cnt - 1; 1324b88c807SRodney W. Grimes } 1334b88c807SRodney W. Grimes 1344b88c807SRodney W. Grimes /* 1354b88c807SRodney W. Grimes * Copy records (max cbsz size chunks) into the output buffer. The 1364b88c807SRodney W. Grimes * translation is done as we copy into the output buffer. 1374b88c807SRodney W. Grimes */ 13878b09ffeSSteve Price ch = 0; 1394b88c807SRodney W. Grimes for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) { 1404b88c807SRodney W. Grimes maxlen = MIN(cbsz, in.dbcnt); 141ad66f7eeSPoul-Henning Kamp if ((t = ctab) != NULL) 142767bc8adSBrian Feldman for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n'; 143767bc8adSBrian Feldman ++cnt) 1444b88c807SRodney W. Grimes *outp++ = t[ch]; 1454b88c807SRodney W. Grimes else 146767bc8adSBrian Feldman for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n'; 147767bc8adSBrian Feldman ++cnt) 1484b88c807SRodney W. Grimes *outp++ = ch; 1494b88c807SRodney W. Grimes /* 1504b88c807SRodney W. Grimes * Check for short record without a newline. Reassemble the 1514b88c807SRodney W. Grimes * input block. 1524b88c807SRodney W. Grimes */ 1534b88c807SRodney W. Grimes if (ch != '\n' && in.dbcnt < cbsz) { 15458687472SBrian Feldman (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt); 1554b88c807SRodney W. Grimes break; 1564b88c807SRodney W. Grimes } 1574b88c807SRodney W. Grimes 1584b88c807SRodney W. Grimes /* Adjust the input buffer numbers. */ 1594b88c807SRodney W. Grimes in.dbcnt -= cnt; 1604b88c807SRodney W. Grimes if (ch == '\n') 1614b88c807SRodney W. Grimes --in.dbcnt; 1624b88c807SRodney W. Grimes 1634b88c807SRodney W. Grimes /* Pad short records with spaces. */ 1644b88c807SRodney W. Grimes if (cnt < cbsz) 1654b88c807SRodney W. Grimes (void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt); 1664b88c807SRodney W. Grimes else { 1674b88c807SRodney W. Grimes /* 1684b88c807SRodney W. Grimes * If the next character wouldn't have ended the 1694b88c807SRodney W. Grimes * block, it's a truncation. 1704b88c807SRodney W. Grimes */ 1714b88c807SRodney W. Grimes if (!in.dbcnt || *inp != '\n') 1724b88c807SRodney W. Grimes ++st.trunc; 1734b88c807SRodney W. Grimes 1744b88c807SRodney W. Grimes /* Toss characters to a newline. */ 1757503d74fSMark Murray for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt) 1767503d74fSMark Murray ; 1774b88c807SRodney W. Grimes if (!in.dbcnt) 1784b88c807SRodney W. Grimes intrunc = 1; 1794b88c807SRodney W. Grimes else 1804b88c807SRodney W. Grimes --in.dbcnt; 1814b88c807SRodney W. Grimes } 1824b88c807SRodney W. Grimes 1834b88c807SRodney W. Grimes /* Adjust output buffer numbers. */ 1844b88c807SRodney W. Grimes out.dbp += cbsz; 1854b88c807SRodney W. Grimes if ((out.dbcnt += cbsz) >= out.dbsz) 1864b88c807SRodney W. Grimes dd_out(0); 1874b88c807SRodney W. Grimes outp = out.dbp; 1884b88c807SRodney W. Grimes } 1894b88c807SRodney W. Grimes in.dbp = in.db + in.dbcnt; 1904b88c807SRodney W. Grimes } 1914b88c807SRodney W. Grimes 1924b88c807SRodney W. Grimes void 193f9bcb0beSWarner Losh block_close(void) 1944b88c807SRodney W. Grimes { 1954b88c807SRodney W. Grimes /* 1964b88c807SRodney W. Grimes * Copy any remaining data into the output buffer and pad to a record. 1974b88c807SRodney W. Grimes * Don't worry about truncation or translation, the input buffer is 1984b88c807SRodney W. Grimes * always empty when truncating, and no characters have been added for 1994b88c807SRodney W. Grimes * translation. The bottom line is that anything left in the input 2004b88c807SRodney W. Grimes * buffer is a truncated record. Anything left in the output buffer 2014b88c807SRodney W. Grimes * just wasn't big enough. 2024b88c807SRodney W. Grimes */ 2034b88c807SRodney W. Grimes if (in.dbcnt) { 2044b88c807SRodney W. Grimes ++st.trunc; 20558687472SBrian Feldman (void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt); 206767bc8adSBrian Feldman (void)memset(out.dbp + in.dbcnt, ctab ? ctab[' '] : ' ', 207767bc8adSBrian Feldman cbsz - in.dbcnt); 2084b88c807SRodney W. Grimes out.dbcnt += cbsz; 2094b88c807SRodney W. Grimes } 2104b88c807SRodney W. Grimes } 2114b88c807SRodney W. Grimes 2124b88c807SRodney W. Grimes /* 2134b88c807SRodney W. Grimes * Convert fixed length (cbsz) records to variable length. Deletes any 2144b88c807SRodney W. Grimes * trailing blanks and appends a newline. 2154b88c807SRodney W. Grimes * 2164b88c807SRodney W. Grimes * max in buffer: MAX(ibs, cbsz) + cbsz 2174b88c807SRodney W. Grimes * max out buffer: obs + cbsz 2184b88c807SRodney W. Grimes */ 2194b88c807SRodney W. Grimes void 220f9bcb0beSWarner Losh unblock(void) 2214b88c807SRodney W. Grimes { 22258687472SBrian Feldman u_char *inp; 22358687472SBrian Feldman const u_char *t; 22458687472SBrian Feldman size_t cnt; 2254b88c807SRodney W. Grimes 2264b88c807SRodney W. Grimes /* Translation and case conversion. */ 227ad66f7eeSPoul-Henning Kamp if ((t = ctab) != NULL) 2287503d74fSMark Murray for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp) 2297503d74fSMark Murray *inp = t[*inp]; 2304b88c807SRodney W. Grimes /* 2314b88c807SRodney W. Grimes * Copy records (max cbsz size chunks) into the output buffer. The 2324b88c807SRodney W. Grimes * translation has to already be done or we might not recognize the 2334b88c807SRodney W. Grimes * spaces. 2344b88c807SRodney W. Grimes */ 2354b88c807SRodney W. Grimes for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) { 236767bc8adSBrian Feldman for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t) 237767bc8adSBrian Feldman ; 2384b88c807SRodney W. Grimes if (t >= inp) { 2394b88c807SRodney W. Grimes cnt = t - inp + 1; 24058687472SBrian Feldman (void)memmove(out.dbp, inp, cnt); 2414b88c807SRodney W. Grimes out.dbp += cnt; 2424b88c807SRodney W. Grimes out.dbcnt += cnt; 2434b88c807SRodney W. Grimes } 2444b88c807SRodney W. Grimes *out.dbp++ = '\n'; 245767bc8adSBrian Feldman if (++out.dbcnt >= out.dbsz) 2464b88c807SRodney W. Grimes dd_out(0); 2474b88c807SRodney W. Grimes } 2484b88c807SRodney W. Grimes if (in.dbcnt) 24958687472SBrian Feldman (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt); 2504b88c807SRodney W. Grimes in.dbp = in.db + in.dbcnt; 2514b88c807SRodney W. Grimes } 2524b88c807SRodney W. Grimes 2534b88c807SRodney W. Grimes void 254f9bcb0beSWarner Losh unblock_close(void) 2554b88c807SRodney W. Grimes { 2564b88c807SRodney W. Grimes u_char *t; 25758687472SBrian Feldman size_t cnt; 2584b88c807SRodney W. Grimes 2594b88c807SRodney W. Grimes if (in.dbcnt) { 2604b88c807SRodney W. Grimes warnx("%s: short input record", in.name); 261767bc8adSBrian Feldman for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t) 262767bc8adSBrian Feldman ; 2634b88c807SRodney W. Grimes if (t >= in.db) { 2644b88c807SRodney W. Grimes cnt = t - in.db + 1; 26558687472SBrian Feldman (void)memmove(out.dbp, in.db, cnt); 2664b88c807SRodney W. Grimes out.dbp += cnt; 2674b88c807SRodney W. Grimes out.dbcnt += cnt; 2684b88c807SRodney W. Grimes } 2694b88c807SRodney W. Grimes ++out.dbcnt; 2704b88c807SRodney W. Grimes *out.dbp++ = '\n'; 2714b88c807SRodney W. Grimes } 2724b88c807SRodney W. Grimes } 273