static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)

{

    const uint8_t *frame_end   = frame + width * height;

    uint8_t *line_ptr;

    int count, i, v, lines, segments;



    lines = bytestream2_get_le16(gb);

    if (lines > height)

        return -1;



    while (lines--) {

        if (bytestream2_get_bytes_left(gb) < 2)

            return -1;

        segments = bytestream2_get_le16u(gb);

        while ((segments & 0xC000) == 0xC000) {

            unsigned delta = -((int16_t)segments * width);

            if (frame_end - frame <= delta)

                return -1;

            frame    += delta;

            segments = bytestream2_get_le16(gb);

        }

        if (segments & 0x8000) {

            frame[width - 1] = segments & 0xFF;

            segments = bytestream2_get_le16(gb);

        }

        line_ptr = frame;

        frame += width;

        while (segments--) {

            if (frame - line_ptr <= bytestream2_peek_byte(gb))

                return -1;

            line_ptr += bytestream2_get_byte(gb);

            count = (int8_t)bytestream2_get_byte(gb);

            if (count >= 0) {

                if (frame - line_ptr < count * 2)

                    return -1;

                if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)

                    return -1;

                line_ptr += count * 2;

            } else {

                count = -count;

                if (frame - line_ptr < count * 2)

                    return -1;

                v = bytestream2_get_le16(gb);

                for (i = 0; i < count; i++)

                    bytestream_put_le16(&line_ptr, v);

            }

        }

    }



    return 0;

}

static int decode_wdlt(GetByteContext *gb, uint8_t *frame, int width, int height)

{

    const uint8_t *frame_end   = frame + width * height;

    uint8_t *line_ptr;

    int count, i, v, lines, segments;



    lines = bytestream2_get_le16(gb);

    if (lines > height)

        return -1;



    while (lines--) {

        if (bytestream2_get_bytes_left(gb) < 2)

            return -1;

        segments = bytestream2_get_le16u(gb);

        while ((segments & 0xC000) == 0xC000) {

            unsigned delta = -((int16_t)segments * width);

            if (frame_end - frame <= delta)

                return -1;

            frame    += delta;

            segments = bytestream2_get_le16(gb);

        }

        if (segments & 0x8000) {

            frame[width - 1] = segments & 0xFF;

            segments = bytestream2_get_le16(gb);

        }

        line_ptr = frame;

        frame += width;

        while (segments--) {

            if (frame - line_ptr <= bytestream2_peek_byte(gb))

                return -1;

            line_ptr += bytestream2_get_byte(gb);

            count = (int8_t)bytestream2_get_byte(gb);

            if (count >= 0) {

                if (frame - line_ptr < count * 2)

                    return -1;

                if (bytestream2_get_buffer(gb, line_ptr, count * 2) != count * 2)

                    return -1;

                line_ptr += count * 2;

            } else {

                count = -count;

                if (frame - line_ptr < count * 2)

                    return -1;

                v = bytestream2_get_le16(gb);

                for (i = 0; i < count; i++)

                    bytestream_put_le16(&line_ptr, v);

            }

        }

    }



    return 0;

}

static int FUNC_0(GetByteContext *VAR_0, uint8_t *VAR_1, int VAR_2, int VAR_3)

{

    const uint8_t *VAR_4   = VAR_1 + VAR_2 * VAR_3;

    uint8_t *line_ptr;

    int VAR_5, VAR_6, VAR_7, VAR_8, VAR_9;



    VAR_8 = bytestream2_get_le16(VAR_0);

    if (VAR_8 > VAR_3)

        return -1;



    while (VAR_8--) {

        if (bytestream2_get_bytes_left(VAR_0) < 2)

            return -1;

        VAR_9 = bytestream2_get_le16u(VAR_0);

        while ((VAR_9 & 0xC000) == 0xC000) {

            unsigned VAR_10 = -((int16_t)VAR_9 * VAR_2);

            if (VAR_4 - VAR_1 <= VAR_10)

                return -1;

            VAR_1    += VAR_10;

            VAR_9 = bytestream2_get_le16(VAR_0);

        }

        if (VAR_9 & 0x8000) {

            VAR_1[VAR_2 - 1] = VAR_9 & 0xFF;

            VAR_9 = bytestream2_get_le16(VAR_0);

        }

        line_ptr = VAR_1;

        VAR_1 += VAR_2;

        while (VAR_9--) {

            if (VAR_1 - line_ptr <= bytestream2_peek_byte(VAR_0))

                return -1;

            line_ptr += bytestream2_get_byte(VAR_0);

            VAR_5 = (int8_t)bytestream2_get_byte(VAR_0);

            if (VAR_5 >= 0) {

                if (VAR_1 - line_ptr < VAR_5 * 2)

                    return -1;

                if (bytestream2_get_buffer(VAR_0, line_ptr, VAR_5 * 2) != VAR_5 * 2)

                    return -1;

                line_ptr += VAR_5 * 2;

            } else {

                VAR_5 = -VAR_5;

                if (VAR_1 - line_ptr < VAR_5 * 2)

                    return -1;

                VAR_7 = bytestream2_get_le16(VAR_0);

                for (VAR_6 = 0; VAR_6 < VAR_5; VAR_6++)

                    bytestream_put_le16(&line_ptr, VAR_7);

            }

        }

    }



    return 0;

}

static int FUNC_0(GetByteContext *VAR_0, uint8_t *VAR_1, int VAR_2, int VAR_3)
{ const uint8_t *VAR_4   = VAR_1 + VAR_2 * VAR_3; uint8_t *line_ptr; int VAR_5, VAR_6, VAR_7, VAR_8, VAR_9; VAR_8 = bytestream2_get_le16(VAR_0); if (VAR_8 > VAR_3)
return -1; while (VAR_8--) { if (bytestream2_get_bytes_left(VAR_0) < 2)
return -1; VAR_9 = bytestream2_get_le16u(VAR_0); while ((VAR_9 & 0xC000) == 0xC000) { unsigned VAR_10 = -((int16_t)VAR_9 * VAR_2); if (VAR_4 - VAR_1 <= VAR_10)
return -1; VAR_1    += VAR_10; VAR_9 = bytestream2_get_le16(VAR_0); } if (VAR_9 & 0x8000) { VAR_1[VAR_2 - 1] = VAR_9 & 0xFF; VAR_9 = bytestream2_get_le16(VAR_0); } line_ptr = VAR_1; VAR_1 += VAR_2; while (VAR_9--) { if (VAR_1 - line_ptr <= bytestream2_peek_byte(VAR_0))
return -1; line_ptr += bytestream2_get_byte(VAR_0); VAR_5 = (int8_t)bytestream2_get_byte(VAR_0); if (VAR_5 >= 0) { if (VAR_1 - line_ptr < VAR_5 * 2)
return -1; if (bytestream2_get_buffer(VAR_0, line_ptr, VAR_5 * 2) != VAR_5 * 2)
return -1; line_ptr += VAR_5 * 2; } else { VAR_5 = -VAR_5; if (VAR_1 - line_ptr < VAR_5 * 2)
return -1; VAR_7 = bytestream2_get_le16(VAR_0); for (VAR_6 = 0; VAR_6 < VAR_5; VAR_6++) bytestream_put_le16(&line_ptr, VAR_7); } } } return 0; }