前言
最近基于huffman编码和最小堆排序算法实现了一个压缩、解压缩的小程序。其源代码已经上传到github上面: Jcompress源代码 ,在本人的github上面有一个叫Utility的repository,该分类下面有一个名为Jcompress的目录便是本文所述的压缩、解压缩小程序的源代码。后续会在Utility下面增加其他一些实用的小程序,比如基于socket的文件断点下载小程序等等。如果你读了此文觉得还不错,不防给笔者的github点个star,
哈哈。在正式介绍Jcompress设计之前,笔者先展示一下Jcompress压缩、解压缩文件的一些截图。下图一是笔者用来做实验的一些文件,包括pdf电子书,可执行文件,jpg图片,以及电影美人鱼等,图中后缀名为.jcprs的便是Jcompress压缩后的文件,而文件名中带数字‘1’的是.jcprs文件经过Jcompress解压缩而得到的文件。剩下的几幅图分别是解压缩后的pdf, exe, jpg, rmvb格式文件正常打开、运行以及播放的截图。为了方便排版,笔者在上传图片的时候将图片长度、宽度重新设置了一下,导致看起来有点模糊,大家凑合着看吧,哈哈。。。
基本思路
相信大家在本科学习数据结构课程(严蔚敏版)的时候都接触过huffman编码吧,课本中的例子给了若干个字符,以及每个字符的出现次数,并以此构造了一颗huffman树。然后从huffman树的根节点到每一个叶子节点走一条路径,如果约定往左编码0,往右编码1,那么每一个叶子便可以得到一个由一串0和1表示的串。现在我们回到实际问题上来,我们要压缩的文件格式多种多样,可能是图片,也可能是各种格式的电影,还有可能是可执行文件。那么问题就来了,课本中的例子是对‘字符’来构造huffman树的,那么面对实际问题我们是对‘谁’来构造huffman树呢?我们来看一看下图,答案就自然浮出水面了。
上图是用winhex软件打开rmvb格式的电影美人鱼后的部分截图,从图中可以看出它本质上其实就是一系列的二进制01串(图中是以16进制表示的)。例如图中第一个‘数字’ 2E其实就是二进制的0010 1110,也就是8个二进制的bit,所以我们何不以每8个bit为一个单位把它看做课本中的例子中的‘字符’呢?根据排列组合,8个bit最多有256中情况:从0x00到0xFF。当然这256种编码所代表的‘字符’只有部分是可以以ASCII码的形式打印出来的。那么我们在代码里把要处理的文件以‘二进制’格式读入内存,把内存里的bits们看做一个个的char,然后统计256种char每一个出现的频率。这样我们就可以根据每一个‘字符’出现的频率来构造一颗huffman树了,进而得到每一个‘字符’的编码,并把编码写入到另外一个文件里面,便得到了压缩后的文件。然后我们按照同样的规则对压缩后的文件按照huffman树来解码,把解码出来的‘字符’在写入到新的文件里面,于是就得到了新的解压缩后的文件了。
上一段文字里面其实漏掉了一个细节:char其实是有符号的,为了方便我们把每8个bit组成的char 定义为unsigned char。这样的好处是255就是0xff,否则的话-1才对应着0xff,因为在有符号的情况下左边第一个bit是代表符号位的。为了避免负char给我们实现带来不必要的麻烦,最好是以unsigned
char的类型来看待每8个bit的组合。其实char只是代表了一个数据的长度为8bit而已,我们在printf的时候以%c来解释的时候它才是字符,完全可以把它看做数据并以%d等格式打印出数字来。当然char类型的名字容易给人以误解,认为它理所当然的代表着字符类型。
在构造huffman树的过程当中,我们每次都需要从现有的结点中选取出现频率(或者权重)最小的两个unsigned char来构造huffman树,因此还需要一个排序算法。堆排序是一种不错的算法,我们可以构造出一个最小堆,堆顶的元素就是最小的元素。这个最小堆应该提供如下功能:第一,提取堆的最小值,这个简单,直接从堆顶取值就行了。当然每一次提取最小值后,此时的堆已经不是最小堆了,要紧跟着调整堆,使之重新成为最小堆;第二,该最小堆应该支持插入操作,因为在构造huffman树的过程中,每一次取出来的两个最小权重的结点会构造一个新的huffman树,而此树的跟会重新作为下一次构造的候选结点,因而该候选结点需要重新插入堆中,并且通过调整使之再次符合最小堆的特征。
综上所述,本文将要实现的压缩、解压缩小程序Jcompress的核心组件是:最小堆排序算法、huffman编码算法。本文将会围绕这两个主题来分析Jcompress的具体实现过程。
数据结构
还记得有句名言吗:算法+数据结构=程序,没错,我们写的任何代码基本上都是要操作数据的。有些程序比较小,可能只需要使用基本的数据类型而不需要定义较为复杂数据结构。但是当一个程序的逻辑相对复杂的时候,为了代码逻辑的清晰,我们往往就需要定义一些数据结构,然后实现一系列的算法来操作这些数据结构,在加上业务逻辑代码,于是一个程序的形态便初步具备了。因此我们可以稍微扩充上面这句话:
算法+数据结构+业务逻辑代码=系统。因此我们每个人在面临设计较为复杂的系统的时候,都应该按照算法、数据结构、业务逻辑代码的三个角度来考虑设计方案。
- typedef struct HuffmanNode{
- unsigned char data_8bit;
- unsigned long long data_8bit_count;
- struct HuffmanNode *parent;
- struct HuffmanNode *left_child;
- struct HuffmanNode *right_child;
- }HuffmanNode;
typedef struct HuffmanNode{
unsigned char data_8bit;
unsigned long long data_8bit_count;
struct HuffmanNode *parent;
struct HuffmanNode *left_child;
struct HuffmanNode *right_child;
}HuffmanNode;数据结构HuffmanNode包括5个字段。data_8bit就是存储前文中我们所说的unsigned char的;data_8bit_count存储data_8bit出现的次数;指针parent表示该结点的父结点;left_child表示该结点的左孩子;right_child表示该结点的右孩子。其中data_8bit_count需要遍历一次文件内容得到,而parent、left_child、right_child在构造huffman树的过程中会被设置。
typedef struct HuffmanEncode{
char *encode;
long length_of_encode;
}HuffmanEncode;
数据结构HuffmanEncode代表每一个unsigned char的编码结果,其中字段encode会指向一串字符,该字符由字符‘0’和字符‘1’组成,从huffman树的根节点走一条到叶子结点的路便可以得到该编码,而length_of_encode代表着encode的长度。此刻细心的读者可能会问两个问题,首先HuffmanEncode结构体好像看不出来该编码对应哪个字符;其次*encode为什么不以\0结束的字符串形式存在,而是多了一个表示长度的字段。
首先对于第一个问题,笔者在使用该数据结构的时候是以HuffmanEncode huffman_encode[256]的方式来使用的,因为unsigned char一共有256种情况,用数组的方式比较方便,下标便直接代表了对应的unsigned char。例如huffman_encode[255]的下标是255,对应着0xff这个unsigned char。对于第二个问题,假设unsigend char 0x255的编码为字符串"00101",我们在压缩的时候需要把"00101"以二进制'00101'的形式写入内存缓冲区,继而写入磁盘文件。我们不能直接把"00101"以\0为结束标志的字符串的形式写入内存缓冲区中,那样的话就达不到压缩的目的了,变成一个字符用5个字符来表示,显然不行,我们应该挨个遍历字符如果是字符‘0’则就往内存对应的bit写入0,‘1’则对应的bit写入1,这样才能最大程度节省空间。因此我们用增加一个长度字段的方式来代替\0为结束标志的字符串,在代码实现上更为方便一点。
当然读者或许会问HuffmanNode数据结构中为什么用专门的字段data_8bit而不是数组索引来代表对应的unsigned char呢?这是因为HuffmanEncode是一个数组,而HuffmanNode最终是以一颗树的形式存在的,在构造huffman树的过程中,特定索引位置对应的结点会发生变化。
- typedef struct EncodeBuffer{
- unsigned char *buffer;
- long size;
- long bits_num_lastbytes;
- }EncodeBuffer;
typedef struct EncodeBuffer{
unsigned char *buffer;
long size;
long bits_num_lastbytes;
}EncodeBuffer;想象一下,在压缩的时候,我们把最初的文件经过编码之后先是存储在内存中,然后在用诸如fwrite的函数把该内存写入磁盘文件中。而在解压缩的时候我们需要把压缩文件的内容读入到内存中,在根据huffman树来恢复原本的数据。这里提到的内存便可以一个数据结构EncodeBuffer来表示,其中的字段buffer将来是通过malloc函数分配的一块内存,字段size表示该buffer存储的有效字节数量,bits_num_lastbytes表示最后一个字节中包含的有效bit数量。这是因为一个文件经过编码后包含的bit数量不一定是8的整数倍,也就是说除以8后,余数可能是1--7中的任意一个,我们需要记住这个数字,最后一个字节的剩下的bit是不需要进行处理的。当然在压缩、解压缩的过程都会使用到这个数据结构,其使用方式的微妙之处在下文的源代码里面可以清晰的看出。
typedef struct DecodeBuffer{
unsigned char *buffer;
long size;
}DecodeBuffer;同样,我们把原始文件读入EncodeBuffer类型的内存缓冲区,然后根据恢复出来的huffman树来找到编码对应的‘原码’,这个‘原码’当然不能直接写入磁盘中的压缩文件中,这样写磁盘次数太多,性能会下降。而应该先把解码出来的数据写入内存缓冲区中,积累多了再一次写入磁盘中。DecodeBuffer便是用来表示该缓冲区的,其中的buffer字段也是通过malloc得到的内存,而size表示buffer中存储的有效字节数。与EncodeBuffer不同,这里没有bits_num_lastbytes这个字段,因为无论压缩文件的bit数是不是8的倍数,压缩前的文件bit数量肯定是8的倍数,因为存储于磁盘上的文件的最小的单位就是字节了。因此解压出来的数据无需担心最后一个bit的有效数量的问题。
- typedef struct CompressFileHeader{
- long length_of_header1;
- long length_of_header2;
- unsigned char length_of_author_name;
- unsigned char routine_name_length;
- unsigned char suffix_length;
- unsigned char file_name_length;
- unsigned char bits_of_lastByte;
- char *author_name;
- char *routine_name;
- char *suffix;
- char *file_name;
- }CompressFileHeader;
typedef struct CompressFileHeader{
long length_of_header1;
long length_of_header2;
unsigned char length_of_author_name;
unsigned char routine_name_length;
unsigned char suffix_length;
unsigned char file_name_length;
unsigned char bits_of_lastByte;
char *author_name;
char *routine_name;
char *suffix;
char *file_name;
}CompressFileHeader;
在解压缩文件的时候,我们所知道的也就是一个压缩文件而已,我们根据什么来解压呢?在压缩的时候,我们构造了一颗huffman树,如果我们能够在解压的时候恢复这颗huffman树,显然我们就可以顺利成章的把压缩文件恢复为原始文件,这个过程也就是所谓的‘解压缩’。因此我们在压缩的时候可以在压缩文件的开始处写入一些元信息,使得我们可以依赖此信息构造出huffman树就可以了。当然还可以在头部加入其它一些信息。在Jcompress中,压缩文件由3部分构成:
- 压缩文件头:用CompressFileHeader类型的格式来存储压缩文件的元信息,以便解压的时候根据这些信息来恢复原文件。其中的11个字段的含义如下:
- length_of_header1:表示CompressFileHeader本身的长度,这样解压过程中,根据文件中的第一个long型的数据便可以读取完整的CompressFileHeader数据,该数据结构中有4个字符指针,在写入内存缓冲区或者磁盘的时候实际写入的是字符串本身,而不是指针。length_of_header1表示的长度也是计入author_name等实际字符个数,后文代码中会有比较清晰的认识。
- length_of_header2:表示截止压缩文件正文前的文件长度。length_of_header2-length_of_header1便可以得到编码表(含义见下文)的长度
- length_of_author_name:Jcompress作者的姓名的字节数长度
- routine_name_length:压缩文件的名称长度,这里当然是Jcompress了
- suffix_length:压缩文件的后缀长度,在Jcompress中, 后缀名为jcprs
- file_name_length:原始文件名的长度(字节数),压缩后的文件名可能会被用户改名,比如a.rmvb.jcprs被改为bc.rmvb.jcprs,但是文件头里存储有原始文件名a.rmvb。如果大家用好压做个实验,把压缩文件改名,然后在解压,会发现解压后的名字还是原始文件名,可以推断,好压在压缩的时候已经把文件名写入压缩文件了。
- author_name:软件作者名称,这里当然就是笔者了,如上文所述,这里虽然是个指针,但是在写入内存或磁盘的时候实际写入的是对应的字符串本身,下面的类似,不再赘述。
- routine_name:压缩软件名称,此处为Jcompress
- suffix:后缀,jcprs
- file_name:原始文件名
- 编码表:按顺序存储256个unsigned char的编码
- 压缩文件正文部分:原始文件压缩后的正文部分
上图便是Jcompress的一个压缩文件用winhex打开后的部分截图。右边红色涂掉的部分便是笔者的姓名,紧接着是软件名Jcompress,后缀jcprs,原始文件名APUE.pdf。注意16进制最开始的两个long 型数据2B 00 00 00和59 0A 00 00分别代表着length_of_header1和length_of_header2. 但是他们都是以小端的方式存储的,其实际的值分别是00 00 00 2B和00 00 0A 59.有兴趣的读者可以下去了解下处理器的大端、小端存储方式
接口设计
有了基本思路和数据结构之后,我们便可以定义接口了,本文接口分为四部分:堆排序部分、huffman编码部分、huffman解码部分和业务逻辑部分。下面我们将围绕这四个部分简要介绍这些接口的作用,其源代码将在下文“Jcompress源代码实现”部分呈现。
1. 堆排序部分
- int heap_min_adjust(HuffmanNode **huffman_node_array, long data_start, long data_end);
- int heap_min_construct(HuffmanNode **huffman_node_array, long array_size);
- int heap_min_get2min(HuffmanNode **huffman_node_array, HuffmanNode **min_first, HuffmanNode **min_second, long *heap_size);
int heap_min_adjust(HuffmanNode **huffman_node_array, long data_start, long data_end);
int heap_min_construct(HuffmanNode **huffman_node_array, long array_size);
int heap_min_get2min(HuffmanNode **huffman_node_array, HuffmanNode **min_first, HuffmanNode **min_second, long *heap_size);
熟悉堆排序的同学都知道,堆排序其实包括两个过程:一个是堆的调整,另一个是堆的构造,后者是以前者为基础的。heap_min_adjust()便是对堆的调整,使之重新符合最小堆的定义。heap_min_construct()基于heap_min_adjust()而来,通过不断调整初始堆来构造一个最小堆。在构造huffman树的过程中,需要不断地从堆中取出最小的两个堆元素,因此定义接口heap_min_get2min来实现这个功能。
我们再来看下这些接口函数的参数部分。每一个接口函数的第一个参数huffman_node_array是一个HuffmanNode **类型的数据,实际上huffman_node_array是一个数组,而数组元素又是一个指针,该指针指向具体的HuffmanNode类型的结点数据。数组元素被设计为指针,是为了方便堆排序,排序的时候可以直接交换指针。data_start和data_end标志了huffman_node_array的起始和结束的位置,这样比较灵活,我们可以选择性的对其中某一段数据进行堆排序。array_size表示了用于构造最小堆的数组的长度。min_first和min_second是两个指针,其指向的内容又是一个指针,该指针指向得到的两个最小的堆元素。heap_size存储的是当前最小堆的长度。
注意参数min_first和min_second都是指向指针的指针,为什么这么做呢?首先这个接口函数为了得到最小的两个堆元素的指针,那么在传递参数的时候如果传递的是HuffmanNode *min_first,那么即使函数内部把min_first设置成了正确的值,调用结束后,在主调函数里min_first依然保持了调用之前的状态,而没有发生丝毫改变,这是因为c函数是传值调用,参数均以值的形式进行传递,指针也不例外。这里有一个误区,很多人可能见过这样的情形:在调用函数里定义了一个变量:int
a; 然后以func(&a)的形式来调用int func( int *a),func内部做了动作*a=8,然后主调函数里面就会得到a的值为8.为什么这里不行呢,在这个例子中,传递进来的是a的地址&a, &a的值始终没有变,只是改变了它指向的内存的内容;而对于heap_min_get2min()来说,如果传递的是HuffmanNode *类型的数据min_first,则min_first的本身的值永远不会变,只会改变它所指向的内存的内容,而我们的代码逻辑是要改名指针本身,为了达到这个目的,我们只能传递指针的指针,也即HuffmanNode
**类型的min_first,在函数内部我们做类似这样的操作*min_first=xxxx,就可以做到改变min_first指向的内容,也即改变HuffmanNode *类型的数据,这正是我们要的结果。
2. huffman编码部分
- int huffman_tree(HuffmanNode **huffman_tree_root, HuffmanNode **huffman_node_array, long array_size);
- int huffman_encode(HuffmanNode **huffman_node_array, long huffman_node_array_size,HuffmanEncode *huffman_encode_array);
int huffman_tree(HuffmanNode **huffman_tree_root, HuffmanNode **huffman_node_array, long array_size);
int huffman_encode(HuffmanNode **huffman_node_array, long huffman_node_array_size,HuffmanEncode *huffman_encode_array);
由前文可知,huffman编码是根据huffman树而来的,走一条从根结点到叶子结点的路径就能得到叶子结点所代表的数据对应的编码。因此上述函数huffman_tree()的作用便是构造一颗huffman树,构造完毕的huffman树的根节点的指针会写入第一个参数指向的内存,第二个参数是调用者传递的huffman结点数组,第三个参数array_size表示huffman结点数组的元素个数。
函数huffman_encode的作用是:根据huffman_tree()构造出的huffman树,对于每一个叶子结点,走一条去往跟结点的路径,这样便得到了该叶子结点对应的huffman编码的逆序列,有了逆序列,自然就可以得到正序列了。第一个参数是一个存储叶子结点的数组,第二个参数是数组元素个数,第三个参数会返回最终每一个叶子结点对应的huffman编码,以便后文使用。
3. huffman解码部分
- int huffman_decode_internal(HuffmanNode **huffman_tree_current_root, long direction);
- int huffman_decode(HuffmanNode *huffman_tree_root, HuffmanNode **last_stop_at, EncodeBuffer *encode_buffer, DecodeBuffer *decode_buffer);
int huffman_decode_internal(HuffmanNode **huffman_tree_current_root, long direction);
int huffman_decode(HuffmanNode *huffman_tree_root, HuffmanNode **last_stop_at, EncodeBuffer *encode_buffer, DecodeBuffer *decode_buffer);
huffman解码部分对应着业务逻辑中的‘解压缩’这个概念,为了使得代码逻辑清晰,解码功能被拆分为两个函数。huffman_decode()会在内部调用huffman_decode_internal() 。其具体功能将在后文的源代码部分有比较详细的解析,这里大概描述下解码的功能:首先解码需要根据huffman树来解码,因此会有其他函数来负责根据压缩文件头里的编码表来恢复哈夫曼树。其次需要读取压缩文件的正文到内存缓冲区里面,这正是我们要解码的对象。最后解码后的结果要存储在两外一块内存缓冲区里面,等待最后写入磁盘。
4. 业务逻辑部分
- int bit_set(unsigned char *ch, long index, long flag);
- int open_file(char *filename, FILE **fp);
- int creat_file(char *filename, FILE **fp);
- int make_uchar_weight(FILE *fp, HuffmanNode **huffman_node, long *size);
- int create_Uncompress_file(char *filename, FILE **fp);
int bit_set(unsigned char *ch, long index, long flag);
int open_file(char *filename, FILE **fp);
int creat_file(char *filename, FILE **fp);
int make_uchar_weight(FILE *fp, HuffmanNode **huffman_node, long *size);
int create_Uncompress_file(char *filename, FILE **fp);
上述有5个函数,除了bit_set()外,其余四个都是打开、创建文件之类的,比较简单不在赘述。bit_set()的功能是把特定字节*ch的第index个bit设置为0或者1,具体根据flag的指示。
- int Jcompress(char *src_name);
- int JparseHead(FILE **fp, HuffmanEncode *huffman_encode_array, char **file_name, long *last_bits);
- int JUnCompress(char *src_name);
- int restore_huffman_tree(HuffmanNode **huffman_tree_root, HuffmanEncode *huffman_encode_array);
int Jcompress(char *src_name);
int JparseHead(FILE **fp, HuffmanEncode *huffman_encode_array, char **file_name, long *last_bits);
int JUnCompress(char *src_name);
int restore_huffman_tree(HuffmanNode **huffman_tree_root, HuffmanEncode *huffman_encode_array);
上述四个函数是比较重要的业务逻辑函数,他们在1,2,3 中描述的函数的基础上负责上层的压缩、解压缩逻辑。Jcompress()是用来对文件进行压缩的,而后三个函数是为了解压缩的,首先JparseHead()会读取压缩文件的头部,并且解析出必要的信息,比如编码表,然后restore_huffman_tree()会根据解析出的编码表来重建huffman树,这个重建的huffman树与压缩时创建的huffman树一模一样,否则的话解码出的结果就是不正确的。而JUnCompress()会在这两个函数的基础上做具体的解压缩操作。
Jcompress代码实现
1. 最小堆代码实现
最小堆排序算法基本上是按照<数据结构>严蔚敏版的算法来实现的,其具体功能这里不再赘述,仅列出代码,读者可以参考课本自行分析之。首先是heap_min_adjust,也就是调整堆,代码如下所示:
- int heap_min_adjust(HuffmanNode **huffman_node_array, long data_start, long data_end){
- /**
- ** check error for argument
- */
- if(huffman_node_array==NULL || data_start<0 || data_end<0 || data_end<data_start){
- printf("heap_min_adjust: argument error\n");
- exit(0);
- }
- if(data_end==data_start) return 1;
- /**
- ** the top of heap-min indicated by index data_start is the only element
- ** which need to be adjusted to make a min-heap
- */
- HuffmanNode * current_data_tobe_adjust=huffman_node_array[data_start];
- long current_indexof_data=data_start;
- for(long cur=2*data_start;cur<=data_end;cur=2*cur){
- if(cur<data_end){
- /** compare the left child and right child to find the min one */
- if(((huffman_node_array[cur])->data_8bit_count)>((huffman_node_array[cur+1])->data_8bit_count)){
- cur+=1;
- }
- }
- if((current_data_tobe_adjust->data_8bit_count)<=((huffman_node_array[cur])->data_8bit_count))
- break;
- huffman_node_array[current_indexof_data]=huffman_node_array[cur];
- current_indexof_data=cur;
- }
- huffman_node_array[current_indexof_data]=current_data_tobe_adjust;
- /**
- ** return 1 means everything is ok
- */
- return 1;
- }
int heap_min_adjust(HuffmanNode **huffman_node_array, long data_start, long data_end){
/**
** check error for argument
*/
if(huffman_node_array==NULL || data_start<0 || data_end<0 || data_end<data_start){
printf("heap_min_adjust: argument error\n");
exit(0);
}
if(data_end==data_start) return 1;
/**
** the top of heap-min indicated by index data_start is the only element
** which need to be adjusted to make a min-heap
*/
HuffmanNode * current_data_tobe_adjust=huffman_node_array[data_start];
long current_indexof_data=data_start;
for(long cur=2*data_start;cur<=data_end;cur=2*cur){
if(cur<data_end){
/** compare the left child and right child to find the min one */
if(((huffman_node_array[cur])->data_8bit_count)>((huffman_node_array[cur+1])->data_8bit_count)){
cur+=1;
}
}
if((current_data_tobe_adjust->data_8bit_count)<=((huffman_node_array[cur])->data_8bit_count))
break;
huffman_node_array[current_indexof_data]=huffman_node_array[cur];
current_indexof_data=cur;
}
huffman_node_array[current_indexof_data]=current_data_tobe_adjust;
/**
** return 1 means everything is ok
*/
return 1;
}
接下来是heap_min_construct()的代码,此函数是通过不断的调用上面的调整堆的函数来达到堆排序的目的。
- int heap_min_construct(HuffmanNode **huffman_node_array, long array_size){
- /** valid data start from index 1 not 0 */
- /**
- ** check error for argument
- */
- if(huffman_node_array==NULL || array_size<=0 || array_size>256){
- printf("heap_min_construct: argument error\n");
- exit(0);
- }
- for(long HeapRoot=array_size/2; HeapRoot>=1;HeapRoot--){
- heap_min_adjust(huffman_node_array,HeapRoot,array_size);
- }
- return 1;
- }
int heap_min_construct(HuffmanNode **huffman_node_array, long array_size){
/** valid data start from index 1 not 0 */
/**
** check error for argument
*/
if(huffman_node_array==NULL || array_size<=0 || array_size>256){
printf("heap_min_construct: argument error\n");
exit(0);
}
for(long HeapRoot=array_size/2; HeapRoot>=1;HeapRoot--){
heap_min_adjust(huffman_node_array,HeapRoot,array_size);
}
return 1;
}
最后是heap_min_get2min()函数。
- int heap_min_get2min(HuffmanNode **huffman_node_array, HuffmanNode **min_first, HuffmanNode **min_second, long *heap_size){
- /**
- ** check argument
- **/
- if(huffman_node_array==NULL){
- printf("heap_min_get2min: argument error\n");
- exit(0);
- }
- *min_first=huffman_node_array[1];
- huffman_node_array[1]=huffman_node_array[*heap_size];
- (*heap_size)-=1;
- /** after we get the min data, we should again adjust the heap to make a min-heap */
- heap_min_adjust(huffman_node_array,1,*heap_size);
- *min_second=huffman_node_array[1];
- huffman_node_array[1]=huffman_node_array[*heap_size];
- (*heap_size)-=1;
- /** the same as above*/
- if(*heap_size>0){
- heap_min_adjust(huffman_node_array,1,*heap_size);
- }
- return 1;
- }
int heap_min_get2min(HuffmanNode **huffman_node_array, HuffmanNode **min_first, HuffmanNode **min_second, long *heap_size){
/**
** check argument
**/
if(huffman_node_array==NULL){
printf("heap_min_get2min: argument error\n");
exit(0);
}
*min_first=huffman_node_array[1];
huffman_node_array[1]=huffman_node_array[*heap_size];
(*heap_size)-=1;
/** after we get the min data, we should again adjust the heap to make a min-heap */
heap_min_adjust(huffman_node_array,1,*heap_size);
*min_second=huffman_node_array[1];
huffman_node_array[1]=huffman_node_array[*heap_size];
(*heap_size)-=1;
/** the same as above*/
if(*heap_size>0){
heap_min_adjust(huffman_node_array,1,*heap_size);
}
return 1;
}
2. 哈夫曼编码实现
哈夫曼编码部分的代码也是基本和<数据结构>课本上的一致,这里也仅给出代码,读者如有不明白的地方可参考课本中的描述。
- int huffman_tree(HuffmanNode **huffman_tree_root, HuffmanNode **huffman_node_array, long array_size){
- /**
- ** check error for argument
- */
- if(huffman_node_array==NULL || array_size<1 || array_size>256){
- printf("huffman_tree:argument error\n ");
- exit(0);
- }
- if(array_size==1){
- printf("huffman_tree: \nnode size: %ld too small to construct huffman tree\ntry a bigger file\n",array_size);
- exit(0);
- }
- long heap_size=array_size;
- HuffmanNode *huffman_parent_node=(HuffmanNode *)malloc(sizeof(HuffmanNode)*(array_size-1+1));
- for(long i=1;i<=array_size-1;i++){
- (huffman_parent_node+i)->data_8bit=0x00;
- (huffman_parent_node+i)->data_8bit_count=0;
- (huffman_parent_node+i)->left_child=NULL;
- (huffman_parent_node+i)->right_child=NULL;
- (huffman_parent_node+i)->parent=NULL;
- }
- heap_min_construct(huffman_node_array,array_size);
- HuffmanNode *min1;
- HuffmanNode *min2;
- for(long i=1;i<=array_size-1;i++){
- heap_min_get2min(huffman_node_array,&min1,&min2,&heap_size);
- huffman_parent_node[i].data_8bit=0x00;
- huffman_parent_node[i].data_8bit_count=min1->data_8bit_count+min2->data_8bit_count;
- huffman_parent_node[i].left_child=min1;
- huffman_parent_node[i].right_child=min2;
- huffman_parent_node[i].parent=NULL;
- min1->parent=&(huffman_parent_node[i]);
- min2->parent=&(huffman_parent_node[i]);
- /** insert parent node and again make min heap*/
- heap_size+=1;
- huffman_node_array[heap_size]=&(huffman_parent_node[i]);
- heap_min_construct(huffman_node_array,heap_size);
- }
- if(huffman_parent_node[array_size-1].parent==NULL){
- *huffman_tree_root=&(huffman_parent_node[array_size-1]);
- }
- else{
- printf("huffman tree construct failed\n");
- exit(0);
- }
- return 1;
- }
int huffman_tree(HuffmanNode **huffman_tree_root, HuffmanNode **huffman_node_array, long array_size){
/**
** check error for argument
*/
if(huffman_node_array==NULL || array_size<1 || array_size>256){
printf("huffman_tree:argument error\n ");
exit(0);
}
if(array_size==1){
printf("huffman_tree: \nnode size: %ld too small to construct huffman tree\ntry a bigger file\n",array_size);
exit(0);
}
long heap_size=array_size;
HuffmanNode *huffman_parent_node=(HuffmanNode *)malloc(sizeof(HuffmanNode)*(array_size-1+1));
for(long i=1;i<=array_size-1;i++){
(huffman_parent_node+i)->data_8bit=0x00;
(huffman_parent_node+i)->data_8bit_count=0;
(huffman_parent_node+i)->left_child=NULL;
(huffman_parent_node+i)->right_child=NULL;
(huffman_parent_node+i)->parent=NULL;
}
heap_min_construct(huffman_node_array,array_size);
HuffmanNode *min1;
HuffmanNode *min2;
for(long i=1;i<=array_size-1;i++){
heap_min_get2min(huffman_node_array,&min1,&min2,&heap_size);
huffman_parent_node[i].data_8bit=0x00;
huffman_parent_node[i].data_8bit_count=min1->data_8bit_count+min2->data_8bit_count;
huffman_parent_node[i].left_child=min1;
huffman_parent_node[i].right_child=min2;
huffman_parent_node[i].parent=NULL;
min1->parent=&(huffman_parent_node[i]);
min2->parent=&(huffman_parent_node[i]);
/** insert parent node and again make min heap*/
heap_size+=1;
huffman_node_array[heap_size]=&(huffman_parent_node[i]);
heap_min_construct(huffman_node_array,heap_size);
}
if(huffman_parent_node[array_size-1].parent==NULL){
*huffman_tree_root=&(huffman_parent_node[array_size-1]);
}
else{
printf("huffman tree construct failed\n");
exit(0);
}
return 1;
}
- int huffman_encode(HuffmanNode **huffman_node_array, long huffman_node_array_size, HuffmanEncode *huffman_encode_array){
- /**
- ** check error for argument
- */
- if(huffman_node_array==NULL || huffman_encode_array==NULL || huffman_node_array_size<1 ||
- huffman_node_array_size>256){
- printf("huffman_encode: argument error\n");
- exit(0);
- }
- if(huffman_node_array_size==1){
- printf("huffman_encode: node array size too small to construct huffman encode\n");
- exit(0);
- }
- HuffmanNode **huffman_node_array_backup=(HuffmanNode **)malloc((huffman_node_array_size+1)*
- sizeof(HuffmanNode *));
- if(NULL==huffman_node_array_backup){
- printf("malloc failed\n");
- exit(0);
- }
- /**
- ** later we will use this backup to make huffman encode
- ** note: backup should before invoking huffman_tree(),because this function will
- ** change array huffman_node_array
- */
- for(long i=1;i<=huffman_node_array_size;i++){
- huffman_node_array_backup[i]=huffman_node_array[i];
- }
- HuffmanNode *huffman_tree_root=NULL;
- huffman_tree(&huffman_tree_root, huffman_node_array, huffman_node_array_size);
- /**
- ** construct huffmanencode, traverse huffman tree, left encode 0, right encode 1
- */
- char encode_temp[260];
- long start_temp=259;
- for(long i=1;i<=huffman_node_array_size;i++){
- start_temp=259;
- HuffmanNode *leaf=huffman_node_array_backup[i];
- //printf("node index and count: %d , ", i);
- //printf("%d ",leaf->data_8bit_count);
- while(leaf->parent!=NULL){
- HuffmanNode *parent=leaf->parent;
- if(parent->left_child==leaf){
- encode_temp[start_temp--]='0';//printf("0");
- }else if(parent->right_child==leaf){
- encode_temp[start_temp--]='1';//printf("1");
- }else{
- printf("the huffman tree we reference seems not correct\n");
- exit(0);
- }
- leaf=leaf->parent;
- }
- /** I have make a low level error to use dat_8bit_count which cause error*/
- long huffman_encode_array_index=huffman_node_array_backup[i]->data_8bit;
- huffman_encode_array[huffman_encode_array_index].encode=(char *)malloc((259-start_temp)*sizeof( char));
- if(huffman_encode_array[huffman_encode_array_index].encode==NULL){
- printf("huffman_encode: malloc failed\n");
- exit(0);
- }
- huffman_encode_array[huffman_encode_array_index].length_of_encode=259-start_temp;
- memcpy(huffman_encode_array[huffman_encode_array_index].encode, encode_temp+start_temp+1, 259-start_temp);
- }
- if(huffman_node_array_backup!=NULL){
- free(huffman_node_array_backup);
- huffman_node_array_backup=NULL;
- }
- return 1;
- }
int huffman_encode(HuffmanNode **huffman_node_array, long huffman_node_array_size, HuffmanEncode *huffman_encode_array){
/**
** check error for argument
*/
if(huffman_node_array==NULL || huffman_encode_array==NULL || huffman_node_array_size<1 ||
huffman_node_array_size>256){
printf("huffman_encode: argument error\n");
exit(0);
}
if(huffman_node_array_size==1){
printf("huffman_encode: node array size too small to construct huffman encode\n");
exit(0);
}
HuffmanNode **huffman_node_array_backup=(HuffmanNode **)malloc((huffman_node_array_size+1)*
sizeof(HuffmanNode *));
if(NULL==huffman_node_array_backup){
printf("malloc failed\n");
exit(0);
}
/**
** later we will use this backup to make huffman encode
** note: backup should before invoking huffman_tree(),because this function will
** change array huffman_node_array
*/
for(long i=1;i<=huffman_node_array_size;i++){
huffman_node_array_backup[i]=huffman_node_array[i];
}
HuffmanNode *huffman_tree_root=NULL;
huffman_tree(&huffman_tree_root, huffman_node_array, huffman_node_array_size);
/**
** construct huffmanencode, traverse huffman tree, left encode 0, right encode 1
*/
char encode_temp[260];
long start_temp=259;
for(long i=1;i<=huffman_node_array_size;i++){
start_temp=259;
HuffmanNode *leaf=huffman_node_array_backup[i];
//printf("node index and count: %d , ", i);
//printf("%d ",leaf->data_8bit_count);
while(leaf->parent!=NULL){
HuffmanNode *parent=leaf->parent;
if(parent->left_child==leaf){
encode_temp[start_temp--]='0';//printf("0");
}else if(parent->right_child==leaf){
encode_temp[start_temp--]='1';//printf("1");
}else{
printf("the huffman tree we reference seems not correct\n");
exit(0);
}
leaf=leaf->parent;
}
/** I have make a low level error to use dat_8bit_count which cause error*/
long huffman_encode_array_index=huffman_node_array_backup[i]->data_8bit;
huffman_encode_array[huffman_encode_array_index].encode=(char *)malloc((259-start_temp)*sizeof(char));
if(huffman_encode_array[huffman_encode_array_index].encode==NULL){
printf("huffman_encode: malloc failed\n");
exit(0);
}
huffman_encode_array[huffman_encode_array_index].length_of_encode=259-start_temp;
memcpy(huffman_encode_array[huffman_encode_array_index].encode, encode_temp+start_temp+1, 259-start_temp);
}
if(huffman_node_array_backup!=NULL){
free(huffman_node_array_backup);
huffman_node_array_backup=NULL;
}
return 1;
}
3. 哈夫曼解码实现
huffman解码功能由两个函数承担,我们先看其中的一个函数huffman_decode_internal()。我们知道,解码的大概流程是:先把磁盘里的压缩文件读入内存中,然后遍历内存中的bit,根据bit的值从哈夫曼树跟结点一直往下走,直至叶子结点,我们便解析出了一个unsigned char类型的数据。当然从根结点到叶子结点是一步一步走下来的,而这其中的“每走一步”的功能便是由下面这个函数来实现的。此函数的第一个参数是一个指针,指针的内容又是一个指向HuffmanNode的指针,该指针也即*huffman_tree_current_root表示当前已经走到了某个特定的结点上,direction表示方向,其值是根据压缩文件里面的bit值而得到的,bit是0则direction便是0,否则是1.direction代码了从当前位置*huffman_tree_current_root处应该往哪个方向走。当然在本文中0意味着往左走,1意味着往右走。
当然每走一步,就会走到一个新的位置处,该位置可能是非叶子结点,也可能是叶子结点。如果走了一步后发现还是非叶子结点,则函数会返回到调用的函数里面,而调用者会继续读内存中的bit,并根据其值来决定下一步的走法。如果是叶子结点,则返回1给调用者,这时候调用者根据返回值1判断当前已经解析出了一个unsigned char,于是便会把这个解析出来的unsigned char写入另外一个内存缓冲区里,解析出unsigned char后,调用者会继续读压缩文件内存中的bit并一直按照这个方法走下去。这里所说的调用者函数便是下面将要介绍的huffman_decode()函数。
- int huffman_decode_internal(HuffmanNode **huffman_tree_current_root, long direction){
- /**
- ** check error for argument
- */
- if(huffman_tree_current_root==NULL || *huffman_tree_current_root==NULL || !(direction==0 || direction==1)){
- printf("huffman_decode_internal: argument error\n");
- exit(0);
- }
- if(direction==0){
- (*huffman_tree_current_root)=(*huffman_tree_current_root)->left_child;
- if((*huffman_tree_current_root)->left_child==NULL &&
- (*huffman_tree_current_root)->right_child==NULL){
- return 1;
- }else{
- return 0;
- }
- }else if(direction==1){
- (*huffman_tree_current_root)=(*huffman_tree_current_root)->right_child;
- if((*huffman_tree_current_root)->left_child==NULL &&
- (*huffman_tree_current_root)->right_child==NULL){
- return 1;
- }else{
- return 0;
- }
- }else{
- printf("\ndirection error: %ld\n",direction);
- exit(0);
- }
- }
int huffman_decode_internal(HuffmanNode **huffman_tree_current_root, long direction){
/**
** check error for argument
*/
if(huffman_tree_current_root==NULL || *huffman_tree_current_root==NULL || !(direction==0 || direction==1)){
printf("huffman_decode_internal: argument error\n");
exit(0);
}
if(direction==0){
(*huffman_tree_current_root)=(*huffman_tree_current_root)->left_child;
if((*huffman_tree_current_root)->left_child==NULL &&
(*huffman_tree_current_root)->right_child==NULL){
return 1;
}else{
return 0;
}
}else if(direction==1){
(*huffman_tree_current_root)=(*huffman_tree_current_root)->right_child;
if((*huffman_tree_current_root)->left_child==NULL &&
(*huffman_tree_current_root)->right_child==NULL){
return 1;
}else{
return 0;
}
}else{
printf("\ndirection error: %ld\n",direction);
exit(0);
}
}
huffman_decode函数会被它的调用者多次反复调用,直至压缩文件被处理完毕。第一个参数huffman_tree_root是重建出来的哈夫曼树的根结点指针,重建的动作由huffman_decode()的调用者函数负责构造,然后直接以参数的形式传递给huffman_decode()。第二个参数last_stop_at实际上就是传递给huffman_decode_interanl()函数的第一个参数,也即负责记录当前走到哈夫曼树的哪一个结点上了。encode_buffer是一个内存缓冲区,huffman_decode()的调用者函数会把压缩文件分块读入encode_buffer所代表的内存缓冲区中,并作为参数传递给huffman_decode()函数来做相应的解码工作。解码的结果会写到第四个参数decode_buffer所代表的内存缓冲区中。
这里有一点需要读者特别注意的地方,前文我们说过,经过压缩的后的文件所包含的有效的bit数量不一定是8的整数倍,这也就意味着压缩文件中最后一块被读入内存缓冲区encode_buffer中的数据需要特别处理,具体来说是该块缓冲区的最后一个字节需要特殊处理。压缩文件头里会记录最后一个字节有多少个有效bit数量,按照这个数量来处理就行了。
- int huffman_decode(HuffmanNode *huffman_tree_root, HuffmanNode **last_stop_at, EncodeBuffer *encode_buffer, DecodeBuffer *decode_buffer){
- /**
- ** check error for argument
- */
- if(NULL==last_stop_at || encode_buffer==NULL || decode_buffer==NULL){
- printf("huffman_decode: argument error\n");
- exit(0);
- }
- long not_full_byte=0;
- long bits_num_ofLastByte=0;
- if(encode_buffer->bits_num_lastbytes>0){
- not_full_byte=1;
- bits_num_ofLastByte=encode_buffer->bits_num_lastbytes;
- }
- if(*last_stop_at==NULL){
- *last_stop_at=huffman_tree_root;
- }
- if(decode_buffer->size!=0){
- decode_buffer->size=0;
- }
- long buffer_size_real=0;
- buffer_size_real=encode_buffer->size;
- unsigned char * ptr_current_buffer=encode_buffer->buffer;
- unsigned char * ptr_endOf_real_buffer=encode_buffer->buffer+buffer_size_real-1;
- long return_value=-1;
- for(;ptr_current_buffer<=ptr_endOf_real_buffer;ptr_current_buffer++){
- unsigned char temp=*ptr_current_buffer;
- for(long i=0;i<8;i++){
- unsigned char bit_for_test=0x80;
- bit_for_test=bit_for_test>>i;
- long direction;
- if(bit_for_test&temp){ direction=1;}
- else{ direction=0;}
- return_value=huffman_decode_internal(last_stop_at,direction);
- if(return_value==1){
- decode_buffer->buffer[decode_buffer->size]=(*last_stop_at)->data_8bit;
- (decode_buffer->size)+=1;
- (*last_stop_at)=huffman_tree_root;
- }
- }
- }
- if(not_full_byte==1){
- unsigned char temp=(encode_buffer->buffer[encode_buffer->size]);
- for(long i=0;i<bits_num_ofLastByte;i++){
- unsigned char bit_for_test=0x80;
- bit_for_test=bit_for_test>>i;
- long direction;
- if(bit_for_test&temp){direction=1;}
- else{direction=0;}
- return_value=huffman_decode_internal(last_stop_at,direction);
- if(return_value==1){
- decode_buffer->buffer[decode_buffer->size]=(*last_stop_at)->data_8bit;
- (decode_buffer->size)+=1;
- (*last_stop_at)=huffman_tree_root;
- }
- }
- }
- return 1;
- }
int huffman_decode(HuffmanNode *huffman_tree_root, HuffmanNode **last_stop_at, EncodeBuffer *encode_buffer, DecodeBuffer *decode_buffer){
/**
** check error for argument
*/
if(NULL==last_stop_at || encode_buffer==NULL || decode_buffer==NULL){
printf("huffman_decode: argument error\n");
exit(0);
}
long not_full_byte=0;
long bits_num_ofLastByte=0;
if(encode_buffer->bits_num_lastbytes>0){
not_full_byte=1;
bits_num_ofLastByte=encode_buffer->bits_num_lastbytes;
}
if(*last_stop_at==NULL){
*last_stop_at=huffman_tree_root;
}
if(decode_buffer->size!=0){
decode_buffer->size=0;
}
long buffer_size_real=0;
buffer_size_real=encode_buffer->size;
unsigned char * ptr_current_buffer=encode_buffer->buffer;
unsigned char * ptr_endOf_real_buffer=encode_buffer->buffer+buffer_size_real-1;
long return_value=-1;
for(;ptr_current_buffer<=ptr_endOf_real_buffer;ptr_current_buffer++){
unsigned char temp=*ptr_current_buffer;
for(long i=0;i<8;i++){
unsigned char bit_for_test=0x80;
bit_for_test=bit_for_test>>i;
long direction;
if(bit_for_test&temp){ direction=1;}
else{ direction=0;}
return_value=huffman_decode_internal(last_stop_at,direction);
if(return_value==1){
decode_buffer->buffer[decode_buffer->size]=(*last_stop_at)->data_8bit;
(decode_buffer->size)+=1;
(*last_stop_at)=huffman_tree_root;
}
}
}
if(not_full_byte==1){
unsigned char temp=(encode_buffer->buffer[encode_buffer->size]);
for(long i=0;i<bits_num_ofLastByte;i++){
unsigned char bit_for_test=0x80;
bit_for_test=bit_for_test>>i;
long direction;
if(bit_for_test&temp){direction=1;}
else{direction=0;}
return_value=huffman_decode_internal(last_stop_at,direction);
if(return_value==1){
decode_buffer->buffer[decode_buffer->size]=(*last_stop_at)->data_8bit;
(decode_buffer->size)+=1;
(*last_stop_at)=huffman_tree_root;
}
}
}
return 1;
}
4. 业务逻辑实现
前文所述业务逻辑部分有几个关于文件操作、位操作等函数功能比较简单,在这里略去,我们只对比较重要的几个函数做一个简要的分析。下面的这个Jcompress函数便是用来实现压缩功能的业务逻辑的。它只有一个参数,便是打算压缩的那个文件的名称,当然它可以是任意格式的。该函数的执行步骤包括:
- 先调用make_uchar_weight()函数统计待压缩文件中每一个unsigned char出现的次数,然后调用huffman_encode()得到每一个unsigned char的哈夫曼编码
- 将一些重要的信息写入到压缩文件的头部,其中头部有一个字段表示最后一个字节的有效bit数量,该数量必须等待文件压缩完毕后才能得到,因此该数据会在文件压缩完毕后在写入到对应的位置处。
- 紧跟着文件头后面,压缩文件正文之前存储的是哈夫曼编码表,这个编码表示用来在解压缩的时候重建哈夫曼树用的。
- 接下来是一个while循环,在循环里面不断的把待压缩文件读入到内存缓冲区中,然后遍历内存缓冲区中的unsigned char,然后从第1步中找到该unsigned char对应的哈夫曼编码并写入到另一块内存缓冲区中。需要注意的是每一次fread读到的文件块处理完毕后,得到的压缩缓冲区所包含的bit数量同样不一定是8的倍数,但是写入磁盘的最小单位是字节。怎么办呢?在本文中是这样做的,对于每一次fread数据块处理完后得到的内存缓冲区,我们把bit数量是8的倍数的那一部分当成一系列的unsigned char字节流写入磁盘,然后剩下的那几个不足以构成一个字节的bit重新移动到缓冲区的开头处。下一轮fread的数据块的编码数据接着刚才所述的那几个bit后面存储。到最后while循环结束后,我们在判断当前处理的总的bit数量,如果不是8的整数倍,则将缓冲区开头处的一个字节写入磁盘,否则不写入磁盘。于此同时并且记录最后一个字节包含的有效bit数量,并写入到文件头的对应字段处。
这个函数里面定义了两个重要的变量:num_ofbits_write和num_ofbits_write_per_while,前者记录截止当前累计写入内存缓冲区中的bit数量,而后者记录的是每一次fread读入的数据块所对应的经压缩后的bit数量。前文我们说过,每一次fread的数据块处理后得到的bit数量不一定是8的倍数,然后我们把余数写入到缓冲区的开头。那么下一次fread的数据对应的经处理后的bit怎么避免覆盖上一次fread产生的不能整除8的那几个bit呢?我们只需要在while循环开头处使用(num_ofbits_write+1)除以8得到一个偏移量,然后在这个偏移量的后面接着写数据就行了。至此为止压缩过程已经分析完毕,其代码如下所示:
- int Jcompress(char *src_name){
- /**
- ** check argument
- **/
- if(src_name==NULL){
- printf("Jcompress: argument error\n");
- exit(0);
- }
- printf("******************Jcompress begin to execute************\n");
- printf("source file name : %s\n",src_name);
- char *dst_name=(char *)malloc(strlen(src_name)+6+1);
- memcpy(dst_name,src_name,strlen(src_name));
- char *cur0=dst_name+strlen(src_name);
- memcpy(cur0,".jcprs",6);
- *(cur0+6)='\0';
- printf("compress file name: %s\n",dst_name);
- FILE *src_fp=NULL;
- FILE *dst_fp=NULL;
- open_file(src_name,&src_fp);
- creat_file(dst_name,&dst_fp);
- HuffmanNode **huffman_node_array=(HuffmanNode **)malloc(257*sizeof(HuffmanNode *));
- if(huffman_node_array==NULL){
- printf("Jcompress: malloc failed\n");
- exit(0);
- }
- long huffman_node_num=0;
- make_uchar_weight(src_fp,huffman_node_array,&huffman_node_num);
- HuffmanEncode *encode_array=(HuffmanEncode *)malloc(257*sizeof(HuffmanEncode));
- if(encode_array==NULL){
- printf("Jcompress: malloc failed\n");
- exit(0);
- }
- for(long i=0;i<257;i++){
- encode_array[i].encode=NULL;
- encode_array[i].length_of_encode=0;
- }
- huffman_encode(huffman_node_array, huffman_node_num, encode_array);
- /**
- ** begin to deal with the header of the compress file, later the uncompress routine
- ** can read the header and restore the huffman tree then uncompress the file
- */
- CompressFileHeader CFH;
- CFH.length_of_author_name=8;
- CFH.author_name="zhangjun";
- CFH.routine_name_length=9;
- CFH.routine_name="Jcompress";
- CFH.suffix_length=5;
- CFH.suffix="jcprs";
- CFH.file_name_length=strlen(src_name);
- CFH.bits_of_lastByte=0x00;
- long length_of_header1=0;
- long length_of_header2=0;
- char *cur=src_name;
- while(cur[length_of_header1++]!='\0');
- length_of_header1+=2*sizeof(long)+9+10+6+1;
- length_of_header2=length_of_header1;
- for(long i=0;i<256;i++){
- if(encode_array[i].length_of_encode==0){
- length_of_header2=length_of_header2+1;
- }
- else if(encode_array[i].length_of_encode>0){
- length_of_header2=length_of_header2+1+
- encode_array[i].length_of_encode;
- }
- }
- unsigned char *buffer_for_header=(unsigned char*)malloc(sizeof(unsigned char)*length_of_header2);
- if(buffer_for_header==NULL){
- printf("Jcompress:malloc failed for buffer_for_header\n");
- exit(0);
- }
- unsigned char *ptr=buffer_for_header;
- *((long *)ptr)=length_of_header1;
- *((long *)(ptr+sizeof(long)))=length_of_header2;
- ptr+=2*sizeof(long);
- *(ptr++)=(unsigned char)CFH.length_of_author_name;
- *(ptr++)=(unsigned char)CFH.routine_name_length;
- *(ptr++)=(unsigned char)CFH.suffix_length;
- *(ptr++)=(unsigned char)CFH.file_name_length;
- *(ptr++)=(unsigned char)CFH.bits_of_lastByte;
- char *ch="zhangjunJcompressjcprs";
- memcpy(ptr,ch,22);
- ptr+=22;
- memcpy(ptr,src_name,CFH.file_name_length);
- ptr+=CFH.file_name_length;
- for(long i=0;i<256;i++){
- if(encode_array[i].length_of_encode==0){
- *ptr=0x00;
- ptr+=1;
- }else{
- *ptr=encode_array[i].length_of_encode;
- ptr++;
- memcpy(ptr,encode_array[i].encode,encode_array[i].length_of_encode);
- ptr+=encode_array[i].length_of_encode;
- }
- }
- long temp=fwrite(buffer_for_header,length_of_header2,1,dst_fp);
- if(temp!=1){
- printf("Jcompress: fwrite for compress file header error\n");
- exit(0);
- }
- EncodeBuffer *encode_buffer=(EncodeBuffer *)malloc(sizeof(EncodeBuffer));
- if(encode_buffer==NULL){
- printf("Jcompress: malloc failed\n");
- exit(0);
- }
- encode_buffer->bits_num_lastbytes=0;
- encode_buffer->buffer=NULL;
- encode_buffer->size=0;
- const long fread_buffer_size=4*1024*1024;
- const long encode_buffer_size=32*fread_buffer_size;
- encode_buffer->buffer=(unsigned char *)malloc(encode_buffer_size*sizeof(unsigned char));
- if(encode_buffer->buffer==NULL){
- printf("Jcompress: malloc failed\n");
- exit(0);
- }
- unsigned char * fread_buffer=(unsigned char *)malloc(fread_buffer_size* sizeof(unsigned char));
- if(fread_buffer==NULL){
- printf("Jcompress: malloc failed\n");
- exit(0);
- }
- long read_bytes=0;
- fseek(src_fp,0L, SEEK_SET);
- /**I have made an error define num_ofbits_write as long which cause bigger file compress wrongly*/
- long long num_ofbits_write=-1;
- while((read_bytes=fread(fread_buffer,sizeof(unsigned char),fread_buffer_size,src_fp))!=0){
- unsigned char *ptr=fread_buffer;
- unsigned char *ptr_end=fread_buffer+read_bytes;
- long num_ofbits_write_per_while=-1;
- if(num_ofbits_write>=0){
- num_ofbits_write_per_while+=(num_ofbits_write+1)%8;
- }
- for(;ptr!=ptr_end;ptr++){
- long index=(long)(*ptr);
- if(encode_array[index].length_of_encode<=0){
- printf("Jcompress : encode_array not correct\n");
- exit(0);
- }
- for(long i=0;i<encode_array[index].length_of_encode;i++){
- num_ofbits_write++;
- num_ofbits_write_per_while++;
- long byte_index=num_ofbits_write_per_while/8;
- long bit_index=num_ofbits_write%8;
- long flag=(long)(encode_array[index].encode[i]- '0');
- bit_set((encode_buffer->buffer)+byte_index, bit_index, flag);
- }
- }
- encode_buffer->size=((num_ofbits_write_per_while+1)/8);
- encode_buffer->bits_num_lastbytes=((num_ofbits_write+1)%8);
- long fwrite_size=encode_buffer->size;
- long real_write_num=fwrite(encode_buffer->buffer,sizeof(unsigned char),fwrite_size,dst_fp);
- if(real_write_num!=fwrite_size){
- printf("Jcompress : fwrite error\n");
- exit(0);
- }
- if(encode_buffer->bits_num_lastbytes>0){
- *(encode_buffer->buffer)=*(encode_buffer->buffer+encode_buffer->size);
- }
- }
- if((num_ofbits_write+1)%8!=0){
- long temp=fwrite(encode_buffer->buffer,sizeof(unsigned char),1,dst_fp);
- if(temp!=1){
- printf("Jcompress: fwrite error\n");
- exit(0);
- }
- CFH.bits_of_lastByte=(num_ofbits_write+1)%8;
- }
- /**
- ** now set the correct bits_of_lastByte of the compress file header
- */
- *(buffer_for_header+2*sizeof(long)+4)=CFH.bits_of_lastByte;
- fseek(dst_fp,0L, SEEK_SET);
- long temp0=fwrite(buffer_for_header,1,2*sizeof(long)+5,dst_fp);
- if(temp0!=(2*sizeof(long)+5)){
- printf("Jcompress: fwrite error\n");
- exit(0);
- }
- printf("Jcompress: succeed\n");
- return 1;
- }
int Jcompress(char *src_name){
/**
** check argument
**/
if(src_name==NULL){
printf("Jcompress: argument error\n");
exit(0);
}
printf("******************Jcompress begin to execute************\n");
printf("source file name : %s\n",src_name);
char *dst_name=(char *)malloc(strlen(src_name)+6+1);
memcpy(dst_name,src_name,strlen(src_name));
char *cur0=dst_name+strlen(src_name);
memcpy(cur0,".jcprs",6);
*(cur0+6)='\0';
printf("compress file name: %s\n",dst_name);
FILE *src_fp=NULL;
FILE *dst_fp=NULL;
open_file(src_name,&src_fp);
creat_file(dst_name,&dst_fp);
HuffmanNode **huffman_node_array=(HuffmanNode **)malloc(257*sizeof(HuffmanNode *));
if(huffman_node_array==NULL){
printf("Jcompress: malloc failed\n");
exit(0);
}
long huffman_node_num=0;
make_uchar_weight(src_fp,huffman_node_array,&huffman_node_num);
HuffmanEncode *encode_array=(HuffmanEncode *)malloc(257*sizeof(HuffmanEncode));
if(encode_array==NULL){
printf("Jcompress: malloc failed\n");
exit(0);
}
for(long i=0;i<257;i++){
encode_array[i].encode=NULL;
encode_array[i].length_of_encode=0;
}
huffman_encode(huffman_node_array, huffman_node_num, encode_array);
/**
** begin to deal with the header of the compress file, later the uncompress routine
** can read the header and restore the huffman tree then uncompress the file
*/
CompressFileHeader CFH;
CFH.length_of_author_name=8;
CFH.author_name="zhangjun";
CFH.routine_name_length=9;
CFH.routine_name="Jcompress";
CFH.suffix_length=5;
CFH.suffix="jcprs";
CFH.file_name_length=strlen(src_name);
CFH.bits_of_lastByte=0x00;
long length_of_header1=0;
long length_of_header2=0;
char *cur=src_name;
while(cur[length_of_header1++]!='\0');
length_of_header1+=2*sizeof(long)+9+10+6+1;
length_of_header2=length_of_header1;
for(long i=0;i<256;i++){
if(encode_array[i].length_of_encode==0){
length_of_header2=length_of_header2+1;
}
else if(encode_array[i].length_of_encode>0){
length_of_header2=length_of_header2+1+
encode_array[i].length_of_encode;
}
}
unsigned char *buffer_for_header=(unsigned char*)malloc(sizeof(unsigned char)*length_of_header2);
if(buffer_for_header==NULL){
printf("Jcompress:malloc failed for buffer_for_header\n");
exit(0);
}
unsigned char *ptr=buffer_for_header;
*((long *)ptr)=length_of_header1;
*((long *)(ptr+sizeof(long)))=length_of_header2;
ptr+=2*sizeof(long);
*(ptr++)=(unsigned char)CFH.length_of_author_name;
*(ptr++)=(unsigned char)CFH.routine_name_length;
*(ptr++)=(unsigned char)CFH.suffix_length;
*(ptr++)=(unsigned char)CFH.file_name_length;
*(ptr++)=(unsigned char)CFH.bits_of_lastByte;
char *ch="zhangjunJcompressjcprs";
memcpy(ptr,ch,22);
ptr+=22;
memcpy(ptr,src_name,CFH.file_name_length);
ptr+=CFH.file_name_length;
for(long i=0;i<256;i++){
if(encode_array[i].length_of_encode==0){
*ptr=0x00;
ptr+=1;
}else{
*ptr=encode_array[i].length_of_encode;
ptr++;
memcpy(ptr,encode_array[i].encode,encode_array[i].length_of_encode);
ptr+=encode_array[i].length_of_encode;
}
}
long temp=fwrite(buffer_for_header,length_of_header2,1,dst_fp);
if(temp!=1){
printf("Jcompress: fwrite for compress file header error\n");
exit(0);
}
EncodeBuffer *encode_buffer=(EncodeBuffer *)malloc(sizeof(EncodeBuffer));
if(encode_buffer==NULL){
printf("Jcompress: malloc failed\n");
exit(0);
}
encode_buffer->bits_num_lastbytes=0;
encode_buffer->buffer=NULL;
encode_buffer->size=0;
const long fread_buffer_size=4*1024*1024;
const long encode_buffer_size=32*fread_buffer_size;
encode_buffer->buffer=(unsigned char *)malloc(encode_buffer_size*sizeof(unsigned char));
if(encode_buffer->buffer==NULL){
printf("Jcompress: malloc failed\n");
exit(0);
}
unsigned char * fread_buffer=(unsigned char *)malloc(fread_buffer_size*sizeof(unsigned char));
if(fread_buffer==NULL){
printf("Jcompress: malloc failed\n");
exit(0);
}
long read_bytes=0;
fseek(src_fp,0L, SEEK_SET);
/**I have made an error define num_ofbits_write as long which cause bigger file compress wrongly*/
long long num_ofbits_write=-1;
while((read_bytes=fread(fread_buffer,sizeof(unsigned char),fread_buffer_size,src_fp))!=0){
unsigned char *ptr=fread_buffer;
unsigned char *ptr_end=fread_buffer+read_bytes;
long num_ofbits_write_per_while=-1;
if(num_ofbits_write>=0){
num_ofbits_write_per_while+=(num_ofbits_write+1)%8;
}
for(;ptr!=ptr_end;ptr++){
long index=(long)(*ptr);
if(encode_array[index].length_of_encode<=0){
printf("Jcompress : encode_array not correct\n");
exit(0);
}
for(long i=0;i<encode_array[index].length_of_encode;i++){
num_ofbits_write++;
num_ofbits_write_per_while++;
long byte_index=num_ofbits_write_per_while/8;
long bit_index=num_ofbits_write%8;
long flag=(long)(encode_array[index].encode[i]-'0');
bit_set((encode_buffer->buffer)+byte_index, bit_index, flag);
}
}
encode_buffer->size=((num_ofbits_write_per_while+1)/8);
encode_buffer->bits_num_lastbytes=((num_ofbits_write+1)%8);
long fwrite_size=encode_buffer->size;
long real_write_num=fwrite(encode_buffer->buffer,sizeof(unsigned char),fwrite_size,dst_fp);
if(real_write_num!=fwrite_size){
printf("Jcompress : fwrite error\n");
exit(0);
}
if(encode_buffer->bits_num_lastbytes>0){
*(encode_buffer->buffer)=*(encode_buffer->buffer+encode_buffer->size);
}
}
if((num_ofbits_write+1)%8!=0){
long temp=fwrite(encode_buffer->buffer,sizeof(unsigned char),1,dst_fp);
if(temp!=1){
printf("Jcompress: fwrite error\n");
exit(0);
}
CFH.bits_of_lastByte=(num_ofbits_write+1)%8;
}
/**
** now set the correct bits_of_lastByte of the compress file header
*/
*(buffer_for_header+2*sizeof(long)+4)=CFH.bits_of_lastByte;
fseek(dst_fp,0L, SEEK_SET);
long temp0=fwrite(buffer_for_header,1,2*sizeof(long)+5,dst_fp);
if(temp0!=(2*sizeof(long)+5)){
printf("Jcompress: fwrite error\n");
exit(0);
}
printf("Jcompress: succeed\n");
return 1;
}
restore_huffman_tree()根据参数huffman_encode_array存储的编码表来重建哈夫曼树,重建的哈夫曼树根结点的指针存储在huffman_tree_root所指向的内存中。重建的过程也很简单,对于每一个usigned char,根据其编码,从哈夫曼树跟结点一遍走一遍构造子结点,便可以非常自然地重现哈夫曼树。这颗哈夫曼树在后文解压缩的时候会使用到。
- int restore_huffman_tree(HuffmanNode **huffman_tree_root, HuffmanEncode *huffman_encode_array){
- /**
- ** check error for argument
- */
- if(huffman_tree_root==NULL || huffman_encode_array==NULL){
- printf("restore_huffman_tree: argument error\n");
- exit(0);
- }
- long valid_encode_num=0;
- for(long i=0;i<256;i++){
- if(huffman_encode_array[i].length_of_encode>0){
- valid_encode_num+=1;
- }
- }
- HuffmanNode *huffman_nodes=(HuffmanNode *)malloc(sizeof(HuffmanNode)*(valid_encode_num*2-1));
- if(huffman_nodes==NULL){
- printf("\nrestore_huffman_tree: malloc failed\n");
- exit(0);
- }
- for(long i=0;i<valid_encode_num*2-1;i++){
- huffman_nodes[i].left_child=NULL;
- huffman_nodes[i].right_child=NULL;
- huffman_nodes[i].parent=NULL;
- }
- long huffman_nodes_index=0;
- (*huffman_tree_root)=&(huffman_nodes[huffman_nodes_index++]);
- for(long i=0;i<256;i++){
- if(huffman_encode_array[i].length_of_encode>0){
- HuffmanNode *current=(*huffman_tree_root);
- for(long j=0;j<huffman_encode_array[i].length_of_encode;j++){
- if(huffman_encode_array[i].encode[j]=='0'){
- if(current->left_child!=NULL) current=current->left_child;
- else{
- current->left_child=&(huffman_nodes[huffman_nodes_index++]);
- current->left_child->parent=current;
- current=current->left_child;
- }
- }else if(huffman_encode_array[i].encode[j]== '1'){
- if(current->right_child!=NULL) current=current->right_child;
- else{
- current->right_child=&(huffman_nodes[huffman_nodes_index++]);
- current->right_child->parent=current;
- current=current->right_child;
- }
- }else{
- printf("restore_huffman_tree: encode array error\n");
- exit(0);
- }
- }
- current->data_8bit=(unsigned char)i;
- }
- }
- return 1;
- }
int restore_huffman_tree(HuffmanNode **huffman_tree_root, HuffmanEncode *huffman_encode_array){
/**
** check error for argument
*/
if(huffman_tree_root==NULL || huffman_encode_array==NULL){
printf("restore_huffman_tree: argument error\n");
exit(0);
}
long valid_encode_num=0;
for(long i=0;i<256;i++){
if(huffman_encode_array[i].length_of_encode>0){
valid_encode_num+=1;
}
}
HuffmanNode *huffman_nodes=(HuffmanNode *)malloc(sizeof(HuffmanNode)*(valid_encode_num*2-1));
if(huffman_nodes==NULL){
printf("\nrestore_huffman_tree: malloc failed\n");
exit(0);
}
for(long i=0;i<valid_encode_num*2-1;i++){
huffman_nodes[i].left_child=NULL;
huffman_nodes[i].right_child=NULL;
huffman_nodes[i].parent=NULL;
}
long huffman_nodes_index=0;
(*huffman_tree_root)=&(huffman_nodes[huffman_nodes_index++]);
for(long i=0;i<256;i++){
if(huffman_encode_array[i].length_of_encode>0){
HuffmanNode *current=(*huffman_tree_root);
for(long j=0;j<huffman_encode_array[i].length_of_encode;j++){
if(huffman_encode_array[i].encode[j]=='0'){
if(current->left_child!=NULL) current=current->left_child;
else{
current->left_child=&(huffman_nodes[huffman_nodes_index++]);
current->left_child->parent=current;
current=current->left_child;
}
}else if(huffman_encode_array[i].encode[j]=='1'){
if(current->right_child!=NULL) current=current->right_child;
else{
current->right_child=&(huffman_nodes[huffman_nodes_index++]);
current->right_child->parent=current;
current=current->right_child;
}
}else{
printf("restore_huffman_tree: encode array error\n");
exit(0);
}
}
current->data_8bit=(unsigned char)i;
}
}
return 1;
}
JparseHead()函数负责在解压缩的过程当中分析压缩文件的头部,解析出一些必要的信息,包括:编码表,最后一个字节有效bit数量等。函数的第一个参数是一个文件指针的指针,fp是由调用者在打开压缩文件后得到的并将它作为参数传递给JparseHead. 第二个参数存储JparseHead()得到的编码表,该编码表会被传递给restore_huffman_tree()以重建哈夫曼树。file_name存储的是一个指针,该指针指向解析出的原始文件名称,最后一个参数last_bits存储的便是解析出来的最后一个字节包含的有效bit数量。各种必要的信息都是按照特定的顺序存储在压缩文件的头部,把它们解析出来是一件很自然的事情,详情见下面的代码:
- int JparseHead(FILE **fp, HuffmanEncode *huffman_encode_array, char **file_name, long *last_bits){
- /**
- ** check error for argument
- */
- if(fp==NULL || *fp==NULL || huffman_encode_array==NULL || file_name==NULL || last_bits==NULL){
- printf("JparseHead: argument error\n");
- exit(0);
- }
- long sizeof_header1=0;
- long sizeof_header2=0;
- unsigned char *buffer=(unsigned char* )malloc(sizeof( long)*2);
- long read_num=fread(buffer,sizeof(long),2,*fp);
- if(read_num!=2){
- printf("JparseHead: read first two long of header failed\n");
- exit(0);
- }
- sizeof_header1=*((long *)buffer);
- sizeof_header2=*((long *)(buffer+sizeof(long)));
- if(buffer!=NULL){
- free(buffer);
- buffer=NULL;
- }
- /** I have make a silly error to use sizeof(sizeof_header2) which take lots of time to debug*/
- unsigned char *buffer_header=(unsigned char *)malloc(sizeof(unsigned char)*sizeof_header2);
- if(buffer_header==NULL){
- printf("JparseHead: malloc for buffer_header failed\n");
- exit(0);
- }
- fseek(*fp,0L, SEEK_SET);
- read_num=fread(buffer_header,sizeof(unsigned char),sizeof_header2,*fp);
- if(read_num!=sizeof_header2){
- printf("JparseHead: fread failed\n");
- exit(0);
- }
- unsigned char * ptr_header1=buffer_header;
- unsigned char * ptr_header1_end=buffer_header+sizeof_header1;
- unsigned char * ptr_header2=buffer_header+sizeof_header1;
- unsigned char * ptr_header2_end=buffer_header+sizeof_header2;
- ptr_header1+=sizeof(long)*2;
- long length_of_author_name=*(ptr_header1);
- long length_of_routine_name=*(ptr_header1+1);
- long length_of_suffix=*(ptr_header1+2);
- long length_of_file_name=*(ptr_header1+3);
- long length_of_lastbits=*(ptr_header1+4);
- ptr_header1+=5;
- *last_bits=length_of_lastbits;
- printf("\nJcompress author: ");
- unsigned char *ptr=NULL;
- long i=0;
- for(i=1,ptr=ptr_header1;i<=length_of_author_name;ptr++,i++){
- printf("%c",*ptr);
- }
- printf("\ncompress routine is: ");
- for(long i=1;i<=length_of_routine_name;ptr++,i++){
- printf("%c",*ptr);
- }
- printf("\ncompress suffix is: ");
- for(long i=1;i<=length_of_suffix;ptr++,i++){
- printf("%c",*ptr);
- }
- (*file_name)=(char *)malloc(sizeof(char)*(length_of_file_name+1));
- if(file_name==NULL){
- printf("JparseHeader: malloc failed\n");
- exit(0);
- }
- memcpy((*file_name),ptr,length_of_file_name);
- (*file_name)[length_of_file_name]='\0';
- printf("\noriginal file name is: ");
- for(long i=1;i<=length_of_file_name;ptr++,i++){
- printf("%c",*ptr);
- }
- printf("\n");
- long index=-1;
- unsigned char *cur=ptr_header2;
- while(cur<ptr_header2_end){
- index++;
- if(*cur==0x00){
- cur++;
- }else{
- huffman_encode_array[index].encode=(char *)malloc((*cur)*sizeof( char));
- if(huffman_encode_array[index].encode==NULL){
- printf("JparseHead: malloc failed\n");
- exit(0);
- }
- huffman_encode_array[index].length_of_encode=*cur;
- memcpy(huffman_encode_array[index].encode,cur+1,*cur);
- cur=cur+1+huffman_encode_array[index].length_of_encode;
- }
- }
- if(buffer_header!=NULL){
- free(buffer_header);
- buffer_header=NULL;
- }
- return 1;
- }
int JparseHead(FILE **fp, HuffmanEncode *huffman_encode_array, char **file_name, long *last_bits){
/**
** check error for argument
*/
if(fp==NULL || *fp==NULL || huffman_encode_array==NULL || file_name==NULL || last_bits==NULL){
printf("JparseHead: argument error\n");
exit(0);
}
long sizeof_header1=0;
long sizeof_header2=0;
unsigned char *buffer=(unsigned char* )malloc(sizeof(long)*2);
long read_num=fread(buffer,sizeof(long),2,*fp);
if(read_num!=2){
printf("JparseHead: read first two long of header failed\n");
exit(0);
}
sizeof_header1=*((long *)buffer);
sizeof_header2=*((long *)(buffer+sizeof(long)));
if(buffer!=NULL){
free(buffer);
buffer=NULL;
}
/** I have make a silly error to use sizeof(sizeof_header2) which take lots of time to debug*/
unsigned char *buffer_header=(unsigned char *)malloc(sizeof(unsigned char)*sizeof_header2);
if(buffer_header==NULL){
printf("JparseHead: malloc for buffer_header failed\n");
exit(0);
}
fseek(*fp,0L, SEEK_SET);
read_num=fread(buffer_header,sizeof(unsigned char),sizeof_header2,*fp);
if(read_num!=sizeof_header2){
printf("JparseHead: fread failed\n");
exit(0);
}
unsigned char * ptr_header1=buffer_header;
unsigned char * ptr_header1_end=buffer_header+sizeof_header1;
unsigned char * ptr_header2=buffer_header+sizeof_header1;
unsigned char * ptr_header2_end=buffer_header+sizeof_header2;
ptr_header1+=sizeof(long)*2;
long length_of_author_name=*(ptr_header1);
long length_of_routine_name=*(ptr_header1+1);
long length_of_suffix=*(ptr_header1+2);
long length_of_file_name=*(ptr_header1+3);
long length_of_lastbits=*(ptr_header1+4);
ptr_header1+=5;
*last_bits=length_of_lastbits;
printf("\nJcompress author: ");
unsigned char *ptr=NULL;
long i=0;
for(i=1,ptr=ptr_header1;i<=length_of_author_name;ptr++,i++){
printf("%c",*ptr);
}
printf("\ncompress routine is: ");
for(long i=1;i<=length_of_routine_name;ptr++,i++){
printf("%c",*ptr);
}
printf("\ncompress suffix is: ");
for(long i=1;i<=length_of_suffix;ptr++,i++){
printf("%c",*ptr);
}
(*file_name)=(char *)malloc(sizeof(char)*(length_of_file_name+1));
if(file_name==NULL){
printf("JparseHeader: malloc failed\n");
exit(0);
}
memcpy((*file_name),ptr,length_of_file_name);
(*file_name)[length_of_file_name]='\0';
printf("\noriginal file name is: ");
for(long i=1;i<=length_of_file_name;ptr++,i++){
printf("%c",*ptr);
}
printf("\n");
long index=-1;
unsigned char *cur=ptr_header2;
while(cur<ptr_header2_end){
index++;
if(*cur==0x00){
cur++;
}else{
huffman_encode_array[index].encode=(char *)malloc((*cur)*sizeof(char));
if(huffman_encode_array[index].encode==NULL){
printf("JparseHead: malloc failed\n");
exit(0);
}
huffman_encode_array[index].length_of_encode=*cur;
memcpy(huffman_encode_array[index].encode,cur+1,*cur);
cur=cur+1+huffman_encode_array[index].length_of_encode;
}
}
if(buffer_header!=NULL){
free(buffer_header);
buffer_header=NULL;
}
return 1;
}
JUnCompress()是本文要介绍的最后一个函数,它是用来解压缩文件的,该函数的实现基于上文介绍的JparseHead()和restore_huffman_tree()函数。其参数src_name存储的是待解压缩的文件的名称。在介绍完前文的一系列函数之后在回过头来看JUnCompress()的代码,其逻辑也比较清晰易懂。它首先调用JparseHead()函数来解析压缩文件的头部,解析出来的哈夫曼编码表交给restore_huffman_tree()来重建哈夫曼树。再往下是一个while循环,该循环不断地调用fread函数来读磁盘中的压缩文件。每读一块交交给huffman_decode()函数来解码,解码而得的数据会被存储到一个新的磁盘文件里。同样在这里也要注意读入的压缩文件bit数量不能被8整除的情况。
- int JUnCompress(char *src_name){
- /**
- ** check error for argument
- */
- if(access(src_name,0)!=0){
- printf("file not exist\n");
- exit(0);
- }
- printf("\n******************UnCompress file begin******************\n");
- printf("compress file name is: %s\n",src_name);
- FILE *ptr_src=NULL;
- open_file(src_name,&ptr_src);
- long compress_file_size=0;
- fseek(ptr_src,0L,SEEK_END);
- compress_file_size=ftell(ptr_src);
- if(compress_file_size==-1){
- printf("compress file too big to display its size(bigger than 2.1G)\nbut Jcompress will run normally\n");
- }else{
- printf("compress file size: %ld bytes\n",compress_file_size);
- }
- fseek(ptr_src,0L, SEEK_SET);
- long last_bits=0;
- char *file_name=NULL;
- HuffmanEncode *huffman_encode_array=(HuffmanEncode *)malloc(sizeof(HuffmanEncode)*256);
- if(huffman_encode_array==NULL){
- printf("JUnCompress: malloc huffman_encode_array failed\n");
- exit(0);
- }
- for(int i=0;i<256;i++){
- huffman_encode_array[i].encode=NULL;
- huffman_encode_array[i].length_of_encode=0;
- }
- JparseHead(&ptr_src,huffman_encode_array,&file_name,&last_bits);
- HuffmanNode *huffman_tree_root=NULL;
- restore_huffman_tree(&huffman_tree_root,huffman_encode_array);
- //test_traverse_prlong long_hfmtree(huffman_tree_root,-1);
- FILE *ptr_dst=NULL;
- create_Uncompress_file(file_name,&ptr_dst);
- EncodeBuffer *encode_buffer=(EncodeBuffer *)malloc(sizeof(EncodeBuffer));
- DecodeBuffer *decode_buffer=(DecodeBuffer *)malloc(sizeof(DecodeBuffer));
- if(encode_buffer==NULL || decode_buffer==NULL){
- printf("JUnCompress: encode/decode buffer malloc failed\n");
- exit(0);
- }
- encode_buffer->bits_num_lastbytes=0;
- encode_buffer->buffer=NULL;
- encode_buffer->size=0;
- const long enbuffer_size=8*1024*1024;
- const long debuffer_size=10*enbuffer_size;
- encode_buffer->buffer=(unsigned char *)malloc(enbuffer_size*sizeof(unsigned char));
- if(encode_buffer->buffer==NULL){
- printf("JUnCompress: EncodeBuffer->buffer malloc failed\n");
- exit(0);
- }
- decode_buffer->size=0;
- decode_buffer->buffer=(unsigned char *)malloc(debuffer_size*sizeof(unsigned char));
- if(decode_buffer->buffer==NULL){
- printf("JUnCompress: EncodeBuffer->buffer malloc failed\n");
- exit(0);
- }
- long long read_compress_file_size=0;
- long read_bytes=0;
- HuffmanNode *last_stop_at=NULL;
- while((read_bytes=fread(encode_buffer->buffer,sizeof(unsigned char),enbuffer_size,ptr_src))!=0){
- read_compress_file_size+=read_bytes;
- if(read_compress_file_size==compress_file_size){
- if(last_bits>0){
- encode_buffer->bits_num_lastbytes=last_bits;
- encode_buffer->size=read_bytes-1;
- }else{
- encode_buffer->bits_num_lastbytes=0;
- encode_buffer->size=read_bytes;
- }
- }else{
- encode_buffer->bits_num_lastbytes=0;
- encode_buffer->size=read_bytes;
- }
- huffman_decode(huffman_tree_root,&last_stop_at,encode_buffer,decode_buffer);
- long write_bytes=fwrite(decode_buffer->buffer,sizeof(unsigned char),decode_buffer->size,ptr_dst);
- if(write_bytes!=decode_buffer->size){
- printf("JUnCompress: fwrite failed\n");
- exit(0);
- }
- }
- printf("UnCompress file succeed\n");
- return 1;
- }
int JUnCompress(char *src_name){
/**
** check error for argument
*/
if(access(src_name,0)!=0){
printf("file not exist\n");
exit(0);
}
printf("\n******************UnCompress file begin******************\n");
printf("compress file name is: %s\n",src_name);
FILE *ptr_src=NULL;
open_file(src_name,&ptr_src);
long compress_file_size=0;
fseek(ptr_src,0L,SEEK_END);
compress_file_size=ftell(ptr_src);
if(compress_file_size==-1){
printf("compress file too big to display its size(bigger than 2.1G)\nbut Jcompress will run normally\n");
}else{
printf("compress file size: %ld bytes\n",compress_file_size);
}
fseek(ptr_src,0L, SEEK_SET);
long last_bits=0;
char *file_name=NULL;
HuffmanEncode *huffman_encode_array=(HuffmanEncode *)malloc(sizeof(HuffmanEncode)*256);
if(huffman_encode_array==NULL){
printf("JUnCompress: malloc huffman_encode_array failed\n");
exit(0);
}
for(int i=0;i<256;i++){
huffman_encode_array[i].encode=NULL;
huffman_encode_array[i].length_of_encode=0;
}
JparseHead(&ptr_src,huffman_encode_array,&file_name,&last_bits);
HuffmanNode *huffman_tree_root=NULL;
restore_huffman_tree(&huffman_tree_root,huffman_encode_array);
//test_traverse_prlong long_hfmtree(huffman_tree_root,-1);
FILE *ptr_dst=NULL;
create_Uncompress_file(file_name,&ptr_dst);
EncodeBuffer *encode_buffer=(EncodeBuffer *)malloc(sizeof(EncodeBuffer));
DecodeBuffer *decode_buffer=(DecodeBuffer *)malloc(sizeof(DecodeBuffer));
if(encode_buffer==NULL || decode_buffer==NULL){
printf("JUnCompress: encode/decode buffer malloc failed\n");
exit(0);
}
encode_buffer->bits_num_lastbytes=0;
encode_buffer->buffer=NULL;
encode_buffer->size=0;
const long enbuffer_size=8*1024*1024;
const long debuffer_size=10*enbuffer_size;
encode_buffer->buffer=(unsigned char *)malloc(enbuffer_size*sizeof(unsigned char));
if(encode_buffer->buffer==NULL){
printf("JUnCompress: EncodeBuffer->buffer malloc failed\n");
exit(0);
}
decode_buffer->size=0;
decode_buffer->buffer=(unsigned char *)malloc(debuffer_size*sizeof(unsigned char));
if(decode_buffer->buffer==NULL){
printf("JUnCompress: EncodeBuffer->buffer malloc failed\n");
exit(0);
}
long long read_compress_file_size=0;
long read_bytes=0;
HuffmanNode *last_stop_at=NULL;
while((read_bytes=fread(encode_buffer->buffer,sizeof(unsigned char),enbuffer_size,ptr_src))!=0){
read_compress_file_size+=read_bytes;
if(read_compress_file_size==compress_file_size){
if(last_bits>0){
encode_buffer->bits_num_lastbytes=last_bits;
encode_buffer->size=read_bytes-1;
}else{
encode_buffer->bits_num_lastbytes=0;
encode_buffer->size=read_bytes;
}
}else{
encode_buffer->bits_num_lastbytes=0;
encode_buffer->size=read_bytes;
}
huffman_decode(huffman_tree_root,&last_stop_at,encode_buffer,decode_buffer);
long write_bytes=fwrite(decode_buffer->buffer,sizeof(unsigned char),decode_buffer->size,ptr_dst);
if(write_bytes!=decode_buffer->size){
printf("JUnCompress: fwrite failed\n");
exit(0);
}
}
printf("UnCompress file succeed\n");
return 1;
}
结语
至此,本文就已经叙述完毕,完整的Jcompress小程序代码,读者可以到笔者的github上面下载,下载链接在文章开头处的前言里面。如果读者对本文有什么疑问,可以在评论里面提出,我如果看见了会及时回答。另外本文如有欠缺或者瑕疵之类的,也请读者不吝赐教。希望我们大家一起学习、一起进步。如果对本文有疑问的话也可以通过微博私信的方式与笔者联系提出,微博地址:http://weibo.com/junhuster
