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 enum { U_PATH_NOT_FOUND, U_PATH_IN_EMBFS, U_PATH_IN_FS };
00048
00049 int u_file_exists(const char*);
00050 int u_write_debug_message(const char*, const char*, int, const char*,
00051 const char*, ...);
00052
00053 struct dirent;
00054 int u_foreach_dir_item(const char *, unsigned int,
00055 int (*)(struct dirent*, const char *, void*),
00056 void*);
00057
00058 char* u_strnrchr(const char *s, char c, size_t len);
00059 char *u_stristr(const char *string, const char *sub);
00060 char *u_strnstr(const char *string, const char *sub, size_t stringlen);
00061
00062 enum { U_COPY_VERBATIM, U_COPY_ENCODE, U_COPY_DECODE };
00063
00064 enum { URLCPY_VERBATIM, URLCPY_ENCODE, URLCPY_DECODE };
00065 ssize_t u_urlncpy(char *dst, const char *src, size_t slen, int flags);
00066
00067 enum { HEXCPY_VERBATIM, HEXCPY_ENCODE, HEXCPY_DECODE };
00068 ssize_t u_hexncpy(char *dst, const char *src, size_t slen, int flags);
00069
00070 enum { HTMLCPY_VERBATIM, HTMLCPY_ENCODE, HTMLCPY_DECODE };
00071 ssize_t u_htmlncpy(char *dst, const char *src, size_t slen, int flags);
00072
00073 enum { SQLCPY_VERBATIM, SQLCPY_ENCODE, SQLCPY_DECODE };
00074 ssize_t u_sqlncpy(char *dst, const char *src, size_t slen, int flags);
00075
00076 int u_printf_ccstr(io_t *o, const char *buf, size_t sz);
00077
00078 int u_file_open(const char *file, int flags, io_t **pio);
00079 int u_tmpfile_open(io_t **pio);
00080 int u_getline(io_t *io, u_string_t *ln);
00081 int u_fgetline(FILE *in, u_string_t *ln);
00082
00083 int u_io_unzip_copy(io_t *out, const char *data, size_t size);
00084
00085 void u_tohex(char *hex, const char *src, size_t sz);
00086 char u_tochex(int n);
00087
00088 int u_md5(const char *buf, size_t sz, char out[MD5_DIGEST_BUFSZ]);
00089 int u_md5io(io_t *io, char out[MD5_DIGEST_BUFSZ]);
00090
00091 typedef void (*u_sig_t)(int);
00092 int u_signal(int sig, u_sig_t handler);
00093 int u_sig_block(int sig);
00094 int u_sig_unblock(int sig);
00095
00096 const char* u_guess_mime_type(const char *file_name);
00097 const mime_map_t* u_get_mime_map(const char *file_name);
00098 int u_match_ext(const char *filename, const char *extension);
00099
00100
00101 int u_tt_to_rfc822(char dst[], time_t ts);
00102 int u_httpdate_to_tt(const char *str, time_t *tp);
00103 int u_rfc822_to_tt(const char *str, time_t *tp);
00104 int u_rfc850_to_tt(const char *str, time_t *tp);
00105 int u_asctime_to_tt(const char *str, time_t *tp);
00106
00107 void u_print_version_and_exit(void);
00108
00109 int u_uri_normalize(char *fqn);
00110 int u_path_where_art_thou(const char *fqn, int *where);
00111
00112 int u_pwd_init_agnostic (const char *fqn, int hashed, int in_memory,
00113 u_pwd_t **ppwd);
00114
00115 #ifdef HAVE_LIBOPENSSL
00116 int u_cipher_encrypt(const EVP_CIPHER *cipher, unsigned char *key,
00117 unsigned char *iv, char *dst, size_t *dcount, const char *src, size_t ssz);
00118 int u_cipher_decrypt(const EVP_CIPHER *cipher, unsigned char *key,
00119 unsigned char *iv, char *dst, size_t *dcount, const char *src, size_t ssz);
00120 #endif
00121
00122 #ifdef __cplusplus
00123 }
00124 #endif
00125
00126 #endif