嵌入式移植7Z解压缩(纯C)

news/2024/10/22 13:34:43/

本文分享一个纯C语言编写的7Z解压缩代码库,本代码库的主要目的是在嵌入式环境下使用7z解压缩文件,可以将升级包通过7z进行压缩,然后发送给设备,减小和设备传输过程中的文件大小,进而达到传输大文件的目的。

下载链接 提取码pi4t

这个库使用的是19版本的库,

这个库是单线程的,移除了多线程代码,因为多线程依赖其他库。我只需要一个独立版本。

已经通过编译。

库的目录结构如下:


#ifdef USE_WINDOWS_FILE
static WRes File_OpenW(CSzFile *p, const WCHAR *name, int writeMode)
{p->handle = CreateFileW(name,writeMode ? GENERIC_WRITE : GENERIC_READ,FILE_SHARE_READ, NULL,writeMode ? CREATE_ALWAYS : OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);return (p->handle != INVALID_HANDLE_VALUE) ? 0 : GetLastError();
}
WRes InFile_OpenW(CSzFile *p, const WCHAR *name) { return File_OpenW(p, name, 0); }
WRes OutFile_OpenW(CSzFile *p, const WCHAR *name) { return File_OpenW(p, name, 1); }
#endifWRes File_Close(CSzFile *p)
{#ifdef USE_WINDOWS_FILEif (p->handle != INVALID_HANDLE_VALUE){if (!CloseHandle(p->handle))return GetLastError();p->handle = INVALID_HANDLE_VALUE;}#elseif (p->file != NULL){int res = fclose(p->file);if (res != 0)return res;p->file = NULL;}#endifreturn 0;
}WRes File_Read(CSzFile *p, void *data, size_t *size)
{size_t originalSize = *size;if (originalSize == 0)return 0;#ifdef USE_WINDOWS_FILE*size = 0;do{DWORD curSize = (originalSize > kChunkSizeMax) ? kChunkSizeMax : (DWORD)originalSize;DWORD processed = 0;BOOL res = ReadFile(p->handle, data, curSize, &processed, NULL);data = (void *)((Byte *)data + processed);originalSize -= processed;*size += processed;if (!res)return GetLastError();if (processed == 0)break;}while (originalSize > 0);return 0;#else*size = fread(data, 1, originalSize, p->file);if (*size == originalSize)return 0;return ferror(p->file);#endif
}

static SRes SzDecodePpmd(const Byte *props, unsigned propsSize, UInt64 inSize, const ILookInStream *inStream,Byte *outBuffer, SizeT outSize, ISzAllocPtr allocMain)
{CPpmd7 ppmd;CByteInToLook s;SRes res = SZ_OK;s.vt.Read = ReadByte;s.inStream = inStream;s.begin = s.end = s.cur = NULL;s.extra = False;s.res = SZ_OK;s.processed = 0;if (propsSize != 5)return SZ_ERROR_UNSUPPORTED;{unsigned order = props[0];UInt32 memSize = GetUi32(props + 1);if (order < PPMD7_MIN_ORDER ||order > PPMD7_MAX_ORDER ||memSize < PPMD7_MIN_MEM_SIZE ||memSize > PPMD7_MAX_MEM_SIZE)return SZ_ERROR_UNSUPPORTED;Ppmd7_Construct(&ppmd);if (!Ppmd7_Alloc(&ppmd, memSize, allocMain))return SZ_ERROR_MEM;Ppmd7_Init(&ppmd, order);}{CPpmd7z_RangeDec rc;Ppmd7z_RangeDec_CreateVTable(&rc);rc.Stream = &s.vt;if (!Ppmd7z_RangeDec_Init(&rc))res = SZ_ERROR_DATA;else if (s.extra)res = (s.res != SZ_OK ? s.res : SZ_ERROR_DATA);else{SizeT i;for (i = 0; i < outSize; i++){int sym = Ppmd7_DecodeSymbol(&ppmd, &rc.vt);if (s.extra || sym < 0)break;outBuffer[i] = (Byte)sym;}if (i != outSize)res = (s.res != SZ_OK ? s.res : SZ_ERROR_DATA);else if (s.processed + (s.cur - s.begin) != inSize || !Ppmd7z_RangeDec_IsFinishedOK(&rc))res = SZ_ERROR_DATA;}}Ppmd7_Free(&ppmd, allocMain);return res;
}#endifstatic SRes SzDecodeLzma(const Byte *props, unsigned propsSize, UInt64 inSize, ILookInStream *inStream,Byte *outBuffer, SizeT outSize, ISzAllocPtr allocMain)
{CLzmaDec state;SRes res = SZ_OK;LzmaDec_Construct(&state);RINOK(LzmaDec_AllocateProbs(&state, props, propsSize, allocMain));state.dic = outBuffer;state.dicBufSize = outSize;LzmaDec_Init(&state);for (;;){const void *inBuf = NULL;size_t lookahead = (1 << 18);if (lookahead > inSize)lookahead = (size_t)inSize;res = ILookInStream_Look(inStream, &inBuf, &lookahead);if (res != SZ_OK)break;{SizeT inProcessed = (SizeT)lookahead, dicPos = state.dicPos;ELzmaStatus status;res = LzmaDec_DecodeToDic(&state, outSize, (const Byte *)inBuf, &inProcessed, LZMA_FINISH_END, &status);lookahead -= inProcessed;inSize -= inProcessed;if (res != SZ_OK)break;if (status == LZMA_STATUS_FINISHED_WITH_MARK){if (outSize != state.dicPos || inSize != 0)res = SZ_ERROR_DATA;break;}if (outSize == state.dicPos && inSize == 0 && status == LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK)break;if (inProcessed == 0 && dicPos == state.dicPos){res = SZ_ERROR_DATA;break;}res = ILookInStream_Skip(inStream, inProcessed);if (res != SZ_OK)break;}}LzmaDec_FreeProbs(&state, allocMain);return res;
}

