please dont rip this site

Method Codec BASE64

/ ****************************************************************************
*!
*!  FILE NAME: bases.c
*!
*!  DESCRIPTION: Functions for encoding/decoding base64 strings.
*!
*!---------------------------------------------------------------------------
*!  HISTORY
*!
*!  DATE     NAME     CHANGES
*!
*!  970127   ronny    Initial version
*!
*!---------------------------------------------------------------------------
*!  (C) Copyright 1997, Axis Communications AB, Lund, Sweden
*!                    by Ronny Ranerup
*!****************************************************************************/
/**************************  Include files  ********************************/
#if !defined(_WIN32)
#include "conf.h"
#else
#include "nt.h"
#endif

#include <stdio.h>
#include "ftplib.h"
#include "bases.h"
/**************************  Constants and macros  *************************/
/**************************  Type definitions  *****************************/
/**************************  Global variables  *****************************/
extern int db1;         /* Debugging printout? */
extern int db2;         /* Debugging printout? */
extern int db3;         /* Debugging printout? */
extern int db4;         /* Debugging printout? */
/**************************  Function prototypes  **************************/
int   IntPow         (int base, int pow);
char *ConverToBase26 (int num);
/****************************************************************************
*#
*#  FUNCTION NAME: Decode
*#
*#  PARAMETERS: 
*#
*#  DESCRIPTION: Decodes a '\0' ended string into base64. Not used by EyeGet. 
*#
*#-------------------------------------------------------------------------*/

