⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lzmautil.c

📁 LZMA压缩解压算法
💻 C
字号:
/* LzmaUtil.c -- Test application for LZMA compression
2008-11-23 : Igor Pavlov : Public domain */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include "Types.h"
#include "LzmaDec.h"
#include "LzmaEnc.h"
#define _CRT_SECURE_NO_WARNINGS
#define _SZ_ALLOC_DEBUG

#ifdef _SZ_ALLOC_DEBUG
int g_allocCount = 0;
int g_allocCountMid = 0;
int g_allocCountBig = 0;
#endif
/*********************以下为内存控制函数封装举例*************************************/
void *MyAlloc(size_t size);
void MyFree(void *address);
static void *SzAlloc(size_t size) {return MyAlloc(size); }
static void SzFree(void *address) {MyFree(address); }
static ISzAlloc g_Alloc = { SzAlloc, SzFree };


void *MyAlloc(size_t size)
{
  if (size == 0)
    return 0;
  #ifdef _SZ_ALLOC_DEBUG
  {
    void *p = malloc(size);
    fprintf(stderr, "\nAlloc %10d bytes, count = %10d,  addr = %8X", size, g_allocCount++, (unsigned)p);
    return p;
  }
  #else
  return malloc(size);
  #endif
}

void MyFree(void *address)
{
  #ifdef _SZ_ALLOC_DEBUG
  if (address != 0)
    fprintf(stderr, "\nFree; count = %10d,  addr = %8X", --g_allocCount, (unsigned)address);
  #endif
  free(address);
}

/********************************************************************************************/
int PrintError(char *buffer, const char *message)
{
  strcat(buffer, "\nError: ");
  strcat(buffer, message);
  strcat(buffer, "\n");
  return 1;
}



int main()
{
  int numArgs = 4;
  char *rs = NULL;
  const char *args[4];
  FILE*  inbuff;
  FILE*  outbuff;
  int res = 0;
  long pos = 0;
  Bool useOutFile = 1;
  SizeT fileSize;
  SizeT destlen = 0;
  unsigned char *infull = NULL;
  unsigned char *outfull = NULL;
  unsigned char *testfull = NULL;
  CLzmaEncProps *props = NULL;
  ISzAlloc alloc;
  ICompressProgress *progress = NULL;
  ELzmaStatus ppz = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
  Byte header[13] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
  size_t headerSize = LZMA_PROPS_SIZE;
  size_t size = LZMA_PROPS_SIZE;
  unsigned long  unpackSize = 50000000;
  size_t sizemalloc = 0 ;
  LzEncode *pe = NULL;
  LzDecode *pd = NULL;

  inbuff = NULL;
  outbuff = NULL;
#if 1
  args[0] = "datasrc.bin";
  args[1] =	"data.bin";
  args[2] = "e";
#else
  args[0] = "data.bin";
  args[1] =	"data2.bin";
  args[2] = "d";
#endif

	if (!(inbuff=fopen(args[0], 0 ? "wb+" : "rb")))
	{
		 return PrintError(rs, "Can not open input file");
	}
   
	  if (!(outbuff=fopen(args[1], 1 ? "wb+" : "rb")))
	  {
			return PrintError(rs, "Can not open output file");
	  }
	alloc.Alloc = SzAlloc;
	alloc.Free = SzFree;
	pos = ftell(inbuff);
	res = fseek(inbuff, 0, SEEK_END);
	fileSize = ftell(inbuff);
	fseek(inbuff, pos, SEEK_SET);
	infull = (unsigned char*)malloc(fileSize);
	
	if (!fread(infull,1,fileSize,inbuff))
		return  0;



  if (args[2] == "e")
  {

	  destlen = fileSize;
	  outfull = (unsigned char*)malloc(destlen);
	  pe = (LzEncode *)malloc(sizeof(LzEncode));
	  pe->src = infull;
	  pe->srclen = fileSize;
	  pe->dest = outfull;
	  pe->destlen = destlen;
	  pe->Alloc = alloc.Alloc;
	  pe->Free =  alloc.Free;
	  pe->memcpy = memcpy;
	  pe->memset = memset;
	  pe->ratio_level = 1; 
	  res=Encode(pe);
	
		 
	 if (!fwrite(outfull,1,res,outbuff))
	 {
		 return 0;
	 }
  }
  else
  {		
	  destlen = unpackSize;
	  pd = (LzDecode *)malloc(sizeof(LzDecode));
	  outfull = (unsigned char*)malloc(unpackSize);
	  pd->src = infull;
	  pd->srclen = fileSize;
	  pd->dest = outfull;
	  pd->destlen = &destlen;
	  pd->Alloc = alloc.Alloc;
	  pd->Free = alloc.Free;
	  pd->memcpy = memcpy;
	  pd->memset = memset;
	  

	  res = Decode(pd);

	 if (!fwrite(outfull,1,res,outbuff))
	 {
		 return 0;
	 }
  }


  if (useOutFile)
  {
	if (outbuff != NULL)
	{
		int res = fclose(outbuff);
		if (res != 0)
			return res;
		outbuff = NULL;
	}
	if (inbuff != NULL)
	{
		int res = fclose(inbuff);
		if (res != 0)
			return res;
		inbuff = NULL;
	}
  }

  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -