static int decode_dsw1(uint8_t *frame, int width, int height,

                       const uint8_t *src, const uint8_t *src_end)

{

    const uint8_t *frame_start = frame;

    const uint8_t *frame_end   = frame + width * height;

    int mask = 0x10000, bitbuf = 0;

    int v, offset, count, segments;



    segments = bytestream_get_le16(&src);

    while (segments--) {

        if (mask == 0x10000) {

            if (src >= src_end)

                return -1;

            bitbuf = bytestream_get_le16(&src);

            mask = 1;

        }

        if (src_end - src < 2 || frame_end - frame < 2)

            return -1;

        if (bitbuf & mask) {

            v = bytestream_get_le16(&src);

            offset = (v & 0x1FFF) << 1;

            count = ((v >> 13) + 2) << 1;

            if (frame - offset < frame_start || frame_end - frame < count)

                return -1;

            // can't use av_memcpy_backptr() since it can overwrite following pixels

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

                frame[v] = frame[v - offset];

            frame += count;

        } else if (bitbuf & (mask << 1)) {

            frame += bytestream_get_le16(&src);

        } else {

            *frame++ = *src++;

            *frame++ = *src++;

        }

        mask <<= 2;

    }



    return 0;

}

static int decode_dsw1(uint8_t *frame, int width, int height,

                       const uint8_t *src, const uint8_t *src_end)

{

    const uint8_t *frame_start = frame;

    const uint8_t *frame_end   = frame + width * height;

    int mask = 0x10000, bitbuf = 0;

    int v, offset, count, segments;



    segments = bytestream_get_le16(&src);

    while (segments--) {

        if (mask == 0x10000) {

            if (src >= src_end)

                return -1;

            bitbuf = bytestream_get_le16(&src);

            mask = 1;

        }

        if (src_end - src < 2 || frame_end - frame < 2)

            return -1;

        if (bitbuf & mask) {

            v = bytestream_get_le16(&src);

            offset = (v & 0x1FFF) << 1;

            count = ((v >> 13) + 2) << 1;

            if (frame - offset < frame_start || frame_end - frame < count)

                return -1;

            

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

                frame[v] = frame[v - offset];

            frame += count;

        } else if (bitbuf & (mask << 1)) {

            frame += bytestream_get_le16(&src);

        } else {

            *frame++ = *src++;

            *frame++ = *src++;

        }

        mask <<= 2;

    }



    return 0;

}

            if (frame - offset < frame_start || frame_end - frame < count)             if (frame - offset < frame_start || frame_end - frame < count)
static int FUNC_0(uint8_t *VAR_0, int VAR_1, int VAR_2,

                       const uint8_t *VAR_3, const uint8_t *VAR_4)

{

    const uint8_t *VAR_5 = VAR_0;

    const uint8_t *VAR_6   = VAR_0 + VAR_1 * VAR_2;

    int VAR_7 = 0x10000, VAR_8 = 0;

    int VAR_9, VAR_10, VAR_11, VAR_12;



    VAR_12 = bytestream_get_le16(&VAR_3);

    while (VAR_12--) {

        if (VAR_7 == 0x10000) {

            if (VAR_3 >= VAR_4)

                return -1;

            VAR_8 = bytestream_get_le16(&VAR_3);

            VAR_7 = 1;

        }

        if (VAR_4 - VAR_3 < 2 || VAR_6 - VAR_0 < 2)

            return -1;

        if (VAR_8 & VAR_7) {

            VAR_9 = bytestream_get_le16(&VAR_3);

            VAR_10 = (VAR_9 & 0x1FFF) << 1;

            VAR_11 = ((VAR_9 >> 13) + 2) << 1;

            if (VAR_0 - VAR_10 < VAR_5 || VAR_6 - VAR_0 < VAR_11)

                return -1;

            

            for (VAR_9 = 0; VAR_9 < VAR_11; VAR_9++)

                VAR_0[VAR_9] = VAR_0[VAR_9 - VAR_10];

            VAR_0 += VAR_11;

        } else if (VAR_8 & (VAR_7 << 1)) {

            VAR_0 += bytestream_get_le16(&VAR_3);

        } else {

            *VAR_0++ = *VAR_3++;

            *VAR_0++ = *VAR_3++;

        }

        VAR_7 <<= 2;

    }



    return 0;

}

static int FUNC_0(uint8_t *VAR_0, int VAR_1, int VAR_2,
const uint8_t *VAR_3, const uint8_t *VAR_4)
{ const uint8_t *VAR_5 = VAR_0; const uint8_t *VAR_6   = VAR_0 + VAR_1 * VAR_2; int VAR_7 = 0x10000, VAR_8 = 0; int VAR_9, VAR_10, VAR_11, VAR_12; VAR_12 = bytestream_get_le16(&VAR_3); while (VAR_12--) { if (VAR_7 == 0x10000) { if (VAR_3 >= VAR_4)
return -1; VAR_8 = bytestream_get_le16(&VAR_3); VAR_7 = 1; } if (VAR_4 - VAR_3 < 2 || VAR_6 - VAR_0 < 2)
return -1; if (VAR_8 & VAR_7) { VAR_9 = bytestream_get_le16(&VAR_3); VAR_10 = (VAR_9 & 0x1FFF) << 1; VAR_11 = ((VAR_9 >> 13) + 2) << 1; if (VAR_0 - VAR_10 < VAR_5 || VAR_6 - VAR_0 < VAR_11)
return -1; for (VAR_9 = 0; VAR_9 < VAR_11; VAR_9++) VAR_0[VAR_9] = VAR_0[VAR_9 - VAR_10]; VAR_0 += VAR_11; } else if (VAR_8 & (VAR_7 << 1)) { VAR_0 += bytestream_get_le16(&VAR_3); } else { *VAR_0++ = *VAR_3++; *VAR_0++ = *VAR_3++; } VAR_7 <<= 2; } return 0; }