int vncws_decode_frame(Buffer *input, uint8_t **payload,

                           size_t *payload_size, size_t *frame_size)

{

    unsigned char opcode = 0, fin = 0, has_mask = 0;

    size_t header_size = 0;

    uint32_t *payload32;

    WsHeader *header = (WsHeader *)input->buffer;

    WsMask mask;

    int i;



    if (input->offset < WS_HEAD_MIN_LEN + 4) {

        /* header not complete */

        return 0;

    }



    fin = (header->b0 & 0x80) >> 7;

    opcode = header->b0 & 0x0f;

    has_mask = (header->b1 & 0x80) >> 7;

    *payload_size = header->b1 & 0x7f;



    if (opcode == WS_OPCODE_CLOSE) {

        /* disconnect */

        return -1;

    }



    /* Websocket frame sanity check:

     * * Websocket fragmentation is not supported.

     * * All  websockets frames sent by a client have to be masked.

     * * Only binary encoding is supported.

     */

    if (!fin || !has_mask || opcode != WS_OPCODE_BINARY_FRAME) {

        VNC_DEBUG("Received faulty/unsupported Websocket frame\n");

        return -2;

    }



    if (*payload_size < 126) {

        header_size = 6;

        mask = header->u.m;

    } else if (*payload_size == 126 && input->offset >= 8) {

        *payload_size = be16_to_cpu(header->u.s16.l16);

        header_size = 8;

        mask = header->u.s16.m16;

    } else if (*payload_size == 127 && input->offset >= 14) {

        *payload_size = be64_to_cpu(header->u.s64.l64);

        header_size = 14;

        mask = header->u.s64.m64;

    } else {

        /* header not complete */

        return 0;

    }



    *frame_size = header_size + *payload_size;



    if (input->offset < *frame_size) {

        /* frame not complete */

        return 0;

    }



    *payload = input->buffer + header_size;



    /* unmask frame */

    /* process 1 frame (32 bit op) */

    payload32 = (uint32_t *)(*payload);

    for (i = 0; i < *payload_size / 4; i++) {

        payload32[i] ^= mask.u;

    }

    /* process the remaining bytes (if any) */

    for (i *= 4; i < *payload_size; i++) {

        (*payload)[i] ^= mask.c[i % 4];

    }



    return 1;

}

int vncws_decode_frame(Buffer *input, uint8_t **payload,

                           size_t *payload_size, size_t *frame_size)

{

    unsigned char opcode = 0, fin = 0, has_mask = 0;

    size_t header_size = 0;

    uint32_t *payload32;

    WsHeader *header = (WsHeader *)input->buffer;

    WsMask mask;

    int i;



    if (input->offset < WS_HEAD_MIN_LEN + 4) {

        

        return 0;

    }



    fin = (header->b0 & 0x80) >> 7;

    opcode = header->b0 & 0x0f;

    has_mask = (header->b1 & 0x80) >> 7;

    *payload_size = header->b1 & 0x7f;



    if (opcode == WS_OPCODE_CLOSE) {

        

        return -1;

    }



    









    if (!fin || !has_mask || opcode != WS_OPCODE_BINARY_FRAME) {

        VNC_DEBUG("Received faulty/unsupported Websocket frame\n");

        return -2;

    }



    if (*payload_size < 126) {

        header_size = 6;

        mask = header->u.m;

    } else if (*payload_size == 126 && input->offset >= 8) {

        *payload_size = be16_to_cpu(header->u.s16.l16);

        header_size = 8;

        mask = header->u.s16.m16;

    } else if (*payload_size == 127 && input->offset >= 14) {

        *payload_size = be64_to_cpu(header->u.s64.l64);

        header_size = 14;

        mask = header->u.s64.m64;

    } else {

        

        return 0;

    }



    *frame_size = header_size + *payload_size;



    if (input->offset < *frame_size) {

        

        return 0;

    }



    *payload = input->buffer + header_size;



    

    

    payload32 = (uint32_t *)(*payload);

    for (i = 0; i < *payload_size / 4; i++) {

        payload32[i] ^= mask.u;

    }

    

    for (i *= 4; i < *payload_size; i++) {

        (*payload)[i] ^= mask.c[i % 4];

    }



    return 1;

}

