aboutsummaryrefslogtreecommitdiff
path: root/src/json/json_gnsrecord.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/json/json_gnsrecord.c
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/json/json_gnsrecord.c')
-rw-r--r--src/json/json_gnsrecord.c217
1 files changed, 109 insertions, 108 deletions
diff --git a/src/json/json_gnsrecord.c b/src/json/json_gnsrecord.c
index 5991683bb..41af2b0a2 100644
--- a/src/json/json_gnsrecord.c
+++ b/src/json/json_gnsrecord.c
@@ -35,7 +35,8 @@
35#define GNUNET_JSON_GNSRECORD_RECORD_NAME "record_name" 35#define GNUNET_JSON_GNSRECORD_RECORD_NAME "record_name"
36#define GNUNET_JSON_GNSRECORD_NEVER "never" 36#define GNUNET_JSON_GNSRECORD_NEVER "never"
37 37
38struct GnsRecordInfo { 38struct GnsRecordInfo
39{
39 char **name; 40 char **name;
40 41
41 unsigned int *rd_count; 42 unsigned int *rd_count;
@@ -45,20 +46,20 @@ struct GnsRecordInfo {
45 46
46 47
47static void 48static void
48cleanup_recordinfo(struct GnsRecordInfo *gnsrecord_info) 49cleanup_recordinfo (struct GnsRecordInfo *gnsrecord_info)
49{ 50{
50 if (NULL != *(gnsrecord_info->rd)) 51 if (NULL != *(gnsrecord_info->rd))
52 {
53 for (int i = 0; i < *(gnsrecord_info->rd_count); i++)
51 { 54 {
52 for (int i = 0; i < *(gnsrecord_info->rd_count); i++) 55 if (NULL != (*(gnsrecord_info->rd))[i].data)
53 { 56 GNUNET_free ((char *) (*(gnsrecord_info->rd))[i].data);
54 if (NULL != (*(gnsrecord_info->rd))[i].data)
55 GNUNET_free((char *)(*(gnsrecord_info->rd))[i].data);
56 }
57 GNUNET_free(*(gnsrecord_info->rd));
58 *(gnsrecord_info->rd) = NULL;
59 } 57 }
58 GNUNET_free (*(gnsrecord_info->rd));
59 *(gnsrecord_info->rd) = NULL;
60 }
60 if (NULL != *(gnsrecord_info->name)) 61 if (NULL != *(gnsrecord_info->name))
61 GNUNET_free(*(gnsrecord_info->name)); 62 GNUNET_free (*(gnsrecord_info->name));
62 *(gnsrecord_info->name) = NULL; 63 *(gnsrecord_info->name) = NULL;
63} 64}
64 65
@@ -72,7 +73,7 @@ cleanup_recordinfo(struct GnsRecordInfo *gnsrecord_info)
72 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 73 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
73 */ 74 */
74static int 75static int
75parse_record(json_t *data, struct GNUNET_GNSRECORD_Data *rd) 76parse_record (json_t *data, struct GNUNET_GNSRECORD_Data *rd)
76{ 77{
77 struct GNUNET_TIME_Absolute abs_expiration_time; 78 struct GNUNET_TIME_Absolute abs_expiration_time;
78 struct GNUNET_TIME_Relative rel_expiration_time; 79 struct GNUNET_TIME_Relative rel_expiration_time;
@@ -82,60 +83,60 @@ parse_record(json_t *data, struct GNUNET_GNSRECORD_Data *rd)
82 int flag; 83 int flag;
83 int unpack_state = 0; 84 int unpack_state = 0;
84 85
85 //interpret single gns record 86 // interpret single gns record
86 unpack_state = json_unpack(data, 87 unpack_state = json_unpack (data,
87 "{s:s, s:s, s:s, s?:i!}", 88 "{s:s, s:s, s:s, s?:i!}",
88 GNUNET_JSON_GNSRECORD_VALUE, 89 GNUNET_JSON_GNSRECORD_VALUE,
89 &value, 90 &value,
90 GNUNET_JSON_GNSRECORD_TYPE, 91 GNUNET_JSON_GNSRECORD_TYPE,
91 &record_type, 92 &record_type,
92 GNUNET_JSON_GNSRECORD_EXPIRATION_TIME, 93 GNUNET_JSON_GNSRECORD_EXPIRATION_TIME,
93 &expiration_time, 94 &expiration_time,
94 GNUNET_JSON_GNSRECORD_FLAG, 95 GNUNET_JSON_GNSRECORD_FLAG,
95 &flag); 96 &flag);
96 if (0 != unpack_state) 97 if (0 != unpack_state)
97 { 98 {
98 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 99 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
99 "Error gnsdata object has a wrong format!\n"); 100 "Error gnsdata object has a wrong format!\n");
100 return GNUNET_SYSERR; 101 return GNUNET_SYSERR;
101 } 102 }
102 rd->record_type = GNUNET_GNSRECORD_typename_to_number(record_type); 103 rd->record_type = GNUNET_GNSRECORD_typename_to_number (record_type);
103 if (UINT32_MAX == rd->record_type) 104 if (UINT32_MAX == rd->record_type)
104 { 105 {
105 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Unsupported type\n"); 106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unsupported type\n");
106 return GNUNET_SYSERR; 107 return GNUNET_SYSERR;
107 } 108 }
108 if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value(rd->record_type, 109 if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value (rd->record_type,
109 value, 110 value,
110 (void**)&rd->data, 111 (void**) &rd->data,
111 &rd->data_size)) 112 &rd->data_size))
112 { 113 {
113 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Value invalid for record type\n"); 114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Value invalid for record type\n");
114 return GNUNET_SYSERR; 115 return GNUNET_SYSERR;
115 } 116 }
116 117
117 if (0 == strcmp(expiration_time, GNUNET_JSON_GNSRECORD_NEVER)) 118 if (0 == strcmp (expiration_time, GNUNET_JSON_GNSRECORD_NEVER))
118 { 119 {
119 rd->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us; 120 rd->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
120 } 121 }
121 else if (GNUNET_OK == 122 else if (GNUNET_OK ==
122 GNUNET_STRINGS_fancy_time_to_absolute(expiration_time, 123 GNUNET_STRINGS_fancy_time_to_absolute (expiration_time,
123 &abs_expiration_time)) 124 &abs_expiration_time))
124 { 125 {
125 rd->expiration_time = abs_expiration_time.abs_value_us; 126 rd->expiration_time = abs_expiration_time.abs_value_us;
126 } 127 }
127 else if (GNUNET_OK == 128 else if (GNUNET_OK ==
128 GNUNET_STRINGS_fancy_time_to_relative(expiration_time, 129 GNUNET_STRINGS_fancy_time_to_relative (expiration_time,
129 &rel_expiration_time)) 130 &rel_expiration_time))
130 { 131 {
131 rd->expiration_time = rel_expiration_time.rel_value_us; 132 rd->expiration_time = rel_expiration_time.rel_value_us;
132 } 133 }
133 else 134 else
134 { 135 {
135 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Expiration time invalid\n"); 136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Expiration time invalid\n");
136 return GNUNET_SYSERR; 137 return GNUNET_SYSERR;
137 } 138 }
138 rd->flags = (enum GNUNET_GNSRECORD_Flags)flag; 139 rd->flags = (enum GNUNET_GNSRECORD_Flags) flag;
139 return GNUNET_OK; 140 return GNUNET_OK;
140} 141}
141 142
@@ -149,23 +150,23 @@ parse_record(json_t *data, struct GNUNET_GNSRECORD_Data *rd)
149 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error 150 * @return #GNUNET_OK upon successful parsing; #GNUNET_SYSERR upon error
150 */ 151 */
151static int 152static int
152parse_record_data(struct GnsRecordInfo *gnsrecord_info, json_t *data) 153parse_record_data (struct GnsRecordInfo *gnsrecord_info, json_t *data)
153{ 154{
154 GNUNET_assert(NULL != data); 155 GNUNET_assert (NULL != data);
155 if (!json_is_array(data)) 156 if (! json_is_array (data))
156 { 157 {
157 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
158 "Error gns record data JSON is not an array!\n"); 159 "Error gns record data JSON is not an array!\n");
159 return GNUNET_SYSERR; 160 return GNUNET_SYSERR;
160 } 161 }
161 *(gnsrecord_info->rd_count) = json_array_size(data); 162 *(gnsrecord_info->rd_count) = json_array_size (data);
162 *(gnsrecord_info->rd) = GNUNET_malloc(sizeof(struct GNUNET_GNSRECORD_Data) * 163 *(gnsrecord_info->rd) = GNUNET_malloc (sizeof(struct GNUNET_GNSRECORD_Data)
163 json_array_size(data)); 164 * json_array_size (data));
164 size_t index; 165 size_t index;
165 json_t *value; 166 json_t *value;
166 json_array_foreach(data, index, value) 167 json_array_foreach (data, index, value)
167 { 168 {
168 if (GNUNET_OK != parse_record(value, &(*(gnsrecord_info->rd))[index])) 169 if (GNUNET_OK != parse_record (value, &(*(gnsrecord_info->rd))[index]))
169 return GNUNET_SYSERR; 170 return GNUNET_SYSERR;
170 } 171 }
171 return GNUNET_OK; 172 return GNUNET_OK;
@@ -173,42 +174,42 @@ parse_record_data(struct GnsRecordInfo *gnsrecord_info, json_t *data)
173 174
174 175
175static int 176static int
176parse_gnsrecordobject(void *cls, 177parse_gnsrecordobject (void *cls,
177 json_t *root, 178 json_t *root,
178 struct GNUNET_JSON_Specification *spec) 179 struct GNUNET_JSON_Specification *spec)
179{ 180{
180 struct GnsRecordInfo *gnsrecord_info; 181 struct GnsRecordInfo *gnsrecord_info;
181 int unpack_state = 0; 182 int unpack_state = 0;
182 const char *name; 183 const char *name;
183 json_t *data; 184 json_t *data;
184 185
185 GNUNET_assert(NULL != root); 186 GNUNET_assert (NULL != root);
186 if (!json_is_object(root)) 187 if (! json_is_object (root))
187 { 188 {
188 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189 "Error record JSON is not an object!\n"); 190 "Error record JSON is not an object!\n");
190 return GNUNET_SYSERR; 191 return GNUNET_SYSERR;
191 } 192 }
192 //interpret single gns record 193 // interpret single gns record
193 unpack_state = json_unpack(root, 194 unpack_state = json_unpack (root,
194 "{s:s, s:o!}", 195 "{s:s, s:o!}",
195 GNUNET_JSON_GNSRECORD_RECORD_NAME, 196 GNUNET_JSON_GNSRECORD_RECORD_NAME,
196 &name, 197 &name,
197 GNUNET_JSON_GNSRECORD_RECORD_DATA, 198 GNUNET_JSON_GNSRECORD_RECORD_DATA,
198 &data); 199 &data);
199 if (0 != unpack_state) 200 if (0 != unpack_state)
200 { 201 {
201 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
202 "Error namestore records object has a wrong format!\n"); 203 "Error namestore records object has a wrong format!\n");
203 return GNUNET_SYSERR; 204 return GNUNET_SYSERR;
204 } 205 }
205 gnsrecord_info = (struct GnsRecordInfo *)spec->ptr; 206 gnsrecord_info = (struct GnsRecordInfo *) spec->ptr;
206 *(gnsrecord_info->name) = GNUNET_strdup(name); 207 *(gnsrecord_info->name) = GNUNET_strdup (name);
207 if (GNUNET_OK != parse_record_data(gnsrecord_info, data)) 208 if (GNUNET_OK != parse_record_data (gnsrecord_info, data))
208 { 209 {
209 cleanup_recordinfo(gnsrecord_info); 210 cleanup_recordinfo (gnsrecord_info);
210 return GNUNET_SYSERR; 211 return GNUNET_SYSERR;
211 } 212 }
212 return GNUNET_OK; 213 return GNUNET_OK;
213} 214}
214 215
@@ -220,11 +221,11 @@ parse_gnsrecordobject(void *cls,
220 * @param[out] spec where to free the data 221 * @param[out] spec where to free the data
221 */ 222 */
222static void 223static void
223clean_gnsrecordobject(void *cls, struct GNUNET_JSON_Specification *spec) 224clean_gnsrecordobject (void *cls, struct GNUNET_JSON_Specification *spec)
224{ 225{
225 struct GnsRecordInfo *gnsrecord_info = (struct GnsRecordInfo *)spec->ptr; 226 struct GnsRecordInfo *gnsrecord_info = (struct GnsRecordInfo *) spec->ptr;
226 227
227 GNUNET_free(gnsrecord_info); 228 GNUNET_free (gnsrecord_info);
228} 229}
229 230
230 231
@@ -235,11 +236,11 @@ clean_gnsrecordobject(void *cls, struct GNUNET_JSON_Specification *spec)
235 * @return JSON Specification 236 * @return JSON Specification
236 */ 237 */
237struct GNUNET_JSON_Specification 238struct GNUNET_JSON_Specification
238GNUNET_JSON_spec_gnsrecord(struct GNUNET_GNSRECORD_Data **rd, 239GNUNET_JSON_spec_gnsrecord (struct GNUNET_GNSRECORD_Data **rd,
239 unsigned int *rd_count, 240 unsigned int *rd_count,
240 char **name) 241 char **name)
241{ 242{
242 struct GnsRecordInfo *gnsrecord_info = GNUNET_new(struct GnsRecordInfo); 243 struct GnsRecordInfo *gnsrecord_info = GNUNET_new (struct GnsRecordInfo);
243 244
244 gnsrecord_info->rd = rd; 245 gnsrecord_info->rd = rd;
245 gnsrecord_info->name = name; 246 gnsrecord_info->name = name;