Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This <strong><em>solution</em></strong> should be pretty straight forward, which includes all the methods to populate the missing information. Tested and functional within the sandbox.</p> <pre><code>// single base64 character conversion static int POS(char c) { if (c&gt;='A' &amp;&amp; c&lt;='Z') return c - 'A'; if (c&gt;='a' &amp;&amp; c&lt;='z') return c - 'a' + 26; if (c&gt;='0' &amp;&amp; c&lt;='9') return c - '0' + 52; if (c == '+') return 62; if (c == '/') return 63; if (c == '=') return -1; [NSException raise:@"invalid BASE64 encoding" format:@"Invalid BASE64 encoding"]; return 0; } - (NSString *)encodeBase64:(const uint8_t *)input length:(NSInteger)length { return [NSString stringWithUTF8String:base64_encode(input, (size_t)length)]; } - (NSString *)decodeBase64:(NSString *)input length:(NSInteger *)length { size_t retLen; uint8_t *retStr = base64_decode([input UTF8String], &amp;retLen); if (length) *length = (NSInteger)retLen; NSString *st = [[[NSString alloc] initWithBytes:retStr length:retLen encoding:NSUTF8StringEncoding] autorelease]; free(retStr); // If base64_decode returns dynamically allocated memory return st; } char* base64_encode(const void* buf, size_t size) { static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; char* str = (char*) malloc((size+3)*4/3 + 1); char* p = str; unsigned char* q = (unsigned char*) buf; size_t i = 0; while(i &lt; size) { int c = q[i++]; c *= 256; if (i &lt; size) c += q[i]; i++; c *= 256; if (i &lt; size) c += q[i]; i++; *p++ = base64[(c &amp; 0x00fc0000) &gt;&gt; 18]; *p++ = base64[(c &amp; 0x0003f000) &gt;&gt; 12]; if (i &gt; size + 1) *p++ = '='; else *p++ = base64[(c &amp; 0x00000fc0) &gt;&gt; 6]; if (i &gt; size) *p++ = '='; else *p++ = base64[c &amp; 0x0000003f]; } *p = 0; return str; } void* base64_decode(const char* s, size_t* data_len_ptr) { size_t len = strlen(s); if (len % 4) [NSException raise:@"Invalid input in base64_decode" format:@"%d is an invalid length for an input string for BASE64 decoding", len]; unsigned char* data = (unsigned char*) malloc(len/4*3); int n[4]; unsigned char* q = (unsigned char*) data; for(const char*p=s; *p; ) { n[0] = POS(*p++); n[1] = POS(*p++); n[2] = POS(*p++); n[3] = POS(*p++); if (n[0]==-1 || n[1]==-1) [NSException raise:@"Invalid input in base64_decode" format:@"Invalid BASE64 encoding"]; if (n[2]==-1 &amp;&amp; n[3]!=-1) [NSException raise:@"Invalid input in base64_decode" format:@"Invalid BASE64 encoding"]; q[0] = (n[0] &lt;&lt; 2) + (n[1] &gt;&gt; 4); if (n[2] != -1) q[1] = ((n[1] &amp; 15) &lt;&lt; 4) + (n[2] &gt;&gt; 2); if (n[3] != -1) q[2] = ((n[2] &amp; 3) &lt;&lt; 6) + n[3]; q += 3; } // make sure that data_len_ptr is not null if (!data_len_ptr) [NSException raise:@"Invalid input in base64_decode" format:@"Invalid destination for output string length"]; *data_len_ptr = q-data - (n[2]==-1) - (n[3]==-1); return data; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload