Base64.cpp 5.6 KB

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