mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-23 09:04:13 +00:00
Added NSData initialisation with hex data
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@15738 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
374ed632bd
commit
460cd690de
3 changed files with 87 additions and 0 deletions
|
@ -3,6 +3,9 @@
|
|||
* Source/NSString.m: Added new MacOS-X method ...
|
||||
([MSMutableString-replaceOccurrencesOfString:withString:options:range:])
|
||||
* Source/Headers/gnustep/base/NSString.h: ditto
|
||||
* Source/Additions/GSCategories.m: Added
|
||||
([NSData-initWithHexadecimalRepresentation:])
|
||||
* Source/Headers/gnustep/base/GSCategories.h: ditto
|
||||
|
||||
2003-01-26 Adam Fedor <fedor@gnu.org>
|
||||
|
||||
|
|
|
@ -39,6 +39,7 @@
|
|||
@interface NSData (GSCategories)
|
||||
|
||||
- (NSString*) hexadecimalRepresentation;
|
||||
- (id) initWithHexadecimalRepresentation: (NSString*)string;
|
||||
- (NSData*) md5Digest;
|
||||
|
||||
@end
|
||||
|
|
|
@ -137,6 +137,89 @@
|
|||
return AUTORELEASE(string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialises the receiver with the supplied string data which contains
|
||||
* a hexadecimal coding of the bytes. The parsing of the string is
|
||||
* fairly tolerant, ignoring whitespace and permitting both upper and
|
||||
* lower case hexadecimal digits (the -hexadecimalRepresentation method
|
||||
* produces a string using only uppercase digits with no white spaqce).<br />
|
||||
* If the string does not contain one or more pairs of hexadecimal digits
|
||||
* then an exception is raised.
|
||||
*/
|
||||
- (id) initWithHexadecimalRepresentation: (NSString*)string
|
||||
{
|
||||
CREATE_AUTORELEASE_POOL(arp);
|
||||
NSData *d;
|
||||
const char *src;
|
||||
const char *end;
|
||||
unsigned char *dst;
|
||||
unsigned int pos = 0;
|
||||
unsigned char byte = 0;
|
||||
BOOL high = NO;
|
||||
|
||||
d = [string dataUsingEncoding: NSASCIIStringEncoding
|
||||
allowLossyConversion: YES];
|
||||
src = (const char*)[d bytes];
|
||||
end = src + [d length];
|
||||
dst = NSZoneMalloc(NSDefaultMallocZone(), [d length]/2 + 1);
|
||||
|
||||
while (src < end)
|
||||
{
|
||||
char c = *src++;
|
||||
unsigned char v;
|
||||
|
||||
if (isspace(c))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
v = c - '0';
|
||||
}
|
||||
else if (c >= 'A' && c <= 'F')
|
||||
{
|
||||
v = c - 'A' + 10;
|
||||
}
|
||||
else if (c >= 'a' && c <= 'f')
|
||||
{
|
||||
v = c - 'a' + 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
pos = 0;
|
||||
break;
|
||||
}
|
||||
if (high == NO)
|
||||
{
|
||||
byte = v << 4;
|
||||
high = YES;
|
||||
}
|
||||
else
|
||||
{
|
||||
byte |= v;
|
||||
high = NO;
|
||||
dst[pos++] = byte;
|
||||
}
|
||||
}
|
||||
if (pos > 0 && high == NO)
|
||||
{
|
||||
self = [self initWithBytes: dst length: pos];
|
||||
}
|
||||
else
|
||||
{
|
||||
DESTROY(self);
|
||||
}
|
||||
NSZoneFree(NSDefaultMallocZone(), dst);
|
||||
RELEASE(arp);
|
||||
if (self == nil)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"%@: invalid hexadeciaml string data",
|
||||
NSStringFromSelector(_cmd)];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
struct MD5Context
|
||||
{
|
||||
unsigned long buf[4];
|
||||
|
|
Loading…
Reference in a new issue