我在加密之前使用Gzip压缩数据。
Gzip gz;
gz.Put(file,size)
gz.MessageEnd();
gz.Get(file,gz.MaxRetrievable());
我希望创建的gzip文件包含原始文件名作为元数据。如何通过Crypto++接口实现这一点?
(if FLG.FNAME set)
+=========================================+
|...original file name, zero-terminated...| (more-->)
+=========================================+
但是Crypto++不允许您设置它(来自gzip.c源代码):
void Gzip::WritePrestreamHeader()
{
m_totalLen = 0;
m_crc.Restart();
AttachedTransformation()->Put(MAGIC1);
AttachedTransformation()->Put(MAGIC2);
AttachedTransformation()->Put(DEFLATED);
AttachedTransformation()->Put(0); // general flag
AttachedTransformation()->PutWord32(0); // time stamp
byte extra = (GetDeflateLevel() == 1) ? FAST : ((GetDeflateLevel() == 9) ? SLOW : 0);
AttachedTransformation()->Put(extra);
AttachedTransformation()->Put(GZIP_OS_CODE);
}
如果在解压缩时存在,Crypto++将会默默地将其丢弃(Gunzip
是 GZIP 解压器)(来自 gzip.c 源代码):
void Gunzip::ProcessPrestreamHeader()
{
...
if (flags & EXTRA_FIELDS) // skip extra fields
{
word16 length;
if (m_inQueue.GetWord16(length, LITTLE_ENDIAN_ORDER) != 2) throw HeaderErr();
if (m_inQueue.Skip(length)!=length) throw HeaderErr();
}
if (flags & FILENAME) // skip filename
do
if(!m_inQueue.Get(b)) throw HeaderErr();
while (b);
if (flags & COMMENTS) // skip comments
do
if(!m_inQueue.Get(b)) throw HeaderErr();
while (b);
}
我记得大约15年来没有人要求过它,所以这不是一个流行的请求 :)
以下是如何修改源代码以添加文件时间、文件名和注释的方法。您可以在Crypto++维基页面上找到Gzip和Gunzip的补丁。您可以在Pastebin上找到SVN差异Crypto ++ gzip用于文件名和注释处理的差异。这些文件大多相同,但维基更加实时,因为它有一些重置代码在Gzip :: WritePoststreamTail
中。
首先,在Gzip
和Gunzip
类中添加以下受保护成员:
word32 m_filetime;
std::string m_filename;
std::string m_comment;
其次,为每个构造函数(包括 Gzip
和 Gunzip
)添加一个 m_filetime
的初始化器。例如,下面是其中一个 Gzip
构造函数的示例:
Gzip(const NameValuePairs ¶meters, BufferedTransformation *attachment=NULL)
: Deflator(parameters, attachment), m_filetime(0) {}
Gzip
类中:void SetFiletime(word32 filetime) { m_filetime = filetime; }
void SetFilename(const std::string& filename) { m_filename = filename; }
void SetComment(const std::string& comment) { m_comment = comment; }
Gunzip
类添加公共getter方法:word32 GetFiletime() const { return m_filetime; }
const std::string& GetFilename() const { return m_filename; }
const std::string& GetComment() const { return m_comment; }
Gzip :: WritePrestreamHeader
更改为以下内容:void Gzip::WritePrestreamHeader()
{
m_totalLen = 0;
m_crc.Restart();
int flags = 0;
if(!m_filename.empty())
flags |= FILENAME;
if(!m_comment.empty())
flags |= COMMENTS;
AttachedTransformation()->Put(MAGIC1);
AttachedTransformation()->Put(MAGIC2);
AttachedTransformation()->Put(DEFLATED);
AttachedTransformation()->Put((byte)flags); // general flag
AttachedTransformation()->PutWord32(m_filetime, LITTLE_ENDIAN_ORDER); // time stamp
byte extra = (GetDeflateLevel() == 1) ? FAST : ((GetDeflateLevel() == 9) ? SLOW : 0);
AttachedTransformation()->Put(extra);
AttachedTransformation()->Put(GZIP_OS_CODE);
if(!m_filename.empty())
AttachedTransformation()->Put((const unsigned char*)m_filename.data(), m_filename.size() +1);
if(!m_comment.empty())
AttachedTransformation()->Put((const unsigned char*)m_comment.data(), m_comment.size() +1);
}
Gzip::WritePoststreamTail
更改为以下内容:void Gzip::WritePoststreamTail()
{
SecByteBlock crc(4);
m_crc.Final(crc);
AttachedTransformation()->Put(crc, 4);
AttachedTransformation()->PutWord32(m_totalLen, LITTLE_ENDIAN_ORDER);
m_filetime = 0;
m_filename.erase(0);
m_filename.reserve(16);
m_comment.erase(0);
m_comment.reserve(32);
}
Gunzip::ProcessPrestreamHeader
更改为以下内容:void Gunzip::ProcessPrestreamHeader()
{
m_length = 0;
m_crc.Restart();
m_filetime = 0;
m_filename.erase(0);
m_filename.reserve(16);
m_comment.erase(0);
m_comment.reserve(32);
byte buf[6];
byte b, flags;
if (m_inQueue.Get(buf, 2)!=2) throw HeaderErr();
if (buf[0] != MAGIC1 || buf[1] != MAGIC2) throw HeaderErr();
if (!m_inQueue.Get(b) || (b != DEFLATED)) throw HeaderErr(); // skip CM flag
if (!m_inQueue.Get(flags)) throw HeaderErr();
if (flags & (ENCRYPTED | CONTINUED)) throw HeaderErr();
if (m_inQueue.GetWord32(m_filetime, LITTLE_ENDIAN_ORDER) != 4) throw HeaderErr();
if (m_inQueue.Skip(2)!=2) throw HeaderErr(); // Skip extra flags and OS type
if (flags & EXTRA_FIELDS) // skip extra fields
{
word16 length;
if (m_inQueue.GetWord16(length, LITTLE_ENDIAN_ORDER) != 2) throw HeaderErr();
if (m_inQueue.Skip(length)!=length) throw HeaderErr();
}
if (flags & FILENAME) // extract filename
{
do
{
if(!m_inQueue.Get(b)) throw HeaderErr();
if(b) m_filename.append( 1, (char)b );
}
while (b);
}
if (flags & COMMENTS) // extract comments
{
do
{
if(!m_inQueue.Get(b)) throw HeaderErr();
if(b) m_comment.append( 1, (char)b );
}
while (b);
}
}
try {
Gzip zipper(new FileSink("gzip-test.gz", true));
zipper.SetFilename("test-filename.txt");
zipper.SetComment("This is a test of filenames and comments");
string data = "abcdefghijklmnopqrstuvwxyz";
zipper.Put( (unsigned char*) data.c_str(), data.size());
zipper.MessageEnd();
}
catch(CryptoPP::Exception& ex)
{
cerr << ex.what() << endl;
}
gzip-test.gz
。标题中的原始文件名字段为test-filename.txt
,标题中的注释为This is a test of filenames and comments
。gzip -d
和Mac OS X上的默认存档提取程序的工作原理:嵌入式文件名被忽略,文件将使用存档文件名保存,但不包括gz
扩展名:
gunzip -N <gz-file>
来提取使用标头中存储的原始文件名。
string s1, s2;
string data = "abcdefghijklmnopqrstuvwxyz";
Gzip zipper(new StringSink(s1));
zipper.SetFilename("test-filename.txt");
zipper.SetComment("This is a test of filenames and comments");
zipper.Put( (unsigned char*) data.c_str(), data.size());
zipper.MessageEnd();
Gunzip unzipper(new StringSink(s2));
unzipper.Put( (unsigned char*) s1.data(), s1.size());
unzipper.MessageEnd();
cout << "Filename: " << unzipper.GetFilename() << endl;
cout << "Comment: " << unzipper.GetComment() << endl;
cout << "Data: " << s2 << endl;