base64.cpp 5.5 KB

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