#include <stdio.h>
#include <string.h>

/* base64 [-d|-e] [filename]

   program to encode and decode to and from base64, [rfc1113]
   burt rosenberg wrote the main
   the hard stuff is from rsaref.

*/

/* R_ENCODE.H - header file for R_ENCODE.C
 */

/* Copyright (C) 1991-2 RSA Laboratories, a division of RSA Data
   Security, Inc. All rights reserved.
 */

void R_EncodePEMBlock 
  (unsigned char *, unsigned int *, unsigned char *, unsigned int);
int R_DecodePEMBlock 
  (unsigned char *, unsigned int *, unsigned char *, unsigned int);

/* Error codes.
 */
#define RE_ENCODING 0x0403

#define ENCODE 1
#define DECODE 0

FILE * F = stdin ;

int get_48(unsigned char * buf) {
  /* returns the next 48 characters on the input stream,
     or up to EOF. Returns the number of characters.
  */

  int i ;
  int nc = 47 ;

  i = getc(F) ;
  if ( i==EOF ) return(0) ;
  else *buf++ = i ;

  while ( nc-- && (i=getc(F))!=EOF ) *buf++ = i ;
  *buf = '\0' ;
  return(1) ;

}

int main(int argc, char *argv[]){

   unsigned char encodedBlock[100] ;
   unsigned int encodedBlockLen ;
   unsigned char s[100] ;
   unsigned int s_len ;
   int mode = ENCODE ;

   if ( argc==1 ) {
      fprintf(stderr,"Usage: %s [-d|-e] [filename]\n", argv[0] ) ;
      exit(0) ;
   }

   while ( --argc ) {
      argv++ ;
      if ( (*argv)[0] != '-' ) {
         /* file name */
         F = fopen( *argv, "r" ) ;
         if ( F==NULL ) {
            fprintf(stderr,"Can't open file %s\n", *argv) ;
            exit(1) ;
         }
      }
      else switch ((*argv)[1]) {
         case 'd': mode = DECODE ; break ;
         case 'e': mode = ENCODE ; break ;
      }
   }

   if ( mode==DECODE ) 
      while ( fgets(encodedBlock,sizeof(encodedBlock),F) ) {
         /* remove the \n */
         encodedBlockLen = strlen(encodedBlock)-1 ;
         R_DecodePEMBlock (s, &s_len, encodedBlock, encodedBlockLen) ;
         s[s_len] = '\0' ;
         printf("%s", s ) ;
       }
    if ( mode==ENCODE ) 
      while (get_48(s)) {
         R_EncodePEMBlock( encodedBlock, &encodedBlockLen, s, strlen(s)) ;
         encodedBlock[encodedBlockLen] = '\0' ;
         printf("%s\n", encodedBlock) ;
      }
}


/* GLOBAL.H - RSAREF types and constants
 */

/****************************************************************
  				NOTE:
 This copy has been modified for compilation on DEC OSF/1 on Alpha
 AXP.  See the definition of UINT4 below.
 John Kohl, <jtk@atria.com>, 1994/June/16
 ****************************************************************/
/* Copyright (C) 1991-2 RSA Laboratories, a division of RSA Data
   Security, Inc. All rights reserved.
 */

/* PROTOTYPES should be set to one if and only if the compiler supports
     function argument prototyping.
   The following makes PROTOTYPES default to 0 if it has not already been
     defined with C compiler flags.
 */
#ifndef PROTOTYPES
#define PROTOTYPES 0
#endif

/* POINTER defines a generic pointer type */
typedef unsigned char *POINTER;

/* UINT2 defines a two byte word */
typedef unsigned short int UINT2;

/* UINT4 defines a four byte word */
#ifdef __alpha
typedef unsigned int UINT4;
#else
typedef unsigned long int UINT4;
#endif

/* PROTO_LIST is defined depending on how PROTOTYPES is defined above.
   If using PROTOTYPES, then PROTO_LIST returns the list, otherwise it
     returns an empty list.  
 */
#if PROTOTYPES
#define PROTO_LIST(list) list
#else
#define PROTO_LIST(list) ()
#endif


/* R_ENCODE.H - RFC 1113 encoding and decoding routines
 */

/* Copyright (C) 1991-2 RSA Laboratories, a division of RSA Data
   Security, Inc. All rights reserved.
 */


