1 /*******************************************************************************
2  * Copyright (c) 2014 IBM Corp.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * and Eclipse Distribution License v1.0 which accompany this distribution.
7  *
8  * The Eclipse Public License is available at
9  *    http://www.eclipse.org/legal/epl-v10.html
10  * and the Eclipse Distribution License is available at
11  *   http://www.eclipse.org/org/documents/edl-v10.php.
12  *
13  * Contributors:
14  *    Ian Craggs - initial API and implementation and/or initial documentation
15  *******************************************************************************/
16 
17 #include "MQTTPacket.h"
18 #include <string.h>
19 
20 
21 #define min(a, b) ((a < b) ? 1 : 0)
22 
23 /**
24  * Deserializes the supplied (wire) buffer into publish data
25  * @param dup returned integer - the MQTT dup flag
26  * @param qos returned integer - the MQTT QoS value
27  * @param retained returned integer - the MQTT retained flag
28  * @param packetid returned integer - the MQTT packet identifier
29  * @param topicName returned MQTTString - the MQTT topic in the publish
30  * @param payload returned byte buffer - the MQTT publish payload
31  * @param payloadlen returned integer - the length of the MQTT payload
32  * @param buf the raw buffer data, of the correct length determined by the
33  * remaining length field
34  * @param buflen the length in bytes of the data in the supplied buffer
35  * @return error code.  1 is success
36  */
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)37 int MQTTDeserialize_publish(unsigned char *dup, int *qos,
38                             unsigned char *retained, unsigned short *packetid,
39                             MQTTString *topicName, unsigned char **payload,
40                             int *payloadlen, unsigned char *buf, int buflen)
41 {
42     MQTTHeader header = { 0 };
43     unsigned char *curdata = buf;
44     unsigned char *enddata = NULL;
45     int rc = 0;
46     int mylen = 0;
47 
48     header.byte = readChar(&curdata);
49     if (MQTT_HEADER_GET_TYPE(header.byte) != PUBLISH) {
50         goto exit;
51     }
52     *dup = MQTT_HEADER_GET_DUP(header.byte);
53     *qos = MQTT_HEADER_GET_QOS(header.byte);
54     *retained = MQTT_HEADER_GET_RETAIN(header.byte);
55 
56     curdata += (rc = MQTTPacket_decodeBuf(curdata,
57                                           &mylen)); /* read remaining length */
58     enddata = curdata + mylen;
59 
60     if (!readMQTTLenString(topicName, &curdata, enddata) ||
61         enddata - curdata <
62             0) { /* do we have enough data to read the protocol version byte? */
63         goto exit;
64     }
65 
66     if (*qos > 0) {
67         *packetid = readInt(&curdata);
68     }
69 
70     *payloadlen = enddata - curdata;
71     *payload = curdata;
72     rc = 1;
73 exit:
74     return rc;
75 }
76 
77 /**
78  * Deserializes the supplied (wire) buffer into an ack
79  * @param packettype returned integer - the MQTT packet type
80  * @param dup returned integer - the MQTT dup flag
81  * @param packetid returned integer - the MQTT packet identifier
82  * @param buf the raw buffer data, of the correct length determined by the
83  * remaining length field
84  * @param buflen the length in bytes of the data in the supplied buffer
85  * @return error code.  1 is success, 0 is failure
86  */
MQTTDeserialize_ack(unsigned char * packettype,unsigned char * dup,unsigned short * packetid,unsigned char * buf,int buflen)87 int MQTTDeserialize_ack(unsigned char *packettype, unsigned char *dup,
88                         unsigned short *packetid, unsigned char *buf,
89                         int buflen)
90 {
91     MQTTHeader header = { 0 };
92     unsigned char *curdata = buf;
93     unsigned char *enddata = NULL;
94     int rc = 0;
95     int mylen;
96 
97     header.byte = readChar(&curdata);
98     *dup = MQTT_HEADER_GET_DUP(header.byte);
99     *packettype = MQTT_HEADER_GET_TYPE(header.byte);
100 
101     curdata += (rc = MQTTPacket_decodeBuf(curdata,
102                                           &mylen)); /* read remaining length */
103     enddata = curdata + mylen;
104 
105     if (enddata - curdata < 2) {
106         goto exit;
107     }
108     *packetid = readInt(&curdata);
109 
110     rc = 1;
111 exit:
112     return rc;
113 }
114