From 55c7cd74a50aafc6bb15754afc0837ac11f032bb Mon Sep 17 00:00:00 2001 From: jbettis Date: Wed, 12 Apr 2006 19:52:10 +0000 Subject: [PATCH] NSBundle.m: objc_executable_location(): A cast was obsuring a misuse of fileSystemRepresentation NSFileManager.m: isExecutableFileAtPath: Added a comment NSPropertyList.m: propertyListFromData:mutabilityOption:format:errorDescription: Don't call memcmp if the data is smaller than 8 bytes. objc-load.m: mingw unicode win32-load.h: mingw unicode NSUserDefaultsWin32.m: Write defaults to registry as unicode strings, read in either unicode(REG_SZ) or ascii(REG_BINARY). git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@22782 72102866-910b-0410-8b05-ffd578937521 --- Source/NSBundle.m | 6 ++- Source/NSFileManager.m | 1 + Source/NSPropertyList.m | 2 +- Source/objc-load.m | 8 ++++ Source/win32-load.h | 2 +- Source/win32/NSUserDefaultsWin32.m | 66 +++++++++++++++++++++++++----- 6 files changed, 71 insertions(+), 14 deletions(-) diff --git a/Source/NSBundle.m b/Source/NSBundle.m index 4845aa538..6d5ee3c31 100644 --- a/Source/NSBundle.m +++ b/Source/NSBundle.m @@ -255,10 +255,14 @@ static NSString *ExecutablePath() /* This function is provided for objc-load.c, although I'm not sure it really needs it (So far only needed if using GNU dld library) */ +#ifdef __MINGW32__ +const unichar * +#else const char * +#endif objc_executable_location (void) { - return (const char*)[[ExecutablePath() stringByDeletingLastPathComponent] + return [[ExecutablePath() stringByDeletingLastPathComponent] fileSystemRepresentation]; } diff --git a/Source/NSFileManager.m b/Source/NSFileManager.m index e1aa4eeb8..eb9270845 100644 --- a/Source/NSFileManager.m +++ b/Source/NSFileManager.m @@ -1491,6 +1491,7 @@ static NSStringEncoding defaultEncoding; { return NO; } + // TODO: Actually should check all extensions in env var PATHEXT if ([[[path pathExtension] lowercaseString] isEqualToString: @"exe"]) { return YES; diff --git a/Source/NSPropertyList.m b/Source/NSPropertyList.m index a96aefa11..673cf0071 100644 --- a/Source/NSPropertyList.m +++ b/Source/NSPropertyList.m @@ -2255,7 +2255,7 @@ GSPropertyListMake(id obj, NSDictionary *loc, BOOL xml, { bytes = [data bytes]; length = [data length]; - if (length > 0 && memcmp(bytes, "bplist00", 8) == 0) + if (length >= 8 && memcmp(bytes, "bplist00", 8) == 0) { format = NSPropertyListBinaryFormat_v1_0; } diff --git a/Source/objc-load.m b/Source/objc-load.m index c86f27956..78f2437c9 100644 --- a/Source/objc-load.m +++ b/Source/objc-load.m @@ -55,7 +55,11 @@ #include "dynamic-load.h" /* Declaration from NSBundle.m */ +#ifdef __MINGW32__ +const unichar *objc_executable_location (void); +#else const char *objc_executable_location (void); +#endif /* dynamic_loaded is YES if the dynamic loader was sucessfully initialized. */ static BOOL dynamic_loaded; @@ -101,7 +105,11 @@ objc_check_undefineds(FILE *errorStream) static int objc_initialize_loading(FILE *errorStream) { +#ifdef __MINGW32__ + const unichar *path; +#else const char *path; +#endif dynamic_loaded = NO; path = objc_executable_location(); diff --git a/Source/win32-load.h b/Source/win32-load.h index 2e49e0cfa..eca0175dc 100644 --- a/Source/win32-load.h +++ b/Source/win32-load.h @@ -34,7 +34,7 @@ typedef void* dl_symbol_t; if no initialization needed. */ static int -__objc_dynamic_init(const char* exec_path) +__objc_dynamic_init(const unichar* exec_path) { return 0; } diff --git a/Source/win32/NSUserDefaultsWin32.m b/Source/win32/NSUserDefaultsWin32.m index 2f72562ac..c64989acb 100644 --- a/Source/win32/NSUserDefaultsWin32.m +++ b/Source/win32/NSUserDefaultsWin32.m @@ -205,7 +205,7 @@ struct NSUserDefaultsWin32_DomainInfo { DWORD i = 0; unichar *name = malloc(200); - unsigned char *data = malloc(1000); + unichar *data = malloc(1000); DWORD namelenbuf = 100, datalenbuf = 1000; DWORD type; @@ -219,7 +219,7 @@ struct NSUserDefaultsWin32_DomainInfo &namelen, NULL, &type, - data, + (void*)data, &datalen); if (rc == ERROR_SUCCESS) { @@ -228,12 +228,31 @@ struct NSUserDefaultsWin32_DomainInfo id v; NSString *k; - v = [NSString stringWithCString: data + switch (type) { + case REG_SZ: { + int datacharlen = datalen / 2; + if (datacharlen > 0 && data[datacharlen-1] == 0) + datacharlen--; + + v = [NSString stringWithCharacters:data length:datacharlen]; + } + break; + case REG_BINARY: { + v = [NSString stringWithCString: (char*)data encoding: NSASCIIStringEncoding]; + } + break; + default: + NSLog(@"Bad registry type %d for '%S'", type, name); + v = 0; + } v = [v propertyList]; + if (v) + { k = [NSString stringWithCharacters: name length: namelen]; [domainDict setObject: v forKey: k]; } + } NS_HANDLER NSLog(@"Bad registry value for '%S'", name); NS_ENDHANDLER @@ -271,7 +290,7 @@ struct NSUserDefaultsWin32_DomainInfo { DWORD i = 0; unichar *name = malloc(200); - unsigned char *data = malloc(1000); + unichar *data = malloc(1000); DWORD namelenbuf = 100, datalenbuf = 1000; DWORD type; @@ -279,13 +298,19 @@ struct NSUserDefaultsWin32_DomainInfo { DWORD namelen = namelenbuf, datalen = datalenbuf; + // RegEnumValueW returns the data as a wide string + // but returns the length in bytes. + // To add insult to injury, datalen includes the terminating + // NULL character, unless there isn't enough room, in which + // case it doesn't. + rc = RegEnumValueW(dinfo->userKey, i, name, &namelen, NULL, &type, - data, + (void*)data, &datalen); if (rc == ERROR_SUCCESS) { @@ -294,12 +319,31 @@ struct NSUserDefaultsWin32_DomainInfo id v; NSString *k; - v = [NSString stringWithCString: data + switch (type) { + case REG_SZ: { + int datacharlen = datalen / 2; + if (datacharlen > 0 && data[datacharlen-1] == 0) + datacharlen--; + + v = [NSString stringWithCharacters:data length:datacharlen]; + } + break; + case REG_BINARY: { + v = [NSString stringWithCString: (char*)data encoding: NSASCIIStringEncoding]; + } + break; + default: + NSLog(@"Bad registry type %d for '%S'", type, name); + v = 0; + } v = [v propertyList]; + if (v) + { k = [NSString stringWithCharacters: name length: namelen]; [domainDict setObject: v forKey: k]; } + } NS_HANDLER NSLog(@"Bad registry value for '%S'", name); NS_ENDHANDLER @@ -523,16 +567,16 @@ struct NSUserDefaultsWin32_DomainInfo if (oldvalue == nil || [value isEqual: oldvalue] == NO) { NSString *result = nil; - const unsigned char *ptr; + const unichar *ptr; GSPropertyListMake(value, nil, NO, NO, 0, &result); - ptr = [result cStringUsingEncoding: NSASCIIStringEncoding]; + ptr = UNISTR(result); rc = RegSetValueExW(dinfo->userKey, UNISTR(valName), 0, - REG_BINARY, - ptr, - strlen(ptr) + 1); + REG_SZ, + (void*)ptr, + 2*(wcslen(ptr) + 1)); if (rc != ERROR_SUCCESS) { NSLog(@"Failed to insert HKEY_CURRENT_USER\\%@\\%@ (%x)",