gpt4 book ai didi

c++ - 跳过源无法按预期工作

转载 作者:行者123 更新时间:2023-11-30 05:23:28 25 4
gpt4 key购买 nike

我使用 Crypto++ 5.6.3,我需要 FileSource Skip(...) 函数。不幸的是,这个函数什么都不做!

这里是这个 function 的例子.

string filename = ...;
string str;

FileSource file(filename, false, new HexEncoder(new StringSink(str)));
file.Skip(24);
file.PumpAll();

有人可以帮帮我吗?

最佳答案

I use Crypto++ 5.6.3 and iI need the FileSource "skip(...) function. Unfortunately this function does nothing!

我能够在 OS X 10.8.5 和 Ubuntu 14.04 上使用 Master、5.6.3 和 5.6.2 下的字符串复制它。

$ cat test.cxx
#include <string>
#include <iostream>
using namespace std;

#include <filters.h>
#include <hex.h>
using namespace CryptoPP;

int main(int argc, char* argv[])
{
string str1, str2;
HexEncoder enc(new StringSink(str1));
for(unsigned int i=0; i < 32; i++)
enc.Put((byte)i);
enc.MessageEnd();

cout << "str1: " << str1 <<endl;

StringSource ss(str1, false, new StringSink(str2));
ss.Skip(10);
ss.PumpAll();

cout << "str2: " << str2 << endl;

return 0;
}

和:

$ ./test.exe
str1: 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
str2: 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F

Crypto++ 5.6.2 非常重要,因为它是 Wei 在将库移交给社区之前使用的最后一个版本。 5.6.2 中的问题只是一个潜在的错误,我们偶尔会遇到它们,就像任何其他项目一样。 (“Wei”错误实际上很少见,它们更接近他的 Art of Computer Programming 中的“Knuth”错误)。

如果它是 5.6.3 及以上的问题,则意味着社区破坏了它。如果社区破坏了它,那么我们需要进行事后分析并分析我们如何/为什么设法破坏曾经有效的东西。

这是库的错误报告:Issue 248: Skip'ing on a Source does not work .我们正在尝试确定它是否是错误;如果是,那么如何进行。


编辑 1:我能够进一步调查这个问题。您可以在 Comment 242890863 阅读分析.简而言之,Skip 用于丢弃输出缓冲区(AttachedTransformation())上的字节,所以有点 按预期工作。但是,Skip Source 上工作,并且只在附加的 Filter 上工作,这没有什么直观的(< em>q.v.,我们到了)。

我还在 Issue 248: Skip'ing on a Source does not work 的邮件列表中征求了一些反馈意见. DB 和 WD 立即发现了它 - 这是库中的一个设计问题。

这是您目前可以使用的解决方法。实际上,您将 Pump() 放入 null Filter 中,这会按预期丢弃输入。然后附加真正的过滤器链来处理真正的处理。

#include <string>
#include <iostream>
using namespace std;

#include <filters.h>
#include <hex.h>
using namespace CryptoPP;

int main(int argc, char* argv[])
{
string str1, str2;
HexEncoder enc(new StringSink(str1));
for(unsigned int i=0; i < 32; i++)
enc.Put((byte)i);
enc.MessageEnd();

cout << "str1: " << str1 <<endl;

// 'ss' has a NULL AttachedTransformation()
StringSource ss(str1, false);
ss.Pump(10);

// Attach the real filter chain to 'ss'
ss.Attach(new StringSink(str2));
ss.PumpAll();

cout << "str2: " << str2 << endl;

return 0;
}

它产生预期的输出:

$ ./test.exe 
str1: 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F
str2: 05060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F

在您的示例程序中,我认为解决方法是:

FileSource file(filename, false);
file.Pump(24);

file.Attach(new HexEncoder(new StringSink(str)));
file.PumpAll();

EDIT 2:这是一种稍微更冗长的解决方法(感谢 DB)。它强调字节被丢弃这一点。 TheBitBucket() 只是一个丢弃过滤器,它的作用与 null AttachedTransformation() 相同。

int main(int argc, char* argv[])
{
string str1, str2;
HexEncoder enc(new StringSink(str1));
for(unsigned int i=0; i < 32; i++)
enc.Put((byte)i);
enc.MessageEnd();

cout << "str1: " << str1 <<endl;

StringSource ss(str1, false, new Redirector(TheBitBucket()));
ss.Pump(10);

ss.Detach(new StringSink(str2));
ss.PumpAll();

cout << "str2: " << str2 << endl;

return 0;
}

上面的程序还有另一个细微差别:它调用了 Detach,它释放了之前的过滤器链。如果您调用 Attach,那么之前的链将被分离,返回给调用者但不会释放。

关于c++ - 跳过源无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39166041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com