3 #endif /* ifdef HAVE_CONFIG_H */
11 #ifdef HAVE_NETINET_IN_H
12 # include <netinet/in.h>
13 #endif /* ifdef HAVE_NETINET_IN_H */
16 # include <winsock2.h>
17 #endif /* ifdef _WIN32 */
22 #include "Eet_private.h"
25 * routines for doing data -> struct and struct -> data conversion
45 * multiple entries ordered as...
47 * fixed size array [ of basic types ]
48 * variable size array [ of basic types ]
49 * linked list [ of basic types ]
50 * hash table [ of basic types ]
52 * need to provide builder/accessor funcs for:
64 typedef struct _Eet_Data_Element Eet_Data_Element;
65 typedef struct _Eet_Data_Basic_Type_Codec Eet_Data_Basic_Type_Codec;
66 typedef struct _Eet_Data_Group_Type_Codec Eet_Data_Group_Type_Codec;
67 typedef struct _Eet_Data_Chunk Eet_Data_Chunk;
68 typedef struct _Eet_Data_Stream Eet_Data_Stream;
69 typedef struct _Eet_Data_Descriptor_Hash Eet_Data_Descriptor_Hash;
70 typedef struct _Eet_Data_Encode_Hash_Info Eet_Data_Encode_Hash_Info;
71 typedef struct _Eet_Free Eet_Free;
72 typedef struct _Eet_Free_Context Eet_Free_Context;
73 typedef struct _Eet_Variant_Unknow Eet_Variant_Unknow;
78 * Eet_Data_Basic_Type_Codec (Coder, Decoder)
79 * Eet_Data_Group_Type_Codec (Coder, Decoder)
81 struct _Eet_Data_Basic_Type_Codec
85 int (*get)(const Eet_Dictionary *ed,
89 void * (*put)(Eet_Dictionary *ed, const void *src, int *size_ret);
92 struct _Eet_Data_Group_Type_Codec
94 int (*get)(Eet_Free_Context *context,
95 const Eet_Dictionary *ed,
96 Eet_Data_Descriptor *edd,
97 Eet_Data_Element *ede,
98 Eet_Data_Chunk *echnk,
104 void (*put)(Eet_Dictionary *ed,
105 Eet_Data_Descriptor *edd,
106 Eet_Data_Element *ede,
111 struct _Eet_Data_Chunk
119 unsigned char group_type;
122 struct _Eet_Data_Stream
129 struct _Eet_Data_Descriptor_Hash
131 Eet_Data_Element *element;
132 Eet_Data_Descriptor_Hash *next;
135 struct _Eet_Data_Descriptor
138 const Eet_Dictionary *ed;
142 void * (*mem_alloc)(size_t size);
143 void (*mem_free)(void *mem);
144 char * (*str_alloc)(const char *str);
145 char * (*str_direct_alloc)(const char *str);
146 void (*str_free)(const char *str);
147 void (*str_direct_free)(const char *str);
148 void * (*list_next)(void *l);
149 void * (*list_append)(void *l, void *d);
150 void * (*list_data)(void *l);
151 void * (*list_free)(void *l);
152 void (*hash_foreach)(void *h,
158 void * (*hash_add)(void *h, const char *k, void *d);
159 void (*hash_free)(void *h);
160 const char *(*type_get)(const void *data, Eina_Bool *unknow);
161 Eina_Bool (*type_set)(const char *type,
164 void * (*array_alloc)(size_t size);
165 void (*array_free)(void *mem);
170 Eet_Data_Element *set;
174 Eet_Data_Descriptor_Hash *buckets;
178 Eina_Bool unified_type : 1;
183 struct _Eet_Data_Element
186 const char *counter_name;
187 const char *directory_name_ptr;
188 Eet_Data_Descriptor *subtype;
189 int offset; /* offset in bytes from the base element */
190 int count; /* number of elements for a fixed array */
191 int counter_offset; /* for a variable array we need the offset of the count variable */
192 unsigned char type; /* EET_T_XXX */
193 unsigned char group_type; /* EET_G_XXX */
196 struct _Eet_Data_Encode_Hash_Info
199 Eet_Data_Element *ede;
203 #define EET_FREE_COUNT 256
207 Eina_Array list[EET_FREE_COUNT];
210 struct _Eet_Free_Context
213 Eet_Free freelist_array;
214 Eet_Free freelist_list;
215 Eet_Free freelist_hash;
216 Eet_Free freelist_str;
217 Eet_Free freelist_direct_str;
220 struct _Eet_Variant_Unknow
231 eet_free_context_init(Eet_Free_Context *context);
233 eet_free_context_shutdown(Eet_Free_Context *context);
236 eet_data_get_char(const Eet_Dictionary *ed,
241 eet_data_put_char(Eet_Dictionary *ed,
245 eet_data_get_short(const Eet_Dictionary *ed,
250 eet_data_put_short(Eet_Dictionary *ed,
254 eet_data_get_int(const Eet_Dictionary *ed,
259 eet_data_put_int(Eet_Dictionary *ed,
263 eet_data_get_long_long(const Eet_Dictionary *ed,
268 eet_data_put_long_long(Eet_Dictionary *ed,
272 eet_data_get_float(const Eet_Dictionary *ed,
277 eet_data_put_float(Eet_Dictionary *ed,
281 eet_data_get_double(const Eet_Dictionary *ed,
286 eet_data_put_double(Eet_Dictionary *ed,
290 eet_data_get_f32p32(const Eet_Dictionary *ed,
295 eet_data_put_f32p32(Eet_Dictionary *ed,
299 eet_data_get_f16p16(const Eet_Dictionary *ed,
304 eet_data_put_f16p16(Eet_Dictionary *ed,
308 eet_data_get_f8p24(const Eet_Dictionary *ed,
313 eet_data_put_f8p24(Eet_Dictionary *ed,
317 eet_data_get_string(const Eet_Dictionary *ed,
322 eet_data_put_string(Eet_Dictionary *ed,
326 eet_data_get_istring(const Eet_Dictionary *ed,
331 eet_data_put_istring(Eet_Dictionary *ed,
335 eet_data_get_null(const Eet_Dictionary *ed,
340 eet_data_put_null(Eet_Dictionary *ed,
345 eet_data_get_type(const Eet_Dictionary *ed,
351 eet_data_put_type(Eet_Dictionary *ed,
357 eet_data_node_simple_type(int type,
362 eet_data_get_unknown(Eet_Free_Context *context,
363 const Eet_Dictionary *ed,
364 Eet_Data_Descriptor *edd,
365 Eet_Data_Element *ede,
366 Eet_Data_Chunk *echnk,
373 eet_data_put_unknown(Eet_Dictionary *ed,
374 Eet_Data_Descriptor *edd,
375 Eet_Data_Element *ede,
379 eet_data_put_array(Eet_Dictionary *ed,
380 Eet_Data_Descriptor *edd,
381 Eet_Data_Element *ede,
385 eet_data_get_array(Eet_Free_Context *context,
386 const Eet_Dictionary *ed,
387 Eet_Data_Descriptor *edd,
388 Eet_Data_Element *ede,
389 Eet_Data_Chunk *echnk,
396 eet_data_get_list(Eet_Free_Context *context,
397 const Eet_Dictionary *ed,
398 Eet_Data_Descriptor *edd,
399 Eet_Data_Element *ede,
400 Eet_Data_Chunk *echnk,
407 eet_data_put_list(Eet_Dictionary *ed,
408 Eet_Data_Descriptor *edd,
409 Eet_Data_Element *ede,
413 eet_data_put_hash(Eet_Dictionary *ed,
414 Eet_Data_Descriptor *edd,
415 Eet_Data_Element *ede,
419 eet_data_get_hash(Eet_Free_Context *context,
420 const Eet_Dictionary *ed,
421 Eet_Data_Descriptor *edd,
422 Eet_Data_Element *ede,
423 Eet_Data_Chunk *echnk,
430 eet_data_put_union(Eet_Dictionary *ed,
431 Eet_Data_Descriptor *edd,
432 Eet_Data_Element *ede,
436 eet_data_get_union(Eet_Free_Context *context,
437 const Eet_Dictionary *ed,
438 Eet_Data_Descriptor *edd,
439 Eet_Data_Element *ede,
440 Eet_Data_Chunk *echnk,
447 eet_data_put_variant(Eet_Dictionary *ed,
448 Eet_Data_Descriptor *edd,
449 Eet_Data_Element *ede,
453 eet_data_get_variant(Eet_Free_Context *context,
454 const Eet_Dictionary *ed,
455 Eet_Data_Descriptor *edd,
456 Eet_Data_Element *ede,
457 Eet_Data_Chunk *echnk,
465 eet_data_chunk_get(const Eet_Dictionary *ed,
466 Eet_Data_Chunk *chnk,
469 static Eet_Data_Chunk *
470 eet_data_chunk_new(void *data,
476 eet_data_chunk_free(Eet_Data_Chunk *chnk);
478 static Eet_Data_Stream *
479 eet_data_stream_new(void);
481 eet_data_stream_write(Eet_Data_Stream *ds,
485 eet_data_stream_free(Eet_Data_Stream *ds);
488 eet_data_chunk_put(Eet_Dictionary *ed,
489 Eet_Data_Chunk *chnk,
490 Eet_Data_Stream *ds);
493 eet_data_descriptor_encode_hash_cb(void *hash,
497 static void *_eet_data_descriptor_encode(Eet_Dictionary *ed,
498 Eet_Data_Descriptor *edd,
501 static void *_eet_data_descriptor_decode(Eet_Free_Context *context,
502 const Eet_Dictionary *ed,
503 Eet_Data_Descriptor *edd,
511 static const Eet_Data_Basic_Type_Codec eet_basic_codec[] =
513 {sizeof(char), "char", eet_data_get_char, eet_data_put_char },
514 {sizeof(short), "short", eet_data_get_short, eet_data_put_short },
515 {sizeof(int), "int", eet_data_get_int, eet_data_put_int },
516 {sizeof(long long), "long_long", eet_data_get_long_long, eet_data_put_long_long},
517 {sizeof(float), "float", eet_data_get_float, eet_data_put_float },
518 {sizeof(double), "double", eet_data_get_double, eet_data_put_double },
519 {sizeof(char), "uchar", eet_data_get_char, eet_data_put_char },
520 {sizeof(short), "ushort", eet_data_get_short, eet_data_put_short },
521 {sizeof(int), "uint", eet_data_get_int, eet_data_put_int },
522 {sizeof(long long), "ulong_long", eet_data_get_long_long, eet_data_put_long_long},
523 {sizeof(char *), "string", eet_data_get_string, eet_data_put_string },
524 {sizeof(char *), "inlined", eet_data_get_istring, eet_data_put_istring },
525 {sizeof(void *), "NULL", eet_data_get_null, eet_data_put_null },
526 {sizeof(Eina_F32p32), "f32p32", eet_data_get_f32p32, eet_data_put_f32p32 },
527 {sizeof(Eina_F16p16), "f16p16", eet_data_get_f16p16, eet_data_put_f16p16 },
528 {sizeof(Eina_F8p24), "f8p24", eet_data_get_f8p24, eet_data_put_f8p24 }
531 static const Eet_Data_Group_Type_Codec eet_group_codec[] =
533 { eet_data_get_unknown, eet_data_put_unknown },
534 { eet_data_get_array, eet_data_put_array },
535 { eet_data_get_array, eet_data_put_array },
536 { eet_data_get_list, eet_data_put_list },
537 { eet_data_get_hash, eet_data_put_hash },
538 { eet_data_get_union, eet_data_put_union },
539 { eet_data_get_variant, eet_data_put_variant }
542 static int _eet_data_words_bigendian = -1;
546 #define SWAP64(x) (x) = \
547 ((((unsigned long long)(x) & 0x00000000000000ffULL) << 56) | \
548 (((unsigned long long)(x) & 0x000000000000ff00ULL) << 40) | \
549 (((unsigned long long)(x) & 0x0000000000ff0000ULL) << 24) | \
550 (((unsigned long long)(x) & 0x00000000ff000000ULL) << 8) | \
551 (((unsigned long long)(x) & 0x000000ff00000000ULL) >> 8) | \
552 (((unsigned long long)(x) & 0x0000ff0000000000ULL) >> 24) | \
553 (((unsigned long long)(x) & 0x00ff000000000000ULL) >> 40) | \
554 (((unsigned long long)(x) & 0xff00000000000000ULL) >> 56))
555 #define SWAP32(x) (x) = \
556 ((((int)(x) & 0x000000ff) << 24) | \
557 (((int)(x) & 0x0000ff00) << 8) | \
558 (((int)(x) & 0x00ff0000) >> 8) | \
559 (((int)(x) & 0xff000000) >> 24))
560 #define SWAP16(x) (x) = \
561 ((((short)(x) & 0x00ff) << 8) | \
562 (((short)(x) & 0xff00) >> 8))
566 #endif /* ifdef CONV8 */
569 #endif /* ifdef CONV16 */
572 #endif /* ifdef CONV32 */
575 #endif /* ifdef CONV64 */
578 #define CONV16(x) {if (_eet_data_words_bigendian) {SWAP16(x); }}
579 #define CONV32(x) {if (_eet_data_words_bigendian) {SWAP32(x); }}
580 #define CONV64(x) {if (_eet_data_words_bigendian) {SWAP64(x); }}
582 #define IS_SIMPLE_TYPE(Type) (Type > EET_T_UNKNOW && Type < EET_T_LAST)
583 #define IS_POINTER_TYPE(Type) (Type >= EET_T_STRING && Type <= EET_T_NULL)
585 #define POINTER_TYPE_DECODE(Context, \
597 ___r = eet_data_get_unknown(Context, \
601 Type, EET_G_UNKNOWN, \
603 if (!___r) { goto Label; } \
606 #define STRUCT_TYPE_DECODE(Data_Ret, Context, Ed, Ede, Data, Size, SubSize, Label) \
608 Data_Ret = _eet_data_descriptor_decode(Context, \
613 SubSize > 0 ? Data_Ret : NULL, \
615 if (!Data_Ret) { goto Label; } \
618 #define EET_I_STRING 1 << 4
619 #define EET_I_INLINED_STRING 2 << 4
620 #define EET_I_NULL 3 << 4
622 #define EET_MAGIC_VARIANT 0xF1234BC
627 eet_data_get_char(const Eet_Dictionary *ed __UNUSED__,
634 if (((char *)src + sizeof(char)) > (char *)src_end)
645 eet_data_put_char(Eet_Dictionary *ed __UNUSED__,
651 d = (char *)malloc(sizeof(char));
658 *size_ret = sizeof(char);
664 eet_data_get_short(const Eet_Dictionary *ed __UNUSED__,
671 if (((char *)src + sizeof(short)) > (char *)src_end)
674 memcpy(dst, src, sizeof(short));
677 return sizeof(short);
681 eet_data_put_short(Eet_Dictionary *ed __UNUSED__,
687 d = (short *)malloc(sizeof(short));
694 *size_ret = sizeof(short);
700 eet_data_get_int(const Eet_Dictionary *ed __UNUSED__,
707 if (((char *)src + sizeof(int)) > (char *)src_end)
710 memcpy(dst, src, sizeof(int));
717 eet_data_put_int(Eet_Dictionary *ed __UNUSED__,
723 d = (int *)malloc(sizeof(int));
730 *size_ret = sizeof(int);
736 eet_data_get_long_long(const Eet_Dictionary *ed __UNUSED__,
741 unsigned long long *d;
743 if (((char *)src + sizeof(unsigned long long)) > (char *)src_end)
746 memcpy(dst, src, sizeof(unsigned long long));
747 d = (unsigned long long *)dst;
749 return sizeof(unsigned long long);
753 eet_data_put_long_long(Eet_Dictionary *ed __UNUSED__,
757 unsigned long long *s, *d;
759 d = (unsigned long long *)malloc(sizeof(unsigned long long));
763 s = (unsigned long long *)src;
766 *size_ret = sizeof(unsigned long long);
772 eet_data_get_string_hash(const Eet_Dictionary *ed,
780 if (eet_data_get_int(ed, src, src_end, &idx) < 0)
783 return eet_dictionary_string_get_hash(ed, idx);
790 eet_data_get_string(const Eet_Dictionary *ed,
804 if (eet_data_get_int(ed, src, src_end, &idx) < 0)
807 str = eet_dictionary_string_get_char(ed, idx);
812 return eet_dictionary_string_get_size(ed, idx);
823 return strlen(s) + 1;
827 eet_data_put_string(Eet_Dictionary *ed,
839 str = *((const char **)src);
843 idx = eet_dictionary_string_add(ed, str);
847 return eet_data_put_int(ed, &idx, size_ret);
850 s = (char *)(*((char **)src));
859 memcpy(d, s, len + 1);
864 /* ALWAYS INLINED STRING TYPE */
866 eet_data_get_istring(const Eet_Dictionary *ed __UNUSED__,
871 return eet_data_get_string(NULL, src, src_end, dst);
875 eet_data_put_istring(Eet_Dictionary *ed __UNUSED__,
879 return eet_data_put_string(NULL, src, size_ret);
882 /* ALWAYS NULL TYPE */
884 eet_data_get_null(const Eet_Dictionary *ed __UNUSED__,
885 const void *src __UNUSED__,
886 const void *src_end __UNUSED__,
898 eet_data_put_null(Eet_Dictionary *ed __UNUSED__,
899 const void *src __UNUSED__,
907 * Fast lookups of simple doubles/floats.
909 * These aren't properly a cache because they don't store pre-calculated
910 * values, but have a so simple math that is almost as fast.
913 _eet_data_float_cache_get(const char *s,
917 /* fast handle of simple case 0xMp+E*/
918 if ((len == 6) && (s[0] == '0') && (s[1] == 'x') && (s[3] == 'p'))
920 int mantisse = (s[2] >= 'a') ? (s[2] - 'a' + 10) : (s[2] - '0');
921 int exponent = (s[5] - '0');
924 *d = (float)(mantisse << exponent);
926 *d = (float)mantisse / (float)(1 << exponent);
935 _eet_data_double_cache_get(const char *s,
939 /* fast handle of simple case 0xMp+E*/
940 if ((len == 6) && (s[0] == '0') && (s[1] == 'x') && (s[3] == 'p'))
942 int mantisse = (s[2] >= 'a') ? (s[2] - 'a' + 10) : (s[2] - '0');
943 int exponent = (s[5] - '0');
946 *d = (double)(mantisse << exponent);
948 *d = (double)mantisse / (double)(1 << exponent);
958 eet_data_get_float(const Eet_Dictionary *ed,
974 s = (const char *)src;
977 while ((p < (const char *)src_end) && (*p != 0)) {len++; p++; }
979 if (_eet_data_float_cache_get(s, len, d) != 0)
982 if (eina_convert_atod(s, len, &mantisse, &exponent) == EINA_FALSE)
985 *d = (float)ldexp((double)mantisse, exponent);
990 if (eet_data_get_int(ed, src, src_end, &idx) < 0)
993 if (!eet_dictionary_string_get_float(ed, idx, d))
1000 eet_data_put_float(Eet_Dictionary *ed,
1007 eina_convert_dtoa((double)(*(float *)src), buf);
1015 d = malloc(len + 1);
1019 memcpy(d, buf, len + 1);
1020 *size_ret = len + 1;
1024 idx = eet_dictionary_string_add(ed, buf);
1028 return eet_data_put_int(ed, &idx, size_ret);
1033 eet_data_get_double(const Eet_Dictionary *ed,
1035 const void *src_end,
1046 long long mantisse = 0;
1050 s = (const char *)src;
1053 while ((p < (const char *)src_end) && (*p != 0)) {len++; p++; }
1055 if (_eet_data_double_cache_get(s, len, d) != 0)
1058 if (eina_convert_atod(s, len, &mantisse, &exponent) == EINA_FALSE)
1061 *d = ldexp((double)mantisse, exponent);
1066 if (eet_data_get_int(ed, src, src_end, &idx) < 0)
1069 if (!eet_dictionary_string_get_double(ed, idx, d))
1076 eet_data_put_double(Eet_Dictionary *ed,
1083 eina_convert_dtoa((double)(*(double *)src), buf);
1091 d = malloc(len + 1);
1095 memcpy(d, buf, len + 1);
1096 *size_ret = len + 1;
1101 idx = eet_dictionary_string_add(ed, buf);
1105 return eet_data_put_int(ed, &idx, size_ret);
1109 eet_data_get_f32p32(const Eet_Dictionary *ed,
1111 const void *src_end,
1117 fp = (Eina_F32p32 *)dst;
1124 s = (const char *)src;
1127 while ((p < (const char *)src_end) && (*p != 0)) { len++; p++; }
1129 if (!(eina_convert_atofp(s, len, fp)))
1135 if (eet_data_get_int(ed, src, src_end, &idx) < 0)
1138 if (!eet_dictionary_string_get_fp(ed, idx, fp))
1145 eet_data_put_f32p32(Eet_Dictionary *ed,
1152 eina_convert_fptoa((Eina_F32p32)(*(Eina_F32p32 *)src), buf);
1160 d = malloc(len + 1);
1164 memcpy(d, buf, len + 1);
1165 *size_ret = len + 1;
1170 idx = eet_dictionary_string_add(ed, buf);
1174 return eet_data_put_int(ed, &idx, size_ret);
1178 eet_data_get_f16p16(const Eet_Dictionary *ed,
1180 const void *src_end,
1186 fp = (Eina_F16p16 *)dst;
1188 if (eet_data_get_f32p32(ed, src, src_end, &tmp) < 0)
1191 *fp = eina_f32p32_to_f16p16(tmp);
1196 eet_data_put_f16p16(Eet_Dictionary *ed,
1202 tmp = eina_f16p16_to_f32p32((Eina_F16p16)(*(Eina_F16p16 *)src));
1203 return eet_data_put_f32p32(ed, &tmp, size_ret);
1207 eet_data_get_f8p24(const Eet_Dictionary *ed,
1209 const void *src_end,
1215 fp = (Eina_F8p24 *)dst;
1217 if (eet_data_get_f32p32(ed, src, src_end, &tmp) < 0)
1220 *fp = eina_f32p32_to_f8p24(tmp);
1225 eet_data_put_f8p24(Eet_Dictionary *ed,
1231 tmp = eina_f8p24_to_f32p32((Eina_F8p24)(*(Eina_F8p24 *)src));
1232 return eet_data_put_f32p32(ed, &tmp, size_ret);
1236 eet_data_get_type(const Eet_Dictionary *ed,
1239 const void *src_end,
1244 ret = eet_basic_codec[type - 1].get(ed, src, src_end, dest);
1248 static inline void *
1249 eet_data_put_type(Eet_Dictionary *ed,
1256 ret = eet_basic_codec[type - 1].put(ed, src, size_ret);
1260 static inline Eina_Bool
1261 eet_data_type_match(int type1,
1267 /* Note: All floating point type are equivalent and could be read
1268 without problem by any other floating point getter. */
1299 * char[4] = "CHnK"; // untyped data ... or
1300 * char[4] = "CHKx"; // typed data - x == type
1302 * int = chunk size (including magic string);
1303 * char[] = chunk magic/name string (0 byte terminated);
1304 * ... sub-chunks (a chunk can contain chuncks recusrively) ...
1306 * ... payload data ...
1311 eet_data_chunk_get(const Eet_Dictionary *ed,
1312 Eet_Data_Chunk *chnk,
1331 if ((s[0] != 'C') || (s[1] != 'H') || (s[2] != 'K'))
1334 chnk->type = (unsigned char)(s[3]);
1335 if (chnk->type >= EET_I_LIMIT)
1338 ((chnk->type - EET_I_LIMIT) & 0xF) + EET_G_UNKNOWN;
1339 switch ((chnk->type - EET_I_LIMIT) & 0xF0)
1341 #define EET_UNMATCH_TYPE(Type) \
1342 case EET_I_ ## Type: chnk->type = EET_T_ ## Type; break;
1344 EET_UNMATCH_TYPE(STRING);
1345 EET_UNMATCH_TYPE(INLINED_STRING);
1346 EET_UNMATCH_TYPE(NULL);
1352 else if (chnk->type > EET_T_LAST)
1354 chnk->group_type = chnk->type;
1355 chnk->type = EET_T_UNKNOW;
1358 chnk->group_type = EET_G_UNKNOWN;
1359 if ((chnk->type >= EET_T_LAST) ||
1360 (chnk->group_type >=
1364 chnk->group_type = 0;
1367 else if ((s[0] != 'C') || (s[1] != 'H') || (s[2] != 'n') || (s[3] != 'K'))
1370 ret1 = eet_data_get_type(ed, EET_T_INT, (s + 4), (s + size), &(chnk->size));
1375 if ((chnk->size < 0) || ((chnk->size + 8) > size))
1378 ret2 = eet_data_get_type(ed, EET_T_STRING, (s + 8), (s + size), &(chnk->name));
1386 chnk->hash = eet_data_get_string_hash(ed, (s + 8), (s + size));
1390 chnk->data = (char *)src + 4 + ret1 + sizeof(int);
1391 chnk->size -= sizeof(int);
1395 chnk->data = (char *)src + 4 + ret1 + chnk->len;
1396 chnk->size -= chnk->len;
1402 static inline Eet_Data_Chunk *
1403 eet_data_chunk_new(void *data,
1409 Eet_Data_Chunk *chnk;
1414 chnk = calloc(1, sizeof(Eet_Data_Chunk));
1418 /* Note: Another security, so older eet library could read file
1419 saved with fixed point value. */
1420 if (type == EET_T_F32P32
1421 || type == EET_T_F16P16
1422 || type == EET_T_F8P24)
1423 type = EET_T_DOUBLE;
1425 chnk->name = strdup(name);
1426 chnk->len = strlen(name) + 1;
1430 chnk->group_type = group_type;
1435 eet_data_chunk_free(Eet_Data_Chunk *chnk)
1443 static inline Eet_Data_Stream *
1444 eet_data_stream_new(void)
1446 Eet_Data_Stream *ds;
1448 ds = calloc(1, sizeof(Eet_Data_Stream));
1456 eet_data_stream_free(Eet_Data_Stream *ds)
1465 eet_data_stream_flush(Eet_Data_Stream *ds)
1471 eet_data_stream_write(Eet_Data_Stream *ds,
1477 if ((ds->pos + size) > ds->size)
1479 ds->data = realloc(ds->data, ds->size + size + 512);
1487 ds->size = ds->size + size + 512;
1491 memcpy(p + ds->pos, data, size);
1496 eet_data_chunk_put(Eet_Dictionary *ed,
1497 Eet_Data_Chunk *chnk,
1498 Eet_Data_Stream *ds)
1505 unsigned char buf[4] = "CHK";
1507 /* disable this check - it will allow empty chunks to be written. this is
1508 * right for corner-cases when y have a struct with empty fields (empty
1509 * strings or empty list ptrs etc.) */
1510 /* if (!chnk->data && chnk->type != EET_T_NULL) return; */
1513 /* eet_data_stream_write(ds, "CHnK", 4);*/
1514 if (chnk->type != EET_T_UNKNOW)
1516 if (chnk->group_type != EET_G_UNKNOWN)
1518 int type = EET_I_LIMIT + chnk->group_type - EET_G_UNKNOWN;
1522 /* Only make sense with pointer type. */
1523 #define EET_MATCH_TYPE(Type) \
1524 case EET_T_ ## Type: type += EET_I_ ## Type; break;
1526 EET_MATCH_TYPE(STRING);
1527 EET_MATCH_TYPE(INLINED_STRING);
1528 EET_MATCH_TYPE(NULL);
1537 buf[3] = chnk->type;
1540 buf[3] = chnk->group_type;
1542 string = eet_data_put_string(ed, &chnk->name, &string_ret);
1546 /* size of chunk payload data + name */
1547 s = chnk->size + string_ret;
1548 size = eet_data_put_int(ed, &s, &size_ret);
1550 /* FIXME: If something goes wrong the resulting file will be corrupted. */
1554 eet_data_stream_write(ds, buf, 4);
1556 /* write chunk length */
1557 eet_data_stream_write(ds, size, size_ret);
1559 /* write chunk name */
1560 eet_data_stream_write(ds, string, string_ret);
1564 eet_data_stream_write(ds, chnk->data, chnk->size);
1574 _eet_descriptor_hash_new(Eet_Data_Descriptor *edd)
1578 edd->elements.hash.size = 1 << 6;
1579 edd->elements.hash.buckets = calloc(
1581 sizeof(Eet_Data_Descriptor_Hash) *
1582 edd->elements.hash.size);
1583 for (i = 0; i < edd->elements.num; i++)
1585 Eet_Data_Element *ede;
1588 ede = &(edd->elements.set[i]);
1589 hash = _eet_hash_gen((char *)ede->name, 6);
1590 if (!edd->elements.hash.buckets[hash].element)
1591 edd->elements.hash.buckets[hash].element = ede;
1594 Eet_Data_Descriptor_Hash *bucket;
1596 bucket = calloc(1, sizeof(Eet_Data_Descriptor_Hash));
1597 bucket->element = ede;
1598 bucket->next = edd->elements.hash.buckets[hash].next;
1599 edd->elements.hash.buckets[hash].next = bucket;
1605 _eet_descriptor_hash_free(Eet_Data_Descriptor *edd)
1609 for (i = 0; i < edd->elements.hash.size; i++)
1611 Eet_Data_Descriptor_Hash *bucket, *pbucket;
1613 bucket = edd->elements.hash.buckets[i].next;
1617 bucket = bucket->next;
1621 if (edd->elements.hash.buckets)
1622 free(edd->elements.hash.buckets);
1625 static Eet_Data_Element *
1626 _eet_descriptor_hash_find(Eet_Data_Descriptor *edd,
1630 Eet_Data_Descriptor_Hash *bucket;
1633 hash = _eet_hash_gen(name, 6);
1637 if (!edd->elements.hash.buckets[hash].element)
1639 When we use the dictionary as a source for chunk name, we will always
1640 have the same pointer in name. It's a good idea to just compare pointer
1641 instead of running strcmp on both string.
1644 if (edd->elements.hash.buckets[hash].element->directory_name_ptr == name)
1645 return edd->elements.hash.buckets[hash].element;
1647 if (!strcmp(edd->elements.hash.buckets[hash].element->name, name))
1649 edd->elements.hash.buckets[hash].element->directory_name_ptr = name;
1650 return edd->elements.hash.buckets[hash].element;
1653 bucket = edd->elements.hash.buckets[hash].next;
1656 if (bucket->element->directory_name_ptr == name)
1657 return bucket->element;
1659 if (!strcmp(bucket->element->name, name))
1661 bucket->element->directory_name_ptr = name;
1662 return bucket->element;
1665 bucket = bucket->next;
1671 _eet_mem_alloc(size_t size)
1673 return calloc(1, size);
1677 _eet_mem_free(void *mem)
1683 _eet_str_alloc(const char *str)
1689 _eet_str_free(const char *str)
1695 _eet_eina_hash_add_alloc(Eina_Hash *hash,
1700 hash = eina_hash_string_small_new(NULL);
1705 eina_hash_add(hash, key, data);
1710 _eet_eina_hash_direct_add_alloc(Eina_Hash *hash,
1715 hash = eina_hash_string_small_new(NULL);
1720 eina_hash_direct_add(hash, key, data);
1725 _eet_str_direct_alloc(const char *str)
1731 _eet_str_direct_free(const char *str __UNUSED__)
1736 _eet_eina_hash_foreach(void *hash,
1737 Eina_Hash_Foreach cb,
1741 eina_hash_foreach(hash, cb, fdata);
1745 _eet_eina_hash_free(void *hash)
1748 eina_hash_free(hash);
1753 eet_eina_stream_data_descriptor_class_set(Eet_Data_Descriptor_Class *eddc,
1754 /* When we change the structure content in the future, we need to handle old structure type too */
1755 unsigned int eddc_size,
1759 if (!eddc || !name || eddc_size != sizeof (Eet_Data_Descriptor_Class))
1764 eddc->version = EET_DATA_DESCRIPTOR_CLASS_VERSION;
1766 eddc->func.mem_alloc = _eet_mem_alloc;
1767 eddc->func.mem_free = _eet_mem_free;
1768 eddc->func.str_alloc = (char *(*)(const char *))eina_stringshare_add;
1769 eddc->func.str_free = eina_stringshare_del;
1770 eddc->func.list_next = (void *(*)(void *))eina_list_next;
1771 eddc->func.list_append = (void *(*)(void *, void *))eina_list_append;
1772 eddc->func.list_data = (void *(*)(void *))eina_list_data_get;
1773 eddc->func.list_free = (void *(*)(void *))eina_list_free;
1774 eddc->func.hash_foreach = (void (*)(void *, int (*)(void *, const char *, void *, void *), void *))_eet_eina_hash_foreach;
1775 eddc->func.hash_add = (void *(*)(void *, const char *, void *))_eet_eina_hash_add_alloc;
1776 eddc->func.hash_free = (void (*)(void *))_eet_eina_hash_free;
1778 /* This will cause an ABI incompatibility */
1779 eddc->func.array_alloc = _eet_mem_alloc;
1780 eddc->func.array_free = _eet_mem_free;
1786 eet_eina_file_data_descriptor_class_set(Eet_Data_Descriptor_Class *eddc,
1787 /* When we change the structure content in the future, we need to handle old structure type too */
1788 unsigned int eddc_size,
1792 if (!eet_eina_stream_data_descriptor_class_set(eddc, eddc_size, name, size))
1795 eddc->version = EET_DATA_DESCRIPTOR_CLASS_VERSION;
1797 eddc->func.hash_add = (void *(*)(void *, const char *, void *))_eet_eina_hash_direct_add_alloc;
1798 eddc->func.str_direct_alloc = _eet_str_direct_alloc;
1799 eddc->func.str_direct_free = _eet_str_direct_free;
1804 static Eet_Data_Descriptor *
1805 _eet_data_descriptor_new(const Eet_Data_Descriptor_Class *eddc,
1808 Eet_Data_Descriptor *edd;
1813 edd = calloc(1, sizeof (Eet_Data_Descriptor));
1817 edd->name = eddc->name;
1819 edd->size = eddc->size;
1820 edd->func.mem_alloc = _eet_mem_alloc;
1821 edd->func.mem_free = _eet_mem_free;
1822 edd->func.str_alloc = _eet_str_alloc;
1823 edd->func.str_free = _eet_str_free;
1824 if (eddc->func.mem_alloc)
1825 edd->func.mem_alloc = eddc->func.mem_alloc;
1827 if (eddc->func.mem_free)
1828 edd->func.mem_free = eddc->func.mem_free;
1830 if (eddc->func.str_alloc)
1831 edd->func.str_alloc = eddc->func.str_alloc;
1833 if (eddc->func.str_free)
1834 edd->func.str_free = eddc->func.str_free;
1836 edd->func.list_next = eddc->func.list_next;
1837 edd->func.list_append = eddc->func.list_append;
1838 edd->func.list_data = eddc->func.list_data;
1839 edd->func.list_free = eddc->func.list_free;
1840 edd->func.hash_foreach = eddc->func.hash_foreach;
1841 edd->func.hash_add = eddc->func.hash_add;
1842 edd->func.hash_free = eddc->func.hash_free;
1844 if (eddc->version > 1 && version > 1)
1846 edd->func.str_direct_alloc = eddc->func.str_direct_alloc;
1847 edd->func.str_direct_free = eddc->func.str_direct_free;
1850 if (eddc->version > 2)
1852 edd->func.type_get = eddc->func.type_get;
1853 edd->func.type_set = eddc->func.type_set;
1856 if (eddc->version > 3)
1858 edd->func.array_alloc = eddc->func.array_alloc;
1859 edd->func.array_free = eddc->func.array_free;
1865 EAPI Eet_Data_Descriptor *
1866 eet_data_descriptor_new(const char *name,
1868 Eet_Descriptor_List_Next_Callback func_list_next,
1869 Eet_Descriptor_List_Append_Callback func_list_append,
1870 Eet_Descriptor_List_Data_Callback func_list_data,
1871 Eet_Descriptor_List_Free_Callback func_list_free,
1872 Eet_Descriptor_Hash_Foreach_Callback func_hash_foreach,
1873 Eet_Descriptor_Hash_Add_Callback func_hash_add,
1874 Eet_Descriptor_Hash_Free_Callback func_hash_free)
1876 Eet_Data_Descriptor_Class eddc;
1881 memset(&eddc, 0, sizeof (Eet_Data_Descriptor_Class));
1887 eddc.func.list_next = func_list_next;
1888 eddc.func.list_append = func_list_append;
1889 eddc.func.list_data = func_list_data;
1890 eddc.func.list_free = func_list_free;
1891 eddc.func.hash_foreach = func_hash_foreach;
1892 eddc.func.hash_add = func_hash_add;
1893 eddc.func.hash_free = func_hash_free;
1895 return _eet_data_descriptor_new(&eddc, 0);
1898 EAPI Eet_Data_Descriptor *
1899 eet_data_descriptor2_new(const Eet_Data_Descriptor_Class *eddc)
1901 return _eet_data_descriptor_new(eddc, 1);
1904 EAPI Eet_Data_Descriptor *
1905 eet_data_descriptor3_new(const Eet_Data_Descriptor_Class *eddc)
1907 return _eet_data_descriptor_new(eddc, 2);
1910 EAPI Eet_Data_Descriptor *
1911 eet_data_descriptor_stream_new(const Eet_Data_Descriptor_Class *eddc)
1913 return _eet_data_descriptor_new(eddc, 1);
1916 EAPI Eet_Data_Descriptor *
1917 eet_data_descriptor_file_new(const Eet_Data_Descriptor_Class *eddc)
1919 return _eet_data_descriptor_new(eddc, 2);
1923 eet_data_descriptor_free(Eet_Data_Descriptor *edd)
1928 _eet_descriptor_hash_free(edd);
1929 if (edd->elements.set)
1930 free(edd->elements.set);
1936 eet_data_descriptor_element_add(Eet_Data_Descriptor *edd,
1942 /* int counter_offset, */
1943 const char *counter_name /* FIXME: Useless should go on a major release */,
1944 Eet_Data_Descriptor *subtype)
1946 Eet_Data_Element *ede;
1947 Eet_Data_Element *tmp;
1949 /* UNION, VARIANT type would not work with simple type, we need a way to map the type. */
1950 if ((group_type == EET_G_UNION
1951 || group_type == EET_G_VARIANT)
1953 (type != EET_T_UNKNOW
1955 || !subtype->func.type_get
1956 || !subtype->func.type_set))
1959 /* VARIANT type will only work if the map only contains EET_G_*, but not UNION, VARIANT and ARRAY. */
1960 if (group_type == EET_G_VARIANT)
1964 for (i = 0; i < subtype->elements.num; ++i)
1965 if (subtype->elements.set[i].type != EET_T_UNKNOW
1966 && subtype->elements.set[i].group_type > EET_G_VAR_ARRAY
1967 && subtype->elements.set[i].group_type < EET_G_UNION)
1970 subtype->unified_type = EINA_TRUE;
1974 && subtype->unified_type
1975 && (type != EET_T_UNKNOW
1976 || group_type < EET_G_UNION))
1979 /* Sanity check done, let allocate ! */
1980 edd->elements.num++;
1981 tmp = realloc(edd->elements.set, edd->elements.num * sizeof(Eet_Data_Element));
1985 edd->elements.set = tmp;
1986 ede = &(edd->elements.set[edd->elements.num - 1]);
1988 ede->directory_name_ptr = NULL;
1991 * We do a special case when we do list,hash or whatever group of simple type.
1992 * Instead of handling it in encode/decode/dump/undump, we create an
1993 * implicit structure with only the simple type.
1995 if ((group_type > EET_G_UNKNOWN)
1996 && (group_type < EET_G_LAST)
1997 && (((type > EET_T_UNKNOW) && (type < EET_T_STRING))
1998 || ((type > EET_T_NULL) && (type < EET_T_LAST)))
2001 subtype = calloc(1, sizeof (Eet_Data_Descriptor));
2005 subtype->name = "implicit";
2006 subtype->size = eet_basic_codec[type - 1].size;
2007 memcpy(&subtype->func, &edd->func, sizeof(subtype->func));
2009 eet_data_descriptor_element_add(subtype,
2010 eet_basic_codec[type - 1].name,
2017 type = EET_T_UNKNOW;
2021 ede->group_type = group_type;
2022 ede->offset = offset;
2024 /* FIXME: For the time being, VAR_ARRAY, UNION and VARIANT will put the counter_offset in count. */
2025 ede->counter_offset = count;
2026 /* ede->counter_offset = counter_offset; */
2027 ede->counter_name = counter_name;
2029 ede->subtype = subtype;
2033 eet_data_read_cipher(Eet_File *ef,
2034 Eet_Data_Descriptor *edd,
2036 const char *cipher_key)
2038 const Eet_Dictionary *ed = NULL;
2039 const void *data = NULL;
2041 Eet_Free_Context context;
2042 int required_free = 0;
2045 ed = eet_dictionary_get(ef);
2048 data = eet_read_direct(ef, name, &size);
2053 data = eet_read_cipher(ef, name, &size, cipher_key);
2058 eet_free_context_init(&context);
2059 data_dec = _eet_data_descriptor_decode(&context, ed, edd, data, size, NULL, 0);
2060 eet_free_context_shutdown(&context);
2069 eet_data_node_read_cipher(Eet_File *ef,
2071 const char *cipher_key)
2073 const Eet_Dictionary *ed = NULL;
2074 const void *data = NULL;
2076 Eet_Free_Context context;
2077 int required_free = 0;
2080 ed = eet_dictionary_get(ef);
2083 data = eet_read_direct(ef, name, &size);
2088 data = eet_read_cipher(ef, name, &size, cipher_key);
2093 eet_free_context_init(&context);
2094 result = _eet_data_descriptor_decode(&context, ed, NULL, data, size, NULL, 0);
2095 eet_free_context_shutdown(&context);
2104 eet_data_read(Eet_File *ef,
2105 Eet_Data_Descriptor *edd,
2108 return eet_data_read_cipher(ef, edd, name, NULL);
2112 eet_data_write_cipher(Eet_File *ef,
2113 Eet_Data_Descriptor *edd,
2115 const char *cipher_key,
2124 ed = eet_dictionary_get(ef);
2126 data_enc = _eet_data_descriptor_encode(ed, edd, data, &size);
2130 val = eet_write_cipher(ef, name, data_enc, size, comp, cipher_key);
2136 eet_data_write(Eet_File *ef,
2137 Eet_Data_Descriptor *edd,
2142 return eet_data_write_cipher(ef, edd, name, NULL, data, comp);
2146 eet_free_context_init(Eet_Free_Context *context)
2150 memset(context, 0, sizeof (Eet_Free_Context));
2151 for (i = 0; i < EET_FREE_COUNT; ++i)
2153 eina_array_step_set(&context->freelist.list[i],
2154 sizeof (context->freelist.list[i]),
2156 eina_array_step_set(&context->freelist_array.list[i],
2157 sizeof (context->freelist.list[i]),
2159 eina_array_step_set(&context->freelist_list.list[i],
2160 sizeof (context->freelist.list[i]),
2162 eina_array_step_set(&context->freelist_hash.list[i],
2163 sizeof (context->freelist.list[i]),
2165 eina_array_step_set(&context->freelist_str.list[i],
2166 sizeof (context->freelist.list[i]),
2168 eina_array_step_set(&context->freelist_direct_str.list[i],
2169 sizeof (context->freelist.list[i]),
2175 eet_free_context_shutdown(Eet_Free_Context *context)
2179 for (i = 0; i < EET_FREE_COUNT; ++i)
2181 eina_array_flush(&context->freelist.list[i]);
2182 eina_array_flush(&context->freelist_array.list[i]);
2183 eina_array_flush(&context->freelist_list.list[i]);
2184 eina_array_flush(&context->freelist_hash.list[i]);
2185 eina_array_flush(&context->freelist_str.list[i]);
2186 eina_array_flush(&context->freelist_direct_str.list[i]);
2191 _eet_free_hash(void *data)
2194 __int64 ptr = (UINT_PTR)data;
2195 #else /* ifdef _WIN64 */
2196 unsigned long ptr = (unsigned long)(data);
2197 #endif /* ifdef _WIN64 */
2205 #if defined (_WIN64) || ((!defined (_WIN32)) && (LONG_BIT != 32))
2210 #endif /* if defined (_WIN64) || ((!defined (_WIN32)) && (LONG_BIT != 32)) */
2216 _eet_free_add(Eet_Free *ef,
2220 Eina_Array_Iterator it;
2224 hash = _eet_free_hash(data);
2226 EINA_ARRAY_ITER_NEXT(&ef->list[hash], i, track, it)
2230 eina_array_push(&ef->list[hash], data);
2235 _eet_free_del(Eet_Free *ef,
2239 Eina_Array_Iterator it;
2243 hash = _eet_free_hash(data);
2245 EINA_ARRAY_ITER_NEXT(&ef->list[hash], i, track, it)
2248 eina_array_data_set(&ef->list[hash], i, NULL);
2256 _eet_free_reset(Eet_Free *ef)
2263 for (i = 0; i < EET_FREE_COUNT; ++i)
2264 eina_array_clean(&ef->list[i]);
2268 _eet_free_ref(Eet_Free *ef)
2274 _eet_free_unref(Eet_Free *ef)
2279 #define _eet_freelist_add(Ctx, Data) _eet_free_add(&Ctx->freelist, Data);
2280 #define _eet_freelist_del(Ctx, Data) _eet_free_del(&Ctx->freelist, Data);
2281 #define _eet_freelist_reset(Ctx) _eet_free_reset(&Ctx->freelist);
2282 #define _eet_freelist_ref(Ctx) _eet_free_ref(&Ctx->freelist);
2283 #define _eet_freelist_unref(Ctx) _eet_free_unref(&Ctx->freelist);
2286 _eet_freelist_free(Eet_Free_Context *context,
2287 Eet_Data_Descriptor *edd)
2290 Eina_Array_Iterator it;
2294 if (context->freelist.ref > 0)
2297 for (j = 0; j < EET_FREE_COUNT; ++j)
2298 EINA_ARRAY_ITER_NEXT(&context->freelist.list[j], i, track, it)
2302 edd->func.mem_free(track);
2306 _eet_free_reset(&context->freelist);
2309 #define _eet_freelist_array_add(Ctx, Data) _eet_free_add(&Ctx->freelist_array, Data);
2310 #define _eet_freelist_array_del(Ctx, Data) _eet_free_del(&Ctx->freelist_array, Data);
2311 #define _eet_freelist_array_reset(Ctx) _eet_free_reset(&Ctx->freelist_array);
2312 #define _eet_freelist_array_ref(Ctx) _eet_free_ref(&Ctx->freelist_array);
2313 #define _eet_freelist_array_unref(Ctx) _eet_free_unref(&Ctx->freelist_array);
2316 _eet_freelist_array_free(Eet_Free_Context *context,
2317 Eet_Data_Descriptor *edd)
2320 Eina_Array_Iterator it;
2324 if (context->freelist_array.ref > 0)
2327 for (j = 0; j < EET_FREE_COUNT; ++j)
2328 EINA_ARRAY_ITER_NEXT(&context->freelist_array.list[j], i, track, it)
2333 if (edd->func.array_free)
2334 edd->func.array_free(track);
2336 edd->func.mem_free(track);
2341 _eet_free_reset(&context->freelist_array);
2344 #define _eet_freelist_list_add(Ctx, Data) _eet_free_add(&Ctx->freelist_list, Data);
2345 #define _eet_freelist_list_del(Ctx, Data) _eet_free_del(&Ctx->freelist_list, Data);
2346 #define _eet_freelist_list_reset(Ctx) _eet_free_reset(&Ctx->freelist_list);
2347 #define _eet_freelist_list_ref(Ctx) _eet_free_ref(&Ctx->freelist_list);
2348 #define _eet_freelist_list_unref(Ctx) _eet_free_unref(&Ctx->freelist_list);
2351 _eet_freelist_list_free(Eet_Free_Context *context,
2352 Eet_Data_Descriptor *edd)
2355 Eina_Array_Iterator it;
2359 if (context->freelist_list.ref > 0)
2362 for (j = 0; j < EET_FREE_COUNT; ++j)
2363 EINA_ARRAY_ITER_NEXT(&context->freelist_list.list[j], i, track, it)
2367 edd->func.list_free(*((void **)(track)));
2369 _eet_free_reset(&context->freelist_list);
2372 #define _eet_freelist_str_add(Ctx, Data) _eet_free_add(&Ctx->freelist_str, Data);
2373 #define _eet_freelist_str_del(Ctx, Data) _eet_free_del(&Ctx->freelist_str, Data);
2374 #define _eet_freelist_str_reset(Ctx) _eet_free_reset(&Ctx->freelist_str);
2375 #define _eet_freelist_str_ref(Ctx) _eet_free_ref(&Ctx->freelist_str);
2376 #define _eet_freelist_str_unref(Ctx) _eet_free_unref(&Ctx->freelist_str);
2379 _eet_freelist_str_free(Eet_Free_Context *context,
2380 Eet_Data_Descriptor *edd)
2383 Eina_Array_Iterator it;
2387 if (context->freelist_str.ref > 0)
2390 for (j = 0; j < EET_FREE_COUNT; ++j)
2391 EINA_ARRAY_ITER_NEXT(&context->freelist_str.list[j], i, track, it)
2395 edd->func.str_free(track);
2399 _eet_free_reset(&context->freelist_str);
2402 #define _eet_freelist_direct_str_add(Ctx, Data) _eet_free_add(&Ctx->freelist_direct_str, Data);
2403 #define _eet_freelist_direct_str_del(Ctx, Data) _eet_free_del(&Ctx->freelist_direct_str, Data);
2404 #define _eet_freelist_direct_str_reset(Ctx) _eet_free_reset(&Ctx->freelist_direct_str);
2405 #define _eet_freelist_direct_str_ref(Ctx) _eet_free_ref(&Ctx->freelist_direct_str);
2406 #define _eet_freelist_direct_str_unref(Ctx) _eet_free_unref(&Ctx->freelist_direct_str);
2409 _eet_freelist_direct_str_free(Eet_Free_Context *context,
2410 Eet_Data_Descriptor *edd)
2413 Eina_Array_Iterator it;
2417 if (context->freelist_direct_str.ref > 0)
2420 for (j = 0; j < EET_FREE_COUNT; ++j)
2421 EINA_ARRAY_ITER_NEXT(&context->freelist_str.list[j], i, track, it)
2425 edd->func.str_direct_free(track);
2429 _eet_free_reset(&context->freelist_direct_str);
2432 #define _eet_freelist_hash_add(Ctx, Data) _eet_free_add(&Ctx->freelist_hash, Data);
2433 #define _eet_freelist_hash_del(Ctx, Data) _eet_free_del(&Ctx->freelist_hash, Data);
2434 #define _eet_freelist_hash_reset(Ctx) _eet_free_reset(&Ctx->freelist_hash);
2435 #define _eet_freelist_hash_ref(Ctx) _eet_free_ref(&Ctx->freelist_hash);
2436 #define _eet_freelist_hash_unref(Ctx) _eet_free_unref(&Ctx->freelist_hash);
2439 _eet_freelist_hash_free(Eet_Free_Context *context,
2440 Eet_Data_Descriptor *edd)
2443 Eina_Array_Iterator it;
2447 if (context->freelist_hash.ref > 0)
2450 for (j = 0; j < EET_FREE_COUNT; ++j)
2451 EINA_ARRAY_ITER_NEXT(&context->freelist_hash.list[j], i, track, it)
2455 edd->func.hash_free(track);
2459 _eet_free_reset(&context->freelist_hash);
2463 _eet_freelist_all_ref(Eet_Free_Context *freelist_context)
2465 _eet_freelist_ref(freelist_context);
2466 _eet_freelist_str_ref(freelist_context);
2467 _eet_freelist_list_ref(freelist_context);
2468 _eet_freelist_hash_ref(freelist_context);
2469 _eet_freelist_direct_str_ref(freelist_context);
2473 _eet_freelist_all_unref(Eet_Free_Context *freelist_context)
2475 _eet_freelist_unref(freelist_context);
2476 _eet_freelist_str_unref(freelist_context);
2477 _eet_freelist_list_unref(freelist_context);
2478 _eet_freelist_hash_unref(freelist_context);
2479 _eet_freelist_direct_str_unref(freelist_context);
2483 eet_data_descriptor_encode_hash_cb(void *hash __UNUSED__,
2484 const char *cipher_key,
2489 Eet_Data_Encode_Hash_Info *edehi;
2490 Eet_Data_Stream *ds;
2491 Eet_Data_Element *ede;
2492 Eet_Data_Chunk *echnk;
2502 data = eet_data_put_type(ed,
2508 echnk = eet_data_chunk_new(data,
2513 eet_data_chunk_put(ed, echnk, ds);
2514 eet_data_chunk_free(echnk);
2519 EET_ASSERT(!((ede->type > EET_T_UNKNOW) && (ede->type < EET_T_STRING)), return );
2522 if (ede->type >= EET_T_STRING)
2523 eet_data_put_unknown(ed, NULL, ede, ds, &hdata);
2527 data = _eet_data_descriptor_encode(ed,
2534 echnk = eet_data_chunk_new(data,
2539 eet_data_chunk_put(ed, echnk, ds);
2540 eet_data_chunk_free(echnk);
2550 _eet_data_dump_token_get(const char *src,
2557 int tlen = 0, tsize = 0;
2559 #define TOK_ADD(x) \
2562 if (tlen >= tsize) \
2565 tok = realloc(tok, tsize); \
2567 tok[tlen - 1] = x; \
2570 for (p = src; *len > 0; p++, (*len)--)
2576 if ((p[0] == '\"') && (p > src) && (p[-1] != '\\'))
2578 else if ((p[0] == '\\') && (*len > 1) &&
2583 else if ((p[0] == '\\') && (p > src) && (p[-1] == '\\'))
2587 else if ((p[0] == '\\') && (*len > 1) && (p[1] == 'n'))
2591 else if ((p[0] == 'n') && (p > src) && (p[-1] == '\\'))
2602 if ((isspace(p[0])) || (p[0] == ';')) /* token ends here */
2613 else if (!((isspace(p[0])) || (p[0] == ';')))
2633 eet_data_encode(Eet_Dictionary *ed,
2634 Eet_Data_Stream *ds,
2641 Eet_Data_Chunk *echnk;
2646 if (group_type != EET_G_UNKNOWN)
2647 if (type >= EET_T_LAST)
2648 type = EET_T_UNKNOW;
2650 echnk = eet_data_chunk_new(data, size, name, type, group_type);
2651 eet_data_chunk_put(ed, echnk, ds);
2652 eet_data_chunk_free(echnk);
2657 _eet_data_dump_encode(int parent_type,
2662 Eet_Data_Chunk *chnk = NULL;
2663 Eet_Data_Stream *ds;
2670 if (_eet_data_words_bigendian == -1)
2672 unsigned long int v;
2674 v = htonl(0x12345678);
2675 if (v == 0x12345678)
2676 _eet_data_words_bigendian = 1;
2678 _eet_data_words_bigendian = 0;
2684 ds = eet_data_stream_new();
2691 for (n = node->values; n; n = n->next)
2693 data = _eet_data_dump_encode(node->type, ed, n, &size);
2696 eet_data_stream_write(ds, data, size);
2703 case EET_G_VAR_ARRAY:
2704 for (child_type = EET_T_NULL, n = node->values; n; n = n->next)
2706 if (n->type != EET_T_NULL)
2708 child_type = n->type;
2713 data = eet_data_put_type(ed,
2725 count = node->count;
2727 for (n = node->values; n; n = n->next)
2734 case EET_T_INLINED_STRING:
2735 data = eet_data_put_type(ed,
2737 &(n->data.value.str),
2754 data = _eet_data_dump_encode(n->type, ed, n, &size);
2768 for (; count; count--)
2779 /* Array is somekind of special case, so we should embed it inside another chunk. */
2780 *size_ret = ds->pos;
2785 eet_data_stream_free(ds);
2791 for (n = node->values; n; n = n->next)
2796 case EET_T_INLINED_STRING:
2797 data = eet_data_put_type(ed,
2799 &(n->data.value.str),
2816 data = _eet_data_dump_encode(node->type, ed, n, &size);
2827 /* List is another somekind of special case, every chunk is embed inside a list chunk. */
2828 *size_ret = ds->pos;
2833 eet_data_stream_free(ds);
2841 data = eet_data_put_type(ed,
2854 /* A Hash without key will not decode correctly. */
2857 for (n = node->values; n; n = n->next)
2862 case EET_T_INLINED_STRING:
2863 data = eet_data_put_type(ed,
2865 &(n->data.value.str),
2882 data = _eet_data_dump_encode(node->type, ed, n, &size);
2893 /* Hash is somekind of special case, so we should embed it inside another chunk. */
2894 *size_ret = ds->pos;
2897 eet_data_stream_flush(ds);
2904 #define EET_DATA_NODE_ENCODE(Eet_Type, Type) \
2906 data = eet_data_put_type(ed, node->type, &(node->data.value.Type), &size); \
2909 eet_data_encode(ed, \
2917 *size_ret = ds->pos; \
2918 eet_data_stream_flush(ds); \
2923 EET_DATA_NODE_ENCODE(EET_T_CHAR, c);
2924 EET_DATA_NODE_ENCODE(EET_T_SHORT, s);
2925 EET_DATA_NODE_ENCODE(EET_T_INT, i);
2926 EET_DATA_NODE_ENCODE(EET_T_LONG_LONG, l);
2927 EET_DATA_NODE_ENCODE(EET_T_FLOAT, f);
2928 EET_DATA_NODE_ENCODE(EET_T_DOUBLE, d);
2929 EET_DATA_NODE_ENCODE(EET_T_UCHAR, uc);
2930 EET_DATA_NODE_ENCODE(EET_T_USHORT, us);
2931 EET_DATA_NODE_ENCODE(EET_T_UINT, ui);
2932 EET_DATA_NODE_ENCODE(EET_T_ULONG_LONG, ul);
2933 EET_DATA_NODE_ENCODE(EET_T_INLINED_STRING, str);
2934 EET_DATA_NODE_ENCODE(EET_T_STRING, str);
2940 if ((node->type >= EET_G_UNKNOWN) && (node->type < EET_G_LAST))
2941 chnk = eet_data_chunk_new(ds->data,
2947 chnk = eet_data_chunk_new(ds->data,
2953 eet_data_stream_flush(ds);
2955 ds = eet_data_stream_new();
2956 eet_data_chunk_put(ed, chnk, ds);
2960 eet_data_stream_flush(ds);
2964 eet_data_chunk_free(chnk);
2970 _eet_data_dump_parse(Eet_Dictionary *ed,
2976 const char *p = NULL;
2981 Eet_Node *node_base = NULL;
2982 Eet_Node *node = NULL;
2983 Eet_Node *n = NULL, *nn = NULL;
2985 /* FIXME; handle parse errors */
2986 #define TOK_GET(t) \
2987 jump = left; t = _eet_data_dump_token_get(p, &left); p += jump - left;
2989 for (p = src; p < (src + size); )
2991 char *tok1, *tok2, *tok3, *tok4;
2996 if (!strcmp(tok1, "group"))
3007 if (!strcmp(tok4, "{"))
3009 /* we have 'group NAM TYP {' */
3023 for (nn = node->values; nn;
3034 n->name = eina_stringshare_add(tok2);
3035 if (!strcmp(tok3, "struct"))
3036 n->type = EET_G_UNKNOWN;
3037 else if (!strcmp(tok3, "array"))
3038 n->type = EET_G_ARRAY;
3039 else if (!strcmp(tok3, "var_array"))
3040 n->type = EET_G_VAR_ARRAY;
3041 else if (!strcmp(tok3, "list"))
3042 n->type = EET_G_LIST;
3043 else if (!strcmp(tok3, "hash"))
3044 n->type = EET_G_HASH;
3047 "ERROR: group type '%s' invalid.",
3063 else if (!strcmp(tok1, "value"))
3074 /* we have 'value NAME TYP XXX' */
3085 for (nn = node->values; nn;
3095 n->name = eina_stringshare_add(tok2);
3096 if (!strcmp(tok3, "char:"))
3098 n->type = EET_T_CHAR;
3099 sscanf(tok4, "%hhi",
3100 &(n->data.value.c));
3102 else if (!strcmp(tok3, "short:"))
3104 n->type = EET_T_SHORT;
3106 &(n->data.value.s));
3108 else if (!strcmp(tok3, "int:"))
3110 n->type = EET_T_INT;
3112 &(n->data.value.i));
3114 else if (!strcmp(tok3, "long_long:"))
3116 n->type = EET_T_LONG_LONG;
3117 sscanf(tok4, "%lli",
3118 &(n->data.value.l));
3120 else if (!strcmp(tok3, "float:"))
3122 n->type = EET_T_FLOAT;
3124 &(n->data.value.f));
3126 else if (!strcmp(tok3, "double:"))
3128 n->type = EET_T_DOUBLE;
3130 &(n->data.value.d));
3132 else if (!strcmp(tok3, "uchar:"))
3134 n->type = EET_T_UCHAR;
3135 sscanf(tok4, "%hhu",
3136 &(n->data.value.uc));
3138 else if (!strcmp(tok3, "ushort:"))
3140 n->type = EET_T_USHORT;
3142 &(n->data.value.us));
3144 else if (!strcmp(tok3, "uint:"))
3146 n->type = EET_T_UINT;
3148 &(n->data.value.ui));
3150 else if (!strcmp(tok3, "ulong_long:"))
3152 n->type = EET_T_ULONG_LONG;
3153 sscanf(tok4, "%llu",
3154 &(n->data.value.ul));
3156 else if (!strcmp(tok3, "string:"))
3158 n->type = EET_T_STRING;
3160 eina_stringshare_add(tok4);
3162 else if (!strcmp(tok3, "inlined:"))
3164 n->type = EET_T_INLINED_STRING;
3166 eina_stringshare_add(tok4);
3168 else if (!strcmp(tok3, "null"))
3170 n->type = EET_T_NULL;
3171 n->data.value.str = NULL;
3175 "ERROR: value type '%s' invalid.",
3189 else if (!strcmp(tok1, "key"))
3194 /* we have 'key NAME' */
3196 node->key = eina_stringshare_add(tok2);
3201 else if (!strcmp(tok1, "count"))
3206 /* we have a 'count COUNT' */
3208 sscanf(tok2, "%i", &(node->count));
3213 else if (!strcmp(tok1, "}"))
3214 /* we have an end of the group */
3216 node = node->parent;
3224 cdata = _eet_data_dump_encode(EET_G_UNKNOWN, ed, node_base, size_ret);
3225 eet_node_del(node_base);
3231 #define NEXT_CHUNK(P, Size, Echnk, Ed) \
3234 __tmp = Ed ? (int)(sizeof(int) * 2) : Echnk.len + 4; \
3235 P += (4 + Echnk.size + __tmp); \
3236 Size -= (4 + Echnk.size + __tmp); \
3240 _eet_data_descriptor_decode(Eet_Free_Context *context,
3241 const Eet_Dictionary *ed,
3242 Eet_Data_Descriptor *edd,
3243 const void *data_in,
3248 Eet_Node *result = NULL;
3252 Eet_Data_Chunk chnk;
3254 if (_eet_data_words_bigendian == -1)
3256 unsigned long int v;
3258 v = htonl(0x12345678);
3259 if (v == 0x12345678)
3260 _eet_data_words_bigendian = 1;
3262 _eet_data_words_bigendian = 0;
3269 if (size_out <= edd->size)
3274 data = edd->func.mem_alloc(edd->size);
3282 for (i = 0; i < edd->elements.num; i++)
3283 edd->elements.set[i].directory_name_ptr = NULL;
3288 _eet_freelist_all_ref(context);
3289 if (data && !data_out)
3290 _eet_freelist_add(context, data);
3292 memset(&chnk, 0, sizeof(Eet_Data_Chunk));
3293 eet_data_chunk_get(ed, &chnk, data_in, size_in);
3298 if (strcmp(chnk.name, edd->name))
3303 size = size_in - (4 + sizeof(int) * 2);
3305 size = size_in - (4 + 4 + chnk.len);
3309 if (!edd->elements.hash.buckets)
3310 _eet_descriptor_hash_new(edd);
3314 switch (chnk.group_type)
3320 return eet_node_string_new(chnk.name, chnk.data);
3322 case EET_T_INLINED_STRING:
3323 return eet_node_inlined_string_new(chnk.name, chnk.data);
3326 return eet_node_null_new(chnk.name);
3329 result = eet_node_struct_new(chnk.name, NULL);
3333 case EET_G_VAR_ARRAY:
3334 return eet_node_var_array_new(chnk.name, NULL);
3348 Eet_Data_Chunk echnk;
3349 Eet_Data_Element *ede = NULL;
3350 Eet_Node *child = NULL;
3351 int group_type = EET_G_UNKNOWN, type = EET_T_UNKNOW;
3354 /* get next data chunk */
3355 memset(&echnk, 0, sizeof(Eet_Data_Chunk));
3356 eet_data_chunk_get(ed, &echnk, p, size);
3358 goto error; /* FIXME: don't REPLY on edd - work without */
3362 ede = _eet_descriptor_hash_find(edd, echnk.name, echnk.hash);
3365 group_type = ede->group_type;
3367 if ((echnk.type == 0) && (echnk.group_type == 0))
3370 group_type = ede->group_type;
3374 if (IS_SIMPLE_TYPE(echnk.type) &&
3375 eet_data_type_match(echnk.type, ede->type))
3376 /* Needed when converting on the fly from FP to Float */
3378 else if ((echnk.group_type > EET_G_UNKNOWN) &&
3379 (echnk.group_type < EET_G_LAST) &&
3380 (echnk.group_type == ede->group_type))
3381 group_type = echnk.group_type;
3385 /*...... dump to node */
3389 group_type = echnk.group_type;
3392 if (!edd && group_type == EET_G_UNKNOWN && IS_SIMPLE_TYPE(type))
3394 unsigned char dd[128];
3396 ret = eet_data_get_type(ed,
3399 ((char *)echnk.data) + echnk.size,
3404 child = eet_data_node_simple_type(type, echnk.name, dd);
3406 eet_node_struct_append(result, echnk.name, child);
3410 ret = eet_group_codec[group_type - 100].get(
3418 ede ? (void *)(((char *)data) + ede->offset) : (void **)&result,
3426 /* advance to next chunk */
3427 NEXT_CHUNK(p, size, echnk, ed);
3430 _eet_freelist_all_unref(context);
3433 _eet_freelist_str_free(context, edd);
3434 _eet_freelist_direct_str_free(context, edd);
3435 _eet_freelist_list_free(context, edd);
3436 _eet_freelist_hash_free(context, edd);
3437 _eet_freelist_array_free(context, edd);
3438 _eet_freelist_free(context, edd);
3442 _eet_freelist_reset(context);
3443 _eet_freelist_str_reset(context);
3444 _eet_freelist_list_reset(context);
3445 _eet_freelist_hash_reset(context);
3446 _eet_freelist_direct_str_reset(context);
3447 _eet_freelist_array_reset(context);
3456 eet_node_del(result);
3458 _eet_freelist_all_unref(context);
3459 _eet_freelist_str_free(context, edd);
3460 _eet_freelist_direct_str_free(context, edd);
3461 _eet_freelist_list_free(context, edd);
3462 _eet_freelist_hash_free(context, edd);
3463 _eet_freelist_array_free(context, edd);
3464 _eet_freelist_free(context, edd);
3466 /* FIXME: Warn that something goes wrong here. */
3471 eet_data_get_list(Eet_Free_Context *context,
3472 const Eet_Dictionary *ed,
3473 Eet_Data_Descriptor *edd,
3474 Eet_Data_Element *ede,
3475 Eet_Data_Chunk *echnk,
3477 int group_type __UNUSED__,
3482 Eet_Data_Descriptor *subtype = NULL;
3487 EET_ASSERT(!((type > EET_T_UNKNOW) && (type < EET_T_STRING)), return 0);
3491 subtype = ede->subtype;
3493 if (type != ede->type)
3497 ptr = (void **)data;
3501 if (IS_POINTER_TYPE(type))
3502 POINTER_TYPE_DECODE(context,
3513 STRUCT_TYPE_DECODE(data_ret,
3524 list = edd->func.list_append(list, data_ret);
3526 _eet_freelist_list_add(context, ptr);
3529 eet_node_list_append(*((Eet_Node **)data), echnk->name, data_ret);
3538 eet_data_get_hash(Eet_Free_Context *context,
3539 const Eet_Dictionary *ed,
3540 Eet_Data_Descriptor *edd,
3541 Eet_Data_Element *ede,
3542 Eet_Data_Chunk *echnk,
3544 int group_type __UNUSED__,
3552 void *data_ret = NULL;
3555 EET_ASSERT(!((type > EET_T_UNKNOW) && (type < EET_T_STRING)), return 0);
3557 ptr = (void **)data;
3561 ret = eet_data_get_type(ed,
3564 ((char *)echnk->data) + echnk->size,
3572 /* Advance to next chunk */
3573 NEXT_CHUNK((*p), (*size), (*echnk), ed);
3574 memset(echnk, 0, sizeof(Eet_Data_Chunk));
3577 eet_data_chunk_get(ed, echnk, *p, *size);
3582 if ((ede->group_type != echnk->group_type)
3583 || (ede->type != echnk->type))
3586 if (IS_POINTER_TYPE(echnk->type))
3587 POINTER_TYPE_DECODE(context,
3598 STRUCT_TYPE_DECODE(data_ret,
3601 ede ? ede->subtype : NULL,
3609 hash = edd->func.hash_add(hash, key, data_ret);
3611 _eet_freelist_hash_add(context, hash);
3614 eet_node_hash_add(*((Eet_Node **)data), echnk->name, key, data_ret);
3622 /* var arrays and fixed arrays have to
3623 * get all chunks at once. for fixed arrays
3624 * we can get each chunk and increment a
3625 * counter stored on the element itself but
3626 * it wont be thread safe. for var arrays
3627 * we still need a way to get the number of
3628 * elements from the data, so storing the
3629 * number of elements and the element data on
3630 * each chunk is pointless.
3633 eet_data_get_array(Eet_Free_Context *context,
3634 const Eet_Dictionary *ed,
3635 Eet_Data_Descriptor *edd,
3636 Eet_Data_Element *ede,
3637 Eet_Data_Chunk *echnk,
3644 Eina_List *childs = NULL;
3653 EET_ASSERT(!((type > EET_T_UNKNOW) && (type < EET_T_STRING)), return 0);
3656 /* read the number of elements */
3657 ret = eet_data_get_type(ed,
3660 ((char *)echnk->data) + echnk->size,
3669 if (IS_POINTER_TYPE(type))
3670 subsize = eet_basic_codec[ede->type].size;
3672 subsize = ede->subtype->size;
3674 if (group_type == EET_G_VAR_ARRAY)
3676 /* store the number of elements
3677 * on the counter offset */
3678 *(int *)(((char *)data) + ede->count - ede->offset) = count;
3679 /* allocate space for the array of elements */
3680 if (edd->func.array_alloc)
3681 *(void **)ptr = edd->func.array_alloc(count * subsize);
3683 *(void **)ptr = edd->func.mem_alloc(count * subsize);
3688 memset(*(void **)ptr, 0, count * subsize);
3690 _eet_freelist_array_add(context, *(void **)ptr);
3694 /* get all array elements */
3695 for (i = 0; i < count; i++)
3699 /* Advance to next chunk */
3700 NEXT_CHUNK((*p), (*size), (*echnk), ed);
3701 memset(echnk, 0, sizeof(Eet_Data_Chunk));
3703 eet_data_chunk_get(ed, echnk, *p, *size);
3704 if (!echnk->name || strcmp(echnk->name, name) != 0)
3705 goto on_error; /* get the data */
3707 if ((echnk->group_type != group_type)
3708 || ((echnk->type != type) && (echnk->type != EET_T_NULL)))
3712 if ((ede->group_type != echnk->group_type)
3713 || ((echnk->type != ede->type) && (echnk->type != EET_T_NULL)))
3716 /* get the destination pointer */
3719 if (group_type == EET_G_ARRAY)
3720 dst = (char *)ptr + (subsize * i);
3722 dst = *(char **)ptr + (subsize * i);
3725 if (IS_POINTER_TYPE(echnk->type))
3727 void *data_ret = NULL;
3729 POINTER_TYPE_DECODE(context,
3740 memcpy(dst, &data_ret, subsize);
3743 childs = eina_list_append(childs, data_ret);
3747 STRUCT_TYPE_DECODE(dst,
3750 ede ? ede->subtype : NULL,
3757 childs = eina_list_append(childs, dst);
3763 Eet_Node *parent = *((Eet_Node **)data);
3766 if (group_type == EET_G_ARRAY)
3767 array = eet_node_array_new(name, count, childs);
3769 array = eet_node_var_array_new(name, childs);
3774 eet_node_struct_append(parent, name, array);
3780 EINA_LIST_FREE(childs, tmp)
3787 eet_data_put_union(Eet_Dictionary *ed,
3788 Eet_Data_Descriptor *edd __UNUSED__,
3789 Eet_Data_Element *ede,
3790 Eet_Data_Stream *ds,
3793 const char *union_type;
3796 EET_ASSERT(!((ede->type != EET_T_UNKNOW) || (!ede->subtype)), return );
3798 union_type = ede->subtype->func.type_get(
3799 ((char *)data_in) + ede->count - ede->offset,
3805 /* Search the structure of the union to encode. */
3806 for (i = 0; i < ede->subtype->elements.num; ++i)
3807 if (strcmp(ede->subtype->elements.set[i].name, union_type) == 0)
3809 Eet_Data_Element *sede;
3813 /* Yeah we found it ! */
3814 data = eet_data_put_type(ed, EET_T_STRING, &union_type, &size);
3824 sede = &(ede->subtype->elements.set[i]);
3825 data = _eet_data_descriptor_encode(ed,
3843 eet_data_get_union(Eet_Free_Context *context,
3844 const Eet_Dictionary *ed,
3845 Eet_Data_Descriptor *edd __UNUSED__,
3846 Eet_Data_Element *ede,
3847 Eet_Data_Chunk *echnk,
3854 const char *union_type;
3855 void *data_ret = NULL;
3860 ret = eet_data_get_type(ed,
3863 ((char *)echnk->data) + echnk->size,
3868 /* Advance to next chunk */
3869 NEXT_CHUNK((*p), (*size), (*echnk), ed);
3870 memset(echnk, 0, sizeof(Eet_Data_Chunk));
3873 eet_data_chunk_get(ed, echnk, *p, *size);
3879 EET_ASSERT(!(ede->group_type != group_type || ede->type != type),
3882 /* Search the structure of the union to decode */
3883 for (i = 0; i < ede->subtype->elements.num; ++i)
3884 if (strcmp(ede->subtype->elements.set[i].name, union_type) == 0)
3886 Eet_Data_Element *sede;
3889 /* Yeah we found it ! */
3890 sede = &(ede->subtype->elements.set[i]);
3891 EET_ASSERT(sede->subtype, goto on_error);
3893 data_ret = _eet_data_descriptor_decode(context,
3899 sede->subtype->size);
3903 /* Set union type. */
3904 if ((!ed) || (!ede->subtype->func.str_direct_alloc))
3906 ut = ede->subtype->func.str_alloc(union_type);
3907 _eet_freelist_str_add(context, ut);
3911 ut = ede->subtype->func.str_direct_alloc(union_type);
3912 _eet_freelist_direct_str_add(context, ut);
3915 ede->subtype->func.type_set(
3917 ((char *)data) + ede->count -
3926 /* FIXME: generate node structure. */
3927 data_ret = _eet_data_descriptor_decode(context,
3929 echnk->data, echnk->size,
3941 eet_data_put_variant(Eet_Dictionary *ed,
3942 Eet_Data_Descriptor *edd __UNUSED__,
3943 Eet_Data_Element *ede,
3944 Eet_Data_Stream *ds,
3947 const char *union_type;
3949 Eina_Bool unknow = EINA_FALSE;
3953 EET_ASSERT(!((ede->type != EET_T_UNKNOW) || (!ede->subtype)), return );
3955 union_type = ede->subtype->func.type_get(
3956 ((char *)data_in) + ede->count - ede->offset,
3959 if (!union_type && unknow == EINA_FALSE)
3964 /* Handle opaque internal representation */
3965 Eet_Variant_Unknow *evu;
3967 data = eet_data_put_type(ed, EET_T_STRING, &union_type, &size);
3977 evu = (Eet_Variant_Unknow *)data_in;
3978 if (evu && EINA_MAGIC_CHECK(evu, EET_MAGIC_VARIANT))
3988 /* Search the structure of the union to encode. */
3989 for (i = 0; i < ede->subtype->elements.num; ++i)
3990 if (strcmp(ede->subtype->elements.set[i].name, union_type) == 0)
3992 Eet_Data_Element *sede;
3994 /* Yeah we found it ! */
3995 data = eet_data_put_type(ed, EET_T_STRING, &union_type, &size);
4005 sede = &(ede->subtype->elements.set[i]);
4007 if (sede->group_type != EET_G_UNKNOWN)
4009 Eet_Data_Stream *lds;
4011 lds = eet_data_stream_new();
4012 eet_group_codec[sede->group_type - 100].put(ed,
4019 eet_data_encode(ed, ds, lds->data, ede->name, lds->pos,
4020 ede->type, ede->group_type);
4026 eet_data_encode(ed, ds, NULL, ede->name, 0,
4027 EET_T_NULL, ede->group_type);
4029 eet_data_stream_free(lds);
4033 data = _eet_data_descriptor_encode(ed,
4052 eet_data_get_variant(Eet_Free_Context *context,
4053 const Eet_Dictionary *ed,
4054 Eet_Data_Descriptor *edd __UNUSED__,
4055 Eet_Data_Element *ede,
4056 Eet_Data_Chunk *echnk,
4057 int type __UNUSED__,
4058 int group_type __UNUSED__,
4063 const char *union_type;
4064 void *data_ret = NULL;
4069 ret = eet_data_get_type(ed,
4072 ((char *)echnk->data) + echnk->size,
4077 /* Advance to next chunk */
4078 NEXT_CHUNK((*p), (*size), (*echnk), ed);
4079 memset(echnk, 0, sizeof(Eet_Data_Chunk));
4082 eet_data_chunk_get(ed, echnk, *p, *size);
4090 EET_ASSERT(ede->subtype, goto on_error);
4092 if ((!ed) || (!ede->subtype->func.str_direct_alloc))
4094 ut = ede->subtype->func.str_alloc(union_type);
4095 _eet_freelist_str_add(context, ut);
4099 ut = ede->subtype->func.str_direct_alloc(union_type);
4100 _eet_freelist_direct_str_add(context, ut);
4103 /* Search the structure of the union to decode */
4104 for (i = 0; i < ede->subtype->elements.num; ++i)
4105 if (strcmp(ede->subtype->elements.set[i].name, union_type) == 0)
4107 Eet_Data_Element *sede;
4109 /* Yeah we found it ! */
4110 sede = &(ede->subtype->elements.set[i]);
4112 if (sede->group_type != EET_G_UNKNOWN)
4114 Eet_Data_Chunk chnk;
4119 size2 = echnk->size;
4121 /* Didn't find a proper way to provide this
4122 without duplicating code */
4125 memset(&chnk, 0, sizeof(Eet_Data_Chunk));
4126 eet_data_chunk_get(ed, &chnk, p2, size2);
4131 ret = eet_group_codec[sede->group_type - 100].get
4132 (context, ed, sede->subtype, sede, &chnk, sede->type,
4133 sede->group_type, data, &p2, &size2);
4138 /* advance to next chunk */
4139 NEXT_CHUNK(p2, size2, chnk, ed);
4142 /* Put garbage so that we will not put eet_variant_unknow in it */
4143 data_ret = (void *)data;
4145 /* Set variant type. */
4146 ede->subtype->func.type_set
4147 (ut, ((char *)data) + ede->count - ede->offset,
4152 data_ret = _eet_data_descriptor_decode(context,
4161 /* And point to the variant data. */
4162 *(void **)data = data_ret;
4164 /* Set variant type. */
4165 ede->subtype->func.type_set
4166 (ut, ((char *)data) + ede->count - ede->offset, EINA_FALSE);
4172 Eet_Variant_Unknow *evu;
4174 evu = calloc(1, sizeof (Eet_Variant_Unknow) + echnk->size - 1);
4178 evu->size = echnk->size;
4179 memcpy(evu->data, echnk->data, evu->size);
4180 EINA_MAGIC_SET(evu, EET_MAGIC_VARIANT);
4182 /* And point to the opaque internal data scructure */
4183 *(void **)data = evu;
4185 /* Set variant type. */
4186 ede->subtype->func.type_set
4187 (ut, ((char *)data) + ede->count - ede->offset, EINA_TRUE);
4192 /* FIXME: dump node structure. */
4193 data_ret = _eet_data_descriptor_decode(context,
4195 echnk->data, echnk->size,
4207 eet_data_node_simple_type(int type,
4213 #endif /* ifdef EET_T_TYPE */
4215 #define EET_T_TYPE(Eet_Type, Eet_Node_Type, Type) \
4217 return eet_node_ ## Eet_Node_Type ## _new(name, *((Type *)dd)); \
4221 EET_T_TYPE(EET_T_CHAR, char, char);
4222 EET_T_TYPE(EET_T_SHORT, short, short);
4223 EET_T_TYPE(EET_T_INT, int, int);
4224 EET_T_TYPE(EET_T_LONG_LONG, long_long, long long);
4225 EET_T_TYPE(EET_T_FLOAT, float, float);
4226 EET_T_TYPE(EET_T_DOUBLE, double, double);
4227 EET_T_TYPE(EET_T_UCHAR, unsigned_char, unsigned char);
4228 EET_T_TYPE(EET_T_USHORT, unsigned_short, unsigned short);
4229 EET_T_TYPE(EET_T_UINT, unsigned_int, unsigned int);
4230 EET_T_TYPE(EET_T_ULONG_LONG, unsigned_long_long, unsigned long long);
4231 EET_T_TYPE(EET_T_STRING, string, char *);
4232 EET_T_TYPE(EET_T_INLINED_STRING, inlined_string, char *);
4235 return eet_node_null_new(name);
4238 ERR("Unknow type passed to eet_data_node_simple_type");
4244 eet_data_get_unknown(Eet_Free_Context *context,
4245 const Eet_Dictionary *ed,
4246 Eet_Data_Descriptor *edd,
4247 Eet_Data_Element *ede,
4248 Eet_Data_Chunk *echnk,
4250 int group_type __UNUSED__,
4252 char **p __UNUSED__,
4253 int *size __UNUSED__)
4258 if (IS_SIMPLE_TYPE(type))
4260 unsigned char dd[128];
4262 ret = eet_data_get_type(ed,
4265 ((char *)echnk->data) + echnk->size,
4266 edd ? (char *)data : (char *)dd);
4272 Eet_Node **parent = data;
4275 node = eet_data_node_simple_type(type, echnk->name, dd);
4278 eet_node_struct_append(*parent, echnk->name, node);
4284 if (type == EET_T_STRING)
4288 str = (char **)(((char *)data));
4291 if ((!ed) || (!edd->func.str_direct_alloc))
4293 *str = edd->func.str_alloc(*str);
4294 _eet_freelist_str_add(context, *str);
4298 *str = edd->func.str_direct_alloc(*str);
4299 _eet_freelist_direct_str_add(context, *str);
4303 else if (edd && type == EET_T_INLINED_STRING)
4307 str = (char **)(((char *)data));
4310 *str = edd->func.str_alloc(*str);
4311 _eet_freelist_str_add(context, *str);
4318 Eet_Data_Descriptor *subtype;
4320 subtype = ede ? ede->subtype : NULL;
4322 if (subtype || !edd)
4324 Eet_Node **parent = data;
4327 data_ret = _eet_data_descriptor_decode(context,
4338 ptr = (void **)(((char *)data));
4339 *ptr = (void *)data_ret;
4343 Eet_Node *node = data_ret;
4347 node = eet_node_struct_child_new(echnk->name, node);
4348 eet_node_struct_append(*parent, echnk->name, node);
4360 eet_data_put_array(Eet_Dictionary *ed,
4361 Eet_Data_Descriptor *edd __UNUSED__,
4362 Eet_Data_Element *ede,
4363 Eet_Data_Stream *ds,
4373 EET_ASSERT(!((ede->type > EET_T_UNKNOW) && (ede->type < EET_T_STRING)),
4376 if (ede->group_type == EET_G_ARRAY)
4377 count = ede->counter_offset;
4379 count = *(int *)(((char *)data_in) + ede->count - ede->offset);
4382 return; /* Store number of elements */
4384 data = eet_data_put_type(ed, EET_T_INT, &count, &size);
4386 eet_data_encode(ed, ds, data, ede->name, size, ede->type, ede->group_type);
4388 if (IS_POINTER_TYPE(ede->type))
4389 subsize = eet_basic_codec[ede->type].size;
4391 subsize = ede->subtype->size;
4393 for (j = 0; j < count; j++)
4398 if (ede->group_type == EET_G_ARRAY)
4399 d = (void *)(((char *)data_in) + offset);
4401 d = *(((char **)data_in)) + offset;
4403 if (IS_POINTER_TYPE(ede->type))
4406 eet_data_put_unknown(ed, NULL, ede, ds, d);
4410 data = _eet_data_descriptor_encode(ed, ede->subtype, d, &size);
4422 /* Add a NULL element just to have the correct array layout. */
4436 eet_data_put_unknown(Eet_Dictionary *ed,
4437 Eet_Data_Descriptor *edd __UNUSED__,
4438 Eet_Data_Element *ede,
4439 Eet_Data_Stream *ds,
4445 if (IS_SIMPLE_TYPE(ede->type))
4446 data = eet_data_put_type(ed, ede->type, data_in, &size);
4447 else if (ede->subtype)
4448 if (*((char **)data_in))
4449 data = _eet_data_descriptor_encode(ed,
4451 *((char **)((char *)(data_in))),
4465 eet_data_put_list(Eet_Dictionary *ed,
4466 Eet_Data_Descriptor *edd,
4467 Eet_Data_Element *ede,
4468 Eet_Data_Stream *ds,
4475 EET_ASSERT(!(((ede->type > EET_T_UNKNOW) && (ede->type < EET_T_STRING))
4476 || ((ede->type > EET_T_NULL) && (ede->type < EET_T_LAST))),
4479 l = *((void **)(((char *)data_in)));
4480 for (; l; l = edd->func.list_next(l))
4482 if (IS_POINTER_TYPE(ede->type))
4484 const void *str = edd->func.list_data(l);
4485 eet_data_put_unknown(ed, NULL, ede, ds, &str);
4489 data = _eet_data_descriptor_encode(ed,
4491 edd->func.list_data(l),
4506 eet_data_put_hash(Eet_Dictionary *ed,
4507 Eet_Data_Descriptor *edd,
4508 Eet_Data_Element *ede,
4509 Eet_Data_Stream *ds,
4512 Eet_Data_Encode_Hash_Info fdata;
4515 l = *((void **)(((char *)data_in)));
4519 edd->func.hash_foreach(l, eet_data_descriptor_encode_hash_cb, &fdata);
4523 eet_data_dump_cipher(Eet_File *ef,
4525 const char *cipher_key,
4526 Eet_Dump_Callback dumpfunc,
4529 const Eet_Dictionary *ed = NULL;
4530 const void *data = NULL;
4532 Eet_Free_Context context;
4533 int required_free = 0;
4536 ed = eet_dictionary_get(ef);
4539 data = eet_read_direct(ef, name, &size);
4544 data = eet_read_cipher(ef, name, &size, cipher_key);
4549 eet_free_context_init(&context);
4550 result = _eet_data_descriptor_decode(&context, ed, NULL, data, size, NULL, 0);
4551 eet_free_context_shutdown(&context);
4553 eet_node_dump(result, 0, dumpfunc, dumpdata);
4555 eet_node_del(result);
4560 return result ? 1 : 0;
4564 eet_data_dump(Eet_File *ef,
4566 Eet_Dump_Callback dumpfunc,
4569 return eet_data_dump_cipher(ef, name, NULL, dumpfunc, dumpdata);
4573 eet_data_text_dump_cipher(const void *data_in,
4574 const char *cipher_key,
4576 Eet_Dump_Callback dumpfunc,
4581 Eet_Free_Context context;
4582 unsigned int ret_len = 0;
4589 if (eet_decipher(data_in, size_in, cipher_key,
4590 strlen(cipher_key), &ret, &ret_len))
4600 ret = (void *)data_in;
4604 eet_free_context_init(&context);
4605 result = _eet_data_descriptor_decode(&context, NULL, NULL, ret, ret_len, NULL, 0);
4606 eet_free_context_shutdown(&context);
4608 eet_node_dump(result, 0, dumpfunc, dumpdata);
4610 eet_node_del(result);
4614 return result ? 1 : 0;
4618 eet_data_text_dump(const void *data_in,
4620 Eet_Dump_Callback dumpfunc,
4623 return eet_data_text_dump_cipher(data_in, NULL, size_in, dumpfunc, dumpdata);
4627 eet_data_text_undump_cipher(const char *text,
4628 const char *cipher_key,
4634 ret = _eet_data_dump_parse(NULL, size_ret, text, textlen);
4635 if (ret && cipher_key)
4637 void *ciphered = NULL;
4638 unsigned int ciphered_len;
4640 if (eet_cipher(ret, *size_ret, cipher_key,
4641 strlen(cipher_key), &ciphered, &ciphered_len))
4652 *size_ret = ciphered_len;
4660 eet_data_text_undump(const char *text,
4664 return eet_data_text_undump_cipher(text, NULL, textlen, size_ret);
4668 eet_data_undump_cipher(Eet_File *ef,
4670 const char *cipher_key,
4680 ed = eet_dictionary_get(ef);
4682 data_enc = _eet_data_dump_parse(ed, &size, text, textlen);
4686 val = eet_write_cipher(ef, name, data_enc, size, comp, cipher_key);
4692 eet_data_undump(Eet_File *ef,
4698 return eet_data_undump_cipher(ef, name, NULL, text, textlen, comp);
4702 eet_data_descriptor_decode_cipher(Eet_Data_Descriptor *edd,
4703 const void *data_in,
4704 const char *cipher_key,
4707 void *deciphered = (void *)data_in;
4709 Eet_Free_Context context;
4710 unsigned int deciphered_len = size_in;
4712 if (cipher_key && data_in)
4713 if (eet_decipher(data_in, size_in, cipher_key,
4714 strlen(cipher_key), &deciphered, &deciphered_len))
4722 eet_free_context_init(&context);
4723 ret = _eet_data_descriptor_decode(&context,
4729 eet_free_context_shutdown(&context);
4731 if (data_in != deciphered)
4738 eet_data_descriptor_decode(Eet_Data_Descriptor *edd,
4739 const void *data_in,
4742 return eet_data_descriptor_decode_cipher(edd, data_in, NULL, size_in);
4746 eet_data_node_decode_cipher(const void *data_in,
4747 const char *cipher_key,
4750 void *deciphered = (void *)data_in;
4752 Eet_Free_Context context;
4753 unsigned int deciphered_len = size_in;
4755 if (cipher_key && data_in)
4756 if (eet_decipher(data_in, size_in, cipher_key,
4757 strlen(cipher_key), &deciphered, &deciphered_len))
4765 eet_free_context_init(&context);
4766 ret = _eet_data_descriptor_decode(&context,
4772 eet_free_context_shutdown(&context);
4774 if (data_in != deciphered)
4781 _eet_data_descriptor_encode(Eet_Dictionary *ed,
4782 Eet_Data_Descriptor *edd,
4783 const void *data_in,
4786 Eet_Data_Stream *ds;
4787 Eet_Data_Chunk *chnk;
4792 if (_eet_data_words_bigendian == -1)
4794 unsigned long int v;
4796 v = htonl(0x12345678);
4797 if (v == 0x12345678)
4798 _eet_data_words_bigendian = 1;
4800 _eet_data_words_bigendian = 0;
4803 ds = eet_data_stream_new();
4804 for (i = 0; i < edd->elements.num; i++)
4806 Eet_Data_Element *ede;
4808 ede = &(edd->elements.set[i]);
4809 eet_group_codec[ede->group_type - 100].put(
4817 chnk = eet_data_chunk_new(ds->data,
4824 eet_data_stream_free(ds);
4826 ds = eet_data_stream_new();
4827 eet_data_chunk_put(ed, chnk, ds);
4833 eet_data_stream_free(ds);
4837 eet_data_chunk_free(chnk);
4843 eet_data_node_write_cipher(Eet_File *ef,
4845 const char *cipher_key,
4854 ed = eet_dictionary_get(ef);
4856 data_enc = _eet_data_dump_encode(EET_G_UNKNOWN, ed, node, &size);
4860 val = eet_write_cipher(ef, name, data_enc, size, comp, cipher_key);
4866 eet_data_node_encode_cipher(Eet_Node *node,
4867 const char *cipher_key,
4871 void *ciphered = NULL;
4872 unsigned int ciphered_len = 0;
4875 ret = _eet_data_dump_encode(EET_G_UNKNOWN, NULL, node, &size);
4876 if (cipher_key && ret)
4878 if (eet_cipher(ret, size, cipher_key,
4879 strlen(cipher_key), &ciphered, &ciphered_len))
4892 size = (int)ciphered_len;
4903 eet_data_descriptor_encode_cipher(Eet_Data_Descriptor *edd,
4904 const void *data_in,
4905 const char *cipher_key,
4909 void *ciphered = NULL;
4910 unsigned int ciphered_len = 0;
4913 ret = _eet_data_descriptor_encode(NULL, edd, data_in, &size);
4914 if (cipher_key && ret)
4916 if (eet_cipher(ret, size, cipher_key,
4917 strlen(cipher_key), &ciphered, &ciphered_len))
4930 size = ciphered_len;
4941 eet_data_descriptor_encode(Eet_Data_Descriptor *edd,
4942 const void *data_in,
4945 return eet_data_descriptor_encode_cipher(edd, data_in, NULL, size_ret);
4949 eet_data_xattr_cipher_get(const char *filename,
4950 const char *attribute,
4951 Eet_Data_Descriptor *edd,
4952 const char *cipher_key)
4958 blob = eina_xattr_get(filename, attribute, &size);
4959 if (!blob) return NULL;
4961 ret = eet_data_descriptor_decode_cipher(edd, blob, cipher_key, size);
4968 eet_data_xattr_cipher_set(const char *filename,
4969 const char *attribute,
4970 Eet_Data_Descriptor *edd,
4971 const char *cipher_key,
4973 Eina_Xattr_Flags flags)
4979 blob = eet_data_descriptor_encode_cipher(edd, data, cipher_key, &size);
4980 if (!blob) return EINA_FALSE;
4982 ret = eina_xattr_set(filename, attribute, blob, size, flags);