universe@25: /* universe@25: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@25: * universe@25: * Copyright 2015 Olaf Wintermann. All rights reserved. universe@25: * universe@25: * Redistribution and use in source and binary forms, with or without universe@25: * modification, are permitted provided that the following conditions are met: universe@25: * universe@25: * 1. Redistributions of source code must retain the above copyright universe@25: * notice, this list of conditions and the following disclaimer. universe@25: * universe@25: * 2. Redistributions in binary form must reproduce the above copyright universe@25: * notice, this list of conditions and the following disclaimer in the universe@25: * documentation and/or other materials provided with the distribution. universe@25: * universe@25: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@25: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@25: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@25: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@25: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@25: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@25: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@25: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@25: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@25: * POSSIBILITY OF SUCH DAMAGE. universe@25: */ universe@25: universe@25: #include universe@25: #include universe@25: #include universe@25: #include universe@25: #include universe@25: #include universe@25: #include universe@25: #include universe@25: #include universe@25: universe@25: #include universe@25: #include universe@25: #include universe@25: #include universe@25: #include universe@25: #include universe@25: universe@25: #include "utils.h" universe@25: #include "crypto.h" universe@25: #include "webdav.h" universe@25: universe@33: #define MACRO1337 1337L universe@25: universe@31: /* -------------------- This is a testing file. -------------------------- */ universe@31: /* universe@25: time_t util_parse_creationdate(char *str) { universe@25: // example: 2012-11-29T21:35:35Z universe@25: if(!str) { universe@25: return 0; universe@25: } universe@25: // TODO universe@25: return 0; universe@25: } universe@31: */ universe@25: time_t util_parse_lastmodified(char *str) { universe@25: // example: Thu, 29 Nov 2012 21:35:35 GMT universe@25: if(!str) { universe@25: return 0; universe@25: } else { universe@25: return curl_getdate(str, NULL); universe@25: } universe@25: } universe@25: universe@25: int util_getboolean(char *v) { universe@25: if(v[0] == 'T' || v[0] == 't') { universe@25: return 1; universe@25: } universe@25: return 0; universe@25: } universe@25: universe@25: int util_strtoint(char *str, int64_t *value) { universe@25: char *end; universe@25: int64_t val = strtoll(str, &end, 0); universe@25: if(strlen(end) == 0) { universe@25: *value = val; universe@25: return 1; universe@25: } else { universe@25: return 0; universe@25: } universe@25: } universe@25: universe@25: char* util_url_path(char *url) { universe@25: char *path = NULL; universe@25: size_t len = strlen(url); universe@25: int slashcount = 0; universe@25: int slmax; universe@25: if(len > 7 && !strncasecmp(url, "http://", 7)) { universe@25: slmax = 3; universe@25: } else if(len > 8 && !strncasecmp(url, "https://", 8)) { universe@25: slmax = 3; universe@25: } else { universe@25: slmax = 1; universe@25: } universe@25: char c; universe@25: for(int i=0;ihandle, url, strlen(url), NULL); universe@25: char *ret = strdup(unesc); universe@25: curl_free(unesc); universe@25: return ret; universe@25: } universe@25: universe@25: char* util_resource_name(char *url) { universe@25: int si = 0; universe@25: int osi = 0; universe@25: int i = 0; universe@25: int p = 0; universe@25: char c; universe@25: while((c = url[i]) != 0) { universe@25: if(c == '/') { universe@25: osi = si; universe@25: si = i; universe@25: p = 1; universe@25: } universe@25: i++; universe@25: } universe@25: universe@25: char *name = url + si + p; universe@25: if(name[0] == 0) { universe@25: name = url + osi + p; universe@25: if(name[0] == 0) { universe@25: return url; universe@25: } universe@25: } universe@25: universe@25: return name; universe@25: } universe@25: universe@25: int util_mkdir(char *path, mode_t mode) { universe@25: #ifdef _WIN32 universe@25: return mkdir(path); universe@25: #else universe@25: return mkdir(path, mode); universe@25: #endif universe@25: } universe@25: universe@25: char* util_concat_path(char *url_base, char *p) { universe@25: sstr_t base = sstr(url_base); universe@25: sstr_t path; universe@25: if(p) { universe@25: path = sstr(p); universe@25: } else { universe@25: path = sstrn("", 0); universe@25: } universe@25: universe@25: int add_separator = 0; universe@25: if(base.ptr[base.length-1] == '/') { universe@25: if(path.ptr[0] == '/') { universe@25: base.length--; universe@25: } universe@25: } else { universe@25: if(path.length == 0 || path.ptr[0] != '/') { universe@25: add_separator = 1; universe@25: } universe@25: } universe@25: universe@25: sstr_t url; universe@25: if(add_separator) { universe@25: url = sstrcat(3, base, sstr("/"), path); universe@25: } else { universe@25: url = sstrcat(2, base, path); universe@25: } universe@25: universe@25: return url.ptr; universe@25: } universe@25: universe@25: void util_set_url(DavSession *sn, char *href) { universe@25: sstr_t base = sstr(sn->base_url); universe@25: sstr_t href_str = sstr(href); universe@25: universe@25: char *base_path = util_url_path(sn->base_url); universe@25: base.length -= strlen(base_path); universe@25: universe@25: sstr_t url = sstrcat(2, base, href_str); universe@25: universe@25: curl_easy_setopt(sn->handle, CURLOPT_URL, url.ptr); universe@25: free(url.ptr); universe@25: } universe@25: universe@25: char* util_path_to_url(DavSession *sn, char *path) { universe@25: char *space = malloc(256); universe@25: UcxBuffer *url = ucx_buffer_new(space, 256, UCX_BUFFER_AUTOEXTEND); universe@25: universe@25: // add base url universe@25: ucx_buffer_write(sn->base_url, 1, strlen(sn->base_url), url); universe@25: // remove trailing slash universe@25: ucx_buffer_seek(url, -1, SEEK_CUR); universe@25: universe@25: sstr_t p = sstr(path); universe@25: ssize_t ntk = 0; universe@25: sstr_t *tks = sstrsplit(p, S("/"), &ntk); universe@25: universe@25: for(int i=0;i 0) { universe@25: char *esc = curl_easy_escape(sn->handle, node.ptr, node.length); universe@25: ucx_buffer_putc(url, '/'); universe@25: ucx_buffer_write(esc, 1, strlen(esc), url); universe@25: curl_free(esc); universe@25: } universe@25: free(node.ptr); universe@25: } universe@25: free(tks); universe@25: if(path[p.length-1] == '/') { universe@25: ucx_buffer_putc(url, '/'); universe@25: } universe@25: ucx_buffer_putc(url, 0); universe@25: universe@25: space = url->space; universe@25: ucx_buffer_free(url); universe@25: universe@25: return space; universe@25: } universe@25: universe@25: char* util_parent_path(char *path) { universe@25: char *name = util_resource_name(path); universe@25: size_t namelen = strlen(name); universe@25: size_t pathlen = strlen(path); universe@25: size_t parentlen = pathlen - namelen; universe@25: char *parent = malloc(parentlen + 1); universe@25: memcpy(parent, path, parentlen); universe@25: parent[parentlen] = '\0'; universe@25: return parent; universe@25: } universe@25: universe@25: universe@25: char* util_xml_get_text(xmlNode *elm) { universe@25: xmlNode *node = elm->children; universe@25: while(node) { universe@25: if(node->type == XML_TEXT_NODE) { universe@25: return (char*)node->content; universe@25: } universe@25: node = node->next; universe@25: } universe@25: return NULL; universe@25: } universe@25: universe@25: universe@25: char* util_base64decode(char *in) { universe@25: int len = 0; universe@25: return util_base64decode_len(in, &len); universe@25: } universe@25: universe@25: char* util_base64decode_len(char* in, int *outlen) { universe@25: size_t len = strlen(in); universe@25: char *out = calloc(1, len); universe@25: universe@25: BIO* b = BIO_new_mem_buf(in, len); universe@25: BIO *d = BIO_new(BIO_f_base64()); universe@25: BIO_set_flags(d, BIO_FLAGS_BASE64_NO_NL); universe@25: b = BIO_push(d, b); universe@25: universe@25: *outlen = BIO_read(b, out, len); universe@25: BIO_free_all(b); universe@25: universe@25: return out; universe@25: } universe@25: universe@25: char* util_base64encode(char *in, size_t len) { universe@25: BIO *b; universe@25: BIO *e; universe@25: BUF_MEM *mem; universe@25: universe@25: e = BIO_new(BIO_f_base64()); universe@25: b = BIO_new(BIO_s_mem()); universe@25: universe@25: e = BIO_push(e, b); universe@25: BIO_write(e, in, len); universe@25: BIO_flush(e); universe@25: universe@25: BIO_get_mem_ptr(e, &mem); universe@25: char *out = malloc(mem->length); universe@25: memcpy(out, mem->data, mem->length -1); universe@25: out[mem->length - 1] = '\0'; universe@25: universe@25: BIO_free_all(e); universe@25: universe@25: return out; universe@25: } universe@25: universe@25: char* util_encrypt_str(DavSession *sn, char *str, char *key) { universe@25: DavKey *k = dav_context_get_key(sn->context, key); universe@25: if(!k) { universe@25: // TODO: session error universe@25: return NULL; universe@25: } universe@25: universe@25: char *enc_str = aes_encrypt(str, k); universe@25: char *ret_str = dav_session_strdup(sn, enc_str); universe@25: free(enc_str); universe@25: return ret_str; universe@25: } universe@25: universe@31: /* commented out for testing reasons */ universe@31: /* universe@25: char* util_decrypt_str(DavSession *sn, char *str, char *key) { universe@25: DavKey *k = dav_context_get_key(sn->context, key); universe@25: if(!k) { universe@25: // TODO: session error universe@25: return NULL; universe@25: } universe@25: universe@25: char *dec_str = aes_decrypt(str, k); universe@25: char *ret_str = dav_session_strdup(sn, dec_str); universe@25: free(dec_str); universe@25: return ret_str; universe@25: } universe@31: */ universe@25: char* util_random_str() { universe@25: unsigned char *str = malloc(25); universe@25: str[24] = '\0'; universe@25: universe@25: sstr_t t = S( universe@25: "01234567890" universe@25: "abcdefghijklmnopqrstuvwxyz" universe@25: "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); universe@25: const unsigned char *table = (const unsigned char*)t.ptr; universe@25: universe@25: RAND_pseudo_bytes(str, 24); universe@25: for(int i=0;i<24;i++) { universe@25: int c = str[i] % t.length; universe@25: str[i] = table[c]; universe@25: } universe@25: universe@25: return (char*)str; universe@25: } universe@25: universe@25: /* universe@25: * gets a substring from 0 to the appearance of the token universe@25: * tokens are separated by space universe@25: * sets sub to the substring and returns the remaining string universe@25: */ universe@25: sstr_t util_getsubstr_until_token(sstr_t str, sstr_t token, sstr_t *sub) { universe@25: int i; universe@25: int token_start = -1; universe@25: int token_end = -1; universe@25: for(i=0;i<=str.length;i++) { universe@25: int c; universe@25: if(i == str.length) { universe@25: c = ' '; universe@25: } else { universe@25: c = str.ptr[i]; universe@25: } universe@25: if(c < 33) { universe@25: if(token_start != -1) { universe@25: token_end = i; universe@25: size_t len = token_end - token_start; universe@25: sstr_t tk = sstrsubsl(str, token_start, len); universe@25: //printf("token: {%.*s}\n", token.length, token.ptr); universe@25: if(!sstrcmp(tk, token)) { universe@25: *sub = sstrtrim(sstrsubsl(str, 0, token_start)); universe@25: break; universe@25: } universe@25: token_start = -1; universe@25: token_end = -1; universe@25: } universe@25: } else { universe@25: if(token_start == -1) { universe@25: token_start = i; universe@25: } universe@25: } universe@25: } universe@25: universe@25: if(i < str.length) { universe@25: return sstrtrim(sstrsubs(str, i)); universe@25: } else { universe@25: str.ptr = NULL; universe@25: str.length = 0; universe@25: return str; universe@25: } universe@25: }