# HG changeset patch # User Olaf Wintermann # Date 1378555723 -7200 # Node ID 3e55bed345f9ed18aef72d344374eec4788608fe # Parent b855f76e965b14d107218538dcb552d39fcb69c2 added some aes functions + ucx update diff -r b855f76e965b -r 3e55bed345f9 dav/crypto.c --- a/dav/crypto.c Tue Sep 03 12:08:35 2013 +0200 +++ b/dav/crypto.c Sat Sep 07 14:08:43 2013 +0200 @@ -31,6 +31,7 @@ #include #include +#include #include "crypto.h" AESDecrypter* aes_decrypter_new(Key *key, void *stream, dav_write_func write_func) { @@ -192,3 +193,76 @@ EVP_CIPHER_CTX_cleanup(&enc->ctx); free(enc); } + + +char* aes_encrypt(char *in, Key *key) { + char *iv = malloc(16); + if(!RAND_bytes(iv, 16)) { + free(iv); + return NULL; + } + + EVP_CIPHER_CTX ctx; + EVP_CIPHER_CTX_init(&ctx); + if(key->type == KEY_AES128) { + EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, key->data, iv); + } else if(key->type == KEY_AES256) { + EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, key->data, iv); + } else { + return NULL; + } + + int len = strlen(in); + int buflen = len + 64; + char *buf = calloc(1, buflen); + memcpy(buf, iv, 16); + + int l = buflen - 16; + EVP_EncryptUpdate(&ctx, buf + 16, &l, in, len); + + int f = 0; + EVP_EncryptFinal_ex(&ctx, buf + 16 + l, &f); + char *out = util_base64encode(buf, 16 + l + f); + free(buf); + return out; +} + +char* aes_decrypt(char *in, Key *key) { + int len; + char *buf = util_base64decode_len(in, &len); + + EVP_CIPHER_CTX ctx; + EVP_CIPHER_CTX_init(&ctx); + if(key->type == KEY_AES128) { + EVP_DecryptInit_ex( + &ctx, + EVP_aes_128_cbc(), + NULL, + key->data, + buf); + } else if(key->type == KEY_AES256) { + EVP_DecryptInit_ex( + &ctx, + EVP_aes_256_cbc(), + NULL, + key->data, + buf); + } else { + return NULL; + } + + char *out = malloc(len + 1); + int outlen = len; + char *in_buf = buf + 16; + int inlen = len - 16; + int f = 0; + + + + EVP_DecryptUpdate(&ctx, out, &outlen, in_buf, inlen); + EVP_DecryptFinal_ex(&ctx, out + outlen, &f); + out[outlen + f] = '\0'; + free(buf); + return out; +} + diff -r b855f76e965b -r 3e55bed345f9 dav/crypto.h --- a/dav/crypto.h Tue Sep 03 12:08:35 2013 +0200 +++ b/dav/crypto.h Sat Sep 07 14:08:43 2013 +0200 @@ -32,6 +32,7 @@ #include #include "config.h" #include +#include #ifdef __cplusplus extern "C" { @@ -68,6 +69,9 @@ size_t aes_read(void *buf, size_t s, size_t n, AESEncrypter *enc); void aes_encrypter_close(AESEncrypter *enc); +char* aes_encrypt(char *in, Key *key); +char* aes_decrypt(char *in, Key *key); + #ifdef __cplusplus } #endif diff -r b855f76e965b -r 3e55bed345f9 libidav/Makefile --- a/libidav/Makefile Tue Sep 03 12:08:35 2013 +0200 +++ b/libidav/Makefile Sat Sep 07 14:08:43 2013 +0200 @@ -37,13 +37,13 @@ OBJ = $(SRC:%.c=../build/libidav/%.$(OBJ_EXT)) -all: libidav +all: ../build/ucx ../build/libidav.$(LIB_EXT) -libidav: $(OBJ) +../build/libidav.$(LIB_EXT): $(OBJ) $(AR) $(ARFLAGS) $(AOFLAGS)../build/libidav.$(LIB_EXT) $(OBJ) -../build/libidav/%.$(OBJ_EXT): %.c ../build/ucx +../build/libidav/%.$(OBJ_EXT): %.c $(CC) $(CFLAGS) $(DAV_CFLAGS) -I../ $(COFLAGS)$@ $< ../build/idav: - mkdir -p ../build/idav + $(MKDIR) $(MKDIRFLAGS) ../build/idav diff -r b855f76e965b -r 3e55bed345f9 libidav/utils.c --- a/libidav/utils.c Tue Sep 03 12:08:35 2013 +0200 +++ b/libidav/utils.c Sat Sep 07 14:08:43 2013 +0200 @@ -197,8 +197,12 @@ } +char* util_base64decode(char *in) { + int len = 0; + return util_base64decode_len(in, &len); +} -char* util_base64decode(char* in) { +char* util_base64decode_len(char* in, int *outlen) { size_t len = strlen(in); char *out = calloc(1, len); @@ -207,12 +211,34 @@ BIO_set_flags(d, BIO_FLAGS_BASE64_NO_NL); b = BIO_push(d, b); - BIO_read(b, out, len); + *outlen = BIO_read(b, out, len); BIO_free_all(b); return out; } +char* util_base64encode(char *in, size_t len) { + BIO *b; + BIO *e; + BUF_MEM *mem; + + e = BIO_new(BIO_f_base64()); + b = BIO_new(BIO_s_mem()); + + e = BIO_push(e, b); + BIO_write(e, in, len); + BIO_flush(e); + + BIO_get_mem_ptr(e, &mem); + char *out = malloc(mem->length); + memcpy(out, mem->data, mem->length -1); + out[mem->length - 1] = '\0'; + + BIO_free_all(e); + + return out; +} + /* * gets a substring from 0 to the appearance of the token * tokens are separated by space diff -r b855f76e965b -r 3e55bed345f9 libidav/utils.h --- a/libidav/utils.h Tue Sep 03 12:08:35 2013 +0200 +++ b/libidav/utils.h Sat Sep 07 14:08:43 2013 +0200 @@ -66,7 +66,9 @@ char* util_xml_get_text(xmlNode *elm); -char* util_base64decode(char* in); +char* util_base64decode(char *in); +char* util_base64decode_len(char *in, int *outlen); +char* util_base64encode(char *in, size_t len); sstr_t util_getsubstr_until_token(sstr_t str, sstr_t token, sstr_t *sub); diff -r b855f76e965b -r 3e55bed345f9 libidav/webdav.c --- a/libidav/webdav.c Tue Sep 03 12:08:35 2013 +0200 +++ b/libidav/webdav.c Sat Sep 07 14:08:43 2013 +0200 @@ -248,6 +248,28 @@ free(sn); } + +void* dav_session_malloc(DavSession *sn, size_t size) { + UcxAllocator *a = sn->allocator; + return a->malloc(a->pool, size); +} + +void* dav_session_calloc(DavSession *sn, size_t nelm, size_t size) { + UcxAllocator *a = sn->allocator; + return a->calloc(a->pool, nelm, size); +} + +void* dav_session_realloc(DavSession *sn, void *ptr, size_t size) { + UcxAllocator *a = sn->allocator; + return a->realloc(a->pool, ptr, size); +} + +void dav_session_free(DavSession *sn, void *ptr) { + UcxAllocator *a = sn->allocator; + a->free(a->pool, ptr); +} + + DavResource* dav_get(DavSession *sn, char *path, char *properties) { char *url = util_concat_path(sn->base_url, path); diff -r b855f76e965b -r 3e55bed345f9 libidav/webdav.h --- a/libidav/webdav.h Tue Sep 03 12:08:35 2013 +0200 +++ b/libidav/webdav.h Sat Sep 07 14:08:43 2013 +0200 @@ -133,8 +133,13 @@ void dav_session_set_auth(DavSession *sn, char *user, char *password); void session_set_error(DavSession *sn, CURLcode c, int status); +void dav_session_destroy(DavSession *sn); -void dav_session_destroy(DavSession *sn); +void* dav_session_malloc(DavSession *sn, size_t size); +void* dav_session_calloc(DavSession *sn, size_t nelm, size_t size); +void* dav_session_realloc(DavSession *sn, void *ptr, size_t size); +void dav_session_free(DavSession *sn, void *ptr); + DavResource* dav_get(DavSession *sn, char *path, char *properties); DavResource* dav_get2(DavSession *sn, DavGetQuery *query); diff -r b855f76e965b -r 3e55bed345f9 ucx/Makefile --- a/ucx/Makefile Tue Sep 03 12:08:35 2013 +0200 +++ b/ucx/Makefile Sat Sep 07 14:08:43 2013 +0200 @@ -40,15 +40,15 @@ SRC += logging.c SRC += buffer.c -OBJ = $(SRC:%.c=../build/ucx/%.$(OBJ_EXT)) +OBJ = $(SRC:%.c=../build/ucx/%$(OBJ_EXT)) -all: libucx +all: ../build/ucx ../build/libucx.$(LIB_EXT) -libucx: $(OBJ) +../build/libucx.$(LIB_EXT): $(OBJ) $(AR) $(ARFLAGS) $(AOFLAGS)../build/libucx.$(LIB_EXT) $(OBJ) -../build/ucx/%.$(OBJ_EXT): %.c ../build/ucx +../build/ucx/%$(OBJ_EXT): %.c $(CC) $(CFLAGS) $(COFLAGS)$@ $< ../build/ucx: - mkdir -p ../build/ucx + $(MKDIR) $(MKDIRFLAGS) ../build/ucx diff -r b855f76e965b -r 3e55bed345f9 ucx/logging.c --- a/ucx/logging.c Tue Sep 03 12:08:35 2013 +0200 +++ b/ucx/logging.c Sat Sep 07 14:08:43 2013 +0200 @@ -61,11 +61,13 @@ free(logger); } +// estimated max. message length (documented) +#define UCX_LOGGER_MSGMAX 4096 + void ucx_logger_logf(UcxLogger *logger, unsigned int level, const char* file, const unsigned int line, const char *format, ...) { if (level <= logger->level) { - const size_t max = 4096; // estimated max. message length (documented) - char msg[max]; + char msg[UCX_LOGGER_MSGMAX]; char *text; size_t k = 0; size_t n; @@ -85,14 +87,18 @@ n = strlen(file); memcpy(msg+k, file, n); k += n; - k += sprintf(msg+k, ":%d ", line); +#ifdef _WIN32 + k += _snprintf(msg+k, UCX_LOGGER_MSGMAX-k, ":%d ", line); +#else + k += snprintf(msg+k, UCX_LOGGER_MSGMAX-k, ":%d ", line); +#endif /* _WIN32 */ } msg[k++] = '-'; msg[k++] = ' '; va_list args; va_start (args, format); - k += vsnprintf(msg+k, max-k-1, format, args); + k += vsnprintf(msg+k, UCX_LOGGER_MSGMAX-k-1, format, args); va_end (args); msg[k++] = '\n'; diff -r b855f76e965b -r 3e55bed345f9 ucx/properties.c --- a/ucx/properties.c Tue Sep 03 12:08:35 2013 +0200 +++ b/ucx/properties.c Sat Sep 07 14:08:43 2013 +0200 @@ -217,38 +217,36 @@ } } +// buffer size is documented - change doc, when you change bufsize! +#define UCX_PROPLOAD_BUFSIZE 1024 int ucx_properties_load(UcxMap *map, FILE *file) { UcxProperties *parser = ucx_properties_new(); if(!(parser && map && file)) { return 1; } - // buffer size is documented - change doc, when you change bufsize! - const size_t bufsize = 1024; - int error = 0; size_t r; - char buf[bufsize]; - while((r = fread(buf, 1, bufsize, file)) != 0) { + char buf[UCX_PROPLOAD_BUFSIZE]; + while((r = fread(buf, 1, UCX_PROPLOAD_BUFSIZE, file)) != 0) { ucx_properties_fill(parser, buf, r); error = ucx_properties2map(parser, map); if (error) { break; } } - ucx_properties_free(parser); return error; } int ucx_properties_store(UcxMap *map, FILE *file) { UcxMapIterator iter = ucx_map_iterator(map); - char *v; + void *v; sstr_t value; size_t written; UCX_MAP_FOREACH(k, v, iter) { - value = sstr(v); + value = sstr((char*)v); written = 0; written += fwrite(k.data, 1, k.len, file); diff -r b855f76e965b -r 3e55bed345f9 ucx/string.c --- a/ucx/string.c Tue Sep 03 12:08:35 2013 +0200 +++ b/ucx/string.c Sat Sep 07 14:08:43 2013 +0200 @@ -119,6 +119,20 @@ return n; } +sstr_t sstrrchr(sstr_t s, int c) { + if (s.length > 0) { + for(size_t i=s.length;i>0;i--) { + if(s.ptr[i-1] == c) { + return sstrsubs(s, i-1); + } + } + } + sstr_t n; + n.ptr = NULL; + n.length = 0; + return n; +} + sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n) { return sstrsplit_a(ucx_default_allocator(), s, d, n); } @@ -197,6 +211,20 @@ } } +int sstrcasecmp(sstr_t s1, sstr_t s2) { + if (s1.length == s2.length) { +#ifdef _WIN32 + return _strnicmp(s1.ptr, s2.ptr, s1.length); +#else + return strncasecmp(s1.ptr, s2.ptr, s1.length); +#endif + } else if (s1.length > s2.length) { + return 1; + } else { + return -1; + } +} + sstr_t sstrdup(sstr_t s) { return sstrdup_a(ucx_default_allocator(), s); } diff -r b855f76e965b -r 3e55bed345f9 ucx/string.h --- a/ucx/string.h Tue Sep 03 12:08:35 2013 +0200 +++ b/ucx/string.h Sat Sep 07 14:08:43 2013 +0200 @@ -195,13 +195,27 @@ * * @param string the string where to locate the character * @param chr the character to locate - * @return a substring starting at the least location of chr + * @return a substring starting at the first location of chr * * @see sstrsubs() */ sstr_t sstrchr(sstr_t string, int chr); /** + * Returns a substring starting at the location of the last occurrence of the + * specified character. + * + * If the string does not contain the character, an empty string is returned. + * + * @param string the string where to locate the character + * @param chr the character to locate + * @return a substring starting at the last location of chr + * + * @see sstrsubs() + */ +sstr_t sstrrchr(sstr_t string, int chr); + +/** * Splits a string into parts by using a delimiter string. * * This function will return NULL, if one of the following happens: @@ -287,6 +301,22 @@ int sstrcmp(sstr_t s1, sstr_t s2); /** + * Compares two UCX strings ignoring the case. + * + * At first it compares the sstr_t.length attribute of the two strings. If and + * only if the lengths match, both strings are compared char by char ignoring + * the case. + * + * @param s1 the first string + * @param s2 the second string + * @return -1, if the length of s1 is less than the length of s2 or 1, if the + * length of s1 is greater than the length of s2 or the difference between the + * first two differing characters otherwise (i.e. 0 if the strings match and + * no characters differ) + */ +int sstrcasecmp(sstr_t s1, sstr_t s2); + +/** * Creates a duplicate of the specified string. * * The new sstr_t will contain a copy allocated by standard diff -r b855f76e965b -r 3e55bed345f9 ucx/ucx.h --- a/ucx/ucx.h Tue Sep 03 12:08:35 2013 +0200 +++ b/ucx/ucx.h Sat Sep 07 14:08:43 2013 +0200 @@ -36,6 +36,15 @@ #ifndef UCX_H #define UCX_H +/** Major UCX version as integer constant. */ +#define UCX_VERSION_MAJOR 1 + +/** Minor UCX version as integer constant. */ +#define UCX_VERSION_MINOR 0 + +/** The UCX version in format [major].[minor] */ +#define UCX_VERSION UCX_VERSION_MAJOR.UCX_VERSION_MINOR + #include #ifdef _WIN32 @@ -54,9 +63,11 @@ #define _Bool bool #define restrict #endif +/** Use C naming even when compiling with C++. */ #define UCX_EXTERN extern "C" extern "C" { #else +/** Pointless in C. */ #define UCX_EXTERN #endif diff -r b855f76e965b -r 3e55bed345f9 ucx/utils.c --- a/ucx/utils.c Tue Sep 03 12:08:35 2013 +0200 +++ b/ucx/utils.c Sat Sep 07 14:08:43 2013 +0200 @@ -134,11 +134,17 @@ /* PRINTF FUNCTIONS */ +#ifdef va_copy #define UCX_PRINTF_BUFSIZE 256 +#else +#pragma message("WARNING: C99 va_copy macro not supported by this platform" \ + " - limiting ucx_*printf to 2 KiB") +#define UCX_PRINTF_BUFSIZE 0x800 +#endif int ucx_fprintf(void *stream, write_func wfc, const char *fmt, ...) { + int ret; va_list ap; - int ret; va_start(ap, fmt); ret = ucx_vfprintf(stream, wfc, fmt, ap); va_end(ap); @@ -147,6 +153,7 @@ int ucx_vfprintf(void *stream, write_func wfc, const char *fmt, va_list ap) { char buf[UCX_PRINTF_BUFSIZE]; +#ifdef va_copy va_list ap2; va_copy(ap2, ap); int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); @@ -167,13 +174,23 @@ } ret = vsnprintf(newbuf, len, fmt, ap2); - va_end(ap2); if (ret > 0) { ret = (int)wfc(newbuf, 1, ret, stream); } free(newbuf); } return ret; +#else + int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); + if (ret < 0) { + return ret; + } else if (ret < UCX_PRINTF_BUFSIZE) { + return (int)wfc(buf, 1, ret, stream); + } else { + errno = ENOMEM; + return -1; + } +#endif } sstr_t ucx_asprintf(UcxAllocator *allocator, const char *fmt, ...) { @@ -189,9 +206,10 @@ sstr_t s; s.ptr = NULL; s.length = 0; + char buf[UCX_PRINTF_BUFSIZE]; +#ifdef va_copy va_list ap2; va_copy(ap2, ap); - char buf[UCX_PRINTF_BUFSIZE]; int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { s.ptr = (char*)a->malloc(a->pool, ret + 1); @@ -204,7 +222,6 @@ int len = ret + 1; s.ptr = (char*)a->malloc(a->pool, len); ret = vsnprintf(s.ptr, len, fmt, ap2); - va_end(ap2); if (ret < 0) { free(s.ptr); s.ptr = NULL; @@ -212,5 +229,16 @@ s.length = (size_t)ret; } } +#else + int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); + if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { + s.ptr = (char*)a->malloc(a->pool, ret + 1); + s.length = (size_t)ret; + memcpy(s.ptr, buf, ret); + s.ptr[s.length] = '\0'; + } else { + errno = ENOMEM; + } +#endif return s; } diff -r b855f76e965b -r 3e55bed345f9 ucx/utils.h --- a/ucx/utils.h Tue Sep 03 12:08:35 2013 +0200 +++ b/ucx/utils.h Sat Sep 07 14:08:43 2013 +0200 @@ -36,9 +36,9 @@ */ #ifndef UCX_UTILS_H -#define UCX_UTILS_H +#define UCX_UTILS_H -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -242,9 +242,9 @@ #define ucx_bprintf(buffer, ...) ucx_fprintf((UcxBuffer*)buffer, \ (write_func)ucx_buffer_write, __VA_ARGS__) -#ifdef __cplusplus +#ifdef __cplusplus } #endif -#endif /* UCX_UTILS_H */ +#endif /* UCX_UTILS_H */