zlib数据格式及解压缩实现

0x01 zlib和其他压缩的魔术头


一般来说压缩文件都有个魔术头,用于区分不同的压缩文件对应不同的解压缩算法。


7z文件: 

00000000   37 7A BC AF 27 1C 00 03  CD F7 CC 2E 66 6A 33 00   7z集'   枉?fj3 


tar.xz文件

00000000   FD 37 7A 58 5A 00 00 04  E6 D6 B4 46 02 00 21 01   ?zXZ   嬷碏  ! 


zip和apk文件

00000000   50 4B 03 04 14 00 00 00  08 00 A7 AD CF 48 D5 52   PK        Л螲誖

rar文件
00000000   52 61 72 21 1A 07 00 CF  90 73 00 00 0D 00 00 00   Rar!   ?s      

zlib文件

00000000   78 01 ED 9D 0B 94 1C 57  79 E7 AB A6 9F 33 9A 99   x ? ?Wy绔3殭



0x02 zlib在JAVA和c++的不同解压方法


JAVA:

public static byte[] decompress(byte[] compress) throws Exception {  ByteArrayInputStream bais = new ByteArrayInputStream(compress);  InflaterInputStream iis = new InflaterInputStream(bais);  ByteArrayOutputStream baos = new ByteArrayOutputStream();  int c = 0;  byte[] buf = new byte[BUFFER_SIZE];  while (true) {  c = iis.read(buf);  if (c == EOF)  break;  baos.write(buf, 0, c);  }  baos.flush();  return baos.toByteArray();  } 

