1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
#include "sys/platform.h"
#include "framework/File.h"
#include "idlib/Base64.h"
/*
Copyright (c) 1996 Lars Wirzenius. All rights reserved.
June 14 2003: TTimo <ttimo@idsoftware.com>
modified + endian bug fixes
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=197039
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/*
============
idBase64::Encode
============
*/
static const char sixtet_to_base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void idBase64::Encode( const byte *from, int size ) {
int i, j;
unsigned int w;
byte *to;
EnsureAlloced( 4*(size+3)/3 + 2 ); // ratio and padding + trailing \0
to = data;
w = 0;
i = 0;
while (size > 0) {
w |= *from << i*8;
++from;
--size;
++i;
if (size == 0 || i == 3) {
byte out[4];
SixtetsForInt( out, w );
for (j = 0; j*6 < i*8; ++j) {
*to++ = sixtet_to_base64[ out[j] ];
}
if (size == 0) {
for (j = i; j < 3; ++j) {
*to++ = '=';
}
}
w = 0;
i = 0;
}
}
*to++ = '\0';
len = to - data;
}
/*
============
idBase64::DecodeLength
returns the minimum size in bytes of the target buffer for decoding
4 base64 digits <-> 3 bytes
============
*/
int idBase64::DecodeLength( void ) const {
return 3*len/4;
}
/*
============
idBase64::Decode
============
*/
int idBase64::Decode( byte *to ) const {
unsigned int w;
int i, j;
size_t n;
static char base64_to_sixtet[256];
static int tab_init = 0;
byte *from = data;
if (!tab_init) {
memset( base64_to_sixtet, 0, 256 );
for (i = 0; (j = sixtet_to_base64[i]) != '\0'; ++i) {
base64_to_sixtet[j] = i;
}
tab_init = 1;
}
w = 0;
i = 0;
n = 0;
byte in[4] = {0,0,0,0};
while (*from != '\0' && *from != '=' ) {
if (*from == ' ' || *from == '\n') {
++from;
continue;
}
in[i] = base64_to_sixtet[* (unsigned char *) from];
++i;
++from;
if (*from == '\0' || *from == '=' || i == 4) {
w = IntForSixtets( in );
for (j = 0; j*8 < i*6; ++j) {
*to++ = w & 0xff;
++n;
w >>= 8;
}
i = 0;
w = 0;
}
}
return n;
}
/*
============
idBase64::Encode
============
*/
void idBase64::Encode( const idStr &src ) {
Encode( (const byte *)src.c_str(), src.Length() );
}
/*
============
idBase64::Decode
============
*/
void idBase64::Decode( idStr &dest ) const {
byte *buf = new byte[ DecodeLength()+1 ]; // +1 for trailing \0
int out = Decode( buf );
buf[out] = '\0';
dest = (const char *)buf;
delete[] buf;
}
/*
============
idBase64::Decode
============
*/
void idBase64::Decode( idFile *dest ) const {
byte *buf = new byte[ DecodeLength()+1 ]; // +1 for trailing \0
int out = Decode( buf );
dest->Write( buf, out );
delete[] buf;
}
#if 0
void idBase64_TestBase64() {
idStr src;
idBase64 dest;
src = "Encode me in base64";
dest.Encode( src );
idLib::common->Printf( "%s -> %s\n", src.c_str(), dest.c_str() );
dest.Decode( src );
idLib::common->Printf( "%s -> %s\n", dest.c_str(), src.c_str() );
idDict src_dict;
src_dict.SetFloat("float", 0.5f);
src_dict.SetBool("bool", true);
src_dict.Set("value", "foo");
idFile_Memory src_fmem("serialize_dict");
src_dict.WriteToFileHandle( &src_fmem );
dest.Encode( (const byte *)src_fmem.GetDataPtr(), src_fmem.Length() );
idLib::common->Printf( "idDict encoded to %s\n", dest.c_str());
// now decode to another stream and build back
idFile_Memory dest_fmem( "build_back" );
dest.Decode( &dest_fmem );
dest_fmem.MakeReadOnly();
idDict dest_dict;
dest_dict.ReadFromFileHandle( &dest_fmem );
idLib::common->Printf( "idDict reconstructed after base64 decode\n");
dest_dict.Print();
// test idDict read from file - from python generated files, see idDict.py
idFile *file = idLib::fileSystem->OpenFileRead("idDict.test");
if (file) {
idDict test_dict;
test_dict.ReadFromFileHandle( file );
//
idLib::common->Printf( "read idDict.test:\n");
test_dict.Print();
idLib::fileSystem->CloseFile(file);
file = NULL;
} else {
idLib::common->Printf( "idDict.test not found\n" );
}
idBase64 base64_src;
void *buffer;
if ( idLib::fileSystem->ReadFile( "idDict.base64.test", &buffer ) != -1 ) {
idFile_Memory mem_src( "dict" );
idLib::common->Printf( "read: %d %s\n", idStr::Length( (char*)buffer ), buffer );
base64_src = (char *)buffer;
base64_src.Decode( &mem_src );
mem_src.MakeReadOnly();
idDict test_dict;
test_dict.ReadFromFileHandle( &mem_src );
idLib::common->Printf( "read idDict.base64.test:\n");
test_dict.Print();
idLib::fileSystem->FreeFile( buffer );
} else {
idLib::common->Printf( "idDict.base64.test not found\n" );
}
}
#endif
|