在二进制文件中查找模式 [英] find a pattern in binary file

查看:70
本文介绍了在二进制文件中查找模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

i需要在二进制文件中找到像0x650A1010这样的十六进制模式。

i可以制作一个小算法来获取匹配的所有文件,

但这个文件太大了,我害怕表演。

有快速搜索的stl方法吗?

Andrea

Hi there,
i need to find an hex pattern like 0x650A1010 in a binary file.
i can make a small algorithm that fetch all the file for the match,
but this file is huge, and i''m scared about performances.
Is there any stl method for a fast search?
Andrea

推荐答案

vizzz写道:
vizzz wrote:

你好,

i需要在二进制文件中找到像0x650A1010这样的十六进制模式。

i可以制作一个小算法来获取匹配的所有文件,

但这个文件很大,而且我很害怕表演。

有快速搜索的stl方法吗?
Hi there,
i need to find an hex pattern like 0x650A1010 in a binary file.
i can make a small algorithm that fetch all the file for the match,
but this file is huge, and i''m scared about performances.
Is there any stl method for a fast search?



您可以尝试使用istreambuf_iterator< std :: search() unsigned char> ;.


但是:


(a)目前尚不清楚您是否会获得良好的表现。一些实现

对于流迭代器来说并不是那么好。


(b)我不确定search()是否允许使用回溯

内部,在这种情况下,您不能将它与流迭代器一起使用。你应该检查



(c)即使搜索发现了一个事件,它也会将结果报告为

迭代器。我不知道将其转换为偏移的便捷方式。

也许,滚动自己并不是那么糟糕。您可以在

块中读取文件(保留前一个块中的最后三个字符)并在块上使用

std :: search()。使用正确的块大小,这可能真的很快。

如果你的操作系统允许文件的内存映射,你可以这样做并使用

std :: search()使用unsigned char *就整个事情而言。这可能是禁食的方式,但是会留下标准C ++的范围。

最好


Kai-Uwe Bux

You could try std::search() with istreambuf_iterator< unsigned char >.

However:

(a) It is not clear that you will get good performance. Some implementations
are not really all that good with stream iterators.

(b) I am not sure whether search() is allowed to use backtracking
internally, in which case you cannot use it with stream iterators. You
should check.

(c) Even if search finds an occurrence, it reports the result as an
iterator. I do not know of a convenient way to convert that into an offset.
Maybe, rolling your own is not all that bad. You could read the file in
chunks (keeping the last three characters from the previous block) and use
std::search() on the blocks. With the right blocksize, this could be really
fast.
If your OS allows memory mapping of the file, you could do that and use
std::search() with unsigned char * on the whole thing. That could be the
fasted way, but will leave the realm of standard C++.
Best

Kai-Uwe Bux


6月20日,1:11 * pm,vizzz< andrea.visin ... @ gmail.comwrote:
On Jun 20, 1:11*pm, vizzz <andrea.visin...@gmail.comwrote:

你好,

i需要在二进制文件中找到像0x650A1010这样的十六进制模式。

i可以制作一个小算法来获取匹配的所有文件,

但这个文件太大了,我害怕表演。

有快速搜索的stl方法吗?

Andrea
Hi there,
i need to find an hex pattern like 0x650A1010 in a binary file.
i can make a small algorithm that fetch all the file for the match,
but this file is huge, and i''m scared about performances.
Is there any stl method for a fast search?
Andrea



嗯...我看了一下这个问题并遇到了一个简单的问题。如何读取二进制文件并将HEX字节回显到屏幕上。

问题是c ++读取函数没有返回字节数

读...所以在上次读入缓冲区时你怎么知道要打印多少个b $ b字符?


谢谢,

Ivan Novick
http://www.mycppquiz.com


Ivan写道:
Ivan wrote:

6月20日,1:11 * pm,vizzz< ; andrea.visin ... @ gmail.comwrote:
On Jun 20, 1:11*pm, vizzz <andrea.visin...@gmail.comwrote:

>嗨那里,
我需要在二进制文件中找到像0x650A1010这样的十六进制模式。
我可以制作一个小算法来获取匹配的所有文件,
但是这个文件太大了,我害怕表演。
是否有任何stl方法快速搜索?
Andrea
>Hi there,
i need to find an hex pattern like 0x650A1010 in a binary file.
i can make a small algorithm that fetch all the file for the match,
but this file is huge, and i''m scared about performances.
Is there any stl method for a fast search?
Andrea



嗯...我看了看这个并且跑了一圈问题很多。你如何读取二进制文件并将HEX以字节形式回显到屏幕上。


Hmmm... I had a look at this and ran accross a simple problem. How do
you read a binary file and just echo the HEX for byte to the screen.



#include< iostream>

#include< ostream>

#include< fstream>

#include< iterator>

#include< iomanip>

#include< algorithm>

#include< cassert>


class print_hex {


std :: ostream * ostr_ptr;

unsigned int line_length;

unsigned int index;


public:


print_hex(std :: ostream& str_ref, unsigned int length)

:ostr_ptr(& str_ref)

,line_length(length)

,index(0)

{}


void operator()(unsigned char ch){

++ index;

if(index > = line_length){

(* ostr_ptr)<< std :: hex<< std :: setw(2)<< std :: setfill(''0'')

<< (unsigned int)(ch)<< ''\ n'';

index = 0;

} else {

(* ostr_ptr)<< std :: hex<< std :: setw(2)<< std :: setfill(''0'')

<< (unsigned int)(ch)<< '''';

}

}


};


int main(int argn,char ** args){

assert(argn == 2);

std :: ifstream in(args [1]);

std :: for_each(std :: istreambuf_iterator< char>(in),

std :: istreambuf_iterator< char>(),

print_hex (std :: cout,25));

std :: cout<< ''\\ n'';

}

#include <iostream>
#include <ostream>
#include <fstream>
#include <iterator>
#include <iomanip>
#include <algorithm>
#include <cassert>

class print_hex {

std::ostream * ostr_ptr;
unsigned int line_length;
unsigned int index;

public:

print_hex ( std::ostream & str_ref, unsigned int length )
: ostr_ptr( &str_ref )
, line_length ( length )
, index ( 0 )
{}

void operator() ( unsigned char ch ) {
++index;
if ( index >= line_length ) {
(*ostr_ptr) << std::hex << std::setw(2) << std::setfill( ''0'' )
<< (unsigned int)(ch) << ''\n'';
index = 0;
} else {
(*ostr_ptr) << std::hex << std::setw(2) << std::setfill( ''0'' )
<< (unsigned int)(ch) << '' '';
}
}

};

int main ( int argn, char ** args ) {
assert( argn == 2 );
std::ifstream in ( args[1] );
std::for_each( std::istreambuf_iterator< char >( in ),
std::istreambuf_iterator< char >(),
print_hex( std::cout, 25 ) );
std::cout << ''\n'';
}


问题是c ++读取函数没有返回数字字节数

读取...所以在最后一次读入缓冲区时你怎么知道要打印多少个b $ b字符?
The issue is the c++ read function doesn''t return number of bytes
read... so on the last read into a buffer how do you know how many
characters to print?



看看若干()。


最好


启-Uwe Bux

Have a look at readsome().

Best

Kai-Uwe Bux


这篇关于在二进制文件中查找模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