/* Deserializes the supplied (wire) buffer into publish data  */
int MQTTDeserialize_publish(unsigned char *dup, int *qos, unsigned char *retained, unsigned short *packetid, MQTTString *topicName, unsigned char **payload, int *payloadlen, unsigned char *buf, int buflen)
/* Deserializes the supplied (wire) buffer into publish data  */
int MQTTDeserialize_publish(unsigned char *dup, int *qos, unsigned char *retained, unsigned short *packetid, MQTTString *topicName, unsigned char **payload, int *payloadlen, unsigned char *buf, int buflen)
{
    MQTTHeader header = {0};
    unsigned char* curdata = buf;
    unsigned char* enddata = NULL;
    int rc = 0;
    int mylen = 0;
    FUNC_ENTRY;
    header.byte = readChar(&curdata);
    if (header.bits.type != PUBLISH)
        goto exit;
    *dup = header.bits.dup;
    *qos = header.bits.qos;
    *retained = header.bits.retain;
    curdata += (rc = MQTTPacket_decodeBuf(curdata,
                                          &mylen));
    enddata = curdata + mylen;
    if (!readMQTTLenString(topicName, &curdata, enddata) ||
        enddata - curdata <
            0)
        goto exit;
    if (*qos > 0)
        *packetid = readInt(&curdata);
    *payloadlen = enddata - curdata;
    *payload = curdata;
    rc = 1;
exit:
    FUNC_EXIT_RC(rc);
    return rc;
}