Search code examples
iosobjective-cxcodensstringnsdata

iOS how to print the equivalent of "view memory of * " in xCode?


I'm trying to print out the "Issuer" data available from a keychain item in iOS. I can't seem to find the proper encoding or understand how this data is stored.

When I choose thew "view memory of*" option in xCode, I see that I see some text among garbage. I'm trying to understand how I can print both the text and garbage that the memory pointer is addressing. Below is a screenshot of what I'm trying to do

enter image description here

enter image description here

I tried these, but I get either nil strings, or japaneese characters for UTF16

        id data = [innerObject objectForKey:@"issr"];

        NSString* string8 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSString* string16 = [[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding];
        NSString* string16be = [[NSString alloc] initWithData:data encoding:NSUTF16BigEndianStringEncoding];
        NSString* string16le = [[NSString alloc] initWithData:data encoding:NSUTF16LittleEndianStringEncoding];
        NSString* string32 = [[NSString alloc] initWithData:data encoding:NSUTF32StringEncoding];

        NSString* string8Bytes = [[NSString alloc]initWithBytes:&data length:[data length] encoding:NSUTF8StringEncoding];
//NSLog the result

Solution

  • Looking at the memory dump, you can select the letters and it will highlight the corresponding memory bytes. There are 2 bytes per letter, which made me realize it was using Ascii encoding, the code below works:

                  @try {
    
                    if([data isKindOfClass:[NSData class]]==NO)
                    {
                        continue;
                    }
                    NSString* stringAscii = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    
    
                    NSCharacterSet* alphaNumeric = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890.@"];
    
    
                    NSCharacterSet *doNotWant = [alphaNumeric invertedSet];
                    NSString* cleanedUpString = [[stringAscii componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @" "];
    
                    if(cleanedUpString.length>0)
                    {
                        DLog(@" %@ Cleaned up: %@",key,cleanedUpString);
                    }
                }
                @catch (NSException *exception) {
    
                }
                @finally {
    
                }