test: Move server to simline subdirectory
[libisds.git] / src / utils.h
blob5752b90b0bca8ad0cb53e76a7a0aa99fd9d2f609
1 #ifndef __ISDS_UTILS_H__
2 #define __ISDS_UTILS_H__
4 #include <stdlib.h>
5 #include <stdarg.h>
6 #include <stdio.h>
7 #include <time.h>
9 /* _hidden macro marks library private symbols. GCC can exclude them from global
10 * symbols table */
11 #if defined(__GNUC__) && (__GNUC__ >= 4)
12 #define _hidden __attribute__((visibility("hidden")))
13 #else
14 #define _hidden
15 #endif
17 /* PANIC macro aborts current process without any clean up.
18 * Use it as last resort fatal error solution */
19 #define PANIC(message) { \
20 if (stderr != NULL ) fprintf(stderr, \
21 "LIBISDS PANIC (%s:%d): %s\n", __FILE__, __LINE__, (message)); \
22 abort(); \
25 /* Concatenate two strings into newly allocated buffer.
26 * You must free() them, when you don't need it anymore.
27 * Any of the arguments can be NULL meaning empty string.
28 * In case of error returns NULL.
29 * Empty string is always returned as allocated empty string. */
30 char *_isds_astrcat(const char *first, const char *second);
32 /* Concatenate three strings into newly allocated buffer.
33 * You must free() them, when you don't need it anymore.
34 * Any of the arguments can be NULL meaning empty string.
35 * In case of error returns NULL.
36 * Empty string is always returned as allocated empty string. */
37 char *_isds_astrcat3(const char *first, const char *second,
38 const char *third);
40 /* Print formatted string into automatically reallocated @buffer.
41 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
42 * memory.
43 * @format format string as for printf(3)
44 * @ap list of variadic arguments, after call will be in undefined state
45 * @Returns number of bytes printed. In case of error, -1 and NULL @buffer*/
46 int isds_vasprintf(char **buffer, const char *format, va_list ap);
48 /* Print formatted string into automatically reallocated @buffer.
49 * @buffer automatically reallocated buffer. Must be &NULL or preallocated
50 * memory.
51 * @format format string as for printf(3)
52 * @... variadic arguments
53 * @Returns number of bytes printed. In case of error, -1 and NULL @buffer*/
54 int isds_asprintf(char **buffer, const char *format, ...);
56 /* Converts UTF8 string into locale encoded string.
57 * @utf string int UTF-8 terminated by zero byte
58 * @return allocated string encoded in locale specific encoding. You must free
59 * it. In case of error or NULL @utf returns NULL. */
60 char *_isds_utf82locale(const char *utf);
62 /* Encode given data into MIME Base64 encoded zero terminated string.
63 * @plain are input data (binary stream)
64 * @length is length of @plain data in bytes
65 * @return allocated string of base64 encoded plain data or NULL in case of
66 * error. You must free it. */
67 char *_isds_b64encode(const void *plain, const size_t length);
69 /* Decode given data from MIME Base64 encoded zero terminated string to binary
70 * stream. Invalid Base64 symbols are skipped.
71 * @encoded are input data (Base64 zero terminated string)
72 * @plain are automatically reallocated output data (binary stream). You must
73 * free it. Will be freed in case of error.
74 * @return length of @plain data in bytes or (size_t) -1 in case of memory
75 * allocation failure. */
76 size_t _isds_b64decode(const char *encoded, void **plain);
78 /* Convert UTC broken time to time_t.
79 * @broken_utc it time in UTC in broken format. Despite its content is not
80 * touched, it'sw not-const because underlying POSIX function has non-const
81 * signature.
82 * @return (time_t) -1 in case of error */
83 time_t _isds_timegm(struct tm *broken_utc);
85 /* Free() and set to NULL pointed memory */
86 #define zfree(memory) { \
87 free(memory); \
88 (memory) = NULL; \
91 #endif