static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)

{

    int size;

    int ret;



    MPJPEGDemuxContext *mpjpeg = s->priv_data;

    if (mpjpeg->boundary == NULL) {

        mpjpeg->boundary = av_strdup("--");

        mpjpeg->searchstr = av_strdup("\r\n--");

        if (!mpjpeg->boundary || !mpjpeg->searchstr) {

            av_freep(&mpjpeg->boundary);

            av_freep(&mpjpeg->searchstr);

            return AVERROR(ENOMEM);

        }

        mpjpeg->searchstr_len = strlen(mpjpeg->searchstr);

    }



    ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s);





    if (ret < 0)

        return ret;



    if (size > 0) {

        /* size has been provided to us in MIME header */

        ret = av_get_packet(s->pb, pkt, size);

    } else {

        /* no size was given -- we read until the next boundary or end-of-file */

        int remaining = 0, len;



        const int read_chunk = 2048;

        av_init_packet(pkt);

        pkt->data = NULL;

        pkt->size = 0;

        pkt->pos  = avio_tell(s->pb);



        /* we may need to return as much as all we've read back to the buffer */

        ffio_ensure_seekback(s->pb, read_chunk);



        while ((ret = av_append_packet(s->pb, pkt, read_chunk - remaining)) >= 0) {

            /* scan the new data */

            len = ret + remaining;

            char *start = pkt->data + pkt->size - len;

            do {

                if (!memcmp(start, mpjpeg->searchstr, mpjpeg->searchstr_len)) {

                    // got the boundary! rewind the stream

                    avio_seek(s->pb, -(len-2), SEEK_CUR);

                    pkt->size -= (len-2);

                    return pkt->size;

                }

                len--;

                start++;

            } while (len >= mpjpeg->searchstr_len);

            remaining = len;

        }



        /* error or EOF occurred */

        if (ret == AVERROR_EOF) {

            ret = pkt->size > 0 ? pkt->size : AVERROR_EOF;

        } else {

            av_packet_unref(pkt);

        }

    }



    return ret;

}

static int mpjpeg_read_packet(AVFormatContext *s, AVPacket *pkt)

{

    int size;

    int ret;



    MPJPEGDemuxContext *mpjpeg = s->priv_data;

    if (mpjpeg->boundary == NULL) {

        mpjpeg->boundary = av_strdup("--");

        mpjpeg->searchstr = av_strdup("\r\n--");

        if (!mpjpeg->boundary || !mpjpeg->searchstr) {

            av_freep(&mpjpeg->boundary);

            av_freep(&mpjpeg->searchstr);

            return AVERROR(ENOMEM);

        }

        mpjpeg->searchstr_len = strlen(mpjpeg->searchstr);

    }



    ret = parse_multipart_header(s->pb, &size, mpjpeg->boundary, s);





    if (ret < 0)

        return ret;



    if (size > 0) {

        

        ret = av_get_packet(s->pb, pkt, size);

    } else {

        

        int remaining = 0, len;



        const int read_chunk = 2048;

        av_init_packet(pkt);

        pkt->data = NULL;

        pkt->size = 0;

        pkt->pos  = avio_tell(s->pb);



        

        ffio_ensure_seekback(s->pb, read_chunk);



        while ((ret = av_append_packet(s->pb, pkt, read_chunk - remaining)) >= 0) {

            

            len = ret + remaining;

            char *start = pkt->data + pkt->size - len;

            do {

                if (!memcmp(start, mpjpeg->searchstr, mpjpeg->searchstr_len)) {

                    

                    avio_seek(s->pb, -(len-2), SEEK_CUR);

                    pkt->size -= (len-2);

                    return pkt->size;

                }

                len--;

                start++;

            } while (len >= mpjpeg->searchstr_len);

            remaining = len;

        }



        

        if (ret == AVERROR_EOF) {

            ret = pkt->size > 0 ? pkt->size : AVERROR_EOF;

        } else {

            av_packet_unref(pkt);

        }

    }



    return ret;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)

{

    int VAR_2;

    int VAR_3;



    MPJPEGDemuxContext *mpjpeg = VAR_0->priv_data;

    if (mpjpeg->boundary == NULL) {

        mpjpeg->boundary = av_strdup("--");

        mpjpeg->searchstr = av_strdup("\r\n--");

        if (!mpjpeg->boundary || !mpjpeg->searchstr) {

            av_freep(&mpjpeg->boundary);

            av_freep(&mpjpeg->searchstr);

            return AVERROR(ENOMEM);

        }

        mpjpeg->searchstr_len = strlen(mpjpeg->searchstr);

    }



    VAR_3 = parse_multipart_header(VAR_0->pb, &VAR_2, mpjpeg->boundary, VAR_0);





    if (VAR_3 < 0)

        return VAR_3;



    if (VAR_2 > 0) {

        

        VAR_3 = av_get_packet(VAR_0->pb, VAR_1, VAR_2);

    } else {

        

        int VAR_4 = 0, VAR_5;



        const int VAR_6 = 2048;

        av_init_packet(VAR_1);

        VAR_1->data = NULL;

        VAR_1->VAR_2 = 0;

        VAR_1->pos  = avio_tell(VAR_0->pb);



        

        ffio_ensure_seekback(VAR_0->pb, VAR_6);



        while ((VAR_3 = av_append_packet(VAR_0->pb, VAR_1, VAR_6 - VAR_4)) >= 0) {

            

            VAR_5 = VAR_3 + VAR_4;

            char *VAR_7 = VAR_1->data + VAR_1->VAR_2 - VAR_5;

            do {

                if (!memcmp(VAR_7, mpjpeg->searchstr, mpjpeg->searchstr_len)) {

                    

                    avio_seek(VAR_0->pb, -(VAR_5-2), SEEK_CUR);

                    VAR_1->VAR_2 -= (VAR_5-2);

                    return VAR_1->VAR_2;

                }

                VAR_5--;

                VAR_7++;

            } while (VAR_5 >= mpjpeg->searchstr_len);

            VAR_4 = VAR_5;

        }



        

        if (VAR_3 == AVERROR_EOF) {

            VAR_3 = VAR_1->VAR_2 > 0 ? VAR_1->VAR_2 : AVERROR_EOF;

        } else {

            av_packet_unref(VAR_1);

        }

    }



    return VAR_3;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1)
{ int VAR_2; int VAR_3; MPJPEGDemuxContext *mpjpeg = VAR_0->priv_data; if (mpjpeg->boundary == NULL) { mpjpeg->boundary = av_strdup("--"); mpjpeg->searchstr = av_strdup("\r\n--"); if (!mpjpeg->boundary || !mpjpeg->searchstr) { av_freep(&mpjpeg->boundary); av_freep(&mpjpeg->searchstr); return AVERROR(ENOMEM); } mpjpeg->searchstr_len = strlen(mpjpeg->searchstr); } VAR_3 = parse_multipart_header(VAR_0->pb, &VAR_2, mpjpeg->boundary, VAR_0); if (VAR_3 < 0)
return VAR_3; if (VAR_2 > 0) { VAR_3 = av_get_packet(VAR_0->pb, VAR_1, VAR_2); } else { int VAR_4 = 0, VAR_5; const int VAR_6 = 2048; av_init_packet(VAR_1); VAR_1->data = NULL; VAR_1->VAR_2 = 0; VAR_1->pos  = avio_tell(VAR_0->pb); ffio_ensure_seekback(VAR_0->pb, VAR_6); while ((VAR_3 = av_append_packet(VAR_0->pb, VAR_1, VAR_6 - VAR_4)) >= 0) { VAR_5 = VAR_3 + VAR_4; char *VAR_7 = VAR_1->data + VAR_1->VAR_2 - VAR_5; do { if (!memcmp(VAR_7, mpjpeg->searchstr, mpjpeg->searchstr_len)) { avio_seek(VAR_0->pb, -(VAR_5-2), SEEK_CUR); VAR_1->VAR_2 -= (VAR_5-2); return VAR_1->VAR_2; } VAR_5--; VAR_7++; } while (VAR_5 >= mpjpeg->searchstr_len); VAR_4 = VAR_5; } if (VAR_3 == AVERROR_EOF) { VAR_3 = VAR_1->VAR_2 > 0 ? VAR_1->VAR_2 : AVERROR_EOF; } else { av_packet_unref(VAR_1); } } return VAR_3; }