root/lib/serialize.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. G_GNUC_PRINTF
  2. go_to_end_of_serialized_string
  3. mc_serialize_str
  4. mc_deserialize_str
  5. mc_serialize_config
  6. mc_deserialize_config

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

/* [previous][next][first][last][top][bottom][index][help]  */