diff --git a/libentcode/ectest.c b/libentcode/ectest.c index 85994dcf..deedad2b 100644 --- a/libentcode/ectest.c +++ b/libentcode/ectest.c @@ -1,3 +1,4 @@ +#include #include #include #include "probenc.h" @@ -139,6 +140,32 @@ int main(int _argc,char **_argv){ ldexp(nbits2,-4),ldexp(nbits,-4)); } ec_byte_writeclear(&buf); - fprintf(stderr,"All tests passed.\n"); + fprintf(stderr,"Testing random streams...\n"); + srand(0); + for(i=0;i<1024;i++){ + unsigned *data; + int j; + ft=rand()/((RAND_MAX>>9)+1)+512; + sz=rand()/((RAND_MAX>>9)+1); + data=(unsigned *)malloc(sz*sizeof(*data)); + ec_byte_writeinit(&buf); + ec_enc_init(&enc,&buf); + for(j=0;jbuf); if(ret<0){ unsigned char *buf; - long bytes; - bytes=ec_byte_bytes(_this->buf); + long i; + i=ec_byte_bytes(_this->buf); buf=ec_byte_get_buffer(_this->buf); /*Breaking abstraction: don't do this at home, kids.*/ - if(_this->buf->storage==bytes){ - ec_byte_adv1(_this->buf); - if(bytes>0){ - unsigned char *p; - p=buf+bytes; - /*If we end in a string of 0 or more EC_FOF_RSV1 bytes preceded by a - zero, return an extra EC_FOF_RSV1 byte.*/ - do p--; - while(p>buf&&p[0]==EC_FOF_RSV1); - if(!p[0])return EC_FOF_RSV1; - } + if(_this->buf->storage==i&&i>0){ + unsigned char *buf; + buf=ec_byte_get_buffer(_this->buf); + /*If we end in a string of 0 or more EC_FOF_RSV1 bytes preceded by a + zero, return an extra EC_FOF_RSV1 byte.*/ + do i--; + while(i>0&&buf[i]==EC_FOF_RSV1); + if(!buf[i])ret=EC_FOF_RSV1; + else ret=0; } - return 0; + else ret=0; + /*Needed to make sure the above conditional only triggers once, and to keep + oc_dec_tell() operating correctly.*/ + ec_byte_adv1(_this->buf); } - else return ret; + return ret; } /*Normalizes the contents of dif and rng so that rng lies entirely in the diff --git a/libentcode/mfrngenc.c b/libentcode/mfrngenc.c index 2b2b15c6..704b3e4e 100644 --- a/libentcode/mfrngenc.c +++ b/libentcode/mfrngenc.c @@ -169,25 +169,26 @@ void ec_enc_done(ec_enc *_this){ } /*If we have a buffered byte...*/ if(_this->rem>=0){ - unsigned char *p; unsigned char *buf; + long i; /*Flush it into the output buffer.*/ ec_enc_carry_out(_this,0); _this->rem=-1; /*We may be able to drop some redundant bytes from the end.*/ buf=ec_byte_get_buffer(_this->buf); - p=buf+ec_byte_bytes(_this->buf)-1; + i=ec_byte_bytes(_this->buf); /*Strip trailing zeros.*/ - while(p>=buf&&!p[0])p--; + do i--; + while(i>0&&!buf[i]); /*Strip one trailing EC_FOF_RSV1 byte if the buffer ends in a string of consecutive EC_FOF_RSV1 bytes preceded by one (or more) zeros.*/ - if(p>buf&&p[0]==EC_FOF_RSV1){ - unsigned char *q; - q=p; - do q--; - while(q>buf&&q[0]==EC_FOF_RSV1); - if(!q[0])p--; + if(i>0&&buf[i]==EC_FOF_RSV1){ + long j; + j=i; + do j--; + while(j>0&&buf[j]==EC_FOF_RSV1); + if(!buf[j])i--; } - ec_byte_writetrunc(_this->buf,p+1-buf); + ec_byte_writetrunc(_this->buf,i+1); } } diff --git a/libentcode/rangedec.c b/libentcode/rangedec.c index bfe66452..1d2815d0 100644 --- a/libentcode/rangedec.c +++ b/libentcode/rangedec.c @@ -110,6 +110,7 @@ }*/ +#include /*Gets the next byte of input. After all the bytes in the current packet have been consumed, and the extra @@ -121,25 +122,26 @@ static int ec_dec_in(ec_dec *_this){ ret=ec_byte_read1(_this->buf); if(ret<0){ unsigned char *buf; - long bytes; - bytes=ec_byte_bytes(_this->buf); + long i; + i=ec_byte_bytes(_this->buf); buf=ec_byte_get_buffer(_this->buf); /*Breaking abstraction: don't do this at home, kids.*/ - if(_this->buf->storage==bytes){ - ec_byte_adv1(_this->buf); - if(bytes>0){ - unsigned char *p; - p=buf+bytes; - /*If we end in a string of 0 or more EC_FOF_RSV1 bytes preceded by a - zero, return an extra EC_FOF_RSV1 byte.*/ - do p--; - while(p>buf&&p[0]==EC_FOF_RSV1); - if(!p[0])return EC_FOF_RSV1; - } + if(_this->buf->storage==i&&i>0){ + unsigned char *buf; + buf=ec_byte_get_buffer(_this->buf); + /*If we end in a string of 0 or more EC_FOF_RSV1 bytes preceded by a + zero, return an extra EC_FOF_RSV1 byte.*/ + do i--; + while(i>0&&buf[i]==EC_FOF_RSV1); + if(!buf[i])ret=EC_FOF_RSV1; + else ret=0; } - return 0; + else ret=0; + /*Needed to make sure the above conditional only triggers once, and to keep + oc_dec_tell() operating correctly.*/ + ec_byte_adv1(_this->buf); } - else return ret; + return ret; } /*Normalizes the contents of dif and rng so that rng lies entirely in the diff --git a/libentcode/rangeenc.c b/libentcode/rangeenc.c index 246a4555..47f067bb 100644 --- a/libentcode/rangeenc.c +++ b/libentcode/rangeenc.c @@ -79,12 +79,10 @@ void ec_enc_init(ec_enc *_this,ec_byte_buffer *_buf){ } void ec_encode(ec_enc *_this,unsigned _fl,unsigned _fh,unsigned _ft){ - unsigned r; - unsigned s; + ec_uint32 r; r=_this->rng/_ft; if(_fl>0){ - s=r*(_ft-_fl); - _this->low+=_this->rng-s; + _this->low+=_this->rng-r*(_ft-_fl); _this->rng=r*(_fh-_fl); } else _this->rng-=r*(_ft-_fh); @@ -141,25 +139,26 @@ void ec_enc_done(ec_enc *_this){ } /*If we have a buffered byte...*/ if(_this->rem>=0){ - unsigned char *p; unsigned char *buf; + long i; /*Flush it into the output buffer.*/ ec_enc_carry_out(_this,0); _this->rem=-1; /*We may be able to drop some redundant bytes from the end.*/ buf=ec_byte_get_buffer(_this->buf); - p=buf+ec_byte_bytes(_this->buf)-1; + i=ec_byte_bytes(_this->buf); /*Strip trailing zeros.*/ - while(p>=buf&&!p[0])p--; + do i--; + while(i>0&&!buf[i]); /*Strip one trailing EC_FOF_RSV1 byte if the buffer ends in a string of consecutive EC_FOF_RSV1 bytes preceded by one (or more) zeros.*/ - if(p>buf&&p[0]==EC_FOF_RSV1){ - unsigned char *q; - q=p; - do q--; - while(q>buf&&q[0]==EC_FOF_RSV1); - if(!q[0])p--; + if(i>0&&buf[i]==EC_FOF_RSV1){ + long j; + j=i; + do j--; + while(j>0&&buf[j]==EC_FOF_RSV1); + if(!buf[j])i--; } - ec_byte_writetrunc(_this->buf,p+1-buf); + ec_byte_writetrunc(_this->buf,i+1); } }