00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef _KLONE_UTILS_H_
00012 #define _KLONE_UTILS_H_
00013
00014 #include "klone_conf.h"
00015 #ifdef HAVE_STDINT
00016 #include <stdint.h>
00017 #endif
00018 #include <stdarg.h>
00019 #include <stdio.h>
00020 #include <limits.h>
00021 #include <time.h>
00022 #include <signal.h>
00023 #include <u/libu.h>
00024 #include <klone/io.h>
00025 #include <klone/md5.h>
00026 #include <klone/os.h>
00027 #include <klone/mime_map.h>
00028
00029 #ifdef __cplusplus
00030 extern "C" {
00031 #endif
00032
00033 #ifndef MIN
00034 #define MIN(a,b) (a < b ? a : b)
00035 #endif
00036
00037 #ifndef MAX
00038 #define MAX(a,b) (a > b ? a : b)
00039 #endif
00040
00041 #define KLONE_FREE(p) do {if (p) { free(p); p = NULL; }} while (0)
00042
00043 #define klone_die(...) do { con(__VA_ARGS__); exit(EXIT_FAILURE); } while(0)
00044 #define klone_die_if(cond, ...) \
00045 do { dbg_ifb(cond) klone_die(__VA_ARGS__); } while(0)
00046
00047 int u_file_exists(const char*);
00048 int u_write_debug_message(const char*, const char*, int, const char*,
00049 const char*, ...);
00050
00051 struct dirent;
00052 int u_foreach_dir_item(const char *, unsigned int,
00053 int (*)(struct dirent*, const char *, void*),
00054 void*);
00055
00056 char* u_strnrchr(const char *s, char c, size_t len);
00057 char *u_stristr(const char *string, const char *sub);
00058 char *u_strnstr(const char *string, const char *sub, size_t stringlen);
00059
00060 enum { U_COPY_VERBATIM, U_COPY_ENCODE, U_COPY_DECODE };
00061
00062 enum { URLCPY_VERBATIM, URLCPY_ENCODE, URLCPY_DECODE };
00063 ssize_t u_urlncpy(char *dst, const char *src, size_t slen, int flags);
00064
00065 enum { HEXCPY_VERBATIM, HEXCPY_ENCODE, HEXCPY_DECODE };
00066 ssize_t u_hexncpy(char *dst, const char *src, size_t slen, int flags);
00067
00068 enum { HTMLCPY_VERBATIM, HTMLCPY_ENCODE, HTMLCPY_DECODE };
00069 ssize_t u_htmlncpy(char *dst, const char *src, size_t slen, int flags);
00070
00071 enum { SQLCPY_VERBATIM, SQLCPY_ENCODE, SQLCPY_DECODE };
00072 ssize_t u_sqlncpy(char *dst, const char *src, size_t slen, int flags);
00073
00074 int u_printf_ccstr(io_t *o, const char *buf, size_t sz);
00075
00076 int u_file_open(const char *file, int flags, io_t **pio);
00077 int u_tmpfile_open(io_t **pio);
00078 int u_getline(io_t *io, u_string_t *ln);
00079 int u_fgetline(FILE *in, u_string_t *ln);
00080
00081 int u_io_unzip_copy(io_t *out, const char *data, size_t size);
00082
00083 void u_tohex(char *hex, const char *src, size_t sz);
00084 char u_tochex(int n);
00085
00086 int u_md5(char *buf, size_t sz, char out[MD5_DIGEST_BUFSZ]);
00087 int u_md5io(io_t *io, char out[MD5_DIGEST_BUFSZ]);
00088
00089 typedef void (*u_sig_t)(int);
00090 int u_signal(int sig, u_sig_t handler);
00091 int u_sig_block(int sig);
00092 int u_sig_unblock(int sig);
00093
00094 const char* u_guess_mime_type(const char *file_name);
00095 const mime_map_t* u_get_mime_map(const char *file_name);
00096 int u_match_ext(const char *filename, const char *extension);
00097
00098
00099 int u_tt_to_rfc822(char dst[], time_t ts);
00100 int u_httpdate_to_tt(const char *str, time_t *tp);
00101 int u_rfc822_to_tt(const char *str, time_t *tp);
00102 int u_rfc850_to_tt(const char *str, time_t *tp);
00103 int u_asctime_to_tt(const char *str, time_t *tp);
00104
00105 void u_print_version_and_exit(void);
00106
00107 int u_uri_normalize(char *fqn);
00108
00109 #ifdef HAVE_LIBOPENSSL
00110 int u_cipher_encrypt(const EVP_CIPHER *cipher, unsigned char *key,
00111 unsigned char *iv, char *dst, size_t *dcount, const char *src, size_t ssz);
00112 int u_cipher_decrypt(const EVP_CIPHER *cipher, unsigned char *key,
00113 unsigned char *iv, char *dst, size_t *dcount, const char *src, size_t ssz);
00114 #endif
00115
00116 #ifdef __cplusplus
00117 }
00118 #endif
00119
00120 #endif