@@ -54,98 +54,4 @@ - (NSString *)aiRemoveColons {
5454 return [self stringByReplacingOccurrencesOfString: @" :" withString: @" " ];
5555}
5656
57- static const short _base64DecodingTable[256 ] = {
58- -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -1 , -1 , -2 , -1 , -1 , -2 , -2 ,
59- -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 ,
60- -1 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , 62 , -2 , -2 , -2 , 63 ,
61- 52 , 53 , 54 , 55 , 56 , 57 , 58 , 59 , 60 , 61 , -2 , -2 , -2 , -2 , -2 , -2 ,
62- -2 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 ,
63- 15 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 25 , -2 , -2 , -2 , -2 , -2 ,
64- -2 , 26 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 38 , 39 , 40 ,
65- 41 , 42 , 43 , 44 , 45 , 46 , 47 , 48 , 49 , 50 , 51 , -2 , -2 , -2 , -2 , -2 ,
66- -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 ,
67- -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 ,
68- -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 ,
69- -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 ,
70- -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 ,
71- -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 ,
72- -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 ,
73- -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2 , -2
74- };
75-
76- // http://stackoverflow.com/a/4727124
77- - (NSData *)aiDecodeBase64 {
78- const char *objPointer = [self cStringUsingEncoding: NSASCIIStringEncoding];
79- size_t intLength = strlen (objPointer);
80- int intCurrent;
81- int i = 0 , j = 0 , k;
82-
83- unsigned char *objResult = calloc (intLength, sizeof (unsigned char ));
84-
85- // Run through the whole string, converting as we go
86- while ( ((intCurrent = *objPointer++) != ' \0 ' ) && (intLength-- > 0 ) ) {
87- if (intCurrent == ' =' ) {
88- if (*objPointer != ' =' && ((i % 4 ) == 1 )) {// || (intLength > 0)) {
89- // the padding character is invalid at this point -- so this entire string is invalid
90- free (objResult);
91- return nil ;
92- }
93- continue ;
94- }
95-
96- intCurrent = _base64DecodingTable[intCurrent];
97- if (intCurrent == -1 ) {
98- // we're at a whitespace -- simply skip over
99- continue ;
100- } else if (intCurrent == -2 ) {
101- // we're at an invalid character
102- free (objResult);
103- return nil ;
104- }
105-
106- switch (i % 4 ) {
107- case 0 :
108- objResult[j] = intCurrent << 2 ;
109- break ;
110-
111- case 1 :
112- objResult[j++] |= intCurrent >> 4 ;
113- objResult[j] = (intCurrent & 0x0f ) << 4 ;
114- break ;
115-
116- case 2 :
117- objResult[j++] |= intCurrent >>2 ;
118- objResult[j] = (intCurrent & 0x03 ) << 6 ;
119- break ;
120-
121- case 3 :
122- objResult[j++] |= intCurrent;
123- break ;
124- }
125- i++;
126- }
127-
128- // mop things up if we ended on a boundary
129- k = j;
130- if (intCurrent == ' =' ) {
131- switch (i % 4 ) {
132- case 1 :
133- // Invalid state
134- free (objResult);
135- return nil ;
136-
137- case 2 :
138- k++;
139- // flow through
140- case 3 :
141- objResult[k] = 0 ;
142- }
143- }
144-
145- // Cleanup and setup the return NSData
146- NSData * objData = [[NSData alloc ] initWithBytes: objResult length: j];
147- free (objResult);
148- return objData;
149- }
150-
15157@end
0 commit comments