Fix the bug that was causing the last byte of the stream to be returned incorrectly, as well as undo jm's reversion, which can cause ec_dec_tell() to operate incorrectly at the end of the stream.

A few other minor updates are included as well.

git-svn-id: http://svn.xiph.org/trunk/ghost@14427 0101bb08-14d6-0310-b084-bc0e0c8e3800
This commit is contained in:
tterribe 2008-01-23 23:04:43 +00:00 committed by Jean-Marc Valin
parent 77ff83a28c
commit 7e3293f713
5 changed files with 85 additions and 55 deletions

View file

@ -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);
}
}