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