+eet_alias_get, related, docs, some spelling corrections
[framework/uifw/eet.git] / src / lib / Eet.h
1 /**
2 @brief Eet Data Handling Library Public API Calls
3
4 These routines are used for Eet Library interaction
5
6 @mainpage Eet Library Documentation
7
8 @image html  e_big.png
9
10 @version 1.0.0
11 @author Carsten Haitzler <raster@@rasterman.com>
12 @author David Goodlad <dgoodlad@@gmail.com>
13 @author Cedric Bail <cedric.bail@@free.fr>
14 @author Arnaud de Turckheim <quarium@@gmail.com>
15 @author Luis Felipe Strano Moraes <lfelipe@@profusion.mobi>
16 @author Chidambar Zinnoury <illogict@@online.fr>
17 @author Vincent Torri <vtorri@@univ-evry.fr>
18 @author Gustavo Sverzut Barbieri <barbieri@@profusion.mobi>
19 @author Raphael Kubo da Costa <kubo@@profusion.mobi>
20 @author Mathieu Taillefumier <mathieu.taillefumier@@free.fr>
21 @author Albin "Lutin" Tonnerre <albin.tonnerre@@gmail.com>
22 @author Adam Simpkins <adam@@adamsimpkins.net>
23 @author Mike Blumenkrantz <mike@zentific.com>
24 @date 2000-2011
25
26 @section toc Table of Contents
27
28 @li @ref intro
29 @li @ref example
30 @li @ref format
31 @li @ref compiling
32 @li @ref install
33 @li @ref next_steps
34 @li @ref intro_example
35
36 @section intro What is Eet?
37
38 It is a tiny library designed to write an arbitrary set of chunks of data
39 to a file and optionally compress each chunk (very much like a zip file)
40 and allow fast random-access reading of the file later on. It does not
41 do zip as a zip itself has more complexity than is needed, and it was much
42 simpler to implement this once here.
43
44 Eet is extremely fast, small and simple. Eet files can be very small and
45 highly compressed, making them very optimal for just sending across the
46 internet without having to archive, compress or decompress and install them.
47 They allow for lightning-fast random-acess reads once created, making them
48 perfect for storing data that is written once (or rarely) and read many
49 times, but the program does not want to have to read it all in at once.
50
51 It also can encode and decode data structures in memory, as well as image
52 data for saving to Eet files or sending across the network to other
53 machines, or just writing to arbitrary files on the system. All data is
54 encoded in a platform independent way and can be written and read by any
55 architecture.
56
57 @section example A simple example on using Eet
58
59 Here is a simple example on how to use Eet to save a series of strings to a
60 file and load them again. The advantage of using Eet over just fprintf() and
61 fscanf() is that not only can these entries be strings, they need no special
62 parsing to handle delimiter characters or escaping, they can be binary data,
63 image data, data structures containing integers, strings, other data
64 structures, linked lists and much more, without the programmer having to
65 worry about parsing, and best of all, Eet is very fast.
66
67 This is just a very simple example that doesn't show all of the capabilities
68 of Eet, but it serves to illustrate its simplicity.
69
70 @include eet-basic.c
71
72 @section compiling How to compile using Eet ?
73
74 Eet is a library your application links to. The procedure for this is very
75 simple. You simply have to compile your application with the appropriate
76 compiler flags that the @p pkg-config script outputs. For example:
77
78 Compiling C or C++ files into object files:
79
80 @verbatim
81 gcc -c -o main.o main.c `pkg-config --cflags eet`
82 @endverbatim
83
84 Linking object files into a binary executable:
85
86 @verbatim
87 gcc -o my_application main.o `pkg-config --libs eet`
88 @endverbatim
89
90 You simply have to make sure that pkg-config is in your shell's PATH (see
91 the manual page for your appropriate shell) and eet.pc in /usr/lib/pkgconfig
92 or its path is in the PKG_CONFIG_PATH environment variable. It's that simple
93 to link and use Eet once you have written your code to use it.
94
95 Since the program is linked to Eet, it is now able to use any advertised
96 API calls to serialize your data.
97
98 You should make sure you add any extra compile and link flags to your
99 compile commands that your application may need as well. The above example
100 is only guaranteed to make Eet add it's own requirements.
101
102
103 @section install How is it installed?
104
105 Simple:
106
107 @verbatim
108 ./configure
109 make
110 su -
111 ...
112 make install
113 @endverbatim
114
115 @section next_steps Next Steps
116
117 After you understood what Eet is and installed it in your system you
118 should proceed understanding the programming interface. We'd recommend
119 you to take a while to learn Eina
120 (http://docs.enlightenment.org/auto/eina/) as it is very convenient
121 and optimized, and Eet provides integration with it.
122
123 Recommended reading:
124
125 @li @ref Eet_File_Group to know the basics to open and save files.
126 @li @ref Eet_Data_Group to know the convenient way to serialize and
127     parse your data structures automatically. Just create your
128     descriptors and let Eet do the work for you.
129
130 @section intro_example Introductory Examples
131
132 @ref Examples
133
134 @todo Document data format for images and data structures.
135
136 */
137
138 #ifndef _EET_H
139 #define _EET_H
140
141 #include <stdlib.h>
142 #include <stdio.h>
143 #include <Eina.h>
144
145 #ifdef EAPI
146 # undef EAPI
147 #endif /* ifdef EAPI */
148
149 #ifdef _WIN32
150 # ifdef EFL_EET_BUILD
151 #  ifdef DLL_EXPORT
152 #   define EAPI __declspec(dllexport)
153 #  else /* ifdef DLL_EXPORT */
154 #   define EAPI
155 #  endif /* ! DLL_EXPORT */
156 # else /* ifdef EFL_EET_BUILD */
157 #  define EAPI __declspec(dllimport)
158 # endif /* ! EFL_EET_BUILD */
159 #else /* ifdef _WIN32 */
160 # ifdef __GNUC__
161 #  if __GNUC__ >= 4
162 #   define EAPI __attribute__ ((visibility("default")))
163 #  else /* if __GNUC__ >= 4 */
164 #   define EAPI
165 #  endif /* if __GNUC__ >= 4 */
166 # else /* ifdef __GNUC__ */
167 #  define EAPI
168 # endif /* ifdef __GNUC__ */
169 #endif /* ! _WIN32 */
170
171 #ifdef __cplusplus
172 extern "C" {
173 #endif /* ifdef __cplusplus */
174
175 /**
176  * @file Eet.h
177  * @brief The file that provides the eet functions.
178  *
179  * This header provides the Eet management functions.
180  *
181  */
182
183 #define EET_VERSION_MAJOR 1
184 #define EET_VERSION_MINOR 4
185 /**
186  * @typedef Eet_Version
187  *
188  * This is the Eet version information structure that can be used at
189  * runtime to detect which version of eet is being used and adapt
190  * appropriately as follows for example:
191  *
192  * @code
193  * #if defined(EET_VERSION_MAJOR) && (EET_VERSION_MAJOR >= 1) && defined(EET_VERSION_MINOR) && (EET_VERSION_MINOR > 2)
194  * printf("Eet version: %i.%i.%i\n",
195  *        eet_version->major,
196  *        eet_version->minor,
197  *        eet_version->micro);
198  * if (eet_version->revision > 0)
199  *   {
200  *     printf("  Built from SVN revision # %i\n", eet_version->revision);
201  *   }
202  * #endif
203  * @endcode
204  *
205  * Note the #if check can be dropped if your program refuses to compile or
206  * work with an Eet version less than 1.3.0.
207  */
208 typedef struct _Eet_Version
209 {
210    int major; /** < major (binary or source incompatible changes) */
211    int minor; /** < minor (new features, bugfixes, major improvements version) */
212    int micro; /** < micro (bugfix, internal improvements, no new features version) */
213    int revision; /** < svn revision (0 if a proper rlease or the svn revsion number Eet is built from) */
214 } Eet_Version;
215
216 EAPI extern Eet_Version *eet_version;
217
218 /**
219  * @defgroup Eet_Group Top level functions
220  * Functions that affect Eet as a whole.
221  *
222  * @{
223  */
224
225 /**
226  * @enum _Eet_Error
227  * All the error identifiers known by Eet.
228  */
229 typedef enum _Eet_Error
230 {
231    EET_ERROR_NONE, /**< No error, it's all fine! */
232    EET_ERROR_BAD_OBJECT, /**< Given object or handle is NULL or invalid */
233    EET_ERROR_EMPTY, /**< There was nothing to do */
234    EET_ERROR_NOT_WRITABLE, /**< Could not write to file or file is #EET_FILE_MODE_READ */
235    EET_ERROR_OUT_OF_MEMORY, /**< Could not allocate memory */
236    EET_ERROR_WRITE_ERROR, /**< Failed to write data to destination */
237    EET_ERROR_WRITE_ERROR_FILE_TOO_BIG, /**< Failed to write file since it is too big */
238    EET_ERROR_WRITE_ERROR_IO_ERROR, /**< Failed to write due a generic Input/Output error */
239    EET_ERROR_WRITE_ERROR_OUT_OF_SPACE, /**< Failed to write due out of space */
240    EET_ERROR_WRITE_ERROR_FILE_CLOSED, /**< Failed to write because file was closed */
241    EET_ERROR_MMAP_FAILED, /**< Could not mmap file */
242    EET_ERROR_X509_ENCODING_FAILED, /**< Could not encode using X509 */
243    EET_ERROR_SIGNATURE_FAILED, /**< Could not validate signature */
244    EET_ERROR_INVALID_SIGNATURE, /**< Signature is invalid */
245    EET_ERROR_NOT_SIGNED, /**< File or contents are not signed */
246    EET_ERROR_NOT_IMPLEMENTED, /**< Function is not implemented */
247    EET_ERROR_PRNG_NOT_SEEDED, /**< Could not introduce random seed */
248    EET_ERROR_ENCRYPT_FAILED, /**< Could not encrypt contents */
249    EET_ERROR_DECRYPT_FAILED /**< Could not decrypt contents */
250 } Eet_Error; /**< Eet error identifiers */
251
252 /**
253  * @}
254  */
255
256 /**
257  * Initialize the EET library.
258  *
259  * The first time this function is called, it will perform all the internal
260  * initialization required for the library to function properly and incrememnt
261  * the initializiation counter. Any subsequent call only increment this counter
262  * and return its new value, so it's safe to call this function more than once.
263  *
264  * @return The new init count. Will be 0 if initialization failed.
265  *
266  * @since 1.0.0
267  * @ingroup Eet_Group
268  */
269 EAPI int
270 eet_init(void);
271
272 /**
273  * Shut down the EET library.
274  *
275  * If eet_init() was called more than once for the running application,
276  * eet_shutdown() will decrement the initialization counter and return its
277  * new value, without doing anything else. When the counter reaches 0, all
278  * of the internal elements will be shutdown and any memory used freed.
279  *
280  * @return The new init count.
281  *
282  * @since 1.0.0
283  * @ingroup Eet_Group
284  */
285 EAPI int
286 eet_shutdown(void);
287
288 /**
289  * Clear eet cache
290  *
291  * For a faster access to previously accessed data, Eet keeps an internal
292  * cache of files. These files will be freed automatically only when
293  * they are unused and the cache gets full, in order based on the last time
294  * they were used.
295  * On systems with little memory this may present an unnecessary constraint,
296  * so eet_clearcache() is available for users to reclaim the memory used by
297  * files that are no longer needed. Those that were open using
298  * ::EET_FILE_MODE_WRITE or ::EET_FILE_MODE_READ_WRITE and have modifications,
299  * will be written down to disk before flushing them from memory.
300  *
301  * @since 1.0.0
302  * @ingroup Eet_Group
303  */
304 EAPI void
305 eet_clearcache(void);
306
307 /**
308  * @defgroup Eet_File_Group Eet File Main Functions
309  *
310  * Functions to create, destroy and do basic manipulation of
311  * #Eet_File handles.
312  *
313  * This sections explains how to use the most basic Eet functions, which
314  * are used to work with eet files, read data from them, store it back in or
315  * take a look at what entries it contains, without making use of the
316  * serialization capabilities explained in @ref Eet_Data_Group.
317  *
318  * The following example will serve as an introduction to most, if not all,
319  * of these functions.
320  *
321  * If you are only using Eet, this is the only header you need to include.
322  * @dontinclude eet-file.c
323  * @skipline Eet.h
324  *
325  * Now let's create ourselves an eet file to play with. The following function
326  * shows step by step how to open a file and write some data in it.
327  * First, we define our file handler and some other things we'll put in it.
328  * @line static int
329  * @skip Eet_File
330  * @until ";
331  * @skip eet_open
332  *
333  * We open a new file in write mode, and if it fails, we just return, since
334  * there's not much more we can do about it..
335  * @until return
336  *
337  * Now, we need to write some data in our file. For now, strings will suffice,
338  * so let's just dump a bunch of them in there.
339  * @until }
340  *
341  * As you can see, we copied a string into our static buffer, which is a bit
342  * bigger than the full length of the string, and then told Eet to write it
343  * into the file, compressed, returning the size of the data written into the
344  * file.
345  * This is all to show that Eet treats data as just data. It doesn't matter
346  * what that data represents (for now), it's all just bytes for it. As running
347  * the following code will show, we took a string of around 30 bytes and put it
348  * in a buffer of 1024 bytes, but the returned size won't be any of those.
349  * @until printf
350  *
351  * Next, we copy into our buffer our set of strings, including their null
352  * terminators and write them into the file. No error checking for the sake
353  * of brevitiy. And a call to eet_sync() to make sure all out data is
354  * properly written down to disk, even though we haven't yet closed the file.
355  * @until eet_sync
356  *
357  * One more write, this time our large array of binary data and... well, I
358  * couldn't come up with a valid use of the last set of strings we stored,
359  * so let's take it out from the file with eet_delete().
360  * @until eet_delete
361  *
362  * Finally, we close the file, saving any changes back to disk and return.
363  * Notice how, if there's any error closing the file or saving its contents,
364  * the return value from the function will be a false one, which later on
365  * will make the program exit with an error code.
366  * @until return
367  *
368  * Moving onto our main function, we will open the same file and read it back.
369  * Trivial, but it'll show how we can do so in more than one way. We'll skip
370  * the variable declarations, as they aren't very different from what we've
371  * seen already.
372  *
373  * We start from the beginning by initializing Eet so things in general work.
374  * Forgetting to do so will result in weird results or crashes when calling
375  * any eet function, so if you experience something like that, the first thing
376  * to look at is whether eet_init() is missing.
377  * Then we call our @p create_eet_file function, described above, to make
378  * sure we have something to work with. If the function fails it will return
379  * 0 and we just exit, since nothing from here onwards will work anyway.
380  * @skip eet_init
381  * @until return
382  *
383  * Let's take a look now at what entries our file has. For this, we use
384  * eet_list(), which will return a list of strings, each being the name of
385  * one entry. Since we skipped before, it may be worth noting that @p list
386  * is declared as a @p char **.
387  * The @p num parameter will, of course, have the number of entries contained
388  * in our file.
389  * If everything's fine, we'll get our list and print it to the screen, and
390  * once done with it, we free the list. That's just the list, not its contents,
391  * as they are internal strings used by Eet and trying to free them will surely
392  * break things.
393  * @until }
394  *
395  * Reading back plain data is simple. Just a call to eet_read() with the file
396  * to read from, and the name of the entry we are interested in. We get back
397  * our data and the passed @p size parameter will contain the size of it. If
398  * the data was stored compressed, it will decompressed first.
399  * @until }
400  *
401  * Another simple read for the set of strings from before, except those were
402  * deleted, so we should get a NULL return and continue normally.
403  * @until }
404  *
405  * Finally, we'll get our binary data in the same way we got the strings. Once
406  * again, it makes no difference for Eet what the data is, it's up to us to
407  * know how to handle it.
408  * @until {
409  *
410  * Now some cheating, we know that this data is an Eet file because, well...
411  * we just know it. So we are going to open it and take a look at its insides.
412  * For this, eet_open() won't work, as it needs to have a file on disk to read
413  * from and all we have is some data in RAM.
414  *
415  * So how do we do? One way would be to create a normal file and write down
416  * our data, then open it with eet_open(). Another, faster and more efficient
417  * if all we want to do is read the file, is to use eet_memopen_read().
418  * @until memopen
419  *
420  * As you can see, the size we got from our previous read was put to good use
421  * this time. Unlike the first one where all we had were strings, the size
422  * of the data read only serves to demonstrate that we are reading back the
423  * entire size of our original @p buf variable.
424  *
425  * A little peeking to see how many entries the file has and to make an
426  * example of eet_num_entries() to get that number when we don't care about
427  * their names.
428  * @until printf
429  *
430  * More cheating follows. Just like we knew this was an Eet file, we also know
431  * what key to read from, and ontop of that we know that the data in it is not
432  * compressed.
433  * Knowing all this allows us to take some shortcuts.
434  * @until read_direct
435  *
436  * That's a direct print of our data, whatever that data is. We don't want
437  * to worry about having to free it later, so we just used eet_direct_read()
438  * to tell Eet to gives a pointer to the internal data in the file, without
439  * duplicating it. Since we said that data was not compressed, we shouldn't
440  * worry about printing garbage to the screen (and yes, we also know the data
441  * is yet another string).
442  * We also don't care about the size of the data as it was stored in the file,
443  * so we passed NULL as the size parameter.
444  * One very important note about this, however, is that we don't care about
445  * the size parameter because the data in the file contains the null
446  * terminator for the string. So when using Eet to store strings this way,
447  * it's very important to consider whether you will keep that final null
448  * byte, or to always get the size read and do the necessary checks and copies.
449  * It's up to the user and the particular use cases to decide how this will
450  * be done.
451  *
452  * With everything done, close this second file and free the data used to open
453  * it. And this is important, we can't free that data until we are done with
454  * the file, as Eet is using it. When opening with eet_memopen_read(), the data
455  * passed to it must be available for as long as the the file is open.
456  * @until }
457  *
458  * Finally, we close the first file, shutdown all internal resources used by
459  * Eet and leave our main function, thus terminating our program.
460  * @until return
461  *
462  * You can look at the full code of the example @ref eet-file.c "here".
463  * @{
464  */
465
466 /**
467  * @enum _Eet_File_Mode
468  * Modes that a file can be opened.
469  */
470 typedef enum _Eet_File_Mode
471 {
472    EET_FILE_MODE_INVALID = -1,
473    EET_FILE_MODE_READ, /**< File is read-only. */
474    EET_FILE_MODE_WRITE, /**< File is write-only. */
475    EET_FILE_MODE_READ_WRITE /**< File is for both read and write */
476 } Eet_File_Mode; /**< Modes that a file can be opened. */
477
478 /**
479  * @typedef Eet_File
480  * Opaque handle that defines an Eet file (or memory).
481  *
482  * This handle will be returned by the functions eet_open() and
483  * eet_memopen_read() and is used by every other function that affects the
484  * file in any way. When you are done with it, call eet_close() to clsoe it
485  * and, if the file was open for writing, write down to disk any changes made
486  * to it.
487  *
488  * @see eet_open()
489  * @see eet_memopen_read()
490  * @see eet_close()
491  */
492 typedef struct _Eet_File         Eet_File;
493
494 /**
495  * @typedef Eet_Dictionary
496  * Opaque handle that defines a file-backed (mmaped) dictionary of strings.
497  */
498 typedef struct _Eet_Dictionary   Eet_Dictionary;
499
500 /**
501  * @}
502  */
503
504 /**
505  * Open an eet file on disk, and returns a handle to it.
506  * @param file The file path to the eet file. eg: @c "/tmp/file.eet".
507  * @param mode The mode for opening. Either #EET_FILE_MODE_READ,
508  *        #EET_FILE_MODE_WRITE or #EET_FILE_MODE_READ_WRITE.
509  * @return An opened eet file handle.
510  * @ingroup Eet_File_Group
511  *
512  * This function will open an exiting eet file for reading, and build
513  * the directory table in memory and return a handle to the file, if it
514  * exists and can be read, and no memory errors occur on the way, otherwise
515  * NULL will be returned.
516  *
517  * It will also open an eet file for writing. This will, if successful,
518  * delete the original file and replace it with a new empty file, till
519  * the eet file handle is closed or flushed. If it cannot be opened for
520  * writing or a memory error occurs, NULL is returned.
521  *
522  * You can also open the file for read/write. If you then write a key that
523  * does not exist it will be created, if the key exists it will be replaced
524  * by the new data.
525  * 
526  * If the same file is opened multiple times, then the same file handle will
527  * be returned as eet maintains an internal list of all currently open
528  * files. Note that it considers files opened for read only and those opened
529  * for read/write and write only as 2 separate sets. Those that do not write
530  * to the file and those that do. Eet will allow 2 handles to the same file
531  * if they are in the 2 separate lists/groups. That means opening a file for
532  * read only looks in the read only set, and returns a handle to that file
533  * handle and increments its reference count. If you open a file for read/write
534  * or write only it looks in the write set and returns a handle after
535  * incrementing the reference count. You need to close an eet file handle
536  * as many times as it has been opened to maintain correct reference counts.
537  * Files whose modified timestamp or size do not match those of the existing
538  * referenced file handles will not be returned and a new handle will be
539  * returned instead.
540  *
541  * @since 1.0.0
542  */
543 EAPI Eet_File *
544 eet_open(const char   *file,
545          Eet_File_Mode mode);
546
547 /**
548  * Open an eet file directly from a memory location. The data is not copied,
549  * so you must keep it around as long as the eet file is open. There is
550  * currently no cache for this kind of Eet_File, so it's reopened every time
551  * you use eet_memopen_read.
552  * Files opened this way will always be in read-only mode.
553  *
554  * @since 1.1.0
555  * @ingroup Eet_File_Group
556  */
557 EAPI Eet_File *
558 eet_memopen_read(const void *data,
559                  size_t      size);
560
561 /**
562  * Get the mode an Eet_File was opened with.
563  * @param ef A valid eet file handle.
564  * @return The mode ef was opened with.
565  *
566  * @since 1.0.0
567  * @ingroup Eet_File_Group
568  */
569 EAPI Eet_File_Mode
570 eet_mode_get(Eet_File *ef);
571
572 /**
573  * Close an eet file handle and flush and writes pending.
574  * @param ef A valid eet file handle.
575  *
576  * This function will flush any pending writes to disk if the eet file
577  * was opened for write, and free all data associated with the file handle
578  * and file, and close the file.
579  *
580  * If the eet file handle is not valid nothing will be done.
581  *
582  * @since 1.0.0
583  * @ingroup Eet_File_Group
584  */
585 EAPI Eet_Error
586 eet_close(Eet_File *ef);
587
588 /**
589  * Sync content of an eet file handle, flushing pending writes.
590  * @param ef A valid eet file handle.
591  *
592  * This function will flush any pending writes to disk. The eet file must
593  * be opened for write.
594  *
595  * If the eet file handle is not valid nothing will be done.
596  *
597  * @since 1.2.4
598  * @ingroup Eet_File_Group
599  */
600 EAPI Eet_Error
601 eet_sync(Eet_File *ef);
602
603 /**
604  * Return a handle to the shared string dictionary of the Eet file
605  * @param ef A valid eet file handle.
606  * @return A handle to the dictionary of the file
607  *
608  * This function returns a handle to the dictionary of an Eet file whose
609  * handle is @p ef, if a dictionary exists. NULL is returned otherwise or
610  * if the file handle is known to be invalid.
611  *
612  * @see eet_dictionary_string_check() to know if given string came
613  *      from the dictionary or it was dynamically allocated using
614  *      the #Eet_Data_Descriptor_Class instructrions.
615  *
616  * @since 1.0.0
617  * @ingroup Eet_File_Group
618  */
619 EAPI Eet_Dictionary *
620 eet_dictionary_get(Eet_File *ef);
621
622 /**
623  * Check if a given string comes from a given dictionary
624  * @param ed A valid dictionary handle
625  * @param string A valid 0 byte terminated C string
626  * @return 1 if it is in the dictionary, 0 otherwise
627  *
628  * This checks the given dictionary to see if the given string is actually
629  * inside that dictionary (i.e. comes from it) and returns 1 if it does.
630  * If the dictionary handle is invalid, the string is NULL or the string is
631  * not in the dictionary, 0 is returned.
632  *
633  * @since 1.0.0
634  * @ingroup Eet_File_Group
635  */
636 EAPI int
637 eet_dictionary_string_check(Eet_Dictionary *ed,
638                             const char     *string);
639
640 /**
641  * Read a specified entry from an eet file and return data
642  * @param ef A valid eet file handle opened for reading.
643  * @param name Name of the entry. eg: "/base/file_i_want".
644  * @param size_ret Number of bytes read from entry and returned.
645  * @return The data stored in that entry in the eet file.
646  *
647  * This function finds an entry in the eet file that is stored under the
648  * name specified, and returns that data, decompressed, if successful.
649  * NULL is returned if the lookup fails or if memory errors are
650  * encountered. It is the job of the calling program to call free() on
651  * the returned data. The number of bytes in the returned data chunk are
652  * placed in size_ret.
653  *
654  * If the eet file handle is not valid NULL is returned and size_ret is
655  * filled with 0.
656  *
657  * @see eet_read_cipher()
658  *
659  * @since 1.0.0
660  * @ingroup Eet_File_Group
661  */
662 EAPI void *
663 eet_read(Eet_File   *ef,
664          const char *name,
665          int        *size_ret);
666
667 /**
668  * Read a specified entry from an eet file and return data
669  * @param ef A valid eet file handle opened for reading.
670  * @param name Name of the entry. eg: "/base/file_i_want".
671  * @param size_ret Number of bytes read from entry and returned.
672  * @return The data stored in that entry in the eet file.
673  *
674  * This function finds an entry in the eet file that is stored under the
675  * name specified, and returns that data if not compressed and successful.
676  * NULL is returned if the lookup fails or if memory errors are
677  * encountered or if the data is comrpessed. The calling program must never
678  * call free() on the returned data. The number of bytes in the returned
679  * data chunk are placed in size_ret.
680  *
681  * If the eet file handle is not valid NULL is returned and size_ret is
682  * filled with 0.
683  *
684  * @since 1.0.0
685  * @ingroup Eet_File_Group
686  */
687 EAPI const void *
688 eet_read_direct(Eet_File   *ef,
689                 const char *name,
690                 int        *size_ret);
691
692 /**
693  * Write a specified entry to an eet file handle
694  * @param ef A valid eet file handle opened for writing.
695  * @param name Name of the entry. eg: "/base/file_i_want".
696  * @param data Pointer to the data to be stored.
697  * @param size Length in bytes in the data to be stored.
698  * @param compress Compression flags (1 == compress, 0 = don't compress).
699  * @return bytes written on successful write, 0 on failure.
700  *
701  * This function will write the specified chunk of data to the eet file
702  * and return greater than 0 on success. 0 will be returned on failure.
703  *
704  * The eet file handle must be a valid file handle for an eet file opened
705  * for writing. If it is not, 0 will be returned and no action will be
706  * performed.
707  *
708  * Name, and data must not be NULL, and size must be > 0. If these
709  * conditions are not met, 0 will be returned.
710  *
711  * The data will be copied (and optionally compressed) in ram, pending
712  * a flush to disk (it will stay in ram till the eet file handle is
713  * closed though).
714  *
715  * @see eet_write_cipher()
716  *
717  * @since 1.0.0
718  * @ingroup Eet_File_Group
719  */
720 EAPI int
721 eet_write(Eet_File   *ef,
722           const char *name,
723           const void *data,
724           int         size,
725           int         compress);
726
727 /**
728  * Delete a specified entry from an Eet file being written or re-written
729  * @param ef A valid eet file handle opened for writing.
730  * @param name Name of the entry. eg: "/base/file_i_want".
731  * @return Success or failure of the delete.
732  *
733  * This function will delete the specified chunk of data from the eet file
734  * and return greater than 0 on success. 0 will be returned on failure.
735  *
736  * The eet file handle must be a valid file handle for an eet file opened
737  * for writing. If it is not, 0 will be returned and no action will be
738  * performed.
739  *
740  * Name, must not be NULL, otherwise 0 will be returned.
741  *
742  * @since 1.0.0
743  * @ingroup Eet_File_Group
744  */
745 EAPI int
746 eet_delete(Eet_File   *ef,
747            const char *name);
748
749 /**
750  * Alias a specific section to another one. Destination may exist or not,
751  * no check are done.
752  * @param ef A valid eet file handle opened for writing.
753  * @param name Name of the entry. eg: "/base/file_i_want".
754  * @param destination Destination of the alias. eg: "/base/the_real_stuff_i_want".
755  * @param compress Compression flags (1 == compress, 0 = don't compress).
756  * @return EINA_TRUE on success, EINA_FALSE on failure.
757  *
758  * Name and Destination must not be NULL, otherwise EINA_FALSE will be returned.
759  *
760  * @since 1.3.3
761  * @ingroup Eet_File_Group
762  */
763 EAPI Eina_Bool
764 eet_alias(Eet_File   *ef,
765           const char *name,
766           const char *destination,
767           int         compress);
768
769 /**
770  * Retrieve the destination name of an alias
771  * @param ef A valid eet file handle opened for writing
772  * @param name Name of the entry. eg: "/base/file_i_want"
773  * @return Destination of the alias. eg: "/base/the_real_stuff_i_want", NULL on failure
774  *
775  * Name must not be NULL, otherwise NULL will be returned.
776  *
777  * @since 1.5
778  * @ingroup Eet_File_Group
779  */
780 EAPI const char *
781 eet_alias_get(Eet_File *ef,
782               const char *name);
783
784 /**
785  * List all entries in eet file matching shell glob.
786  * @param ef A valid eet file handle.
787  * @param glob A shell glob to match against.
788  * @param count_ret Number of entries found to match.
789  * @return Pointer to an array of strings.
790  *
791  * This function will list all entries in the eet file matching the
792  * supplied shell glob and return an allocated list of their names, if
793  * there are any, and if no memory errors occur.
794  *
795  * The eet file handle must be valid and glob must not be NULL, or NULL
796  * will be returned and count_ret will be filled with 0.
797  *
798  * The calling program must call free() on the array returned, but NOT
799  * on the string pointers in the array. They are taken as read-only
800  * internals from the eet file handle. They are only valid as long as
801  * the file handle is not closed. When it is closed those pointers in the
802  * array are now not valid and should not be used.
803  *
804  * On success the array returned will have a list of string pointers
805  * that are the names of the entries that matched, and count_ret will have
806  * the number of entries in this array placed in it.
807  *
808  * Hint: an easy way to list all entries in an eet file is to use a glob
809  * value of "*".
810  *
811  * @since 1.0.0
812  * @ingroup Eet_File_Group
813  */
814 EAPI char **
815 eet_list(Eet_File   *ef,
816          const char *glob,
817          int        *count_ret);
818
819 /**
820  * Return the number of entries in the specified eet file.
821  * @param ef A valid eet file handle.
822  * @return Number of entries in ef or -1 if the number of entries
823  *         cannot be read due to open mode restrictions.
824  *
825  * @since 1.0.0
826  * @ingroup Eet_File_Group
827  */
828 EAPI int
829 eet_num_entries(Eet_File *ef);
830
831 /**
832  * @defgroup Eet_File_Cipher_Group Eet File Ciphered Main Functions
833  *
834  * Most of the @ref Eet_File_Group have alternative versions that
835  * accounts for ciphers to protect their content.
836  *
837  * @see @ref Eet_Cipher_Group
838  *
839  * @ingroup Eet_File_Group
840  */
841
842 /**
843  * Read a specified entry from an eet file and return data using a cipher.
844  * @param ef A valid eet file handle opened for reading.
845  * @param name Name of the entry. eg: "/base/file_i_want".
846  * @param size_ret Number of bytes read from entry and returned.
847  * @param cipher_key The key to use as cipher.
848  * @return The data stored in that entry in the eet file.
849  *
850  * This function finds an entry in the eet file that is stored under the
851  * name specified, and returns that data, decompressed, if successful.
852  * NULL is returned if the lookup fails or if memory errors are
853  * encountered. It is the job of the calling program to call free() on
854  * the returned data. The number of bytes in the returned data chunk are
855  * placed in size_ret.
856  *
857  * If the eet file handle is not valid NULL is returned and size_ret is
858  * filled with 0.
859  *
860  * @see eet_read()
861  *
862  * @since 1.0.0
863  * @ingroup Eet_File_Cipher_Group
864  */
865 EAPI void *
866 eet_read_cipher(Eet_File   *ef,
867                 const char *name,
868                 int        *size_ret,
869                 const char *cipher_key);
870
871 /**
872  * Write a specified entry to an eet file handle using a cipher.
873  * @param ef A valid eet file handle opened for writing.
874  * @param name Name of the entry. eg: "/base/file_i_want".
875  * @param data Pointer to the data to be stored.
876  * @param size Length in bytes in the data to be stored.
877  * @param compress Compression flags (1 == compress, 0 = don't compress).
878  * @param cipher_key The key to use as cipher.
879  * @return bytes written on successful write, 0 on failure.
880  *
881  * This function will write the specified chunk of data to the eet file
882  * and return greater than 0 on success. 0 will be returned on failure.
883  *
884  * The eet file handle must be a valid file handle for an eet file opened
885  * for writing. If it is not, 0 will be returned and no action will be
886  * performed.
887  *
888  * Name, and data must not be NULL, and size must be > 0. If these
889  * conditions are not met, 0 will be returned.
890  *
891  * The data will be copied (and optionally compressed) in ram, pending
892  * a flush to disk (it will stay in ram till the eet file handle is
893  * closed though).
894  *
895  * @see eet_write()
896  *
897  * @since 1.0.0
898  * @ingroup Eet_File_Cipher_Group
899  */
900 EAPI int
901 eet_write_cipher(Eet_File   *ef,
902                  const char *name,
903                  const void *data,
904                  int         size,
905                  int         compress,
906                  const char *cipher_key);
907
908 /**
909  * @defgroup Eet_File_Image_Group Image Store and Load
910  *
911  * Eet efficiently stores and loads images, including alpha
912  * channels and lossy compressions.
913  *
914  * Eet can handle both lossy compression with different levels of quality and
915  * non-lossy compression with different compression levels. It's also possible,
916  * given an image data, to only read its header to get the image information
917  * without decoding the entire content for it.
918  *
919  * The encode family of functions will take an image raw buffer and its
920  * parameters and compress it in memory, returning the new buffer.
921  * Likewise, the decode functions will read from the given location in memory
922  * and return the uncompressed image.
923  *
924  * The read and write functions will, respectively, encode and decode to or
925  * from an Eet file, under the specified key.
926  *
927  * These functions are fairly low level and the same functionality can be
928  * achieved using Evas and Edje, making it much easier to work with images
929  * as well as not needing to worry about things like scaling them.
930  */
931
932 /**
933  * Read just the header data for an image and dont decode the pixels.
934  * @param ef A valid eet file handle opened for reading.
935  * @param name Name of the entry. eg: "/base/file_i_want".
936  * @param w A pointer to the unsigned int to hold the width in pixels.
937  * @param h A pointer to the unsigned int to hold the height in pixels.
938  * @param alpha A pointer to the int to hold the alpha flag.
939  * @param compress A pointer to the int to hold the compression amount.
940  * @param quality A pointer to the int to hold the quality amount.
941  * @param lossy A pointer to the int to hold the lossiness flag.
942  * @return 1 on successful decode, 0 otherwise
943  *
944  * Reads and decodes the image header data stored under the given key and
945  * Eet file.
946  *
947  * The information decoded is placed in each of the parameters, which must be
948  * provided. The width and height, measured in pixels, will be stored under
949  * the variables pointed by @p w and @p h, respectively. If the read or
950  * decode of the header fails, this values will be 0. The @p alpha parameter
951  * will be 1 or 0, denoting if the alpha channel of the image is used or not.
952  * If the image was losslessly compressed, the @p compress parameter will hold
953  * the compression amount used, ranging from 0 to 9 and @p lossy will be 0.
954  * In the case of lossy compression, @p lossy will be 1, and the compreesion
955  * quality will be placed under @p quality, with a value ranging from 0 to 100.
956  *
957  * @see eet_data_image_header_decode()
958  * @see eet_data_image_header_read_cipher()
959  *
960  * @since 1.0.0
961  * @ingroup Eet_File_Image_Group
962  */
963 EAPI int
964 eet_data_image_header_read(Eet_File     *ef,
965                            const char   *name,
966                            unsigned int *w,
967                            unsigned int *h,
968                            int          *alpha,
969                            int          *compress,
970                            int          *quality,
971                            int          *lossy);
972
973 /**
974  * Read image data from the named key in the eet file.
975  * @param ef A valid eet file handle opened for reading.
976  * @param name Name of the entry. eg: "/base/file_i_want".
977  * @param w A pointer to the unsigned int to hold the width in pixels.
978  * @param h A pointer to the unsigned int to hold the height in pixels.
979  * @param alpha A pointer to the int to hold the alpha flag.
980  * @param compress A pointer to the int to hold the compression amount.
981  * @param quality A pointer to the int to hold the quality amount.
982  * @param lossy A pointer to the int to hold the lossiness flag.
983  * @return The image pixel data decoded
984  *
985  * Reads and decodes the image stored in the given Eet file under the named
986  * key.
987  *
988  * The returned pixel data is a linear array of pixels starting from the
989  * top-left of the image, scanning row by row from left to right. Each pile
990  * is a 32bit value, with the high byte being the alpha channel, the next being
991  * red, then green, and the low byte being blue.
992  *
993  * The rest of the parameters are the same as in eet_data_image_header_read().
994  *
995  * On success the function returns a pointer to the image data decoded. The
996  * calling application is responsible for calling free() on the image data
997  * when it is done with it. On failure NULL is returned and the parameter
998  * values may not contain any sensible data.
999  *
1000  * @see eet_data_image_header_read()
1001  * @see eet_data_image_decode()
1002  * @see eet_data_image_read_cipher()
1003  * @see eet_data_image_read_to_surface()
1004  *
1005  * @since 1.0.0
1006  * @ingroup Eet_File_Image_Group
1007  */
1008 EAPI void *
1009 eet_data_image_read(Eet_File     *ef,
1010                     const char   *name,
1011                     unsigned int *w,
1012                     unsigned int *h,
1013                     int          *alpha,
1014                     int          *compress,
1015                     int          *quality,
1016                     int          *lossy);
1017
1018 /**
1019  * Read image data from the named key in the eet file and store it in the given buffer.
1020  * @param ef A valid eet file handle opened for reading.
1021  * @param name Name of the entry. eg: "/base/file_i_want".
1022  * @param src_x The starting x coordinate from where to dump the stream.
1023  * @param src_y The starting y coordinate from where to dump the stream.
1024  * @param d A pointer to the pixel surface.
1025  * @param w The expected width in pixels of the pixel surface to decode.
1026  * @param h The expected height in pixels of the pixel surface to decode.
1027  * @param row_stride The length of a pixels line in the destination surface.
1028  * @param alpha A pointer to the int to hold the alpha flag.
1029  * @param compress A pointer to the int to hold the compression amount.
1030  * @param quality A pointer to the int to hold the quality amount.
1031  * @param lossy A pointer to the int to hold the lossiness flag.
1032  * @return 1 on success, 0 otherwise.
1033  *
1034  * Reads and decodes the image stored in the given Eet file, placing the
1035  * resulting pixel data in the buffer pointed by the user.
1036  *
1037  * Like eet_data_image_read(), it takes the image data stored under the
1038  * @p name key in the @p ef file, but instead of returning a new buffer with
1039  * the pixel data, it places the result in the buffer pointed by @p d, which
1040  * must be provided by the user and of sufficient size to hold the requested
1041  * portion of the image.
1042  *
1043  * The @p src_x and @p src_y parameters indicate the top-left corner of the
1044  * section of the image to decode. These have to be higher or equal than 0 and
1045  * less than the respective total width and height of the image. The width
1046  * and height of the section of the image to decode are given in @p w and @p h
1047  * and also can't be higher than the total width and height of the image.
1048  *
1049  * The @p row_stride parameter indicates the length in bytes of each line in
1050  * the destination buffer and it has to be at least @p w * 4.
1051  *
1052  * All the other parameters are the same as in eet_data_image_read().
1053  *
1054  * On success the function returns 1, and 0 on failure. On failure the
1055  * parameter values may not contain any sensible data.
1056  *
1057  * @see eet_data_image_read()
1058  * @see eet_data_image_decode()
1059  * @see eet_data_image_decode_to_surface()
1060  * @see eet_data_image_read_to_surface_cipher()
1061  *
1062  * @since 1.0.2
1063  * @ingroup Eet_File_Image_Group
1064  */
1065 EAPI int
1066 eet_data_image_read_to_surface(Eet_File     *ef,
1067                                const char   *name,
1068                                unsigned int  src_x,
1069                                unsigned int  src_y,
1070                                unsigned int *d,
1071                                unsigned int  w,
1072                                unsigned int  h,
1073                                unsigned int  row_stride,
1074                                int          *alpha,
1075                                int          *compress,
1076                                int          *quality,
1077                                int          *lossy);
1078
1079 /**
1080  * Write image data to the named key in an eet file.
1081  * @param ef A valid eet file handle opened for writing.
1082  * @param name Name of the entry. eg: "/base/file_i_want".
1083  * @param data A pointer to the image pixel data.
1084  * @param w The width of the image in pixels.
1085  * @param h The height of the image in pixels.
1086  * @param alpha The alpha channel flag.
1087  * @param compress The compression amount.
1088  * @param quality The quality encoding amount.
1089  * @param lossy The lossiness flag.
1090  * @return Success if the data was encoded and written or not.
1091  *
1092  * This function takes image pixel data and encodes it in an eet file
1093  * stored under the supplied name key, and returns how many bytes were
1094  * actually written to encode the image data.
1095  *
1096  * The data expected is the same format as returned by eet_data_image_read.
1097  * If this is not the case weird things may happen. Width and height must
1098  * be between 1 and 8000 pixels. The alpha flags can be 0 or 1 (0 meaning
1099  * the alpha values are not useful and 1 meaning they are). Compress can
1100  * be from 0 to 9 (0 meaning no compression, 9 meaning full compression).
1101  * This is only used if the image is not lossily encoded. Quality is used on
1102  * lossy compression and should be a value from 0 to 100. The lossy flag
1103  * can be 0 or 1. 0 means encode losslessly and 1 means to encode with
1104  * image quality loss (but then have a much smaller encoding).
1105  *
1106  * On success this function returns the number of bytes that were required
1107  * to encode the image data, or on failure it returns 0.
1108  *
1109  * @see eet_data_image_read()
1110  * @see eet_data_image_encode()
1111  * @see eet_data_image_write_cipher()
1112  *
1113  * @since 1.0.0
1114  * @ingroup Eet_File_Image_Group
1115  */
1116 EAPI int
1117 eet_data_image_write(Eet_File    *ef,
1118                      const char  *name,
1119                      const void  *data,
1120                      unsigned int w,
1121                      unsigned int h,
1122                      int          alpha,
1123                      int          compress,
1124                      int          quality,
1125                      int          lossy);
1126
1127 /**
1128  * Decode Image data header only to get information.
1129  * @param data The encoded pixel data.
1130  * @param size The size, in bytes, of the encoded pixel data.
1131  * @param w A pointer to the unsigned int to hold the width in pixels.
1132  * @param h A pointer to the unsigned int to hold the height in pixels.
1133  * @param alpha A pointer to the int to hold the alpha flag.
1134  * @param compress A pointer to the int to hold the compression amount.
1135  * @param quality A pointer to the int to hold the quality amount.
1136  * @param lossy A pointer to the int to hold the lossiness flag.
1137  * @return 1 on success, 0 on failure.
1138  *
1139  * This function works exactly like eet_data_image_header_read(), but instead
1140  * of reading from an Eet file, it takes the buffer of size @p size pointed
1141  * by @p data, which must be a valid Eet encoded image.
1142  *
1143  * On success the function returns 1 indicating the header was read and
1144  * decoded properly, or 0 on failure.
1145  *
1146  * @see eet_data_image_header_read()
1147  * @see eet_data_image_header_decode_cipher()
1148  *
1149  * @since 1.0.0
1150  * @ingroup Eet_File_Image_Group
1151  */
1152 EAPI int
1153 eet_data_image_header_decode(const void   *data,
1154                              int           size,
1155                              unsigned int *w,
1156                              unsigned int *h,
1157                              int          *alpha,
1158                              int          *compress,
1159                              int          *quality,
1160                              int          *lossy);
1161
1162 /**
1163  * Decode Image data into pixel data.
1164  * @param data The encoded pixel data.
1165  * @param size The size, in bytes, of the encoded pixel data.
1166  * @param w A pointer to the unsigned int to hold the width in pixels.
1167  * @param h A pointer to the unsigned int to hold the height in pixels.
1168  * @param alpha A pointer to the int to hold the alpha flag.
1169  * @param compress A pointer to the int to hold the compression amount.
1170  * @param quality A pointer to the int to hold the quality amount.
1171  * @param lossy A pointer to the int to hold the lossiness flag.
1172  * @return The image pixel data decoded
1173  *
1174  * This function takes encoded pixel data and decodes it into raw RGBA
1175  * pixels on success.
1176  *
1177  * It works exactly like eet_data_image_read(), but it takes the encoded
1178  * data in the @p data buffer of size @p size, instead of reading from a file.
1179  * All the others parameters are also the same.
1180  *
1181  * On success the function returns a pointer to the image data decoded. The
1182  * calling application is responsible for calling free() on the image data
1183  * when it is done with it. On failure NULL is returned and the parameter
1184  * values may not contain any sensible data.
1185  *
1186  * @see eet_data_image_read()
1187  * @see eet_data_image_decode_cipher()
1188  *
1189  * @since 1.0.0
1190  * @ingroup Eet_File_Image_Group
1191  */
1192 EAPI void *
1193 eet_data_image_decode(const void   *data,
1194                       int           size,
1195                       unsigned int *w,
1196                       unsigned int *h,
1197                       int          *alpha,
1198                       int          *compress,
1199                       int          *quality,
1200                       int          *lossy);
1201
1202 /**
1203  * Decode Image data into pixel data and stores in the given buffer.
1204  * @param data The encoded pixel data.
1205  * @param size The size, in bytes, of the encoded pixel data.
1206  * @param src_x The starting x coordinate from where to dump the stream.
1207  * @param src_y The starting y coordinate from where to dump the stream.
1208  * @param d A pointer to the pixel surface.
1209  * @param w The expected width in pixels of the pixel surface to decode.
1210  * @param h The expected height in pixels of the pixel surface to decode.
1211  * @param row_stride The length of a pixels line in the destination surface.
1212  * @param alpha A pointer to the int to hold the alpha flag.
1213  * @param compress A pointer to the int to hold the compression amount.
1214  * @param quality A pointer to the int to hold the quality amount.
1215  * @param lossy A pointer to the int to hold the lossiness flag.
1216  * @return 1 on success, 0 otherwise.
1217  *
1218  * Like eet_data_image_read_to_surface(), but reading the given @p data buffer
1219  * instead of a file.
1220  *
1221  * On success the function returns 1, and 0 on failure. On failure the
1222  * parameter values may not contain any sensible data.
1223  *
1224  * @see eet_data_image_read_to_surface()
1225  * @see eet_data_image_decode_to_surface_cipher()
1226  *
1227  * @since 1.0.2
1228  * @ingroup Eet_File_Image_Group
1229  */
1230 EAPI int
1231 eet_data_image_decode_to_surface(const void   *data,
1232                                  int           size,
1233                                  unsigned int  src_x,
1234                                  unsigned int  src_y,
1235                                  unsigned int *d,
1236                                  unsigned int  w,
1237                                  unsigned int  h,
1238                                  unsigned int  row_stride,
1239                                  int          *alpha,
1240                                  int          *compress,
1241                                  int          *quality,
1242                                  int          *lossy);
1243
1244 /**
1245  * Encode image data for storage or transmission.
1246  * @param data A pointer to the image pixel data.
1247  * @param size_ret A pointer to an int to hold the size of the returned data.
1248  * @param w The width of the image in pixels.
1249  * @param h The height of the image in pixels.
1250  * @param alpha The alpha channel flag.
1251  * @param compress The compression amount.
1252  * @param quality The quality encoding amount.
1253  * @param lossy The lossiness flag.
1254  * @return The encoded image data.
1255  *
1256  * This function stakes image pixel data and encodes it with compression and
1257  * possible loss of quality (as a trade off for size) for storage or
1258  * transmission to another system.
1259  *
1260  * It works like eet_data_image_write(), but instead of writing the encoded
1261  * image into an Eet file, it allocates a new buffer of the size required and
1262  * returns the encoded data in it.
1263  *
1264  * On success this function returns a pointer to the encoded data that you
1265  * can free with free() when no longer needed.
1266  *
1267  * @see eet_data_image_write()
1268  * @see eet_data_image_read()
1269  * @see eet_data_image_encode_cipher()
1270  *
1271  * @since 1.0.0
1272  * @ingroup Eet_File_Image_Group
1273  */
1274 EAPI void *
1275 eet_data_image_encode(const void  *data,
1276                       int         *size_ret,
1277                       unsigned int w,
1278                       unsigned int h,
1279                       int          alpha,
1280                       int          compress,
1281                       int          quality,
1282                       int          lossy);
1283
1284 /**
1285  * @defgroup Eet_File_Image_Cipher_Group Image Store and Load using a Cipher
1286  *
1287  * Most of the @ref Eet_File_Image_Group have alternative versions
1288  * that accounts for ciphers to protect their content.
1289  *
1290  * @see @ref Eet_Cipher_Group
1291  *
1292  * @ingroup Eet_File_Image_Group
1293  */
1294
1295 /**
1296  * Read just the header data for an image and dont decode the pixels using a cipher.
1297  * @param ef A valid eet file handle opened for reading.
1298  * @param name Name of the entry. eg: "/base/file_i_want".
1299  * @param cipher_key The key to use as cipher.
1300  * @param w A pointer to the unsigned int to hold the width in pixels.
1301  * @param h A pointer to the unsigned int to hold the height in pixels.
1302  * @param alpha A pointer to the int to hold the alpha flag.
1303  * @param compress A pointer to the int to hold the compression amount.
1304  * @param quality A pointer to the int to hold the quality amount.
1305  * @param lossy A pointer to the int to hold the lossiness flag.
1306  * @return 1 on successful decode, 0 otherwise
1307  *
1308  * This function reads an image from an eet file stored under the named
1309  * key in the eet file and return a pointer to the decompressed pixel data.
1310  *
1311  * The other parameters of the image (width, height etc.) are placed into
1312  * the values pointed to (they must be supplied). The pixel data is a linear
1313  * array of pixels starting from the top-left of the image scanning row by
1314  * row from left to right. Each pixel is a 32bit value, with the high byte
1315  * being the alpha channel, the next being red, then green, and the low byte
1316  * being blue. The width and height are measured in pixels and will be
1317  * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes
1318  * that the alpha channel is not used. 1 denotes that it is significant.
1319  * Compress is filled with the compression value/amount the image was
1320  * stored with. The quality value is filled with the quality encoding of
1321  * the image file (0 - 100). The lossy flags is either 0 or 1 as to if
1322  * the image was encoded lossily or not.
1323  *
1324  * On success the function returns 1 indicating the header was read and
1325  * decoded properly, or 0 on failure.
1326  *
1327  * @see eet_data_image_header_read()
1328  *
1329  * @since 1.0.0
1330  * @ingroup Eet_File_Image_Cipher_Group
1331  */
1332 EAPI int
1333 eet_data_image_header_read_cipher(Eet_File     *ef,
1334                                   const char   *name,
1335                                   const char   *cipher_key,
1336                                   unsigned int *w,
1337                                   unsigned int *h,
1338                                   int          *alpha,
1339                                   int          *compress,
1340                                   int          *quality,
1341                                   int          *lossy);
1342
1343 /**
1344  * Read image data from the named key in the eet file using a cipher.
1345  * @param ef A valid eet file handle opened for reading.
1346  * @param name Name of the entry. eg: "/base/file_i_want".
1347  * @param cipher_key The key to use as cipher.
1348  * @param w A pointer to the unsigned int to hold the width in pixels.
1349  * @param h A pointer to the unsigned int to hold the height in pixels.
1350  * @param alpha A pointer to the int to hold the alpha flag.
1351  * @param compress A pointer to the int to hold the compression amount.
1352  * @param quality A pointer to the int to hold the quality amount.
1353  * @param lossy A pointer to the int to hold the lossiness flag.
1354  * @return The image pixel data decoded
1355  *
1356  * This function reads an image from an eet file stored under the named
1357  * key in the eet file and return a pointer to the decompressed pixel data.
1358  *
1359  * The other parameters of the image (width, height etc.) are placed into
1360  * the values pointed to (they must be supplied). The pixel data is a linear
1361  * array of pixels starting from the top-left of the image scanning row by
1362  * row from left to right. Each pixel is a 32bit value, with the high byte
1363  * being the alpha channel, the next being red, then green, and the low byte
1364  * being blue. The width and height are measured in pixels and will be
1365  * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes
1366  * that the alpha channel is not used. 1 denotes that it is significant.
1367  * Compress is filled with the compression value/amount the image was
1368  * stored with. The quality value is filled with the quality encoding of
1369  * the image file (0 - 100). The lossy flags is either 0 or 1 as to if
1370  * the image was encoded lossily or not.
1371  *
1372  * On success the function returns a pointer to the image data decoded. The
1373  * calling application is responsible for calling free() on the image data
1374  * when it is done with it. On failure NULL is returned and the parameter
1375  * values may not contain any sensible data.
1376  *
1377  * @see eet_data_image_read()
1378  *
1379  * @since 1.0.0
1380  * @ingroup Eet_File_Image_Cipher_Group
1381  */
1382 EAPI void *
1383 eet_data_image_read_cipher(Eet_File     *ef,
1384                            const char   *name,
1385                            const char   *cipher_key,
1386                            unsigned int *w,
1387                            unsigned int *h,
1388                            int          *alpha,
1389                            int          *compress,
1390                            int          *quality,
1391                            int          *lossy);
1392    
1393 /**
1394  * Read image data from the named key in the eet file using a cipher.
1395  * @param ef A valid eet file handle opened for reading.
1396  * @param name Name of the entry. eg: "/base/file_i_want".
1397  * @param cipher_key The key to use as cipher.
1398  * @param src_x The starting x coordinate from where to dump the stream.
1399  * @param src_y The starting y coordinate from where to dump the stream.
1400  * @param d A pointer to the pixel surface.
1401  * @param w The expected width in pixels of the pixel surface to decode.
1402  * @param h The expected height in pixels of the pixel surface to decode.
1403  * @param row_stride The length of a pixels line in the destination surface.
1404  * @param alpha A pointer to the int to hold the alpha flag.
1405  * @param compress A pointer to the int to hold the compression amount.
1406  * @param quality A pointer to the int to hold the quality amount.
1407  * @param lossy A pointer to the int to hold the lossiness flag.
1408  * @return 1 on success, 0 otherwise.
1409  *
1410  * This function reads an image from an eet file stored under the named
1411  * key in the eet file and return a pointer to the decompressed pixel data.
1412  *
1413  * The other parameters of the image (width, height etc.) are placed into
1414  * the values pointed to (they must be supplied). The pixel data is a linear
1415  * array of pixels starting from the top-left of the image scanning row by
1416  * row from left to right. Each pixel is a 32bit value, with the high byte
1417  * being the alpha channel, the next being red, then green, and the low byte
1418  * being blue. The width and height are measured in pixels and will be
1419  * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes
1420  * that the alpha channel is not used. 1 denotes that it is significant.
1421  * Compress is filled with the compression value/amount the image was
1422  * stored with. The quality value is filled with the quality encoding of
1423  * the image file (0 - 100). The lossy flags is either 0 or 1 as to if
1424  * the image was encoded lossily or not.
1425  *
1426  * On success the function returns 1, and 0 on failure. On failure the
1427  * parameter values may not contain any sensible data.
1428  *
1429  * @see eet_data_image_read_to_surface()
1430  *
1431  * @since 1.0.2
1432  * @ingroup Eet_File_Image_Cipher_Group
1433  */
1434 EAPI int
1435 eet_data_image_read_to_surface_cipher(Eet_File     *ef,
1436                                       const char   *name,
1437                                       const char   *cipher_key,
1438                                       unsigned int  src_x,
1439                                       unsigned int  src_y,
1440                                       unsigned int *d,
1441                                       unsigned int  w,
1442                                       unsigned int  h,
1443                                       unsigned int  row_stride,
1444                                       int          *alpha,
1445                                       int          *compress,
1446                                       int          *quality,
1447                                       int          *lossy);
1448
1449 /**
1450  * Write image data to the named key in an eet file using a cipher.
1451  * @param ef A valid eet file handle opened for writing.
1452  * @param name Name of the entry. eg: "/base/file_i_want".
1453  * @param cipher_key The key to use as cipher.
1454  * @param data A pointer to the image pixel data.
1455  * @param w The width of the image in pixels.
1456  * @param h The height of the image in pixels.
1457  * @param alpha The alpha channel flag.
1458  * @param compress The compression amount.
1459  * @param quality The quality encoding amount.
1460  * @param lossy The lossiness flag.
1461  * @return Success if the data was encoded and written or not.
1462  *
1463  * This function takes image pixel data and encodes it in an eet file
1464  * stored under the supplied name key, and returns how many bytes were
1465  * actually written to encode the image data.
1466  *
1467  * The data expected is the same format as returned by eet_data_image_read.
1468  * If this is not the case weird things may happen. Width and height must
1469  * be between 1 and 8000 pixels. The alpha flags can be 0 or 1 (0 meaning
1470  * the alpha values are not useful and 1 meaning they are). Compress can
1471  * be from 0 to 9 (0 meaning no compression, 9 meaning full compression).
1472  * This is only used if the image is not lossily encoded. Quality is used on
1473  * lossy compression and should be a value from 0 to 100. The lossy flag
1474  * can be 0 or 1. 0 means encode losslessly and 1 means to encode with
1475  * image quality loss (but then have a much smaller encoding).
1476  *
1477  * On success this function returns the number of bytes that were required
1478  * to encode the image data, or on failure it returns 0.
1479  *
1480  * @see eet_data_image_write()
1481  *
1482  * @since 1.0.0
1483  * @ingroup Eet_File_Image_Cipher_Group
1484  */
1485 EAPI int
1486 eet_data_image_write_cipher(Eet_File    *ef,
1487                             const char  *name,
1488                             const char  *cipher_key,
1489                             const void  *data,
1490                             unsigned int w,
1491                             unsigned int h,
1492                             int          alpha,
1493                             int          compress,
1494                             int          quality,
1495                             int          lossy);
1496
1497 /**
1498  * Decode Image data header only to get information using a cipher.
1499  * @param data The encoded pixel data.
1500  * @param cipher_key The key to use as cipher.
1501  * @param size The size, in bytes, of the encoded pixel data.
1502  * @param w A pointer to the unsigned int to hold the width in pixels.
1503  * @param h A pointer to the unsigned int to hold the height in pixels.
1504  * @param alpha A pointer to the int to hold the alpha flag.
1505  * @param compress A pointer to the int to hold the compression amount.
1506  * @param quality A pointer to the int to hold the quality amount.
1507  * @param lossy A pointer to the int to hold the lossiness flag.
1508  * @return 1 on success, 0 on failure.
1509  *
1510  * This function takes encoded pixel data and decodes it into raw RGBA
1511  * pixels on success.
1512  *
1513  * The other parameters of the image (width, height etc.) are placed into
1514  * the values pointed to (they must be supplied). The pixel data is a linear
1515  * array of pixels starting from the top-left of the image scanning row by
1516  * row from left to right. Each pixel is a 32bit value, with the high byte
1517  * being the alpha channel, the next being red, then green, and the low byte
1518  * being blue. The width and height are measured in pixels and will be
1519  * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes
1520  * that the alpha channel is not used. 1 denotes that it is significant.
1521  * Compress is filled with the compression value/amount the image was
1522  * stored with. The quality value is filled with the quality encoding of
1523  * the image file (0 - 100). The lossy flags is either 0 or 1 as to if
1524  * the image was encoded lossily or not.
1525  *
1526  * On success the function returns 1 indicating the header was read and
1527  * decoded properly, or 0 on failure.
1528  *
1529  * @see eet_data_image_header_decode()
1530  *
1531  * @since 1.0.0
1532  * @ingroup Eet_File_Image_Cipher_Group
1533  */
1534 EAPI int
1535 eet_data_image_header_decode_cipher(const void   *data,
1536                                     const char   *cipher_key,
1537                                     int           size,
1538                                     unsigned int *w,
1539                                     unsigned int *h,
1540                                     int          *alpha,
1541                                     int          *compress,
1542                                     int          *quality,
1543                                     int          *lossy);
1544
1545 /**
1546  * Decode Image data into pixel data using a cipher.
1547  * @param data The encoded pixel data.
1548  * @param cipher_key The key to use as cipher.
1549  * @param size The size, in bytes, of the encoded pixel data.
1550  * @param w A pointer to the unsigned int to hold the width in pixels.
1551  * @param h A pointer to the unsigned int to hold the height in pixels.
1552  * @param alpha A pointer to the int to hold the alpha flag.
1553  * @param compress A pointer to the int to hold the compression amount.
1554  * @param quality A pointer to the int to hold the quality amount.
1555  * @param lossy A pointer to the int to hold the lossiness flag.
1556  * @return The image pixel data decoded
1557  *
1558  * This function takes encoded pixel data and decodes it into raw RGBA
1559  * pixels on success.
1560  *
1561  * The other parameters of the image (width, height etc.) are placed into
1562  * the values pointed to (they must be supplied). The pixel data is a linear
1563  * array of pixels starting from the top-left of the image scanning row by
1564  * row from left to right. Each pixel is a 32bit value, with the high byte
1565  * being the alpha channel, the next being red, then green, and the low byte
1566  * being blue. The width and height are measured in pixels and will be
1567  * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes
1568  * that the alpha channel is not used. 1 denotes that it is significant.
1569  * Compress is filled with the compression value/amount the image was
1570  * stored with. The quality value is filled with the quality encoding of
1571  * the image file (0 - 100). The lossy flags is either 0 or 1 as to if
1572  * the image was encoded lossily or not.
1573  *
1574  * On success the function returns a pointer to the image data decoded. The
1575  * calling application is responsible for calling free() on the image data
1576  * when it is done with it. On failure NULL is returned and the parameter
1577  * values may not contain any sensible data.
1578  *
1579  * @see eet_data_image_decode()
1580  *
1581  * @since 1.0.0
1582  * @ingroup Eet_File_Image_Cipher_Group
1583  */
1584 EAPI void *
1585 eet_data_image_decode_cipher(const void   *data,
1586                              const char   *cipher_key,
1587                              int           size,
1588                              unsigned int *w,
1589                              unsigned int *h,
1590                              int          *alpha,
1591                              int          *compress,
1592                              int          *quality,
1593                              int          *lossy);
1594
1595 /**
1596  * Decode Image data into pixel data using a cipher.
1597  * @param data The encoded pixel data.
1598  * @param cipher_key The key to use as cipher.
1599  * @param size The size, in bytes, of the encoded pixel data.
1600  * @param src_x The starting x coordinate from where to dump the stream.
1601  * @param src_y The starting y coordinate from where to dump the stream.
1602  * @param d A pointer to the pixel surface.
1603  * @param w The expected width in pixels of the pixel surface to decode.
1604  * @param h The expected height in pixels of the pixel surface to decode.
1605  * @param row_stride The length of a pixels line in the destination surface.
1606  * @param alpha A pointer to the int to hold the alpha flag.
1607  * @param compress A pointer to the int to hold the compression amount.
1608  * @param quality A pointer to the int to hold the quality amount.
1609  * @param lossy A pointer to the int to hold the lossiness flag.
1610  * @return 1 on success, 0 otherwise.
1611  *
1612  * This function takes encoded pixel data and decodes it into raw RGBA
1613  * pixels on success.
1614  *
1615  * The other parameters of the image (alpha, compress etc.) are placed into
1616  * the values pointed to (they must be supplied). The pixel data is a linear
1617  * array of pixels starting from the top-left of the image scanning row by
1618  * row from left to right. Each pixel is a 32bit value, with the high byte
1619  * being the alpha channel, the next being red, then green, and the low byte
1620  * being blue. The width and height are measured in pixels and will be
1621  * greater than 0 when returned. The alpha flag is either 0 or 1. 0 denotes
1622  * that the alpha channel is not used. 1 denotes that it is significant.
1623  * Compress is filled with the compression value/amount the image was
1624  * stored with. The quality value is filled with the quality encoding of
1625  * the image file (0 - 100). The lossy flags is either 0 or 1 as to if
1626  * the image was encoded lossily or not.
1627  *
1628  * On success the function returns 1, and 0 on failure. On failure the
1629  * parameter values may not contain any sensible data.
1630  *
1631  * @see eet_data_image_decode_to_surface()
1632  *
1633  * @since 1.0.2
1634  * @ingroup Eet_File_Image_Cipher_Group
1635  */
1636 EAPI int
1637 eet_data_image_decode_to_surface_cipher(const void   *data,
1638                                         const char   *cipher_key,
1639                                         int           size,
1640                                         unsigned int  src_x,
1641                                         unsigned int  src_y,
1642                                         unsigned int *d,
1643                                         unsigned int  w,
1644                                         unsigned int  h,
1645                                         unsigned int  row_stride,
1646                                         int          *alpha,
1647                                         int          *compress,
1648                                         int          *quality,
1649                                         int          *lossy);
1650
1651 /**
1652  * Encode image data for storage or transmission using a cipher.
1653  * @param data A pointer to the image pixel data.
1654  * @param cipher_key The key to use as cipher.
1655  * @param size_ret A pointer to an int to hold the size of the returned data.
1656  * @param w The width of the image in pixels.
1657  * @param h The height of the image in pixels.
1658  * @param alpha The alpha channel flag.
1659  * @param compress The compression amount.
1660  * @param quality The quality encoding amount.
1661  * @param lossy The lossiness flag.
1662  * @return The encoded image data.
1663  *
1664  * This function stakes image pixel data and encodes it with compression and
1665  * possible loss of quality (as a trade off for size) for storage or
1666  * transmission to another system.
1667  *
1668  * The data expected is the same format as returned by eet_data_image_read.
1669  * If this is not the case weird things may happen. Width and height must
1670  * be between 1 and 8000 pixels. The alpha flags can be 0 or 1 (0 meaning
1671  * the alpha values are not useful and 1 meaning they are). Compress can
1672  * be from 0 to 9 (0 meaning no compression, 9 meaning full compression).
1673  * This is only used if the image is not lossily encoded. Quality is used on
1674  * lossy compression and should be a value from 0 to 100. The lossy flag
1675  * can be 0 or 1. 0 means encode losslessly and 1 means to encode with
1676  * image quality loss (but then have a much smaller encoding).
1677  *
1678  * On success this function returns a pointer to the encoded data that you
1679  * can free with free() when no longer needed.
1680  *
1681  * @see eet_data_image_encode()
1682  *
1683  * @since 1.0.0
1684  * @ingroup Eet_File_Image_Cipher_Group
1685  */
1686 EAPI void *
1687 eet_data_image_encode_cipher(const void   *data,
1688                              const char   *cipher_key,
1689                              unsigned int  w,
1690                              unsigned int  h,
1691                              int           alpha,
1692                              int           compress,
1693                              int           quality,
1694                              int           lossy,
1695                              int          *size_ret);
1696
1697 /**
1698  * @defgroup Eet_Cipher_Group Cipher, Identity and Protection Mechanisms
1699  *
1700  * Eet allows one to protect entries of an #Eet_File
1701  * individually. This may be used to ensure data was not tampered or
1702  * that third party does not read your data.
1703  *
1704  * @see @ref Eet_File_Cipher_Group
1705  * @see @ref Eet_File_Image_Cipher_Group
1706  *
1707  * @{
1708  */
1709
1710 /**
1711  * @typedef Eet_Key
1712  * Opaque handle that defines an identity (also known as key)
1713  * in Eet's cipher system.
1714  */
1715 typedef struct _Eet_Key   Eet_Key;
1716
1717 /**
1718  * @}
1719  */
1720
1721 /**
1722  * Callback used to request if needed the password of a private key.
1723  *
1724  * @param buffer the buffer where to store the password.
1725  * @param size the maximum password size (size of buffer, including '@\0').
1726  * @param rwflag if the buffer is also readable or just writable.
1727  * @param data currently unused, may contain some context in future.
1728  * @return 1 on success and password was set to @p buffer, 0 on failure.
1729  *
1730  * @since 1.2.0
1731  * @ingroup Eet_Cipher_Group
1732  */
1733 typedef int (*Eet_Key_Password_Callback) (char *buffer, int size, int rwflag, void *data);
1734
1735 /**
1736  * Create an Eet_Key needed for signing an eet file.
1737  *
1738  * The certificate should provide the public that match the private key.
1739  * No verification is done to ensure that.
1740  *
1741  * @param certificate_file The file where to find the certificate.
1742  * @param private_key_file The file that contains the private key.
1743  * @param cb Function to callback if password is required to unlock
1744  *        private key.
1745  * @return A key handle to use, or @c NULL on failure.
1746  *
1747  * @see eet_identity_close()
1748  *
1749  * @since 1.2.0
1750  * @ingroup Eet_Cipher_Group
1751  */
1752 EAPI Eet_Key *
1753 eet_identity_open(const char                *certificate_file,
1754                   const char                *private_key_file,
1755                   Eet_Key_Password_Callback  cb);
1756
1757 /**
1758  * Close and release all ressource used by an Eet_Key.  An
1759  * reference counter prevent it from being freed until all file
1760  * using it are also closed.
1761  *
1762  * @param key the key handle to close and free resources.
1763  *
1764  * @since 1.2.0
1765  * @ingroup Eet_Cipher_Group
1766  */
1767 EAPI void
1768 eet_identity_close(Eet_Key *key);
1769
1770 /**
1771  * Set a key to sign a file
1772  *
1773  * @param ef the file to set the identity.
1774  * @param key the key handle to set as identity.
1775  * @return #EET_ERROR_BAD_OBJECT if @p ef is invalid or
1776  *         #EET_ERROR_NONE on success.
1777  *
1778  * @since 1.2.0
1779  * @ingroup Eet_Cipher_Group
1780  */
1781 EAPI Eet_Error
1782 eet_identity_set(Eet_File *ef,
1783                  Eet_Key  *key);
1784
1785 /**
1786  * Display both private and public key of an Eet_Key.
1787  *
1788  * @param key the handle to print.
1789  * @param out where to print.
1790  *
1791  * @since 1.2.0
1792  * @ingroup Eet_Cipher_Group
1793  */
1794 EAPI void
1795 eet_identity_print(Eet_Key *key,
1796                    FILE    *out);
1797
1798 /**
1799  * Get the x509 der certificate associated with an Eet_File. Will return NULL
1800  * if the file is not signed.
1801  *
1802  * @param ef The file handle to query.
1803  * @param der_length The length of returned data, may be @c NULL.
1804  * @return the x509 certificate or @c NULL on error.
1805  *
1806  * @since 1.2.0
1807  * @ingroup Eet_Cipher_Group
1808  */
1809 EAPI const void *
1810 eet_identity_x509(Eet_File *ef,
1811                   int      *der_length);
1812
1813 /**
1814  * Get the raw signature associated with an Eet_File. Will return NULL
1815  * if the file is not signed.
1816  *
1817  * @param ef The file handle to query.
1818  * @param signature_length The length of returned data, may be @c NULL.
1819  * @return the raw signature or @c NULL on error.
1820  *
1821  * @ingroup Eet_Cipher_Group
1822  */
1823 EAPI const void *
1824 eet_identity_signature(Eet_File *ef,
1825                        int      *signature_length);
1826
1827 /**
1828  * Get the SHA1 associated with a file. Could be the one used to
1829  * sign the data or if the data where not signed, it will be the
1830  * SHA1 of the file.
1831  *
1832  * @param ef The file handle to query.
1833  * @param sha1_length The length of returned data, may be @c NULL.
1834  * @return the associated SHA1 or @c NULL on error.
1835  *
1836  * @since 1.2.0
1837  * @ingroup Eet_Cipher_Group
1838  */
1839 EAPI const void *
1840 eet_identity_sha1(Eet_File *ef,
1841                   int      *sha1_length);
1842
1843 /**
1844  * Display the x509 der certificate to out.
1845  *
1846  * @param certificate the x509 certificate to print
1847  * @param der_length The length the certificate.
1848  * @param out where to print.
1849  *
1850  * @since 1.2.0
1851  * @ingroup Eet_Cipher_Group
1852  */
1853 EAPI void
1854 eet_identity_certificate_print(const unsigned char *certificate,
1855                                int                  der_length,
1856                                FILE                *out);
1857
1858 /**
1859  * @defgroup Eet_Data_Group Eet Data Serialization
1860  *
1861  * Convenience functions to serialize and parse complex data
1862  * structures to binary blobs.
1863  *
1864  * While Eet core just handles binary blobs, it is often required
1865  * to save some structured data of different types, such as
1866  * strings, integers, lists, hashes and so on.
1867  *
1868  * Eet can serialize and then parse data types given some
1869  * construction instructions. These are defined in two levels:
1870  *
1871  * - #Eet_Data_Descriptor_Class to tell generic memory handling,
1872  *   such as the size of the type, how to allocate memory, strings,
1873  *   lists, hashes and so on.
1874  *
1875  * - #Eet_Data_Descriptor to tell inside such type, the members and
1876  *   their offsets inside the memory blob, their types and
1877  *   names. These members can be simple types or other
1878  *   #Eet_Data_Descriptor, allowing hierarchical types to be
1879  *   defined.
1880  *
1881  * Given that C provides no introspection, this process can be
1882  * quite cumbersome, so we provide lots of macros and convenience
1883  * functions to aid creating the types.
1884  *
1885  * We make now a quick overview of some of the most commonly used elements
1886  * of this part of the library. A simple example of a configuration system
1887  * will work as a somewhat real life example that is still simple enough to
1888  * follow.
1889  * Only the relevant sections will be shown here, but you can get the full
1890  * code @ref eet-data-simple.c "here".
1891  *
1892  * Ignoring the included headers, we'll begin by defining our configuration
1893  * struct.
1894  * @dontinclude eet-data-simple.c
1895  * @skip typedef
1896  * @until }
1897  *
1898  * When using Eet, you don't think in matters of what data the program needs
1899  * to run and which you would like to store. It's all the same and if it makes
1900  * more sense to keep them together, it's perfectly fine to do so. At the time
1901  * of telling Eet how your data is comprised you can leave out the things
1902  * that are runtime only and let Eet take care of the rest for you.
1903  *
1904  * The key used to store the config follows, as well as the variable used to
1905  * store our data descriptor.
1906  * This last one is very important. It's the one thing that Eet will use to
1907  * identify your data, both at the time of writing it to the file and when
1908  * loading from it.
1909  * @skipline MY_CONF
1910  * @skipline Eet_Data_Descriptor
1911  *
1912  * Now we'll see how to create this descriptor, so Eet knows how to handle
1913  * our data later on.
1914  * Begin our function by declaring an Eet_Data_Descriptor_Class, which is
1915  * used to create the actual descriptor. This class contains the name of
1916  * our data type, its size and several functions that dictate how Eet should
1917  * handle memory to allocate the necessary bits to bring our data to life.
1918  * You, as a user, will very hardly set this class' contents directly. The
1919  * most common scenario is to use one of the provided macros that set it using
1920  * the Eina data types, so that's what we'll be doing across all our examples.
1921  * @skip static void
1922  * @until eet_data_descriptor_stream_new
1923  *
1924  * Now that we have our descriptor, we need to make it describe something.
1925  * We do so by telling it which members of our struct we want it to know about
1926  * and their types.
1927  * The eet_data_descriptor_element_add() function takes care of this, but it's
1928  * too cumbersome for normal use, so several macros are provided that make
1929  * it easier to handle. Even with them, however, code can get very repetitive
1930  * and it's not uncommon to define custom macros using them to save on typing.
1931  * @skip #define
1932  * @until }
1933  *
1934  * Now our descriptor knows about the parts of our structure that we are
1935  * interesting in saving. You can see that not all of them are there, yet Eet
1936  * will find those that need saving and do the right thing. When loading our
1937  * data, any non-described fields in the structure will be zeroed, so there's
1938  * no need to worry about garbage memory in them.
1939  * Refer to the documentation of #EET_DATA_DESCRIPTOR_ADD_BASIC to understand
1940  * what our macro does.
1941  *
1942  * We are done with our descriptor init function and it's proper to have the
1943  * relevant shutdown. Proper coding guidelines indiciate that all memory
1944  * allocated should be freed when the program ends, and since you will most
1945  * likely keep your descriptor around for the life or your application, it's
1946  * only right to free it at the end.
1947  * @skip static void
1948  * @until }
1949  *
1950  * Not listed here, but included in the full example are functions to create
1951  * a blank configuration and free it. The first one will only be used when
1952  * no file exists to load from, or nothing is found in it, but the latter is
1953  * used regardless of where our data comes from. Unless you are reading direct
1954  * data from the Eet file, you will be in charge of freeing anything loaded
1955  * from it.
1956  *
1957  * Now it's time to look at how we can load our config from some file.
1958  * Begin by opening the Eet file normally.
1959  * @skip static My_Conf_Type
1960  * @until }
1961  *
1962  * And now we need to read the data from the file and decode it using our
1963  * descriptor. Fortunately, that's all done in one single step.
1964  * @until goto
1965  *
1966  * And that's it for all Eet cares about. But since we are dealing with a
1967  * common case, as is save and load of user configurations, the next fragment
1968  * of code shows why we have a version field in our struct, and how you can
1969  * use it to load older configuration files and update them as needed.
1970  * @until }
1971  *
1972  * Finally, clsoe the file and return the newly loaded config data.
1973  * @until }
1974  *
1975  * Saving data is just as easy. The full version of the following function
1976  * includes code to save to a temporary file first, so you can be sure not
1977  * to lose all your data in the case of a failure mid-writing. You can look
1978  * at it @ref eet-data-simple.c "here".
1979  * @skip static Eina_Bool
1980  * @until {
1981  * @skipline Eina_Bool ret
1982  * @skip eet_open
1983  * @until eet_close
1984  * @skip return
1985  * @until }
1986  *
1987  * To close, our main function, which doesn't do much. Just take some arguments
1988  * from the command line with the name of the file to load and another one
1989  * where to save again. If input file doesn't exist, a new config structure
1990  * will be created and saved to our output file.
1991  * @skip int main
1992  * @until return ret
1993  * @until }
1994  *
1995  * The following is a list of more advanced and detailed examples.
1996  * @li @ref eet_data_nested_example
1997  * @li @ref eet_data_file_descriptor
1998  */
1999
2000 /**
2001  * @page eet_data_nested_example Nested structures and Eet Data Descriptors
2002  *
2003  * We've seen already a simple example of how to use Eet Data Descriptors
2004  * to handle our structures, but it didn't show how this works when you
2005  * have structures inside other structures.
2006  *
2007  * Now, there's a very simple case of this, for when you have inline structs
2008  * to keep your big structure more organized, you don't need anything else
2009  * besides what @ref eet-data-simple.c "this simple example does".
2010  * Just use something like @p some_struct.sub_struct.member when adding the
2011  * member to the descriptor and it will work.
2012  *
2013  * For example:
2014  * @code
2015  * typedef struct
2016  * {
2017  *    int a_number;
2018  *    char *a_string;
2019  *    struct {
2020  *       int other_num;
2021  *       int one_more;
2022  *    } sub;
2023  * } some_struct;
2024  *
2025  * void some_function()
2026  * {
2027  *    ...
2028  *    my_desc = eet_data_descriptor_stream_new(&eddc);
2029  *    EET_DATA_DESCRIPTOR_ADD_BASIC(my_desc, some_struct, "a_number",
2030  *                                  a_number, EET_T_INT);
2031  *    EET_DATA_DESCRIPTOR_ADD_BASIC(my_desc, some_struct, "a_string",
2032  *                                  a_string, EET_T_STRING);
2033  *    EET_DATA_DESCRIPTOR_ADD_BASIC(my_desc, some_struct, "sub.other_num",
2034  *                                  sub.other_num, EET_T_INT);
2035  *    EET_DATA_DESCRIPTOR_ADD_BASIC(my_desc, some_struct, "sub.one_more",
2036  *                                  sub.one_more", EET_T_INT);
2037  *    ...
2038  * }
2039  * @endcode
2040  *
2041  * But this is not what we are here for today. When we talk about nested
2042  * structures, what we really want are things like lists and hashes to be
2043  * taken into consideration automatically, and all their contents saved and
2044  * loaded just like ordinary integers and strings are.
2045  *
2046  * And of course, Eet can do that, and considering the work it saves you as a
2047  * programmer, we could say it's even easier to do than handling just integers.
2048  *
2049  * Let's begin with our example then, which is not all too different from the
2050  * simple one introduced earlier.
2051  *
2052  * We won't ignore the headers this time to show how easy it is to use Eina
2053  * data types with Eet, but we'll still skip most of the code that is not
2054  * pertinent to what we want to show now, but as usual, you can get it full
2055  * by follwing @ref eet-data-nested.c "this link".
2056  *
2057  * @dontinclude eet-data-nested.c
2058  * @skipline Eina.h
2059  * @skipline Eet.h
2060  * @skip typedef struct
2061  * @until } My_Conf_Subtype
2062  *
2063  * Extremely similar to our previous example. Just a new struct in there, and
2064  * a pointer to a list in the one we already had. Handling a list of subtypes
2065  * is easy on our program, but now we'll see what Eet needs to work with them
2066  * (Hint: it's easy too).
2067  * @skip _my_conf_descriptor
2068  * @until _my_conf_sub_descriptor
2069  *
2070  * Since we have two structures now, it's only natural that we'll need two
2071  * descriptors. One for each, which will be defined exactly as before.
2072  * @skip static void
2073  * @until eddc
2074  * @skip EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET
2075  * @until _my_conf_sub_descriptor
2076  *
2077  * We create our descriptors, each for one type, and as before, we are going to
2078  * use a simple macro to set their contents, to save on typing.
2079  * @skip #define
2080  * @until EET_T_UCHAR
2081  *
2082  * So far, nothing new. We have our descriptors and we know already how to
2083  * save them separately. But what we want is to link them together, and even
2084  * more so, we want our main type to hold a list of more than one of the new
2085  * sub type. So how do we do that?
2086  *
2087  * Simple enough, we tell Eet that our main descriptor will hold a list, of
2088  * which each node will point to some type described by our new descriptor.
2089  * @skip EET_DATA_DESCRIPTOR_ADD_LIST
2090  * @until _my_conf_sub_descriptor
2091  *
2092  * And that's all. We are closing the function now so as to not leave dangling
2093  * curly braces, but there's nothing more to show in this example. Only other
2094  * additions are the necessary code to free our new data, but you can see it
2095  * in the full code listing.
2096  * @until }
2097  */
2098
2099 /**
2100  * @page eet_data_file_descriptor Advanced use of Eet Data Descriptors
2101  *
2102  * A real life example is usually the best way to see how things are used,
2103  * but they also involve a lot more code than what needs to be shown, so
2104  * instead of going that way, we'll be borrowing some pieces from one in
2105  * the following example. It's been slightly modified from the original
2106  * source to show more of the varied ways in which Eet can handle our data.
2107  *
2108  * @ref eet-data-file_descriptor.c "This example" shows a cache of user
2109  * accounts and messages received, and it's a bit more interactive than
2110  * previous examples.
2111  *
2112  * Let's begin by looking at the structures we'll be using. First we have
2113  * one to define the messages the user receives and one for the one he posts.
2114  * Straight forward and nothing new here.
2115  * @dontinclude eet-data-file_descriptor.c
2116  * @skip typedef
2117  * @until My_Post
2118  *
2119  * One more to declare the account itself. This one will contain a list of
2120  * all messages received, and the posts we make ourselves will be kept in an
2121  * array. No special reason other than to show how to use arrays with Eet.
2122  * @until My_Account
2123  *
2124  * Finally, the main structure to hold our cache of accounts. We'll be looking
2125  * for these accounts by their names, so let's keep them in a hash, using
2126  * that name as the key.
2127  * @until My_Cache
2128  *
2129  * As explained before, we need one descriptor for each struct we want Eet
2130  * to handle, but this time we also want to keep around our Eet file and its
2131  * string dictionary. You will see why in a moment.
2132  * @skip Eet_Data_Descriptor
2133  * @until _my_post_descriptor
2134  * @skip Eet_File
2135  * @until Eet_Dictionary
2136  *
2137  * The differences begin now. They aren't much, but we'll be creating our
2138  * descriptors differently. Things can be added to our cache, but we won't
2139  * be modifying the current contents, so we can consider the data read from
2140  * it to be read-only, and thus allow Eet to save time and memory by not
2141  * duplicating thins unnecessary.
2142  * @skip static void
2143  * @until _my_post_descriptor
2144  *
2145  * As the comment in the code explains, we are asking Eet to give us strings
2146  * directly from the mapped file, which avoids having to load it in memory
2147  * and data duplication.
2148  * Of course, there are things to take into account when doing things this
2149  * way, and they will be mentioned as we encounter those special cases.
2150  *
2151  * Next comes the actual description of our data, just like we did in the
2152  * previous examples.
2153  * @skip #define
2154  * @until #undef
2155  * @until #define
2156  * @until #undef
2157  *
2158  * And the account struct's description doesn't add much new, but it's worth
2159  * commenting on it.
2160  * @skip #define
2161  * @until _my_post_descriptor
2162  *
2163  * How to add a list we've seen before, but now we are also adding an array.
2164  * There's nothing really special about it, but it's important to note that
2165  * the EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY is used to add arrays of variable
2166  * length to a descriptor. That is, arrays just like the one we defined.
2167  * Since there's no way in C to know how long they are, we need to keep
2168  * track of the count ourselves and Eet needs to know how to do so as well.
2169  * That's what the @p posts_count member of our struct is for. When adding
2170  * our array member, this macro will look for another variable in the struct
2171  * named just like the array, but with @p _count attached to the end.
2172  * When saving our data, Eet will know how many elements the array contains
2173  * by looking into this count variable. When loading back from a file, this
2174  * variable will be set to the right number of elements.
2175  *
2176  * Another option for arrays is to use EET_DATA_DESCRIPTOR_ADD_ARRAY, which
2177  * takes care of fixed sized arrays.
2178  * For example, let's suppose that we want to keep track of only the last
2179  * ten posts the user sent, and we declare our account struct as follows
2180  * @code
2181  * typedef struct
2182  * {
2183  *    unsigned int id;
2184  *    const char  *name;
2185  *    Eina_List   *messages;
2186  *    My_Post      posts[10];
2187  * } My_Account;
2188  * @endcode
2189  * Then we would add the array to our descriptor with
2190  * @code
2191  * EET_DATA_DESCRIPTOR_ADD_ARRAY(_my_account_descriptor, My_Account, "posts",
2192  *                               posts, _my_post_descriptor);
2193  * @endcode
2194  *
2195  * Notice how this time we don't have a @p posts_count variable in our struct.
2196  * We could have it for the program to keep track of how many posts the
2197  * array actually contains, but Eet no longer needs it. Being defined that
2198  * way the array is already taking up all the memory needed for the ten
2199  * elements, and it is possible in C to determine how much it is in code.
2200  * When saving our data, Eet will just dump the entire memory blob into the
2201  * file, regardless of how much of it is really used. So it's important to
2202  * take into consideration this kind of things when defining your data types.
2203  * Each has its uses, its advantages and disadvantages and it's up to you
2204  * to decide which to use.
2205  *
2206  * Now, going back to our example, we have to finish adding our data to the
2207  * descriptors. We are only missing the main one for the cache, which
2208  * contains our hash of accounts.
2209  * Unless you are using your own hash functions when setting the descriptor
2210  * class, always use hashes with string type keys.
2211  * @skip #define
2212  * @until }
2213  *
2214  * If you remember, we told Eet not to duplicate memory when possible at the
2215  * time of loading back our data. But this doesn't mean everything will be
2216  * loaded straight from disk and we don't have to worry about freeing it.
2217  * Data in the Eet file is compressed and encoded, so it still needs to be
2218  * decoded and memory will be allocated to convert it back into something we
2219  * can use. We also need to take care of anything we add in the current
2220  * instance of the program.
2221  * To summarize, any string we get from Eet is likely to be a pointer to the
2222  * internal dictionary, and trying to free it will, in the best case, crash
2223  * our application right away.
2224  *
2225  * So how do we know if we have to free a string? We check if it's part of
2226  * the dictionary, and if it's not there we can be sure it's safe to get
2227  * rid of it.
2228  * @skip static void
2229  * @skip }
2230  * @skip static void
2231  * @until }
2232  *
2233  * See how this is used when adding a new message to our cache.
2234  * @skip static My_Message
2235  * @until return msg
2236  * @until free(msg)
2237  * @until }
2238  *
2239  * Skipping all the utility functions used by our program (remember you can
2240  * look at the full example @ref eet-data-file_descriptor.c "here") we get to
2241  * our cache loading code. Nothing out of the ordinary at first, just the
2242  * same old open file, read data using our main descriptor to decode it
2243  * into something we can use and check version of loaded data and if it doesn't
2244  * match, do something accordingly.
2245  * @skip static My_Cache
2246  * @until }
2247  * @until }
2248  * @until }
2249  *
2250  * Then comes the interesting part. Remember how we kept two more global
2251  * variables with our descriptors? One of them we already used to check if
2252  * it was right to free a string or not, but we didn't know where it came from.
2253  * Loading our data straight from the mmapped file means that we can't close
2254  * it until we are done using it, so we need to keep its handler around until
2255  * then. It also means that any changes done to the file can, and will,
2256  * invalidate all our pointers to the file backed data, so if we add something
2257  * and save the file, we need to reload our cache.
2258  *
2259  * Thus our load function checks if we had an open file, if there is it gets
2260  * closed and our variable is updated to the new handler. Then we get the
2261  * string dictionary we use to check if a string is part of it or not.
2262  * Updating any references to the cache data is up you as a programmer to
2263  * handle properly, there's nothing Eet can do in this situation.
2264  * @until }
2265  *
2266  * The save function doesn't have anything new, and all that's left after it
2267  * is the main program, which doesn't really have anything of interest within
2268  * the scope of what we are learning.
2269  */
2270
2271 /**
2272  * @addtogroup Eet_Data_Group
2273  * @{
2274  */
2275 #define EET_T_UNKNOW         0 /**< Unknown data encoding type */
2276 #define EET_T_CHAR           1 /**< Data type: char */
2277 #define EET_T_SHORT          2 /**< Data type: short */
2278 #define EET_T_INT            3 /**< Data type: int */
2279 #define EET_T_LONG_LONG      4 /**< Data type: long long */
2280 #define EET_T_FLOAT          5 /**< Data type: float */
2281 #define EET_T_DOUBLE         6 /**< Data type: double */
2282 #define EET_T_UCHAR          7 /**< Data type: unsigned char */
2283 #define EET_T_USHORT         8 /**< Data type: unsigned short */
2284 #define EET_T_UINT           9 /**< Data type: unsigned int */
2285 #define EET_T_ULONG_LONG     10 /**< Data type: unsigned long long */
2286 #define EET_T_STRING         11 /**< Data type: char * */
2287 #define EET_T_INLINED_STRING 12 /**< Data type: char * (but compressed inside the resulting eet) */
2288 #define EET_T_NULL           13 /**< Data type: (void *) (only use it if you know why) */
2289 #define EET_T_F32P32         14 /**< Data type: fixed point 32.32 */
2290 #define EET_T_F16P16         15 /**< Data type: fixed point 16.16 */
2291 #define EET_T_F8P24          16 /**< Data type: fixed point 8.24 */
2292 #define EET_T_LAST           18 /**< Last data type */
2293
2294 #define EET_G_UNKNOWN        100 /**< Unknown group data encoding type */
2295 #define EET_G_ARRAY          101 /**< Fixed size array group type */
2296 #define EET_G_VAR_ARRAY      102 /**< Variable size array group type */
2297 #define EET_G_LIST           103 /**< Linked list group type */
2298 #define EET_G_HASH           104 /**< Hash table group type */
2299 #define EET_G_UNION          105 /**< Union group type */
2300 #define EET_G_VARIANT        106 /**< Selectable subtype group */
2301 #define EET_G_LAST           107 /**< Last group type */
2302
2303 #define EET_I_LIMIT          128 /**< Other type exist but are reserved for internal purpose. */
2304
2305 /**
2306  * @typedef Eet_Data_Descriptor
2307  *
2308  * Opaque handle that have information on a type members.
2309  *
2310  * Descriptors are created using an #Eet_Data_Descriptor_Class, and they
2311  * describe the contents of the structure that will be serialized by Eet.
2312  * Not all members need be described by it, just those that should be handled
2313  * by Eet. This way it's possible to have one structure with both data to be
2314  * saved to a file, like application configuration, and runtime information
2315  * that would be meaningless to store, but is appropriate to keep together
2316  * during the program execution.
2317  * The members are added by means of
2318  * EET_DATA_DESCRIPTOR_ADD_BASIC(), EET_DATA_DESCRIPTOR_ADD_SUB(),
2319  * EET_DATA_DESCRIPTOR_ADD_LIST(), EET_DATA_DESCRIPTOR_ADD_HASH()
2320  * or eet_data_descriptor_element_add().
2321  *
2322  * @see eet_data_descriptor_stream_new()
2323  * @see eet_data_descriptor_file_new()
2324  * @see eet_data_descriptor_free()
2325  */
2326 typedef struct _Eet_Data_Descriptor         Eet_Data_Descriptor;
2327
2328 /**
2329  * @def EET_DATA_DESCRIPTOR_CLASS_VERSION
2330  * The version of #Eet_Data_Descriptor_Class at the time of the
2331  * distribution of the sources. One should define this to its
2332  * version member so it is compatible with abi changes, or at least
2333  * will not crash with them.
2334  */
2335 #define EET_DATA_DESCRIPTOR_CLASS_VERSION 4
2336
2337 /**
2338  * @typedef Eet_Data_Descriptor_Class
2339  *
2340  * Instructs Eet about memory management for different needs under
2341  * serialization and parse process.
2342  */
2343 typedef struct _Eet_Data_Descriptor_Class   Eet_Data_Descriptor_Class;
2344
2345 typedef int         (*Eet_Descriptor_Hash_Foreach_Callback_Callback)(void *h, const char *k, void *dt, void *fdt);
2346   
2347 typedef void       *(*Eet_Descriptor_Mem_Alloc_Callback)(size_t size);
2348 typedef void        (*Eet_Descriptor_Mem_Free_Callback)(void *mem);
2349 typedef char       *(*Eet_Descriptor_Str_Alloc_Callback)(const char *str);
2350 typedef void        (*Eet_Descriptor_Str_Free_Callback)(const char *str);
2351 typedef void       *(*Eet_Descriptor_List_Next_Callback)(void *l);
2352 typedef void       *(*Eet_Descriptor_List_Append_Callback)(void *l, void *d);
2353 typedef void       *(*Eet_Descriptor_List_Data_Callback)(void *l);
2354 typedef void       *(*Eet_Descriptor_List_Free_Callback)(void *l);
2355 typedef void        (*Eet_Descriptor_Hash_Foreach_Callback)(void *h, Eet_Descriptor_Hash_Foreach_Callback_Callback func, void *fdt);
2356 typedef void       *(*Eet_Descriptor_Hash_Add_Callback)(void *h, const char *k, void *d);
2357 typedef void        (*Eet_Descriptor_Hash_Free_Callback)(void *h);
2358 typedef char       *(*Eet_Descriptor_Str_Direct_Alloc_Callback)(const char *str);
2359 typedef void        (*Eet_Descriptor_Str_Direct_Free_Callback)(const char *str);
2360 typedef const char *(*Eet_Descriptor_Type_Get_Callback)(const void *data, Eina_Bool *unknow);
2361 typedef Eina_Bool   (*Eet_Descriptor_Type_Set_Callback)(const char *type, void *data, Eina_Bool unknow);
2362 typedef void       *(*Eet_Descriptor_Array_Alloc_Callback)(size_t size);
2363 typedef void        (*Eet_Descriptor_Array_Free_Callback)(void *mem);
2364 /**
2365  * @struct _Eet_Data_Descriptor_Class
2366  *
2367  * Instructs Eet about memory management for different needs under
2368  * serialization and parse process.
2369  *
2370  * The list and hash methods match the Eina API, so for a more detalied
2371  * reference on them, look at the Eina_List and Eina_Hash documentation,
2372  * respectively.
2373  * For the most part these will be used with the standard Eina functions,
2374  * so using EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET() and
2375  * EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET() will set up everything
2376  * accordingly.
2377  */
2378 struct _Eet_Data_Descriptor_Class
2379 {
2380    int         version;  /**< ABI version. Should always be set to #EET_DATA_DESCRIPTOR_CLASS_VERSION */
2381    const char *name;  /**< Name of the user data type to be serialized */
2382    int         size;  /**< Size in bytes of the user data type to be serialized */
2383    struct {
2384      Eet_Descriptor_Mem_Alloc_Callback mem_alloc; /**< how to allocate memory (usually malloc()) */
2385      Eet_Descriptor_Mem_Free_Callback mem_free; /**< how to free memory (usually free()) */
2386      Eet_Descriptor_Str_Alloc_Callback str_alloc; /**< how to allocate a string */
2387      Eet_Descriptor_Str_Free_Callback str_free; /**< how to free a string */
2388      Eet_Descriptor_List_Next_Callback list_next; /**< how to iterate to the next element of a list. Receives and should return the list node. */
2389      Eet_Descriptor_List_Append_Callback list_append; /**< how to append data @p d to list which head node is @p l */
2390      Eet_Descriptor_List_Data_Callback list_data; /**< retrieves the data from node @p l */
2391      Eet_Descriptor_List_Free_Callback list_free; /**< free all the nodes from the list which head node is @p l */
2392      Eet_Descriptor_Hash_Foreach_Callback hash_foreach; /**< iterates over all elements in the hash @p h in no specific order */
2393      Eet_Descriptor_Hash_Add_Callback hash_add; /**< add a new data @p d with key @p k in hash @p h */
2394      Eet_Descriptor_Hash_Free_Callback hash_free; /**< free all entries from the hash @p h */
2395      Eet_Descriptor_Str_Direct_Alloc_Callback str_direct_alloc; /**< how to allocate a string directly from file backed/mmaped region pointed by @p str */
2396      Eet_Descriptor_Str_Direct_Free_Callback str_direct_free; /**< how to free a string returned by str_direct_alloc */
2397      Eet_Descriptor_Type_Get_Callback type_get; /**< get the type, as used in the union or variant mapping, that should be used to store the given data into the eet file. */
2398      Eet_Descriptor_Type_Set_Callback type_set; /**< called when loading a mapped type with the given @p type used to describe the type in the descriptor */
2399      Eet_Descriptor_Array_Alloc_Callback array_alloc; /**< how to allocate memory for array (usually malloc()) */
2400      Eet_Descriptor_Array_Free_Callback array_free; /**< how to free memory for array (usually free()) */
2401    } func;
2402 };
2403
2404 /**
2405  * @}
2406  */
2407
2408   
2409 /**
2410  * Create a new empty data structure descriptor.
2411  * @param name The string name of this data structure (most be a
2412  *        global constant and never change).
2413  * @param size The size of the struct (in bytes).
2414  * @param func_list_next The function to get the next list node.
2415  * @param func_list_append The function to append a member to a list.
2416  * @param func_list_data The function to get the data from a list node.
2417  * @param func_list_free The function to free an entire linked list.
2418  * @param func_hash_foreach The function to iterate through all
2419  *        hash table entries.
2420  * @param func_hash_add The function to add a member to a hash table.
2421  * @param func_hash_free The function to free an entire hash table.
2422  * @return A new empty data descriptor.
2423  *
2424  * This function creates a new data descriptore and returns a handle to the
2425  * new data descriptor. On creation it will be empty, containing no contents
2426  * describing anything other than the shell of the data structure.
2427  *
2428  * You add structure members to the data descriptor using the macros
2429  * EET_DATA_DESCRIPTOR_ADD_BASIC(), EET_DATA_DESCRIPTOR_ADD_SUB() and
2430  * EET_DATA_DESCRIPTOR_ADD_LIST(), depending on what type of member you are
2431  * adding to the description.
2432  *
2433  * Once you have described all the members of a struct you want loaded, or
2434  * saved eet can load and save those members for you, encode them into
2435  * endian-independent serialised data chunks for transmission across a
2436  * a network or more.
2437  *
2438  * The function pointers to the list and hash table functions are only
2439  * needed if you use those data types, else you can pass NULL instead.
2440  *
2441  * @since 1.0.0
2442  * @ingroup Eet_Data_Group
2443  *
2444  * @deprecated use eet_data_descriptor_stream_new() or
2445  *             eet_data_descriptor_file_new()
2446  */
2447 EINA_DEPRECATED EAPI Eet_Data_Descriptor *
2448 eet_data_descriptor_new(const char *name,
2449                         int size,
2450                         Eet_Descriptor_List_Next_Callback func_list_next,
2451                         Eet_Descriptor_List_Append_Callback func_list_append,
2452                         Eet_Descriptor_List_Data_Callback func_list_data,
2453                         Eet_Descriptor_List_Free_Callback func_list_free,
2454                         Eet_Descriptor_Hash_Foreach_Callback func_hash_foreach,
2455                         Eet_Descriptor_Hash_Add_Callback func_hash_add,
2456                         Eet_Descriptor_Hash_Free_Callback func_hash_free);
2457 /*
2458  * FIXME:
2459  *
2460  * moving to this api from the old above. this will break things when the
2461  * move happens - but be warned
2462  */
2463 EINA_DEPRECATED EAPI Eet_Data_Descriptor *
2464 eet_data_descriptor2_new(const Eet_Data_Descriptor_Class *eddc);
2465 EINA_DEPRECATED EAPI Eet_Data_Descriptor *
2466 eet_data_descriptor3_new(const Eet_Data_Descriptor_Class *eddc);
2467
2468 /**
2469  * This function creates a new data descriptor and returns a handle to the
2470  * new data descriptor. On creation it will be empty, containing no contents
2471  * describing anything other than the shell of the data structure.
2472  * @param eddc The class from where to create the data descriptor.
2473  *
2474  * You add structure members to the data descriptor using the macros
2475  * EET_DATA_DESCRIPTOR_ADD_BASIC(), EET_DATA_DESCRIPTOR_ADD_SUB() and
2476  * EET_DATA_DESCRIPTOR_ADD_LIST(), depending on what type of member you are
2477  * adding to the description.
2478  *
2479  * Once you have described all the members of a struct you want loaded or
2480  * savedi, eet can load and save those members for you, encode them into
2481  * endian-independent serialised data chunks for transmission across a
2482  * a network or more.
2483  *
2484  * This function specially ignores str_direct_alloc and str_direct_free. It
2485  * is useful when the eet_data you are reading doesn't have a dictionary,
2486  * like network stream or IPC. It also mean that all string will be allocated
2487  * and duplicated in memory.
2488  *
2489  * @since 1.2.3
2490  * @ingroup Eet_Data_Group
2491  */
2492 EAPI Eet_Data_Descriptor *
2493 eet_data_descriptor_stream_new(const Eet_Data_Descriptor_Class *eddc);
2494
2495 /**
2496  * This function creates a new data descriptor and returns a handle to the
2497  * new data descriptor. On creation it will be empty, containing no contents
2498  * describing anything other than the shell of the data structure.
2499  * @param eddc The class from where to create the data descriptor.
2500  *
2501  * You add structure members to the data descriptor using the macros
2502  * EET_DATA_DESCRIPTOR_ADD_BASIC(), EET_DATA_DESCRIPTOR_ADD_SUB() and
2503  * EET_DATA_DESCRIPTOR_ADD_LIST(), depending on what type of member you are
2504  * adding to the description.
2505  *
2506  * Once you have described all the members of a struct you want loaded or
2507  * savedi, eet can load and save those members for you, encode them into
2508  * endian-independent serialised data chunks for transmission across a
2509  * a network or more.
2510  *
2511  * This function uses str_direct_alloc and str_direct_free. It is
2512  * useful when the eet_data you are reading come from a file and
2513  * have a dictionary. This will reduce memory use and improve the
2514  * possibility for the OS to page this string out.
2515  * However, the load speed and memory saving comes with some drawbacks to keep
2516  * in mind. If you never modify the contents of the structures loaded from
2517  * the file, all you need to remember is that closing the eet file will make
2518  * the strings go away. On the other hand, should you need to free a string,
2519  * before doing so you have to verify that it's not part of the eet dictionary.
2520  * You can do this in the following way, assuming @p ef is a valid Eet_File
2521  * and @p str is a string loaded from said file.
2522  *
2523  * @code
2524  * void eet_string_free(Eet_File *ef, const char *str)
2525  * {
2526  *    Eet_Dictionary *dict = eet_dictionary_get(ef);
2527  *    if (dict && eet_dictionary_string_check(dict, str))
2528  *      {
2529  *         // The file contains a dictionary and the given string is a part of
2530  *         // of it, so we can't free it, just return.
2531  *         return;
2532  *      }
2533  *    // We assume eina_stringshare was used on the descriptor
2534  *    eina_stringshare_del(str);
2535  * }
2536  * @endcode
2537  *
2538  * @since 1.2.3
2539  * @ingroup Eet_Data_Group
2540  */
2541 EAPI Eet_Data_Descriptor *
2542 eet_data_descriptor_file_new(const Eet_Data_Descriptor_Class *eddc);
2543
2544 /**
2545  * This function is an helper that set all the parameters of an
2546  * Eet_Data_Descriptor_Class correctly when you use Eina data type
2547  * with a stream.
2548  * @param eddc The Eet_Data_Descriptor_Class you want to set.
2549  * @param eddc_size The size of the Eet_Data_Descriptor_Class at the compilation time.
2550  * @param name The name of the structure described by this class.
2551  * @param size The size of the structure described by this class.
2552  * @return EINA_TRUE if the structure was correctly set (The only
2553  *         reason that could make it fail is if you did give wrong
2554  *         parameter).
2555  *
2556  * @note Unless there's a very specific reason to use this function directly,
2557  * the EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET macro is recommended.
2558  *
2559  * @since 1.2.3
2560  * @ingroup Eet_Data_Group
2561  */
2562 EAPI Eina_Bool
2563 eet_eina_stream_data_descriptor_class_set(Eet_Data_Descriptor_Class *eddc,
2564                                           unsigned int               eddc_size,
2565                                           const char                *name,
2566                                           int                        size);
2567
2568 /**
2569  * This macro is an helper that set all the parameter of an
2570  * Eet_Data_Descriptor_Class correctly when you use Eina data type
2571  * with stream.
2572  * @param clas The Eet_Data_Descriptor_Class you want to set.
2573  * @param type The type of the structure described by this class.
2574  * @return EINA_TRUE if the structure was correctly set (The only
2575  *         reason that could make it fail is if you did give wrong
2576  *         parameter).
2577  *
2578  * @see eet_data_descriptor_stream_new
2579  * @since 1.2.3
2580  * @ingroup Eet_Data_Group
2581  */
2582 #define EET_EINA_STREAM_DATA_DESCRIPTOR_CLASS_SET(clas, type)\
2583    (eet_eina_stream_data_descriptor_class_set(clas, sizeof (*(clas)), # type, sizeof(type)))
2584
2585 /**
2586  * This function is an helper that set all the parameter of an
2587  * Eet_Data_Descriptor_Class correctly when you use Eina data type
2588  * with a file.
2589  * @param eddc The Eet_Data_Descriptor_Class you want to set.
2590  * @param eddc_size The size of the Eet_Data_Descriptor_Class at the compilation time.
2591  * @param name The name of the structure described by this class.
2592  * @param size The size of the structure described by this class.
2593  * @return EINA_TRUE if the structure was correctly set (The only
2594  *         reason that could make it fail is if you did give wrong
2595  *         parameter).
2596  *
2597  * @note Unless there's a very specific reason to use this function directly,
2598  * the EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET macro is recommended.
2599  *
2600  * @since 1.2.3
2601  * @ingroup Eet_Data_Group
2602  */
2603 EAPI Eina_Bool
2604 eet_eina_file_data_descriptor_class_set(Eet_Data_Descriptor_Class *eddc,
2605                                         unsigned int               eddc_size,
2606                                         const char                *name,
2607                                         int                        size);
2608
2609 /**
2610  * This macro is an helper that set all the parameter of an
2611  * Eet_Data_Descriptor_Class correctly when you use Eina data type
2612  * with file.
2613  * @param clas The Eet_Data_Descriptor_Class you want to set.
2614  * @param type The type of the structure described by this class.
2615  * @return EINA_TRUE if the structure was correctly set (The only
2616  *         reason that could make it fail is if you did give wrong
2617  *         parameter).
2618  *
2619  * @see eet_data_descriptor_file_new
2620  * @since 1.2.3
2621  * @ingroup Eet_Data_Group
2622  */
2623 #define EET_EINA_FILE_DATA_DESCRIPTOR_CLASS_SET(clas, type)\
2624   (eet_eina_file_data_descriptor_class_set(clas, sizeof (*(clas)), # type, sizeof(type)))
2625
2626 /**
2627  * This function frees a data descriptor when it is not needed anymore.
2628  * @param edd The data descriptor to free.
2629  *
2630  * This function takes a data descriptor handle as a parameter and frees all
2631  * data allocated for the data descriptor and the handle itself. After this
2632  * call the descriptor is no longer valid.
2633  *
2634  * @since 1.0.0
2635  * @ingroup Eet_Data_Group
2636  */
2637 EAPI void
2638 eet_data_descriptor_free(Eet_Data_Descriptor *edd);
2639
2640 /**
2641  * This function is an internal used by macros.
2642  *
2643  * This function is used by macros EET_DATA_DESCRIPTOR_ADD_BASIC(),
2644  * EET_DATA_DESCRIPTOR_ADD_SUB() and EET_DATA_DESCRIPTOR_ADD_LIST(). It is
2645  * complex to use by hand and should be left to be used by the macros, and
2646  * thus is not documented.
2647  *
2648  * @param edd The data descriptor handle to add element (member).
2649  * @param name The name of element to be serialized.
2650  * @param type The type of element to be serialized, like
2651  *        #EET_T_INT. If #EET_T_UNKNOW, then it is considered to be a
2652  *        group, list or hash.
2653  * @param group_type If element type is #EET_T_UNKNOW, then the @p
2654  *        group_type will speficy if it is a list (#EET_G_LIST),
2655  *        array (#EET_G_ARRAY) and so on. If #EET_G_UNKNOWN, then
2656  *        the member is a subtype (pointer to another type defined by
2657  *        another #Eet_Data_Descriptor).
2658  * @param offset byte offset inside the source memory to be serialized.
2659  * @param count number of elements (if #EET_G_ARRAY or #EET_G_VAR_ARRAY).
2660  * @param counter_name variable that defines the name of number of elements.
2661  * @param subtype If contains a subtype, then its data descriptor.
2662  *
2663  * @since 1.0.0
2664  * @ingroup Eet_Data_Group
2665  */
2666 EAPI void
2667 eet_data_descriptor_element_add(Eet_Data_Descriptor *edd,
2668                                 const char          *name,
2669                                 int                  type,
2670                                 int                  group_type,
2671                                 int                  offset,
2672                                 /* int                  count_offset, */
2673                                 int                  count,
2674                                 const char          *counter_name,
2675                                 Eet_Data_Descriptor *subtype);
2676
2677 /**
2678  * Read a data structure from an eet file and decodes it.
2679  * @param ef The eet file handle to read from.
2680  * @param edd The data descriptor handle to use when decoding.
2681  * @param name The key the data is stored under in the eet file.
2682  * @return A pointer to the decoded data structure.
2683  *
2684  * This function decodes a data structure stored in an eet file, returning
2685  * a pointer to it if it decoded successfully, or NULL on failure. This
2686  * can save a programmer dozens of hours of work in writing configuration
2687  * file parsing and writing code, as eet does all that work for the program
2688  * and presents a program-friendly data structure, just as the programmer
2689  * likes. Eet can handle members being added or deleted from the data in
2690  * storage and safely zero-fills unfilled members if they were not found
2691  * in the data. It checks sizes and headers whenever it reads data, allowing
2692  * the programmer to not worry about corrupt data.
2693  *
2694  * Once a data structure has been described by the programmer with the
2695  * fields they wish to save or load, storing or retrieving a data structure
2696  * from an eet file, or from a chunk of memory is as simple as a single
2697  * function call.
2698  *
2699  * @see eet_data_read_cipher()
2700  *
2701  * @since 1.0.0
2702  * @ingroup Eet_Data_Group
2703  */
2704 EAPI void *
2705 eet_data_read(Eet_File            *ef,
2706               Eet_Data_Descriptor *edd,
2707               const char          *name);
2708
2709 /**
2710  * Write a data structure from memory and store in an eet file.
2711  * @param ef The eet file handle to write to.
2712  * @param edd The data descriptor to use when encoding.
2713  * @param name The key to store the data under in the eet file.
2714  * @param data A pointer to the data structure to ssave and encode.
2715  * @param compress Compression flags for storage.
2716  * @return bytes written on successful write, 0 on failure.
2717  *
2718  * This function is the reverse of eet_data_read(), saving a data structure
2719  * to an eet file. The file must have been opening in write mode and the data
2720  * will be kept in memory until the file is either closed or eet_sync() is
2721  * called to flush any unwritten changes.
2722  *
2723  * @see eet_data_write_cipher()
2724  *
2725  * @since 1.0.0
2726  * @ingroup Eet_Data_Group
2727  */
2728 EAPI int
2729 eet_data_write(Eet_File            *ef,
2730                Eet_Data_Descriptor *edd,
2731                const char          *name,
2732                const void          *data,
2733                int                  compress);
2734
2735 typedef void (*Eet_Dump_Callback)(void *data, const char *str);
2736
2737 /**
2738  * Dump an eet encoded data structure into ascii text
2739  * @param data_in The pointer to the data to decode into a struct.
2740  * @param size_in The size of the data pointed to in bytes.
2741  * @param dumpfunc The function to call passed a string when new
2742  *        data is converted to text
2743  * @param dumpdata The data to pass to the @p dumpfunc callback.
2744  * @return 1 on success, 0 on failure
2745  *
2746  * This function will take a chunk of data encoded by
2747  * eet_data_descriptor_encode() and convert it into human readable
2748  * ascii text.  It does this by calling the @p dumpfunc callback
2749  * for all new text that is generated. This callback should append
2750  * to any existing text buffer and will be passed the pointer @p
2751  * dumpdata as a parameter as well as a string with new text to be
2752  * appended.
2753  *
2754  * Example:
2755  *
2756  * @code
2757  * void output(void *data, const char *string)
2758  * {
2759  *   printf("%s", string);
2760  * }
2761  *
2762  * void dump(const char *file)
2763  * {
2764  *   FILE *f;
2765  *   int len;
2766  *   void *data;
2767  *
2768  *   f = fopen(file, "r");
2769  *   fseek(f, 0, SEEK_END);
2770  *   len = ftell(f);
2771  *   rewind(f);
2772  *   data = malloc(len);
2773  *   fread(data, len, 1, f);
2774  *   fclose(f);
2775  *   eet_data_text_dump(data, len, output, NULL);
2776  * }
2777  * @endcode
2778  *
2779  * @see eet_data_text_dump_cipher()
2780  *
2781  * @since 1.0.0
2782  * @ingroup Eet_Data_Group
2783  */
2784 EAPI int
2785 eet_data_text_dump(const void  *data_in,
2786                    int          size_in,
2787                    Eet_Dump_Callback dumpfunc,
2788                    void        *dumpdata);
2789
2790 /**
2791  * Take an ascii encoding from eet_data_text_dump() and re-encode in binary.
2792  * @param text The pointer to the string data to parse and encode.
2793  * @param textlen The size of the string in bytes (not including 0
2794  *        byte terminator).
2795  * @param size_ret This gets filled in with the encoded data blob
2796  *        size in bytes.
2797  * @return The encoded data on success, NULL on failure.
2798  *
2799  * This function will parse the string pointed to by @p text and return
2800  * an encoded data lump the same way eet_data_descriptor_encode() takes an
2801  * in-memory data struct and encodes into a binary blob. @p text is a normal
2802  * C string.
2803  *
2804  * @see eet_data_text_undump_cipher()
2805  *
2806  * @since 1.0.0
2807  * @ingroup Eet_Data_Group
2808  */
2809 EAPI void *
2810 eet_data_text_undump(const char *text,
2811                      int         textlen,
2812                      int        *size_ret);
2813
2814 /**
2815  * Dump an eet encoded data structure from an eet file into ascii text
2816  * @param ef A valid eet file handle.
2817  * @param name Name of the entry. eg: "/base/file_i_want".
2818  * @param dumpfunc The function to call passed a string when new
2819  *        data is converted to text
2820  * @param dumpdata The data to pass to the @p dumpfunc callback.
2821  * @return 1 on success, 0 on failure
2822  *
2823  * This function will take an open and valid eet file from
2824  * eet_open() request the data encoded by
2825  * eet_data_descriptor_encode() corresponding to the key @p name
2826  * and convert it into human readable ascii text. It does this by
2827  * calling the @p dumpfunc callback for all new text that is
2828  * generated. This callback should append to any existing text
2829  * buffer and will be passed the pointer @p dumpdata as a parameter
2830  * as well as a string with new text to be appended.
2831  *
2832  * @see eet_data_dump_cipher()
2833  *
2834  * @since 1.0.0
2835  * @ingroup Eet_Data_Group
2836  */
2837 EAPI int
2838 eet_data_dump(Eet_File    *ef,
2839               const char  *name,
2840               Eet_Dump_Callback dumpfunc,
2841               void        *dumpdata);
2842
2843 /**
2844  * Take an ascii encoding from eet_data_dump() and re-encode in binary.
2845  * @param ef A valid eet file handle.
2846  * @param name Name of the entry. eg: "/base/file_i_want".
2847  * @param text The pointer to the string data to parse and encode.
2848  * @param textlen The size of the string in bytes (not including 0
2849  *        byte terminator).
2850  * @param compress Compression flags (1 == compress, 0 = don't compress).
2851  * @return 1 on success, 0 on failure
2852  *
2853  * This function will parse the string pointed to by @p text,
2854  * encode it the same way eet_data_descriptor_encode() takes an
2855  * in-memory data struct and encodes into a binary blob.
2856  *
2857  * The data (optionally compressed) will be in ram, pending a flush to
2858  * disk (it will stay in ram till the eet file handle is closed though).
2859  *
2860  * @see eet_data_undump_cipher()
2861  *
2862  * @since 1.0.0
2863  * @ingroup Eet_Data_Group
2864  */
2865 EAPI int
2866 eet_data_undump(Eet_File   *ef,
2867                 const char *name,
2868                 const char *text,
2869                 int         textlen,
2870                 int         compress);
2871
2872 /**
2873  * Decode a data structure from an arbitrary location in memory.
2874  * @param edd The data  descriptor to use when decoding.
2875  * @param data_in The pointer to the data to decode into a struct.
2876  * @param size_in The size of the data pointed to in bytes.
2877  * @return NULL on failure, or a valid decoded struct pointer on success.
2878  *
2879  * This function will decode a data structure that has been encoded using
2880  * eet_data_descriptor_encode(), and return a data structure with all its
2881  * elements filled out, if successful, or NULL on failure.
2882  *
2883  * The data to be decoded is stored at the memory pointed to by @p data_in,
2884  * and is described by the descriptor pointed to by @p edd. The data size is
2885  * passed in as the value to @p size_in, ande must be greater than 0 to
2886  * succeed.
2887  *
2888  * This function is useful for decoding data structures delivered to the
2889  * application by means other than an eet file, such as an IPC or socket
2890  * connection, raw files, shared memory etc.
2891  *
2892  * Please see eet_data_read() for more information.
2893  *
2894  * @see eet_data_descriptor_decode_cipher()
2895  *
2896  * @since 1.0.0
2897  * @ingroup Eet_Data_Group
2898  */
2899 EAPI void *
2900 eet_data_descriptor_decode(Eet_Data_Descriptor *edd,
2901                            const void          *data_in,
2902                            int                  size_in);
2903
2904 /**
2905  * Encode a dsata struct to memory and return that encoded data.
2906  * @param edd The data  descriptor to use when encoding.
2907  * @param data_in The pointer to the struct to encode into data.
2908  * @param size_ret pointer to the an int to be filled with the decoded size.
2909  * @return NULL on failure, or a valid encoded data chunk on success.
2910  *
2911  * This function takes a data structutre in memory and encodes it into a
2912  * serialised chunk of data that can be decoded again by
2913  * eet_data_descriptor_decode(). This is useful for being able to transmit
2914  * data structures across sockets, pipes, IPC or shared file mechanisms,
2915  * without having to worry about memory space, machine type, endianess etc.
2916  *
2917  * The parameter @p edd must point to a valid data descriptor, and
2918  * @p data_in must point to the right data structure to encode. If not, the
2919  * encoding may fail.
2920  *
2921  * On success a non NULL valid pointer is returned and what @p size_ret
2922  * points to is set to the size of this decoded data, in bytes. When the
2923  * encoded data is no longer needed, call free() on it. On failure NULL is
2924  * returned and what @p size_ret points to is set to 0.
2925  *
2926  * Please see eet_data_write() for more information.
2927  *
2928  * @see eet_data_descriptor_encode_cipher()
2929  *
2930  * @since 1.0.0
2931  * @ingroup Eet_Data_Group
2932  */
2933 EAPI void *
2934 eet_data_descriptor_encode(Eet_Data_Descriptor *edd,
2935                            const void          *data_in,
2936                            int                 *size_ret);
2937
2938 /**
2939  * Add a basic data element to a data descriptor.
2940  * @param edd The data descriptor to add the type to.
2941  * @param struct_type The type of the struct.
2942  * @param name The string name to use to encode/decode this member
2943  *        (must be a constant global and never change).
2944  * @param member The struct member itself to be encoded.
2945  * @param type The type of the member to encode.
2946  *
2947  * This macro is a convenience macro provided to add a member to
2948  * the data descriptor @p edd. The type of the structure is
2949  * provided as the @p struct_type parameter (for example: struct
2950  * my_struct). The @p name parameter defines a string that will be
2951  * used to uniquely name that member of the struct (it is suggested
2952  * to use the struct member itself).  The @p member parameter is
2953  * the actual struct member itself (for example: values), and @p type is the
2954  * basic data type of the member which must be one of: EET_T_CHAR, EET_T_SHORT,
2955  * EET_T_INT, EET_T_LONG_LONG, EET_T_FLOAT, EET_T_DOUBLE, EET_T_UCHAR,
2956  * EET_T_USHORT, EET_T_UINT, EET_T_ULONG_LONG or EET_T_STRING.
2957  *
2958  * @since 1.0.0
2959  * @ingroup Eet_Data_Group
2960  */
2961 #define EET_DATA_DESCRIPTOR_ADD_BASIC(edd, struct_type, name, member, type) \
2962    do { \
2963       struct_type ___ett; \
2964       eet_data_descriptor_element_add(edd, name, type, EET_G_UNKNOWN, \
2965                                       (char *)(& (___ett.member)) - \
2966                                       (char *)(& (___ett)), \
2967                                       0, /* 0,  */ NULL, NULL); \
2968    } while(0)
2969
2970 /**
2971  * Add a sub-element type to a data descriptor
2972  * @param edd The data descriptor to add the type to.
2973  * @param struct_type The type of the struct.
2974  * @param name The string name to use to encode/decode this member
2975  *        (must be a constant global and never change).
2976  * @param member The struct member itself to be encoded.
2977  * @param subtype The type of sub-type struct to add.
2978  *
2979  * This macro lets you easily add a sub-type (a struct that's pointed to
2980  * by this one). All the parameters are the same as for
2981  * EET_DATA_DESCRIPTOR_ADD_BASIC(), with the @p subtype being the exception.
2982  * This must be the data descriptor of the struct that is pointed to by
2983  * this element.
2984  *
2985  * @since 1.0.0
2986  * @ingroup Eet_Data_Group
2987  */
2988 #define EET_DATA_DESCRIPTOR_ADD_SUB(edd, struct_type, name, member, subtype) \
2989    do { \
2990       struct_type ___ett; \
2991       eet_data_descriptor_element_add(edd, name, EET_T_UNKNOW, EET_G_UNKNOWN, \
2992                                       (char *)(& (___ett.member)) - \
2993                                       (char *)(& (___ett)), \
2994                                       0, /* 0,  */ NULL, subtype); \
2995    } while (0)
2996
2997 /**
2998  * Add a linked list type to a data descriptor
2999  * @param edd The data descriptor to add the type to.
3000  * @param struct_type The type of the struct.
3001  * @param name The string name to use to encode/decode this member
3002  *        (must be a constant global and never change).
3003  * @param member The struct member itself to be encoded.
3004  * @param subtype The type of linked list member to add.
3005  *
3006  * This macro lets you easily add a linked list of other data types. All the
3007  * parameters are the same as for EET_DATA_DESCRIPTOR_ADD_BASIC(), with the
3008  * @p subtype being the exception. This must be the data descriptor of the
3009  * element that is in each member of the linked list to be stored.
3010  *
3011  * @since 1.0.0
3012  * @ingroup Eet_Data_Group
3013  */
3014 #define EET_DATA_DESCRIPTOR_ADD_LIST(edd, struct_type, name, member, subtype) \
3015    do { \
3016       struct_type ___ett; \
3017       eet_data_descriptor_element_add(edd, name, EET_T_UNKNOW, EET_G_LIST, \
3018                                       (char *)(& (___ett.member)) - \
3019                                       (char *)(& (___ett)), \
3020                                       0, /* 0,  */ NULL, subtype); \
3021    } while (0)
3022
3023 /**
3024  * Add a linked list of string to a data descriptor
3025  * @param edd The data descriptor to add the type to.
3026  * @param struct_type The type of the struct.
3027  * @param name The string name to use to encode/decode this member
3028  *        (must be a constant global and never change).
3029  * @param member The struct member itself to be encoded.
3030  *
3031  * This macro lets you easily add a linked list of char *. All the
3032  * parameters are the same as for EET_DATA_DESCRIPTOR_ADD_BASIC().
3033  *
3034  * @since 1.5.0
3035  * @ingroup Eet_Data_Group
3036  */
3037 #define EET_DATA_DESCRIPTOR_ADD_LIST_STRING(edd, struct_type, name, member) \
3038    do { \
3039       struct_type ___ett; \
3040       eet_data_descriptor_element_add(edd, name, EET_T_STRING, EET_G_LIST, \
3041                                       (char *)(& (___ett.member)) - \
3042                                       (char *)(& (___ett)), \
3043                                       0, /* 0,  */ NULL, NULL); \
3044    } while (0)
3045
3046 /**
3047  * Add a hash type to a data descriptor
3048  * @param edd The data descriptor to add the type to.
3049  * @param struct_type The type of the struct.
3050  * @param name The string name to use to encode/decode this member
3051  *        (must be a constant global and never change).
3052  * @param member The struct member itself to be encoded.
3053  * @param subtype The type of hash member to add.
3054  *
3055  * This macro lets you easily add a hash of other data types. All the
3056  * parameters are the same as for EET_DATA_DESCRIPTOR_ADD_BASIC(), with the
3057  * @p subtype being the exception. This must be the data descriptor of the
3058  * element that is in each member of the hash to be stored.
3059  * The hash keys must be strings.
3060  *
3061  * @since 1.0.0
3062  * @ingroup Eet_Data_Group
3063  */
3064 #define EET_DATA_DESCRIPTOR_ADD_HASH(edd, struct_type, name, member, subtype) \
3065    do { \
3066       struct_type ___ett; \
3067       eet_data_descriptor_element_add(edd, name, EET_T_UNKNOW, EET_G_HASH, \
3068                                       (char *)(& (___ett.member)) - \
3069                                       (char *)(& (___ett)), \
3070                                       0, /* 0,  */ NULL, subtype); \
3071    } while (0)
3072
3073 /**
3074  * Add a hash of string to a data descriptor
3075  * @param edd The data descriptor to add the type to.
3076  * @param struct_type The type of the struct.
3077  * @param name The string name to use to encode/decode this member
3078  *        (must be a constant global and never change).
3079  * @param member The struct member itself to be encoded.
3080  *
3081  * This macro lets you easily add a hash of string elements. All the
3082  * parameters are the same as for EET_DATA_DESCRIPTOR_ADD_HASH().
3083  *
3084  * @since 1.3.4
3085  * @ingroup Eet_Data_Group
3086  */
3087 #define EET_DATA_DESCRIPTOR_ADD_HASH_STRING(edd, struct_type, name, member) \
3088    do { \
3089       struct_type ___ett; \
3090       eet_data_descriptor_element_add(edd, name, EET_T_STRING, EET_G_HASH, \
3091                                       (char *)(& (___ett.member)) - \
3092                                       (char *)(& (___ett)), \
3093                                       0, /* 0,  */ NULL, NULL); \
3094    } while (0)
3095
3096 /**
3097  * Add a fixed size array type to a data descriptor
3098  * @param edd The data descriptor to add the type to.
3099  * @param struct_type The type of the struct.
3100  * @param name The string name to use to encode/decode this member
3101  *        (must be a constant global and never change).
3102  * @param member The struct member itself to be encoded.
3103  * @param subtype The type of hash member to add.
3104  *
3105  * This macro lets you easily add a fixed size array of other data
3106  * types. All the parameters are the same as for
3107  * EET_DATA_DESCRIPTOR_ADD_BASIC(), with the @p subtype being the
3108  * exception. This must be the data descriptor of the element that
3109  * is in each member of the array to be stored.
3110  * The array must be defined with a fixed size in the declaration of the
3111  * struct containing it.
3112  *
3113  * @since 1.0.2
3114  * @ingroup Eet_Data_Group
3115  */
3116 #define EET_DATA_DESCRIPTOR_ADD_ARRAY(edd, struct_type, name, member, subtype) \
3117    do { \
3118       struct_type ___ett; \
3119       eet_data_descriptor_element_add(edd, name, EET_T_UNKNOW, EET_G_ARRAY, \
3120                                       (char *)(& (___ett.member)) - \
3121                                       (char *)(& (___ett)), \
3122                                       /* 0,  */ sizeof(___ett.member) / \
3123                                       sizeof(___ett.member[0]), NULL, subtype); \
3124    } while (0)
3125
3126 /**
3127  * Add a variable size array type to a data descriptor
3128  * @param edd The data descriptor to add the type to.
3129  * @param struct_type The type of the struct.
3130  * @param name The string name to use to encode/decode this member
3131  *        (must be a constant global and never change).
3132  * @param member The struct member itself to be encoded.
3133  * @param subtype The type of hash member to add.
3134  *
3135  * This macro lets you easily add a variable size array of other data
3136  * types. All the parameters are the same as for
3137  * EET_DATA_DESCRIPTOR_ADD_BASIC(), with the @p subtype being the
3138  * exception. This must be the data descriptor of the element that
3139  * is in each member of the array to be stored. This assumes you have
3140  * a struct member (of type EET_T_INT) called member_count (note the
3141  * _count appended to the member) that holds the number of items in
3142  * the array. This array will be allocated separately to the struct it
3143  * is in.
3144  *
3145  * @since 1.0.2
3146  * @ingroup Eet_Data_Group
3147  */
3148 #define EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY(edd, struct_type, name, member, subtype) \
3149    do { \
3150       struct_type ___ett; \
3151       eet_data_descriptor_element_add(edd, \
3152                                       name, \
3153                                       EET_T_UNKNOW, \
3154                                       EET_G_VAR_ARRAY, \
3155                                       (char *)(& (___ett.member)) - \
3156                                       (char *)(& (___ett)), \
3157                                       (char *)(& (___ett.member ## _count)) - \
3158                                       (char *)(& (___ett)), \
3159                                       /* 0,  */ NULL, \
3160                                       subtype); \
3161    } while (0)
3162
3163 /**
3164  * Add a variable size array type to a data descriptor
3165  * @param edd The data descriptor to add the type to.
3166  * @param struct_type The type of the struct.
3167  * @param name The string name to use to encode/decode this member
3168  *        (must be a constant global and never change).
3169  * @param member The struct member itself to be encoded.
3170  *
3171  * This macro lets you easily add a variable size array of strings. All
3172  * the parameters are the same as for EET_DATA_DESCRIPTOR_ADD_BASIC().
3173  *
3174  * @since 1.4.0
3175  * @ingroup Eet_Data_Group
3176  */
3177 #define EET_DATA_DESCRIPTOR_ADD_VAR_ARRAY_STRING(edd, struct_type, name, member) \
3178    do { \
3179       struct_type ___ett; \
3180       eet_data_descriptor_element_add(edd, \
3181                                       name, \
3182                                       EET_T_STRING, \
3183                                       EET_G_VAR_ARRAY, \
3184                                       (char *)(& (___ett.member)) - \
3185                                       (char *)(& (___ett)), \
3186                                       (char *)(& (___ett.member ## _count)) - \
3187                                       (char *)(& (___ett)), \
3188                                       /* 0,  */ NULL, \
3189                                       NULL); \
3190    } while (0)
3191
3192 /**
3193  * Add an union type to a data descriptor
3194  * @param edd The data descriptor to add the type to.
3195  * @param struct_type The type of the struct.
3196  * @param name The string name to use to encode/decode this member
3197  *        (must be a constant global and never change).
3198  * @param member The struct member itself to be encoded.
3199  * @param type_member The member that give hints on what is in the union.
3200  * @param unified_type Describe all possible type the union could handle.
3201  *
3202  * This macro lets you easily add an union with a member that specify what is inside.
3203  * The @p unified_type is an Eet_Data_Descriptor, but only the entry that match the name
3204  * returned by type_get will be used for each serialized data. The type_get and type_set
3205  * callback of unified_type should be defined.
3206  *
3207  * @since 1.2.4
3208  * @ingroup Eet_Data_Group
3209  * @see Eet_Data_Descriptor_Class
3210  */
3211 #define EET_DATA_DESCRIPTOR_ADD_UNION(edd, struct_type, name, member, type_member, unified_type) \
3212    do { \
3213       struct_type ___ett; \
3214       eet_data_descriptor_element_add(edd, name, EET_T_UNKNOW, EET_G_UNION, \
3215                                       (char *)(& (___ett.member)) - \
3216                                       (char *)(& (___ett)), \
3217                                       (char *)(& (___ett.type_member)) - \
3218                                       (char *)(& (___ett)), \
3219                                       NULL, unified_type); \
3220    } while (0)
3221    
3222 /**
3223  * Add a automatically selectable type to a data descriptor
3224  * @param edd The data descriptor to add the type to.
3225  * @param struct_type The type of the struct.
3226  * @param name The string name to use to encode/decode this member
3227  *        (must be a constant global and never change).
3228  * @param member The struct member itself to be encoded.
3229  * @param type_member The member that give hints on what is in the union.
3230  * @param unified_type Describe all possible type the union could handle.
3231  *
3232  * This macro lets you easily define what the content of @p member points to depending of
3233  * the content of @p type_member. The type_get and type_set callback of unified_type should
3234  * be defined. If the the type is not know at the time of restoring it, eet will still call
3235  * type_set of @p unified_type but the pointer will be set to a serialized binary representation
3236  * of what eet know. This make it possible, to save this pointer again by just returning the string
3237  * given previously and telling it by setting unknow to EINA_TRUE.
3238  *
3239  * @since 1.2.4
3240  * @ingroup Eet_Data_Group
3241  * @see Eet_Data_Descriptor_Class
3242  */
3243 #define EET_DATA_DESCRIPTOR_ADD_VARIANT(edd, struct_type, name, member, type_member, unified_type) \
3244    do { \
3245       struct_type ___ett; \
3246       eet_data_descriptor_element_add(edd, name, EET_T_UNKNOW, EET_G_VARIANT, \
3247                                       (char *)(& (___ett.member)) - \
3248                                       (char *)(& (___ett)), \
3249                                       (char *)(& (___ett.type_member)) - \
3250                                       (char *)(& (___ett)), \
3251                                       NULL, unified_type); \
3252    } while (0)
3253    
3254 /**
3255  * Add a mapping to a data descriptor that will be used by union, variant or inherited type
3256  * @param unified_type The data descriptor to add the mapping to.
3257  * @param name The string name to get/set type.
3258  * @param subtype The matching data descriptor.
3259  *
3260  * @since 1.2.4
3261  * @ingroup Eet_Data_Group
3262  * @see Eet_Data_Descriptor_Class
3263  */
3264 #define EET_DATA_DESCRIPTOR_ADD_MAPPING(unified_type, name, subtype) \
3265    eet_data_descriptor_element_add(unified_type, \
3266                                    name, \
3267                                    EET_T_UNKNOW, \
3268                                    EET_G_UNKNOWN, \
3269                                    0, \
3270                                    0, \
3271                                    NULL, \
3272                                    subtype)
3273
3274 /**
3275  * @defgroup Eet_Data_Cipher_Group Eet Data Serialization using A Ciphers
3276  *
3277  * Most of the @ref Eet_Data_Group have alternative versions that
3278  * accounts for ciphers to protect their content.
3279  *
3280  * @see @ref Eet_Cipher_Group
3281  *
3282  * @ingroup Eet_Data_Group
3283  */
3284
3285 /**
3286  * Read a data structure from an eet file and decodes it using a cipher.
3287  * @param ef The eet file handle to read from.
3288  * @param edd The data descriptor handle to use when decoding.
3289  * @param name The key the data is stored under in the eet file.
3290  * @param cipher_key The key to use as cipher.
3291  * @return A pointer to the decoded data structure.
3292  *
3293  * This function decodes a data structure stored in an eet file, returning
3294  * a pointer to it if it decoded successfully, or NULL on failure. This
3295  * can save a programmer dozens of hours of work in writing configuration
3296  * file parsing and writing code, as eet does all that work for the program
3297  * and presents a program-friendly data structure, just as the programmer
3298  * likes. Eet can handle members being added or deleted from the data in
3299  * storage and safely zero-fills unfilled members if they were not found
3300  * in the data. It checks sizes and headers whenever it reads data, allowing
3301  * the programmer to not worry about corrupt data.
3302  *
3303  * Once a data structure has been described by the programmer with the
3304  * fields they wish to save or load, storing or retrieving a data structure
3305  * from an eet file, or from a chunk of memory is as simple as a single
3306  * function call.
3307  *
3308  * @see eet_data_read()
3309  *
3310  * @since 1.0.0
3311  * @ingroup Eet_Data_Cipher_Group
3312  */
3313 EAPI void *
3314 eet_data_read_cipher(Eet_File            *ef,
3315                      Eet_Data_Descriptor *edd,
3316                      const char          *name,
3317                      const char          *cipher_key);
3318
3319 /**
3320  * Write a data structure from memory and store in an eet file
3321  * using a cipher.
3322  * @param ef The eet file handle to write to.
3323  * @param edd The data descriptor to use when encoding.
3324  * @param name The key to store the data under in the eet file.
3325  * @param cipher_key The key to use as cipher.
3326  * @param data A pointer to the data structure to ssave and encode.
3327  * @param compress Compression flags for storage.
3328  * @return bytes written on successful write, 0 on failure.
3329  *
3330  * This function is the reverse of eet_data_read(), saving a data structure
3331  * to an eet file.
3332  *
3333  * @see eet_data_write_cipher()
3334  *
3335  * @since 1.0.0
3336  * @ingroup Eet_Data_Cipher_Group
3337  */
3338 EAPI int
3339 eet_data_write_cipher(Eet_File            *ef,
3340                       Eet_Data_Descriptor *edd,
3341                       const char          *name,
3342                       const char          *cipher_key,
3343                       const void          *data,
3344                       int                  compress);
3345
3346 /**
3347  * Dump an eet encoded data structure into ascii text using a cipher.
3348  * @param data_in The pointer to the data to decode into a struct.
3349  * @param cipher_key The key to use as cipher.
3350  * @param size_in The size of the data pointed to in bytes.
3351  * @param dumpfunc The function to call passed a string when new
3352  *        data is converted to text
3353  * @param dumpdata The data to pass to the @p dumpfunc callback.
3354  * @return 1 on success, 0 on failure
3355  *
3356  * This function will take a chunk of data encoded by
3357  * eet_data_descriptor_encode() and convert it into human readable
3358  * ascii text.  It does this by calling the @p dumpfunc callback
3359  * for all new text that is generated. This callback should append
3360  * to any existing text buffer and will be passed the pointer @p
3361  * dumpdata as a parameter as well as a string with new text to be
3362  * appended.
3363  *
3364  * Example:
3365  *
3366  * @code
3367  * void output(void *data, const char *string)
3368  * {
3369  *   printf("%s", string);
3370  * }
3371  *
3372  * void dump(const char *file)
3373  * {
3374  *   FILE *f;
3375  *   int len;
3376  *   void *data;
3377  *
3378  *   f = fopen(file, "r");
3379  *   fseek(f, 0, SEEK_END);
3380  *   len = ftell(f);
3381  *   rewind(f);
3382  *   data = malloc(len);
3383  *   fread(data, len, 1, f);
3384  *   fclose(f);
3385  *   eet_data_text_dump_cipher(data, cipher_key, len, output, NULL);
3386  * }
3387  * @endcode
3388  *
3389  * @see eet_data_text_dump()
3390  *
3391  * @since 1.0.0
3392  * @ingroup Eet_Data_Cipher_Group
3393  */
3394 EAPI int
3395 eet_data_text_dump_cipher(const void *data_in,
3396                           const char *cipher_key,
3397                           int size_in,
3398                           Eet_Dump_Callback dumpfunc,
3399                           void *dumpdata);
3400
3401 /**
3402  * Take an ascii encoding from eet_data_text_dump() and re-encode
3403  * in binary using a cipher.
3404  * @param text The pointer to the string data to parse and encode.
3405  * @param cipher_key The key to use as cipher.
3406  * @param textlen The size of the string in bytes (not including 0
3407  *        byte terminator).
3408  * @param size_ret This gets filled in with the encoded data blob
3409  *        size in bytes.
3410  * @return The encoded data on success, NULL on failure.
3411  *
3412  * This function will parse the string pointed to by @p text and return
3413  * an encoded data lump the same way eet_data_descriptor_encode() takes an
3414  * in-memory data struct and encodes into a binary blob. @p text is a normal
3415  * C string.
3416  *
3417  * @see eet_data_text_undump()
3418  *
3419  * @since 1.0.0
3420  * @ingroup Eet_Data_Cipher_Group
3421  */
3422 EAPI void *
3423 eet_data_text_undump_cipher(const char *text,
3424                             const char *cipher_key,
3425                             int         textlen,
3426                             int        *size_ret);
3427
3428 /**
3429  * Dump an eet encoded data structure from an eet file into ascii
3430  * text using a cipher.
3431  * @param ef A valid eet file handle.
3432  * @param name Name of the entry. eg: "/base/file_i_want".
3433  * @param cipher_key The key to use as cipher.
3434  * @param dumpfunc The function to call passed a string when new
3435  *        data is converted to text
3436  * @param dumpdata The data to pass to the @p dumpfunc callback.
3437  * @return 1 on success, 0 on failure
3438  *
3439  * This function will take an open and valid eet file from
3440  * eet_open() request the data encoded by
3441  * eet_data_descriptor_encode() corresponding to the key @p name
3442  * and convert it into human readable ascii text. It does this by
3443  * calling the @p dumpfunc callback for all new text that is
3444  * generated. This callback should append to any existing text
3445  * buffer and will be passed the pointer @p dumpdata as a parameter
3446  * as well as a string with new text to be appended.
3447  *
3448  * @see eet_data_dump()
3449  *
3450  * @since 1.0.0
3451  * @ingroup Eet_Data_Cipher_Group
3452  */
3453 EAPI int
3454 eet_data_dump_cipher(Eet_File    *ef,
3455                      const char  *name,
3456                      const char  *cipher_key,
3457                      Eet_Dump_Callback dumpfunc,
3458                      void        *dumpdata);
3459
3460 /**
3461  * Take an ascii encoding from eet_data_dump() and re-encode in
3462  * binary using a cipher.
3463  * @param ef A valid eet file handle.
3464  * @param name Name of the entry. eg: "/base/file_i_want".
3465  * @param cipher_key The key to use as cipher.
3466  * @param text The pointer to the string data to parse and encode.
3467  * @param textlen The size of the string in bytes (not including 0
3468  *        byte terminator).
3469  * @param compress Compression flags (1 == compress, 0 = don't compress).
3470  * @return 1 on success, 0 on failure
3471  *
3472  * This function will parse the string pointed to by @p text,
3473  * encode it the same way eet_data_descriptor_encode() takes an
3474  * in-memory data struct and encodes into a binary blob.
3475  *
3476  * The data (optionally compressed) will be in ram, pending a flush to
3477  * disk (it will stay in ram till the eet file handle is closed though).
3478  *
3479  * @see eet_data_undump()
3480  *
3481  * @since 1.0.0
3482  * @ingroup Eet_Data_Cipher_Group
3483  */
3484 EAPI int
3485 eet_data_undump_cipher(Eet_File   *ef,
3486                        const char *name,
3487                        const char *cipher_key,
3488                        const char *text,
3489                        int         textlen,
3490                        int         compress);
3491
3492 /**
3493  * Decode a data structure from an arbitrary location in memory
3494  * using a cipher.
3495  * @param edd The data  descriptor to use when decoding.
3496  * @param data_in The pointer to the data to decode into a struct.
3497  * @param cipher_key The key to use as cipher.
3498  * @param size_in The size of the data pointed to in bytes.
3499  * @return NULL on failure, or a valid decoded struct pointer on success.
3500  *
3501  * This function will decode a data structure that has been encoded using
3502  * eet_data_descriptor_encode(), and return a data structure with all its
3503  * elements filled out, if successful, or NULL on failure.
3504  *
3505  * The data to be decoded is stored at the memory pointed to by @p data_in,
3506  * and is described by the descriptor pointed to by @p edd. The data size is
3507  * passed in as the value to @p size_in, ande must be greater than 0 to
3508  * succeed.
3509  *
3510  * This function is useful for decoding data structures delivered to the
3511  * application by means other than an eet file, such as an IPC or socket
3512  * connection, raw files, shared memory etc.
3513  *
3514  * Please see eet_data_read() for more information.
3515  *
3516  * @see eet_data_descriptor_decode()
3517  *
3518  * @since 1.0.0
3519  * @ingroup Eet_Data_Cipher_Group
3520  */
3521 EAPI void *
3522 eet_data_descriptor_decode_cipher(Eet_Data_Descriptor *edd,
3523                                   const void          *data_in,
3524                                   const char          *cipher_key,
3525                                   int                  size_in);
3526
3527 /**
3528  * Encode a data struct to memory and return that encoded data
3529  * using a cipher.
3530  * @param edd The data  descriptor to use when encoding.
3531  * @param data_in The pointer to the struct to encode into data.
3532  * @param cipher_key The key to use as cipher.
3533  * @param size_ret pointer to the an int to be filled with the decoded size.
3534  * @return NULL on failure, or a valid encoded data chunk on success.
3535  *
3536  * This function takes a data structutre in memory and encodes it into a
3537  * serialised chunk of data that can be decoded again by
3538  * eet_data_descriptor_decode(). This is useful for being able to transmit
3539  * data structures across sockets, pipes, IPC or shared file mechanisms,
3540  * without having to worry about memory space, machine type, endianess etc.
3541  *
3542  * The parameter @p edd must point to a valid data descriptor, and
3543  * @p data_in must point to the right data structure to encode. If not, the
3544  * encoding may fail.
3545  *
3546  * On success a non NULL valid pointer is returned and what @p size_ret
3547  * points to is set to the size of this decoded data, in bytes. When the
3548  * encoded data is no longer needed, call free() on it. On failure NULL is
3549  * returned and what @p size_ret points to is set to 0.
3550  *
3551  * Please see eet_data_write() for more information.
3552  *
3553  * @see eet_data_descriptor_encode()
3554  *
3555  * @since 1.0.0
3556  * @ingroup Eet_Data_Cipher_Group
3557  */
3558 EAPI void *
3559 eet_data_descriptor_encode_cipher(Eet_Data_Descriptor *edd,
3560                                   const void          *data_in,
3561                                   const char          *cipher_key,
3562                                   int                 *size_ret);
3563
3564 /**
3565  * @defgroup Eet_Node_Group Low-level Serialization Structures.
3566  *
3567  * Functions that create, destroy and manipulate serialization nodes
3568  * used by @ref Eet_Data_Group.
3569  *
3570  * @{
3571  */
3572
3573 /**
3574  * @typedef Eet_Node
3575  * Opaque handle to manage serialization node.
3576  */
3577 typedef struct _Eet_Node        Eet_Node;
3578
3579 /**
3580  * @typedef Eet_Node_Data
3581  * Contains an union that can fit any kind of node.
3582  */
3583 typedef struct _Eet_Node_Data   Eet_Node_Data;
3584
3585 /**
3586  * @struct _Eet_Node_Data
3587  * Contains an union that can fit any kind of node.
3588  */
3589 struct _Eet_Node_Data
3590 {
3591    union {
3592       char                c;
3593       short               s;
3594       int                 i;
3595       long long           l;
3596       float               f;
3597       double              d;
3598       unsigned char       uc;
3599       unsigned short      us;
3600       unsigned int        ui;
3601       unsigned long long  ul;
3602       const char         *str;
3603    } value;
3604 };
3605
3606 /**
3607  * @}
3608  */
3609
3610 /**
3611  * TODO FIX ME
3612  * @ingroup Eet_Node_Group
3613  */
3614 EAPI Eet_Node *
3615  eet_node_char_new(const char *name,
3616                    char        c);
3617
3618 /**
3619  * TODO FIX ME
3620  * @ingroup Eet_Node_Group
3621  */
3622 EAPI Eet_Node *
3623 eet_node_short_new(const char *name,
3624                    short       s);
3625
3626 /**
3627  * TODO FIX ME
3628  * @ingroup Eet_Node_Group
3629  */
3630 EAPI Eet_Node *
3631 eet_node_int_new(const char *name,
3632                  int         i);
3633
3634 /**
3635  * TODO FIX ME
3636  * @ingroup Eet_Node_Group
3637  */
3638 EAPI Eet_Node *
3639 eet_node_long_long_new(const char *name,
3640                        long long   l);
3641
3642 /**
3643  * TODO FIX ME
3644  * @ingroup Eet_Node_Group
3645  */
3646 EAPI Eet_Node *
3647 eet_node_float_new(const char *name,
3648                    float       f);
3649
3650 /**
3651  * TODO FIX ME
3652  * @ingroup Eet_Node_Group
3653  */
3654 EAPI Eet_Node *
3655 eet_node_double_new(const char *name,
3656                     double      d);
3657
3658 /**
3659  * TODO FIX ME
3660  * @ingroup Eet_Node_Group
3661  */
3662 EAPI Eet_Node *
3663 eet_node_unsigned_char_new(const char    *name,
3664                            unsigned char  uc);
3665
3666 /**
3667  * TODO FIX ME
3668  * @ingroup Eet_Node_Group
3669  */
3670 EAPI Eet_Node *
3671 eet_node_unsigned_short_new(const char     *name,
3672                             unsigned short  us);
3673
3674 /**
3675  * TODO FIX ME
3676  * @ingroup Eet_Node_Group
3677  */
3678 EAPI Eet_Node *
3679 eet_node_unsigned_int_new(const char   *name,
3680                           unsigned int  ui);
3681
3682 /**
3683  * TODO FIX ME
3684  * @ingroup Eet_Node_Group
3685  */
3686 EAPI Eet_Node *
3687 eet_node_unsigned_long_long_new(const char         *name,
3688                                 unsigned long long  l);
3689
3690 /**
3691  * TODO FIX ME
3692  * @ingroup Eet_Node_Group
3693  */
3694 EAPI Eet_Node *
3695 eet_node_string_new(const char *name,
3696                     const char *str);
3697
3698 /**
3699  * TODO FIX ME
3700  * @ingroup Eet_Node_Group
3701  */
3702 EAPI Eet_Node *
3703 eet_node_inlined_string_new(const char *name,
3704                             const char *str);
3705
3706 /**
3707  * TODO FIX ME
3708  * @ingroup Eet_Node_Group
3709  */
3710 EAPI Eet_Node *
3711 eet_node_null_new(const char *name);
3712
3713 /**
3714  * TODO FIX ME
3715  * @ingroup Eet_Node_Group
3716  */
3717 EAPI Eet_Node *
3718 eet_node_list_new(const char *name,
3719                   Eina_List  *nodes);
3720
3721 /**
3722  * TODO FIX ME
3723  * @ingroup Eet_Node_Group
3724  */
3725 EAPI Eet_Node *
3726 eet_node_array_new(const char *name,
3727                    int         count,
3728                    Eina_List  *nodes);
3729
3730 /**
3731  * TODO FIX ME
3732  * @ingroup Eet_Node_Group
3733  */
3734 EAPI Eet_Node *
3735 eet_node_var_array_new(const char *name,
3736                        Eina_List  *nodes);
3737
3738 /**
3739  * TODO FIX ME
3740  * @ingroup Eet_Node_Group
3741  */
3742 EAPI Eet_Node *
3743 eet_node_hash_new(const char *name,
3744                   const char *key,
3745                   Eet_Node   *node);
3746
3747 /**
3748  * TODO FIX ME
3749  * @ingroup Eet_Node_Group
3750  */
3751 EAPI Eet_Node *
3752 eet_node_struct_new(const char *name,
3753                     Eina_List  *nodes);
3754
3755 /**
3756  * TODO FIX ME
3757  * @ingroup Eet_Node_Group
3758  */
3759 EAPI Eet_Node *
3760 eet_node_struct_child_new(const char *parent,
3761                           Eet_Node   *child);
3762
3763 /**
3764  * @brief Get a node's child nodes
3765  * @param node The node
3766  * @return The first child node which contains a pointer to the
3767  * next child node and the parent.
3768  * @since 1.5
3769  */
3770 EAPI Eet_Node *
3771 eet_node_children_get(Eet_Node *node);
3772
3773 /**
3774  * @brief Get the next node in a list of nodes
3775  * @param node The node
3776  * @return A node which contains a pointer to the
3777  * next child node and the parent.
3778  * @since 1.5
3779  */
3780 EAPI Eet_Node *
3781 eet_node_next_get(Eet_Node *node);
3782
3783 /**
3784  * @brief Get the parent node of a node
3785  * @param node The node
3786  * @return The parent node of @p node
3787  * @since 1.5
3788  */
3789 EAPI Eet_Node *
3790 eet_node_parent_get(Eet_Node *node);
3791
3792 /**
3793  * TODO FIX ME
3794  * @ingroup Eet_Node_Group
3795  */
3796 EAPI void
3797 eet_node_list_append(Eet_Node   *parent,
3798                      const char *name,
3799                      Eet_Node   *child);
3800
3801 /**
3802  * TODO FIX ME
3803  * @ingroup Eet_Node_Group
3804  */
3805 EAPI void
3806 eet_node_struct_append(Eet_Node   *parent,
3807                        const char *name,
3808                        Eet_Node   *child);
3809
3810 /**
3811  * TODO FIX ME
3812  * @ingroup Eet_Node_Group
3813  */
3814 EAPI void
3815 eet_node_hash_add(Eet_Node   *parent,
3816                   const char *name,
3817                   const char *key,
3818                   Eet_Node   *child);
3819
3820 /**
3821  * TODO FIX ME
3822  * @ingroup Eet_Node_Group
3823  */
3824 EAPI void
3825 eet_node_dump(Eet_Node  *n,
3826               int        dumplevel,
3827               Eet_Dump_Callback dumpfunc,
3828               void      *dumpdata);
3829
3830 /**
3831  * @brief Return the type of a node
3832  * @param node The node
3833  * @return The node's type (EET_T_$TYPE)
3834  * @since 1.5
3835  */
3836 EAPI int
3837 eet_node_type_get(Eet_Node *node);
3838
3839 /**
3840  * @brief Return the node's data
3841  * @param node The node
3842  * @return The data contained in the node
3843  * @since 1.5
3844  */
3845 EAPI Eet_Node_Data *
3846 eet_node_value_get(Eet_Node *node);
3847
3848 /**
3849  * TODO FIX ME
3850  * @ingroup Eet_Node_Group
3851  */
3852 EAPI void
3853 eet_node_del(Eet_Node *n);
3854
3855 /**
3856  * TODO FIX ME
3857  * @ingroup Eet_Node_Group
3858  */
3859 EAPI void *
3860 eet_data_node_encode_cipher(Eet_Node   *node,
3861                             const char *cipher_key,
3862                             int        *size_ret);
3863
3864 /**
3865  * TODO FIX ME
3866  * @ingroup Eet_Node_Group
3867  */
3868 EAPI Eet_Node *
3869 eet_data_node_decode_cipher(const void *data_in,
3870                             const char *cipher_key,
3871                             int         size_in);
3872
3873 /**
3874  * TODO FIX ME
3875  * @ingroup Eet_Node_Group
3876  */
3877 EAPI Eet_Node *
3878 eet_data_node_read_cipher(Eet_File   *ef,
3879                           const char *name,
3880                           const char *cipher_key);
3881
3882 /**
3883  * TODO FIX ME
3884  * @ingroup Eet_Node_Group
3885  */
3886 EAPI int
3887 eet_data_node_write_cipher(Eet_File   *ef,
3888                            const char *name,
3889                            const char *cipher_key,
3890                            Eet_Node   *node,
3891                            int         compress);
3892
3893 /* EXPERIMENTAL: THIS API MAY CHANGE IN THE FUTURE, USE IT ONLY IF YOU KNOW WHAT YOU ARE DOING. */
3894
3895 /**
3896  * @typedef Eet_Node_Walk
3897  * Describes how to walk trees of #Eet_Node.
3898  */
3899 typedef struct _Eet_Node_Walk   Eet_Node_Walk;
3900
3901 typedef void *(*Eet_Node_Walk_Struct_Alloc_Callback)(const char *type, void *user_data);
3902 typedef void  (*Eet_Node_Walk_Struct_Add_Callback)(void *parent, const char *name, void *child, void *user_data);
3903 typedef void *(*Eet_Node_Walk_Array_Callback)(Eina_Bool variable, const char *name, int count, void *user_data);
3904 typedef void  (*Eet_Node_Walk_Insert_Callback)(void *array, int index, void *child, void *user_data);
3905 typedef void *(*Eet_Node_Walk_List_Callback)(const char *name, void *user_data);
3906 typedef void  (*Eet_Node_Walk_Append_Callback)(void *list, void *child, void *user_data);
3907 typedef void *(*Eet_Node_Walk_Hash_Callback)(void *parent, const char *name, const char *key, void *value, void *user_data);
3908 typedef void *(*Eet_Node_Walk_Simple_Callback)(int type, Eet_Node_Data *data, void *user_data);
3909   
3910 /**
3911  * @struct _Eet_Node_Walk
3912  * Describes how to walk trees of #Eet_Node.
3913  */
3914 struct _Eet_Node_Walk
3915 {
3916    Eet_Node_Walk_Struct_Alloc_Callback struct_alloc;
3917    Eet_Node_Walk_Struct_Add_Callback struct_add;
3918    Eet_Node_Walk_Array_Callback array;
3919    Eet_Node_Walk_Insert_Callback insert;
3920    Eet_Node_Walk_List_Callback list;
3921    Eet_Node_Walk_Append_Callback append;
3922    Eet_Node_Walk_Hash_Callback hash;
3923    Eet_Node_Walk_Simple_Callback simple;
3924 };
3925
3926 EAPI void *
3927 eet_node_walk(void          *parent,
3928               const char    *name,
3929               Eet_Node      *root,
3930               Eet_Node_Walk *cb,
3931               void          *user_data);
3932
3933 /*******/
3934
3935 /**
3936  * @defgroup Eet_Connection_Group Helper function to use eet over a network link
3937  *
3938  * Function that reconstruct and prepare packet of @ref Eet_Data_Group to be send.
3939  *
3940  */
3941
3942 /**
3943  * @typedef Eet_Connection
3944  * Opaque handle to track paquet for a specific connection.
3945  *
3946  * @ingroup Eet_Connection_Group
3947  */
3948 typedef struct _Eet_Connection   Eet_Connection;
3949
3950 /**
3951  * @typedef Eet_Read_Cb
3952  * Called back when an @ref Eet_Data_Group has been received completly and could be used.
3953  *
3954  * @ingroup Eet_Connection_Group
3955  */
3956 typedef Eina_Bool Eet_Read_Cb (const void *eet_data, size_t size, void *user_data);
3957
3958 /**
3959  * @typedef Eet_Write_Cb
3960  * Called back when a packet containing @ref Eet_Data_Group data is ready to be send.
3961  *
3962  * @ingroup Eet_Connection_Group
3963  */
3964 typedef Eina_Bool Eet_Write_Cb (const void *data, size_t size, void *user_data);
3965
3966 /**
3967  * Instanciate a new connection to track.
3968  * @param eet_read_cb Function to call when one Eet_Data packet has been fully assemble.
3969  * @param eet_write_cb Function to call when one Eet_Data packet is ready to be send over the wire.
3970  * @param user_data Pointer provided to both functions to be used as a context handler.
3971  * @return NULL on failure, or a valid Eet_Connection handler.
3972  *
3973  * For every connection to track you will need a separate Eet_Connection provider.
3974  *
3975  * @since 1.2.4
3976  * @ingroup Eet_Connection_Group
3977  */
3978 EAPI Eet_Connection *
3979 eet_connection_new(Eet_Read_Cb  *eet_read_cb,
3980                    Eet_Write_Cb *eet_write_cb,
3981                    const void   *user_data);
3982
3983 /**
3984  * Process a raw packet received over the link
3985  * @param conn Connection handler to track.
3986  * @param data Raw data packet.
3987  * @param size The size of that packet.
3988  * @return 0 on complete success, any other value indicate where in the stream it got wrong (It could be before that packet).
3989  *
3990  * Every time you receive a packet related to your connection, you should pass
3991  * it to that function so that it could process and assemble packet has you
3992  * receive it. It will automatically call Eet_Read_Cb when one is fully received.
3993  *
3994  * @since 1.2.4
3995  * @ingroup Eet_Connection_Group
3996  */
3997 EAPI int
3998 eet_connection_received(Eet_Connection *conn,
3999                         const void     *data,
4000                         size_t          size);
4001
4002 /**
4003  * Convert a complex structure and prepare it to be send.
4004  * @param conn Connection handler to track.
4005  * @param edd The data descriptor to use when encoding.
4006  * @param data_in The pointer to the struct to encode into data.
4007  * @param cipher_key The key to use as cipher.
4008  * @return EINA_TRUE if the data where correctly send, EINA_FALSE if they don't.
4009  *
4010  * This function serialize data_in with edd, assemble the packet and call
4011  * Eet_Write_Cb when ready. The data passed Eet_Write_Cb are temporary allocated
4012  * and will vanish just after the return of the callback.
4013  *
4014  * @see eet_data_descriptor_encode_cipher
4015  *
4016  * @since 1.2.4
4017  * @ingroup Eet_Connection_Group
4018  */
4019 EAPI Eina_Bool
4020 eet_connection_send(Eet_Connection      *conn,
4021                     Eet_Data_Descriptor *edd,
4022                     const void          *data_in,
4023                     const char          *cipher_key);
4024
4025 /**
4026  * Convert a Eet_Node tree and prepare it to be send.
4027  * @param conn Connection handler to track.
4028  * @param node The data tree to use when encoding.
4029  * @param cipher_key The key to use as cipher.
4030  * @return EINA_TRUE if the data where correctly send, EINA_FALSE if they don't.
4031  *
4032  * This function serialize node, assemble the packet and call
4033  * Eet_Write_Cb when ready. The data passed Eet_Write_Cb are temporary allocated
4034  * and will vanish just after the return of the callback.
4035  *
4036  * @see eet_data_node_encode_cipher
4037  *
4038  * @since 1.2.4
4039  * @ingroup Eet_Connection_Group
4040  */
4041 EAPI Eina_Bool
4042 eet_connection_node_send(Eet_Connection *conn,
4043                          Eet_Node       *node,
4044                          const char     *cipher_key);
4045
4046 /**
4047  * Close a connection and lost its track.
4048  * @param conn Connection handler to close.
4049  * @param on_going Signal if a partial packet wasn't completed.
4050  * @return the user_data passed to both callback.
4051  *
4052  * @since 1.2.4
4053  * @ingroup Eet_Connection_Group
4054  */
4055 EAPI void *
4056 eet_connection_close(Eet_Connection *conn,
4057                      Eina_Bool      *on_going);
4058
4059 /***************************************************************************/
4060
4061 #ifdef __cplusplus
4062 }
4063 #endif /* ifdef __cplusplus */
4064
4065 #endif /* ifndef _EET_H */