/baohebao/baohebao/Comm/NewBase64.m

http://baohebao.googlecode.com/ · Objective C · 162 lines · 120 code · 34 blank · 8 comment · 41 complexity · 53fcaffd0c41ee86ee4e14de3e6b04e7 MD5 · raw file

  1. //
  2. // Base64.m
  3. // iColgate
  4. //
  5. // Created by Altynbek Usenbekov on 9/7/10.
  6. // Copyright 2010 MagiClick. All rights reserved.
  7. //
  8. #import "NewBase64.h"
  9. static char encodingTable[64] = {
  10. 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
  11. 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
  12. 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
  13. 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' };
  14. @implementation NSData (VQBase64)
  15. - (id)initWithString:(NSString *)string {
  16. if (self = [super init]) {
  17. [self initWithBase64EncodedString:string];
  18. }
  19. return self;
  20. }
  21. + (NSData *) dataWithBase64EncodedString:(NSString *) string {
  22. return [[[NSData allocWithZone:nil] initWithBase64EncodedString:string] autorelease];
  23. }
  24. - (id) initWithBase64EncodedString:(NSString *) string {
  25. NSMutableData *mutableData = nil;
  26. if( string ) {
  27. unsigned long ixtext = 0;
  28. unsigned long lentext = 0;
  29. unsigned char ch = 0;
  30. unsigned char inbuf[4], outbuf[3];
  31. short i = 0, ixinbuf = 0;
  32. BOOL flignore = NO;
  33. BOOL flendtext = NO;
  34. NSData *base64Data = nil;
  35. const unsigned char *base64Bytes = nil;
  36. // Convert the string to ASCII data.
  37. base64Data = [string dataUsingEncoding:NSASCIIStringEncoding];
  38. base64Bytes = [base64Data bytes];
  39. mutableData = [NSMutableData dataWithCapacity:[base64Data length]];
  40. lentext = [base64Data length];
  41. while( YES ) {
  42. if( ixtext >= lentext ) break;
  43. ch = base64Bytes[ixtext++];
  44. flignore = NO;
  45. if( ( ch >= 'A' ) && ( ch <= 'Z' ) ) ch = ch - 'A';
  46. else if( ( ch >= 'a' ) && ( ch <= 'z' ) ) ch = ch - 'a' + 26;
  47. else if( ( ch >= '0' ) && ( ch <= '9' ) ) ch = ch - '0' + 52;
  48. else if( ch == '+' ) ch = 62;
  49. else if( ch == '=' ) flendtext = YES;
  50. else if( ch == '/' ) ch = 63;
  51. else flignore = YES;
  52. if( ! flignore ) {
  53. short ctcharsinbuf = 3;
  54. BOOL flbreak = NO;
  55. if( flendtext ) {
  56. if( ! ixinbuf ) break;
  57. if( ( ixinbuf == 1 ) || ( ixinbuf == 2 ) ) ctcharsinbuf = 1;
  58. else ctcharsinbuf = 2;
  59. ixinbuf = 3;
  60. flbreak = YES;
  61. }
  62. inbuf [ixinbuf++] = ch;
  63. if( ixinbuf == 4 ) {
  64. ixinbuf = 0;
  65. outbuf [0] = ( inbuf[0] << 2 ) | ( ( inbuf[1] & 0x30) >> 4 );
  66. outbuf [1] = ( ( inbuf[1] & 0x0F ) << 4 ) | ( ( inbuf[2] & 0x3C ) >> 2 );
  67. outbuf [2] = ( ( inbuf[2] & 0x03 ) << 6 ) | ( inbuf[3] & 0x3F );
  68. for( i = 0; i < ctcharsinbuf; i++ )
  69. [mutableData appendBytes:&outbuf[i] length:1];
  70. }
  71. if( flbreak ) break;
  72. }
  73. }
  74. }
  75. self = [self initWithData:mutableData];
  76. return self;
  77. }
  78. #pragma mark -
  79. - (NSString *) base64EncodingWithLineLength:(unsigned int) lineLength {
  80. const unsigned char *bytes = [self bytes];
  81. NSMutableString *result = [NSMutableString stringWithCapacity:[self length]];
  82. unsigned long ixtext = 0;
  83. unsigned long lentext = [self length];
  84. long ctremaining = 0;
  85. unsigned char inbuf[3], outbuf[4];
  86. unsigned short i = 0;
  87. unsigned short charsonline = 0, ctcopy = 0;
  88. unsigned long ix = 0;
  89. while( YES ) {
  90. ctremaining = lentext - ixtext;
  91. if( ctremaining <= 0 ) break;
  92. for( i = 0; i < 3; i++ ) {
  93. ix = ixtext + i;
  94. if( ix < lentext ) inbuf[i] = bytes[ix];
  95. else inbuf [i] = 0;
  96. }
  97. outbuf [0] = (inbuf [0] & 0xFC) >> 2;
  98. outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4);
  99. outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6);
  100. outbuf [3] = inbuf [2] & 0x3F;
  101. ctcopy = 4;
  102. switch( ctremaining ) {
  103. case 1:
  104. ctcopy = 2;
  105. break;
  106. case 2:
  107. ctcopy = 3;
  108. break;
  109. }
  110. for( i = 0; i < ctcopy; i++ )
  111. [result appendFormat:@"%c", encodingTable[outbuf[i]]];
  112. for( i = ctcopy; i < 4; i++ )
  113. [result appendString:@"="];
  114. ixtext += 3;
  115. charsonline += 4;
  116. if( lineLength > 0 ) {
  117. if( charsonline >= lineLength ) {
  118. charsonline = 0;
  119. [result appendString:@"\n"];
  120. }
  121. }
  122. }
  123. return [NSString stringWithString:result];
  124. }
  125. - (NSString *) base64Encoding{
  126. return [self base64EncodingWithLineLength:0];
  127. }
  128. @end