TBase64.cxx

Go to the documentation of this file.
00001 // @(#)root/base:$Id: TBase64.cxx 28641 2009-05-15 15:20:08Z rdm $
00002 // Author: Gerardo Ganis + Fons Rademakers   15/5/2009
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2009, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 //////////////////////////////////////////////////////////////////////////
00013 //                                                                      //
00014 // TBase64                                                              //
00015 //                                                                      //
00016 // This code implements the Base64 encoding and decoding.               //
00017 // Base64 encoded messages are typically used in authentication         //
00018 // protocols and to pack binary data in HTTP messages.                  //
00019 //                                                                      //
00020 //////////////////////////////////////////////////////////////////////////
00021 
00022 #include "TBase64.h"
00023 
00024 ClassImp(TBase64)
00025 
00026 
00027 //_________________________________________________________________________
00028 static int ToB64low(const char *in, char *out, int mod)
00029 {
00030    // Base64 encoding of 3 bytes from in.
00031    // Output (4 bytes) saved in out (not null terminated).
00032    // Returns 0 on success, -1 if input or output arrays are
00033    // not defined.
00034 
00035    static char b64ref[64] = {
00036       'A','B','C','D','E','F','G','H','I','J',
00037       'K','L','M','N','O','P','Q','R','S','T',
00038       'U','V','W','X','Y','Z',
00039       'a','b','c','d','e','f','g','h','i','j',
00040       'k','l','m','n','o','p','q','r','s','t',
00041       'u','v','w','x','y','z',
00042       '0','1','2','3','4','5','6','7','8','9',
00043       '+','/'
00044    };
00045 
00046    if (!in || !out)
00047       return -1;
00048 
00049    if (mod == 1) {
00050       *out++ = b64ref[ 0x3F & (in[0] >> 2) ];
00051       *out++ = b64ref[ 0x3F & (0x30 & (in[0] << 4)) ];
00052       *out++ = '=';
00053       *out++ = '=';
00054    } else if (mod == 2) {
00055       *out++ = b64ref[ 0x3F & (in[0] >> 2) ];
00056       *out++ = b64ref[ 0x3F & ((0x30 & (in[0] << 4)) | (0x0F & (in[1] >> 4))) ];
00057       *out++ = b64ref[ 0x3F & (0x3C & (in[1] << 2)) ];
00058       *out++ = '=';
00059    } else {
00060       *out++ = b64ref[ (int)(0x3F & (in[0] >> 2)) ];
00061       *out++ = b64ref[ 0x3F & ((0x30 & (in[0] << 4)) | (0x0F & (in[1] >> 4))) ];
00062       *out++ = b64ref[ 0x3F & ((0x3C & (in[1] << 2)) | (0x03 & (in[2] >> 6))) ];
00063       *out++ = b64ref[ 0x3F & in[2] ];
00064    }
00065 
00066    return 0;
00067 }
00068 
00069 //_________________________________________________________________________
00070 static int FromB64low(const char *in, TString &out)
00071 {
00072    // Base64 decoding of 4 bytes from in.
00073    // Output (3 bytes) returned in out.
00074    // No check for base64-ness of input characters.
00075 
00076    static int b64inv[256] = {
00077       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00078       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00079       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
00080       52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,
00081       -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
00082       15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
00083       -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
00084       41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,
00085       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00086       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00087       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00088       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00089       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00090       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00091       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
00092       -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
00093    };
00094 
00095    UInt_t i0 = (UInt_t)(in[0]);
00096    UInt_t i1 = (UInt_t)(in[1]);
00097    UInt_t i2 = (UInt_t)(in[2]);
00098    UInt_t i3 = (UInt_t)(in[3]);
00099    if (in[3] != '=') {
00100       out.Append((char)(0xFC & (b64inv[i0] << 2)) | (0x03 & (b64inv[i1] >> 4)));
00101       out.Append((char)(0xF0 & (b64inv[i1] << 4)) | (0x0F & (b64inv[i2] >> 2)));
00102       out.Append((char)(0xC0 & (b64inv[i2] << 6)) | (0x3F &  b64inv[i3]));
00103       return 3;
00104    } else if (in[2] == '=') {
00105       out.Append((char)(0xFC & (b64inv[i0] << 2)) | (0x03 & (b64inv[i1] >> 4)));
00106       return 1;
00107    } else {
00108       out.Append((char)(0xFC & (b64inv[i0] << 2)) | (0x03 & (b64inv[i1] >> 4)));
00109       out.Append((char)(0xF0 & (b64inv[i1] << 4)) | (0x0F & (b64inv[i2] >> 2)));
00110       return 2;
00111    }
00112 }
00113 
00114 //______________________________________________________________________________
00115 TString TBase64::Encode(const char *data)
00116 {
00117    // Transform data into a null terminated base64 string.
00118 
00119    return Encode(data, strlen(data));
00120 }
00121 
00122 //______________________________________________________________________________
00123 TString TBase64::Encode(const char *data, Int_t len)
00124 {
00125    // Transform len bytes from data into a null terminated base64 string.
00126 
00127    TString ret(len * 2);
00128 
00129    int mod = 0;
00130    char oo[5] = {0};
00131    for (int i = 0; i < len; i += 3) {
00132       mod = len-i;
00133       ToB64low(data+i, oo, mod);
00134       oo[4] = 0;
00135       ret += oo;
00136    }
00137    return ret;
00138 }
00139 
00140 //______________________________________________________________________________
00141 TString TBase64::Decode(const char *data)
00142 {
00143    // Decode a base64 string date into a generic TString.
00144    // No check for base64-ness of input characters.
00145 
00146    int len = strlen(data);
00147    TString ret(len);
00148 
00149    for (int i = 0; i < len; i += 4)
00150       FromB64low(data+i, ret);
00151 
00152    return ret;
00153 }

Generated on Tue Jul 5 14:11:19 2011 for ROOT_528-00b_version by  doxygen 1.5.1