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