6

I've looked at numerous Stack Overflow posts on how to decode Base64 encoded strings in Objective C, but I'm still having issues with it.

Essentially, I'm trying to port a Python script to Objective C. The Python line of code is:

zlib.decompress(base64.urlsafe_b64decode(string))

When I run "string" through the Python version of base64.urlsafe_b64decode, it turns out correctly and can then be properly Zlib-decompressed. When I run the "string" through any variant of an Objective C Base64 decoder, it sort of works, but the results are not the same and the Zlib decompression fails.

Is there a difference between URL-safe Base 64 decoding and the code that's widely available here on SO? If anyone has experienced difficulties like this before, any insight on what to do is appreciated.

Thanks, SO!

Edit 1: I used the Base 64 code found here. The original data (still Base64 encoded) can be found here, the Objective-C generated can be found here, and the Python generated can be found here. Ideally, I want the Objective C code to decode to the same text as the Python script.

5
  • 2
    it might help if you post your code, the lib you use in your objective c and the outcome of the python and that of the objective c routines when given a similar input string Jun 19, 2012 at 17:47
  • What encoding method is string using? ASCII? UTF-8? Something else?
    – Dan O
    Jun 19, 2012 at 17:49
  • @orzechowskid string should be UTF-8 encoded. @Harold I'll edit the question itself with your answers momentarily.
    – Jake Wood
    Jun 19, 2012 at 17:53
  • "The original data (still Base64 encoded) can be found here" that's RFC4648 base64url encoding, which the ObjC encoder doesn't handle. It only accepts "vanilla" base64.
    – user23743
    Jun 19, 2012 at 18:28
  • 1
    @GrahamLee I actually adapted the encoding to match, but I still need to fix the decoding. Do you know of any ObjC decoders for RFC4648?
    – Jake Wood
    Jun 19, 2012 at 18:31

2 Answers 2

13

Special thanks to Graham for pointing out the RFC differences, I was able to solve the problem. If anyone in the future encounters this, here's how to solve it:

  1. Download the NSData+Base64 code from here.
  2. In NSData+Base64.m, you'll need to change the look-up tables to the following:
//
// Mapping from 6 bit pattern to ASCII character.
//
static unsigned char base64EncodeLookup[65] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

//
// Definition for "masked-out" areas of the base64DecodeLookup mapping
//
#define xx 65

//
// Mapping from ASCII character to 6 bit pattern.
//
static unsigned char base64DecodeLookup[256] =
{
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 62, xx, xx, 
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, xx, xx, xx, xx, xx, xx, 
    xx,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, xx, xx, xx, xx, 63, 
    xx, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, xx, xx, xx, xx, xx, 
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 
};

This will make Objective C's decoding equivalent to Python's.

3
  • great stuff - you just got rid of my headache
    – gheese
    Mar 21, 2013 at 12:45
  • Worked pretty well for me!
    – marsant
    Sep 26, 2014 at 5:42
  • 1
    @KrupalGhorpade this question is very old, it pre-dates iOS 7 which added this functionality built-in. I've changed the accepted answer to the now-correct one. Thanks for pointing that out!
    – Jake Wood
    Mar 29, 2021 at 22:36
12

iOS 7.0 and later has the method "base64EncodedStringWithOptions" on NSData that will do the above lookup. But this method does not return a URL safe encoded string. So one would have to do the replacements manually to get a URL safe encoded string. See below..

NSData *originalData = [originalString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [originalData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
base64String = [base64String stringByReplacingOccurrencesOfString:@"/" withString:@"_"];
base64String = [base64String stringByReplacingOccurrencesOfString:@"+" withString:@"-"];

The resultant base64String is now URL safe.

1
  • This is a nice solution since you don't need to pull in any dependancies! Sep 4, 2015 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.