serialize.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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
  39. G_GNUC_PRINTF (2, 3)
  40. prepend_error_message (GError **error, const char *format, ...)
  41. {
  42. char *prepend_str;
  43. char *split_str;
  44. va_list ap;
  45. if ((error == NULL) || (*error == NULL))
  46. return;
  47. va_start (ap, format);
  48. prepend_str = g_strdup_vprintf (format, ap);
  49. va_end (ap);
  50. split_str = g_strdup_printf ("%s: %s", prepend_str, (*error)->message);
  51. g_free (prepend_str);
  52. g_free ((*error)->message);
  53. (*error)->message = split_str;
  54. }
  55. /* --------------------------------------------------------------------------------------------- */
  56. static const char *
  57. go_to_end_of_serialized_string (const char *non_serialized_data,
  58. const char *already_serialized_part, size_t *offset)
  59. {
  60. size_t calculated_offset;
  61. const char *semi_ptr = strchr (non_serialized_data + 1, SRLZ_DELIM_C);
  62. calculated_offset = (semi_ptr - non_serialized_data) + 1 + strlen (already_serialized_part);
  63. if (calculated_offset >= strlen (non_serialized_data))
  64. return NULL;
  65. non_serialized_data += calculated_offset;
  66. *offset += calculated_offset;
  67. return non_serialized_data;
  68. }
  69. /* --------------------------------------------------------------------------------------------- */
  70. /*** public functions ****************************************************************************/
  71. /* --------------------------------------------------------------------------------------------- */
  72. /**
  73. * Serialize some string object to string
  74. *
  75. * @param prefix prefix for serialization
  76. * @param data data for serialization
  77. * @param error contain pointer to object for handle error code and message
  78. *
  79. * @return serialized data as newly allocated string
  80. */
  81. char *
  82. mc_serialize_str (const char prefix, const char *data, GError **error)
  83. {
  84. if (data == NULL)
  85. {
  86. g_set_error (error, MC_ERROR, 0, "mc_serialize_str(): Input data is NULL.");
  87. return NULL;
  88. }
  89. return g_strdup_printf ("%c%zu" SRLZ_DELIM_S "%s", prefix, strlen (data), data);
  90. }
  91. /* --------------------------------------------------------------------------------------------- */
  92. /**
  93. * Deserialize string to string object
  94. *
  95. * @param prefix prefix for deserailization
  96. * @param data data for deserialization
  97. * @param error contain pointer to object for handle error code and message
  98. *
  99. * @return newly allocated string
  100. */
  101. #define FUNC_NAME "mc_serialize_str()"
  102. char *
  103. mc_deserialize_str (const char prefix, const char *data, GError **error)
  104. {
  105. size_t data_len;
  106. if ((data == NULL) || (*data == '\0'))
  107. {
  108. g_set_error (error, MC_ERROR, 0, FUNC_NAME ": Input data is NULL or empty.");
  109. return NULL;
  110. }
  111. if (*data != prefix)
  112. {
  113. g_set_error (error, MC_ERROR, 0, FUNC_NAME ": String prefix doesn't equal to '%c'", prefix);
  114. return NULL;
  115. }
  116. {
  117. char buffer[BUF_TINY];
  118. char *semi_ptr;
  119. size_t semi_offset;
  120. semi_ptr = strchr (data + 1, SRLZ_DELIM_C);
  121. if (semi_ptr == NULL)
  122. {
  123. g_set_error (error, MC_ERROR, 0,
  124. FUNC_NAME ": Length delimiter '%c' doesn't exists", SRLZ_DELIM_C);
  125. return NULL;
  126. }
  127. semi_offset = semi_ptr - (data + 1);
  128. if (semi_offset >= BUF_TINY)
  129. {
  130. g_set_error (error, MC_ERROR, 0, FUNC_NAME ": Too big string length");
  131. return NULL;
  132. }
  133. strncpy (buffer, data + 1, semi_offset);
  134. buffer[semi_offset] = '\0';
  135. data_len = atol (buffer);
  136. data += semi_offset + 2;
  137. }
  138. if (data_len > strlen (data))
  139. {
  140. g_set_error (error, MC_ERROR, 0,
  141. FUNC_NAME
  142. ": Specified data length (%zu) is greater than actual data length (%zu)",
  143. data_len, strlen (data));
  144. return NULL;
  145. }
  146. return g_strndup (data, data_len);
  147. }
  148. #undef FUNC_NAME
  149. /* --------------------------------------------------------------------------------------------- */
  150. /**
  151. * Serialize mc_config_t object to string
  152. *
  153. * @param data data for serialization
  154. * @param error contain pointer to object for handle error code and message
  155. *
  156. * @return serialized data as newly allocated string
  157. */
  158. char *
  159. mc_serialize_config (mc_config_t *data, GError **error)
  160. {
  161. gchar **groups, **group_iterator;
  162. GString *buffer;
  163. buffer = g_string_new ("");
  164. groups = mc_config_get_groups (data, NULL);
  165. for (group_iterator = groups; *group_iterator != NULL; group_iterator++)
  166. {
  167. char *serialized_str;
  168. gchar **params, **param_iterator;
  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. params = mc_config_get_keys (data, *group_iterator, NULL);
  179. for (param_iterator = params; *param_iterator != NULL; param_iterator++)
  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. }
  205. g_strfreev (params);
  206. }
  207. g_strfreev (groups);
  208. return g_string_free (buffer, FALSE);
  209. }
  210. /* --------------------------------------------------------------------------------------------- */
  211. /**
  212. * Deserialize string to mc_config_t object
  213. *
  214. * @param data data for serialization
  215. * @param error contain pointer to object for handle error code and message
  216. *
  217. * @return newly allocated mc_config_t object
  218. */
  219. #define FUNC_NAME "mc_deserialize_config()"
  220. #define prepend_error_and_exit() { \
  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 = WAIT_GROUP;
  237. ret_data = mc_config_init (NULL, FALSE);
  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_value == 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. default:
  277. break;
  278. }
  279. }
  280. g_free (current_group);
  281. g_free (current_param);
  282. return ret_data;
  283. }
  284. #undef FUNC_NAME
  285. /* --------------------------------------------------------------------------------------------- */