android c++ gizp 调用 so,在Web浏览器(C++)中定义的ZLIB GZIP无效响应

我用C++编写了一个FASTCGI应用程序。我喜欢将gzip压缩的响应发送到客户机。

(zlib版本“1.2.11”)

以下是我的源代码示例:

#pragma warning (disable : 4231)

#pragma warning(disable : 4996)

//3:45 PM 11/24/2018

#if !(defined(_WIN32)||defined(_WIN64)) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))

#error Have to check !TODO

#else

#if !defined(_IOSTREAM_)

#include

#endif//!_IOSTREAM_

#ifndef _WINDOWS_

#include

#endif//!_WINDOWS_

#endif//_WIN32||_WIN64/__unix__

#if !defined(_INC_STDIO)

#include /* defines FILENAME_MAX, printf, sprintf */

#endif//!_INC_STDIO

#ifndef _XSTRING_

#include // !_XSTRING_// memcpy, memset

#endif //!_XSTRING_

#if !defined(ZLIB_H)

#include

#endif//!ZLIB_H

#if !defined(_SSTREAM_)

#include // std::stringstream

#endif//_SSTREAM_

#if !defined(CHUNK)

#define CHUNK 16384

#endif//!CHUNK

#ifndef OS_CODE

# define OS_CODE 0x03 /* assume Unix */

#endif//!OS_CODE

#if MAX_MEM_LEVEL >= 8

# define DEF_MEM_LEVEL 8

#else

# define DEF_MEM_LEVEL MAX_MEM_LEVEL

#endif//!MAX_MEM_LEVEL

#if !defined(assert)

#define assert(expression) ((void)0)

#endif//!assert

static int gz_magic[2] = { 0x1f, 0x8b }; /* gzip magic header */

void __write_magic_header(std::stringstream&output) {

char*dest = (char*)malloc(10);

sprintf(dest, "%c%c%c%c%c%c%c%c%c%c", gz_magic[0], gz_magic[1], Z_DEFLATED, 0 /*flags*/, 0, 0, 0, 0 /*time*/, 0 /*xflags*/, OS_CODE);

output.write(const_cast(dest), 10);

free(dest);

};

int ____def_strm(std::stringstream&source, std::stringstream&dest, int level = Z_BEST_SPEED) {

//6:00 AM 1/18/2019

int ret, flush;

unsigned have;

z_stream strm;

/* allocate deflate state */

strm.zalloc = Z_NULL;

strm.zfree = Z_NULL;

strm.opaque = Z_NULL;

ret = deflateInit2_(&strm, level, Z_DEFLATED,

-MAX_WBITS,

DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,

ZLIB_VERSION, (int)sizeof(z_stream));

if (ret != Z_OK)

return ret;

/* compress until end of stream */

std::streamsize n;

source.seekg(0, std::ios::end);//Go to end of stream

std::streamoff size = source.tellg();

source.seekg(0, std::ios::beg);//Back to begain of stream

int write_len = 0;

do {

char in[CHUNK];

n = source.rdbuf()->sgetn(in, CHUNK);

strm.avail_in = (uInt)n;

size -= n;

flush = size <= 0 ? Z_FINISH : Z_NO_FLUSH;

strm.next_in = (Bytef*)in;

/* run deflate() on input until output buffer not full, finish

compression if all of source has been read in */

do {

char out[CHUNK];

strm.avail_out = CHUNK;

strm.next_out = (Bytef*)out;

ret = deflate(&strm, flush); /* no bad return value */

assert(ret != Z_STREAM_ERROR); /* state not clobbered */

have = CHUNK - strm.avail_out;

dest.write(out, have);

write_len += have;

} while (strm.avail_out == 0);

assert(strm.avail_in == 0); /* all input will be used */

/* done when last data in file processed */

} while (flush != Z_FINISH);

assert(ret == Z_STREAM_END); /* stream will be complete */

/* clean up and return */

(void)deflateEnd(&strm);

return write_len;

};

void compress_gzip (std::stringstream&source, std::stringstream&output) {

__write_magic_header(output);

____def_strm(source, output);

return;

};

void gzip_test(int loop) {

std::stringstream body(std::stringstream::in | std::stringstream::out | std::stringstream::binary);

for (int i = 0; i < loop; i++) {

body << "Hello World
";

body << "general-purpose programming language";

body << "\r\n";

}

std::stringstream compressed(std::stringstream::in | std::stringstream::out | std::stringstream::binary);

compress_gzip(body, compressed);

std::stringstream().swap(body);

std::cout << compressed.str();

std::stringstream().swap(compressed);

};

void write_header(const char* ct) {

std::cout << "Content-Type:" << ct << "\n";

std::cout << "Accept-Ranges:bytes\n";

};

int main(int argc, char *argv[], char*envp[]) {

//100 problem ==> ERR_CONTENT_DECODING_FAILED

//1000 problem ==> ERR_CONTENT_DECODING_FAILED

//10000 Ok

write_header("text/plain");

std::cout << "Content-Encoding:gzip\n";

std::cout << "\r\n";

gzip_test(10000);

return EXIT_SUCCESS;

};

它可以工作,但我认为这个程序有缺陷,但我无法解决。

问题如下:

如果

gzip_test(10000);

那么好吧

如果

gzip_test(100);

浏览器显示

ERR_CONTENT_DECODING_FAILED

如果

gzip_test(1000);

浏览器显示

错误\内容\解码\失败

请帮我找出这个错误。

成功响应:

t9TgM.jpg

错误响应:

Z0imH.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
"gzip: stdin: not in gzip format"是一个错误提示,表示输入的数据不符合gzip格式。这个错误通常在尝试使用gzip命令解压或压缩文件时出现。这个错误可能有多个原因,例如输入的文件不是有效的gzip文件、文件已经被破坏或者文件格式不正确。 要解决这个问题,首先确认你正在操作的文件是有效的gzip文件。你可以通过使用file命令检查文件类型,例如"file filename"。如果文件类型不是gzip,那么可能是文件格式不正确或者文件已经被破坏。 如果确认文件是有效的gzip文件,但仍然出现这个错误,可能是gzip工具本身的问题。你可以尝试使用其他的解压工具,如tar命令进行解压。例如,你可以使用"tar -xvf filename.tar.gz"命令来解压gzip压缩的文件。 另外,还需要确保你的操作系统上已经安装了gzip和tar工具,并且它们的版本是最新的。你可以通过运行"gzip --version"和"tar --version"来检查它们的版本信息。如果发现版本过旧,建议更新到最新版本。 总结起来,要解决"gzip: stdin: not in gzip format"错误,你可以按照以下步骤进行操作: 1. 确认你正在操作的文件是有效的gzip文件,通过使用file命令检查文件类型。 2. 如果文件类型不是gzip,检查文件格式是否正确或者文件是否被破坏。 3. 尝试使用其他解压工具,如tar命令进行解压。 4. 确保你的操作系统上已经安装了最新版本的gzip和tar工具。如果版本过旧,更新到最新版本。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [gzip: stdin: not in gzip format 的解决方法](https://blog.csdn.net/sethinking/article/details/88189735)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [易语言GZIP数据解压缩](https://download.csdn.net/download/weixin_38661100/12643599)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值