serialize.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. Provides a serialize/unserialize functionality for INI-like formats.
  3. Copyright (C) 2011-2017
  4. 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. G_GNUC_PRINTF (2, 3)
  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, 0, "mc_serialize_str(): Input data is NULL.");
  85. return NULL;
  86. }
  87. return g_strdup_printf ("%c%zu" 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, 0, FUNC_NAME ": Input data is NULL or empty.");
  107. return NULL;
  108. }
  109. if (*data != prefix)
  110. {
  111. g_set_error (error, MC_ERROR, 0, FUNC_NAME ": String prefix doesn't equal to '%c'", 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, 0,
  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, 0, 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, 0,
  139. FUNC_NAME
  140. ": Specified data length (%zu) is greater than actual data length (%zu)",
  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. GString *buffer;
  161. buffer = g_string_new ("");
  162. groups = mc_config_get_groups (data, NULL);
  163. for (group_iterator = groups; *group_iterator != NULL; group_iterator++)
  164. {
  165. char *serialized_str;
  166. gchar **params, **param_iterator;
  167. serialized_str = mc_serialize_str ('g', *group_iterator, error);
  168. if (serialized_str == NULL)
  169. {
  170. g_string_free (buffer, TRUE);
  171. g_strfreev (groups);
  172. return NULL;
  173. }
  174. g_string_append (buffer, serialized_str);
  175. g_free (serialized_str);
  176. params = mc_config_get_keys (data, *group_iterator, NULL);
  177. for (param_iterator = params; *param_iterator != NULL; param_iterator++)
  178. {
  179. char *value;
  180. serialized_str = mc_serialize_str ('p', *param_iterator, error);
  181. if (serialized_str == NULL)
  182. {
  183. g_string_free (buffer, TRUE);
  184. g_strfreev (params);
  185. g_strfreev (groups);
  186. return NULL;
  187. }
  188. g_string_append (buffer, serialized_str);
  189. g_free (serialized_str);
  190. value = mc_config_get_string_raw (data, *group_iterator, *param_iterator, "");
  191. serialized_str = mc_serialize_str ('v', value, error);
  192. g_free (value);
  193. if (serialized_str == NULL)
  194. {
  195. g_string_free (buffer, TRUE);
  196. g_strfreev (params);
  197. g_strfreev (groups);
  198. return NULL;
  199. }
  200. g_string_append (buffer, serialized_str);
  201. g_free (serialized_str);
  202. }
  203. g_strfreev (params);
  204. }
  205. return g_string_free (buffer, FALSE);
  206. }
  207. /* --------------------------------------------------------------------------------------------- */
  208. /**
  209. * Deserialize string to mc_config_t object
  210. *
  211. * @param data data for serialization
  212. * @param error contain pointer to object for handle error code and message
  213. *
  214. * @return newly allocated mc_config_t object
  215. */
  216. #define FUNC_NAME "mc_deserialize_config()"
  217. #define prepend_error_and_exit() { \
  218. prepend_error_message (error, FUNC_NAME " at %zu", current_position + 1); \
  219. mc_config_deinit (ret_data); \
  220. return NULL; \
  221. }
  222. mc_config_t *
  223. mc_deserialize_config (const char *data, GError ** error)
  224. {
  225. char *current_group = NULL, *current_param = NULL, *current_value = NULL;
  226. size_t current_position = 0;
  227. mc_config_t *ret_data;
  228. enum automat_status
  229. {
  230. WAIT_GROUP,
  231. WAIT_PARAM,
  232. WAIT_VALUE
  233. } current_status = WAIT_GROUP;
  234. ret_data = mc_config_init (NULL, FALSE);
  235. while (data != NULL)
  236. {
  237. if ((current_status == WAIT_GROUP) && (*data == 'p') && (current_group != NULL))
  238. current_status = WAIT_PARAM;
  239. switch (current_status)
  240. {
  241. case WAIT_GROUP:
  242. g_free (current_group);
  243. current_group = mc_deserialize_str ('g', data, error);
  244. if (current_group == NULL)
  245. prepend_error_and_exit ();
  246. data = go_to_end_of_serialized_string (data, current_group, &current_position);
  247. current_status = WAIT_PARAM;
  248. break;
  249. case WAIT_PARAM:
  250. g_free (current_param);
  251. current_param = mc_deserialize_str ('p', data, error);
  252. if (current_param == NULL)
  253. {
  254. g_free (current_group);
  255. prepend_error_and_exit ();
  256. }
  257. data = go_to_end_of_serialized_string (data, current_param, &current_position);
  258. current_status = WAIT_VALUE;
  259. break;
  260. case WAIT_VALUE:
  261. current_value = mc_deserialize_str ('v', data, error);
  262. if (current_value == NULL)
  263. {
  264. g_free (current_group);
  265. g_free (current_param);
  266. prepend_error_and_exit ();
  267. }
  268. mc_config_set_string (ret_data, current_group, current_param, current_value);
  269. data = go_to_end_of_serialized_string (data, current_value, &current_position);
  270. g_free (current_value);
  271. current_status = WAIT_GROUP;
  272. break;
  273. default:
  274. break;
  275. }
  276. }
  277. g_free (current_group);
  278. g_free (current_param);
  279. return ret_data;
  280. }
  281. #undef FUNC_NAME
  282. /* --------------------------------------------------------------------------------------------- */