diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d0ea6f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.svn +._* +.DS_Store diff --git a/Classes/NSString+MD5Addition.h b/UIDevice-with-UniqueIdentifier-for-iOS-5/NSString+MD5Addition.h similarity index 100% rename from Classes/NSString+MD5Addition.h rename to UIDevice-with-UniqueIdentifier-for-iOS-5/NSString+MD5Addition.h diff --git a/Classes/NSString+MD5Addition.m b/UIDevice-with-UniqueIdentifier-for-iOS-5/NSString+MD5Addition.m similarity index 92% rename from Classes/NSString+MD5Addition.m rename to UIDevice-with-UniqueIdentifier-for-iOS-5/NSString+MD5Addition.m index 62abdbc..cdad2ad 100644 --- a/Classes/NSString+MD5Addition.m +++ b/UIDevice-with-UniqueIdentifier-for-iOS-5/NSString+MD5Addition.m @@ -12,21 +12,21 @@ @implementation NSString(MD5Addition) - (NSString *) stringFromMD5{ - + if(self == nil || [self length] == 0) return nil; - + const char *value = [self UTF8String]; - + unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH]; CC_MD5(value, strlen(value), outputBuffer); - + NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2]; for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){ [outputString appendFormat:@"%02x",outputBuffer[count]]; } - - return [outputString autorelease]; + + return outputString; } @end diff --git a/Classes/UIDevice+IdentifierAddition.h b/UIDevice-with-UniqueIdentifier-for-iOS-5/UIDevice+IdentifierAddition.h similarity index 100% rename from Classes/UIDevice+IdentifierAddition.h rename to UIDevice-with-UniqueIdentifier-for-iOS-5/UIDevice+IdentifierAddition.h diff --git a/Classes/UIDevice+IdentifierAddition.m b/UIDevice-with-UniqueIdentifier-for-iOS-5/UIDevice+IdentifierAddition.mm similarity index 61% rename from Classes/UIDevice+IdentifierAddition.m rename to UIDevice-with-UniqueIdentifier-for-iOS-5/UIDevice+IdentifierAddition.mm index 353b023..c9fafd7 100644 --- a/Classes/UIDevice+IdentifierAddition.m +++ b/UIDevice-with-UniqueIdentifier-for-iOS-5/UIDevice+IdentifierAddition.mm @@ -9,6 +9,8 @@ #import "UIDevice+IdentifierAddition.h" #import "NSString+MD5Addition.h" +#include + #include // Per msqr #include #include @@ -29,48 +31,59 @@ @implementation UIDevice (IdentifierAddition) // Return the local MAC addy // Courtesy of FreeBSD hackers email list // Accidentally munged during previous update. Fixed thanks to erica sadun & mlamb. -- (NSString *) macaddress{ - - int mib[6]; - size_t len; - char *buf; - unsigned char *ptr; - struct if_msghdr *ifm; - struct sockaddr_dl *sdl; +- (NSString *) macaddress +{ + static const unsigned int MIB_SIZE = 6; + int mib[6] = + { + CTL_NET, + AF_ROUTE, + 0, + AF_LINK, + NET_RT_IFLIST + }; - mib[0] = CTL_NET; - mib[1] = AF_ROUTE; - mib[2] = 0; - mib[3] = AF_LINK; - mib[4] = NET_RT_IFLIST; + size_t len = 0; + char *buf = NULL; + unsigned char *ptr = NULL; + struct if_msghdr *ifm = {0}; + struct sockaddr_dl *sdl = {0}; + + std::vector bufGuard; + - if ((mib[5] = if_nametoindex("en0")) == 0) { - printf("Error: if_nametoindex error\n"); - return NULL; + if ((mib[5] = if_nametoindex("en0")) == 0) + { + NSLog(@"Error: if_nametoindex error\n"); + return nil; } - - if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) { - printf("Error: sysctl, take 1\n"); - return NULL; + if (sysctl(mib, MIB_SIZE, NULL, &len, NULL, 0) < 0) + { + NSLog(@"Error: sysctl, take 1\n"); + return nil; } + - if ((buf = malloc(len)) == NULL) { - printf("Could not allocate memory. error!\n"); - return NULL; + bufGuard.resize( len, 0 ); + if ( 0 == len || bufGuard.empty() ) + { + NSLog( @"Could not allocate memory. error!\n" ); + return nil; } + buf = reinterpret_cast( &bufGuard[ 0 ] ); + - if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) { + if (sysctl(mib, MIB_SIZE, buf, &len, NULL, 0) < 0) + { printf("Error: sysctl, take 2"); - free(buf); return NULL; } - ifm = (struct if_msghdr *)buf; - sdl = (struct sockaddr_dl *)(ifm + 1); - ptr = (unsigned char *)LLADDR(sdl); + ifm = reinterpret_cast( buf ); + sdl = reinterpret_cast(ifm + 1); + ptr = reinterpret_cast( LLADDR(sdl) ); NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)]; - free(buf); return outstring; }