views:

204

answers:

1

I am passing a byte array from a java server to an iPad client in XML. The server is using xstream to convert the byte array to XML with the EncodedByteArrayConverter, which should convert the array to Base 64. Using xstream, I can decode the xml back to the proper byte array in a java client, but in the iPad client, I'm getting an invalid length error. To do my decoding, I'm using the code at the bottom of this page. The length of the string is indeed not a multiple of 4, so there must be something strange with my string - although since xstream can decode it just fine, I'm guessing there's just something I need to to on the iPad side to get it to decode. I've tried cutting off padding at the end of the string to get it down to the right size, and that does allow the decoder to work, but I end up with JPG's that have invalid headers, and are not displayable.

On the server side, I'm using the following code:

Object rtrn = getByteArray(); 
XStream xstream = new XStream();
String xml = xstream.toXML(rtrn);

On the client side, I'm calling the above decoder from the XML parsing callback like this:

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    NSLog(@"Converting data; string length: %d", [string length]);
    //NSLog(@"%@", string);
    NSData *data = [Base64 decode:string];
    NSLog(@"converted data length: %d", [data length]);
}

Any ideas what could be going wrong?

A: 

I found out that it was a difference between the Base64 decoder that I found on the internet, and the encoder that XStream used. I reworked the decoder to match the one in the XStream implementation, and this one seems to work great. I wrote this very quickly, so there may be bugs/inefficiencies. This is based directly on the XStream Base64 implementation, which is available in the XStream source code.

@implementation XStreamBase64

#define ArrayLength(x) (sizeof(x)/sizeof(*(x)))

static char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static char decodingTable[123];
static int decIdx = 0;

+ (void) initialize {
    if (self == [XStreamBase64 class]) {
        memset(decodingTable, 0, ArrayLength(decodingTable));
        for (NSInteger i = 0; i < ArrayLength(encodingTable); i++) {
            decodingTable[encodingTable[i]] = i+1;
        }
    }
}

+(NSData*) decode:(NSString*)string
{
    NSInteger outputLength = string.length * 3 / 4;
    NSMutableData *data = [NSMutableData dataWithLength:outputLength];
    uint8_t *output = data.mutableBytes;

    decIdx = 0;
    int oIdx = 0;

    for(int i = 0;i < string.length;i+=4)
    {
        int a[4];
        a[0] = [self mapCharToInt:string];
        a[1] = [self mapCharToInt:string];
        a[2] = [self mapCharToInt:string];
        a[3] = [self mapCharToInt:string];

        int oneBigNumber = (a[0] & 0x3f) << 18 | (a[1] & 0x3f) << 12 | (a[2] & 0x3f) << 6 | (a[3] & 0x3f);
        for(int j = 0;j < 3;j++)
        {
            if(a[j + 1] >= 0)
            {
                output[oIdx] = 0xff & oneBigNumber >> 8 * (2 - j);
                oIdx++;
            }
        }        
    }

    return data;
}

+(int) mapCharToInt:(NSString*)string 
{
    while(decIdx < string.length)
    {
        int c = [string characterAtIndex:decIdx];
        int result = decodingTable[c];
        decIdx++;
        if(result != 0) return result - 1;
        if(c == '=') return -1;
    }

    return -1;
}
@end
Matt McMinn