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. 17*fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 184b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 194b88c807SRodney W. Grimes * without specific prior written permission. 204b88c807SRodney W. Grimes * 214b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 224b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 234b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 244b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 254b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 264b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 274b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 284b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 294b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 304b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 314b88c807SRodney W. Grimes * SUCH DAMAGE. 324b88c807SRodney W. Grimes */ 334b88c807SRodney W. Grimes 344b88c807SRodney W. Grimes #ifndef lint 35cbf6f7d3SPhilippe Charnier #if 0 361ba0e048SPhilippe Charnier static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94"; 37cbf6f7d3SPhilippe Charnier #endif 384b88c807SRodney W. Grimes #endif /* not lint */ 395eb43ac2SDavid E. O'Brien #include <sys/cdefs.h> 405eb43ac2SDavid E. O'Brien __FBSDID("$FreeBSD$"); 414b88c807SRodney W. Grimes 424b88c807SRodney W. Grimes #include <sys/param.h> 434b88c807SRodney W. Grimes 444b88c807SRodney W. Grimes #include <err.h> 457503d74fSMark Murray #include <inttypes.h> 464b88c807SRodney W. Grimes #include <string.h> 474b88c807SRodney W. Grimes 484b88c807SRodney W. Grimes #include "dd.h" 494b88c807SRodney W. Grimes #include "extern.h" 504b88c807SRodney W. Grimes 514b88c807SRodney W. Grimes /* 524b88c807SRodney W. Grimes * def -- 534b88c807SRodney W. Grimes * Copy input to output. Input is buffered until reaches obs, and then 544b88c807SRodney W. Grimes * output until less than obs remains. Only a single buffer is used. 554b88c807SRodney W. Grimes * Worst case buffer calculation is (ibs + obs - 1). 564b88c807SRodney W. Grimes */ 574b88c807SRodney W. Grimes void 58f9bcb0beSWarner Losh def(void) 594b88c807SRodney W. Grimes { 6058687472SBrian Feldman u_char *inp; 6158687472SBrian Feldman const u_char *t; 6258687472SBrian Feldman size_t cnt; 634b88c807SRodney W. Grimes 64ad66f7eeSPoul-Henning Kamp if ((t = ctab) != NULL) 654b88c807SRodney W. Grimes for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp) 664b88c807SRodney W. Grimes *inp = t[*inp]; 674b88c807SRodney W. Grimes 684b88c807SRodney W. Grimes /* Make the output buffer look right. */ 694b88c807SRodney W. Grimes out.dbp = in.dbp; 704b88c807SRodney W. Grimes out.dbcnt = in.dbcnt; 714b88c807SRodney W. Grimes 724b88c807SRodney W. Grimes if (in.dbcnt >= out.dbsz) { 734b88c807SRodney W. Grimes /* If the output buffer is full, write it. */ 744b88c807SRodney W. Grimes dd_out(0); 754b88c807SRodney W. Grimes 764b88c807SRodney W. Grimes /* 7766d5f719SThomas Quinot * dd_out copies the leftover output to the beginning of 784b88c807SRodney W. Grimes * the buffer and resets the output buffer. Reset the 794b88c807SRodney W. Grimes * input buffer to match it. 804b88c807SRodney W. Grimes */ 814b88c807SRodney W. Grimes in.dbp = out.dbp; 824b88c807SRodney W. Grimes in.dbcnt = out.dbcnt; 834b88c807SRodney W. Grimes } 844b88c807SRodney W. Grimes } 854b88c807SRodney W. Grimes 864b88c807SRodney W. Grimes void 87f9bcb0beSWarner Losh def_close(void) 884b88c807SRodney W. Grimes { 894b88c807SRodney W. Grimes /* Just update the count, everything is already in the buffer. */ 904b88c807SRodney W. Grimes if (in.dbcnt) 914b88c807SRodney W. Grimes out.dbcnt = in.dbcnt; 924b88c807SRodney W. Grimes } 934b88c807SRodney W. Grimes 944b88c807SRodney W. Grimes /* 954b88c807SRodney W. Grimes * Copy variable length newline terminated records with a max size cbsz 964b88c807SRodney W. Grimes * bytes to output. Records less than cbs are padded with spaces. 974b88c807SRodney W. Grimes * 984b88c807SRodney W. Grimes * max in buffer: MAX(ibs, cbsz) 994b88c807SRodney W. Grimes * max out buffer: obs + cbsz 1004b88c807SRodney W. Grimes */ 1014b88c807SRodney W. Grimes void 102f9bcb0beSWarner Losh block(void) 1034b88c807SRodney W. Grimes { 10458687472SBrian Feldman u_char *inp, *outp; 10558687472SBrian Feldman const u_char *t; 10658687472SBrian Feldman size_t cnt, maxlen; 1074b88c807SRodney W. Grimes static int intrunc; 1087599187eSBrian Feldman int ch; 1094b88c807SRodney W. Grimes 1104b88c807SRodney W. Grimes /* 1114b88c807SRodney W. Grimes * Record truncation can cross block boundaries. If currently in a 1124b88c807SRodney W. Grimes * truncation state, keep tossing characters until reach a newline. 1134b88c807SRodney W. Grimes * Start at the beginning of the buffer, as the input buffer is always 1144b88c807SRodney W. Grimes * left empty. 1154b88c807SRodney W. Grimes */ 1164b88c807SRodney W. Grimes if (intrunc) { 117767bc8adSBrian Feldman for (inp = in.db, cnt = in.dbrcnt; cnt && *inp++ != '\n'; --cnt) 118767bc8adSBrian Feldman ; 1194b88c807SRodney W. Grimes if (!cnt) { 1204b88c807SRodney W. Grimes in.dbcnt = 0; 1214b88c807SRodney W. Grimes in.dbp = in.db; 1224b88c807SRodney W. Grimes return; 1234b88c807SRodney W. Grimes } 1244b88c807SRodney W. Grimes intrunc = 0; 1254b88c807SRodney W. Grimes /* Adjust the input buffer numbers. */ 1264b88c807SRodney W. Grimes in.dbcnt = cnt - 1; 1274b88c807SRodney W. Grimes in.dbp = inp + cnt - 1; 1284b88c807SRodney W. Grimes } 1294b88c807SRodney W. Grimes 1304b88c807SRodney W. Grimes /* 1314b88c807SRodney W. Grimes * Copy records (max cbsz size chunks) into the output buffer. The 1324b88c807SRodney W. Grimes * translation is done as we copy into the output buffer. 1334b88c807SRodney W. Grimes */ 13478b09ffeSSteve Price ch = 0; 1354b88c807SRodney W. Grimes for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) { 136f4cfd28bSKurt Jaeger maxlen = MIN(cbsz, in.dbcnt); 137ad66f7eeSPoul-Henning Kamp if ((t = ctab) != NULL) 138767bc8adSBrian Feldman for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n'; 139767bc8adSBrian Feldman ++cnt) 1404b88c807SRodney W. Grimes *outp++ = t[ch]; 1414b88c807SRodney W. Grimes else 142767bc8adSBrian Feldman for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n'; 143767bc8adSBrian Feldman ++cnt) 1444b88c807SRodney W. Grimes *outp++ = ch; 1454b88c807SRodney W. Grimes /* 1464b88c807SRodney W. Grimes * Check for short record without a newline. Reassemble the 1474b88c807SRodney W. Grimes * input block. 1484b88c807SRodney W. Grimes */ 149f4cfd28bSKurt Jaeger if (ch != '\n' && in.dbcnt < cbsz) { 15058687472SBrian Feldman (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt); 1514b88c807SRodney W. Grimes break; 1524b88c807SRodney W. Grimes } 1534b88c807SRodney W. Grimes 1544b88c807SRodney W. Grimes /* Adjust the input buffer numbers. */ 1554b88c807SRodney W. Grimes in.dbcnt -= cnt; 1564b88c807SRodney W. Grimes if (ch == '\n') 1574b88c807SRodney W. Grimes --in.dbcnt; 1584b88c807SRodney W. Grimes 1594b88c807SRodney W. Grimes /* Pad short records with spaces. */ 1604b88c807SRodney W. Grimes if (cnt < cbsz) 1614b88c807SRodney W. Grimes (void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt); 1624b88c807SRodney W. Grimes else { 1634b88c807SRodney W. Grimes /* 1644b88c807SRodney W. Grimes * If the next character wouldn't have ended the 1654b88c807SRodney W. Grimes * block, it's a truncation. 1664b88c807SRodney W. Grimes */ 1674b88c807SRodney W. Grimes if (!in.dbcnt || *inp != '\n') 1684b88c807SRodney W. Grimes ++st.trunc; 1694b88c807SRodney W. Grimes 1704b88c807SRodney W. Grimes /* Toss characters to a newline. */ 1717503d74fSMark Murray for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt) 1727503d74fSMark Murray ; 1734b88c807SRodney W. Grimes if (!in.dbcnt) 1744b88c807SRodney W. Grimes intrunc = 1; 1754b88c807SRodney W. Grimes else 1764b88c807SRodney W. Grimes --in.dbcnt; 1774b88c807SRodney W. Grimes } 1784b88c807SRodney W. Grimes 1794b88c807SRodney W. Grimes /* Adjust output buffer numbers. */ 1804b88c807SRodney W. Grimes out.dbp += cbsz; 1814b88c807SRodney W. Grimes if ((out.dbcnt += cbsz) >= out.dbsz) 1824b88c807SRodney W. Grimes dd_out(0); 1834b88c807SRodney W. Grimes outp = out.dbp; 1844b88c807SRodney W. Grimes } 1854b88c807SRodney W. Grimes in.dbp = in.db + in.dbcnt; 1864b88c807SRodney W. Grimes } 1874b88c807SRodney W. Grimes 1884b88c807SRodney W. Grimes void 189f9bcb0beSWarner Losh block_close(void) 1904b88c807SRodney W. Grimes { 1914b88c807SRodney W. Grimes /* 1924b88c807SRodney W. Grimes * Copy any remaining data into the output buffer and pad to a record. 1934b88c807SRodney W. Grimes * Don't worry about truncation or translation, the input buffer is 1944b88c807SRodney W. Grimes * always empty when truncating, and no characters have been added for 1954b88c807SRodney W. Grimes * translation. The bottom line is that anything left in the input 1964b88c807SRodney W. Grimes * buffer is a truncated record. Anything left in the output buffer 1974b88c807SRodney W. Grimes * just wasn't big enough. 1984b88c807SRodney W. Grimes */ 1994b88c807SRodney W. Grimes if (in.dbcnt) { 2004b88c807SRodney W. Grimes ++st.trunc; 20158687472SBrian Feldman (void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt); 202767bc8adSBrian Feldman (void)memset(out.dbp + in.dbcnt, ctab ? ctab[' '] : ' ', 203767bc8adSBrian Feldman cbsz - in.dbcnt); 2044b88c807SRodney W. Grimes out.dbcnt += cbsz; 2054b88c807SRodney W. Grimes } 2064b88c807SRodney W. Grimes } 2074b88c807SRodney W. Grimes 2084b88c807SRodney W. Grimes /* 2094b88c807SRodney W. Grimes * Convert fixed length (cbsz) records to variable length. Deletes any 2104b88c807SRodney W. Grimes * trailing blanks and appends a newline. 2114b88c807SRodney W. Grimes * 2124b88c807SRodney W. Grimes * max in buffer: MAX(ibs, cbsz) + cbsz 2134b88c807SRodney W. Grimes * max out buffer: obs + cbsz 2144b88c807SRodney W. Grimes */ 2154b88c807SRodney W. Grimes void 216f9bcb0beSWarner Losh unblock(void) 2174b88c807SRodney W. Grimes { 21858687472SBrian Feldman u_char *inp; 21958687472SBrian Feldman const u_char *t; 22058687472SBrian Feldman size_t cnt; 2214b88c807SRodney W. Grimes 2224b88c807SRodney W. Grimes /* Translation and case conversion. */ 223ad66f7eeSPoul-Henning Kamp if ((t = ctab) != NULL) 2247503d74fSMark Murray for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp) 2257503d74fSMark Murray *inp = t[*inp]; 2264b88c807SRodney W. Grimes /* 2274b88c807SRodney W. Grimes * Copy records (max cbsz size chunks) into the output buffer. The 2284b88c807SRodney W. Grimes * translation has to already be done or we might not recognize the 2294b88c807SRodney W. Grimes * spaces. 2304b88c807SRodney W. Grimes */ 231f4cfd28bSKurt Jaeger for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) { 232767bc8adSBrian Feldman for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t) 233767bc8adSBrian Feldman ; 2344b88c807SRodney W. Grimes if (t >= inp) { 2354b88c807SRodney W. Grimes cnt = t - inp + 1; 23658687472SBrian Feldman (void)memmove(out.dbp, inp, cnt); 2374b88c807SRodney W. Grimes out.dbp += cnt; 2384b88c807SRodney W. Grimes out.dbcnt += cnt; 2394b88c807SRodney W. Grimes } 2404b88c807SRodney W. Grimes *out.dbp++ = '\n'; 241767bc8adSBrian Feldman if (++out.dbcnt >= out.dbsz) 2424b88c807SRodney W. Grimes dd_out(0); 2434b88c807SRodney W. Grimes } 2444b88c807SRodney W. Grimes if (in.dbcnt) 24558687472SBrian Feldman (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt); 2464b88c807SRodney W. Grimes in.dbp = in.db + in.dbcnt; 2474b88c807SRodney W. Grimes } 2484b88c807SRodney W. Grimes 2494b88c807SRodney W. Grimes void 250f9bcb0beSWarner Losh unblock_close(void) 2514b88c807SRodney W. Grimes { 2524b88c807SRodney W. Grimes u_char *t; 25358687472SBrian Feldman size_t cnt; 2544b88c807SRodney W. Grimes 2554b88c807SRodney W. Grimes if (in.dbcnt) { 2564b88c807SRodney W. Grimes warnx("%s: short input record", in.name); 257767bc8adSBrian Feldman for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t) 258767bc8adSBrian Feldman ; 2594b88c807SRodney W. Grimes if (t >= in.db) { 2604b88c807SRodney W. Grimes cnt = t - in.db + 1; 26158687472SBrian Feldman (void)memmove(out.dbp, in.db, cnt); 2624b88c807SRodney W. Grimes out.dbp += cnt; 2634b88c807SRodney W. Grimes out.dbcnt += cnt; 2644b88c807SRodney W. Grimes } 2654b88c807SRodney W. Grimes ++out.dbcnt; 2664b88c807SRodney W. Grimes *out.dbp++ = '\n'; 2674b88c807SRodney W. Grimes } 2684b88c807SRodney W. Grimes } 269