简体   繁体   English

有效负载阅读器从PHP到Objective-c

[英]Payload reader from PHP to Objective-c

I have this code below that process an ASCII character in PHP and transform it in a real message: 我下面有这段代码,用于处理PHP中的ASCII字符并将其转换为真实消息:

$message = '';

$len = ord($buffer[1]) & 127;

$masks = null;
$data = null;

if ($len === 126) {
    $masks = substr($buffer, 4, 4);
    $data = substr($buffer, 8);
}
elseif($len === 127) {
    $masks = substr($buffer, 10, 4);
    $data = substr($buffer, 14);
} else {
    $masks = substr($buffer, 2, 4);
    $data = substr($buffer, 6);
}

for ($index = 0; $index < strlen($data); $index++) {
    $message. = $data[$index] ^ $masks[$index % 4];
}

I'm trying to do the same thing in objective-c, for this: 为此,我正在尝试在Objective-C中执行相同的操作:

NSData * buffer = self.data;

NSString * mystring = [
    [NSString alloc] initWithData: buffer encoding: NSASCIIStringEncoding];

unichar len = [mystring characterAtIndex: 1] & 127;

NSString * masks = nil;
NSString * data = nil;

NSString * message = @"";

if (len == 126) {
    masks = [mystring substringWithRange: NSMakeRange(4, 4)];
    data = [mystring substringToIndex: 8];
} else if (len == 127) {
    masks = [mystring substringWithRange: NSMakeRange(10, 4)];
    data = [mystring substringToIndex: 14];
} else {
    masks = [mystring substringWithRange: NSMakeRange(2, 4)];
    data = [mystring substringToIndex: 6];
}

for (int index = 0; index < [data length]; index++) {
    [message stringByAppendingString: [NSString stringWithFormat: @"%@",
    data[index] ^ masks[index % 4]]];
}

This code has a little problem, I'm receiving this error message: 这段代码有一个小问题,我收到以下错误消息:

Expected method to read array element not found on object of type 'NSString*' 读取“ NSString *”类型的对象上找不到的数组元素的预期方法

Why and how can I solve this problem? 为什么以及如何解决这个问题?

When assigning to len you use characterAtIndex: to obtain a character from mystring . 当分配给len您可以使用characterAtIndex:mystring获得一个字符。

Later when trying to access characters in data and masks you instead try to use [] indexing, and the compiler advises you: 稍后,当尝试访问datamasks字符时,您改为尝试使用[]索引,并且编译器建议您:

Expected method to read array element not found on object of type 'NSString*' 读取“ NSString *”类型的对象上找不到的数组元素的预期方法

Use characterAtIndex: , as you did when indexing mystring . 使用characterAtIndex: ,就像索引mystring

Note that characterAtIndex returns back a unichar - the type of len - and this is 16-bits not an 8-bit byte. 请注意, characterAtIndex返回一个unichar - len的类型-这是16位而不是8位字节。 You may be better off using the NSString method cStringUsingEncoding , or one of its siblings, and processing you data as a C string or array of bytes. 使用NSString方法cStringUsingEncoding或其同级之一,然后将数据作为C字符串或字节数组进行处理,可能会更好。

HTH 高温超导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM