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

📄 collapse.cpp

📁 zlib-1.2.3.tar是新的zlib库藏 用于压缩 等等
💻 CPP
字号:
/* Created:06-02-00 */
/* By Jeff Connelly */

/* "Collapse" lossy text compression method */
/* This only works for pure ASCII with no extended ASCII bytes, but has   */
/* a guarenteed ratio of compressing 8 bytes to 7 bytes (~12.5% decrease) */

/* Compressed 1990 CIA World Factbook from 1,967,691 to 1,721,731, 12.5%  */
/* 245960 bytes saved, about 240.2K */

/* Bugs: Extra data produces garbage at end of decoded file, usually ^?   */

/* The collapse compression method is hereby placed in public domain by   */
/* Jeff Connelly, 2000 */

#include "codec.h"

/* Bit routines - from lzari.cpp with a few modifications */
static void PutBit(int bit)  /* Output one bit (bit = 0,1) */
{
	static unsigned int  buffer = 0, mask = 128;
	
	if (bit) buffer |= mask;
	if ((mask >>= 1) == 0) {
        write_byte(buffer);
        buffer = 0;  mask = 128;
	}
}

static void FlushBitBuffer(void)  /* Send remaining bits */
{
	int  i;
	
    for (i = 0; i < 7; ++i) PutBit(0);
}

static int GetBit(void)  /* Get one bit (0 or 1) */
{
	static unsigned int  buffer, mask = 0;
	
	if ((mask >>= 1) == 0) {
        buffer = read_byte();  mask = 128;
	}
	return ((buffer & mask) != 0);
}


int encode_collapse()
{
    while(!end_of_data())
    {
        GetBit();           /* this is where the compression happens */
        PutBit(GetBit());
        PutBit(GetBit());
        PutBit(GetBit());
        PutBit(GetBit());
        PutBit(GetBit());
        PutBit(GetBit());
        PutBit(GetBit());
    }
    FlushBitBuffer();
    return 0;
}

int decode_collapse()
{
    while(!end_of_data())
    {
        PutBit(0);
        PutBit(GetBit());
        PutBit(GetBit());
        PutBit(GetBit());
        PutBit(GetBit());
        PutBit(GetBit());
        PutBit(GetBit());
        PutBit(GetBit());
    }
    return 0;
}

⌨️ 快捷键说明

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