Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
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\ingroup Base
14
15This code implements the Base64 encoding and decoding.
16
17Base64 encoded messages are typically used in authentication
18protocols and to pack binary data in HTTP messages.
19*/
20
21#include "TBase64.h"
22
23#include <ROOT/RConfig.hxx>
24
25
26////////////////////////////////////////////////////////////////////////////////
27/// Base64 encoding of 3 bytes from in.
28/// Output (4 bytes) saved in out (not null terminated).
29
30static void ToB64low(const char *in, char *out, int mod)
31{
32 static char b64ref[64] = {
33 'A','B','C','D','E','F','G','H','I','J',
34 'K','L','M','N','O','P','Q','R','S','T',
35 'U','V','W','X','Y','Z',
36 'a','b','c','d','e','f','g','h','i','j',
37 'k','l','m','n','o','p','q','r','s','t',
38 'u','v','w','x','y','z',
39 '0','1','2','3','4','5','6','7','8','9',
40 '+','/'
41 };
42
43 if (R__likely(mod > 2)) {
44 *out++ = b64ref[ (int)(0x3F & (in[0] >> 2)) ];
45 *out++ = b64ref[ 0x3F & ((0x30 & (in[0] << 4)) | (0x0F & (in[1] >> 4))) ];
46 *out++ = b64ref[ 0x3F & ((0x3C & (in[1] << 2)) | (0x03 & (in[2] >> 6))) ];
47 *out++ = b64ref[ 0x3F & in[2] ];
48 } else if (mod == 1) {
49 *out++ = b64ref[ 0x3F & (in[0] >> 2) ];
50 *out++ = b64ref[ 0x3F & (0x30 & (in[0] << 4)) ];
51 *out++ = '=';
52 *out++ = '=';
53 } else if (mod == 2) {
54 *out++ = b64ref[ 0x3F & (in[0] >> 2) ];
55 *out++ = b64ref[ 0x3F & ((0x30 & (in[0] << 4)) | (0x0F & (in[1] >> 4))) ];
56 *out++ = b64ref[ 0x3F & (0x3C & (in[1] << 2)) ];
57 *out++ = '=';
58 }
59}
60
61////////////////////////////////////////////////////////////////////////////////
62/// Base64 decoding of 4 bytes from in.
63/// Output (3 bytes) appended to out.
64/// No check for base64-ness of input characters.
65
66static void FromB64low(const char *in, TString &out)
67{
68 static int b64inv[256] = {
69 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
70 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
71 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
72 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,
73 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
74 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
75 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
76 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,
77 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
78 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
79 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
80 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-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 };
86
87 const UInt_t i0 = (UInt_t)(in[0]);
88 const UInt_t i1 = (UInt_t)(in[1]);
89 const UInt_t i2 = (UInt_t)(in[2]);
90 const UInt_t i3 = (UInt_t)(in[3]);
91 if (R__likely(in[3] != '=')) {
92 out.Append((char)(0xFC & (b64inv[i0] << 2)) | (0x03 & (b64inv[i1] >> 4)));
93 out.Append((char)(0xF0 & (b64inv[i1] << 4)) | (0x0F & (b64inv[i2] >> 2)));
94 out.Append((char)(0xC0 & (b64inv[i2] << 6)) | (0x3F & b64inv[i3]));
95 } else if (in[2] == '=') {
96 out.Append((char)(0xFC & (b64inv[i0] << 2)) | (0x03 & (b64inv[i1] >> 4)));
97 } else {
98 out.Append((char)(0xFC & (b64inv[i0] << 2)) | (0x03 & (b64inv[i1] >> 4)));
99 out.Append((char)(0xF0 & (b64inv[i1] << 4)) | (0x0F & (b64inv[i2] >> 2)));
100 }
101}
102
103////////////////////////////////////////////////////////////////////////////////
104/// Transform data into a null terminated base64 string.
105
106TString TBase64::Encode(const char *data)
107{
108 return Encode(data, strlen(data));
109}
110
111////////////////////////////////////////////////////////////////////////////////
112/// Transform len bytes from data into a null terminated base64 string.
113
114TString TBase64::Encode(const char *data, Int_t len)
115{
116 TString ret((len < 8) ? 16 : (int) (len * 1.25 + 8)); // every 3 bytes coded in 4 base64
117
118 char oo[4];
119 for (int i = 0; i < len; i += 3) {
120 ToB64low(data+i, oo, len-i);
121 ret.Append(oo, 4);
122 }
123 return ret;
124}
125
126////////////////////////////////////////////////////////////////////////////////
127/// Decode a base64 string date into a generic TString.
128/// No check for base64-ness of input characters.
129
130TString TBase64::Decode(const char *data)
131{
132 int len = strlen(data);
133 TString ret(len);
134
135 for (int i = 0; i < len; i += 4)
136 FromB64low(data+i, ret);
137
138 return ret;
139}
#define R__likely(expr)
Definition RConfig.hxx:587
char * ret
Definition Rotated.cxx:221
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
Definition RtypesCore.h:60
static void FromB64low(const char *in, TString &out)
Base64 decoding of 4 bytes from in.
Definition TBase64.cxx:66
static void ToB64low(const char *in, char *out, int mod)
Base64 encoding of 3 bytes from in.
Definition TBase64.cxx:30
static TString Decode(const char *data)
Decode a base64 string date into a generic TString.
Definition TBase64.cxx:130
static TString Encode(const char *data)
Transform data into a null terminated base64 string.
Definition TBase64.cxx:106
Basic string class.
Definition TString.h:138
TString & Append(const char *cs)
Definition TString.h:581