C++

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "zlib.h"#if defined(MSDOS) || defined(OS2) || defined(WIN32) || defined(__CYGWIN__)
#  include <fcntl.h>
#  include <io.h>
#  define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
#else
#  define SET_BINARY_MODE(file)
#endif#define CHUNK 16384/* Compress from file source to file dest until EOF on source.def() returns Z_OK on success, Z_MEM_ERROR if memory could not beallocated for processing, Z_STREAM_ERROR if an invalid compressionlevel is supplied, Z_VERSION_ERROR if the version of zlib.h and theversion of the library linked do not match, or Z_ERRNO if there isan error reading or writing the files. */
int def(FILE *source, FILE *dest, int level)
{int ret, flush;unsigned have;z_stream strm;unsigned char in[CHUNK];unsigned char out[CHUNK];/* allocate deflate state */strm.zalloc = Z_NULL;strm.zfree = Z_NULL;strm.opaque = Z_NULL;ret = deflateInit(&strm, level);if (ret != Z_OK)return ret;/* compress until end of file */do {strm.avail_in = fread(in, 1, CHUNK, source);if (ferror(source)) {(void)deflateEnd(&strm);return Z_ERRNO;}flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;strm.next_in = in;/* run deflate() on input until output buffer not full, finishcompression if all of source has been read in */do {strm.avail_out = CHUNK;strm.next_out = out;ret = deflate(&strm, flush);    /* no bad return value */assert(ret != Z_STREAM_ERROR);  /* state not clobbered */have = CHUNK - strm.avail_out;if (fwrite(out, 1, have, dest) != have || ferror(dest)) {(void)deflateEnd(&strm);return Z_ERRNO;}} 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 Z_OK;
}/* Decompress from file source to file dest until stream ends or EOF.inf() returns Z_OK on success, Z_MEM_ERROR if memory could not beallocated for processing, Z_DATA_ERROR if the deflate data isinvalid or incomplete, Z_VERSION_ERROR if the version of zlib.h andthe version of the library linked do not match, or Z_ERRNO if thereis an error reading or writing the files. */
int inf(FILE *source, FILE *dest)
{int ret;unsigned have;z_stream strm;unsigned char in[CHUNK];unsigned char out[CHUNK];/* allocate inflate state */strm.zalloc = Z_NULL;strm.zfree = Z_NULL;strm.opaque = Z_NULL;strm.avail_in = 0;strm.next_in = Z_NULL;ret = inflateInit(&strm);if (ret != Z_OK)return ret;/* decompress until deflate stream ends or end of file */do {strm.avail_in = fread(in, 1, CHUNK, source);if (ferror(source)) {(void)inflateEnd(&strm);return Z_ERRNO;}if (strm.avail_in == 0)break;strm.next_in = in;/* run inflate() on input until output buffer not full */do {strm.avail_out = CHUNK;strm.next_out = out;ret = inflate(&strm, Z_NO_FLUSH);assert(ret != Z_STREAM_ERROR);  /* state not clobbered */switch (ret) {case Z_NEED_DICT:ret = Z_DATA_ERROR;     /* and fall through */case Z_DATA_ERROR:case Z_MEM_ERROR:(void)inflateEnd(&strm);return ret;}have = CHUNK - strm.avail_out;if (fwrite(out, 1, have, dest) != have || ferror(dest)) {(void)inflateEnd(&strm);return Z_ERRNO;}} while (strm.avail_out == 0);/* done when inflate() says it's done */} while (ret != Z_STREAM_END);/* clean up and return */(void)inflateEnd(&strm);return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}/* report a zlib or i/o error */
void zerr(int ret)
{fputs("zpipe: ", stderr);switch (ret) {case Z_ERRNO:if (ferror(stdin))fputs("error reading stdin\n", stderr);if (ferror(stdout))fputs("error writing stdout\n", stderr);break;case Z_STREAM_ERROR:fputs("invalid compression level\n", stderr);break;case Z_DATA_ERROR:fputs("invalid or incomplete deflate data\n", stderr);break;case Z_MEM_ERROR:fputs("out of memory\n", stderr);break;case Z_VERSION_ERROR:fputs("zlib version mismatch!\n", stderr);}
}/* compress or decompress from stdin to stdout */
int main(int argc, char **argv)
{int ret;/* avoid end-of-line conversions */SET_BINARY_MODE(stdin);SET_BINARY_MODE(stdout);/* do compression if no arguments */if (argc == 1) {ret = def(stdin, stdout, Z_DEFAULT_COMPRESSION);if (ret != Z_OK)zerr(ret);return ret;}/* do decompression if -d specified */else if (argc == 2 && strcmp(argv[1], "-d") == 0) {ret = inf(stdin, stdout);if (ret != Z_OK)zerr(ret);return ret;}/* otherwise, report usage */else {fputs("zpipe usage: zpipe [-d] < source > dest\n", stderr);return 1;}
}




本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/499595.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python3 Crypto环境

前言 最开始想尝试在windows下面安装python3.6&#xff0c;虽然python安装成功&#xff0c;但在安装Cryto模块用pip3 install pycrypto老是会报错。老夫搞了半天&#xff0c;最终决定在linux下面去做。 以下流程限于linux系统&#xff1a; 0x00 安装python apt-get install p…

win10用Eclipse+OpenJTag对S3C2440开发板进行动态调试

0 背景在S3C2400开发板裸板调试程序中&#xff0c;常用调试手段有三种&#xff1a;点灯法&#xff0c;串口打印&#xff0c;OpenOCD。OpenOCD又分命令行和图形界面(Eclipse)。点灯发和串口打印调试效率都很低&#xff0c;若能掌握第三种调试方法&#xff0c;会让开发过程变得高…

无源码情况下动态调试混淆的java程序

逆向工程JAVA通常是非常简单的&#xff0c;因为优秀的JAVA二进制反编译器已经存在多年。类似于jd-gui工具和恢复java二进制文件源代码功能也做的非常出色的。在这种情况下我们需要动态调试java反编译java程序的情况下&#xff0c;可以从反编译导出然后导入java IDE如Eclipse作为…

mdb access2000 中文密码破解

access数据库破解工具很多&#xff0c;密码能不用费多大功夫就能破解出来&#xff0c;但是对于包含特殊字符包括中文字符的密码&#xff0c;就算破解出来后想通过数据库工具查看&#xff0c;复制粘贴到密码输入框实际都起不了作用 已迁移到&#xff1a;分享最前沿的安全信息-a…

OpenJTAG调试S3C2440裸板程序

0x00 懵逼当你写好的初始化代码head.S和链接脚本uart.lds共同编译出来的*.bin&#xff0c;烧录到NandFlash中的时候&#xff0c;发现串口输出一片空白&#xff0c;这时你的想法是什么&#xff0c;砸电脑还是干点其他有用的事&#xff1f;还是老实的搭建调试环境吧&#xff0c;上…

APK逆向之静态分析篇

0x00 APK包结构0x01 APK反编译-apktool啰嗦一句&#xff0c;反编译之前配置好java环境&#xff0c;具体JDK安装过程&#xff0c;请参照之前的文章。下载最新版本的apktool.jar&#xff0c;并在当前目录下编辑脚本apktool.bat&#xff0c;内容如下&#xff1a; echo off set PAT…

S3C2440 lds链接脚本解析

1. SECTIONS到底意味着什么在一个裸版程序里面含有*.lds文件&#xff0c;而lds文件意味着如果你的程序烧录在nandflash&#xff0c;那在nandflash的内存将根据lds文件指定偏移来分布&#xff0c;下面从不同场景来解释SECTIONS的内容。2. 小于4K程序若程序小于4K&#xff0c;那…

安装qt5.9.5 windows环境

下载&#xff1a;用国外链接下载慢&#xff0c;还是乖乖用国内链接地址吧&#xff0c;我这里5.9.5http://mirrors.ustc.edu.cn/qtproject/archive/qt/5.9/5.9.5/qt-opensource-windows-x86-5.9.5.exe。安装&#xff1a; 在安装的时候需要创建qt账号&#xff0c;然后根据你的vis…

qt在visual studio 2015下的使用

创建工程&#xff1a; 打开visual studio&#xff0c;按上一篇文章的方式创建新工程QtGuiApplication1&#xff0c;默认我们可以看到里面会出现QtGuiApplication1这个类是继承于QMainWindow这个类的。在创建过程中注意下图选项&#xff1a;有三个对象分别是QMainwindow&#xf…

qt 收缩窗体

效果图&#xff1a;功能拆分图&#xff1a;代码&#xff1a; QtStubOption.cpp QtSubOption::QtSubOption(QWidget *parent): QLabel(parent) {ui.setupUi(this);m_GuiShow SHOWGUI;setMouseTracking(true);m_PicStatus[SHOWGUI] ":/QtGuiApplication3/tile";m_Pic…

Android的ELF文件重定位详解,包括64位

0x01 引言 ELF文件格式&#xff0c;主要基于两种&#xff0c;一种是基于链接视图&#xff0c;链接视图即是基于节(Section)来进行解析&#xff0c;一种是基于执行视图&#xff0c;执行视图即是基于段(Segment)来进行解析。前一种是用于静态分析的时候&#xff0c;譬如IDA载入。…

lua安全之关于lua扩展第三方库

android lua require第三方扩展库有三种方式&#xff1a; 1. 用c实现独立的lua模块作为android的第三方动态库来引入&#xff0c;优点是lua扩展库独立方便更新替换&#xff0c;缺点是需要修改虚拟机&#xff0c;开启宏支持dlopen调用的方式&#xff0c;并且还需要设置lua寻找so…

IBinder获取手机服务信息异常

小米8 利用IBinder transact获取服务的接口名字&#xff0c;结果出现以下异常&#xff1a; W/System.err: java.lang.SecurityException W/System.err: at android.os.BinderProxy.transactNative(Native Method) W/System.err: at android.os.BinderProxy.transact(B…

服务动态选择域名问题

服务动态选择域名三种方案&#xff1a; 1. 通过DNS就近调度。缺点&#xff1a;DNS的ip采集库维护很麻烦。优点&#xff1a;业务无缝接入。 2. sim卡和语言判断国家&#xff0c;做国家与域名的映射&#xff0c;缺点&#xff1a;国际漫游不准确。优点&#xff1a;一般能准确路由…

安装openCV到VS2010,Win764位机时遇到的问题的解决办法

安装了一天的OpenCV&#xff0c;终于成功了&#xff0c;打算把遇到的问题和解决方案记录下来&#xff0c;以便以后再遇到时不要重蹈覆辙。 首先最常规的步骤就不说了&#xff0c;我是完全安装OpenCV中文网上的教程一步一步配置的。链接如下&#xff1a; http://wiki.opencv.o…

初学C遇到的一些知识点汇总

<span style"font-family: arial, courier new, courier, 宋体, monospace; white-space: pre-wrap;">本人是以C#为入门语言&#xff0c;现在开始学习数字图像处理&#xff0c;正在学习c语言&#xff0c;在这方面还是小白&#xff0c;所以打算把遇到的问题进行…

利用OpenCV的Haar特征目标检测方法进行人脸识别的尝试(一)

一、前言 由于还处于学习阶段&#xff0c;大多数内容都是从网上学习借鉴的&#xff0c;重复的内容就不多赘述&#xff0c;只是将自己的经验和想法分享出来。感觉不错的学习资源如下 http://www.cnblogs.com/tornadomeet/archive/2012/03/28/2420936.html http://www.cnblog…

Haar特征原理与icvCreateIntHaarFeatures方法的具体实现附详细注释—— 人脸识别的尝试系列(二)

带着强烈的兴趣&#xff0c;上周开始人脸识别的尝试与学习&#xff0c;并且将具体的操作过程记录了下来 链接如下&#xff1a;http://blog.csdn.net/u011583927/article/details/44627493 这周开始了对于算法的深入学习&#xff0c;下面进入正题。 Haar特征的原理是什么&…

createsamples.cpp中生成vec文件的实现及详细注释、图解——人脸识别的尝试系列(三)

在我们开始训练我们的Haar分类器之前&#xff0c;首先要对样本进行处理。 人脸识别的尝试系列&#xff08;一&#xff09;中&#xff1a;http://blog.csdn.net/u011583927/article/details/44627493 我们已经提到了如何准备我们的样本&#xff0c;在如下图准备好样本之后 需…

常用函数总结——sprintf

本文内容转自http://blog.csdn.net/sjf331/article/details/339254 printf 可能是许多程序员在开始学习C 语言时接触到的第二个函数&#xff08;我猜第一个是main&#xff09;&#xff0c;说 起来&#xff0c;自然是老朋友了&#xff0c;可是&#xff0c;你对这个老朋友了解多吗…