此处还提供了“准则”,并且运行完美,并显示错误 - http://ideone.com/gWEoIV
#include <stdio.h>
#include <stdlib.h>
unsigned char *MyCompress(unsigned int num, unsigned char *buffer);
unsigned int MyDeCompress(unsigned char **buffer);
int main(void) {
unsigned char *abc, *abc2;
unsigned len;
abc=(unsigned char *)malloc(12);
abc2=abc;
len=25960;
abc = MyCompress(len, abc);
len=385;
abc = MyCompress(len, abc);
len=900;
abc = MyCompress(len, abc);
len=20560;
abc = MyCompress(len, abc);
len=384;
abc = MyCompress(len, abc);
abc=abc2;
len = MyDeCompress(&abc);
len = MyDeCompress(&abc);
len = MyDeCompress(&abc);
len = MyDeCompress(&abc);
len = MyDeCompress(&abc);
return 0;
}
unsigned int MyDeCompress(unsigned char **buffer){
unsigned int c;
unsigned int num = 0;
unsigned char *p = *buffer;
printf("\n\nDeCompression\n===============\n");
do{
c = ((unsigned char) *p++);
printf("Read=%d\n", c);
if (c <= 0) {
printf("Error: c is < 0 in uncompress1()\n");
exit(0);
}
num <<= 7;
num |= c & 127;
printf("uncompress: c = %d num = %d\n", c, num);
if (!num)
break;
}while (c & 128);
*buffer = p;
printf("Returning %d\n", num);
return num;
}
unsigned char *MyCompress(unsigned int num, unsigned char *buffer){
int i = 0;
unsigned int r = num;
unsigned char temp;
unsigned char s[5];
printf("Compression\n===============\n");
printf("received %d to compress\n", num);
if(!r){
*buffer++ = 0;
return buffer;
}
while (r){
s[i] = r & 127;
r >>= 7;
printf("s[%d]=%d; r=%d\n", i, s[i], r);
i++;
}
while (--i >= 0){
temp = (unsigned char)(s[i] | (i ? 128 : 0));
printf("temp=%d\n", temp);
*buffer++=temp;
}
return buffer;
}
//输出:
Compression
===============
received 25960 to compress
s[0]=104; r=202
s[1]=74; r=1
s[2]=1; r=0
temp=129
temp=202
temp=104
Compression
===============
received 385 to compress
s[0]=1; r=3
s[1]=3; r=0
temp=131
temp=1
Compression
===============
received 900 to compress
s[0]=4; r=7
s[1]=7; r=0
temp=135
temp=4
Compression
===============
received 20560 to compress
s[0]=80; r=160
s[1]=32; r=1
s[2]=1; r=0
temp=129
temp=160
temp=80
Compression
===============
received 384 to compress
s[0]=0; r=3
s[1]=3; r=0
temp=131
temp=0
DeCompression
===============
Read=129
uncompress: c = 129 num = 1
Read=202
uncompress: c = 202 num = 202
Read=104
uncompress: c = 104 num = 25960
Returning 25960
DeCompression
===============
Read=131
uncompress: c = 131 num = 3
Read=1
uncompress: c = 1 num = 385
Returning 385
DeCompression
===============
Read=135
uncompress: c = 135 num = 7
Read=4
uncompress: c = 4 num = 900
Returning 900
DeCompression
===============
Read=129
uncompress: c = 129 num = 1
Read=160
uncompress: c = 160 num = 160
Read=80
uncompress: c = 80 num = 20560
Returning 20560
DeCompression
===============
Read=131
uncompress: c = 131 num = 3
Read=0
Error: c is < 0 in uncompress1()
为什么我收到错误:当DeCompress 384号码?