fixed issues #1. thanks @jasperblues
fixed bug in Base64 encode. (encode empty data) clean up hex encode/decode.
This commit is contained in:
parent
feb4413b8a
commit
19919aa0e0
@ -581,7 +581,7 @@
|
|||||||
{
|
{
|
||||||
static const char lookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
static const char lookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
|
||||||
NSUInteger inputLength = [data length];
|
long long inputLength = [data length];
|
||||||
const unsigned char *inputBytes = [data bytes];
|
const unsigned char *inputBytes = [data bytes];
|
||||||
|
|
||||||
long long maxOutputLength = (inputLength / 3 + 1) * 4;
|
long long maxOutputLength = (inputLength / 3 + 1) * 4;
|
||||||
@ -604,7 +604,7 @@
|
|||||||
outputBytes[outputLength++] = lookup[(inputBytes[index] & 0xFC) >> 2];
|
outputBytes[outputLength++] = lookup[(inputBytes[index] & 0xFC) >> 2];
|
||||||
outputBytes[outputLength++] = lookup[((inputBytes[index] & 0x03) << 4) | ((inputBytes[index + 1] & 0xF0) >> 4)];
|
outputBytes[outputLength++] = lookup[((inputBytes[index] & 0x03) << 4) | ((inputBytes[index + 1] & 0xF0) >> 4)];
|
||||||
outputBytes[outputLength++] = lookup[(inputBytes[index + 1] & 0x0F) << 2];
|
outputBytes[outputLength++] = lookup[(inputBytes[index + 1] & 0x0F) << 2];
|
||||||
outputBytes[outputLength++] = '=';
|
outputBytes[outputLength++] = '=';
|
||||||
}
|
}
|
||||||
else if (index == inputLength - 1)
|
else if (index == inputLength - 1)
|
||||||
{
|
{
|
||||||
@ -629,8 +629,10 @@
|
|||||||
// convert NSData to hex string
|
// convert NSData to hex string
|
||||||
- (NSString *)hex:(NSData *)data useLower:(BOOL)isOutputLower
|
- (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' };
|
if (data.length == 0) { return nil; }
|
||||||
static const char HexEncodeChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
|
|
||||||
|
static const char HexEncodeCharsLower[] = "0123456789abcdef";
|
||||||
|
static const char HexEncodeChars[] = "0123456789ABCDEF";
|
||||||
char *resultData;
|
char *resultData;
|
||||||
// malloc result data
|
// malloc result data
|
||||||
resultData = malloc([data length] * 2 +1);
|
resultData = malloc([data length] * 2 +1);
|
||||||
@ -717,6 +719,8 @@
|
|||||||
}
|
}
|
||||||
- (NSData *)hex:(NSString *)data
|
- (NSData *)hex:(NSString *)data
|
||||||
{
|
{
|
||||||
|
if (data.length == 0) { return nil; }
|
||||||
|
|
||||||
static const unsigned char HexDecodeChars[] =
|
static const unsigned char HexDecodeChars[] =
|
||||||
{
|
{
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
|
|||||||
@ -32,9 +32,13 @@
|
|||||||
// HEX
|
// HEX
|
||||||
STAssertEqualObjects([encoder hex:[decoder hex:@"CC0A69779E15780ADAE46C45EB451A23"] useLower:false],
|
STAssertEqualObjects([encoder hex:[decoder hex:@"CC0A69779E15780ADAE46C45EB451A23"] useLower:false],
|
||||||
@"CC0A69779E15780ADAE46C45EB451A23", nil);
|
@"CC0A69779E15780ADAE46C45EB451A23", nil);
|
||||||
|
STAssertNil([encoder hex:[NSData new] useLower:YES], nil);
|
||||||
|
STAssertNil([decoder hex:@""], nil);
|
||||||
|
|
||||||
// Base64
|
// Base64
|
||||||
STAssertEqualObjects([encoder base64:[decoder base64:@"zT1PS64MnXIUDCUiy13RRg=="]], @"zT1PS64MnXIUDCUiy13RRg==", nil);
|
STAssertEqualObjects([encoder base64:[decoder base64:@"zT1PS64MnXIUDCUiy13RRg=="]], @"zT1PS64MnXIUDCUiy13RRg==", nil);
|
||||||
|
STAssertNil([encoder base64:[NSData new]], nil);
|
||||||
|
STAssertNil([decoder base64:@""], nil);
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)testAES
|
- (void)testAES
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user