|
①准备一个简单的Demo例程,这里以STM32F407为例,需要有外扩内存至少有1M的外扩RAM大小;
②去7Zip官网下载源码解压如下:
③将源码添加到准备好的工程中,截图如下:
④使用的压缩测试代码如下:
static void *SzAlloc(void *p, size_t size) { (void)p; return MyAlloc(size); }
static void SzFree(void *p, void *address) { (void)p; MyFree(address); }
static ISzAlloc stAllocator = { (void *(*)(ISzAllocPtr, size_t))SzAlloc, (void (*)(ISzAllocPtr, void *))SzFree };
#define COMP_BUFLEN 1000
#define UNCOMP_BUFLEN 5000
char* pTestData = "DEADBEEF|askjd9827x34bnzkjaskjd9827x34bnzkjaskjd9827x34bnzkjaskjd9827x34bnzkjaskjd9827x34bnzkj#%wioeruxo3b84nxijlhwqdzhwerzu39b87r#_3b9p78bznor83y4frAAAABBBBBBCCCCCDDDDDDFFFF";
unsigned char pCompBuf[1000]; /* Compressed data */
unsigned char pUnCompBuf[5000]; /* Unompressed data - at the end its contents */
int LZMA_Compress(uint8_t *dst, size_t *dst_len, uint8_t *src, size_t src_len)
{
CLzmaEncProps stProps;
SRes rc;
size_t nPropsBufLen = LZMA_PROPS_SIZE;
LzmaEncProps_Init(&stProps);
stProps.level = 9;
stProps.lc = 0;
stProps.lp = 0;
stProps.dictSize = 1 << 12;
stProps.numThreads = 1;
// PRINT_APP_INFO("dst_len: %d, src_len: %d, prop_len:%d\r\n", *dst_len, src_len, nPropsBufLen);
rc = LzmaEncode(dst + LZMA_PROPS_SIZE, dst_len, src, src_len, &stProps, dst, &nPropsBufLen, 0, NULL, &stAllocator, &stAllocator);
if(rc != SZ_OK)
{
PRINT_APP_INFO("LZMA compression failed (rc = %d)\r\n", rc);
return -1;
}
else
{
PRINT_APP_INFO(RTT_CTRL_BG_CYAN"压缩: %8d-->%-8d"RTT_CTRL_RESET, src_len, *dst_len);
PRINT_APP_INFO("\r\n");
return 0;
}
}
int LZMA_DeCompress(uint8_t *dst, size_t *dst_len, uint8_t *src, size_t src_len)
{
ELzmaStatus nStatus;
SRes rc = LzmaDecode(dst, dst_len, src + LZMA_PROPS_SIZE, &src_len, src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &nStatus, &stAllocator);
if ( rc != SZ_OK )
{
PRINT_APP_INFO("LZMA decompression failed (rc = %d, status = %d)\r\n", rc, nStatus);
return -1;
}
else
{
PRINT_APP_INFO(RTT_CTRL_BG_CYAN"解压: %8d-->%-8d"RTT_CTRL_RESET, src_len, *dst_len);
PRINT_APP_INFO("\r\n");
return 0;
}
}
void LZMA_Test(void)
{
size_t nTestDataLen = strlen(pTestData);
size_t nCompBufLen = COMP_BUFLEN;
size_t nUnCompBufLen = UNCOMP_BUFLEN;
PRINT_APP_INFO("LZMA: %s, %s\r\n", MY_VERSION, MY_CPU_NAME);
memset(pCompBuf, 0, COMP_BUFLEN);
memset(pUnCompBuf, 0, UNCOMP_BUFLEN);
PRINT_APP_INFO(RTT_CTRL_TEXT_BRIGHT_RED"分配/释放 编号\t内存分配/释放大小\t地址\t内存总占用\r\n"RTT_CTRL_RESET);
if(LZMA_Compress(pCompBuf, &nCompBufLen, (uint8_t *)pTestData, nTestDataLen) != SZ_OK)
{
PRINT_APP_INFO("LZMA_Compress ERR...%u...\r\n", __LINE__);
while(1);
}
nUnCompBufLen = nTestDataLen;
if(LZMA_DeCompress(pUnCompBuf, &nUnCompBufLen, pCompBuf, nCompBufLen) != SZ_OK)
{
PRINT_APP_INFO("LZMA_DeCompress ERR...%u...\r\n", __LINE__);
while(1);
}
if ((nUnCompBufLen != nTestDataLen) || memcmp(pTestData, pUnCompBuf, nTestDataLen) )
{
PRINT_APP_INFO("Compression and/or decompression failed!\r\n");
PRINT_APP_INFO("Input data length [nTestDataLen] : %d\r\n", nTestDataLen);
PRINT_APP_INFO("Compressed data length [nCompBufLen] : \r%d\n", nCompBufLen);
PRINT_APP_INFO("Uncompressed data length [nUnCompBufLen]: %d\r\n", nUnCompBufLen);
if (memcmp(pTestData, pUnCompBuf, nTestDataLen) )
{
PRINT_APP_INFO("pTestData and pUnCompBuf contain different data!\r\n");
}
else
{
PRINT_APP_INFO("dst_len:%d\r\n,dst_data:%s\r\n,src_len:%d\r\n,src_data:%s\r\n\r\n",nUnCompBufLen, pUnCompBuf, nTestDataLen, pTestData);
}
}
}
⑤测试结果如下:
⑥工程源码:
|
评分
-
查看全部评分
|