C ++ - zlib解压缩函数崩溃程序

时间:2013-04-03 01:02:41

标签: c++ compression zlib

我应该注意到这个程序正在坚持(至少,试图)Tiled API

我正在尝试在zlib中使用uncompress()函数,但由于某种原因,每当我调用该函数时,我的程序都会崩溃。这就是我所拥有的,所有参数看起来都正确,所以我不确定问题是什么。

// const char* filedata passed in function is Zlib compressed and Base64 encoded

uLong inLen = static_cast<uLong>((strlen(filedata)*6)/8);   // Calculate the length
std::string inBuffer = BASE64_DECODE(filedata);    // My data

uLongf outLen = static_cast<uLongf>(width*height*4);    // Tiled API specification
Bytef* outBuffer = new Bytef(outLen);     // Destination

int ret = uncompress(outBuffer, &outLen,
              reinterpret_cast<Bytef*>(&inBuffer[0]), inLen);

ret什么都不返回,程序崩溃了。有人有什么想法吗?这是BASE64_DECODE函数:

std::string BASE64_DECODE(std::string const& encoded_string)
{
    int in_len = encoded_string.size();
    int i = 0;
    int j = 0;
    int in_ = 0;

    unsigned char char_array_4[4], char_array_3[3];
    std::string ret;

    while(in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_]))
    {
        char_array_4[i++] = encoded_string[in_]; in_++;

        if (i == 4)
        {
            for (i = 0; i <4; i++)
                char_array_4[i] = base64_chars.find(char_array_4[i]);

            char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
            char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
            char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

            for (i = 0; (i < 3); i++)
                ret += char_array_3[i];

            i = 0;
        }
    }

    if(i)
    {
        for (j = i; j <4; j++)
          char_array_4[j] = 0;

        for (j = 0; j <4; j++)
          char_array_4[j] = base64_chars.find(char_array_4[j]);

        char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
        char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
        char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

        for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
    }

    return ret;
}

编辑:如果您将来要查看此内容,请确保在程序后期delete outBuffer变量以防止内存泄漏。

1 个答案:

答案 0 :(得分:0)

Bytef不与带有长度参数的构造函数一起使用。你可能意味着Bytef* outBuffer = new Bytef[outLen];(方括号)。

我认为Bytef通常是针对某种原始类型的类型定义,因此使用它类似于new int[len]new uint64_t[len]