ROOT  6.06/09
Reference Guide
TBase64.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Gerardo Ganis + Fons Rademakers 15/5/2009
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2009, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 /** \class TBase64
13 This code implements the Base64 encoding and decoding.
14 
15 Base64 encoded messages are typically used in authentication
16 protocols and to pack binary data in HTTP messages.
17 */
18 
19 #include "TBase64.h"
20 
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 /// Base64 encoding of 3 bytes from in.
25 /// Output (4 bytes) saved in out (not null terminated).
26 /// Returns 0 on success, -1 if input or output arrays are
27 /// not defined.
28 
29 static int ToB64low(const char *in, char *out, int mod)
30 {
31  static char b64ref[64] = {
32  'A','B','C','D','E','F','G','H','I','J',
33  'K','L','M','N','O','P','Q','R','S','T',
34  'U','V','W','X','Y','Z',
35  'a','b','c','d','e','f','g','h','i','j',
36  'k','l','m','n','o','p','q','r','s','t',
37  'u','v','w','x','y','z',
38  '0','1','2','3','4','5','6','7','8','9',
39  '+','/'
40  };
41 
42  if (!in || !out)
43  return -1;
44 
45  if (mod == 1) {
46  *out++ = b64ref[ 0x3F & (in[0] >> 2) ];
47  *out++ = b64ref[ 0x3F & (0x30 & (in[0] << 4)) ];
48  *out++ = '=';
49  *out++ = '=';
50  } else if (mod == 2) {
51  *out++ = b64ref[ 0x3F & (in[0] >> 2) ];
52  *out++ = b64ref[ 0x3F & ((0x30 & (in[0] << 4)) | (0x0F & (in[1] >> 4))) ];
53  *out++ = b64ref[ 0x3F & (0x3C & (in[1] << 2)) ];
54  *out++ = '=';
55  } else {
56  *out++ = b64ref[ (int)(0x3F & (in[0] >> 2)) ];
57  *out++ = b64ref[ 0x3F & ((0x30 & (in[0] << 4)) | (0x0F & (in[1] >> 4))) ];
58  *out++ = b64ref[ 0x3F & ((0x3C & (in[1] << 2)) | (0x03 & (in[2] >> 6))) ];
59  *out++ = b64ref[ 0x3F & in[2] ];
60  }
61 
62  return 0;
63 }
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Base64 decoding of 4 bytes from in.
67 /// Output (3 bytes) returned in out.
68 /// No check for base64-ness of input characters.
69 
70 static int FromB64low(const char *in, TString &out)
71 {
72  static int b64inv[256] = {
73  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
74  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
75  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
76  52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,
77  -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
78  15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
79  -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
80  41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,
81  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
82  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
83  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
84  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
85  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
86  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
87  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
88  -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
89  };
90 
91  UInt_t i0 = (UInt_t)(in[0]);
92  UInt_t i1 = (UInt_t)(in[1]);
93  UInt_t i2 = (UInt_t)(in[2]);
94  UInt_t i3 = (UInt_t)(in[3]);
95  if (in[3] != '=') {
96  out.Append((char)(0xFC & (b64inv[i0] << 2)) | (0x03 & (b64inv[i1] >> 4)));
97  out.Append((char)(0xF0 & (b64inv[i1] << 4)) | (0x0F & (b64inv[i2] >> 2)));
98  out.Append((char)(0xC0 & (b64inv[i2] << 6)) | (0x3F & b64inv[i3]));
99  return 3;
100  } else if (in[2] == '=') {
101  out.Append((char)(0xFC & (b64inv[i0] << 2)) | (0x03 & (b64inv[i1] >> 4)));
102  return 1;
103  } else {
104  out.Append((char)(0xFC & (b64inv[i0] << 2)) | (0x03 & (b64inv[i1] >> 4)));
105  out.Append((char)(0xF0 & (b64inv[i1] << 4)) | (0x0F & (b64inv[i2] >> 2)));
106  return 2;
107  }
108 }
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 /// Transform data into a null terminated base64 string.
112 
113 TString TBase64::Encode(const char *data)
114 {
115  return Encode(data, strlen(data));
116 }
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Transform len bytes from data into a null terminated base64 string.
120 
121 TString TBase64::Encode(const char *data, Int_t len)
122 {
123  TString ret(len * 2);
124 
125  int mod = 0;
126  char oo[5] = {0};
127  for (int i = 0; i < len; i += 3) {
128  mod = len-i;
129  ToB64low(data+i, oo, mod);
130  oo[4] = 0;
131  ret += oo;
132  }
133  return ret;
134 }
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 /// Decode a base64 string date into a generic TString.
138 /// No check for base64-ness of input characters.
139 
140 TString TBase64::Decode(const char *data)
141 {
142  int len = strlen(data);
143  TString ret(len);
144 
145  for (int i = 0; i < len; i += 4)
146  FromB64low(data+i, ret);
147 
148  return ret;
149 }
This code implements the Base64 encoding and decoding.
Definition: TBase64.h:33
static TString Decode(const char *data)
Decode a base64 string date into a generic TString.
Definition: TBase64.cxx:140
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
static TString Encode(const char *data)
Transform data into a null terminated base64 string.
Definition: TBase64.cxx:113
TString & Append(const char *cs)
Definition: TString.h:492
static int FromB64low(const char *in, TString &out)
Base64 decoding of 4 bytes from in.
Definition: TBase64.cxx:70
ClassImp(TBase64) static int ToB64low(const char *in
Base64 encoding of 3 bytes from in.
unsigned int UInt_t
Definition: RtypesCore.h:42