[英]C++ - zlib uncompress function crashes program
我應該注意,該程序正在(至少嘗試)堅持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
變量,以防止內存泄漏。
Bytef
不適用於帶有length參數的構造函數。 您可能是說Bytef* outBuffer = new Bytef[outLen];
(方括號)。
我認為Bytef
通常Bytef
都將typedef定義為某種原始類型,因此使用它類似於new int[len]
或new uint64_t[len]
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.