用Zlib解压缩文本字符串

Decompress text string with zlib

本文关键字:字符串 文本 解压缩 Zlib      更新时间:2023-10-16

我需要以zlib格式解码十六进制字符串。一个例子是:

180000001300000000aefjyoaazidfcmqgggqqaajwacg ==

其中18000000和13000000是未压缩/压缩数据的大小(在这种情况下为24和19(。

我也知道字符串的其余部分

020000000000000000030000000001000000030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

问题在哪里?按照任何教程,例如https://panthema.net/2007/0328-zlibstring.html压缩该字符串,我得到了

x?302@?p ??

在十六进制中可以写为

783F33330324053F503F103FF5

与我预期的压缩字符串无关,所以我没有找到解压缩原始字符串的方式(这是我的最终目标(

预先感谢您的任何提示!

ps。我正在使用解压缩程序https://github.com/systemed/intersector/blob/master/helpers.cpp

看来该字符串已在base64上编码(谢谢@zdenek和 @mark-adler(,我设法用

对其进行了解码
BYTE *res;
int resSize = FromBase64Simple((BYTE*)actualData.c_str(),actualData.len(),res,sizeCompressed);

您可以从https://github.com/kengonakajima/luvit-base64/blob/master/base/base64.c 读取实施。

,但这不是问题,因为我可以使用

转让结果
char* resChar = new char[resSize];
for(int i = 0;i<resSize;i++)
{
    int asciiCode = (BYTE)res[i];
    resChar[i]=char(asciiCode);
    char buffer [2];
    itoa (asciiCode,buffer,16);
    qDebug()<<"["<<i<<"]t"<<asciiCode<<"t"<<buffer;
}

我在十进制和十六进制中获得每个字节的结果,两者都可以。十六进制看起来像:

78 01 63 62 80 00 66 20 C5 08 C4 20 1A 04 00 9C 00 0A

但是reschar是" x?cb?"这与 @Mark-Adler所说的" X?302 @?p"无关。(显然在哪里'?'符号是不可打印的(,我真的认为这是问题所在,但是我的数据似乎与此表相符:https://www.asciatible.com/,Mark的一个人也不是这样。Web https://conv.darkbyte.ru/返回与我的算法相同的结果

我尝试使用上面说的实现来解压缩字符串,但失败了(也尝试了https://gist.github.com/arq5x/5315739(,但其解剖值是单个字符串"

在这里,我们使用最小可重复的情况:

#include <string>
static char LookupDigits[] = {
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //gap: ctrl chars
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //gap: ctrl chars
    0,0,0,0,0,0,0,0,0,0,0,           //gap: spc,!"#$%'()*
    62,                   // +
    0, 0, 0,             // gap ,-.
    63,                   // /
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, // 0-9
    0, 0, 0,             // gap: :;<
    99,                   //  = (end padding)
    0, 0, 0,             // gap: >?@
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16,
    17,18,19,20,21,22,23,24,25, // A-Z
    0, 0, 0, 0, 0, 0,    // gap: []^_`
    26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,
    43,44,45,46,47,48,49,50,51, // a-z
    0, 0, 0, 0,          // gap: {|}~ (and the rest...)
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
int FromBase64Simple(const unsigned char* pSrc, int nLenSrc, unsigned char* pDst, int nLenDst)
{
    int nLenOut = 0;
    for (int j = 0; j<nLenSrc; j += 4) {
        if (nLenOut > nLenDst) {
            return(0); // error, buffer too small
        }
        unsigned char s1 = LookupDigits[*pSrc++];
        unsigned char s2 = LookupDigits[*pSrc++];
        unsigned char s3 = LookupDigits[*pSrc++];
        unsigned char s4 = LookupDigits[*pSrc++];
        unsigned char d1 = ((s1 & 0x3f) << 2) | ((s2 & 0x30) >> 4);
        unsigned char d2 = ((s2 & 0x0f) << 4) | ((s3 & 0x3c) >> 2);
        unsigned char d3 = ((s3 & 0x03) << 6) | ((s4 & 0x3f) >> 0);
        *pDst++ = d1;  nLenOut++;
        if (s3 == 99) break;      // end padding found
        *pDst++ = d2;  nLenOut++;
        if (s4 == 99) break;      // end padding found
        *pDst++ = d3;  nLenOut++;
    }
    return(nLenOut);
}

int main()
{
    std::string inputData = "eAFjYoAAZiDFCMQgGgQAAJwACg==";

    //19 is hardcoded since I know its size prior to this call
    unsigned char res[19];
    int resSize = FromBase64Simple((unsigned char*)inputData.c_str(), inputData.size(), res, 19);

    for (int i = 0; i<resSize; i++)
    {
        int asciiCode = res[i];
        printf("[%i]t%it%xn", i, asciiCode, asciiCode);
    }
    printf("nnres: %s", (char*)res);
    getchar();
    return 0;
}

这对我来说很好。我使用了您链接的解压功能和您提供的base64函数。我删除了错误检查并重新格式化以使其短。

#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <zlib.h>
#pragma comment(lib, "zdll.lib")
static char LookupDigits[] = {
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,62,0,0,0,63,52,53,54,55,56,57,58,59,60,61,
    0,0,0,99,0,0,0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,
    20,21,22,23,24,25,0,0,0,0,0,0,26,27,28,29,30,31,32,33,34,35,36,
    37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
int FromBase64Simple(const unsigned char* pSrc, int nLenSrc, unsigned char* pDst, int nLenDst)
{
    int nLenOut = 0;
    for (int j = 0; j<nLenSrc; j += 4)
    {
        if (nLenOut > nLenDst)
        {
            return(0); // error, buffer too small
        }
        unsigned char s1 = LookupDigits[*pSrc++];
        unsigned char s2 = LookupDigits[*pSrc++];
        unsigned char s3 = LookupDigits[*pSrc++];
        unsigned char s4 = LookupDigits[*pSrc++];
        unsigned char d1 = ((s1 & 0x3f) << 2) | ((s2 & 0x30) >> 4);
        unsigned char d2 = ((s2 & 0x0f) << 4) | ((s3 & 0x3c) >> 2);
        unsigned char d3 = ((s3 & 0x03) << 6) | ((s4 & 0x3f) >> 0);
        *pDst++ = d1;  nLenOut++;
        if (s3 == 99) break;      // end padding found
        *pDst++ = d2;  nLenOut++;
        if (s4 == 99) break;      // end padding found
        *pDst++ = d3;  nLenOut++;
    }
    return(nLenOut);
}
std::string decompress_string(const std::string& str)
{
    z_stream zs;                        // z_stream is zlib's control structure
    memset(&zs, 0, sizeof(zs));
    inflateInit(&zs);
    zs.next_in = (Bytef*)str.data();
    zs.avail_in = str.size();
    int ret;
    char outbuffer[32768];
    std::string outstring;
    do
    {
        zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
        zs.avail_out = sizeof(outbuffer);
        ret = inflate(&zs, 0);
        if (outstring.size() < zs.total_out)
        {
            outstring.append(outbuffer, zs.total_out - outstring.size());
        }
    }
    while (ret == Z_OK);
    inflateEnd(&zs);
    return outstring;
}
int main()
{
    std::string inputData = "eAFjYoAAZiDFCMQgGgQAAJwACg==";
    //19 is hardcoded since I know its size prior to this call
    std::string res(19, '');
    FromBase64Simple((unsigned char*)inputData.c_str(), inputData.size(), (unsigned char*)res.data(), res.size());
    std::string d = decompress_string(res);
    for (int c : d)
    {
        printf("%02x", c);
    }
    printf("n");
    getchar();
    return 0;
}

输出:020000000000000003000000010000000300000000000000

" eafjyoaazidfcmqgggqaajwacg ==" base64编码。您需要首先将其解码给二进制文件,以获取可以解压缩的东西。在十六进制中表达的二进制是:

78 01 63 62 80 00 66 20 c5 08 c4 20 1a 04 00 00 9c 00 0a

这是一个有效的zlib流,它解压缩了,以十六进制表示:

02 00 00 00 00 00 00 00 03 00 00 00 01 00 00 00 03 00 00 00 00 00 00 00

您的压缩结果" x?302@?p?"最初是在二进制中,无法打印。这些问号并不是原始的问号,而是其他不打印的字节。所以不要打印。您将印刷结果转换为六角的尝试是不正确的,因为您在十六进制中有问号(3F(。