NSData+Common.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // NSData+Common.m
  3. // CommonLibrary
  4. //
  5. // Created by Alexi on 13-11-6.
  6. // Copyright (c) 2013年 ywchen. All rights reserved.
  7. //
  8. //#if kSupportNSDataCommon
  9. #import "NSData+Common.h"
  10. #import <CommonCrypto/CommonDigest.h>
  11. #define xx 65
  12. //
  13. // Mapping from ASCII character to 6 bit pattern.
  14. //
  15. static unsigned char base64EncodeLookup[256] =
  16. {
  17. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
  18. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
  19. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 62, xx, xx, xx, 63,
  20. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, xx, xx, xx, xx, xx, xx,
  21. xx, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  22. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, xx, xx, xx, xx, xx,
  23. xx, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  24. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, xx, xx, xx, xx, xx,
  25. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
  26. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
  27. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
  28. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
  29. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
  30. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
  31. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
  32. xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx,
  33. };
  34. //
  35. // Fundamental sizes of the binary and base64 encode/decode units in bytes
  36. //
  37. #define BINARY_UNIT_SIZE 3
  38. #define BASE64_UNIT_SIZE 4
  39. void *NewBase64Decode(const char *inputBuffer, size_t length, size_t *outputLength)
  40. {
  41. if (length == (size_t)-1)
  42. {
  43. length = strlen(inputBuffer);
  44. }
  45. size_t outputBufferSize =
  46. ((length+BASE64_UNIT_SIZE-1) / BASE64_UNIT_SIZE) * BINARY_UNIT_SIZE;
  47. unsigned char *outputBuffer = (unsigned char *)malloc(outputBufferSize);
  48. size_t i = 0;
  49. size_t j = 0;
  50. while (i < length)
  51. {
  52. //
  53. // Accumulate 4 valid characters (ignore everything else)
  54. //
  55. unsigned char accumulated[BASE64_UNIT_SIZE];
  56. size_t accumulateIndex = 0;
  57. while (i < length)
  58. {
  59. unsigned char decode = base64EncodeLookup[inputBuffer[i++]];
  60. if (decode != xx)
  61. {
  62. accumulated[accumulateIndex] = decode;
  63. accumulateIndex++;
  64. if (accumulateIndex == BASE64_UNIT_SIZE)
  65. {
  66. break;
  67. }
  68. }
  69. }
  70. //
  71. // Store the 6 bits from each of the 4 characters as 3 bytes
  72. //
  73. // (Uses improved bounds checking suggested by Alexandre Colucci)
  74. //
  75. if(accumulateIndex >= 2)
  76. outputBuffer[j] = (unsigned char)(accumulated[0] << 2) | (accumulated[1] >> 4);
  77. if(accumulateIndex >= 3)
  78. outputBuffer[j + 1] = (unsigned char)(accumulated[1] << 4) | (accumulated[2] >> 2);
  79. if(accumulateIndex >= 4)
  80. outputBuffer[j + 2] = (unsigned char)(accumulated[2] << 6) | accumulated[3];
  81. j += accumulateIndex - 1;
  82. }
  83. if (outputLength)
  84. {
  85. *outputLength = j;
  86. }
  87. return outputBuffer;
  88. }
  89. //
  90. // NewBase64Encode
  91. //
  92. // Encodes the arbitrary data in the inputBuffer as base64 into a newly malloced
  93. // output buffer.
  94. //
  95. // inputBuffer - the source data for the encode
  96. // length - the length of the input in bytes
  97. // separateLines - if zero, no CR/LF characters will be added. Otherwise
  98. // a CR/LF pair will be added every 64 encoded chars.
  99. // outputLength - if not-NULL, on output will contain the encoded length
  100. // (not including terminating 0 char)
  101. //
  102. // returns the encoded buffer. Must be free'd by caller. Length is given by
  103. // outputLength.
  104. //
  105. char *NewBase64Encode(const void *buffer, size_t length, bool separateLines, size_t *outputLength)
  106. {
  107. const unsigned char *inputBuffer = (const unsigned char *)buffer;
  108. #define MAX_NUM_PADDING_CHARS 2
  109. #define OUTPUT_LINE_LENGTH 64
  110. #define INPUT_LINE_LENGTH ((OUTPUT_LINE_LENGTH / BASE64_UNIT_SIZE) * BINARY_UNIT_SIZE)
  111. #define CR_LF_SIZE 2
  112. //
  113. // Byte accurate calculation of final buffer size
  114. //
  115. size_t outputBufferSize =
  116. ((length / BINARY_UNIT_SIZE)
  117. + ((length % BINARY_UNIT_SIZE) ? 1 : 0))
  118. * BASE64_UNIT_SIZE;
  119. if (separateLines)
  120. {
  121. outputBufferSize +=
  122. (outputBufferSize / OUTPUT_LINE_LENGTH) * CR_LF_SIZE;
  123. }
  124. //
  125. // Include space for a terminating zero
  126. //
  127. outputBufferSize += 1;
  128. //
  129. // Allocate the output buffer
  130. //
  131. char *outputBuffer = (char *)malloc(outputBufferSize);
  132. if (!outputBuffer)
  133. {
  134. return NULL;
  135. }
  136. size_t i = 0;
  137. size_t j = 0;
  138. const size_t lineLength = separateLines ? INPUT_LINE_LENGTH : length;
  139. size_t lineEnd = lineLength;
  140. while (true)
  141. {
  142. if (lineEnd > length)
  143. {
  144. lineEnd = length;
  145. }
  146. for (; i + BINARY_UNIT_SIZE - 1 < lineEnd; i += BINARY_UNIT_SIZE)
  147. {
  148. //
  149. // Inner loop: turn 48 bytes into 64 base64 characters
  150. //
  151. outputBuffer[j++] = (char)base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2];
  152. outputBuffer[j++] = (char)base64EncodeLookup[((inputBuffer[i] & 0x03) << 4)
  153. | ((inputBuffer[i + 1] & 0xF0) >> 4)];
  154. outputBuffer[j++] = (char)base64EncodeLookup[((inputBuffer[i + 1] & 0x0F) << 2)
  155. | ((inputBuffer[i + 2] & 0xC0) >> 6)];
  156. outputBuffer[j++] = (char)base64EncodeLookup[inputBuffer[i + 2] & 0x3F];
  157. }
  158. if (lineEnd == length)
  159. {
  160. break;
  161. }
  162. //
  163. // Add the newline
  164. //
  165. outputBuffer[j++] = '\r';
  166. outputBuffer[j++] = '\n';
  167. lineEnd += lineLength;
  168. }
  169. if (i + 1 < length)
  170. {
  171. //
  172. // Handle the single '=' case
  173. //
  174. outputBuffer[j++] = (char)base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2];
  175. outputBuffer[j++] = (char)base64EncodeLookup[((inputBuffer[i] & 0x03) << 4)
  176. | ((inputBuffer[i + 1] & 0xF0) >> 4)];
  177. outputBuffer[j++] = (char)base64EncodeLookup[(inputBuffer[i + 1] & 0x0F) << 2];
  178. outputBuffer[j++] = '=';
  179. }
  180. else if (i < length)
  181. {
  182. //
  183. // Handle the double '=' case
  184. //
  185. outputBuffer[j++] = (char)base64EncodeLookup[(inputBuffer[i] & 0xFC) >> 2];
  186. outputBuffer[j++] = (char)base64EncodeLookup[(inputBuffer[i] & 0x03) << 4];
  187. outputBuffer[j++] = '=';
  188. outputBuffer[j++] = '=';
  189. }
  190. outputBuffer[j] = 0;
  191. //
  192. // Set the output length and return the buffer
  193. //
  194. if (outputLength)
  195. {
  196. *outputLength = j;
  197. }
  198. return outputBuffer;
  199. }
  200. @implementation NSData (Common)
  201. + (NSData *)dataFromBase64String:(NSString *)aString
  202. {
  203. NSData *data = [aString dataUsingEncoding:NSASCIIStringEncoding];
  204. size_t outputLength;
  205. void *outputBuffer = NewBase64Decode([data bytes], [data length], &outputLength);
  206. NSData *result = [NSData dataWithBytes:outputBuffer length:outputLength];
  207. free(outputBuffer);
  208. return result;
  209. }
  210. - (NSString *)base64EncodedString
  211. {
  212. size_t outputLength = 0;
  213. char *outputBuffer =
  214. NewBase64Encode([self bytes], [self length], false, &outputLength);
  215. NSString *result = [[NSString alloc] initWithBytes:outputBuffer length:outputLength encoding:NSASCIIStringEncoding];
  216. free(outputBuffer);
  217. // return CommonReturnAutoReleased(result);
  218. return result;
  219. }
  220. ///////////////////////////////////////////////////////////////////////////////////////////////////
  221. /**
  222. * Calculate an md5 hash using CC_MD5.
  223. *
  224. * @returns The md5 hash of this data.
  225. */
  226. - (NSString *)md5Hash {
  227. unsigned char result[CC_MD5_DIGEST_LENGTH];
  228. CC_MD5([self bytes], (CC_LONG)[self length], result);
  229. return [NSString stringWithFormat:
  230. @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  231. result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
  232. result[8], result[9], result[10], result[11], result[12], result[13], result[14],
  233. result[15]
  234. ];
  235. }
  236. ///////////////////////////////////////////////////////////////////////////////////////////////////
  237. /**
  238. * Calculate the SHA1 hash using CC_SHA1.
  239. *
  240. * @returns The SHA1 hash of this data.
  241. */
  242. - (NSString *)sha1Hash {
  243. unsigned char result[CC_SHA1_DIGEST_LENGTH];
  244. CC_SHA1([self bytes], (CC_LONG)[self length], result);
  245. return [NSString stringWithFormat:
  246. @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  247. result[0], result[1], result[2], result[3], result[4], result[5], result[6], result[7],
  248. result[8], result[9], result[10], result[11], result[12], result[13], result[14],
  249. result[15], result[16], result[17], result[18], result[19]
  250. ];
  251. }
  252. @end
  253. //#endif