Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:19

0001 /*-
0002  * Copyright (c) 1991, 1993, 1994
0003  *  The Regents of the University of California.  All rights reserved.
0004  *
0005  * This code is derived from software contributed to Berkeley by
0006  * Keith Muller of the University of California, San Diego and Lance
0007  * Visser of Convex Computer Corporation.
0008  *
0009  * Redistribution and use in source and binary forms, with or without
0010  * modification, are permitted provided that the following conditions
0011  * are met:
0012  * 1. Redistributions of source code must retain the above copyright
0013  *    notice, this list of conditions and the following disclaimer.
0014  * 2. Redistributions in binary form must reproduce the above copyright
0015  *    notice, this list of conditions and the following disclaimer in the
0016  *    documentation and/or other materials provided with the distribution.
0017  * 4. Neither the name of the University nor the names of its contributors
0018  *    may be used to endorse or promote products derived from this software
0019  *    without specific prior written permission.
0020  *
0021  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
0022  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0023  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0024  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
0025  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
0026  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
0027  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
0028  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
0029  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
0030  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
0031  * SUCH DAMAGE.
0032  */
0033 
0034 #ifdef HAVE_CONFIG_H
0035 #include "config.h"
0036 #endif
0037 
0038 #ifndef lint
0039 #if 0
0040 static char sccsid[] = "@(#)conv.c  8.3 (Berkeley) 4/2/94";
0041 #endif
0042 #endif /* not lint */
0043 #include <sys/cdefs.h>
0044 __FBSDID("$FreeBSD: src/bin/dd/conv.c,v 1.19 2004/04/06 20:06:45 markm Exp $");
0045 
0046 #include <sys/param.h>
0047 
0048 #include "err.h"
0049 #include <inttypes.h>
0050 #include <string.h>
0051 
0052 #include "dd.h"
0053 #include "extern-dd.h"
0054 
0055 /*
0056  * def --
0057  * Copy input to output.  Input is buffered until reaches obs, and then
0058  * output until less than obs remains.  Only a single buffer is used.
0059  * Worst case buffer calculation is (ibs + obs - 1).
0060  */
0061 void
0062 def(rtems_shell_dd_globals* globals)
0063 {
0064     u_char *inp;
0065     const u_char *t;
0066     size_t cnt;
0067 
0068     if ((t = ctab) != NULL)
0069         for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
0070             *inp = t[*inp];
0071 
0072     /* Make the output buffer look right. */
0073     out.dbp = in.dbp;
0074     out.dbcnt = in.dbcnt;
0075 
0076     if (in.dbcnt >= out.dbsz) {
0077         /* If the output buffer is full, write it. */
0078         dd_out(globals, 0);
0079 
0080         /*
0081          * Ddout copies the leftover output to the beginning of
0082          * the buffer and resets the output buffer.  Reset the
0083          * input buffer to match it.
0084          */
0085         in.dbp = out.dbp;
0086         in.dbcnt = out.dbcnt;
0087     }
0088 }
0089 
0090 void
0091 def_close(rtems_shell_dd_globals* globals)
0092 {
0093     /* Just update the count, everything is already in the buffer. */
0094     if (in.dbcnt)
0095         out.dbcnt = in.dbcnt;
0096 }
0097 
0098 /*
0099  * Copy variable length newline terminated records with a max size cbsz
0100  * bytes to output.  Records less than cbs are padded with spaces.
0101  *
0102  * max in buffer:  MAX(ibs, cbsz)
0103  * max out buffer: obs + cbsz
0104  */
0105 void
0106 block(rtems_shell_dd_globals* globals)
0107 {
0108     u_char *inp, *outp;
0109     const u_char *t;
0110     size_t cnt, maxlen;
0111     static int intrunc;
0112     int ch;
0113 
0114     /*
0115      * Record truncation can cross block boundaries.  If currently in a
0116      * truncation state, keep tossing characters until reach a newline.
0117      * Start at the beginning of the buffer, as the input buffer is always
0118      * left empty.
0119      */
0120     if (intrunc) {
0121         for (inp = in.db, cnt = in.dbrcnt; cnt && *inp++ != '\n'; --cnt)
0122             ;
0123         if (!cnt) {
0124             in.dbcnt = 0;
0125             in.dbp = in.db;
0126             return;
0127         }
0128         intrunc = 0;
0129         /* Adjust the input buffer numbers. */
0130         in.dbcnt = cnt - 1;
0131         in.dbp = inp + cnt - 1;
0132     }
0133 
0134     /*
0135      * Copy records (max cbsz size chunks) into the output buffer.  The
0136      * translation is done as we copy into the output buffer.
0137      */
0138     ch = 0;
0139     for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
0140         maxlen = MIN(cbsz, in.dbcnt);
0141         if ((t = ctab) != NULL)
0142             for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
0143                 ++cnt)
0144                 *outp++ = t[ch];
0145         else
0146             for (cnt = 0; cnt < maxlen && (ch = *inp++) != '\n';
0147                 ++cnt)
0148                 *outp++ = ch;
0149         /*
0150          * Check for short record without a newline.  Reassemble the
0151          * input block.
0152          */
0153         if (ch != '\n' && in.dbcnt < cbsz) {
0154             (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
0155             break;
0156         }
0157 
0158         /* Adjust the input buffer numbers. */
0159         in.dbcnt -= cnt;
0160         if (ch == '\n')
0161             --in.dbcnt;
0162 
0163         /* Pad short records with spaces. */
0164         if (cnt < cbsz)
0165             (void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
0166         else {
0167             /*
0168              * If the next character wouldn't have ended the
0169              * block, it's a truncation.
0170              */
0171             if (!in.dbcnt || *inp != '\n')
0172                 ++st.trunc;
0173 
0174             /* Toss characters to a newline. */
0175             for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt)
0176                 ;
0177             if (!in.dbcnt)
0178                 intrunc = 1;
0179             else
0180                 --in.dbcnt;
0181         }
0182 
0183         /* Adjust output buffer numbers. */
0184         out.dbp += cbsz;
0185         if ((out.dbcnt += cbsz) >= out.dbsz)
0186             dd_out(globals, 0);
0187         outp = out.dbp;
0188     }
0189     in.dbp = in.db + in.dbcnt;
0190 }
0191 
0192 void
0193 block_close(rtems_shell_dd_globals* globals)
0194 {
0195     /*
0196      * Copy any remaining data into the output buffer and pad to a record.
0197      * Don't worry about truncation or translation, the input buffer is
0198      * always empty when truncating, and no characters have been added for
0199      * translation.  The bottom line is that anything left in the input
0200      * buffer is a truncated record.  Anything left in the output buffer
0201      * just wasn't big enough.
0202      */
0203     if (in.dbcnt) {
0204         ++st.trunc;
0205         (void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
0206         (void)memset(out.dbp + in.dbcnt, ctab ? ctab[' '] : ' ',
0207             cbsz - in.dbcnt);
0208         out.dbcnt += cbsz;
0209     }
0210 }
0211 
0212 /*
0213  * Convert fixed length (cbsz) records to variable length.  Deletes any
0214  * trailing blanks and appends a newline.
0215  *
0216  * max in buffer:  MAX(ibs, cbsz) + cbsz
0217  * max out buffer: obs + cbsz
0218  */
0219 void
0220 unblock(rtems_shell_dd_globals* globals)
0221 {
0222     u_char *inp;
0223     const u_char *t;
0224     size_t cnt;
0225 
0226     /* Translation and case conversion. */
0227     if ((t = ctab) != NULL)
0228         for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
0229             *inp = t[*inp];
0230     /*
0231      * Copy records (max cbsz size chunks) into the output buffer.  The
0232      * translation has to already be done or we might not recognize the
0233      * spaces.
0234      */
0235     for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
0236         for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t)
0237             ;
0238         if (t >= inp) {
0239             cnt = t - inp + 1;
0240             (void)memmove(out.dbp, inp, cnt);
0241             out.dbp += cnt;
0242             out.dbcnt += cnt;
0243         }
0244         *out.dbp++ = '\n';
0245         if (++out.dbcnt >= out.dbsz)
0246             dd_out(globals, 0);
0247     }
0248     if (in.dbcnt)
0249         (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
0250     in.dbp = in.db + in.dbcnt;
0251 }
0252 
0253 void
0254 unblock_close(rtems_shell_dd_globals* globals)
0255 {
0256     u_char *t;
0257     size_t cnt;
0258 
0259     if (in.dbcnt) {
0260         warnx("%s: short input record", in.name);
0261         for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t)
0262             ;
0263         if (t >= in.db) {
0264             cnt = t - in.db + 1;
0265             (void)memmove(out.dbp, in.db, cnt);
0266             out.dbp += cnt;
0267             out.dbcnt += cnt;
0268         }
0269         ++out.dbcnt;
0270         *out.dbp++ = '\n';
0271     }
0272 }