/src/mosquitto/libcommon/cjson_common.c
Line | Count | Source |
1 | | /* |
2 | | Copyright (c) 2009-2021 Roger Light <roger@atchoo.org> |
3 | | |
4 | | All rights reserved. This program and the accompanying materials |
5 | | are made available under the terms of the Eclipse Public License 2.0 |
6 | | and Eclipse Distribution License v1.0 which accompany this distribution. |
7 | | |
8 | | The Eclipse Public License is available at |
9 | | https://www.eclipse.org/legal/epl-2.0/ |
10 | | and the Eclipse Distribution License is available at |
11 | | http://www.eclipse.org/org/documents/edl-v10.php. |
12 | | |
13 | | SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
14 | | |
15 | | Contributors: |
16 | | Roger Light - initial implementation and documentation. |
17 | | */ |
18 | | |
19 | | #include "config.h" |
20 | | |
21 | | #include <cjson/cJSON.h> |
22 | | #include <ctype.h> |
23 | | #include <errno.h> |
24 | | #include <limits.h> |
25 | | #include <stdbool.h> |
26 | | #include <stdio.h> |
27 | | #include <stdlib.h> |
28 | | #include <string.h> |
29 | | |
30 | | #include "mosquitto.h" |
31 | | |
32 | | |
33 | | cJSON *mosquitto_properties_to_json(const mosquitto_property *properties) |
34 | 0 | { |
35 | 0 | cJSON *array, *obj; |
36 | 0 | char *name, *value; |
37 | 0 | uint8_t i8; |
38 | 0 | uint16_t len; |
39 | 0 | int propid; |
40 | |
|
41 | 0 | if(!properties){ |
42 | 0 | return NULL; |
43 | 0 | } |
44 | | |
45 | 0 | array = cJSON_CreateArray(); |
46 | 0 | if(!array){ |
47 | 0 | return NULL; |
48 | 0 | } |
49 | | |
50 | 0 | do{ |
51 | 0 | propid = mosquitto_property_identifier(properties); |
52 | 0 | obj = cJSON_CreateObject(); |
53 | 0 | if(!obj){ |
54 | 0 | cJSON_Delete(array); |
55 | 0 | return NULL; |
56 | 0 | } |
57 | 0 | cJSON_AddItemToArray(array, obj); |
58 | | /* identifier, (key), value */ |
59 | 0 | if(cJSON_AddStringToObject(obj, |
60 | 0 | "identifier", |
61 | 0 | mosquitto_property_identifier_to_string(propid)) == NULL |
62 | 0 | ){ |
63 | 0 | cJSON_Delete(array); |
64 | 0 | return NULL; |
65 | 0 | } |
66 | | |
67 | 0 | switch(propid){ |
68 | 0 | case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR: |
69 | | /* byte */ |
70 | 0 | mosquitto_property_read_byte(properties, propid, &i8, false); |
71 | 0 | if(cJSON_AddNumberToObject(obj, "value", i8) == NULL){ |
72 | 0 | cJSON_Delete(array); |
73 | 0 | return NULL; |
74 | 0 | } |
75 | 0 | break; |
76 | | |
77 | 0 | case MQTT_PROP_CONTENT_TYPE: |
78 | 0 | case MQTT_PROP_RESPONSE_TOPIC: |
79 | 0 | case MQTT_PROP_REASON_STRING: |
80 | | /* str */ |
81 | 0 | if(mosquitto_property_read_string(properties, propid, &value, false) == NULL){ |
82 | 0 | cJSON_Delete(array); |
83 | 0 | return NULL; |
84 | 0 | } |
85 | 0 | if(cJSON_AddStringToObject(obj, "value", value) == NULL){ |
86 | 0 | free(value); |
87 | 0 | cJSON_Delete(array); |
88 | 0 | return NULL; |
89 | 0 | } |
90 | 0 | free(value); |
91 | 0 | break; |
92 | | |
93 | 0 | case MQTT_PROP_CORRELATION_DATA: |
94 | 0 | { |
95 | | /* bin */ |
96 | 0 | void *binval = NULL; |
97 | 0 | mosquitto_property_read_binary(properties, propid, &binval, &len, false); |
98 | 0 | char *hexval = malloc(2*(size_t)len + 1); |
99 | 0 | if(!hexval){ |
100 | 0 | free(binval); |
101 | 0 | cJSON_Delete(array); |
102 | 0 | return NULL; |
103 | 0 | } |
104 | 0 | for(int i=0; i<len; i++){ |
105 | 0 | sprintf(&hexval[i*2], "%02X", ((uint8_t *)binval)[i]); |
106 | 0 | } |
107 | 0 | hexval[2*len] = '\0'; |
108 | 0 | free(binval); |
109 | |
|
110 | 0 | if(cJSON_AddStringToObject(obj, "value", hexval) == NULL){ |
111 | 0 | free(hexval); |
112 | 0 | cJSON_Delete(array); |
113 | 0 | return NULL; |
114 | 0 | } |
115 | 0 | free(hexval); |
116 | 0 | } |
117 | 0 | break; |
118 | | |
119 | 0 | case MQTT_PROP_USER_PROPERTY: |
120 | | /* pair */ |
121 | 0 | mosquitto_property_read_string_pair(properties, propid, &name, &value, false); |
122 | 0 | if(cJSON_AddStringToObject(obj, "name", name) == NULL |
123 | 0 | || cJSON_AddStringToObject(obj, "value", value) == NULL){ |
124 | |
|
125 | 0 | free(name); |
126 | 0 | free(value); |
127 | 0 | cJSON_Delete(array); |
128 | 0 | return NULL; |
129 | 0 | } |
130 | 0 | free(name); |
131 | 0 | free(value); |
132 | 0 | break; |
133 | | |
134 | 0 | default: |
135 | 0 | break; |
136 | 0 | } |
137 | | |
138 | 0 | properties = mosquitto_property_next(properties); |
139 | 0 | }while(properties); |
140 | | |
141 | 0 | return array; |
142 | 0 | } |