From a2a8d1b068db4ff8e102ce6931191064a2246fad Mon Sep 17 00:00:00 2001 From: Bibiko Date: Thu, 24 Sep 2009 12:51:08 +0000 Subject: =?UTF-8?q?=E2=80=A2=20eliminated=20the=20deprecated=20method=20's?= =?UTF-8?q?tringWithCString'=20and=20simplified=20the=20method=20'dataToFo?= =?UTF-8?q?rmattedHexString'=20a=20bit=20(this=20fixes=20the=20issue=20for?= =?UTF-8?q?=20broken=20lines=20as=20well=20-=20seen=20under=20OSX=2010.6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/SPDataAdditions.m | 73 ++++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 46 deletions(-) (limited to 'Source/SPDataAdditions.m') diff --git a/Source/SPDataAdditions.m b/Source/SPDataAdditions.m index 1af06f5b..7069866c 100644 --- a/Source/SPDataAdditions.m +++ b/Source/SPDataAdditions.m @@ -267,23 +267,22 @@ static char base64encodingTable[64] = { returns the hex representation of the given data */ { - unsigned i; - unsigned totalLength = [self length]; - int bytesPerLine = 16; + NSUInteger i, j; + NSUInteger totalLength = [self length]; + NSUInteger bytesPerLine = 16; NSMutableString *retVal = [NSMutableString string]; - char *nodisplay = "\t\n\r\f"; - + // get the length of the longest location - int longest = [(NSString *)[NSString stringWithFormat:@"%X", totalLength - ( totalLength % bytesPerLine )] length]; - + NSUInteger longest = [(NSString *)[NSString stringWithFormat:@"%X", totalLength - ( totalLength % bytesPerLine )] length]; + for ( i = 0; i < totalLength; i += bytesPerLine ) { - int j; + NSMutableString *hex = [[NSMutableString alloc] initWithCapacity:(3 * bytesPerLine - 1)]; NSMutableString *location = [[NSMutableString alloc] initWithCapacity:(longest + 2)]; - NSMutableString *chars = [[NSMutableString alloc] init]; + unsigned char *buffer; - int buffLength = bytesPerLine; - + NSUInteger buffLength = bytesPerLine; + // add hex value of location [location appendString:[NSString stringWithFormat:@"%X", i]]; @@ -296,56 +295,38 @@ static char base64encodingTable[64] = { if ( i + buffLength >= totalLength ) { buffLength = totalLength - i; } - buffer = (unsigned char*) malloc( sizeof( unsigned char ) * buffLength ); - NSRange range = { i, buffLength }; - [self getBytes:buffer range:range]; + + buffer = (unsigned char*) malloc( sizeof( unsigned char ) * buffLength + 1); + + [self getBytes:buffer range:NSMakeRange(i, buffLength)]; // build the hex string for ( j = 0; j < buffLength; j++ ) { - unsigned char byte = *(buffer + j); - if ( byte < 16 ) { - [hex appendString:@"0"]; - } - [hex appendString:[NSString stringWithFormat:@"%X", byte]]; - [hex appendString:@" "]; - - // if the char is undisplayable, replace it with "." - unsigned char current; - int count = 0; - while ( ( current = *(nodisplay + count++) ) > 0 ) { - if ( current == byte ) { - *(buffer + j) = '.'; - break; - } - } + + [hex appendString:[NSString stringWithFormat:@"%02X ", *(buffer + j)]]; + + // Replace non-displayed bytes by '.' + // non-displayed bytes are all bytes whose hex code is less than 0x20 + if(*(buffer + j) < ' ') *(buffer + j) = '.'; + } - + // Create a NULL-terminated buffer for [NSString stringWithFormat:@"%s"] + *(buffer + j) = '\0'; + // add padding to missing hex values. for ( j = 0; j < bytesPerLine - buffLength; j++ ) { [hex appendString:@" "]; } - - // remove extra ghost characters - [chars appendString:[NSString stringWithCString:(char *)buffer]]; - if ( [chars length] > bytesPerLine ) { - [chars deleteCharactersInRange:NSMakeRange( bytesPerLine, [chars length] - bytesPerLine )]; - } - + // build line - [retVal appendString:location]; - [retVal appendString:@" "]; - [retVal appendString:hex]; - [retVal appendString:@" "]; - [retVal appendString:chars]; - [retVal appendString:@"\n"]; + [retVal appendFormat:@"%@ %@ %@\n", location, hex, [NSString stringWithFormat:@"%s", buffer]]; // clean up [hex release]; - [chars release]; [location release]; free( buffer ); } - + return retVal; } -- cgit v1.2.3