serialize.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. Provides a serialize/unserialize functionality for INI-like formats.
  3. Copyright (C) 2011
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Slava Zanko <slavazanko@gmail.com>, 2011
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file lib/serialize.c
  20. * \brief Source: serialize/unserialize functionality for INI-like formats.
  21. */
  22. #include <config.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <stdarg.h>
  26. #include "lib/global.h"
  27. #include "lib/serialize.h"
  28. /*** global variables ****************************************************************************/
  29. /*** file scope macro definitions ****************************************************************/
  30. #define SRLZ_DELIM_C ':'
  31. #define SRLZ_DELIM_S ":"
  32. /*** file scope type declarations ****************************************************************/
  33. /*** file scope variables ************************************************************************/
  34. /*** file scope functions ************************************************************************/
  35. /* --------------------------------------------------------------------------------------------- */
  36. static void
  37. prepend_error_message (GError ** error, const char *format, ...)
  38. {
  39. char *prepend_str;
  40. char *split_str;
  41. va_list ap;
  42. if ((error == NULL) || (*error == NULL))
  43. return;
  44. va_start (ap, format);
  45. prepend_str = g_strdup_vprintf (format, ap);
  46. va_end (ap);
  47. split_str = g_strdup_printf ("%s: %s", prepend_str, (*error)->message);
  48. g_free (prepend_str);
  49. g_free ((*error)->message);
  50. (*error)->message = split_str;
  51. }
  52. /* --------------------------------------------------------------------------------------------- */
  53. static const char *
  54. go_to_end_of_serialized_string (const char *non_serialized_data,
  55. const char *already_serialized_part, size_t * offset)
  56. {
  57. size_t calculated_offset;
  58. const char *semi_ptr = strchr (non_serialized_data + 1, SRLZ_DELIM_C);
  59. calculated_offset = (semi_ptr - non_serialized_data) + 1 + strlen (already_serialized_part);
  60. if (calculated_offset >= strlen (non_serialized_data))
  61. return NULL;
  62. non_serialized_data += calculated_offset;
  63. *offset += calculated_offset;
  64. return non_serialized_data;
  65. }
  66. /* --------------------------------------------------------------------------------------------- */
  67. /*** public functions ****************************************************************************/
  68. /* --------------------------------------------------------------------------------------------- */
  69. /**
  70. * Serialize some string object to string
  71. *
  72. * @param prefix prefix for serailization
  73. * @param data data for serialization
  74. * @param error contain pointer to object for handle error code and message
  75. *
  76. * @return serialized data as newly allocated string
  77. */
  78. char *
  79. mc_serialize_str (const char prefix, const char *data, GError ** error)
  80. {
  81. if (data == NULL)
  82. {
  83. g_set_error (error, MC_ERROR, -1, "mc_serialize_str(): Input data is NULL.");
  84. return NULL;
  85. }
  86. return g_strdup_printf ("%c%zd" SRLZ_DELIM_S "%s", prefix, strlen (data), data);
  87. }
  88. /* --------------------------------------------------------------------------------------------- */
  89. /**
  90. * Deserialize string to string object
  91. *
  92. * @param prefix prefix for deserailization
  93. * @param data data for deserialization
  94. * @param error contain pointer to object for handle error code and message
  95. *
  96. * @return newly allocated string
  97. */
  98. #define FUNC_NAME "mc_serialize_str()"
  99. char *
  100. mc_deserialize_str (const char prefix, const char *data, GError ** error)
  101. {
  102. size_t data_len;
  103. if ((data == NULL) || (strlen (data) == 0))
  104. {
  105. g_set_error (error, MC_ERROR, -1, FUNC_NAME ": Input data is NULL or empty.");
  106. return NULL;
  107. }
  108. if (*data != prefix)
  109. {
  110. g_set_error (error, MC_ERROR, -2, FUNC_NAME ": String prefix doesn't equal to '%c'",
  111. prefix);
  112. return NULL;
  113. }
  114. {
  115. char buffer[BUF_TINY];
  116. char *semi_ptr;
  117. size_t semi_offset;
  118. semi_ptr = strchr (data + 1, SRLZ_DELIM_C);
  119. if (semi_ptr == NULL)
  120. {
  121. g_set_error (error, MC_ERROR, -3,
  122. FUNC_NAME ": Length delimiter '%c' doesn't exists", SRLZ_DELIM_C);
  123. return NULL;
  124. }
  125. semi_offset = semi_ptr - (data + 1);
  126. if (semi_offset >= BUF_TINY)
  127. {
  128. g_set_error (error, MC_ERROR, -3, FUNC_NAME ": Too big string length");
  129. return NULL;
  130. }
  131. strncpy (buffer, data + 1, semi_offset);
  132. buffer[semi_offset] = '\0';
  133. data_len = atol (buffer);
  134. data += semi_offset + 2;
  135. }
  136. if (data_len > strlen (data))
  137. {
  138. g_set_error (error, MC_ERROR, -3,
  139. FUNC_NAME
  140. ": 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;
  232. enum automat_status
  233. {
  234. WAIT_GROUP,
  235. WAIT_PARAM,
  236. WAIT_VALUE
  237. } current_status = WAIT_GROUP;
  238. ret_data = mc_config_init (NULL, FALSE);
  239. while (data != NULL)
  240. {
  241. if ((current_status == WAIT_GROUP) && (*data == 'p') && (current_group != NULL))
  242. current_status = WAIT_PARAM;
  243. switch (current_status)
  244. {
  245. case WAIT_GROUP:
  246. g_free (current_group);
  247. current_group = mc_deserialize_str ('g', data, error);
  248. if (current_group == NULL)
  249. prepend_error_and_exit ();
  250. data = go_to_end_of_serialized_string (data, current_group, &current_position);
  251. current_status = WAIT_PARAM;
  252. break;
  253. case WAIT_PARAM:
  254. g_free (current_param);
  255. current_param = mc_deserialize_str ('p', data, error);
  256. if (current_param == NULL)
  257. {
  258. g_free (current_group);
  259. prepend_error_and_exit ();
  260. }
  261. data = go_to_end_of_serialized_string (data, current_param, &current_position);
  262. current_status = WAIT_VALUE;
  263. break;
  264. case WAIT_VALUE:
  265. current_value = mc_deserialize_str ('v', data, error);
  266. if (current_value == NULL)
  267. {
  268. g_free (current_group);
  269. g_free (current_param);
  270. prepend_error_and_exit ();
  271. }
  272. mc_config_set_string (ret_data, current_group, current_param, current_value);
  273. data = go_to_end_of_serialized_string (data, current_value, &current_position);
  274. g_free (current_value);
  275. current_status = WAIT_GROUP;
  276. break;
  277. }
  278. }
  279. g_free (current_group);
  280. g_free (current_param);
  281. return ret_data;
  282. }
  283. #undef FUNC_NAME
  284. /* --------------------------------------------------------------------------------------------- */