serialize.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. Provides a serialize/unserialize functionality for INI-like formats.
  3. Copyright (C) 2011-2024
  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. /*** forward declarations (file scope functions) *************************************************/
  34. /*** file scope variables ************************************************************************/
  35. /* --------------------------------------------------------------------------------------------- */
  36. /*** file scope functions ************************************************************************/
  37. /* --------------------------------------------------------------------------------------------- */
  38. static void G_GNUC_PRINTF (2, 3)
  39. prepend_error_message (GError **error, const char *format, ...)
  40. {
  41. char *prepend_str;
  42. char *split_str;
  43. va_list ap;
  44. if ((error == NULL) || (*error == NULL))
  45. return;
  46. va_start (ap, format);
  47. prepend_str = g_strdup_vprintf (format, ap);
  48. va_end (ap);
  49. split_str = g_strdup_printf ("%s: %s", prepend_str, (*error)->message);
  50. g_free (prepend_str);
  51. g_free ((*error)->message);
  52. (*error)->message = split_str;
  53. }
  54. /* --------------------------------------------------------------------------------------------- */
  55. static const char *
  56. go_to_end_of_serialized_string (const char *non_serialized_data,
  57. const char *already_serialized_part, size_t *offset)
  58. {
  59. size_t calculated_offset;
  60. const char *semi_ptr = strchr (non_serialized_data + 1, SRLZ_DELIM_C);
  61. calculated_offset = (semi_ptr - non_serialized_data) + 1 + strlen (already_serialized_part);
  62. if (calculated_offset >= strlen (non_serialized_data))
  63. return NULL;
  64. non_serialized_data += calculated_offset;
  65. *offset += calculated_offset;
  66. return non_serialized_data;
  67. }
  68. /* --------------------------------------------------------------------------------------------- */
  69. /*** public functions ****************************************************************************/
  70. /* --------------------------------------------------------------------------------------------- */
  71. /**
  72. * Serialize some string object to string
  73. *
  74. * @param prefix prefix for serialization
  75. * @param data data for serialization
  76. * @param error contain pointer to object for handle error code and message
  77. *
  78. * @return serialized data as newly allocated string
  79. */
  80. char *
  81. mc_serialize_str (const char prefix, const char *data, GError **error)
  82. {
  83. if (data == NULL)
  84. {
  85. g_set_error (error, MC_ERROR, 0, "mc_serialize_str(): Input data is NULL.");
  86. return NULL;
  87. }
  88. return g_strdup_printf ("%c%zu" SRLZ_DELIM_S "%s", prefix, strlen (data), data);
  89. }
  90. /* --------------------------------------------------------------------------------------------- */
  91. /**
  92. * Deserialize string to string object
  93. *
  94. * @param prefix prefix for deserailization
  95. * @param data data for deserialization
  96. * @param error contain pointer to object for handle error code and message
  97. *
  98. * @return newly allocated string
  99. */
  100. #define FUNC_NAME "mc_serialize_str()"
  101. char *
  102. mc_deserialize_str (const char prefix, const char *data, GError **error)
  103. {
  104. size_t data_len;
  105. if ((data == NULL) || (*data == '\0'))
  106. {
  107. g_set_error (error, MC_ERROR, 0, FUNC_NAME ": Input data is NULL or empty.");
  108. return NULL;
  109. }
  110. if (*data != prefix)
  111. {
  112. g_set_error (error, MC_ERROR, 0, FUNC_NAME ": String prefix doesn't equal to '%c'", 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, 0, FUNC_NAME ": Length delimiter '%c' doesn't exists",
  123. 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, 0, 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, 0,
  140. FUNC_NAME
  141. ": Specified data length (%zu) is greater than actual data length (%zu)",
  142. data_len, strlen (data));
  143. return NULL;
  144. }
  145. return g_strndup (data, data_len);
  146. }
  147. #undef FUNC_NAME
  148. /* --------------------------------------------------------------------------------------------- */
  149. /**
  150. * Serialize mc_config_t object to string
  151. *
  152. * @param data data for serialization
  153. * @param error contain pointer to object for handle error code and message
  154. *
  155. * @return serialized data as newly allocated string
  156. */
  157. char *
  158. mc_serialize_config (mc_config_t *data, GError **error)
  159. {
  160. gchar **groups, **group_iterator;
  161. GString *buffer;
  162. buffer = g_string_new ("");
  163. groups = mc_config_get_groups (data, NULL);
  164. for (group_iterator = groups; *group_iterator != NULL; group_iterator++)
  165. {
  166. char *serialized_str;
  167. gchar **params, **param_iterator;
  168. serialized_str = mc_serialize_str ('g', *group_iterator, error);
  169. if (serialized_str == NULL)
  170. {
  171. g_string_free (buffer, TRUE);
  172. g_strfreev (groups);
  173. return NULL;
  174. }
  175. g_string_append (buffer, serialized_str);
  176. g_free (serialized_str);
  177. params = mc_config_get_keys (data, *group_iterator, NULL);
  178. for (param_iterator = params; *param_iterator != NULL; param_iterator++)
  179. {
  180. char *value;
  181. serialized_str = mc_serialize_str ('p', *param_iterator, error);
  182. if (serialized_str == NULL)
  183. {
  184. g_string_free (buffer, TRUE);
  185. g_strfreev (params);
  186. g_strfreev (groups);
  187. return NULL;
  188. }
  189. g_string_append (buffer, serialized_str);
  190. g_free (serialized_str);
  191. value = mc_config_get_string_raw (data, *group_iterator, *param_iterator, "");
  192. serialized_str = mc_serialize_str ('v', value, error);
  193. g_free (value);
  194. if (serialized_str == NULL)
  195. {
  196. g_string_free (buffer, TRUE);
  197. g_strfreev (params);
  198. g_strfreev (groups);
  199. return NULL;
  200. }
  201. g_string_append (buffer, serialized_str);
  202. g_free (serialized_str);
  203. }
  204. g_strfreev (params);
  205. }
  206. g_strfreev (groups);
  207. return g_string_free (buffer, FALSE);
  208. }
  209. /* --------------------------------------------------------------------------------------------- */
  210. /**
  211. * Deserialize string to mc_config_t object
  212. *
  213. * @param data data for serialization
  214. * @param error contain pointer to object for handle error code and message
  215. *
  216. * @return newly allocated mc_config_t object
  217. */
  218. #define FUNC_NAME "mc_deserialize_config()"
  219. #define prepend_error_and_exit() \
  220. { \
  221. prepend_error_message (error, FUNC_NAME " at %zu", current_position + 1); \
  222. mc_config_deinit (ret_data); \
  223. return NULL; \
  224. }
  225. mc_config_t *
  226. mc_deserialize_config (const char *data, GError **error)
  227. {
  228. char *current_group = NULL, *current_param = NULL, *current_value = NULL;
  229. size_t current_position = 0;
  230. mc_config_t *ret_data;
  231. enum automat_status
  232. {
  233. WAIT_GROUP,
  234. WAIT_PARAM,
  235. WAIT_VALUE
  236. } current_status
  237. = 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. default:
  278. break;
  279. }
  280. }
  281. g_free (current_group);
  282. g_free (current_param);
  283. return ret_data;
  284. }
  285. #undef FUNC_NAME
  286. /* --------------------------------------------------------------------------------------------- */