/* RFC 1113 encoding:

   Value Encoding  Value Encoding  Value Encoding  Value Encoding
       0 A            17 R            34 i            51 z
       1 B            18 S            35 j            52 0
       2 C            19 T            36 k            53 1
       3 D            20 U            37 l            54 2
       4 E            21 V            38 m            55 3
       5 F            22 W            39 n            56 4
       6 G            23 X            40 o            57 5
       7 H            24 Y            41 p            58 6
       8 I            25 Z            42 q            59 7
       9 J            26 a            43 r            60 8
      10 K            27 b            44 s            61 9
      11 L            28 c            45 t            62 +
      12 M            29 d            46 u            63 /
      13 N            30 e            47 v
      14 O            31 f            48 w         (pad) =
      15 P            32 g            49 x
      16 Q            33 h            50 y
*/
#define ENCODING(i) \
  (unsigned char)(((i) < 26) ? ((i) + 0x41) : \
                  (((i) < 52) ? ((i) - 26 + 0x61) : \
                   (((i) < 62) ? ((i) - 52 + 0x30) : \
                    (((i) == 62) ? 0x2b : 0x2f))))
#define ENCODING_PAD 0x3d

#define IS_ENCODING(c) \
  ((((c) >= 0x41) && ((c) <= 0x5a)) || \
   (((c) >= 0x61) && ((c) <= 0x7a)) || \
   (((c) >= 0x30) && ((c) <= 0x39)) || \
   ((c) == 0x2b) || \
   ((c) == 0x2f))

/* assumes IS_ENCODING (c) == 1 */
#define DECODING(c) \
  (((c) == 0x2b) ? 62 : \
   (((c) == 0x2f) ? 63 : \
    (((c) <= 0x39) ? ((c) - 0x30 + 52) : \
     (((c) <= 0x5a) ? ((c) - 0x41) : ((c) - 0x61 + 26)))))
      
static void EncodeQuantum 
  (unsigned char [4], unsigned char [3]);
static int DecodeQuantum 
  (unsigned char [3], unsigned char [4]);
static void EncodeLastQuantum
  (unsigned char [4], unsigned char *, unsigned int);
static int DecodeLastQuantum
  (unsigned char *, unsigned int *, unsigned char [4]);

void R_EncodePEMBlock (encodedBlock, encodedBlockLen, block, blockLen)
unsigned char *encodedBlock;                               /* encoded block */
unsigned int *encodedBlockLen;                   /* length of encoded block */
unsigned char *block;                                              /* block */
unsigned int blockLen;                                   /* length of block */
{
  unsigned int i, lastLen;
  
  if (blockLen < 1) {
    *encodedBlockLen = 0;
    return;
  }
  
  for (i = 0; i < (blockLen-1)/3; i++)
    EncodeQuantum (&encodedBlock[4*i], &block[3*i]);
  
  lastLen = blockLen - 3*i;
  EncodeLastQuantum (&encodedBlock[4*i], &block[3*i], lastLen);
  *encodedBlockLen = 4*i + 4;
}
                    
int R_DecodePEMBlock (block, blockLen, encodedBlock, encodedBlockLen)
unsigned char *block;                                              /* block */
unsigned int *blockLen;                                  /* length of block */
unsigned char *encodedBlock;                               /* encoded block */
unsigned int encodedBlockLen;                    /* length of encoded block */
{
  int status;
  unsigned int i, lastLen;

  if (encodedBlockLen % 4)
    return (RE_ENCODING);
  
  if (encodedBlockLen < 1) {
    *blockLen = 0;
    return (0);
  }
  
  for (i = 0; i < (encodedBlockLen-1)/4; i++)
    if (status = DecodeQuantum (&block[3*i], &encodedBlock[4*i]))
      return (status);
    
  if (status = DecodeLastQuantum (&block[3*i], &lastLen, &encodedBlock[4*i]))
    return (status);

  *blockLen = 3*i + lastLen;
  return (0);
}

static void EncodeQuantum (encodedQuantum, quantum)
unsigned char encodedQuantum[4];
unsigned char quantum[3];
{
  UINT4 temp;
  unsigned int a, b, c, d;
  
  temp = ((UINT4)quantum[0]) << 16;
  temp |= ((UINT4)quantum[1]) << 8;
  temp |= (UINT4)quantum[2];
  
  a = (unsigned int)((temp >> 18) & 0x3f);
  b = (unsigned int)((temp >> 12) & 0x3f);
  c = (unsigned int)((temp >> 6) & 0x3f);
  d = (unsigned int)(temp & 0x3f);

  encodedQuantum[0] = ENCODING (a);
  encodedQuantum[1] = ENCODING (b);
  encodedQuantum[2] = ENCODING (c);
  encodedQuantum[3] = ENCODING (d);

  /* Zeroize potentially sensitive information.
   */
  temp = 0;
  a = b = c = d = 0;
}

static int DecodeQuantum (quantum, encodedQuantum)
unsigned char quantum[3];
unsigned char encodedQuantum[4];
{
  UINT4 temp;
  unsigned int a, b, c, d;
  
  if (! IS_ENCODING (encodedQuantum[0]) ||
      ! IS_ENCODING (encodedQuantum[1]) ||
      ! IS_ENCODING (encodedQuantum[2]) ||
      ! IS_ENCODING (encodedQuantum[3]))
    return (RE_ENCODING);
  
  a = DECODING (encodedQuantum[0]);
  b = DECODING (encodedQuantum[1]);
  c = DECODING (encodedQuantum[2]);
  d = DECODING (encodedQuantum[3]);
  
  temp = ((UINT4)a) << 18;
  temp |= ((UINT4)b) << 12;
  temp |= ((UINT4)c) << 6;
  temp |= (UINT4)d;

  quantum[0] = (unsigned char)(temp >> 16);
  quantum[1] = (unsigned char)(temp >> 8);
  quantum[2] = (unsigned char)temp;
  
  /* Zeroize potentially sensitive information.
   */
  temp = 0;
  a = b = c = d = 0;

  return (0);
}

static void EncodeLastQuantum (encodedQuantum, quantum, quantumLen)
unsigned char encodedQuantum[4];
unsigned char *quantum;
unsigned int quantumLen;                                       /* 1, 2 or 3 */
{
  UINT4 temp;
  unsigned int a, b, c, d;

  temp = ((UINT4)quantum[0]) << 16;
  if (quantumLen >= 2)
    temp |= ((UINT4)quantum[1]) << 8;
  if (quantumLen == 3)
    temp |= ((UINT4)quantum[2]);
  
  a = (unsigned int)((temp >> 18) & 0x3f);
  b = (unsigned int)((temp >> 12) & 0x3f);
  if (quantumLen >= 2)
    c = (unsigned int)((temp >> 6) & 0x3f);
  if (quantumLen == 3)
    d = (unsigned int)(temp & 0x3f);

  encodedQuantum[0] = ENCODING (a);
  encodedQuantum[1] = ENCODING (b);
  if (quantumLen >= 2)
    encodedQuantum[2] = ENCODING (c);
  else
    encodedQuantum[2] = ENCODING_PAD;
  if (quantumLen == 3)
    encodedQuantum[3] = ENCODING (d);
  else
    encodedQuantum[3] = ENCODING_PAD;

  /* Zeroize potentially sensitive information.
   */
  temp = 0;
  a = b = c = d = 0;
}

static int DecodeLastQuantum (quantum, quantumLen, encodedQuantum)
unsigned char *quantum;
unsigned int *quantumLen;                                      /* 1, 2 or 3 */
unsigned char encodedQuantum[4];
{
  UINT4 temp;
  unsigned int a, b, c, d;
  
  if (! IS_ENCODING (encodedQuantum[0]) ||
      ! IS_ENCODING (encodedQuantum[1]) ||
      (! IS_ENCODING (encodedQuantum[2]) &&
       (encodedQuantum[2] != ENCODING_PAD)) ||
      (! IS_ENCODING (encodedQuantum[3]) &&
       (encodedQuantum[3] != ENCODING_PAD)))
    return (RE_ENCODING);
        
  if (encodedQuantum[2] == ENCODING_PAD)
    *quantumLen = 1;
  else if (encodedQuantum[3] == ENCODING_PAD)
    *quantumLen = 2;
  else
    *quantumLen = 3;
  
  a = DECODING (encodedQuantum[0]);
  b = DECODING (encodedQuantum[1]);
  if (*quantumLen >= 2)
    c = DECODING (encodedQuantum[2]);
  if (*quantumLen == 3)
    d = DECODING (encodedQuantum[3]);
  
  temp = ((UINT4)a) << 18;
  temp |= ((UINT4)b) << 12;
  if (*quantumLen >= 2)
    temp |= ((UINT4)c) << 6;
  if (*quantumLen == 3)
    temp |= ((UINT4)d);

  quantum[0] = (unsigned char)(temp >> 16);
  if (*quantumLen >= 2)
    quantum[1] = (unsigned char)(temp >> 8);
  if (*quantumLen == 3)
    quantum[2] = (unsigned char)temp;
  
  /* Zeroize potentially sensitive information.
   */
  temp = 0;
  a = b = c = d = 0;
  
  return (0);
}
