C LZW中的压缩问题
我在安装LZW的压缩机时遇到问题。压缩器似乎工作正常,但在处理某些流时,它没有放入用值256定义的流结束字符,结果是解压器将无限循环。 压缩机的代码如下所示:C LZW中的压缩问题,c,compression,lzw,C,Compression,Lzw,我在安装LZW的压缩机时遇到问题。压缩器似乎工作正常,但在处理某些流时,它没有放入用值256定义的流结束字符,结果是解压器将无限循环。 压缩机的代码如下所示: int compress1(FILE* input, BIT_FILE* output) { CODE next_code; // next node CODE current_code; // current node CODE index; // node of the found c
int compress1(FILE* input, BIT_FILE* output) {
CODE next_code; // next node
CODE current_code; // current node
CODE index; // node of the found character
int character;
int ret;
next_code = FIRST_CODE;
dictionary_init();
if ((current_code = getc(input)) == EOF)
current_code = EOS;
while ((character = getc(input)) != EOF) {
index = dictionary_lookup(current_code, (SYMBOL)character);
if (dictionary[index].code != UNUSED) {
current_code = dictionary[index].code;
}
else {
if (next_code <= MAX_CODE-1) {
dictionary[index].code = next_code++;
dictionary[index].parent = current_code;
dictionary[index].symbol = (SYMBOL)character;
}
else {
// handling full dictionary
dictionary_init();
next_code = FIRST_CODE;
}
ret = bit_write(output, (uint64_t) current_code, BITS);
if( ret != 0)
return -1;
current_code = (CODE)character;
}
}
ret = bit_write(output, (uint64_t) current_code, BITS);
if (ret != 0)
return -1;
ret = bit_write(output, (uint64_t) EOS, BITS);
if (ret != 0)
return -1;
if (bit_close(output) == -1) {
printf("Ops: error during closing\n");
return -1;
}
return 0;
}
我已经使用另一个函数打开了该文件,因此位_write将指向bf结构的指针作为输入。
有人能帮我找出错误吗
出现此问题的示例如下:
如果输入字符串为Nel mezzo del cammi,一切正常,我有以下十六进制压缩文件,使用12位编码符号:
4E 50 06 6C 00 02 6D 50 06 7A A0 07 6F 00 02 64
20 10 20 30 06 61 D0 06 6D 90 06 0D A0 00 01
如果我在字符串中添加另一个字符,特别是Nel mezzo del cammin,我会得到以下结果:
4E 50 06 6C 00 02 6D 50 06 7A A0 07 6F 00 02 64
20 10 20 30 06 61 D0 06 6D 90 06 6E D0 00 0A 00
十,
在第二种情况下,它不能正确地写入流的结尾
解决方案:检查缓冲区中是否有足够的空间容纳我将要写入的整个编码符号。只要改变一下:
if (space == 0)
致:
这是一个稳定的问题吗?也就是说,在相同的输入上是否有相同的结果无流结束字符?如果是,向我们展示问题输入的示例。在不放置EOS的情况下,位_write是否返回-1?我添加了一个执行示例来向您展示压缩文件。bit_write return-1在文件处理过程中出现错误时打开、关闭、刷新等,但不是在您提到的情况下。我终于找到了这个问题的解决方案。我必须检查缓冲区中是否有足够的空间容纳整个编码符号,否则它将分割符号,并且无法再恢复。所以做一点小小的改变,让它工作起来:if space==0&&space
if (space == 0)
if(space == 0 && space < len)