Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 147 lines (120 sloc) 3.92 kB
a327ea2 @andreyvit Imported official Platypus 4.0 sources.
authored
1 // Created by khammond on Mon Oct 29 2001.
2 // Formatted by Timothy Hatcher on Sun Jul 4 2004.
3 // Copyright (c) 2001 Kyle Hammond. All rights reserved.
4 // Original development by Dave Winer.
5
6 #import "NSDataAdditions.h"
7
8 static char encodingTable[64] = {
9 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
10 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
11 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
12 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' };
13
14 @implementation NSData (NSDataAdditions)
15 + (NSData *) dataWithBase64EncodedString:(NSString *) string {
16 NSData *result = [[NSData alloc] initWithBase64EncodedString:string];
17 return [result autorelease];
18 }
19
20 - (id) initWithBase64EncodedString:(NSString *) string {
21 NSMutableData *mutableData = nil;
22
23 if( string ) {
24 unsigned long ixtext = 0;
25 unsigned long lentext = 0;
26 unsigned char ch = 0;
27 unsigned char inbuf[3], outbuf[4];
28 short i = 0, ixinbuf = 0;
29 BOOL flignore = NO;
30 BOOL flendtext = NO;
31 NSData *base64Data = nil;
32 const unsigned char *base64Bytes = nil;
33
34 // Convert the string to ASCII data.
35 base64Data = [string dataUsingEncoding:NSASCIIStringEncoding];
36 base64Bytes = [base64Data bytes];
37 mutableData = [NSMutableData dataWithCapacity:[base64Data length]];
38 lentext = [base64Data length];
39
40 while( YES ) {
41 if( ixtext >= lentext ) break;
42 ch = base64Bytes[ixtext++];
43 flignore = NO;
44
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
53 if( ! flignore ) {
54 short ctcharsinbuf = 3;
55 BOOL flbreak = NO;
56
57 if( flendtext ) {
58 if( ! ixinbuf ) break;
59 if( ( ixinbuf == 1 ) || ( ixinbuf == 2 ) ) ctcharsinbuf = 1;
60 else ctcharsinbuf = 2;
61 ixinbuf = 3;
62 flbreak = YES;
63 }
64
65 inbuf [ixinbuf++] = ch;
66
67 if( ixinbuf == 4 ) {
68 ixinbuf = 0;
69 outbuf [0] = ( inbuf[0] << 2 ) | ( ( inbuf[1] & 0x30) >> 4 );
70 outbuf [1] = ( ( inbuf[1] & 0x0F ) << 4 ) | ( ( inbuf[2] & 0x3C ) >> 2 );
71 outbuf [2] = ( ( inbuf[2] & 0x03 ) << 6 ) | ( inbuf[3] & 0x3F );
72
73 for( i = 0; i < ctcharsinbuf; i++ )
74 [mutableData appendBytes:&outbuf[i] length:1];
75 }
76
77 if( flbreak ) break;
78 }
79 }
80 }
81
82 self = [self initWithData:mutableData];
83 return self;
84 }
85
86 #pragma mark -
87
88 - (NSString *) base64Encoding {
89 return [self base64EncodingWithLineLength:0];
90 }
91
92 - (NSString *) base64EncodingWithLineLength:(unsigned int) lineLength {
93 const unsigned char *bytes = [self bytes];
94 NSMutableString *result = [NSMutableString stringWithCapacity:[self length]];
95 unsigned long ixtext = 0;
96 unsigned long lentext = [self length];
97 long ctremaining = 0;
98 unsigned char inbuf[3], outbuf[4];
99 short i = 0;
100 short charsonline = 0, ctcopy = 0;
101 unsigned long ix = 0;
102
103 while( YES ) {
104 ctremaining = lentext - ixtext;
105 if( ctremaining <= 0 ) break;
106
107 for( i = 0; i < 3; i++ ) {
108 ix = ixtext + i;
109 if( ix < lentext ) inbuf[i] = bytes[ix];
110 else inbuf [i] = 0;
111 }
112
113 outbuf [0] = (inbuf [0] & 0xFC) >> 2;
114 outbuf [1] = ((inbuf [0] & 0x03) << 4) | ((inbuf [1] & 0xF0) >> 4);
115 outbuf [2] = ((inbuf [1] & 0x0F) << 2) | ((inbuf [2] & 0xC0) >> 6);
116 outbuf [3] = inbuf [2] & 0x3F;
117 ctcopy = 4;
118
119 switch( ctremaining ) {
120 case 1:
121 ctcopy = 2;
122 break;
123 case 2:
124 ctcopy = 3;
125 break;
126 }
127
128 for( i = 0; i < ctcopy; i++ )
129 [result appendFormat:@"%c", encodingTable[outbuf[i]]];
130
131 for( i = ctcopy; i < 4; i++ )
132 [result appendString:@"="];
133
134 ixtext += 3;
135 charsonline += 4;
136
137 if( lineLength > 0 ) {
138 if (charsonline >= lineLength) {
139 charsonline = 0;
140 [result appendString:@"\n"];
141 }
142 }
143 }
144
145 return [NSString stringWithString:result];
146 }
147 @end
Something went wrong with that request. Please try again.