void* MQTTPacket_publish(unsigned char aHeader, char* data, size_t datalen) { Publish* pack = malloc(sizeof(Publish)); char* curdata = data; char* enddata = &data[datalen]; FUNC_ENTRY; pack->header.byte = aHeader; if ((pack->topic = readUTFlen(&curdata, enddata, &pack->topiclen)) == NULL) // Topic name on which to publish. { free(pack); pack = NULL; goto exit; } if (pack->header.bits.qos > 0) // Msgid only exists for QoS 1 or 2 pack->msgId = readInt(&curdata); else pack->msgId = 0; pack->payload = curdata; pack->payloadlen = datalen-(curdata-data); exit: FUNC_EXIT; return pack; }
/** * Reads a "UTF" string from the input buffer. UTF as in the MQTT v3 spec which really means * a length delimited string. So it reads the two byte length then the data according to * that length. The end of the buffer is provided too, so we can prevent buffer overruns caused * by an incorrect length. * @param pptr pointer to the input buffer - incremented by the number of bytes used & returned * @param enddata pointer to the end of the buffer not to be read beyond * @return an allocated C string holding the characters read, or NULL if the length read would * have caused an overrun. */ char* readUTF(char** pptr, char* enddata) { int len; return readUTFlen(pptr, enddata, &len); }