/* Deserializes the supplied (wire) buffer into suback data  */
int MQTTDeserialize_suback(unsigned short *packetid, int maxcount, int *count, int grantedQoSs[], unsigned char *buf, int buflen)
/* Deserializes the supplied (wire) buffer into suback data  */
int MQTTDeserialize_suback(unsigned short *packetid, int maxcount, int *count, int grantedQoSs[], unsigned char *buf, int buflen)
{
    MQTTHeader header = {0};
    unsigned char* curdata = buf;
    unsigned char* enddata = NULL;
    int rc = 0;
    int mylen;
    FUNC_ENTRY;
    header.byte = readChar(&curdata);
    if (header.bits.type != SUBACK)
        goto exit;
    curdata += (rc = MQTTPacket_decodeBuf(curdata,
                                          &mylen));
    enddata = curdata + mylen;
    if (enddata - curdata < 2)
        goto exit;
    *packetid = readInt(&curdata);
    *count = 0;
    while (curdata < enddata) {
        if (*count > maxcount) {
            rc = -1;
            goto exit;
        }
        grantedQoSs[(*count)++] = readChar(&curdata);
    }
    rc = 1;
exit:
    FUNC_EXIT_RC(rc);
    return rc;
}