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