No Description

base64.m 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * RFC 1521 base64 encoding/decoding
  3. *
  4. * Copyright (C) 2006-2007 Christophe Devine
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. //#include "stdafx.h"
  21. #include "config.h"
  22. #if defined(XYSSL_BASE64_C)
  23. #include "base64.h"
  24. static const unsigned char base64_enc_map[64] =
  25. {
  26. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  27. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  28. 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
  29. 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  30. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
  31. 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
  32. '8', '9', '+', '/'
  33. };
  34. static const unsigned char base64_dec_map[128] =
  35. {
  36. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  37. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  38. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  39. 127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
  40. 127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
  41. 54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
  42. 127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
  43. 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  44. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
  45. 25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
  46. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  47. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  48. 49, 50, 51, 127, 127, 127, 127, 127
  49. };
  50. /*
  51. * Encode a buffer into base64 format
  52. */
  53. int base64_encode( unsigned char *dst, int *dlen,
  54. unsigned char *src, int slen )
  55. {
  56. int i, n;
  57. int C1, C2, C3;
  58. unsigned char *p;
  59. if( slen == 0 )
  60. return( 0 );
  61. n = (slen << 3) / 6;
  62. switch( (slen << 3) - (n * 6) )
  63. {
  64. case 2: n += 3; break;
  65. case 4: n += 2; break;
  66. default: break;
  67. }
  68. if( *dlen < n + 1 )
  69. {
  70. *dlen = n + 1;
  71. return( XYSSL_ERR_BASE64_BUFFER_TOO_SMALL );
  72. }
  73. n = (slen / 3) * 3;
  74. for( i = 0, p = dst; i < n; i += 3 )
  75. {
  76. C1 = *src++;
  77. C2 = *src++;
  78. C3 = *src++;
  79. *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
  80. *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
  81. *p++ = base64_enc_map[(((C2 & 15) << 2) + (C3 >> 6)) & 0x3F];
  82. *p++ = base64_enc_map[C3 & 0x3F];
  83. }
  84. if( i < slen )
  85. {
  86. C1 = *src++;
  87. C2 = ((i + 1) < slen) ? *src++ : 0;
  88. *p++ = base64_enc_map[(C1 >> 2) & 0x3F];
  89. *p++ = base64_enc_map[(((C1 & 3) << 4) + (C2 >> 4)) & 0x3F];
  90. if( (i + 1) < slen )
  91. *p++ = base64_enc_map[((C2 & 15) << 2) & 0x3F];
  92. else *p++ = '=';
  93. *p++ = '=';
  94. }
  95. *dlen = p - dst;
  96. *p = 0;
  97. return( 0 );
  98. }
  99. /*
  100. * Decode a base64-formatted buffer
  101. */
  102. int base64_decode( unsigned char *dst, int *dlen,
  103. unsigned char *src, int slen )
  104. {
  105. int i, j, n;
  106. unsigned long x;
  107. unsigned char *p;
  108. for( i = j = n = 0; i < slen; i++ )
  109. {
  110. if( ( slen - i ) >= 2 &&
  111. src[i] == '\r' && src[i + 1] == '\n' )
  112. continue;
  113. if( src[i] == '\n' )
  114. continue;
  115. if( src[i] == '=' && ++j > 2 )
  116. return( XYSSL_ERR_BASE64_INVALID_CHARACTER );
  117. if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
  118. return( XYSSL_ERR_BASE64_INVALID_CHARACTER );
  119. if( base64_dec_map[src[i]] < 64 && j != 0 )
  120. return( XYSSL_ERR_BASE64_INVALID_CHARACTER );
  121. n++;
  122. }
  123. if( n == 0 )
  124. return( 0 );
  125. n = ((n * 6) + 7) >> 3;
  126. if( *dlen < n )
  127. {
  128. *dlen = n;
  129. return( XYSSL_ERR_BASE64_BUFFER_TOO_SMALL );
  130. }
  131. for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
  132. {
  133. if( *src == '\r' || *src == '\n' )
  134. continue;
  135. j -= ( base64_dec_map[*src] == 64 );
  136. x = (x << 6) | ( base64_dec_map[*src] & 0x3F );
  137. if( ++n == 4 )
  138. {
  139. n = 0;
  140. if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
  141. if( j > 1 ) *p++ = (unsigned char)( x >> 8 );
  142. if( j > 2 ) *p++ = (unsigned char)( x );
  143. }
  144. }
  145. *dlen = p - dst;
  146. return( 0 );
  147. }
  148. @implementation AlipayBase64
  149. + (NSData *)decodeString:(NSString *)string {
  150. char *dst = NULL;
  151. int dlen = 0;
  152. char *src = (char *)[string UTF8String];
  153. int slen = (int)strlen((char *)src);
  154. int ret = base64_decode((unsigned char *)dst, &dlen, (unsigned char *)src, slen);
  155. if (ret == XYSSL_ERR_BASE64_BUFFER_TOO_SMALL) {
  156. dst = (char *)malloc(dlen);
  157. ret = base64_decode((unsigned char *)dst, &dlen, (unsigned char *)src, slen);
  158. }
  159. NSData *data = [NSData dataWithBytes:dst length:dlen];
  160. if (dst != NULL) {
  161. free(dst);
  162. }
  163. return data;
  164. }
  165. @end
  166. #endif