NSData subdataWithRange crashing - objective-c

I have the following code and I am getting an exception with it.
-[_NSInlineData replaceBytesInRange:withBytes:]: unrecognized selector sent to instance 0x6000027ff2e0'
terminating with uncaught exception of type NSException
Can someone point out the issue with this?
NSMutableData *data = [[NSMutableData alloc]
initWithLength:1000];
NSMutableData *d1 =(NSMutableData *) [data
subdataWithRange:NSMakeRange(sizeof(uint8_t),10)];
uint8_t i = 10;
[d1 replaceBytesInRange: NSMakeRange(1, sizeof(uint8_t)
withBytes:&i];

subdataWithRange: returns an immutable NSData instance. It's a copy of the original data. If you want to replace data in that copy, without affecting your original data object, you can do:
NSData *tmp = [data subdataWithRange:NSMakeRange(sizeof(uint8_t),10)];
NSMutableData *d1 = [tmp mutableCopy];
If you want to modify the mutable data object instead, do so directly by calculating the correct range:
// Offset derived from your example code, lines 5 and 7.
NSUInteger offset = sizeof(uint8_t) + 1;
NSRange range = NSMakeRange(offset, sizeof(uint8_t));
[data replaceBytesInRange:range withBytes:&i];

Related

What is wrong with this code implementation? Objectice-c, IOS

Testing the relationship between NSData, NSMutableData And bytes method and Byte Type variables want to change NSData Value to Bytes, when i run this it crashes the app but doesnt throw any error..
This Runs Ok
NSData *myData = [[NSData alloc] initWithData:someData];
Byte *finalValue = (Byte *)[myData bytes];
But This throws crashes app and doesnt throw an error
NSData *myData = [[NSData alloc] initWithData:someData];
NSMutableData *testingWaters = (NSMutableData *)[myData bytes];
Byte *finalValue = (Byte *)[testingWaters bytes];
EDITED: Keep In mind i want to convert a NSData Variable or NSMutableData Variable into a Byte variable.
You can create a mutable copy of myData
NSData* someData = [[NSString stringWithFormat:#"HELLO WORLD"]dataUsingEncoding:NSUTF8StringEncoding];
NSData *myData = [[NSData alloc] initWithData:someData];
NSMutableData *testingWaters = (NSMutableData *)[myData mutableCopy];
Byte *finalValue = (Byte *)[testingWaters bytes];

Conversion from decrypted NSData to NSDictionary fails

I have to encrypt and scramble a NSDictionary and then unscramble and decrypt in another method. I have followed the instructions from Securely storing keys in iOS Application except that I'm not using a PLIST file to fill the NSDictionary values, but NSString values that are being inputed in the program and I'm doing this targeting OSX
The program can be divided in 3 parts:
Encryption: Encrypts the NSDictionary and returns a base64encoded NSString
Scramble: Scrambles the base64encoded key among the values of the previous NSString
Unscramble and Decryption
My problem is in the last decryption. The unscramble works just fine, but the decryption seems to produce NSData that can't be convertible into the original NSDictionary
Encryption
What I'm doing for encryption is:
Initialising a NSDictionary
Converting it to NSData via NSPropertyListSerialization and format NSPropertyListXMLFormat_v1_0
Encrypting this data using RNCryptor and a key
Returning the base64 representation of this NSData as NSString
The code for this part is:
+(NSString *)encrypt:(NSString *)firstValue secondValue:(NSString *)secondValue andKey:(NSString *)key;
{
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:[[NSArray alloc] initWithObjects:firstValue, secondValue, nil] forKeys:[[NSArray alloc] initWithObjects:#"first", #"second", nil]];
NSData *data = [NSPropertyListSerialization dataFromPropertyList:dictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
NSData *encryptData = [RNEncryptor encryptData:data withSettings:kRNCryptorAES256Settings password:key error:nil];
return [encryptData base64EncodedString];
}
Scramble
The second part of the process is to scramble the key in base64 among the characters of the data, I'm doing this by:
Converting the key to NSData using -(NSData *)dataUsingEncoding:
Converting this NSData to a base64encoded NSString
Inserting char of this NSString into a NSMutadedString in a particular order
Returning NSString from this NSMutadedString to be decrypted later
Here's the code for the scramble part:
+(NSString *)scrambleStrings:(NSString *)firstValue secondValue:(NSString *)secondValue andKey:(NSString *)key;
{
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
key = [keyData base64EncodedString];
// String from the first method
NSString *encrypt = [self encrypt:firstValue secondValue:secondValue andKey:key];
NSArray *myOrder = [self mySortAlgorithm:key];
NSMutableString *mutableEncrypt = [NSMutableString stringWithString:encrypt];
for(int i=0; i<[myOrder count];i++)
{
unichar c = [key characterAtIndex:i];
int index = [[myOrder objectAtIndex:i] intValue];
[mutableEncrypt insertString:[NSString stringWithCharacters:&c length:1] atIndex:index];
}
return [NSString stringWithString:mutableEncrypt];
}
Decryption
The third part consists of testing if I can decrypt and unscramble it to the original NSDictionary and NSString key. It basically consists of:
Retrieve the scrambled NSString from the second method
Separating the scrambled key from the dictionary NSData from the previous NSString
Decode the base64encode key (Works for sure 'till here)
Decode the base64encoded dictionary data
Decrypt the previous data using RNCryptor using the decoded key
Initialising a NSDictionary using the data from the last step
Output the NSDictionary values via NSLog
And Here's the code for this last part:
+(BOOL)testScrambleWithFirstValue:(NSString *)firstValue secondValue:(NSString *)secondValue andKey:(NSString *)key;
{
NSString *scrambledAndEncryptedKeys = [self scrambleFirstValue:firstValue secondValue:SecondValue andKey:key];
NSArray *myOrder = [self mySortAlgorithm:key];
NSMutableString *encryptedDictionary = [[NSMutableString alloc] init];
NSMutableString *encryptedKey = [[NSMutableString alloc] init];
for (int i=0; i<scrambledAndEncryptedKeys.length; i++) {
char c = [scrambledAndEncryptedKeys characterAtIndex:i];
if ([myOrder doesContain:[NSNumber numberWithInt:i]]) {
[encryptedKey appendFormat:#"%c", c];
} else {
[encryptedDictionary appendFormat:#"%c",c];
}
}
NSString *decryptedKey = [[NSString alloc] initWithData:[NSData dataFromBase64String:[NSString stringWithString:encryptedKey]] encoding:NSUTF8StringEncoding];
NSString *base64DataStr = [[NSString alloc] initWithData:[NSData dataFromBase64String:[NSString stringWithString:encryptedDictionary]] encoding:NSUTF8StringEncoding];
NSData *decryptedData = [RNDecryptor decryptData:[base64DataStr dataUsingEncoding:NSUTF8StringEncoding] withPassword:decryptedKey error:nil];
NSPropertyListFormat xmlFormat;
NSDictionary *decryptedDictionary = [NSPropertyListSerialization propertyListWithData:decryptedData options:NSPropertyListImmutable format:&xmlFormat error:nil];
//Outputs NULL
NSLog(#"decryptedDictionary:%#", decryptedDictionary);
//Just to simplify I'm returning YES here
return YES;
}
The problem is, that the last NSLog outputs a NULL value. Which makes me think that the NSData is not being decrypted.
I've been hammering my head over this since yesterday morning, so all the help is appreciated. Thanks in advance

[__NSCFString count]: Unrecognized selector

I know this has been asked before, but there is no answer that I have found useful.
First off here is my code
// load the .csv file with all information about the track
NSError *error;
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"file" ofType:#"csv" inDirectory:nil];
NSString *datastring1 = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error];
NSArray *datarow = [datastring1 componentsSeparatedByString:#"\r"];
//fill arrays with the values from .csv file
NSArray *data_seg = [datarow objectAtIndex:0]; //segment number
NSArray *data_slength = [datarow objectAtIndex:1]; //strait length
NSArray *data_slope = [datarow objectAtIndex:2]; //slope
NSArray *data_cradius = [datarow objectAtIndex:3]; //circle radius
NSArray *data_cangle = [datarow objectAtIndex:4]; //circle angle
NSLog(#"%i", [data_seg count]);
Okay, so there is the code, and I read that is has something to do with autorelease, but I was not able to add a retain like NSArray *data_seg = [[datarow objectAtIndex:0] retain]
When I run the code, I get [__NSCFString count]: unrecognized selector sent to instance 0x9d1ad50
Any help is appreciated, I'm not good at programming, and I am very new.
componentsSeparatedByString method returns an NSArray of NSString. Every item that you extract from datarow array is an NSString and an NSString doesn't respond to 'count'. Your code starting at //fill arrays is incorrect. Every objectAtIndex call will return an NSString*.
This is another way of saying that the datatype for data_seg is NSString* (not NSArray*).
With the corrected code snippet, the problem is because data_seg is a string, and -count is not a method of NSString. It seems you think data_seg is an NSArray.
Look at the documentation for -[NSString componentsSeparatedByString:] and see what it returns -- strings! So you get back an array of strings. So what you want is:
NSString *data_seg = [datarow objectAtIndex:0]; //segment number
NSLog(#"my segment number is: %#", data_seg);

EXC_BAD_ACESS error

I get that error EXC_BAD_ACESS at the following line:
NSString *titleVarName = [[NSString alloc] initWithFormat:#"%#%#",#"occasionTitle",i];
Here is the for loop where the above code line is located:
for (i=0; i < count; ++i)
{
//Save the occasionS details to NSUserDefaults
NSString *titleVarName = [[NSString alloc] initWithFormat:#"%#%#",#"occasionTitle",i];
NSString *dateVarName = [[NSString alloc] initWithFormat:#"%#%#",#"occasionDate",i];
NSString *imageVarName = [[NSString alloc] initWithFormat:#"%#%#",#"occasionImage",i];
[[NSUserDefaults standardUserDefaults] setValue:[[[self displayedObjects] objectAtIndex:i]
title] forKey:titleVarName];
[[NSUserDefaults standardUserDefaults] setValue:[[[self displayedObjects] objectAtIndex:i]
date] forKey:dateVarName];
[[NSUserDefaults standardUserDefaults] setValue:[[[self displayedObjects] objectAtIndex:i]
imagePath] forKey:imageVarName];
//release
[titleVarName release];
[dateVarName release];
[imageVarName release];
[self dismissModalViewControllerAnimated:YES];
}
Isn't ok to alloc objects and release them inside a for loop?
You need to use %d or %i specifier instead of %# to specify an integer. If %# is used with int then it will try to access the object at the address specified by the int. For example, if the value of i is one then it is trying to access the object at address one which will cause a bad access.
NSString *titleVarName = [[NSString alloc] initWithFormat:#"%#%d",#"occasionTitle",i];
And also you don't need alloc and release here, though that is not the reason of bad access. You can use a convenience constructor.
NSString *titleVarName = [NSString stringWithFormat:#"occasionTitle%d", i];
// release not required
Do the same for dateVarName and imageVarName too.
Assuming i is an int, that line should be
NSString *titleVarName = [[NSString alloc] initWithFormat:#"%#%i",#"occasionTitle",i];
%# is used for Cocoa objects, not primitives like an int, float or bool;
Use the %# format specifier only for NSObject objects.
As i is an integer in your code, you have to use %d or %i for integers.
Moreover, there is no need to include the string using %#, you can use the static string directly in your format string:
NSString *titleVarName = [[NSString alloc] initWithFormat:#"occasionTitle%i",i];

Convert NSData bytes to NSString?

I'm trying to use the BEncoding ObjC class to decode a .torrent file.
NSData *rawdata = [NSData dataWithContentsOfFile:#"/path/to/the.torrent"];
NSData *torrent = [BEncoding objectFromEncodedData:rawdata];
When I NSLog torrent I get the following:
{
announce = <68747470 3a2f2f74 6f727265 6e742e75 62756e74 752e636f 6d3a3639 36392f61 6e6e6f75 6e6365>;
comment = <5562756e 74752043 44207265 6c656173 65732e75 62756e74 752e636f 6d>;
"creation date" = 1225365524;
info = {
length = 732766208;
name = <7562756e 74752d38 2e31302d 6465736b 746f702d 69333836 2e69736f>;
"piece length" = 524288;
....
How do I convert the name into a NSString? I have tried..
NSData *info = [torrent valueForKey:#"info"];
NSData *name = [info valueForKey:#"name"];
unsigned char aBuffer[[name length]];
[name getBytes:aBuffer length:[name length]];
NSLog(#"File name: %s", aBuffer);
..which retrives the data, but seems to have additional unicode rubbish after it:
File name: ubuntu-8.10-desktop-i386.iso)
I have also tried (from here)..
NSString *secondtry = [NSString stringWithCharacters:[name bytes] length:[name length] / sizeof(unichar)];
..but this seems to return a bunch of random characters:
扵湵畴㠭ㄮⴰ敤歳潴⵰㍩㘸椮潳
The fact the first way (as mentioned in the Apple documentation) returns most of the data correctly, with some additional bytes makes me think it might be an error in the BEncoding library.. but my lack of knowledge about ObjC is more likely to be at fault..
That's an important point that should be re-emphasized I think. It turns out that,
NSString *content = [NSString stringWithUTF8String:[responseData bytes]];
is not the same as,
NSString *content = [[NSString alloc] initWithBytes:[responseData bytes]
length:[responseData length] encoding: NSUTF8StringEncoding];
the first expects a NULL terminated byte string, the second doesn't. In the above two cases content will be NULL in the first example if the byte string isn't correctly terminated.
How about
NSString *content = [[[NSString alloc] initWithData:myData
encoding:NSUTF8StringEncoding] autorelease];
NSData *torrent = [BEncoding objectFromEncodedData:rawdata];
When I NSLog torrent I get the following:
{
⋮
}
That would be an NSDictionary, then, not an NSData.
unsigned char aBuffer[[name length]];
[name getBytes:aBuffer length:[name length]];
NSLog(#"File name: %s", aBuffer);
..which retrives the data, but seems to have additional unicode rubbish after it:
File name: ubuntu-8.10-desktop-i386.iso)
No, it retrieved the filename just fine; you simply printed it incorrectly. %s takes a C string, which is null-terminated; the bytes of a data object are not null-terminated (they are just bytes, not necessarily characters in any encoding, and 0—which is null as a character—is a perfectly valid byte). You would have to allocate one more character, and set the last one in the array to 0:
size_t length = [name length] + 1;
unsigned char aBuffer[length];
[name getBytes:aBuffer length:length];
aBuffer[length - 1] = 0;
NSLog(#"File name: %s", aBuffer);
But null-terminating the data in an NSData object is wrong (except when you really do need a C string). I'll get to the right way in a moment.
I have also tried […]..
NSString *secondtry = [NSString stringWithCharacters:[name bytes] length:[name length] / sizeof(unichar)];
..but this seems to return random Chinese characters:
扵湵畴㠭ㄮⴰ敤歳潴⵰㍩㘸椮潳
That's because your bytes are UTF-8, which encodes one character in (usually) one byte.
unichar is, and stringWithCharacters:length: accepts, UTF-16. In that encoding, one character is (usually) two bytes. (Hence the division by sizeof(unichar): it divides the number of bytes by 2 to get the number of characters.)
So you said “here's some UTF-16 data”, and it went and made characters from every two bytes; each pair of bytes was supposed to be two characters, not one, so you got garbage (which turned out to be mostly CJK ideographs).
You answered your own question pretty well, except that stringWithUTF8String: is simpler than stringWithCString:encoding: for UTF-8-encoded strings.
However, when you have the length (as you do when you have an NSData), it is even easier—and more proper—to use initWithBytes:length:encoding:. It's easier because it does not require null-terminated data; it simply uses the length you already have. (Don't forget to release or autorelease it.)
A nice quick and dirty approach is to use NSString's stringWithFormat initializer to help you out. One of the less-often used features of string formatting is the ability to specify a mximum string length when outputting a string. Using this handy feature allows you to convert NSData into a string pretty easily:
NSData *myData = [self getDataFromSomewhere];
NSString *string = [NSString stringWithFormat:#"%.*s", [myData length], [myData bytes]];
If you want to output it to the log, it can be even easier:
NSLog(#"my Data: %.*s", [myData length], [myData bytes]);
Aha, the NSString method stringWithCString works correctly:
With the bencoding.h/.m files added to your project, the complete .m file:
#import <Foundation/Foundation.h>
#import "BEncoding.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// Read raw file, and de-bencode
NSData *rawdata = [NSData dataWithContentsOfFile:#"/path/to/a.torrent"];
NSData *torrent = [BEncoding objectFromEncodedData:rawdata];
// Get the file name
NSData *infoData = [torrent valueForKey:#"info"];
NSData *nameData = [infoData valueForKey:#"name"];
NSString *filename = [NSString stringWithCString:[nameData bytes] encoding:NSUTF8StringEncoding];
NSLog(#"%#", filename);
[pool drain];
return 0;
}
..and the output:
ubuntu-8.10-desktop-i386.iso
In cases where I don't have control over the data being transformed into a string, such as reading from the network, I prefer to use NSString -initWithBytes:length:encoding: so that I'm not dependent upon having a NULL terminated string in order to get defined results. Note that Apple's documentation says if cString is not a NULL terminated string, that the results are undefined.
Use a category on NSData:
NSData+NSString.h
#interface NSData (NSString)
- (NSString *)toString;
#end
NSData+NSString.m
#import "NSData+NSString.h"
#implementation NSData (NSString)
- (NSString *)toString
{
Byte *dataPointer = (Byte *)[self bytes];
NSMutableString *result = [NSMutableString stringWithCapacity:0];
NSUInteger index;
for (index = 0; index < [self length]; index++)
{
[result appendFormat:#"0x%02x,", dataPointer[index]];
}
return result;
}
#end
Then just NSLog(#"Data is %#", [nsData toString])"
You can try this. Fine with me.
DLog(#"responeData: %#", [[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSASCIIStringEncoding] autorelease]);
Sometimes you need to create Base64 encoded string from NSData. For instance, when you create a e-mail MIME. In this case use the following:
#import "NSData+Base64.h"
NSString *string = [data base64EncodedString];
This will work.
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];