utils.h

00001 /*
00002  * Copyright (c) 2005-2012 by KoanLogic s.r.l. <http://www.koanlogic.com>
00003  * All rights reserved.
00004  *
00005  * This file is part of KLone, and as such it is subject to the license stated
00006  * in the LICENSE file which you have received as part of this distribution.
00007  *
00008  * $Id: utils.h,v 1.35 2009/05/29 10:26:00 tho Exp $
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 /* HAVE_STDINT */
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 #ifdef SSL_ON
00029 #include <openssl/evp.h>
00030 #endif
00031 
00032 #ifdef __cplusplus
00033 extern "C" {
00034 #endif
00035 
00036 #define KLONE_FREE(p) do {if (p) { free(p); p = NULL; }} while (0)
00037 
00038 #define klone_die(...) do { u_con(__VA_ARGS__); exit(EXIT_FAILURE); } while(0)
00039 #define klone_die_if(cond, ...) \
00040     do { dbg_ifb(cond) klone_die(__VA_ARGS__); } while(0)
00041 
00042 enum { U_PATH_NOT_FOUND, U_PATH_IN_EMBFS, U_PATH_IN_FS };
00043 
00044 const char *u_addr_get_ip (const char *a, char *d, size_t dlen);
00045 const char *u_addr_get_port (const char *a, char *d, size_t dlen);
00046 const char *u_addr_fmt (const char *ip, const char *port, char *d, size_t dlen);
00047 
00048 int u_file_exists(const char*);
00049 int u_write_debug_message(const char*, const char*, int, const char*, 
00050     const char*, ...);
00051 
00052 struct dirent;
00053 int u_foreach_dir_item(const char *, unsigned int,
00054     int (*)(struct dirent*, const char *, void*), 
00055     void*);
00056 
00057 char* u_strnrchr(const char *s, char c, size_t len);
00058 char *u_stristr(const char *string, const char *sub);
00059 char *u_strnstr(const char *string, const char *sub, size_t stringlen);
00060 
00061 enum { U_COPY_VERBATIM, U_COPY_ENCODE, U_COPY_DECODE };
00062 
00063 enum { URLCPY_VERBATIM, URLCPY_ENCODE, URLCPY_DECODE };
00064 ssize_t u_urlncpy(char *dst, const char *src, size_t slen, int flags);
00065 
00066 enum { HEXCPY_VERBATIM, HEXCPY_ENCODE, HEXCPY_DECODE };
00067 ssize_t u_hexncpy(char *dst, const char *src, size_t slen, int flags);
00068 
00069 enum { HTMLCPY_VERBATIM, HTMLCPY_ENCODE, HTMLCPY_DECODE };
00070 ssize_t u_htmlncpy(char *dst, const char *src, size_t slen, int flags);
00071 
00072 enum { SQLCPY_VERBATIM, SQLCPY_ENCODE, SQLCPY_DECODE };
00073 ssize_t u_sqlncpy(char *dst, const char *src, size_t slen, int flags);
00074 
00075 int u_printf_ccstr(io_t *o, const char *buf, size_t sz);
00076 
00077 int u_file_open(const char *file, int flags, io_t **pio);
00078 int u_tmpfile_open(const char *tmpdir, io_t **pio);
00079 int u_getline(io_t *io, u_string_t *ln);
00080 int u_fgetline(FILE *in, u_string_t *ln);
00081 
00082 int u_io_unzip_copy(io_t *out, const char *data, size_t size);
00083 
00084 void u_tohex(char *hex, const char *src, size_t sz);
00085 char u_tochex(int n);
00086 
00087 int u_md5(const char *buf, size_t sz, char out[MD5_DIGEST_BUFSZ]);
00088 int u_md5io(io_t *io, char out[MD5_DIGEST_BUFSZ]);
00089 
00090 typedef void (*u_sig_t)(int);
00091 int u_signal(int sig, u_sig_t handler);
00092 int u_sig_block(int sig);
00093 int u_sig_unblock(int sig);
00094 
00095 const char* u_guess_mime_type(const char *file_name);
00096 const mime_map_t* u_get_mime_map(const char *file_name);
00097 int u_match_ext(const char *filename, const char *extension);
00098 
00099 /* date time conversion funcs */
00100 #define RFC822_DATE_BUFSZ   32
00101 
00102 int u_tt_to_rfc822(char dst[RFC822_DATE_BUFSZ], time_t ts);
00103 int u_httpdate_to_tt(const char *str, time_t *tp);
00104 int u_rfc822_to_tt(const char *str, time_t *tp);
00105 int u_rfc850_to_tt(const char *str, time_t *tp);
00106 int u_asctime_to_tt(const char *str, time_t *tp);
00107 
00108 void u_print_version_and_exit(void);
00109 
00110 int u_uri_normalize(char *fqn);
00111 int u_path_where_art_thou(const char *fqn, int *where);
00112 
00113 int u_pwd_init_agnostic (const char *fqn, int hashed, int in_memory, 
00114         u_pwd_t **ppwd);
00115 
00116 #ifdef SSL_ON
00117 int u_cipher_encrypt(const EVP_CIPHER *cipher, unsigned char *key, 
00118     unsigned char *iv, char *dst, size_t *dcount, const char *src, size_t ssz);
00119 int u_cipher_decrypt(const EVP_CIPHER *cipher, unsigned char *key, 
00120     unsigned char *iv, char *dst, size_t *dcount, const char *src, size_t ssz);
00121 #endif
00122 
00123 #ifdef __cplusplus
00124 }
00125 #endif 
00126 
00127 #endif

←Products
Copyright © 2005-2012 - KoanLogic S.r.l. - All rights reserved