1
0

base64.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include "base/base64.h"
  24. namespace cocos2d {
  25. unsigned char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  26. int _base64Decode(const unsigned char *input, unsigned int input_len, unsigned char *output, unsigned int *output_len )
  27. {
  28. static char inalphabet[256], decoder[256];
  29. int i, bits, c = 0, char_count, errors = 0;
  30. unsigned int input_idx = 0;
  31. unsigned int output_idx = 0;
  32. for (i = (sizeof alphabet) - 1; i >= 0 ; i--) {
  33. inalphabet[alphabet[i]] = 1;
  34. decoder[alphabet[i]] = i;
  35. }
  36. char_count = 0;
  37. bits = 0;
  38. for( input_idx=0; input_idx < input_len ; input_idx++ ) {
  39. c = input[ input_idx ];
  40. if (c == '=')
  41. break;
  42. if (c > 255 || ! inalphabet[c])
  43. continue;
  44. bits += decoder[c];
  45. char_count++;
  46. if (char_count == 4) {
  47. output[ output_idx++ ] = (bits >> 16);
  48. output[ output_idx++ ] = ((bits >> 8) & 0xff);
  49. output[ output_idx++ ] = ( bits & 0xff);
  50. bits = 0;
  51. char_count = 0;
  52. } else {
  53. bits <<= 6;
  54. }
  55. }
  56. if( c == '=' ) {
  57. switch (char_count) {
  58. case 1:
  59. #if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
  60. fprintf(stderr, "base64Decode: encoding incomplete: at least 2 bits missing");
  61. #endif
  62. errors++;
  63. break;
  64. case 2:
  65. output[ output_idx++ ] = ( bits >> 10 );
  66. break;
  67. case 3:
  68. output[ output_idx++ ] = ( bits >> 16 );
  69. output[ output_idx++ ] = (( bits >> 8 ) & 0xff);
  70. break;
  71. }
  72. } else if ( input_idx < input_len ) {
  73. if (char_count) {
  74. #if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
  75. fprintf(stderr, "base64 encoding incomplete: at least %d bits truncated",
  76. ((4 - char_count) * 6));
  77. #endif
  78. errors++;
  79. }
  80. }
  81. *output_len = output_idx;
  82. return errors;
  83. }
  84. void _base64Encode( const unsigned char *input, unsigned int input_len, char *output )
  85. {
  86. unsigned int char_count;
  87. unsigned int bits;
  88. unsigned int input_idx = 0;
  89. unsigned int output_idx = 0;
  90. char_count = 0;
  91. bits = 0;
  92. for( input_idx=0; input_idx < input_len ; input_idx++ ) {
  93. bits |= input[ input_idx ];
  94. char_count++;
  95. if (char_count == 3) {
  96. output[ output_idx++ ] = alphabet[(bits >> 18) & 0x3f];
  97. output[ output_idx++ ] = alphabet[(bits >> 12) & 0x3f];
  98. output[ output_idx++ ] = alphabet[(bits >> 6) & 0x3f];
  99. output[ output_idx++ ] = alphabet[bits & 0x3f];
  100. bits = 0;
  101. char_count = 0;
  102. } else {
  103. bits <<= 8;
  104. }
  105. }
  106. if (char_count) {
  107. if (char_count == 1) {
  108. bits <<= 8;
  109. }
  110. output[ output_idx++ ] = alphabet[(bits >> 18) & 0x3f];
  111. output[ output_idx++ ] = alphabet[(bits >> 12) & 0x3f];
  112. if (char_count > 1) {
  113. output[ output_idx++ ] = alphabet[(bits >> 6) & 0x3f];
  114. } else {
  115. output[ output_idx++ ] = '=';
  116. }
  117. output[ output_idx++ ] = '=';
  118. }
  119. output[ output_idx++ ] = 0;
  120. }
  121. int base64Decode(const unsigned char *in, unsigned int inLength, unsigned char **out)
  122. {
  123. unsigned int outLength = 0;
  124. //should be enough to store 6-bit buffers in 8-bit buffers
  125. *out = (unsigned char*)malloc(inLength / 4 * 3 + 1);
  126. if( *out ) {
  127. int ret = _base64Decode(in, inLength, *out, &outLength);
  128. if (ret > 0 )
  129. {
  130. #if (CC_TARGET_PLATFORM != CC_PLATFORM_BADA)
  131. printf("Base64Utils: error decoding");
  132. #endif
  133. free(*out);
  134. *out = nullptr;
  135. outLength = 0;
  136. }
  137. }
  138. return outLength;
  139. }
  140. int base64Encode(const unsigned char *in, unsigned int inLength, char **out) {
  141. unsigned int outLength = (inLength + 2) / 3 * 4;
  142. //should be enough to store 8-bit buffers in 6-bit buffers
  143. *out = (char*)malloc(outLength+1);
  144. if( *out ) {
  145. _base64Encode(in, inLength, *out);
  146. }
  147. return outLength;
  148. }
  149. }//namespace cocos2d