在二进制数据中查找字符串 [英] Find Character String In Binary Data
问题描述
我有一个二进制文件,我使用NSData对象加载。有没有办法找到一个字符序列,'abcd',例如,在该二进制数据和返回偏移量,而不将整个文件转换为字符串?似乎应该是一个简单的答案,但我不知道该怎么做。任何想法?
I have a binary file I've loaded using an NSData object. Is there a way to locate a sequence of characters, 'abcd' for example, within that binary data and return the offset without converting the entire file to a string? Seems like it should be a simple answer, but I'm not sure how to do it. Any ideas?
我在iOS 3这样做,所以我没有 -rangeOfData:options:range:
可用。
I'm doing this on iOS 3 so I don't have -rangeOfData:options:range:
available.
我要授予这个给十六奥多建议strstr。我去找到C函数strstr的源代码,并重写它工作在一个固定长度的字节数组 - 这意味着不同于一个char数组,因为它不是null终止。这里是我最后得到的代码:
I'm going to award this one to Sixteen Otto for suggesting strstr. I went and found the source code for the C function strstr and rewrote it to work on a fixed length Byte array--which incidentally is different from a char array as it is not null terminated. Here is the code I ended up with:
- (Byte*)offsetOfBytes:(Byte*)bytes inBuffer:(const Byte*)buffer ofLength:(int)len;
{
Byte *cp = bytes;
Byte *s1, *s2;
if ( !*buffer )
return bytes;
int i = 0;
for (i=0; i < len; ++i)
{
s1 = cp;
s2 = (Byte*)buffer;
while ( *s1 && *s2 && !(*s1-*s2) )
s1++, s2++;
if (!*s2)
return cp;
cp++;
}
return NULL;
}
这会返回一个指向第一个字节的指针,在缓冲区中寻找应该包含字节的字节数组。
This returns a pointer to the first occurrence of bytes, the thing I'm looking for, in buffer, the byte array that should contain bytes.
我这样调用:
// data is the NSData object
const Byte *bytes = [data bytes];
Byte* index = [self offsetOfBytes:tag inBuffer:bytes ofLength:[data length]];
推荐答案
将子字符串转换为 NSData
对象,并使用 NSData library / documentation / Cocoa / Reference / Foundation / Classes / NSData_Class / Reference / Reference.html#// apple_ref / occ / instm / NSData / rangeOfData:options:range:rel =nofollow noreferrer> rangeOfData:options:range:
。确保字符串编码匹配!
Convert your substring to an NSData
object, and search for those bytes in the larger NSData
using rangeOfData:options:range:
. Make sure that the string encodings match!
在iPhone上,如果不可用,您可能需要自己做。 C函数 strstr()
将为您提供指向缓冲区中第一次出现的模式的指针(只要不包含空值!),而不是索引。这是一个函数,应该做这项工作(但没有承诺,因为我没有试过实际运行它...):
On iPhone, where that isn't available, you may have to do this yourself. The C function strstr()
will give you a pointer to the first occurrence of a pattern within the buffer (as long as neither contain nulls!), but not the index. Here's a function that should do the job (but no promises, since I haven't tried actually running it...):
- (NSUInteger)indexOfData:(NSData*)needle inData:(NSData*)haystack
{
const void* needleBytes = [needle bytes];
const void* haystackBytes = [haystack bytes];
// walk the length of the buffer, looking for a byte that matches the start
// of the pattern; we can skip (|needle|-1) bytes at the end, since we can't
// have a match that's shorter than needle itself
for (NSUInteger i=0; i < [haystack length]-[needle length]+1; i++)
{
// walk needle's bytes while they still match the bytes of haystack
// starting at i; if we walk off the end of needle, we found a match
NSUInteger j=0;
while (j < [needle length] && needleBytes[j] == haystackBytes[i+j])
{
j++;
}
if (j == [needle length])
{
return i;
}
}
return NSNotFound;
}
这样运行像O(nm),其中n是缓冲区长度,m是子字符串的大小。它写成使用 NSData
,因为两个原因:1)这是你看起来有在手,和2)这些对象已经封装了实际的字节,和长度缓冲区。
This runs in something like O(nm), where n is the buffer length, and m is the size of the substring. It's written to work with NSData
for two reasons: 1) that's what you seem to have in hand, and 2) those objects already encapsulate both the actual bytes, and the length of the buffer.
这篇关于在二进制数据中查找字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!