void
Decode(char *buf)
{
  int i;
  char c;
  char base[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  unsigned char index;
  char up[100000] = {'\0',};
  int o = 0;

  for (i = 0; i < (int) strlen(buf); i += 4) {
    c = buf[i];
    if (c == '=')
      break;
    up[o] = (strchr(base, c) - base) << 2;
    
    c = buf[i+1];
    if (c == '=')
      break;
    index = strchr(base, c) - base;
    up[o]   |= index >> 4;
    up[++o] |= index << 4;
    
    c = buf[i+2];
    index = strchr(base, c) - base;
    if (c == '=')
      break;
    up[o]   |= index >> 2;
    up[++o] |= index << 6;

    c = buf[i+3];
    if (c == '=')
      break;
    up[o++] |= index = strchr(base, c) - base;
    
  }
  printf("\n%s\n", up);
}

/****************************************************************************
*#
*#  FUNCTION NAME: Encode
*#
*#  PARAMETERS: char *u: '\0' ended encoded string.
*#              int len: length of u.
*#              char *e: Resulting encoded string. Must be all zeroes on entry.
*#                       Takes 1.25 times the space of the unencoded string.
*#
*#  DESCRIPTION: Encodes a string into base64. 
*#
*#-------------------------------------------------------------------------*/

void 
Encode(char *u, int len, char *e)
{
  int i;
  char c;
  char base[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  int o = 0;

  if (db3) printf(">>> Encode: '%s'\n", u);

  for (i = 0; i < len; i += 3) {
    c = (u[i] & 253) >> 2; 
    e[o] = base[(int)c];
      
    c = (u[i] & 3) << 4;
    c |= (u[i+1] & 241) >> 4;
    e[o+1] |= base[(int)c];
  
    c = (u[i+1] & 15) << 2;
    c |= (u[i+2] & 192) >> 6;
    e[o+2] |= base[(int)c];
  
    if (i+3 > len) {
      if (i + 1 == len) {
	e[o+2] = '=';
	e[o+3] = '=';
	o += 4;
	break;
      }
      else if (i + 2 == len) {
	e[o+3] = '=';
	o += 4;
	break;
      }
      else {
	c = (u[i+2] & 63);
	e[o+3] = base[(int)c];
	o += 4;
	break;
      }
    }
    
    c = (u[i+2] & 63);
    e[o+3] = base[(int)c];

    o += 4;
  }

  e[o] = '\0';

  if (db3) printf("<<< Encode: '%s'\n", e);
}

/****************************************************************************
*#
*#  FUNCTION NAME: ConverToBase26
*#
*#  PARAMETERS: The integer to convert.
*#
*#  DESCRIPTION: Convert an integer to a number in base 26.
*#
*#  RETURNS: The new number in a string.
*#
*#---------------------------------------------------------------------------
*#  HISTORY
*#
*#  DATE     NAME     CHANGES  
*#  ----     ----     -------
*#  961022   ronny    Initial version
*#  
*#***************************************************************************/

char *
ConverToBase26(int num)
{
  static char res[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  int remainder;
  int tmp = num;
  const int base = 26;
  int cnt = strlen(res)-1;
  
  do {
    remainder = tmp % base;
    tmp = tmp / base;
    res[cnt] = remainder + 'a';
    if (db4) printf("* res[%d] = '%c'\n",  cnt, res[cnt]);
    cnt--;
  } while (tmp > 0);
  
  return (&res[sizeof(res)-5]);
}

/****************************************************************************
*#
*#  FUNCTION NAME: IntPow
*#
*#  DESCRIPTION: pow, but with integer.
*#
*#  RETURNS: int result..
*#
*#---------------------------------------------------------------------------
*#  HISTORY
*#
*#  DATE     NAME     CHANGES  
*#  ----     ----     -------
*#  961028   ronny    Initial version
*#  
*#***************************************************************************/

int 
IntPow(int base, int pow)
{
  int i;
  int delta = 1;

  for (i = 0; i != pow; i++) {
    delta *= base;
  }

  return (delta);
}

/****************************************************************************
*#
*#  FUNCTION NAME: ConverFromBase26
*#
*#  PARAMETERS: The string to convert.
*#
*#  DESCRIPTION: Convert an string with a number in base 26 to an int.
*#
*#  RETURNS: The number in base 10.
*#
*#---------------------------------------------------------------------------
*#  HISTORY
*#
*#  DATE     NAME     CHANGES  
*#  ----     ----     -------
*#  961028   ronny    Initial version
*#  
*#***************************************************************************/

int
ConverFromBase26(char *base26)
{
  int tmp;
  int sum = 0;
  int i;
  const int base = 26;

  if (db4) printf(">>> ConverFromBase26: %s\n", base26);
  for (i = 0; i != (int) strlen(base26); i++) {
    if (!(isalpha(base26[i]))) {
      if (db4) printf("Not an alphanumeric char\n");
      return(0);
    }
    tmp = base26[i] - 'a';
    sum += IntPow(base, (int) strlen(base26) - (i+1)) * tmp;
    if (db4) printf("sum = %d\n", sum);
  }
  
  if (db4) printf("end sum = %d\n", sum);
  return ((int) sum);
}

/****************************************************************************
*#
*#  FUNCTION NAME: handler
*#
*#  PARAMETERS: Signal number.
*#
*#  DESCRIPTION: 
*#
*#---------------------------------------------------------------------------
*#  HISTORY
*#
*#  DATE     NAME     CHANGES  
*#  ----     ----     -------
*#  960819   ronny    Initial version
*#  960929   ronny    alarmFlag is always set.
*#
*#***************************************************************************/

/* Only used for seperate compilation.  */

/*
void
main(int argc, char *argv[])
{
  FILE *fd;
  struct stat st;
  char *in;
  int res;
  char *out;

  stat(argv[1], &st);
  if ((fd = fopen(argv[1], "r")) == NULL) {
    printf("Cant open %s\n", argv[1]);
    exit(1);
  }

  in = (char*) malloc(st.st_size + 2);
  in[st.st_size] = 0;
  in[st.st_size + 1] = 0;
  out = (char*) malloc(st.st_size * 2);

  printf("Size: %d\n", (int) st.st_size);

  res = fread(in, 1, st.st_size, fd);
  printf("Read %d bytes.\n", res);

  Encode(in, res, out);
  Decode(out);
}
*/





file: /Techref/method/codec/base64.txt, 7KB, , updated: 1999/12/21 11:57, local time: 2019/7/17 14:45,
TOP NEW HELP FIND: 
3.92.28.84:LOG IN

 ©2019 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE! / 

<A HREF="http://techref.massmind.org/techref/method/codec/base64.txt"> method codec base64</A>

Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to techref.massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .