
#import "ABUtil.h"
#import "base64.h"

@implementation ABUtil

+ (NSString*)b64encode:(NSData*)data
{
    const unsigned char *data_ = [data bytes];
    char *base64_ = base64_encode(data_, (int)[data length]);
    return [[[NSString alloc] initWithBytesNoCopy:base64_ length:strlen(base64_) encoding:NSASCIIStringEncoding freeWhenDone:YES] autorelease];
}

+ (NSData*)b64decode:(NSString*)base64
{
    if (base64 == NULL) return NULL;
    const char *base64_ = [base64 cStringUsingEncoding:NSASCIIStringEncoding];
    if (base64_ == NULL) return NULL;
    
    int data_length;
    unsigned char *data_ = base64_decode(base64_, &data_length);
    
	return [[[NSData alloc] initWithBytesNoCopy:data_ length:data_length freeWhenDone:YES] autorelease];
}

@end
