Path: blob/devel/elmergrid/src/metis-5.1.0/GKlib/b64.c
3206 views
/*!1\file b64.c2\brief This file contains some simple 8bit-to-6bit encoding/deconding routines34Most of these routines are outdated and should be converted using glibc's equivalent5routines.67\date Started 2/22/058\author George9\version\verbatim $Id: b64.c 10711 2011-08-31 22:23:04Z karypis $ \endverbatim1011\verbatim12$Copyright$13$License$14\endverbatim1516*/171819#include "GKlib.h"2021#define B64OFFSET 48 /* This is the '0' number */222324/******************************************************************************25* Encode 3 '8-bit' binary bytes as 4 '6-bit' characters26*******************************************************************************/27void encodeblock(unsigned char *in, unsigned char *out)28{29out[0] = (in[0] >> 2);30out[1] = (((in[0] & 0x03) << 4) | (in[1] >> 4));31out[2] = (((in[1] & 0x0f) << 2) | (in[2] >> 6));32out[3] = (in[2] & 0x3f);3334out[0] += B64OFFSET;35out[1] += B64OFFSET;36out[2] += B64OFFSET;37out[3] += B64OFFSET;3839// printf("%c %c %c %c %2x %2x %2x %2x %2x %2x %2x\n", out[0], out[1], out[2], out[3], out[0], out[1], out[2], out[3], in[0], in[1], in[2]);40}4142/******************************************************************************43* Decode 4 '6-bit' characters into 3 '8-bit' binary bytes44*******************************************************************************/45void decodeblock(unsigned char *in, unsigned char *out)46{47in[0] -= B64OFFSET;48in[1] -= B64OFFSET;49in[2] -= B64OFFSET;50in[3] -= B64OFFSET;5152out[0] = (in[0] << 2 | in[1] >> 4);53out[1] = (in[1] << 4 | in[2] >> 2);54out[2] = (in[2] << 6 | in[3]);55}565758/******************************************************************************59* This function encodes an input array of bytes into a base64 encoding. Memory60* for the output array is assumed to have been allocated by the calling program61* and be sufficiently large. The output string is NULL terminated.62*******************************************************************************/63void GKEncodeBase64(int nbytes, unsigned char *inbuffer, unsigned char *outbuffer)64{65int i, j;6667if (nbytes%3 != 0)68gk_errexit(SIGERR, "GKEncodeBase64: Input buffer size should be a multiple of 3! (%d)\n", nbytes);6970for (j=0, i=0; i<nbytes; i+=3, j+=4)71encodeblock(inbuffer+i, outbuffer+j);7273//printf("%d %d\n", nbytes, j);74outbuffer[j] = '\0';75}76777879/******************************************************************************80* This function decodes an input array of base64 characters into their actual81* 8-bit codes. Memory * for the output array is assumed to have been allocated82* by the calling program and be sufficiently large. The padding is discarded.83*******************************************************************************/84void GKDecodeBase64(int nbytes, unsigned char *inbuffer, unsigned char *outbuffer)85{86int i, j;8788if (nbytes%4 != 0)89gk_errexit(SIGERR, "GKDecodeBase64: Input buffer size should be a multiple of 4! (%d)\n", nbytes);9091for (j=0, i=0; i<nbytes; i+=4, j+=3)92decodeblock(inbuffer+i, outbuffer+j);93}94959697