static int expand_rle_row16(SgiState *s, uint16_t *out_buf,

                            int len, int pixelstride)

{

    unsigned short pixel;

    unsigned char count;

    unsigned short *orig = out_buf;

    uint16_t *out_end = out_buf + len;



    while (out_buf < out_end) {

        if (bytestream2_get_bytes_left(&s->g) < 2)

            return AVERROR_INVALIDDATA;

        pixel = bytestream2_get_be16u(&s->g);

        if (!(count = (pixel & 0x7f)))

            break;



        /* Check for buffer overflow. */

        if (pixelstride * (count - 1) >= len) {

            av_log(s->avctx, AV_LOG_ERROR, "Invalid pixel count.\n");

            return AVERROR_INVALIDDATA;

        }



        if (pixel & 0x80) {

            while (count--) {

                pixel = bytestream2_get_ne16(&s->g);

                AV_WN16A(out_buf, pixel);

                out_buf += pixelstride;

            }

        } else {

            pixel = bytestream2_get_ne16(&s->g);



            while (count--) {

                AV_WN16A(out_buf, pixel);

                out_buf += pixelstride;

            }

        }

    }

    return (out_buf - orig) / pixelstride;

}

static int expand_rle_row16(SgiState *s, uint16_t *out_buf,

                            int len, int pixelstride)

{

    unsigned short pixel;

    unsigned char count;

    unsigned short *orig = out_buf;

    uint16_t *out_end = out_buf + len;



    while (out_buf < out_end) {

        if (bytestream2_get_bytes_left(&s->g) < 2)

            return AVERROR_INVALIDDATA;

        pixel = bytestream2_get_be16u(&s->g);

        if (!(count = (pixel & 0x7f)))

            break;



        

        if (pixelstride * (count - 1) >= len) {

            av_log(s->avctx, AV_LOG_ERROR, "Invalid pixel count.\n");

            return AVERROR_INVALIDDATA;

        }



        if (pixel & 0x80) {

            while (count--) {

                pixel = bytestream2_get_ne16(&s->g);

                AV_WN16A(out_buf, pixel);

                out_buf += pixelstride;

            }

        } else {

            pixel = bytestream2_get_ne16(&s->g);



            while (count--) {

                AV_WN16A(out_buf, pixel);

                out_buf += pixelstride;

            }

        }

    }

    return (out_buf - orig) / pixelstride;

}

static int FUNC_0(SgiState *VAR_0, uint16_t *VAR_1,

                            int VAR_2, int VAR_3)

{

    unsigned short VAR_4;

    unsigned char VAR_5;

    unsigned short *VAR_6 = VAR_1;

    uint16_t *out_end = VAR_1 + VAR_2;



    while (VAR_1 < out_end) {

        if (bytestream2_get_bytes_left(&VAR_0->g) < 2)

            return AVERROR_INVALIDDATA;

        VAR_4 = bytestream2_get_be16u(&VAR_0->g);

        if (!(VAR_5 = (VAR_4 & 0x7f)))

            break;



        

        if (VAR_3 * (VAR_5 - 1) >= VAR_2) {

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid VAR_4 VAR_5.\n");

            return AVERROR_INVALIDDATA;

        }



        if (VAR_4 & 0x80) {

            while (VAR_5--) {

                VAR_4 = bytestream2_get_ne16(&VAR_0->g);

                AV_WN16A(VAR_1, VAR_4);

                VAR_1 += VAR_3;

            }

        } else {

            VAR_4 = bytestream2_get_ne16(&VAR_0->g);



            while (VAR_5--) {

                AV_WN16A(VAR_1, VAR_4);

                VAR_1 += VAR_3;

            }

        }

    }

    return (VAR_1 - VAR_6) / VAR_3;

}

static int FUNC_0(SgiState *VAR_0, uint16_t *VAR_1,
int VAR_2, int VAR_3)
{ unsigned short VAR_4; unsigned char VAR_5; unsigned short *VAR_6 = VAR_1; uint16_t *out_end = VAR_1 + VAR_2; while (VAR_1 < out_end) { if (bytestream2_get_bytes_left(&VAR_0->g) < 2)
return AVERROR_INVALIDDATA; VAR_4 = bytestream2_get_be16u(&VAR_0->g); if (!(VAR_5 = (VAR_4 & 0x7f)))
break; if (VAR_3 * (VAR_5 - 1) >= VAR_2) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid VAR_4 VAR_5.\n"); return AVERROR_INVALIDDATA; } if (VAR_4 & 0x80) { while (VAR_5--) { VAR_4 = bytestream2_get_ne16(&VAR_0->g); AV_WN16A(VAR_1, VAR_4); VAR_1 += VAR_3; } } else { VAR_4 = bytestream2_get_ne16(&VAR_0->g); while (VAR_5--) { AV_WN16A(VAR_1, VAR_4); VAR_1 += VAR_3; } } } return (VAR_1 - VAR_6) / VAR_3; }