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

📄 c_zlib.c

📁 嵌入式系统中文件系统源代码
💻 C
字号:
/*************************************************************
File Name: C_ZLIB.C (RAMDisk 3.5)			     *
**************************************************************
Programmer: MSC
Last Modified Date: 2000/01/20
Compiler : GNU Cross-compiler/SDS
Platform : X86 protection mode, MIPS, Dragonball
Usage :
	The compress and uncompress function wrapper of zlib
*************************************************************/
#include <sys/syscall.h>
#include <ramdisk.h>
#include <compress.h>



#ifdef RAMDISK_ID



#ifdef RAMDISK_COMPRESS_USE_ZLIB

#include "/rtos/kernel/usrlib/tool/zlib/src/zlib.h"



/*************************************************************
Function: rd_compress_zlib
Description:
	compress the buffer
Input:
	buffer - the data to be compressed
	size - the size of data to be compressed
Output:
	buffer - the compressed data
Return value:
	the compressed data size
	-1 if failed
Note:
**************************************************************/
long rd_compress_zlib(unsigned char *buffer, long size)
{
  unsigned char *dest;
  unsigned long destLen;
  int result;
  unsigned long i;

  // the destination
  //   According to the comments in compress() of zlib, the destination
  //   buffer must be at least 0.1% larger than srcLen plus 12 bytes.
  //   Well, I make it 1% larger for safety.
  destLen = size + size / 100 + 12;
  dest = (unsigned char *)ap_malloc(destLen);

  if (dest == NULL)
  {
//	printStringLn("[rd_compress] Memory allocation error!");
	return -1;
  }

  // start compression
  result = compress(dest, &destLen, buffer, size);
  if (result != Z_OK)
  {
	ap_free(dest);
	return -1;
  }

  // compression success, copy the compressed data to user buffer
  for (i = 0; i < destLen; i++)
	buffer[i] = dest[i];

  ap_free(dest);

  return (long)destLen;
}



/*************************************************************
Function: rd_uncompress_zlib
Description:
	uncompress a block to buffer
Input:
	buffer - the data to be decompressed
	size - the size of data to be decompressed
Output:
	NONE
Return value:
	the uncompressed data size
	-1 if failed
Note:
**************************************************************/
long rd_uncompress_zlib(struct diskBlock *block, unsigned char *buffer)
{
  unsigned char *src;
  unsigned long srcLen;
  unsigned long destLen;
  int result;
  long i;

  // the source buffer and its size
  src = (unsigned char *)((unsigned long)block + sizeof(struct diskBlock));
  srcLen = block->dataSize;
  destLen = RD_BLOCK_SIZE;

  // start decompression
  result = uncompress(buffer, &destLen, src, srcLen);
  if (result != Z_OK)
  {
//	printString("[rd_uncompress] Zlib decompression failed! Error code= ");
//	printInt(result, HEX);
	return -1;
  }

  // check uncompressed size
  if (block->actualSize != destLen)
  {
//	printStringLn("[rd_uncompress] Zlib decompression size mismatch!");
//	printString("Expected size= ");
//	myPrintInt(block->actualSize, DEC);
//	printString("    Uncompressed size= ");
//	printInt(destLen, DEC);
	return -1;
  }

  for (i = destLen; i < RD_BLOCK_SIZE; i++)
	buffer[i] = 0;

  return destLen;
}

#endif	// RAMDISK_COMPRESS_USE_ZLIB



#endif	// #ifdef RAMDISK_ID



⌨️ 快捷键说明

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