From aa846fe6510f8717371cd1942e0920d9d54d5f63 Mon Sep 17 00:00:00 2001 From: CaS Date: Fri, 24 Jan 2003 12:06:33 +0000 Subject: [PATCH] md5 improvements and documentation tidyups git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@15688 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 10 +++++ Documentation/GNUmakefile | 2 +- Documentation/Makefile.postamble | 13 +++++-- Headers/gnustep/base/GSCategories.h | 3 +- Source/Additions/GSCategories.m | 58 +++++++++++++++++++++++++++-- Source/Additions/GSMime.m | 2 +- Source/NSString.m | 27 ++++++++++++++ 7 files changed, 105 insertions(+), 10 deletions(-) diff --git a/ChangeLog b/ChangeLog index 275024308..a2185d151 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2003-01-24 Richard Frith-Macdonald + + + * Source/Additions/GSCategories.m: Changed name of md5 digest method + and added -hexadecimalRepresentation. + * Source/Additions/GSMime.m: Update for md5 change. + * Documentation/GNUmakefile: Fixed error in installation location. + * Documentation/Makefile.postamble: Fixed to install BaseAdditions + documentation. + 2003-01-23 Richard Frith-Macdonald * Documentation/Base.gsdoc: Refer to additions. diff --git a/Documentation/GNUmakefile b/Documentation/GNUmakefile index e0fa18cfa..d7ecfcde7 100644 --- a/Documentation/GNUmakefile +++ b/Documentation/GNUmakefile @@ -79,7 +79,7 @@ ANNOUNCE_DOC_INSTALL_DIR = Developer/Base/ReleaseNotes/$(VERSION) # file using the make file ../Source/DocMakefile. If headers are added to the # library, you need to modify that file to get the autogsdoc tool to use them. # -REF_DOC_INSTALL_DIR = $(GNUSTEP_INSTALLATION_DIR)/Documentation/Developer/Base/Reference +REF_DOC_INSTALL_DIR = $(GNUSTEP_INSTALLATION_DIR)/Documentation/Developer -include Makefile.preamble diff --git a/Documentation/Makefile.postamble b/Documentation/Makefile.postamble index fc13f179d..e5e02fd68 100644 --- a/Documentation/Makefile.postamble +++ b/Documentation/Makefile.postamble @@ -53,13 +53,18 @@ endif # after-install:: ifeq ($(HAVE_LIBXML),1) - rm -rf $(REF_DOC_INSTALL_DIR) - $(MKDIRS) $(REF_DOC_INSTALL_DIR) + rm -rf $(REF_DOC_INSTALL_DIR)/Base/Reference + rm -rf $(REF_DOC_INSTALL_DIR)/BaseAdditions/Reference + $(MKDIRS) $(REF_DOC_INSTALL_DIR)/Base/Reference + $(MKDIRS) $(REF_DOC_INSTALL_DIR)/BaseAdditions/Reference (cd Base; $(TAR) cf - .) | \ - (cd $(REF_DOC_INSTALL_DIR); $(TAR) xf -) + (cd $(REF_DOC_INSTALL_DIR)/Base/Reference; $(TAR) xf -) + (cd BaseAdditions; $(TAR) cf - .) | \ + (cd $(REF_DOC_INSTALL_DIR)/BaseAdditions/Reference; $(TAR) xf -) ifneq ($(CHOWN_TO),) $(CHOWN) -R $(CHOWN_TO) \ - $(REF_DOC_INSTALL_DIR) + $(REF_DOC_INSTALL_DIR/Base) + $(REF_DOC_INSTALL_DIR/BaseAdditions) endif endif diff --git a/Headers/gnustep/base/GSCategories.h b/Headers/gnustep/base/GSCategories.h index 8c9f40ddd..107647ce8 100644 --- a/Headers/gnustep/base/GSCategories.h +++ b/Headers/gnustep/base/GSCategories.h @@ -38,7 +38,8 @@ @interface NSData (GSCategories) -- (NSData*) MD5Digest; +- (NSString*) hexadecimalRepresentation; +- (NSData*) md5Digest; @end diff --git a/Source/Additions/GSCategories.m b/Source/Additions/GSCategories.m index dcab37e08..40dfa932b 100644 --- a/Source/Additions/GSCategories.m +++ b/Source/Additions/GSCategories.m @@ -93,7 +93,49 @@ * Extension methods for the NSData class */ @implementation NSData (GSCategories) - + +/** + * Returns an NSString object containing an ASCII hexadecimal representation + * of the receiver. This means that the returned object will contain + * exactly twice as many characters as there are bytes as the receiver, + * as each byte in the receiver is represented by two hexadecimal digits.
+ * The high order four bits of each byte is encoded before the low + * order four bits. Capital letters 'A' to 'F' are used to represent + * values from 10 to 15.
+ * If you need the hexadecimal representation as raw byte data, use code + * like - + * + * hexData = [[sourceData hexadecimalRepresentation] + * dataUsingEncoding: NSASCIIStringEncoding]; + * + */ +- (NSString*) hexadecimalRepresentation +{ + static const char *hexChars = "0123456789ABCDEF"; + unsigned slen = [self length]; + unsigned dlen = slen * 2; + const unsigned char *src = (const unsigned char *)[self bytes]; + char *dst = (char*)NSZoneMalloc(NSDefaultMallocZone(), dlen); + unsigned spos = 0; + unsigned dpos = 0; + NSData *data; + NSString *string; + + while (spos < slen) + { + unsigned char c = src[spos++]; + + dst[dpos++] = hexChars[(c >> 4) & 0x0f]; + dst[dpos++] = hexChars[c & 0x0f]; + } + data = [NSData allocWithZone: NSDefaultMallocZone()]; + data = [data initWithBytesNoCopy: dst length: dlen freeWhenDone: YES]; + string = [[NSString alloc] initWithData: data + encoding: NSASCIIStringEncoding]; + RELEASE(data); + return AUTORELEASE(string); +} + struct MD5Context { unsigned long buf[4]; @@ -354,9 +396,19 @@ static void MD5Transform (unsigned long buf[4], unsigned long const in[16]) /** * Creates an MD5 digest of the information stored in the receiver and - * returns it as an autoreleased 16 byte NSData object. + * returns it as an autoreleased 16 byte NSData object.
+ * If you need to produce a digest of string information, you need to + * decide what character encoding is to be used and convert your string + * to a data object of that encoding type first using the + * [NSString-dataUsingEncoding:] method - + * + * myDigest = [[myString dataUsingEncoding: NSUTF8StringEncoding] md5Digest]; + * + * If you need to use the digest in a human readable form, you will + * probably want it to be seen as 32 hexadecimal digits, and can do that + * using the -hexadecimalRepresentation method. */ -- (NSData*) MD5Digest +- (NSData*) md5Digest { struct MD5Context ctx; unsigned char digest[16]; diff --git a/Source/Additions/GSMime.m b/Source/Additions/GSMime.m index 9f7f27292..08cab691c 100644 --- a/Source/Additions/GSMime.m +++ b/Source/Additions/GSMime.m @@ -3742,7 +3742,7 @@ static NSCharacterSet *tokenSet = nil; source = [[[NSProcessInfo processInfo] globallyUniqueString] dataUsingEncoding: NSUTF8StringEncoding]; - digest = [source MD5Digest]; + digest = [source md5Digest]; memcpy(output, [digest bytes], 16); output[16] = (sequence >> 24) & 0xff; output[17] = (sequence >> 16) & 0xff; diff --git a/Source/NSString.m b/Source/NSString.m index 74eb9d21c..45b97934a 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -1505,21 +1505,48 @@ handle_printf_atsign (FILE *stream, return array; } +/** + * Returns a substring of the receiver from character at the specified + * index to the end of the string.
+ * So, supplying an index of 3 would return a substring consisting of + * the entire string apart from the first three character (those would + * be at index 0, 1, and 2).
+ * If the supplied index is greater than or equal to the length of the + * receiver an exception is raised. + */ - (NSString*) substringFromIndex: (unsigned int)index { return [self substringWithRange: ((NSRange){index, [self length]-index})]; } +/** + * Returns a substring of the receiver from the start of the + * string to (but not including) the specified index position.
+ * So, supplying an index of 3 would return a substring consisting of + * the first three characters of the receiver.
+ * If the supplied index is greater than the length of the receiver + * an exception is raised. + */ - (NSString*) substringToIndex: (unsigned int)index { return [self substringWithRange: ((NSRange){0,index})];; } +/** + * An obsolete name for -substringWithRange: ... deprecated. + */ - (NSString*) substringFromRange: (NSRange)aRange { return [self substringWithRange: aRange]; } +/** + * Returns a substring of the receiver containing the characters + * in aRange.
+ * If aRange specifies any character position not + * present in the receiver, an exception is raised.
+ * If aRange has a length of zero, an empty string is returned. + */ - (NSString*) substringWithRange: (NSRange)aRange { unichar *buf;