serialize.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. Provides a serialize/unserialize functionality for INI-like formats.
  3. Copyright (C) 2011 Free Software Foundation, Inc.
  4. Written:
  5. 2011 Slava Zanko <slavazanko@gmail.com>.
  6. This file is part of the Midnight Commander.
  7. The Midnight Commander is free software; you can redistribute it
  8. and/or modify it under the terms of the GNU General Public License as
  9. published by the Free Software Foundation; either version 2 of the
  10. License, or (at your option) any later version.
  11. The Midnight Commander is distributed in the hope that it will be
  12. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. MA 02110-1301, USA.
  19. */
  20. /** \file serialize.c
  21. * \brief Source: serialize/unserialize functionality for INI-like formats.
  22. */
  23. #include <config.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <stdarg.h>
  27. #include "lib/global.h"
  28. #include "lib/serialize.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  31. #define SRLZ_DELIM_C ':'
  32. #define SRLZ_DELIM_S ":"
  33. /*** file scope type declarations ****************************************************************/
  34. /*** file scope variables ************************************************************************/
  35. /*** file scope functions ************************************************************************/
  36. /* --------------------------------------------------------------------------------------------- */
  37. static void
  38. prepend_error_message (GError ** error, const char *format, ...)
  39. {
  40. char *prepend_str;
  41. char *split_str;
  42. va_list ap;
  43. if ((error == NULL) || (*error == NULL))
  44. return;
  45. va_start (ap, format);
  46. prepend_str = g_strdup_vprintf (format, ap);
  47. va_end (ap);
  48. split_str = g_strdup_printf ("%s: %s", prepend_str, (*error)->message);
  49. g_free (prepend_str);
  50. g_free ((*error)->message);
  51. (*error)->message = split_str;
  52. }
  53. /* --------------------------------------------------------------------------------------------- */
  54. static const char *
  55. go_to_end_of_serialized_string (const char *non_serialized_data,
  56. const char *already_serialized_part, size_t * offset)
  57. {
  58. size_t calculated_offset;
  59. const char *semi_ptr = strchr (non_serialized_data + 1, SRLZ_DELIM_C);
  60. calculated_offset = (semi_ptr - non_serialized_data) + 1 + strlen (already_serialized_part);
  61. if (calculated_offset >= strlen (non_serialized_data))
  62. return NULL;
  63. non_serialized_data += calculated_offset;
  64. *offset += calculated_offset;
  65. return non_serialized_data;
  66. }
  67. /* --------------------------------------------------------------------------------------------- */
  68. /*** public functions ****************************************************************************/
  69. /* --------------------------------------------------------------------------------------------- */
  70. /**
  71. * Serialize some string object to string
  72. *
  73. * @param prefix prefix for serailization
  74. * @param data data for serialization
  75. * @param error contain pointer to object for handle error code and message
  76. *
  77. * @return serialized data as newly allocated string
  78. */
  79. char *
  80. mc_serialize_str (const char prefix, const char *data, GError ** error)
  81. {
  82. if (data == NULL)
  83. {
  84. g_set_error (error, MC_ERROR, -1, "mc_serialize_str(): Input data is NULL.");
  85. return NULL;
  86. }
  87. return g_strdup_printf ("%c%zd" SRLZ_DELIM_S "%s", prefix, strlen (data), data);
  88. }
  89. /* --------------------------------------------------------------------------------------------- */
  90. /**
  91. * Deserialize string to string object
  92. *
  93. * @param prefix prefix for deserailization
  94. * @param data data for deserialization
  95. * @param error contain pointer to object for handle error code and message
  96. *
  97. * @return newly allocated string
  98. */
  99. #define FUNC_NAME "mc_serialize_str()"
  100. char *
  101. mc_deserialize_str (const char prefix, const char *data, GError ** error)
  102. {
  103. size_t data_len;
  104. if ((data == NULL) || (strlen (data) == 0))
  105. {
  106. g_set_error (error, MC_ERROR, -1, FUNC_NAME ": Input data is NULL or empty.");
  107. return NULL;
  108. }
  109. if (*data != prefix)
  110. {
  111. g_set_error (error, MC_ERROR, -2, FUNC_NAME ": String prefix doesn't equal to '%c'",
  112. prefix);
  113. return NULL;
  114. }
  115. {
  116. char buffer[BUF_TINY];
  117. char *semi_ptr;
  118. size_t semi_offset;
  119. semi_ptr = strchr (data + 1, SRLZ_DELIM_C);
  120. if (semi_ptr == NULL)
  121. {
  122. g_set_error (error, MC_ERROR, -3,
  123. FUNC_NAME ": Length delimiter '%c' doesn't exists", SRLZ_DELIM_C);
  124. return NULL;
  125. }
  126. semi_offset = semi_ptr - (data + 1);
  127. if (semi_offset >= BUF_TINY)
  128. {
  129. g_set_error (error, MC_ERROR, -3, FUNC_NAME ": Too big string length");
  130. return NULL;
  131. }
  132. strncpy (buffer, data + 1, semi_offset);
  133. buffer[semi_offset] = '\0';
  134. data_len = atol (buffer);
  135. data += semi_offset + 2;
  136. }
  137. if (data_len > strlen (data))
  138. {
  139. g_set_error (error, MC_ERROR, -3,
  140. FUNC_NAME ": Specified data length (%zd) is greater than actual data length (%zd)",
  141. data_len, strlen (data));
  142. return NULL;
  143. }
  144. return g_strndup (data, data_len);
  145. }
  146. #undef FUNC_NAME
  147. /* --------------------------------------------------------------------------------------------- */
  148. /**
  149. * Serialize mc_config_t object to string
  150. *
  151. * @param data data for serialization
  152. * @param error contain pointer to object for handle error code and message
  153. *
  154. * @return serialized data as newly allocated string
  155. */
  156. char *
  157. mc_serialize_config (const mc_config_t * data, GError ** error)
  158. {
  159. gchar **groups, **group_iterator;
  160. size_t group_count;
  161. GString *buffer;
  162. buffer = g_string_new ("");
  163. group_iterator = groups = mc_config_get_groups (data, &group_count);
  164. while (group_count-- != 0)
  165. {
  166. char *serialized_str;
  167. gchar **params, **param_iterator;
  168. size_t param_count;
  169. serialized_str = mc_serialize_str ('g', *group_iterator, error);
  170. if (serialized_str == NULL)
  171. {
  172. g_string_free (buffer, TRUE);
  173. g_strfreev (groups);
  174. return NULL;
  175. }
  176. g_string_append (buffer, serialized_str);
  177. g_free (serialized_str);
  178. param_iterator = params = mc_config_get_keys (data, *group_iterator, &param_count);
  179. while (param_count-- != 0)
  180. {
  181. char *value;
  182. serialized_str = mc_serialize_str ('p', *param_iterator, error);
  183. if (serialized_str == NULL)
  184. {
  185. g_string_free (buffer, TRUE);
  186. g_strfreev (params);
  187. g_strfreev (groups);
  188. return NULL;
  189. }
  190. g_string_append (buffer, serialized_str);
  191. g_free (serialized_str);
  192. value = mc_config_get_string_raw (data, *group_iterator, *param_iterator, "");
  193. serialized_str = mc_serialize_str ('v', value, error);
  194. g_free (value);
  195. if (serialized_str == NULL)
  196. {
  197. g_string_free (buffer, TRUE);
  198. g_strfreev (params);
  199. g_strfreev (groups);
  200. return NULL;
  201. }
  202. g_string_append (buffer, serialized_str);
  203. g_free (serialized_str);
  204. param_iterator++;
  205. }
  206. g_strfreev (params);
  207. group_iterator++;
  208. }
  209. return g_string_free (buffer, FALSE);
  210. }
  211. /* --------------------------------------------------------------------------------------------- */
  212. /**
  213. * Deserialize string to mc_config_t object
  214. *
  215. * @param data data for serialization
  216. * @param error contain pointer to object for handle error code and message
  217. *
  218. * @return newly allocated mc_config_t object
  219. */
  220. #define FUNC_NAME "mc_deserialize_config()"
  221. #define prepend_error_and_exit() { \
  222. prepend_error_message (error, FUNC_NAME " at %lu", current_position + 1); \
  223. mc_config_deinit (ret_data); \
  224. return NULL; \
  225. }
  226. mc_config_t *
  227. mc_deserialize_config (const char *data, GError ** error)
  228. {
  229. char *current_group = NULL, *current_param = NULL, *current_value = NULL;
  230. size_t current_position = 0;
  231. mc_config_t *ret_data = mc_config_init (NULL);
  232. enum automat_status
  233. {
  234. WAIT_GROUP,
  235. WAIT_PARAM,
  236. WAIT_VALUE
  237. } current_status = WAIT_GROUP;
  238. while (data != NULL)
  239. {
  240. if ((current_status == WAIT_GROUP) && (*data == 'p') && (current_group != NULL))
  241. current_status = WAIT_PARAM;
  242. switch (current_status)
  243. {
  244. case WAIT_GROUP:
  245. g_free (current_group);
  246. current_group = mc_deserialize_str ('g', data, error);
  247. if (current_group == NULL)
  248. prepend_error_and_exit ();
  249. data = go_to_end_of_serialized_string (data, current_group, &current_position);
  250. current_status = WAIT_PARAM;
  251. break;
  252. case WAIT_PARAM:
  253. g_free (current_param);
  254. current_param = mc_deserialize_str ('p', data, error);
  255. if (current_param == NULL)
  256. {
  257. g_free (current_group);
  258. prepend_error_and_exit ();
  259. }
  260. data = go_to_end_of_serialized_string (data, current_param, &current_position);
  261. current_status = WAIT_VALUE;
  262. break;
  263. case WAIT_VALUE:
  264. current_value = mc_deserialize_str ('v', data, error);
  265. if (current_param == NULL)
  266. {
  267. g_free (current_group);
  268. g_free (current_param);
  269. prepend_error_and_exit ();
  270. }
  271. mc_config_set_string (ret_data, current_group, current_param, current_value);
  272. data = go_to_end_of_serialized_string (data, current_value, &current_position);
  273. g_free (current_value);
  274. current_status = WAIT_GROUP;
  275. break;
  276. }
  277. }
  278. g_free (current_group);
  279. g_free (current_param);
  280. return ret_data;
  281. }
  282. #undef FUNC_NAME
  283. /* --------------------------------------------------------------------------------------------- */