Change functions to ANSI C.
[openssl.git] / crypto / des / des.c
1 /* crypto/des/des.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  * 
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  * 
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  * 
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from 
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  * 
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * 
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #ifndef MSDOS
62 #include <unistd.h>
63 #else
64 #include <io.h>
65 #define RAND
66 #endif
67
68 #include <time.h>
69 #include "des_ver.h"
70
71 #ifdef VMS
72 #include <types.h>
73 #include <stat.h>
74 #else
75 #ifndef _IRIX
76 #include <sys/types.h>
77 #endif
78 #include <sys/stat.h>
79 #endif
80 #if defined(NOCONST)
81 #define const
82 #endif
83 #include "des.h"
84
85 #if defined(__STDC__) || defined(VMS) || defined(M_XENIX) || defined(MSDOS)
86 #include <string.h>
87 #endif
88
89 #ifdef RAND
90 #define random rand
91 #define srandom(s) srand(s)
92 #endif
93
94 #ifndef NOPROTO
95 void usage(void);
96 void doencryption(void);
97 int uufwrite(unsigned char *data, int size, unsigned int num, FILE *fp);
98 void uufwriteEnd(FILE *fp);
99 int uufread(unsigned char *out,int size,unsigned int num,FILE *fp);
100 int uuencode(unsigned char *in,int num,unsigned char *out);
101 int uudecode(unsigned char *in,int num,unsigned char *out);
102 void des_3cbc_encrypt(des_cblock *input,des_cblock *output,long length,
103         des_key_schedule sk1,des_key_schedule sk2,
104         des_cblock *ivec1,des_cblock *ivec2,int enc);
105 #else
106 void usage();
107 void doencryption();
108 int uufwrite();
109 void uufwriteEnd();
110 int uufread();
111 int uuencode();
112 int uudecode();
113 void des_3cbc_encrypt();
114 #endif
115
116 #ifdef VMS
117 #define EXIT(a) exit(a&0x10000000L)
118 #else
119 #define EXIT(a) exit(a)
120 #endif
121
122 #define BUFSIZE (8*1024)
123 #define VERIFY  1
124 #define KEYSIZ  8
125 #define KEYSIZB 1024 /* should hit tty line limit first :-) */
126 char key[KEYSIZB+1];
127 int do_encrypt,longk=0;
128 FILE *DES_IN,*DES_OUT,*CKSUM_OUT;
129 char uuname[200];
130 unsigned char uubuf[50];
131 int uubufnum=0;
132 #define INUUBUFN        (45*100)
133 #define OUTUUBUF        (65*100)
134 unsigned char b[OUTUUBUF];
135 unsigned char bb[300];
136 des_cblock cksum={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
137 char cksumname[200]="";
138
139 int vflag,cflag,eflag,dflag,kflag,bflag,fflag,sflag,uflag,flag3,hflag,error;
140
141 int main(int argc, char **argv)
142         {
143         int i;
144         struct stat ins,outs;
145         char *p;
146         char *in=NULL,*out=NULL;
147
148         vflag=cflag=eflag=dflag=kflag=hflag=bflag=fflag=sflag=uflag=flag3=0;
149         error=0;
150         memset(key,0,sizeof(key));
151
152         for (i=1; i<argc; i++)
153                 {
154                 p=argv[i];
155                 if ((p[0] == '-') && (p[1] != '\0'))
156                         {
157                         p++;
158                         while (*p)
159                                 {
160                                 switch (*(p++))
161                                         {
162                                 case '3':
163                                         flag3=1;
164                                         longk=1;
165                                         break;
166                                 case 'c':
167                                         cflag=1;
168                                         strncpy(cksumname,p,200);
169                                         p+=strlen(cksumname);
170                                         break;
171                                 case 'C':
172                                         cflag=1;
173                                         longk=1;
174                                         strncpy(cksumname,p,200);
175                                         p+=strlen(cksumname);
176                                         break;
177                                 case 'e':
178                                         eflag=1;
179                                         break;
180                                 case 'v':
181                                         vflag=1;
182                                         break;
183                                 case 'E':
184                                         eflag=1;
185                                         longk=1;
186                                         break;
187                                 case 'd':
188                                         dflag=1;
189                                         break;
190                                 case 'D':
191                                         dflag=1;
192                                         longk=1;
193                                         break;
194                                 case 'b':
195                                         bflag=1;
196                                         break;
197                                 case 'f':
198                                         fflag=1;
199                                         break;
200                                 case 's':
201                                         sflag=1;
202                                         break;
203                                 case 'u':
204                                         uflag=1;
205                                         strncpy(uuname,p,200);
206                                         p+=strlen(uuname);
207                                         break;
208                                 case 'h':
209                                         hflag=1;
210                                         break;
211                                 case 'k':
212                                         kflag=1;
213                                         if ((i+1) == argc)
214                                                 {
215                                                 fputs("must have a key with the -k option\n",stderr);
216                                                 error=1;
217                                                 }
218                                         else
219                                                 {
220                                                 int j;
221
222                                                 i++;
223                                                 strncpy(key,argv[i],KEYSIZB);
224                                                 for (j=strlen(argv[i])-1; j>=0; j--)
225                                                         argv[i][j]='\0';
226                                                 }
227                                         break;
228                                 default:
229                                         fprintf(stderr,"'%c' unknown flag\n",p[-1]);
230                                         error=1;
231                                         break;
232                                         }
233                                 }
234                         }
235                 else
236                         {
237                         if (in == NULL)
238                                 in=argv[i];
239                         else if (out == NULL)
240                                 out=argv[i];
241                         else
242                                 error=1;
243                         }
244                 }
245         if (error) usage();
246         /* We either
247          * do checksum or
248          * do encrypt or
249          * do decrypt or
250          * do decrypt then ckecksum or
251          * do checksum then encrypt
252          */
253         if (((eflag+dflag) == 1) || cflag)
254                 {
255                 if (eflag) do_encrypt=DES_ENCRYPT;
256                 if (dflag) do_encrypt=DES_DECRYPT;
257                 }
258         else
259                 {
260                 if (vflag) 
261                         {
262 #ifndef _Windows                        
263                         fprintf(stderr,"des(1) built with %s\n",libdes_version);
264 #endif                  
265                         EXIT(1);
266                         }
267                 else usage();
268                 }
269
270 #ifndef _Windows                        
271         if (vflag) fprintf(stderr,"des(1) built with %s\n",libdes_version);
272 #endif                  
273         if (    (in != NULL) &&
274                 (out != NULL) &&
275 #ifndef MSDOS
276                 (stat(in,&ins) != -1) &&
277                 (stat(out,&outs) != -1) &&
278                 (ins.st_dev == outs.st_dev) &&
279                 (ins.st_ino == outs.st_ino))
280 #else /* MSDOS */
281                 (strcmp(in,out) == 0))
282 #endif
283                         {
284                         fputs("input and output file are the same\n",stderr);
285                         EXIT(3);
286                         }
287
288         if (!kflag)
289                 if (des_read_pw_string(key,KEYSIZB+1,"Enter key:",eflag?VERIFY:0))
290                         {
291                         fputs("password error\n",stderr);
292                         EXIT(2);
293                         }
294
295         if (in == NULL)
296                 DES_IN=stdin;
297         else if ((DES_IN=fopen(in,"r")) == NULL)
298                 {
299                 perror("opening input file");
300                 EXIT(4);
301                 }
302
303         CKSUM_OUT=stdout;
304         if (out == NULL)
305                 {
306                 DES_OUT=stdout;
307                 CKSUM_OUT=stderr;
308                 }
309         else if ((DES_OUT=fopen(out,"w")) == NULL)
310                 {
311                 perror("opening output file");
312                 EXIT(5);
313                 }
314
315 #ifdef MSDOS
316         /* This should set the file to binary mode. */
317         {
318 #include <fcntl.h>
319         if (!(uflag && dflag))
320                 setmode(fileno(DES_IN),O_BINARY);
321         if (!(uflag && eflag))
322                 setmode(fileno(DES_OUT),O_BINARY);
323         }
324 #endif
325
326         doencryption();
327         fclose(DES_IN);
328         fclose(DES_OUT);
329         EXIT(0);
330         }
331
332 void usage(void)
333         {
334         char **u;
335         static const char *Usage[]={
336 "des <options> [input-file [output-file]]",
337 "options:",
338 "-v         : des(1) version number",
339 "-e         : encrypt using sunOS compatible user key to DES key conversion.",
340 "-E         : encrypt ",
341 "-d         : decrypt using sunOS compatible user key to DES key conversion.",
342 "-D         : decrypt ",
343 "-c[ckname] : generate a cbc_cksum using sunOS compatible user key to",
344 "             DES key conversion and output to ckname (stdout default,",
345 "             stderr if data being output on stdout).  The checksum is",
346 "             generated before encryption and after decryption if used",
347 "             in conjunction with -[eEdD].",
348 "-C[ckname] : generate a cbc_cksum as for -c but compatible with -[ED].",
349 "-k key     : use key 'key'",
350 "-h         : the key that is entered will be a hexidecimal number",
351 "             that is used directly as the des key",
352 "-u[uuname] : input file is uudecoded if -[dD] or output uuencoded data if -[eE]",
353 "             (uuname is the filename to put in the uuencode header).",
354 "-b         : encrypt using DES in ecb encryption mode, the defaut is cbc mode.",
355 "-3         : encrypt using tripple DES encryption.  This uses 2 keys",
356 "             generated from the input key.  If the input key is less",
357 "             than 8 characters long, this is equivelent to normal",
358 "             encryption.  Default is triple cbc, -b makes it triple ecb.",
359 NULL
360 };
361         for (u=(char **)Usage; *u; u++)
362                 {
363                 fputs(*u,stderr);
364                 fputc('\n',stderr);
365                 }
366
367         EXIT(1);
368         }
369
370 void doencryption(void)
371         {
372 #ifdef _LIBC
373         extern int srandom();
374         extern int random();
375         extern unsigned long time();
376 #endif
377
378         register int i;
379         des_key_schedule ks,ks2;
380         unsigned char iv[8],iv2[8];
381         char *p;
382         int num=0,j,k,l,rem,ll,len,last,ex=0;
383         des_cblock kk,k2;
384         FILE *O;
385         int Exit=0;
386 #ifndef MSDOS
387         static unsigned char buf[BUFSIZE+8],obuf[BUFSIZE+8];
388 #else
389         static unsigned char *buf=NULL,*obuf=NULL;
390
391         if (buf == NULL)
392                 {
393                 if (    (( buf=(unsigned char *)Malloc(BUFSIZE+8)) == NULL) ||
394                         ((obuf=(unsigned char *)Malloc(BUFSIZE+8)) == NULL))
395                         {
396                         fputs("Not enough memory\n",stderr);
397                         Exit=10;
398                         goto problems;
399                         }
400                 }
401 #endif
402
403         if (hflag)
404                 {
405                 j=(flag3?16:8);
406                 p=key;
407                 for (i=0; i<j; i++)
408                         {
409                         k=0;
410                         if ((*p <= '9') && (*p >= '0'))
411                                 k=(*p-'0')<<4;
412                         else if ((*p <= 'f') && (*p >= 'a'))
413                                 k=(*p-'a'+10)<<4;
414                         else if ((*p <= 'F') && (*p >= 'A'))
415                                 k=(*p-'A'+10)<<4;
416                         else
417                                 {
418                                 fputs("Bad hex key\n",stderr);
419                                 Exit=9;
420                                 goto problems;
421                                 }
422                         p++;
423                         if ((*p <= '9') && (*p >= '0'))
424                                 k|=(*p-'0');
425                         else if ((*p <= 'f') && (*p >= 'a'))
426                                 k|=(*p-'a'+10);
427                         else if ((*p <= 'F') && (*p >= 'A'))
428                                 k|=(*p-'A'+10);
429                         else
430                                 {
431                                 fputs("Bad hex key\n",stderr);
432                                 Exit=9;
433                                 goto problems;
434                                 }
435                         p++;
436                         if (i < 8)
437                                 kk[i]=k;
438                         else
439                                 k2[i-8]=k;
440                         }
441                 des_set_key((C_Block *)k2,ks2);
442                 memset(k2,0,sizeof(k2));
443                 }
444         else if (longk || flag3)
445                 {
446                 if (flag3)
447                         {
448                         des_string_to_2keys(key,(C_Block *)kk,(C_Block *)k2);
449                         des_set_key((C_Block *)k2,ks2);
450                         memset(k2,0,sizeof(k2));
451                         }
452                 else
453                         des_string_to_key(key,(C_Block *)kk);
454                 }
455         else
456                 for (i=0; i<KEYSIZ; i++)
457                         {
458                         l=0;
459                         k=key[i];
460                         for (j=0; j<8; j++)
461                                 {
462                                 if (k&1) l++;
463                                 k>>=1;
464                                 }
465                         if (l & 1)
466                                 kk[i]=key[i]&0x7f;
467                         else
468                                 kk[i]=key[i]|0x80;
469                         }
470
471         des_set_key((C_Block *)kk,ks);
472         memset(key,0,sizeof(key));
473         memset(kk,0,sizeof(kk));
474         /* woops - A bug that does not showup under unix :-( */
475         memset(iv,0,sizeof(iv));
476         memset(iv2,0,sizeof(iv2));
477
478         l=1;
479         rem=0;
480         /* first read */
481         if (eflag || (!dflag && cflag))
482                 {
483                 for (;;)
484                         {
485                         num=l=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
486                         l+=rem;
487                         num+=rem;
488                         if (l < 0)
489                                 {
490                                 perror("read error");
491                                 Exit=6;
492                                 goto problems;
493                                 }
494
495                         rem=l%8;
496                         len=l-rem;
497                         if (feof(DES_IN))
498                                 {
499                                 srandom((unsigned int)time(NULL));
500                                 for (i=7-rem; i>0; i--)
501                                         buf[l++]=random()&0xff;
502                                 buf[l++]=rem;
503                                 ex=1;
504                                 len+=rem;
505                                 }
506                         else
507                                 l-=rem;
508
509                         if (cflag)
510                                 {
511                                 des_cbc_cksum((C_Block *)buf,(C_Block *)cksum,
512                                         (long)len,ks,(C_Block *)cksum);
513                                 if (!eflag)
514                                         {
515                                         if (feof(DES_IN)) break;
516                                         else continue;
517                                         }
518                                 }
519
520                         if (bflag && !flag3)
521                                 for (i=0; i<l; i+=8)
522                                         des_ecb_encrypt(
523                                                 (des_cblock *)&(buf[i]),
524                                                 (des_cblock *)&(obuf[i]),
525                                                 ks,do_encrypt);
526                         else if (flag3 && bflag)
527                                 for (i=0; i<l; i+=8)
528                                         des_ecb2_encrypt(
529                                                 (des_cblock *)&(buf[i]),
530                                                 (des_cblock *)&(obuf[i]),
531                                                 ks,ks2,do_encrypt);
532                         else if (flag3 && !bflag)
533                                 {
534                                 char tmpbuf[8];
535
536                                 if (rem) memcpy(tmpbuf,&(buf[l]),
537                                         (unsigned int)rem);
538                                 des_3cbc_encrypt(
539                                         (des_cblock *)buf,(des_cblock *)obuf,
540                                         (long)l,ks,ks2,(des_cblock *)iv,
541                                         (des_cblock *)iv2,do_encrypt);
542                                 if (rem) memcpy(&(buf[l]),tmpbuf,
543                                         (unsigned int)rem);
544                                 }
545                         else
546                                 {
547                                 des_cbc_encrypt(
548                                         (des_cblock *)buf,(des_cblock *)obuf,
549                                         (long)l,ks,(des_cblock *)iv,do_encrypt);
550                                 if (l >= 8) memcpy(iv,&(obuf[l-8]),8);
551                                 }
552                         if (rem) memcpy(buf,&(buf[l]),(unsigned int)rem);
553
554                         i=0;
555                         while (i < l)
556                                 {
557                                 if (uflag)
558                                         j=uufwrite(obuf,1,(unsigned int)l-i,
559                                                 DES_OUT);
560                                 else
561                                         j=fwrite(obuf,1,(unsigned int)l-i,
562                                                 DES_OUT);
563                                 if (j == -1)
564                                         {
565                                         perror("Write error");
566                                         Exit=7;
567                                         goto problems;
568                                         }
569                                 i+=j;
570                                 }
571                         if (feof(DES_IN))
572                                 {
573                                 if (uflag) uufwriteEnd(DES_OUT);
574                                 break;
575                                 }
576                         }
577                 }
578         else /* decrypt */
579                 {
580                 ex=1;
581                 for (;;)
582                         {
583                         if (ex) {
584                                 if (uflag)
585                                         l=uufread(buf,1,BUFSIZE,DES_IN);
586                                 else
587                                         l=fread(buf,1,BUFSIZE,DES_IN);
588                                 ex=0;
589                                 rem=l%8;
590                                 l-=rem;
591                                 }
592                         if (l < 0)
593                                 {
594                                 perror("read error");
595                                 Exit=6;
596                                 goto problems;
597                                 }
598
599                         if (bflag && !flag3)
600                                 for (i=0; i<l; i+=8)
601                                         des_ecb_encrypt(
602                                                 (des_cblock *)&(buf[i]),
603                                                 (des_cblock *)&(obuf[i]),
604                                                 ks,do_encrypt);
605                         else if (flag3 && bflag)
606                                 for (i=0; i<l; i+=8)
607                                         des_ecb2_encrypt(
608                                                 (des_cblock *)&(buf[i]),
609                                                 (des_cblock *)&(obuf[i]),
610                                                 ks,ks2,do_encrypt);
611                         else if (flag3 && !bflag)
612                                 {
613                                 des_3cbc_encrypt(
614                                         (des_cblock *)buf,(des_cblock *)obuf,
615                                         (long)l,ks,ks2,(des_cblock *)iv,
616                                         (des_cblock *)iv2,do_encrypt);
617                                 }
618                         else
619                                 {
620                                 des_cbc_encrypt(
621                                         (des_cblock *)buf,(des_cblock *)obuf,
622                                         (long)l,ks,(des_cblock *)iv,do_encrypt);
623                                 if (l >= 8) memcpy(iv,&(buf[l-8]),8);
624                                 }
625
626                         if (uflag)
627                                 ll=uufread(&(buf[rem]),1,BUFSIZE,DES_IN);
628                         else
629                                 ll=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
630                         ll+=rem;
631                         rem=ll%8;
632                         ll-=rem;
633                         if (feof(DES_IN) && (ll == 0))
634                                 {
635                                 last=obuf[l-1];
636
637                                 if ((last > 7) || (last < 0))
638                                         {
639                                         fputs("The file was not decrypted correctly.\n",
640                                                 stderr);
641                                         Exit=8;
642                                         last=0;
643                                         }
644                                 l=l-8+last;
645                                 }
646                         i=0;
647                         if (cflag) des_cbc_cksum((C_Block *)obuf,
648                                 (C_Block *)cksum,(long)l/8*8,ks,
649                                 (C_Block *)cksum);
650                         while (i != l)
651                                 {
652                                 j=fwrite(obuf,1,(unsigned int)l-i,DES_OUT);
653                                 if (j == -1)
654                                         {
655                                         perror("Write error");
656                                         Exit=7;
657                                         goto problems;
658                                         }
659                                 i+=j;
660                                 }
661                         l=ll;
662                         if ((l == 0) && feof(DES_IN)) break;
663                         }
664                 }
665         if (cflag)
666                 {
667                 l=0;
668                 if (cksumname[0] != '\0')
669                         {
670                         if ((O=fopen(cksumname,"w")) != NULL)
671                                 {
672                                 CKSUM_OUT=O;
673                                 l=1;
674                                 }
675                         }
676                 for (i=0; i<8; i++)
677                         fprintf(CKSUM_OUT,"%02X",cksum[i]);
678                 fprintf(CKSUM_OUT,"\n");
679                 if (l) fclose(CKSUM_OUT);
680                 }
681 problems:
682         memset(buf,0,sizeof(buf));
683         memset(obuf,0,sizeof(obuf));
684         memset(ks,0,sizeof(ks));
685         memset(ks2,0,sizeof(ks2));
686         memset(iv,0,sizeof(iv));
687         memset(iv2,0,sizeof(iv2));
688         memset(kk,0,sizeof(kk));
689         memset(k2,0,sizeof(k2));
690         memset(uubuf,0,sizeof(uubuf));
691         memset(b,0,sizeof(b));
692         memset(bb,0,sizeof(bb));
693         memset(cksum,0,sizeof(cksum));
694         if (Exit) EXIT(Exit);
695         }
696
697 /*    We ignore this parameter but it should be > ~50 I believe    */
698 int uufwrite(unsigned char *data, int size, unsigned int num, FILE *fp)
699         {
700         int i,j,left,rem,ret=num;
701         static int start=1;
702
703         if (start)
704                 {
705                 fprintf(fp,"begin 600 %s\n",
706                         (uuname[0] == '\0')?"text.d":uuname);
707                 start=0;
708                 }
709
710         if (uubufnum)
711                 {
712                 if (uubufnum+num < 45)
713                         {
714                         memcpy(&(uubuf[uubufnum]),data,(unsigned int)num);
715                         uubufnum+=num;
716                         return(num);
717                         }
718                 else
719                         {
720                         i=45-uubufnum;
721                         memcpy(&(uubuf[uubufnum]),data,(unsigned int)i);
722                         j=uuencode((unsigned char *)uubuf,45,b);
723                         fwrite(b,1,(unsigned int)j,fp);
724                         uubufnum=0;
725                         data+=i;
726                         num-=i;
727                         }
728                 }
729
730         for (i=0; i<(((int)num)-INUUBUFN); i+=INUUBUFN)
731                 {
732                 j=uuencode(&(data[i]),INUUBUFN,b);
733                 fwrite(b,1,(unsigned int)j,fp);
734                 }
735         rem=(num-i)%45;
736         left=(num-i-rem);
737         if (left)
738                 {
739                 j=uuencode(&(data[i]),left,b);
740                 fwrite(b,1,(unsigned int)j,fp);
741                 i+=left;
742                 }
743         if (i != num)
744                 {
745                 memcpy(uubuf,&(data[i]),(unsigned int)rem);
746                 uubufnum=rem;
747                 }
748         return(ret);
749         }
750
751 void uufwriteEnd(FILE *fp)
752         {
753         int j;
754         static const char *end=" \nend\n";
755
756         if (uubufnum != 0)
757                 {
758                 uubuf[uubufnum]='\0';
759                 uubuf[uubufnum+1]='\0';
760                 uubuf[uubufnum+2]='\0';
761                 j=uuencode(uubuf,uubufnum,b);
762                 fwrite(b,1,(unsigned int)j,fp);
763                 }
764         fwrite(end,1,strlen(end),fp);
765         }
766
767 /* int size:  should always be > ~ 60; I actually ignore this parameter :-)    */
768 int uufread(unsigned char *out, int size, unsigned int num, FILE *fp)
769         {
770         int i,j,tot;
771         static int done=0;
772         static int valid=0;
773         static int start=1;
774
775         if (start)
776                 {
777                 for (;;)
778                         {
779                         b[0]='\0';
780                         fgets((char *)b,300,fp);
781                         if (b[0] == '\0')
782                                 {
783                                 fprintf(stderr,"no 'begin' found in uuencoded input\n");
784                                 return(-1);
785                                 }
786                         if (strncmp((char *)b,"begin ",6) == 0) break;
787                         }
788                 start=0;
789                 }
790         if (done) return(0);
791         tot=0;
792         if (valid)
793                 {
794                 memcpy(out,bb,(unsigned int)valid);
795                 tot=valid;
796                 valid=0;
797                 }
798         for (;;)
799                 {
800                 b[0]='\0';
801                 fgets((char *)b,300,fp);
802                 if (b[0] == '\0') break;
803                 i=strlen((char *)b);
804                 if ((b[0] == 'e') && (b[1] == 'n') && (b[2] == 'd'))
805                         {
806                         done=1;
807                         while (!feof(fp))
808                                 {
809                                 fgets((char *)b,300,fp);
810                                 }
811                         break;
812                         }
813                 i=uudecode(b,i,bb);
814                 if (i < 0) break;
815                 if ((i+tot+8) > num)
816                         {
817                         /* num to copy to make it a multiple of 8 */
818                         j=(num/8*8)-tot-8;
819                         memcpy(&(out[tot]),bb,(unsigned int)j);
820                         tot+=j;
821                         memcpy(bb,&(bb[j]),(unsigned int)i-j);
822                         valid=i-j;
823                         break;
824                         }
825                 memcpy(&(out[tot]),bb,(unsigned int)i);
826                 tot+=i;
827                 }
828         return(tot);
829         }
830
831 #define ccc2l(c,l)      (l =((DES_LONG)(*((c)++)))<<16, \
832                          l|=((DES_LONG)(*((c)++)))<< 8, \
833                          l|=((DES_LONG)(*((c)++))))
834
835 #define l2ccc(l,c)      (*((c)++)=(unsigned char)(((l)>>16)&0xff), \
836                     *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
837                     *((c)++)=(unsigned char)(((l)    )&0xff))
838
839
840 int uuencode(unsigned char *in, int num, unsigned char *out)
841         {
842         int j,i,n,tot=0;
843         DES_LONG l;
844         register unsigned char *p;
845         p=out;
846
847         for (j=0; j<num; j+=45)
848                 {
849                 if (j+45 > num)
850                         i=(num-j);
851                 else    i=45;
852                 *(p++)=i+' ';
853                 for (n=0; n<i; n+=3)
854                         {
855                         ccc2l(in,l);
856                         *(p++)=((l>>18)&0x3f)+' ';
857                         *(p++)=((l>>12)&0x3f)+' ';
858                         *(p++)=((l>> 6)&0x3f)+' ';
859                         *(p++)=((l    )&0x3f)+' ';
860                         tot+=4;
861                         }
862                 *(p++)='\n';
863                 tot+=2;
864                 }
865         *p='\0';
866         l=0;
867         return(tot);
868         }
869
870 int uudecode(unsigned char *in, int num, unsigned char *out)
871         {
872         int j,i,k;
873         unsigned int n=0,space=0;
874         DES_LONG l;
875         DES_LONG w,x,y,z;
876         unsigned int blank=(unsigned int)'\n'-' ';
877
878         for (j=0; j<num; )
879                 {
880                 n= *(in++)-' ';
881                 if (n == blank)
882                         {
883                         n=0;
884                         in--;
885                         }
886                 if (n > 60)
887                         {
888                         fprintf(stderr,"uuencoded line length too long\n");
889                         return(-1);
890                         }
891                 j++;
892
893                 for (i=0; i<n; j+=4,i+=3)
894                         {
895                         /* the following is for cases where spaces are
896                          * removed from lines.
897                          */
898                         if (space)
899                                 {
900                                 w=x=y=z=0;
901                                 }
902                         else
903                                 {
904                                 w= *(in++)-' ';
905                                 x= *(in++)-' ';
906                                 y= *(in++)-' ';
907                                 z= *(in++)-' ';
908                                 }
909                         if ((w > 63) || (x > 63) || (y > 63) || (z > 63))
910                                 {
911                                 k=0;
912                                 if (w == blank) k=1;
913                                 if (x == blank) k=2;
914                                 if (y == blank) k=3;
915                                 if (z == blank) k=4;
916                                 space=1;
917                                 switch (k) {
918                                 case 1: w=0; in--;
919                                 case 2: x=0; in--;
920                                 case 3: y=0; in--;
921                                 case 4: z=0; in--;
922                                         break;
923                                 case 0:
924                                         space=0;
925                                         fprintf(stderr,"bad uuencoded data values\n");
926                                         w=x=y=z=0;
927                                         return(-1);
928                                         break;
929                                         }
930                                 }
931                         l=(w<<18)|(x<<12)|(y<< 6)|(z    );
932                         l2ccc(l,out);
933                         }
934                 if (*(in++) != '\n')
935                         {
936                         fprintf(stderr,"missing nl in uuencoded line\n");
937                         w=x=y=z=0;
938                         return(-1);
939                         }
940                 j++;
941                 }
942         *out='\0';
943         w=x=y=z=0;
944         return(n);
945         }