int avio_read(AVIOContext *s, unsigned char *buf, int size)

{

    int len, size1;



    size1 = size;

    while (size > 0) {

        len = FFMIN(s->buf_end - s->buf_ptr, size);

        if (len == 0 || s->write_flag) {

            if((s->direct || size > s->buffer_size) && !s->update_checksum) {

                // bypass the buffer and read data directly into buf

                if(s->read_packet)

                    len = s->read_packet(s->opaque, buf, size);

                else

                    len = AVERROR_EOF;

                if (len == AVERROR_EOF) {

                    /* do not modify buffer if EOF reached so that a seek back can

                    be done without rereading data */

                    s->eof_reached = 1;

                    break;

                } else if (len < 0) {

                    s->eof_reached = 1;

                    s->error= len;

                    break;

                } else {

                    s->pos += len;

                    s->bytes_read += len;

                    size -= len;

                    buf += len;

                    // reset the buffer

                    s->buf_ptr = s->buffer;

                    s->buf_end = s->buffer/* + len*/;

                }

            } else {

                fill_buffer(s);

                len = s->buf_end - s->buf_ptr;

                if (len == 0)

                    break;

            }

        } else {

            memcpy(buf, s->buf_ptr, len);

            buf += len;

            s->buf_ptr += len;

            size -= len;

        }

    }

    if (size1 == size) {

        if (s->error)      return s->error;

        if (avio_feof(s))  return AVERROR_EOF;

    }

    return size1 - size;

}

int avio_read(AVIOContext *s, unsigned char *buf, int size)

{

    int len, size1;



    size1 = size;

    while (size > 0) {

        len = FFMIN(s->buf_end - s->buf_ptr, size);

        if (len == 0 || s->write_flag) {

            if((s->direct || size > s->buffer_size) && !s->update_checksum) {

                

                if(s->read_packet)

                    len = s->read_packet(s->opaque, buf, size);

                else

                    len = AVERROR_EOF;

                if (len == AVERROR_EOF) {

                    



                    s->eof_reached = 1;

                    break;

                } else if (len < 0) {

                    s->eof_reached = 1;

                    s->error= len;

                    break;

                } else {

                    s->pos += len;

                    s->bytes_read += len;

                    size -= len;

                    buf += len;

                    

                    s->buf_ptr = s->buffer;

                    s->buf_end = s->buffer;

                }

            } else {

                fill_buffer(s);

                len = s->buf_end - s->buf_ptr;

                if (len == 0)

                    break;

            }

        } else {

            memcpy(buf, s->buf_ptr, len);

            buf += len;

            s->buf_ptr += len;

            size -= len;

        }

    }

    if (size1 == size) {

        if (s->error)      return s->error;

        if (avio_feof(s))  return AVERROR_EOF;

    }

    return size1 - size;

}

int FUNC_0(AVIOContext *VAR_0, unsigned char *VAR_1, int VAR_2)

{

    int VAR_3, VAR_4;



    VAR_4 = VAR_2;

    while (VAR_2 > 0) {

        VAR_3 = FFMIN(VAR_0->buf_end - VAR_0->buf_ptr, VAR_2);

        if (VAR_3 == 0 || VAR_0->write_flag) {

            if((VAR_0->direct || VAR_2 > VAR_0->buffer_size) && !VAR_0->update_checksum) {

                

                if(VAR_0->read_packet)

                    VAR_3 = VAR_0->read_packet(VAR_0->opaque, VAR_1, VAR_2);

                else

                    VAR_3 = AVERROR_EOF;

                if (VAR_3 == AVERROR_EOF) {

                    



                    VAR_0->eof_reached = 1;

                    break;

                } else if (VAR_3 < 0) {

                    VAR_0->eof_reached = 1;

                    VAR_0->error= VAR_3;

                    break;

                } else {

                    VAR_0->pos += VAR_3;

                    VAR_0->bytes_read += VAR_3;

                    VAR_2 -= VAR_3;

                    VAR_1 += VAR_3;

                    

                    VAR_0->buf_ptr = VAR_0->buffer;

                    VAR_0->buf_end = VAR_0->buffer;

                }

            } else {

                fill_buffer(VAR_0);

                VAR_3 = VAR_0->buf_end - VAR_0->buf_ptr;

                if (VAR_3 == 0)

                    break;

            }

        } else {

            memcpy(VAR_1, VAR_0->buf_ptr, VAR_3);

            VAR_1 += VAR_3;

            VAR_0->buf_ptr += VAR_3;

            VAR_2 -= VAR_3;

        }

    }

    if (VAR_4 == VAR_2) {

        if (VAR_0->error)      return VAR_0->error;

        if (avio_feof(VAR_0))  return AVERROR_EOF;

    }

    return VAR_4 - VAR_2;

}

int FUNC_0(AVIOContext *VAR_0, unsigned char *VAR_1, int VAR_2)
{ int VAR_3, VAR_4; VAR_4 = VAR_2; while (VAR_2 > 0) { VAR_3 = FFMIN(VAR_0->buf_end - VAR_0->buf_ptr, VAR_2); if (VAR_3 == 0 || VAR_0->write_flag) { if((VAR_0->direct || VAR_2 > VAR_0->buffer_size) && !VAR_0->update_checksum) { if(VAR_0->read_packet)
VAR_3 = VAR_0->read_packet(VAR_0->opaque, VAR_1, VAR_2); else
VAR_3 = AVERROR_EOF; if (VAR_3 == AVERROR_EOF) { VAR_0->eof_reached = 1; break; } else if (VAR_3 < 0) { VAR_0->eof_reached = 1; VAR_0->error= VAR_3; break; } else { VAR_0->pos += VAR_3; VAR_0->bytes_read += VAR_3; VAR_2 -= VAR_3; VAR_1 += VAR_3; VAR_0->buf_ptr = VAR_0->buffer; VAR_0->buf_end = VAR_0->buffer; } } else { fill_buffer(VAR_0); VAR_3 = VAR_0->buf_end - VAR_0->buf_ptr; if (VAR_3 == 0)
break; } } else { memcpy(VAR_1, VAR_0->buf_ptr, VAR_3); VAR_1 += VAR_3; VAR_0->buf_ptr += VAR_3; VAR_2 -= VAR_3; } } if (VAR_4 == VAR_2) { if (VAR_0->error)      return VAR_0->error; if (avio_feof(VAR_0))  return AVERROR_EOF; } return VAR_4 - VAR_2; }