Changeset 266


Ignore:
Timestamp:
04/15/07 23:40:45 (17 years ago)
Author:
r2d
Message:

Improved decoder robustness against corrupted files

Location:
libmpc/branches/r2d/libmpcdec
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libmpc/branches/r2d/libmpcdec/internal.h

    r206 r266  
    6262        unsigned int count; /// unread bits in current byte
    6363};
    64 
    65 #define DEMUX_BUFFER_SIZE 65536
     64       
     65#define MAX_FRAME_SIZE 4352
     66#define DEMUX_BUFFER_SIZE (65536 - MAX_FRAME_SIZE) // need some space as sand box
    6667
    6768struct mpc_demux_t {
     
    7172
    7273        // buffer
    73         mpc_uint8_t buffer[DEMUX_BUFFER_SIZE];
     74        mpc_uint8_t buffer[DEMUX_BUFFER_SIZE + MAX_FRAME_SIZE];
    7475        mpc_size_t bytes_total;
    7576        mpc_bits_reader bits_reader;
  • libmpc/branches/r2d/libmpcdec/mpc_demux.c

    r262 r266  
    5050void  streaminfo_gain(mpc_streaminfo* si, const mpc_bits_reader * r_in);
    5151
    52 #define MAX_FRAME_SIZE 4352
    5352
    5453enum {
     
    300299                mpc_demux_fill(d, 11, 0); // max header block size
    301300                size = mpc_bits_get_block(&d->bits_reader, &b);
     301                // FIXME : stop scan if invalid key
    302302                while( memcmp(b.key, "AP", 2) != 0 ){ // scan all blocks until audio
    303303                        mpc_demux_fill(d, 11 + (mpc_uint32_t) b.size, 0);
     
    317317                }
    318318                d->bits_reader.buff -= size;
     319                if (d->si.stream_version == 0) // si no initialized !!!
     320                        return MPC_STATUS_INVALIDSV;
    319321        } else
    320322                return MPC_STATUS_INVALIDSV;
     
    335337                        p_tmp->d = mpc_decoder_init(&p_tmp->si);
    336338                } else {
     339                        if (p_tmp->seek_table)
     340                                free(p_tmp->seek_table);
    337341                        free(p_tmp);
    338342                        p_tmp = 0;
     
    357361void mpc_demux_decode(mpc_demux * d, mpc_frame_info * i)
    358362{
     363        mpc_bits_reader r;
    359364        if (d->si.stream_version >= 8) {
    360                 mpc_bits_reader r;
    361365                i->is_key_frame = MPC_FALSE;
    362366
     
    389393                d->block_bits -= ((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count;
    390394                d->block_frames--;
    391                 if (d->block_bits < 0) i->bits = -1;
     395                if (d->block_bits < 0 || (d->block_frames == 0 && d->block_bits > 7)) {
     396                        // an error occured, stop decoding
     397                        // FIXME : return an error code.
     398                        i->bits = -1; // we pretend it's end of file
     399                }
    392400        } else {
    393                 mpc_bits_reader r;
    394401                if (d->d->decoded_samples == (d->seek_table_size << d->seek_pwr) * MPC_FRAME_LENGTH) {
    395402                        d->seek_table[d->seek_table_size] = (mpc_uint32_t) mpc_demux_pos(d);
     
    397404                }
    398405                mpc_demux_fill(d, MAX_FRAME_SIZE, MPC_BUFFER_FULL | MPC_BUFFER_SWAP);
    399                 mpc_bits_read(&d->bits_reader, 20); // read frame size
     406                d->block_bits = (mpc_int_t) mpc_bits_read(&d->bits_reader, 20); // read frame size
    400407                r = d->bits_reader;
    401408                mpc_decoder_decode_frame(d->d, &d->bits_reader, i);
     409                if (d->block_bits != ((d->bits_reader.buff - r.buff) << 3) + r.count - d->bits_reader.count) {
     410                        // an error occured, stop decoding
     411                        // FIXME : return an error code.
     412                        i->bits = -1; // we pretend it's end of file
     413                }
     414        }
     415        if (d->buffer + d->bytes_total < d->bits_reader.buff + ((8 - d->bits_reader.count) >> 3)) {
     416                // we're reading outside the buffer bytes, this is an error !
     417                // FIXME : return an error code.
     418                i->bits = -1; // we pretend it's end of file
    402419        }
    403420}
Note: See TracChangeset for help on using the changeset viewer.