Manual pages: mcmcdiffmceditmcview

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-2026
   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     const char *semi_ptr = strchr (non_serialized_data + 1, SRLZ_DELIM_C);
  84     const size_t calculated_offset =
  85         (semi_ptr - non_serialized_data) + 1 + strlen (already_serialized_part);
  86 
  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 
 153         const char *semi_ptr = strchr (data + 1, SRLZ_DELIM_C);
 154         if (semi_ptr == NULL)
 155         {
 156             g_set_error (error, MC_ERROR, 0, FUNC_NAME ": Length delimiter '%c' doesn't exists",
 157                          SRLZ_DELIM_C);
 158             return NULL;
 159         }
 160 
 161         const size_t semi_offset = semi_ptr - (data + 1);
 162         if (semi_offset >= BUF_TINY)
 163         {
 164             g_set_error (error, MC_ERROR, 0, FUNC_NAME ": Too big string length");
 165             return NULL;
 166         }
 167         strncpy (buffer, data + 1, semi_offset);
 168         buffer[semi_offset] = '\0';
 169         data_len = atol (buffer);
 170         data += semi_offset + 2;
 171     }
 172 
 173     if (data_len > strlen (data))
 174     {
 175         g_set_error (error, MC_ERROR, 0,
 176                      FUNC_NAME
 177                      ": Specified data length (%zu) is greater than actual data length (%zu)",
 178                      data_len, strlen (data));
 179         return NULL;
 180     }
 181     return g_strndup (data, data_len);
 182 }
 183 
 184 #undef FUNC_NAME
 185 
 186 /* --------------------------------------------------------------------------------------------- */
 187 /**
 188  * Serialize mc_config_t object to string
 189  *
 190  * @param data data for serialization
 191  * @param error contain pointer to object for handle error code and message
 192  *
 193  * @return serialized data as newly allocated string
 194  */
 195 
 196 char *
 197 mc_serialize_config (mc_config_t *data, GError **error)
     /* [previous][next][first][last][top][bottom][index][help]  */
 198 {
 199     gchar **groups, **group_iterator;
 200     GString *buffer;
 201 
 202     buffer = g_string_new ("");
 203     groups = mc_config_get_groups (data, NULL);
 204 
 205     for (group_iterator = groups; *group_iterator != NULL; group_iterator++)
 206     {
 207         char *serialized_str;
 208         gchar **params, **param_iterator;
 209 
 210         serialized_str = mc_serialize_str ('g', *group_iterator, error);
 211         if (serialized_str == NULL)
 212         {
 213             g_string_free (buffer, TRUE);
 214             g_strfreev (groups);
 215             return NULL;
 216         }
 217         g_string_append (buffer, serialized_str);
 218         g_free (serialized_str);
 219 
 220         params = mc_config_get_keys (data, *group_iterator, NULL);
 221 
 222         for (param_iterator = params; *param_iterator != NULL; param_iterator++)
 223         {
 224             char *value;
 225 
 226             serialized_str = mc_serialize_str ('p', *param_iterator, error);
 227             if (serialized_str == NULL)
 228             {
 229                 g_string_free (buffer, TRUE);
 230                 g_strfreev (params);
 231                 g_strfreev (groups);
 232                 return NULL;
 233             }
 234             g_string_append (buffer, serialized_str);
 235             g_free (serialized_str);
 236 
 237             value = mc_config_get_string_raw (data, *group_iterator, *param_iterator, "");
 238             serialized_str = mc_serialize_str ('v', value, error);
 239             g_free (value);
 240 
 241             if (serialized_str == NULL)
 242             {
 243                 g_string_free (buffer, TRUE);
 244                 g_strfreev (params);
 245                 g_strfreev (groups);
 246                 return NULL;
 247             }
 248 
 249             g_string_append (buffer, serialized_str);
 250             g_free (serialized_str);
 251         }
 252 
 253         g_strfreev (params);
 254     }
 255 
 256     g_strfreev (groups);
 257 
 258     return g_string_free (buffer, FALSE);
 259 }
 260 
 261 /* --------------------------------------------------------------------------------------------- */
 262 /**
 263  * Deserialize string to mc_config_t object
 264  *
 265  * @param data data for serialization
 266  * @param error contain pointer to object for handle error code and message
 267  *
 268  * @return newly allocated mc_config_t object
 269  */
 270 
 271 #define FUNC_NAME "mc_deserialize_config()"
 272 #define prepend_error_and_exit()                                                                   \
 273     {                                                                                              \
 274         prepend_error_message (error, FUNC_NAME " at %zu", current_position + 1);                  \
 275         mc_config_deinit (ret_data);                                                               \
 276         return NULL;                                                                               \
 277     }
 278 
 279 mc_config_t *
 280 mc_deserialize_config (const char *data, GError **error)
     /* [previous][next][first][last][top][bottom][index][help]  */
 281 {
 282     char *current_group = NULL, *current_param = NULL, *current_value = NULL;
 283     size_t current_position = 0;
 284     mc_config_t *ret_data;
 285     enum automat_status
 286     {
 287         WAIT_GROUP,
 288         WAIT_PARAM,
 289         WAIT_VALUE
 290     } current_status = WAIT_GROUP;
 291 
 292     ret_data = mc_config_init (NULL, FALSE);
 293 
 294     while (data != NULL)
 295     {
 296         if ((current_status == WAIT_GROUP) && (*data == 'p') && (current_group != NULL))
 297             current_status = WAIT_PARAM;
 298 
 299         switch (current_status)
 300         {
 301         case WAIT_GROUP:
 302             g_free (current_group);
 303 
 304             current_group = mc_deserialize_str ('g', data, error);
 305             if (current_group == NULL)
 306                 prepend_error_and_exit ();
 307 
 308             data = go_to_end_of_serialized_string (data, current_group, &current_position);
 309             current_status = WAIT_PARAM;
 310             break;
 311         case WAIT_PARAM:
 312             g_free (current_param);
 313 
 314             current_param = mc_deserialize_str ('p', data, error);
 315             if (current_param == NULL)
 316             {
 317                 g_free (current_group);
 318                 prepend_error_and_exit ();
 319             }
 320 
 321             data = go_to_end_of_serialized_string (data, current_param, &current_position);
 322             current_status = WAIT_VALUE;
 323             break;
 324         case WAIT_VALUE:
 325             current_value = mc_deserialize_str ('v', data, error);
 326             if (current_value == NULL)
 327             {
 328                 g_free (current_group);
 329                 g_free (current_param);
 330                 prepend_error_and_exit ();
 331             }
 332             mc_config_set_string (ret_data, current_group, current_param, current_value);
 333 
 334             data = go_to_end_of_serialized_string (data, current_value, &current_position);
 335             g_free (current_value);
 336             current_status = WAIT_GROUP;
 337             break;
 338         default:
 339             break;
 340         }
 341     }
 342     g_free (current_group);
 343     g_free (current_param);
 344 
 345     return ret_data;
 346 }
 347 
 348 #undef FUNC_NAME
 349 
 350 /* --------------------------------------------------------------------------------------------- */

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