home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / mskermit / msbmkb.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  18KB  |  694 lines

  1. /* File MSBMKB.C
  2.  
  3. Author: Robert Weiner, Programming Plus, rweiner@watsun.cc.columbia.edu
  4.  
  5. Synopsis: Encodes a binary file into printable ASCII "boo" format, preserving
  6. the exact file length, using a 4-for-3 byte encoding.
  7.  
  8. Modification history:
  9.   28-APR-92    Initial pre-Alpha Release
  10.   29-APR-92    Fixes around fwrite, fclose
  11.             Changed writestr -> writeustr
  12.             Added prototypes
  13.         We're now at Beta Release!
  14.             MSDOS 5, MSC 5.1 tests out ok.
  15.             VAX/VMS VAXC032 is ok.
  16.             SUNOS is ok.
  17.             Both work with old or new msbpct.
  18.   30-APR-92    Fix to output()
  19.             Defaults to no prototypes
  20.             Added char counts logic
  21.             Added -v arg
  22.             writexstr takes chgcase now
  23.   01-MAY-92    Added 3rd arg interior-name
  24.             Added -l -u -q
  25.             Added stdin/out support
  26.             Add path stripping
  27.   02-MAY-92    Add '>' to path stripping (vms)
  28.   05-MAY-92    Release after outside testing
  29.             Added VOID usage() proto
  30.             Thanks to Christian Hemsing for OS-9 testing & defs.
  31.             Thanks to Steve Walton for Amiga testing & defs.
  32.   08-MAY-92    Prepare for general release
  33.             Added uchar defs, Modified _CDECL define,
  34.             Fixed up for MSDOS GNU CC
  35.             (GCC warnings noticed by Christian Hemsing)
  36.             Use gcc -DMSDOS to compile.
  37.             This MSDOS GCC defines "unix" which doesn't
  38.                 help us at all!
  39.   17-MAY-92        Add AtariST defs & improved __STDC__ check
  40.                   from Bruce Moore
  41.                         Removed string fns so don't need string.h.
  42.             Next general release now ready... Thanks to those
  43.                 listed in the directory below
  44.   12-JUL-92    Near Final release...??
  45.             Added portability items, cmd line overrides
  46.             ifdef UCHAR, VOID, NOANSI
  47.             Shortened lines to 79 max (got them all?)
  48.             Only thing not done is checking #ifdef NOUCHAR
  49.             and adding any anding off bits which signed
  50.             chars may intruduce in boo().
  51.  
  52. Beta Testing Informaton, Supported Systems Directory:
  53. =====================================================================
  54. ( Testor / Operating System / O.S. Version / Compiler )
  55.  
  56. Rob Weiner, rweiner@watsun.cc.columbia.edu:
  57.     MSDOS        5.0        MSC 5.1
  58.     MSDOS        5.0        GCC (DJGPP DOS 386/G++ 1.05)
  59.     VAX/VMS        5.4-2        VAXC032
  60.     SUNOS        4.1
  61.     UNIXPC        3.51
  62. Christian Hemsing, chris@v750.lfm.rwth-aachen.de:
  63.     OS-9
  64. Stephen Walton, swalton@solaria.csun.edu:
  65.     AMIGA                MANX C (defines MCH_AMIGA)
  66. Bruce J. Moore, moorebj@icd.ab.com:
  67.     AtariST TOS/GEMDOS        MWC 3.7
  68.  
  69. Fun stuff such as my favorite testing shell command is now possible:
  70.     $ for i in *
  71.     do
  72.         echo $i:
  73.         cat $i | msbmkb -q - - | msbpct -q - - | cmp -l - $i
  74.     done
  75.  
  76. This version properly implements the Lasner ~0 fixes.
  77.  
  78. SYNOPSYS: The en-booer writes out printable text from binary text via a 3
  79. input char to 4 output char conversion (called "triple to quad" conversion).
  80. Since the input text can run out before the last triple can be formed, all
  81. en-booers (msbmkb) would add 1 or 2 nulls to the input stream to complete
  82. the triple such that a valid quad can be output.  Thus the problem where
  83. often a de-booer (msbpct) will create an output file from a boo encoded
  84. file, but the output file is larger than the input file by 1 or 2 nulls.
  85. Charles Lasner documented this problem and offered a fix... For each 1 or 2
  86. extra null pad chars added to the input stream, the en-booer should add a
  87. trailing ~0 to the created boo file.  ~X (where X-'0' is a repeat value
  88. which indicates a number of "repeated nulls" does not have a value for the
  89. sequence "~0" which would imply: ``decode into a series of 0 nulls,'' a noop
  90. for "old" debooers.  Hence ~0 can be used as a flag that the input text had
  91. a "padding null" added to it and then the de-booer can know NOT to add these
  92. padding chars to the output stream.  This allows the en-boo/de-boo programs
  93. to finally always guarantee that you get what you started with after passing
  94. through the en-boo then de-boo process.
  95.  
  96. Some bugs/facts with the MSBPCT/MSBMKB programs which popped up
  97. or were discovered recently (January through March 1992):
  98.  -    CURRENT msbpct will NOT make a correct output file from
  99.     the boo file THIS msbmkb creates.  It loses or adds a char.
  100.         Comes from improper implementation of Lasner changes.
  101.         Note: CURRENT enbooer with CURRENT unbooer make the
  102.         same mistakes encoding/uncoding hense files come out
  103.         more or less ok.
  104.  -    OLD msbpct will create a proper output file from a boo
  105.     file created from THIS en-booer.
  106.  -    Current msbpct also screws up output column checking and can
  107.     override the max (usually ~0~0 at eof) and undercut the
  108.     standard value.
  109.  -    Current msbpct doesn't correctly implement lasner fixes.
  110.  -    Current msbpct tells of "using an old booer" at times
  111.     it can determine that that statement is meaningless.
  112.  -    Addtl improper implementation of Lasner change yields
  113.     (quite often) an additional 2 nulls in the output file which
  114.     are removed by an additional 2 ~0 sequence... to break even.
  115.     ie. where old & this enbooer at eof writes "~A", the
  116.     current (bad) booer writes "~C~0~0".
  117. (other items not listed).
  118.  
  119. This program was redone from scratch for portability and implementation
  120. functionality reasons, we also get VMS support here as a bonus.  Also, there
  121. are a few unnecessary things eliminated like adding nulls to the end of
  122. buffers which don't seem to serve any purpose.
  123.  
  124. Character counts for MSDOS ignore the fact that \n is really \r\n, it is
  125. just counting real boo data (in reality the \r can be left out and ignored).
  126. This is done on purpose & is the difference between "data bytes out" and
  127. simply "bytes out".
  128.  
  129. The old enbooer calculated the efficiency of enbooing, well, in reality you
  130. should be calculating the loss as the file grows bigger.  That calc was the
  131. only bit of floating point in the program... so I left it out intentionally.
  132. This program is 100% integer only math now.  Note that sometimes the boo
  133. file is SMALLER than the original, due to lots of null compressions.
  134.  
  135. This new msbmkb replaces the old one (msbmkb's dated before March 1992).
  136. Credit should be given to the maintainers of the old msbmkb:
  137.  
  138.     Original by Bill Catchings, Columbia University, July 1984.
  139.     Modifications by Howie Kaye & Frank da Cruz of Columbia
  140.     University and Christian Hemsing of the Rheinisch-Westphaelish
  141.     Technische Hochschule, Aachen, Germany.
  142. */
  143.  
  144. #include <stdio.h>            /* only header we need */
  145.  
  146. /*
  147.   Version Dependencies... Give each new special case its own defs:
  148. */
  149.  
  150.  
  151. #ifdef VAX11C                /* VAXC032 */
  152. #define SYSTEM        "VAX/VMS"
  153. #define EXIT_GOOD    1
  154. #define EXIT_INFO    3
  155. #define EXIT_BAD    5
  156. #define FOPEN_ROPTS    "rb"
  157. #define FOPEN_WOPTS    "w","rat=cr","rfm=var","mrs=0"
  158. #define CASE_CHANGE    CHANGE_LOWER    /* lowercase boo file name for vms */
  159. #define YES_PROTOS
  160. #endif
  161.  
  162.  
  163. #ifdef MSDOS                /* MSC 5.1 */
  164. #define SYSTEM        "MSDOS"
  165. #define EXIT_GOOD    0
  166. #define EXIT_INFO    1
  167. #define EXIT_BAD    2
  168. #define FOPEN_ROPTS    "rb"
  169. #define FOPEN_WOPTS    "w"
  170. #define CASE_CHANGE    CHANGE_LOWER    /* lowercase boo file name for msdos */
  171. #define YES_PROTOS
  172. #endif
  173.  
  174.  
  175. #ifdef GEMDOS                /* AtariST - TOS - MWC v3.7 */
  176. #define SYSTEM        "AtariST/TOS"
  177. #define EXIT_GOOD    0
  178. #define EXIT_INFO    1
  179. #define EXIT_BAD    2
  180. #define FOPEN_ROPTS    "rb"
  181. #define FOPEN_WOPTS    "w"
  182. #define CASE_CHANGE    CHANGE_LOWER    /* lowercase boo file name */
  183. #define YES_PROTOS
  184. #endif
  185.  
  186.  
  187. #ifdef OSK
  188. #define SYSTEM        "OS-9"
  189. #define EXIT_GOOD    0
  190. #define EXIT_INFO    1
  191. #define EXIT_BAD    1
  192. #define FOPEN_ROPTS    "r"
  193. #define FOPEN_WOPTS    "w"
  194. #define CASE_CHANGE    CHANGE_NONE    /* leave filename case sensitive */
  195. /*
  196. #undef  YES_PROTOS                      * default OS9 to noprotos *
  197. */
  198. #endif
  199.  
  200. #ifndef FOPEN_ROPTS            /* No system found, default to unix */
  201. #define SYSTEM        "UNIX/Amiga/Generic"
  202. #define EXIT_GOOD    0
  203. #define EXIT_INFO    1
  204. #define EXIT_BAD    2
  205. #define FOPEN_ROPTS    "r"
  206. #define FOPEN_WOPTS    "w"
  207. #define CASE_CHANGE    CHANGE_NONE    /* leave filename case sensitive */
  208. /*
  209. #undef  YES_PROTOS                      * default UNIX/generic to noprotos *
  210. */
  211. #endif
  212.  
  213. #ifndef NOANSI                /* allow cmd line override to STDC */
  214. #ifdef __STDC__                /* Ansi likes prototypes */
  215. #if __STDC__                /* MWC sets this defined but 0 valued */
  216. #define YES_PROTOS
  217. #endif
  218. #endif /* __STDC__ */
  219. #endif /* NOANSI */
  220.  
  221. #ifndef VOID                /* allow cmd line override to VOID */
  222. #define VOID void            /* assume system likes void */
  223. #endif
  224.  
  225. #ifndef _CDECL
  226. #define _CDECL
  227. #endif
  228.  
  229. #ifndef __DATE__
  230. #define __DATE__ "01-MAY-1992"
  231. #endif
  232.  
  233. #ifndef __TIME__
  234. #define __TIME__ "00:00:00"
  235. #endif
  236.  
  237. /*
  238.   BOO Encoder Options
  239. */
  240. #define MAXOUTLEN        72    /* max output chars per line */
  241. #define MAXNULLCOMP        78    /* max null compression via ~ */
  242. #define MINNULLCOMP        2    /* min of 2 nulls to compress */
  243.  
  244. #define tochar(c)    ( (c) + '0' )
  245.  
  246. #define CHANGE_NONE    1
  247. #define CHANGE_UPPER    2
  248. #define CHANGE_LOWER    3
  249.  
  250. /*
  251.   Typedefs
  252. */
  253. #ifndef UCHAR                /* allow cmd line override */
  254. typedef unsigned char uchar;        /* possible portability concern */
  255. #define UCHAR    uchar
  256. #else
  257. #define NOUCHAR        1        /* flag saying cmd line changed uchar */
  258. #endif
  259.  
  260. /*
  261.   Here are the function prototypes...
  262.   If your 'C' don't like prototypes, don't declare YES_PROTOS.
  263. */
  264. #ifdef YES_PROTOS
  265. VOID _CDECL convert    (FILE *, FILE *);
  266. int  _CDECL get3       (FILE *, UCHAR *);
  267. VOID _CDECL output     (FILE *, UCHAR *, int);
  268. VOID _CDECL writechars (FILE *, char *, int);
  269. VOID _CDECL writexstr  (FILE *, char *, int);
  270. VOID _CDECL boo        (UCHAR *, UCHAR *);
  271. VOID _CDECL change_case(char *, int);
  272. VOID _CDECL usage      (VOID);
  273. #else
  274. VOID convert    ();
  275. int  get3       ();
  276. VOID output     ();
  277. VOID writechars ();
  278. VOID writexstr  ();
  279. VOID boo        ();
  280. VOID change_case();
  281. VOID usage();
  282. #endif
  283.  
  284. long count_in=0, count_out=0;        /* character counts */
  285. int quiet=0;
  286.  
  287. main(argc,argv)
  288. int argc;
  289. char **argv;
  290. {
  291.     FILE *fpin, *fpout;
  292.     char *booptr;
  293.     int force_case=0;
  294.     int leave_path=0;
  295.  
  296.     while( argc > 1 && *argv[1]=='-' )
  297.         {
  298.         if( argv[1][1] == '\0' )
  299.             break;
  300.         switch( argv[1][1] )
  301.             {
  302.             case 'v':        /* version */
  303.                 fprintf(stderr,
  304.                 "MSBMKB.C, Date=\"%s, %s\", System=\"%s\"\n",
  305.                     __DATE__,__TIME__,SYSTEM);
  306.                 fprintf(stderr, "\
  307. Email comments to \"rweiner@kermit.columbia.edu\" \
  308. (Rob Weiner/Programming Plus)\
  309. \n");
  310.                 fprintf(stderr,"\n");
  311.                 break;
  312.             case 'l':        /* lowercase internal name */
  313.                 force_case = CHANGE_LOWER ;
  314.                 if( !quiet )
  315.                     fprintf(stderr,
  316.                     "Forcing Lowercased Internal Name\n");
  317.                 break;
  318.             case 'u':        /* uppercase internal name */
  319.                 force_case = CHANGE_UPPER ;
  320.                 if( !quiet )
  321.                     fprintf(stderr,
  322.                     "Forcing Uppercased Internal Name\n");
  323.                 break;
  324.             case 'p':            /* leave paths */
  325.                 leave_path=1;
  326.                 break;
  327.             case 'q':            /* quiet */
  328.                 quiet=1;
  329.                 break;
  330.             default:
  331.                 usage();
  332.             }
  333.         argc--;
  334.         argv++;
  335.         }
  336.  
  337.     if( argc < 3 || argc > 4 )
  338.         usage();
  339.  
  340.     if( argv[1][0]=='-' && argv[1][1]=='\0' )
  341.         {
  342.         fpin = stdin;
  343.         }
  344.     else if( (fpin = fopen( argv[1] , FOPEN_ROPTS )) == NULL )
  345.         {
  346.         fprintf(stderr,"Error, cannot open input file \"%s\"\n",
  347.             argv[1]);
  348.         exit(EXIT_BAD);
  349.         }
  350.  
  351.     if( argv[2][0]=='-' && argv[2][1]=='\0' )
  352.         {
  353.         fpout = stdout;
  354.         }
  355.     else if( (fpout = fopen( argv[2] , FOPEN_WOPTS )) == NULL )
  356.         {
  357.         fprintf(stderr,"Error, cannot open output file \"%s\"\n",
  358.             argv[2]);
  359.         exit(EXIT_BAD);
  360.         }
  361.  
  362.     if( !quiet )
  363.         fprintf(stderr,
  364.                "Creating BOO File \"%s\" from Binary File \"%s\"...\n",
  365.                 argv[2],argv[1]);
  366.  
  367.     booptr = argv[1] ;    /* input file name */
  368.     if( argc > 3 )        /* command line override internal name */
  369.         {
  370.         booptr = argv[3];
  371.         if( !quiet )
  372.             fprintf(stderr,
  373.     "Command Line Argument \"%s\" Overrides Internal BOO File Name\n",
  374.                 booptr);
  375.         }
  376.     else if( !leave_path )
  377.         {    /* strip path regexpr ".*[/\\\]:>]" from booptr */
  378.         char *s, *t;
  379.         for( s = t = booptr ; *s ; s++ )
  380.             {
  381.             if(*s=='/' || *s=='\\' || *s==']' ||
  382.                *s==':' || *s=='>')
  383.                 t = s + 1 ;
  384.             }
  385.         if( *t == '\0' )
  386.             t = "_";
  387.         if( t != booptr )
  388.             {
  389.             if( !quiet )
  390.                 fprintf(stderr,
  391.             "Internal BOO File Name Without Path = \"%s\"\n",t);
  392.             }
  393.         booptr = t ;
  394.         }
  395.  
  396.     if( force_case == 0 )
  397.         force_case = CASE_CHANGE ;
  398.  
  399.     /* first line in output file is filename */
  400.     writexstr( fpout, booptr, force_case );
  401.  
  402.     convert(fpin,fpout);
  403.  
  404.     writechars(fpout,"",0);        /* flush output buffering */
  405.  
  406.     fclose(fpin);
  407.     fclose(fpout);
  408.  
  409.     if( !quiet )
  410.         {
  411.         fprintf(stderr,"Data bytes in: %ld,  ",  count_in);
  412.         fprintf(stderr,"Data bytes out: %ld,  ", count_out);
  413.         fprintf(stderr,"Difference: %ld bytes\n",
  414.                 count_out - count_in);
  415.         }
  416.     exit(EXIT_GOOD);
  417. }
  418.  
  419.  
  420. VOID usage()
  421. {
  422.     fprintf(stderr, "MSBMKB = Encode Binary File into Ascii BOO Format\n");
  423.     fprintf(stderr, "\
  424. Usage: MSBMKB [-v -l -u -p -q] input_file output_boo_file [internal_boo_name]\
  425. \n");
  426.     fprintf(stderr,
  427. "              -v = show version information\n");
  428.     fprintf(stderr,
  429. "              -l = lowercase internal BOO file name\n");
  430.     fprintf(stderr,
  431. "              -u = uppercase internal BOO file name\n");
  432.     fprintf(stderr,
  433. "              -p = leave internal BOO path intact\n");
  434.     fprintf(stderr,
  435. "              -q = quiet mode\n");
  436.     fprintf(stderr,
  437. "              Note: Filenames of '-' are supported for stdin & stdout\n");
  438.     exit(EXIT_INFO);
  439. }
  440.  
  441. VOID convert(fpin,fpout)        /* convert each 3 chars to 4 */
  442. FILE *fpin, *fpout;
  443. {
  444.     int n;
  445.     int fill_nulls = 0;
  446.     UCHAR inbuf[10], outbuf[10];
  447.  
  448.     while( (n = get3(fpin,inbuf)) != 0 )
  449.         {
  450.         if( n < 0 )        /* bunch of nulls */
  451.             {
  452.             outbuf[0] = '~' ;
  453.             outbuf[1] = tochar( -n );
  454.  
  455.             output(fpout,outbuf,2);
  456.             }
  457.         else    {
  458.             while( n < 3 )
  459.                 {
  460.                 inbuf[n++] = '\0' ;
  461.                 fill_nulls++ ;
  462.                 }
  463.  
  464.             boo( inbuf , outbuf );
  465.  
  466.             output(fpout,outbuf,4);
  467.             }
  468.         }
  469.  
  470.     if( fill_nulls > 0 )
  471.         {
  472.         if( !quiet )
  473.             fprintf(stderr,"Fill Nulls = %d\n",fill_nulls);
  474.  
  475. /*        strcpy( outbuf , "~0" );    */
  476.         outbuf[0] = '~' ;        /* redone w/o strcpy... */
  477.         outbuf[1] = '0' ;
  478.         outbuf[2] = '\0' ;
  479.  
  480.         while( fill_nulls-- > 0 )
  481.             {
  482.             output(fpout,outbuf,2);
  483.             }
  484.         }
  485.     output(fpout, (UCHAR *)"", -1);    /* make sure last line is \n termed */
  486. }
  487.  
  488. int get3( fp , buf )    /* return: pos=# read, neg=# nulls found */
  489. FILE *fp;
  490. UCHAR *buf;
  491. {
  492.     int i=0;        /* amt last read */
  493.     int nulls=0;        /* amt nulls found */
  494.     int c;
  495.  
  496.     do    {
  497.         if( (c = getc(fp)) == EOF )        /* hit eof */
  498.             {
  499.             if( ferror(fp) )        /* quick check */
  500.                 {
  501.                 fprintf(stderr,
  502.                     "get3(): fread error on input file\n");
  503.                 exit(EXIT_BAD);
  504.                 }
  505.             break;                /* stop */
  506.             }
  507.         count_in++;
  508.  
  509.         if( (nulls > 0) && (c != '\0') )    /* stop collecting */
  510.             {
  511.             if( nulls < MINNULLCOMP )
  512.                 {        /* correct for too few nulls */
  513.                 i = nulls + 1 ;        /* nulls + new char */
  514.                 while( nulls-- > 0 )    /* restore null data */
  515.                     *buf++ = '\0' ;
  516.                 *buf++ = c ;        /* store curr char */
  517.                 }
  518.             else    {
  519.                 ungetc(c,fp);        /* save non-null */
  520.                 count_in--;
  521.                 break;
  522.                 }
  523.             }
  524.         else if( (i == 0) && (c == '\0') )    /* collect */
  525.             {
  526.             nulls++ ;            /* keep collecting */
  527.             }
  528.         else    {
  529.             i++;                /* count till 3 */
  530.             *buf++ = c ;            /* save chars */
  531.             }
  532.  
  533.         } while( (i <= 2) && (nulls <= MAXNULLCOMP) );
  534.  
  535.     if( nulls > MAXNULLCOMP )
  536.         {
  537.         ungetc(c,fp);        /* save the 79th null for next time */
  538.         nulls--;
  539.         count_in--;
  540.         }
  541.  
  542.     if( nulls > 0 )
  543.         return( -nulls );
  544.  
  545.     return(i);
  546. }
  547.  
  548.  
  549. VOID output(fp,buf,n)    /* output chars taking care of line wraps */
  550. FILE *fp;        /* we are keeping output quads on the same line */
  551. UCHAR *buf;
  552. int n;        /* -1 is flag to end last line with \n if its not already */
  553. {
  554.     static outlen=0;
  555.  
  556.     if( ((n < 0) && (outlen != 0)) || ((outlen+n) > MAXOUTLEN) )
  557.         {
  558.         writechars(fp,"\n",1);
  559.         outlen=0;
  560.         }
  561.  
  562.     if( n > 0 )
  563.         {
  564.         outlen += n;
  565.         writechars(fp,(char *)buf,n);
  566.         }
  567. }
  568.  
  569. VOID writechars( fp, s, n )            /* n==0 = flush */
  570. char *s;
  571. int n;
  572. FILE *fp;
  573. {
  574.     static char buf[BUFSIZ];
  575.     static char *p=buf;
  576.     int flush = (n==0) ;
  577.     unsigned count;
  578.  
  579.     if( (p+n) >= (buf+sizeof(buf)) )
  580.         {
  581.         fprintf(stderr,
  582.             "writechars: error would exceed output buffer\n");
  583.         exit(EXIT_BAD);
  584.         }
  585.  
  586.     while( n-- > 0 )
  587.         *p++ = *s++ ;
  588.  
  589.     /* we know there is a \n at the end of the ~73 char lines! */
  590.  
  591.     if( flush || (p[-1] == '\n') )        /* time to dump buffer */
  592.         {
  593.         if( (count = p-buf) != 0 )
  594.             {
  595.             /* this must be "p-buf,1" ordered here for VMS
  596.                varying recs to come out right */
  597.             count_out += count ;
  598. #if 0                        /* Ignore the nl's for now */
  599. #if MSDOS
  600.             if( !flush )        /* MSDOS does \r\n not \n */
  601.                 count_out++ ;
  602. #endif
  603. #endif
  604.             if( fwrite( buf , count , 1 , fp ) != 1 )
  605.                 {
  606.                 fprintf(stderr,
  607.                 "writechars(): fwrite error on output file\n");
  608.                 exit(EXIT_BAD);
  609.                 }
  610.             p = buf ;
  611.             }
  612.         if( flush )
  613.             fflush(fp);
  614.         }
  615. }
  616.  
  617. VOID writexstr(fp,s,t)            /* write uppercased string */
  618. FILE *fp;
  619. char *s;
  620. int t;                    /* type of case change */
  621. {
  622.     int i;
  623.     char buf[BUFSIZ], *p;
  624.  
  625. /*
  626.     i=strlen(s);
  627.     if( i > BUFSIZ )        * make sure name is sane length *
  628.         i = BUFSIZ ;
  629.     s[i] = '\0' ;
  630.     strcpy(buf,s);
  631. */
  632.                     /* redone w/o strlen & strcpy... */
  633.     i = BUFSIZ - 1;            /* watch buffer overrun */
  634.     p = buf;
  635.     while( (i-- > 0) && ((*p = *s++) != '\0') )
  636.         p++ ;
  637.     *p = '\0' ;            /* make sure there's a null */
  638.     i = p-buf;            /* strlen */
  639.  
  640.     change_case(buf,t);        /* change case as appropriate */
  641.     writechars(fp,buf,i);
  642.     writechars(fp,"\n",1);
  643. }
  644.  
  645.  
  646. VOID boo( inbuf , outbuf )    /* here is where we boo 3 into 4 chars */
  647. UCHAR *inbuf, *outbuf;
  648. {
  649.     UCHAR x,y,z,a,b,c,d;
  650.  
  651.     /* get x,y,z the 3 input bytes */
  652.  
  653.     x = *inbuf++;
  654.     y = *inbuf++;
  655.     z = *inbuf;
  656.  
  657.     /* generate a,b,c,d the 4 output bytes */
  658.  
  659.     a = x >> 2 ;
  660.     b = ( (x << 4) | (y >> 4) ) & 077 ;
  661.     c = ( (y << 2) | (z >> 6) ) & 077 ;
  662.     d = z & 077 ;
  663.  
  664.     *outbuf++ = tochar(a);
  665.     *outbuf++ = tochar(b);
  666.     *outbuf++ = tochar(c);
  667.     *outbuf   = tochar(d);
  668. }
  669.  
  670.  
  671. VOID change_case(s,t)
  672. char *s;
  673. int t;
  674. {
  675.     if( t != CHANGE_UPPER && t != CHANGE_LOWER && t != CHANGE_NONE )
  676.         {
  677.         fprintf(stderr,"Error, bad case change type\n");
  678.         exit(EXIT_BAD);
  679.         }
  680.  
  681.     while( *s )
  682.         {
  683.         if( ((t==CHANGE_UPPER) && ( (*s >= 'a') && (*s <= 'z') )) ||
  684.             ((t==CHANGE_LOWER) && ( (*s >= 'A') && (*s <= 'Z') )) )
  685.             *s ^= 040;
  686.         s++;
  687.         }
  688. }
  689.  
  690. /*
  691.   [EOF]
  692. */
  693.  
  694.