fixed issues #1. thanks @jasperblues

fixed bug in Base64 encode. (encode empty data)
clean up hex encode/decode.
This commit is contained in:
Kelp 2013-03-25 15:11:48 +08:00
parent feb4413b8a
commit 19919aa0e0
2 changed files with 12 additions and 4 deletions

View File

@ -581,7 +581,7 @@
{
static const char lookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
NSUInteger inputLength = [data length];
long long inputLength = [data length];
const unsigned char *inputBytes = [data bytes];
long long maxOutputLength = (inputLength / 3 + 1) * 4;
@ -604,7 +604,7 @@
outputBytes[outputLength++] = lookup[(inputBytes[index] & 0xFC) >> 2];
outputBytes[outputLength++] = lookup[((inputBytes[index] & 0x03) << 4) | ((inputBytes[index + 1] & 0xF0) >> 4)];
outputBytes[outputLength++] = lookup[(inputBytes[index + 1] & 0x0F) << 2];
outputBytes[outputLength++] = '=';
outputBytes[outputLength++] = '=';
}
else if (index == inputLength - 1)
{
@ -629,8 +629,10 @@
// convert NSData to hex string
- (NSString *)hex:(NSData *)data useLower:(BOOL)isOutputLower
{
static const char HexEncodeCharsLower[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
static const char HexEncodeChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
if (data.length == 0) { return nil; }
static const char HexEncodeCharsLower[] = "0123456789abcdef";
static const char HexEncodeChars[] = "0123456789ABCDEF";
char *resultData;
// malloc result data
resultData = malloc([data length] * 2 +1);
@ -717,6 +719,8 @@
}
- (NSData *)hex:(NSString *)data
{
if (data.length == 0) { return nil; }
static const unsigned char HexDecodeChars[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

View File

@ -32,9 +32,13 @@
// HEX
STAssertEqualObjects([encoder hex:[decoder hex:@"CC0A69779E15780ADAE46C45EB451A23"] useLower:false],
@"CC0A69779E15780ADAE46C45EB451A23", nil);
STAssertNil([encoder hex:[NSData new] useLower:YES], nil);
STAssertNil([decoder hex:@""], nil);
// Base64
STAssertEqualObjects([encoder base64:[decoder base64:@"zT1PS64MnXIUDCUiy13RRg=="]], @"zT1PS64MnXIUDCUiy13RRg==", nil);
STAssertNil([encoder base64:[NSData new]], nil);
STAssertNil([decoder base64:@""], nil);
}
- (void)testAES