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+
9+ #import " NSDataEx.h"
10+
11+ static char encodingTable[64 ] = {
12+ ' A' ,' B' ,' C' ,' D' ,' E' ,' F' ,' G' ,' H' ,' I' ,' J' ,' K' ,' L' ,' M' ,' N' ,' O' ,' P' ,
13+ ' Q' ,' R' ,' S' ,' T' ,' U' ,' V' ,' W' ,' X' ,' Y' ,' Z' ,' a' ,' b' ,' c' ,' d' ,' e' ,' f' ,
14+ ' g' ,' h' ,' i' ,' j' ,' k' ,' l' ,' m' ,' n' ,' o' ,' p' ,' q' ,' r' ,' s' ,' t' ,' u' ,' v' ,
15+ ' w' ,' x' ,' y' ,' z' ,' 0' ,' 1' ,' 2' ,' 3' ,' 4' ,' 5' ,' 6' ,' 7' ,' 8' ,' 9' ,' +' ,' /' };
16+
17+ @implementation NSData (NSDataBase64Additions)
18+
19+ + (NSData *) dataWithBase64EncodedString : (NSString *) string {
20+ NSData *result = [[NSData alloc ] initWithBase64EncodedString: string];
21+ return result;
22+ }
23+
24+ - (id ) initWithBase64EncodedString : (NSString *) string {
25+ NSMutableData *mutableData = nil ;
26+
27+ if ( string ) {
28+ unsigned long ixtext = 0 ;
29+ unsigned long lentext = 0 ;
30+ unsigned char ch = 0 ;
31+ unsigned char inbuf[4 ], outbuf[3 ];
32+ short i = 0 , ixinbuf = 0 ;
33+ BOOL flignore = NO ;
34+ BOOL flendtext = NO ;
35+ NSData *base64Data = nil ;
36+ const unsigned char *base64Bytes = nil ;
37+
38+ // Convert the string to ASCII data.
39+ base64Data = [string dataUsingEncoding: NSASCIIStringEncoding];
40+ base64Bytes = [base64Data bytes ];
41+ mutableData = [NSMutableData dataWithCapacity: [base64Data length ]];
42+ lentext = [base64Data length ];
43+
44+ while ( YES ) {
45+ if ( ixtext >= lentext ) break ;
46+ ch = base64Bytes[ixtext++];
47+ flignore = NO ;
48+
49+ if ( ( ch >= ' A' ) && ( ch <= ' Z' ) ) ch = ch - ' A' ;
50+ else if ( ( ch >= ' a' ) && ( ch <= ' z' ) ) ch = ch - ' a' + 26 ;
51+ else if ( ( ch >= ' 0' ) && ( ch <= ' 9' ) ) ch = ch - ' 0' + 52 ;
52+ else if ( ch == ' +' ) ch = 62 ;
53+ else if ( ch == ' =' ) flendtext = YES ;
54+ else if ( ch == ' /' ) ch = 63 ;
55+ else flignore = YES ;
56+
57+ if ( ! flignore ) {
58+ short ctcharsinbuf = 3 ;
59+ BOOL flbreak = NO ;
60+
61+ if ( flendtext ) {
62+ if ( ! ixinbuf ) break ;
63+ if ( ( ixinbuf == 1 ) || ( ixinbuf == 2 ) ) ctcharsinbuf = 1 ;
64+ else ctcharsinbuf = 2 ;
65+ ixinbuf = 3 ;
66+ flbreak = YES ;
67+ }
68+
69+ inbuf [ixinbuf++] = ch;
70+
71+ if ( ixinbuf == 4 ) {
72+ ixinbuf = 0 ;
73+ outbuf [0 ] = ( inbuf[0 ] << 2 ) | ( ( inbuf[1 ] & 0x30 ) >> 4 );
74+ outbuf [1 ] = ( ( inbuf[1 ] & 0x0F ) << 4 ) | ( ( inbuf[2 ] & 0x3C ) >> 2 );
75+ outbuf [2 ] = ( ( inbuf[2 ] & 0x03 ) << 6 ) | ( inbuf[3 ] & 0x3F );
76+
77+ for ( i = 0 ; i < ctcharsinbuf; i++ )
78+ [mutableData appendBytes: &outbuf[i] length: 1 ];
79+ }
80+
81+ if ( flbreak ) break ;
82+ }
83+ }
84+ }
85+
86+ self = [self initWithData: mutableData];
87+ return self;
88+ }
89+
90+ #pragma mark -
91+
92+ - (NSString *) base64Encoding {
93+ return [self base64EncodingWithLineLength: 0 ];
94+ }
95+
96+ - (NSString *) base64EncodingWithLineLength : (unsigned int ) lineLength {
97+ const unsigned char *bytes = [self bytes ];
98+ NSMutableString *result = [NSMutableString stringWithCapacity: [self length ]];
99+ unsigned long ixtext = 0 ;
100+ unsigned long lentext = [self length ];
101+ long ctremaining = 0 ;
102+ unsigned char inbuf[3 ], outbuf[4 ];
103+ short i = 0 ;
104+ short charsonline = 0 , ctcopy = 0 ;
105+ unsigned long ix = 0 ;
106+
107+ while ( YES ) {
108+ ctremaining = lentext - ixtext;
109+ if ( ctremaining <= 0 ) break ;
110+
111+ for ( i = 0 ; i < 3 ; i++ ) {
112+ ix = ixtext + i;
113+ if ( ix < lentext ) inbuf[i] = bytes[ix];
114+ else inbuf [i] = 0 ;
115+ }
116+
117+ outbuf [0 ] = (inbuf [0 ] & 0xFC ) >> 2 ;
118+ outbuf [1 ] = ((inbuf [0 ] & 0x03 ) << 4 ) | ((inbuf [1 ] & 0xF0 ) >> 4 );
119+ outbuf [2 ] = ((inbuf [1 ] & 0x0F ) << 2 ) | ((inbuf [2 ] & 0xC0 ) >> 6 );
120+ outbuf [3 ] = inbuf [2 ] & 0x3F ;
121+ ctcopy = 4 ;
122+
123+ switch ( ctremaining ) {
124+ case 1 :
125+ ctcopy = 2 ;
126+ break ;
127+ case 2 :
128+ ctcopy = 3 ;
129+ break ;
130+ }
131+
132+ for ( i = 0 ; i < ctcopy; i++ )
133+ [result appendFormat: @" %c " , encodingTable[outbuf[i]]];
134+
135+ for ( i = ctcopy; i < 4 ; i++ )
136+ [result appendFormat: @" %c " ,' =' ];
137+
138+ ixtext += 3 ;
139+ charsonline += 4 ;
140+
141+ if ( lineLength > 0 ) {
142+ if (charsonline >= lineLength) {
143+ charsonline = 0 ;
144+ [result appendString: @" \n " ];
145+ }
146+ }
147+ }
148+
149+ return result;
150+ }
151+
152+
153+
154+ // - (NSString*) urlEncodedString
155+ // {
156+ // char *hex = "0123456789ABCDEF";
157+ // unsigned char* data = (unsigned char*)[self bytes];
158+ // int len = [self length];
159+ // //NSLog(@"len = %d", len);
160+ // NSMutableString* s = [NSMutableString string];
161+ // for(int i = 0;i<len;i++){
162+ // unsigned char c = data[i];
163+ // if( ('a' <= c && c <= 'z')
164+ // || ('A' <= c && c <= 'Z')
165+ // || ('0' <= c && c <= '9') ){
166+ // NSString* ts = [[NSString alloc] initWithCString:(char *)&c length:1];
167+ //
168+ // [s appendString:ts];
169+ // [ts release];
170+ // } else {
171+ // [s appendString:@"%"];
172+ // char ts1 = hex[c >> 4];
173+ // // NSLog(@"ts = %c", ts1);
174+ // NSString* ts = [[NSString alloc] initWithCString:&ts1 length:1];
175+ // [s appendString:ts];
176+ // [ts release];
177+ // char ts2 = hex[c & 15];
178+ // ts = [[NSString alloc] initWithCString:&ts2 length:1];
179+ // [s appendString:ts];
180+ // [ts release];
181+ //
182+ // }
183+ // }
184+ // return s;
185+ // }
186+
187+ @end
0 commit comments