链接: https://pan.baidu.com/s/1ldjyd3l4zyC9AOw8-ox67g 提取码: pi4t

请用户按需下载


http://www.ppmy.cn/news/1457773.html

相关文章

短视频矩阵系统源码saas开发--可视化剪辑、矩阵托管、多功能合一开发

短视频矩阵系统源码saas开发&#xff08;可视化剪辑、矩阵托管、智能私信聚合、线索转化、数据看板、seo关键词、子账号等多个板块开发&#xff09; 短视频矩阵系统是一种集成了多种功能的系统&#xff0c;旨在帮助用户在短视频平台上进行高效的内容创作、管理和发布。根据您提…

vector介绍与使用【C++】

C vector 前言一、vector的介绍c文档介绍简介 二、vector的定义和使用vector的定义vector代码演示 vector的使用vector iterator 的使用vector 空间增长问题vector 增删查改vector 迭代器失效问题引起底层空间改变eraseg与vs检测比较string迭代器失效 vector 在OJ中的使用只出现…

SGP.22-V.3.1-安全2

有任何关于GSMA\IOT\eSIM\RSP\业务应用场景相关的问题&#xff0c;欢迎W: xiangcunge59 一起讨论, 共同进步 (加的时候请注明: 来自CSDN-iot). InitialiseSecureChannel 在 SGP.22 v3.1 技术规范的第 2.5.4.1 节 "Description of InitialiseSecureChannel Block"…

纯血鸿蒙APP实战开发——短视频切换实现案例

短视频切换实现案例 介绍 短视频切换在应用开发中是一种常见场景&#xff0c;上下滑动可以切换视频&#xff0c;十分方便。本模块基于Swiper组件和Video组件实现短视频切换功能。 效果图预览 使用说明 上下滑动可以切换视频。点击屏幕暂停视频&#xff0c;再次点击继续播放…

LeetCode 每日一题 ---- 【2079.给植物浇水】

LeetCode 每日一题 ---- 【2079.给植物浇水】 2079.给植物浇水方法&#xff1a;模拟-维护水的剩余量 2079.给植物浇水 方法&#xff1a;模拟-维护水的剩余量 模拟浇水和灌水的步骤就可以了&#xff0c;当剩余水大于等于需要浇的水&#xff0c;步数累加1即可&#xff0c;当剩余…

抢占用户|AI助力企业高效挖掘潜在客户,推动高质量转化

随着人工智能&#xff08;AI&#xff09;技术的崛起&#xff0c;企业终于可以在这个数字化时代获得一种强大的工具&#xff0c;帮助企业迅速而准确地找到潜在客户。AI不仅能够处理海量的数据&#xff0c;还能自动分析和识别潜在客户的特征和行为模式&#xff0c;为企业营销提供…

GORM的常见命令

文章目录 一、什么是GORM&#xff1f;二、GORM连接mysql以及AutoMigrate创建表三、查询1、检索此对象是否存在于数据库&#xff08;First,Take,Last方法&#xff09;2、Find()方法检索3、根据指定字段查询 四、更新1、Save() 保存多个字段2、更新单个字段 五、删除 一、什么是G…

花一个月时间为 vue3 重制了 vue-styled-components

花一个月时间为 vue3 重制了 vue-styled-components 前言 styled-components 在 React 是一个超级热门的 css in js 工具库。其实 styled-components 也有 Vue 版本&#xff08;vue-styled-components&#xff09;&#xff0c;可惜的是只支持 Vue2&#xff0c;并且该项目已有…