谷歌 brotli 解压缩简易版
By
terrorist
at 2022-02-05 • 0人收藏 • 822人看过
一,库源码:
namespace aaz.libbrotli{ _dll := ..raw.loadDll($"~\lib\aaz\libbrotli\.res\libbrotli.dll",,"cdecl") decompress = function(str){ var state = _dll.BrotliDecoderCreateInstanceP(0, 0, 0); var availableIn = {int value=#str}; var nextIn = {ptr v = ..raw.buffer(str)} var ptr = ..raw.realloc(1) var result = 3/*_BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT*/; while(result == 3/*_BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT*/){ var availableOut = {int value=0}; result = _dll.BrotliDecoderDecompressStream( state, availableIn, nextIn, availableOut, 0, 0 ); var buffer = _dll.BrotliDecoderTakeOutputP(state, availableOut) if(availableOut.value){ ptr = ..raw.concat(ptr, buffer, availableOut.value) } } var ret; if(result == 1/*_BROTLI_DECODER_RESULT_SUCCESS*/ ){ ret = ..raw.str(ptr) } _dll.BrotliDecoderDestroyInstance(state); ptr = ..raw.realloc(0, ptr) return ret; }; }
二, 使用:
io.open() import inet.whttp import aaz.libbrotli; var whttp = inet.whttp(,"HTTP://127.0.0.1:10083") whttp.addHeaders = /** accept-encoding: br user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 **/ var html = whttp.get( "https://www.youtube.com/" ) if(html){ var ret = aaz.libbrotli.decompress(html) if(ret){ io.print(ret) } } execute("pause")
三,参考资料:
2, BrotliDecoderDecompressStream 函数用法 https://brotli.org/decode.html#a234
3, BrotliDecoderTakeOutput 函数用法 https://brotli.org/decode.html#a234
四, dll 编译方法:
1, 安装 Perl
2, 下载 1.0.9 版本的源码并解压 https://github.com/google/brotli/archive/refs/tags/v1.0.9.zip
3, 下载编译脚本 winbro.bat https://github.com/diovoemor/winbro 放入源码的目录,然后运行
4, 打开 VS 的命令行工具 x86 Native Tools Command Prompt 并切换到源码目录
5, 执行命令 nmake /f makefile.msvc
6, 等待编译 dll 完成
五, 总结:
BrotliDecoderDecompressStream 函数里面的参数 const uint8_t ** 可用
{ptr v = ..raw.buffer(str)}
表示
1 个回复 | 最后更新于 2022-02-01
登录后方可回帖
新年第一贴,多谢分享,学习了。