int vncws_decode_frame(Buffer *input, uint8_t **payload,                            size_t *payload_size, size_t *frame_size)     size_t header_size = 0;     uint32_t *payload32;     WsMask mask;     int i;     *payload_size = header->b1 & 0x7f;     if (*payload_size < 126) {         header_size = 6;         mask = header->u.m;     } else if (*payload_size == 126 && input->offset >= 8) {         *payload_size = be16_to_cpu(header->u.s16.l16);         header_size = 8;         mask = header->u.s16.m16;     } else if (*payload_size == 127 && input->offset >= 14) {         *payload_size = be64_to_cpu(header->u.s64.l64);         header_size = 14;         mask = header->u.s64.m64;     *frame_size = header_size + *payload_size;     if (input->offset < *frame_size) {     *payload = input->buffer + header_size;         payload32[i] ^= mask.u;         (*payload)[i] ^= mask.c[i % 4]; int vncws_decode_frame(Buffer *input, uint8_t **payload,
int FUNC_0(Buffer *VAR_0, uint8_t **VAR_1,

                           size_t *VAR_2, size_t *VAR_3)

{

    unsigned char VAR_4 = 0, VAR_5 = 0, VAR_6 = 0;

    size_t header_size = 0;

    uint32_t *payload32;

    WsHeader *header = (WsHeader *)VAR_0->buffer;

    WsMask mask;

    int VAR_7;



    if (VAR_0->offset < WS_HEAD_MIN_LEN + 4) {

        

        return 0;

    }



    VAR_5 = (header->b0 & 0x80) >> 7;

    VAR_4 = header->b0 & 0x0f;

    VAR_6 = (header->b1 & 0x80) >> 7;

    *VAR_2 = header->b1 & 0x7f;



    if (VAR_4 == WS_OPCODE_CLOSE) {

        

        return -1;

    }



    









    if (!VAR_5 || !VAR_6 || VAR_4 != WS_OPCODE_BINARY_FRAME) {

        VNC_DEBUG("Received faulty/unsupported Websocket frame\n");

        return -2;

    }



    if (*VAR_2 < 126) {

        header_size = 6;

        mask = header->u.m;

    } else if (*VAR_2 == 126 && VAR_0->offset >= 8) {

        *VAR_2 = be16_to_cpu(header->u.s16.l16);

        header_size = 8;

        mask = header->u.s16.m16;

    } else if (*VAR_2 == 127 && VAR_0->offset >= 14) {

        *VAR_2 = be64_to_cpu(header->u.s64.l64);

        header_size = 14;

        mask = header->u.s64.m64;

    } else {

        

        return 0;

    }



    *VAR_3 = header_size + *VAR_2;



    if (VAR_0->offset < *VAR_3) {

        

        return 0;

    }



    *VAR_1 = VAR_0->buffer + header_size;



    

    

    payload32 = (uint32_t *)(*VAR_1);

    for (VAR_7 = 0; VAR_7 < *VAR_2 / 4; VAR_7++) {

        payload32[VAR_7] ^= mask.u;

    }

    

    for (VAR_7 *= 4; VAR_7 < *VAR_2; VAR_7++) {

        (*VAR_1)[VAR_7] ^= mask.c[VAR_7 % 4];

    }



    return 1;

}

int FUNC_0(Buffer *VAR_0, uint8_t **VAR_1,
size_t *VAR_2, size_t *VAR_3)
{ unsigned char VAR_4 = 0, VAR_5 = 0, VAR_6 = 0; size_t header_size = 0; uint32_t *payload32; WsHeader *header = (WsHeader *)VAR_0->buffer; WsMask mask; int VAR_7; if (VAR_0->offset < WS_HEAD_MIN_LEN + 4) { return 0; } VAR_5 = (header->b0 & 0x80) >> 7; VAR_4 = header->b0 & 0x0f; VAR_6 = (header->b1 & 0x80) >> 7; *VAR_2 = header->b1 & 0x7f; if (VAR_4 == WS_OPCODE_CLOSE) { return -1; } if (!VAR_5 || !VAR_6 || VAR_4 != WS_OPCODE_BINARY_FRAME) { VNC_DEBUG("Received faulty/unsupported Websocket frame\n"); return -2; } if (*VAR_2 < 126) { header_size = 6; mask = header->u.m; } else if (*VAR_2 == 126 && VAR_0->offset >= 8) { *VAR_2 = be16_to_cpu(header->u.s16.l16); header_size = 8; mask = header->u.s16.m16; } else if (*VAR_2 == 127 && VAR_0->offset >= 14) { *VAR_2 = be64_to_cpu(header->u.s64.l64); header_size = 14; mask = header->u.s64.m64; } else { return 0; } *VAR_3 = header_size + *VAR_2; if (VAR_0->offset < *VAR_3) { return 0; } *VAR_1 = VAR_0->buffer + header_size; payload32 = (uint32_t *)(*VAR_1); for (VAR_7 = 0; VAR_7 < *VAR_2 / 4; VAR_7++) { payload32[VAR_7] ^= mask.u; } for (VAR_7 *= 4; VAR_7 < *VAR_2; VAR_7++) { (*VAR_1)[VAR_7] ^= mask.c[VAR_7 % 4]; } return 1; }