This source file includes following definitions.
- G_GNUC_PRINTF
- go_to_end_of_serialized_string
- mc_serialize_str
- mc_deserialize_str
- mc_serialize_config
- mc_deserialize_config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 #include <config.h>
31
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
35
36 #include "lib/global.h"
37
38 #include "lib/serialize.h"
39
40
41
42
43
44 #define SRLZ_DELIM_C ':'
45 #define SRLZ_DELIM_S ":"
46
47
48
49
50
51
52
53
54
55
56
57 static void
58 G_GNUC_PRINTF (2, 3)
59 prepend_error_message (GError **error, const char *format, ...)
60 {
61 char *prepend_str;
62 char *split_str;
63 va_list ap;
64
65 if ((error == NULL) || (*error == NULL))
66 return;
67
68 va_start (ap, format);
69 prepend_str = g_strdup_vprintf (format, ap);
70 va_end (ap);
71
72 split_str = g_strdup_printf ("%s: %s", prepend_str, (*error)->message);
73 g_free (prepend_str);
74 g_free ((*error)->message);
75 (*error)->message = split_str;
76 }
77
78
79
80 static const char *
81 go_to_end_of_serialized_string (const char *non_serialized_data,
82 const char *already_serialized_part, size_t *offset)
83 {
84 size_t calculated_offset;
85 const char *semi_ptr = strchr (non_serialized_data + 1, SRLZ_DELIM_C);
86
87 calculated_offset = (semi_ptr - non_serialized_data) + 1 + strlen (already_serialized_part);
88 if (calculated_offset >= strlen (non_serialized_data))
89 return NULL;
90
91 non_serialized_data += calculated_offset;
92 *offset += calculated_offset;
93
94 return non_serialized_data;
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 char *
112 mc_serialize_str (const char prefix, const char *data, GError **error)
113 {
114 if (data == NULL)
115 {
116 g_set_error (error, MC_ERROR, 0, "mc_serialize_str(): Input data is NULL.");
117 return NULL;
118 }
119 return g_strdup_printf ("%c%zu" SRLZ_DELIM_S "%s", prefix, strlen (data), data);
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133 #define FUNC_NAME "mc_serialize_str()"
134 char *
135 mc_deserialize_str (const char prefix, const char *data, GError **error)
136 {
137 size_t data_len;
138
139 if ((data == NULL) || (*data == '\0'))
140 {
141 g_set_error (error, MC_ERROR, 0, FUNC_NAME ": Input data is NULL or empty.");
142 return NULL;
143 }
144
145 if (*data != prefix)
146 {
147 g_set_error (error, MC_ERROR, 0, FUNC_NAME ": String prefix doesn't equal to '%c'", prefix);
148 return NULL;
149 }
150
151 {
152 char buffer[BUF_TINY];
153 char *semi_ptr;
154 size_t semi_offset;
155
156 semi_ptr = strchr (data + 1, SRLZ_DELIM_C);
157 if (semi_ptr == NULL)
158 {
159 g_set_error (error, MC_ERROR, 0,
160 FUNC_NAME ": Length delimiter '%c' doesn't exists", SRLZ_DELIM_C);
161 return NULL;
162 }
163 semi_offset = semi_ptr - (data + 1);
164 if (semi_offset >= BUF_TINY)
165 {
166 g_set_error (error, MC_ERROR, 0, FUNC_NAME ": Too big string length");
167 return NULL;
168 }
169 strncpy (buffer, data + 1, semi_offset);
170 buffer[semi_offset] = '\0';
171 data_len = atol (buffer);
172 data += semi_offset + 2;
173 }
174
175 if (data_len > strlen (data))
176 {
177 g_set_error (error, MC_ERROR, 0,
178 FUNC_NAME
179 ": Specified data length (%zu) is greater than actual data length (%zu)",
180 data_len, strlen (data));
181 return NULL;
182 }
183 return g_strndup (data, data_len);
184 }
185
186 #undef FUNC_NAME
187
188
189
190
191
192
193
194
195
196
197
198 char *
199 mc_serialize_config (mc_config_t *data, GError **error)
200 {
201 gchar **groups, **group_iterator;
202 GString *buffer;
203
204 buffer = g_string_new ("");
205 groups = mc_config_get_groups (data, NULL);
206
207 for (group_iterator = groups; *group_iterator != NULL; group_iterator++)
208 {
209 char *serialized_str;
210 gchar **params, **param_iterator;
211
212 serialized_str = mc_serialize_str ('g', *group_iterator, error);
213 if (serialized_str == NULL)
214 {
215 g_string_free (buffer, TRUE);
216 g_strfreev (groups);
217 return NULL;
218 }
219 g_string_append (buffer, serialized_str);
220 g_free (serialized_str);
221
222 params = mc_config_get_keys (data, *group_iterator, NULL);
223
224 for (param_iterator = params; *param_iterator != NULL; param_iterator++)
225 {
226 char *value;
227
228 serialized_str = mc_serialize_str ('p', *param_iterator, error);
229 if (serialized_str == NULL)
230 {
231 g_string_free (buffer, TRUE);
232 g_strfreev (params);
233 g_strfreev (groups);
234 return NULL;
235 }
236 g_string_append (buffer, serialized_str);
237 g_free (serialized_str);
238
239 value = mc_config_get_string_raw (data, *group_iterator, *param_iterator, "");
240 serialized_str = mc_serialize_str ('v', value, error);
241 g_free (value);
242
243 if (serialized_str == NULL)
244 {
245 g_string_free (buffer, TRUE);
246 g_strfreev (params);
247 g_strfreev (groups);
248 return NULL;
249 }
250
251 g_string_append (buffer, serialized_str);
252 g_free (serialized_str);
253 }
254
255 g_strfreev (params);
256 }
257
258 g_strfreev (groups);
259
260 return g_string_free (buffer, FALSE);
261 }
262
263
264
265
266
267
268
269
270
271
272
273 #define FUNC_NAME "mc_deserialize_config()"
274 #define prepend_error_and_exit() { \
275 prepend_error_message (error, FUNC_NAME " at %zu", current_position + 1); \
276 mc_config_deinit (ret_data); \
277 return NULL; \
278 }
279
280 mc_config_t *
281 mc_deserialize_config (const char *data, GError **error)
282 {
283 char *current_group = NULL, *current_param = NULL, *current_value = NULL;
284 size_t current_position = 0;
285 mc_config_t *ret_data;
286 enum automat_status
287 {
288 WAIT_GROUP,
289 WAIT_PARAM,
290 WAIT_VALUE
291 } current_status = WAIT_GROUP;
292
293 ret_data = mc_config_init (NULL, FALSE);
294
295 while (data != NULL)
296 {
297 if ((current_status == WAIT_GROUP) && (*data == 'p') && (current_group != NULL))
298 current_status = WAIT_PARAM;
299
300 switch (current_status)
301 {
302 case WAIT_GROUP:
303 g_free (current_group);
304
305 current_group = mc_deserialize_str ('g', data, error);
306 if (current_group == NULL)
307 prepend_error_and_exit ();
308
309 data = go_to_end_of_serialized_string (data, current_group, ¤t_position);
310 current_status = WAIT_PARAM;
311 break;
312 case WAIT_PARAM:
313 g_free (current_param);
314
315 current_param = mc_deserialize_str ('p', data, error);
316 if (current_param == NULL)
317 {
318 g_free (current_group);
319 prepend_error_and_exit ();
320 }
321
322 data = go_to_end_of_serialized_string (data, current_param, ¤t_position);
323 current_status = WAIT_VALUE;
324 break;
325 case WAIT_VALUE:
326 current_value = mc_deserialize_str ('v', data, error);
327 if (current_value == NULL)
328 {
329 g_free (current_group);
330 g_free (current_param);
331 prepend_error_and_exit ();
332 }
333 mc_config_set_string (ret_data, current_group, current_param, current_value);
334
335 data = go_to_end_of_serialized_string (data, current_value, ¤t_position);
336 g_free (current_value);
337 current_status = WAIT_GROUP;
338 break;
339 default:
340 break;
341 }
342 }
343 g_free (current_group);
344 g_free (current_param);
345
346 return ret_data;
347 }
348
349 #undef FUNC_NAME
350
351