No Description

NSDataEx.m 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // NSDataEx.m
  3. // iX3.0
  4. //
  5. // Created by Feng Huajun on 09-4-16.
  6. // Copyright 2009 Infothinker. All rights reserved.
  7. //
  8. #import "NSDataEx.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 (NSDataBase64Additions)
  15. + (NSData *) dataWithBase64EncodedString:(NSString *) string {
  16. NSData *result = [[NSData alloc] initWithBase64EncodedString:string];
  17. return result;
  18. }
  19. - (id) initWithBase64EncodedString:(NSString *) string {
  20. NSMutableData *mutableData = nil;
  21. if( string ) {
  22. unsigned long ixtext = 0;
  23. unsigned long lentext = 0;
  24. unsigned char ch = 0;
  25. unsigned char inbuf[4], outbuf[3];
  26. short i = 0, ixinbuf = 0;
  27. BOOL flignore = NO;
  28. BOOL flendtext = NO;
  29. NSData *base64Data = nil;
  30. const unsigned char *base64Bytes = nil;
  31. // Convert the string to ASCII data.
  32. base64Data = [string dataUsingEncoding:NSASCIIStringEncoding];
  33. base64Bytes = [base64Data bytes];
  34. mutableData = [NSMutableData dataWithCapacity:[base64Data length]];
  35. lentext = [base64Data length];
  36. while( YES ) {
  37. if( ixtext >= lentext ) break;
  38. ch = base64Bytes[ixtext++];
  39. flignore = NO;
  40. if( ( ch >= 'A' ) && ( ch <= 'Z' ) ) ch = ch - 'A';
  41. else if( ( ch >= 'a' ) && ( ch <= 'z' ) ) ch = ch - 'a' + 26;
  42. else if( ( ch >= '0' ) && ( ch <= '9' ) ) ch = ch - '0' + 52;
  43. else if( ch == '+' ) ch = 62;
  44. else if( ch == '=' ) flendtext = YES;
  45. else if( ch == '/' ) ch = 63;
  46. else flignore = YES;
  47. if( ! flignore ) {
  48. short ctcharsinbuf = 3;
  49. BOOL flbreak = NO;
  50. if( flendtext ) {
  51. if( ! ixinbuf ) break;
  52. if( ( ixinbuf == 1 ) || ( ixinbuf == 2 ) ) ctcharsinbuf = 1;
  53. else ctcharsinbuf = 2;
  54. ixinbuf = 3;
  55. flbreak = YES;
  56. }
  57. inbuf [ixinbuf++] = ch;
  58. if( ixinbuf == 4 ) {
  59. ixinbuf = 0;
  60. outbuf [0] = ( inbuf[0] << 2 ) | ( ( inbuf[1] & 0x30) >> 4 );
  61. outbuf [1] = ( ( inbuf[1] & 0x0F ) << 4 ) | ( ( inbuf[2] & 0x3C ) >> 2 );
  62. outbuf [2] = ( ( inbuf[2] & 0x03 ) << 6 ) | ( inbuf[3] & 0x3F );
  63. for( i = 0; i < ctcharsinbuf; i++ )
  64. [mutableData appendBytes:&outbuf[i] length:1];
  65. }
  66. if( flbreak ) break;
  67. }
  68. }
  69. }
  70. self = [self initWithData:mutableData];
  71. return self;
  72. }
  73. #pragma mark -
  74. - (NSString *) base64Encoding {
  75. return [self base64EncodingWithLineLength:0];
  76. }
  77. - (NSString *) base64EncodingWithLineLength:(unsigned int) lineLength {
  78. const unsigned char *bytes = [self bytes];
  79. NSMutableString *result = [NSMutableString stringWithCapacity:[self length]];
  80. unsigned long ixtext = 0;
  81. unsigned long lentext = [self length];
  82. long ctremaining = 0;
  83. unsigned char inbuf[3], outbuf[4];
  84. short i = 0;
  85. short charsonline = 0, ctcopy = 0;
  86. unsigned long ix = 0;
  87. while( YES ) {
  88. ctremaining = lentext - ixtext;
  89. if( ctremaining <= 0 ) break;
  90. for( i = 0; i < 3; i++ ) {
  91. ix = ixtext + i;
  92. if( ix < lentext ) inbuf[i] = bytes[ix];
  93. else inbuf [i] = 0;
  94. }
  95. outbuf [0] = (inbuf [0] & 0xFC) >> 2;
  96. outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4);
  97. outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6);
  98. outbuf [3] = inbuf [2] & 0x3F;
  99. ctcopy = 4;
  100. switch( ctremaining ) {
  101. case 1:
  102. ctcopy = 2;
  103. break;
  104. case 2:
  105. ctcopy = 3;
  106. break;
  107. }
  108. for( i = 0; i < ctcopy; i++ )
  109. [result appendFormat:@"%c", encodingTable[outbuf[i]]];
  110. for( i = ctcopy; i < 4; i++ )
  111. [result appendFormat:@"%c",'='];
  112. ixtext += 3;
  113. charsonline += 4;
  114. if( lineLength > 0 ) {
  115. if (charsonline >= lineLength) {
  116. charsonline = 0;
  117. [result appendString:@"\n"];
  118. }
  119. }
  120. }
  121. return result;
  122. }
  123. //- (NSString*) urlEncodedString
  124. //{
  125. // char *hex = "0123456789ABCDEF";
  126. // unsigned char* data = (unsigned char*)[self bytes];
  127. // int len = [self length];
  128. // //NSLog(@"len = %d", len);
  129. // NSMutableString* s = [NSMutableString string];
  130. // for(int i = 0;i<len;i++){
  131. // unsigned char c = data[i];
  132. // if( ('a' <= c && c <= 'z')
  133. // || ('A' <= c && c <= 'Z')
  134. // || ('0' <= c && c <= '9') ){
  135. // NSString* ts = [[NSString alloc] initWithCString:(char *)&c length:1];
  136. //
  137. // [s appendString:ts];
  138. // [ts release];
  139. // } else {
  140. // [s appendString:@"%"];
  141. // char ts1 = hex[c >> 4];
  142. // // NSLog(@"ts = %c", ts1);
  143. // NSString* ts = [[NSString alloc] initWithCString:&ts1 length:1];
  144. // [s appendString:ts];
  145. // [ts release];
  146. // char ts2 = hex[c & 15];
  147. // ts = [[NSString alloc] initWithCString:&ts2 length:1];
  148. // [s appendString:ts];
  149. // [ts release];
  150. //
  151. // }
  152. // }
  153. // return s;
  154. //}
  155. @end