libidav/utils.c

Thu, 03 Oct 2024 18:08:29 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 03 Oct 2024 18:08:29 +0200
changeset 46
31bc86844659
parent 20
db263186edf3
child 49
2f71f4ee247a
permissions
-rw-r--r--

implement action_newfile

1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2019 Olaf Wintermann. All rights reserved.
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <time.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdio.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <stdlib.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <string.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <errno.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <ctype.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include <cx/string.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include <cx/buffer.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include <cx/utils.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include <cx/printf.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #include <libxml/tree.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include <curl/curl.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #ifdef _WIN32
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #include <conio.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #define getpasswordchar() getch()
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #define IS_PATH_SEPARATOR(c) (c == '/' || c == '\\')
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 #define PATH_SEPARATOR '\\'
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 #else
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 #include <unistd.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 #include <spawn.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 #include <sys/wait.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 #include <termios.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 #define getpasswordchar() getchar()
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 #define IS_PATH_SEPARATOR(c) (c == '/')
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 #define PATH_SEPARATOR '/'
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 #endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 #include "webdav.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 #include "utils.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 #include "crypto.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 #include "session.h"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 /*
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 #include <openssl/hmac.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 #include <openssl/evp.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 #include <openssl/bio.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 #include <openssl/buffer.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 #include <openssl/rand.h>
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 static size_t extractval(cxstring str, char *result, char delim) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 size_t n = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 for(size_t i = 0; i < str.length ; i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 if(isdigit(str.ptr[i])) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 result[n++] = str.ptr[i];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 } else if(str.ptr[i] != delim) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 result[n] = '\0';
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 return n;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 static time_t parse_iso8601(char *iso8601str) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 // safety
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 if(!iso8601str) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 // local vars
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 struct tm tparts;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 memset(&tparts, 0, sizeof(struct tm));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 long val;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 char conv[16];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 // work on the trimmed string
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 cxstring date = cx_strtrim(cx_str(iso8601str));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 cxstring time = cx_strchr(date, 'T');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 if(time.length == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 date.length = time.ptr - date.ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 time.ptr++; time.length--;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 cxstring tzinfo;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 if((tzinfo = cx_strchr(time, 'Z')).length > 0 ||
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 (tzinfo = cx_strchr(time, '+')).length > 0 ||
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 (tzinfo = cx_strchr(time, '-')).length > 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 time.length = tzinfo.ptr - time.ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 // parse date
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 if((date.length != 8 && date.length != 10)
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 || extractval(date, conv , '-') != 8) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 val = atol(conv);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 if(val < 19000000L) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 tparts.tm_mday = val % 100;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 tparts.tm_mon = (val % 10000) / 100 - 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 tparts.tm_year = val / 10000 - 1900;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 // parse time and skip possible fractional seconds
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 cxstring frac;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 if((frac = cx_strchr(time, '.')).length > 0 ||
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 (frac = cx_strchr(time, ',')).length > 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 time.length = frac.ptr - time.ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 if((time.length != 6 && time.length != 8)
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 || extractval(time, conv , ':') != 6) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 val = atol(conv);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 tparts.tm_sec = val % 100;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 tparts.tm_min = (val % 10000) / 100;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 tparts.tm_hour = val / 10000;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 // parse time zone (if any)
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 if(tzinfo.length == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 // local time
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 tparts.tm_isdst = -1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 return mktime(&tparts);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 } else if(!cx_strcmp(tzinfo, cx_str("Z"))) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 #if defined(__FreeBSD__)
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 return timegm(&tparts);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 #elif defined(_WIN32)
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 return _mkgmtime(&tparts);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 #else
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 return mktime(&tparts) - timezone;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 #endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 } else if(tzinfo.ptr[0] == '+' || tzinfo.ptr[0] == '-') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 int sign = (tzinfo.ptr[0] == '+') ? -1 : 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 if(tzinfo.length > 6) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 tzinfo.ptr++; tzinfo.length--;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 extractval(tzinfo, conv, ':');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 val = atol(conv);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 val = 60 * (val / 100) + (val % 100);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 #if defined(__FreeBSD__)
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 return timegm(&tparts) + (time_t) (60 * val * sign);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 #elif defined(_WIN32)
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 return _mkgmtime(&tparts) + (time_t)(60 * val * sign);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 #else
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 return mktime(&tparts) - timezone + (time_t) (60 * val * sign);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 #endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 time_t util_parse_creationdate(char *str) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 // parse a ISO-8601 date (rfc-3339)
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 // example: 2012-11-29T21:35:35Z
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 if(!str) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 return parse_iso8601(str);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 time_t util_parse_lastmodified(char *str) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 // parse a rfc-1123 date
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 // example: Thu, 29 Nov 2012 21:35:35 GMT
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 if(!str) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 time_t result = curl_getdate(str, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 if(result == -1) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 // fall back to the ISO-8601 format (e.g. Microsoft Sharepoint
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 // illegally uses this format for lastmodified, but also some
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 // users might want to give an ISO-8601 date)
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 return util_parse_creationdate(str);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 return result;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 int util_getboolean(const char *v) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 if(v[0] == 'T' || v[0] == 't') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 int util_strtouint(const char *str, uint64_t *value) {
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
216 if (str == NULL || *str == '\0') return 0;
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 char *end;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 errno = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 uint64_t val = strtoull(str, &end, 0);
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
220 if(errno == 0 && *end == '\0') {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 *value = val;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 int util_strtoint(const char *str, int64_t *value) {
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
229 if (str == NULL || *str == '\0') return 0;
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 char *end;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 errno = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 int64_t val = strtoll(str, &end, 0);
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
233 if(errno == 0 && *end == '\0') {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 *value = val;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 int util_szstrtouint(const char *str, uint64_t *value) {
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
242 if (str == NULL || *str == '\0') return 0;
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 char *end;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 errno = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 size_t len = strlen(str);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 uint64_t val = strtoull(str, &end, 0);
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
247 if(errno != 0) {
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
248 return 0;
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
249 } if(end == str+len) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 *value = val;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 } else if(end == str+len-1) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 uint64_t mul = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 switch(end[0]) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 case 'k':
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 case 'K': mul = 1024; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 case 'm':
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 case 'M': mul = 1024*1024; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 case 'g':
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 case 'G': mul = 1024*1024*1024; break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 default: return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 uint64_t result = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 if(util_uint_mul(val, mul, &result)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 *value = result;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 int util_uint_mul(uint64_t a, uint64_t b, uint64_t *result) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 if(a == 0 || b == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 *result = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 uint64_t r = a * b;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 if(r / b == a) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 *result = r;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 *result = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
289 cxstring util_url_base_s(cxstring url) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290 size_t i = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 if(url.length > 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 int slmax;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 if(cx_strprefix(url, cx_str("http://"))) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 slmax = 3;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 } else if(cx_strprefix(url, cx_str("https://"))) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 slmax = 3;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 slmax = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 int slashcount = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 for(i=0;i<url.length;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 if(url.ptr[i] == '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 slashcount++;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 if(slashcount == slmax) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 i++;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 }
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
311 return cx_strsubsl(url, 0, i);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
314 char* util_url_base(const char *url) {
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
315 return cx_strdup(util_url_base_s(cx_str(url))).ptr;
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318 #ifdef _WIN32
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 #define strncasecmp _strnicmp
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 #endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 const char* util_url_path(const char *url) {
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
323 return util_url_path_s(cx_str(url)).ptr;
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
324 }
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
325
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
326 cxstring util_url_path_s(cxstring url) {
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
327 cxstring path = { "", 0 };
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 int slashcount = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 int slmax;
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
330 if(url.length > 7 && !strncasecmp(url.ptr, "http://", 7)) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 slmax = 3;
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
332 } else if(url.length > 8 && !strncasecmp(url.ptr, "https://", 8)) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 slmax = 3;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 slmax = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 char c;
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
338 for(int i=0;i<url.length;i++) {
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
339 c = url.ptr[i];
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 if(c == '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 slashcount++;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 if(slashcount == slmax) {
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
343 path = cx_strsubs(url, i);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 return path;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351 char* util_url_decode(DavSession *sn, const char *url) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 char *unesc = curl_easy_unescape(sn->handle, url, strlen(url), NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 char *ret = strdup(unesc);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 curl_free(unesc);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355 return ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 static size_t util_header_callback(char *buffer, size_t size,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 size_t nitems, void *data) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 cxstring sbuffer = cx_strn(buffer, size*nitems);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 CxMap *map = (CxMap*) data;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365 // if we get a status line, clear the map and exit
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366 if(cx_strprefix(sbuffer, cx_str("HTTP/"))) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
367 // TODO: use new map destructor ucx_map_free_content(map, free);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368 cxMapClear(map);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 return size*nitems;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 // if we get the terminating CRLF, just exit
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373 if(!cx_strcmp(sbuffer, cx_str("\r\n"))) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 return 2;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 cxstring key = sbuffer;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 cxstring value = cx_strchr(sbuffer, ':');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 if(value.length == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 return 0; // invalid header line
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 key.length = value.ptr - key.ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 value.ptr++; value.length--;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387 cxmutstr key_cp = cx_strdup(cx_strtrim(key));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 cx_strlower(key_cp);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 cxmutstr value_cp = cx_strdup(cx_strtrim(value));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 cxMapPut(map, cx_hash_key(key_cp.ptr, key_cp.length), value_cp.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 free(key_cp.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 return sbuffer.length;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 int util_path_isrelated(const char *path1, const char *path2) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 cxstring p1 = cx_str(path1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 cxstring p2 = cx_str(path2);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402 if(IS_PATH_SEPARATOR(p1.ptr[p1.length-1])) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 p1.length--;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 if(IS_PATH_SEPARATOR(p2.ptr[p2.length-1])) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406 p2.length--;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409 if(p2.length < p1.length) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413 if(!cx_strcmp(p1, p2)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 if(cx_strprefix(p2, p1)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418 if(IS_PATH_SEPARATOR(p2.ptr[p1.length])) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
423 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
424 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426 #ifdef _WIN32
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 int util_path_isabsolut(const char *path) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428 if(strlen(path) < 3) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
429 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
430 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
431
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
432 // check if first char is A-Z or a-z
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433 char c = path[0];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434 if(!((c >= 65 && c <= 90) || (c >= 97 && c <= 122))) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
435 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
436 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
437
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438 if(path[1] == ':' && path[2] == '\\') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
441 return 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
442 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
443 #else
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
444 int util_path_isabsolut(const char *path) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 return path[0] == '/';
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
446 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
447 #endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
448
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
449 char* util_path_normalize(const char *path) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
450 size_t len = strlen(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
451 CxBuffer buf;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
452 cxBufferInit(&buf, NULL, len+1, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
453
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
454 if(path[0] == '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 cxBufferPut(&buf, '/');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
457
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
458 int add_separator = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
459 int seg_start = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
460 for(int i=0;i<=len;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
461 char c = path[i];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
462 if(IS_PATH_SEPARATOR(c) || c == '\0') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
463 const char *seg_ptr = path+seg_start;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
464 int seg_len = i - seg_start;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
465 if(IS_PATH_SEPARATOR(seg_ptr[0])) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
466 seg_ptr++;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
467 seg_len--;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
468 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
469
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470 if(seg_len > 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
471 cxstring seg = cx_strn(seg_ptr, seg_len);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
472 if(!cx_strcmp(seg, CX_STR(".."))) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
473 for(int j=buf.pos;j>=0;j--) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
474 char t = j < buf.pos ? buf.space[j] : 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
475 if(IS_PATH_SEPARATOR(t) || j == 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
476 buf.pos = j;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
477 buf.size = j;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
478 buf.space[j] = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
479 add_separator = IS_PATH_SEPARATOR(t) ? 1 : 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
480 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
481 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
482 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
483 } else if(!cx_strcmp(seg, CX_STR("."))) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
484 // ignore
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
485 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
486 if(add_separator) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
487 cxBufferPut(&buf, PATH_SEPARATOR);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
488 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
489 cxBufferWrite(seg_ptr, 1, seg_len, &buf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
490 add_separator = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
491 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
492 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
493
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
494 seg_start = i;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
495 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
496 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
497
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
498 cxBufferPut(&buf, 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
499
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
500 return buf.space;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
501 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
502
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
503 static char* create_relative_path(const char *abspath, const char *base) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
504 size_t path_len = strlen(abspath);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
505 size_t base_len = strlen(base);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
506
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
507 if(IS_PATH_SEPARATOR(abspath[path_len-1])) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
508 path_len--;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
509 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
510 if(IS_PATH_SEPARATOR(base[base_len-1])) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
511 base_len--;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
512 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
513 // get base parent
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
514 for(int i=base_len-1;i>=0;i--) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
515 if(IS_PATH_SEPARATOR(base[i])) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
516 base_len = i+1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
517 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
518 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
519 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
520
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
521 size_t max = path_len > base_len ? base_len : path_len;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
522
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
523 // get prefix of abspath and base
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
524 // this dir is the root of the link
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
525 size_t i;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
526 size_t last_dir = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
527 for(i=0;i<max;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
528 char c = abspath[i];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
529 if(c != base[i]) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
530 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
531 } else if(IS_PATH_SEPARATOR(c)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
532 last_dir = i;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
533 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
534 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
535
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
536 char *ret = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
537 CxBuffer out;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
538 if(last_dir+1 < base_len) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
539 // base is deeper than the link root, we have to go backwards
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
540 int dircount = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
541 for(int i=last_dir+1;i<base_len;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
542 if(IS_PATH_SEPARATOR(base[i])) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
543 dircount++;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
544 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
545 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
546
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
547 cxBufferInit(&out, NULL, dircount*3+path_len-last_dir, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
548
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
549 for(int i=0;i<dircount;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
550 cxBufferPutString(&out, "../");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
551 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
552 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
553 cxBufferInit(&out, NULL, path_len - last_dir, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
554 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
555
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
556 cxBufferPutString(&out, abspath + last_dir + 1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
557 cxBufferPut(&out, 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
558
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
559 return out.space;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
560 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
561
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
562 #ifdef _WIN32
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
563 char* util_create_relative_path(const char *abspath, const char *base) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
564 char *abspath_converted = strdup(abspath);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
565 char *base_converted = strdup(base);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
566 size_t abs_len = strlen(abspath_converted);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
567 size_t base_len = strlen(base_converted);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
568
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
569 for(int i=0;i<abs_len;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
570 if(abspath_converted[i] == '\\') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
571 abspath_converted[i] = '/';
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
572 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
573 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
574 for(int i=0;i<base_len;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
575 if(base_converted[i] == '\\') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
576 base_converted[i] = '/';
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
577 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
578 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
579
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
580 char *ret = create_relative_path(abspath_converted, base_converted);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
581 free(abspath_converted);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
582 free(base_converted);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
583 return ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
584 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
585 #else
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
586 char* util_create_relative_path(const char *abspath, const char *base) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
587 return create_relative_path(abspath, base);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
588 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
589 #endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
590
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
591
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
592 void util_capture_header(CURL *handle, CxMap* map) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
593 if(map) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
594 curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, util_header_callback);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
595 curl_easy_setopt(handle, CURLOPT_HEADERDATA, map);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
596 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
597 curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
598 curl_easy_setopt(handle, CURLOPT_HEADERDATA, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
599 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
600 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
601
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
602 const char* util_resource_name(const char *url) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
603 cxstring urlstr = cx_str(url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
604 if(urlstr.ptr[urlstr.length-1] == '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
605 urlstr.length--;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
606 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
607 cxstring resname = cx_strrchr(urlstr, '/');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
608 if(resname.length > 1) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
609 return resname.ptr+1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
610 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
611 return url;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
612 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
613 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
614
18
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
615 const char* util_resource_name_c(const char *url, char pathseparator) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
616 cxstring urlstr = cx_str(url);
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
617 if(urlstr.ptr[urlstr.length-1] == pathseparator) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
618 urlstr.length--;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
619 }
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
620 cxstring resname = cx_strrchr(urlstr, pathseparator);
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
621 if(resname.length > 1) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
622 return resname.ptr+1;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
623 } else {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
624 return url;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
625 }
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
626 }
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
627
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
628 const char* util_path_file_name(const char *url) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
629 #ifdef _WIN32
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
630 return util_resource_name_c(url, '\\');
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
631 #else
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
632 return util_resource_name_c(url, '/');
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
633 #endif
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
634 }
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
635
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
636
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
637 int util_mkdir(char *path, mode_t mode) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
638 #ifdef _WIN32
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
639 return mkdir(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
640 #else
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
641 return mkdir(path, mode);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
642 #endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
643 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
644
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
645 char* util_concat_path(const char *url_base, const char *p) {
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
646 cxstring base = cx_str(url_base);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
647 cxstring path;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
648 if(p) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
649 path = cx_str((char*)p);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
650 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
651 path = CX_STR("");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
652 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
653
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
654 return util_concat_path_s(base, path).ptr;
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
655 }
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
656
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
657 cxmutstr util_concat_path_s(cxstring base, cxstring path) {
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
658 if(!path.ptr) {
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
659 path = CX_STR("");
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
660 }
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
661
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
662 int add_separator = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
663 if(base.length != 0 && base.ptr[base.length-1] == '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
664 if(path.ptr[0] == '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
665 base.length--;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
666 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
667 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
668 if(path.length == 0 || path.ptr[0] != '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
669 add_separator = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
670 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
671 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
672
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
673 cxmutstr url;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
674 if(add_separator) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
675 url = cx_strcat(3, base, CX_STR("/"), path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
676 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
677 url = cx_strcat(2, base, path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
678 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
679
2
fbdfaacc4182 update ucx, libidav and add first gui code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 1
diff changeset
680 return url;
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
681 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
682
18
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
683 cxmutstr util_concat_path_ext(cxstring base, cxstring path, char separator) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
684 if(!path.ptr) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
685 path = CX_STR("");
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
686 }
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
687
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
688 int add_separator = 0;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
689 if(base.length != 0 && base.ptr[base.length-1] == separator) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
690 if(path.ptr[0] == separator) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
691 base.length--;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
692 }
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
693 } else {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
694 if(path.length == 0 || path.ptr[0] != separator) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
695 add_separator = 1;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
696 }
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
697 }
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
698
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
699 cxmutstr url;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
700 if(add_separator) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
701 url = cx_strcat(3, base, cx_strn(&separator, 1), path);
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
702 } else {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
703 url = cx_strcat(2, base, path);
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
704 }
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
705
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
706 return url;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
707 }
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
708
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
709 cxmutstr util_concat_sys_path(cxstring base, cxstring path) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
710 #ifdef _WIN32
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
711 return util_concat_path_ext(base, path, '\\');
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
712 #else
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
713 return util_concat_path_ext(base, path, '/');
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
714 #endif
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
715 }
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
716
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
717 char* util_get_url(DavSession *sn, const char *href) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
718 cxstring base = cx_str(sn->base_url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
719 cxstring href_str = cx_str(href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
720
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
721 const char *base_path = util_url_path(sn->base_url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
722 base.length -= strlen(base_path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
723
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
724 cxmutstr url = cx_strcat(2, base, href_str);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
725 return url.ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
726 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
727
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
728 void util_set_url(DavSession *sn, const char *href) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
729 char *url = util_get_url(sn, href);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
730 curl_easy_setopt(sn->handle, CURLOPT_URL, url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
731 free(url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
732 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
733
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
734 char* util_path_to_url(DavSession *sn, const char *path) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
735 CxBuffer url;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
736 cxBufferInit(&url, NULL, 256, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
737
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
738 // add base url
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
739 cxBufferWrite(sn->base_url, 1, strlen(sn->base_url), &url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
740 // remove trailing slash
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
741 cxBufferSeek(&url, -1, SEEK_CUR);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
742
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
743 cxstring p = cx_str(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
744
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
745 CxStrtokCtx tkctx = cx_strtok(p, CX_STR("/"), INT_MAX);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
746 cxstring node;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
747 while(cx_strtok_next(&tkctx, &node)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
748 if(node.length > 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
749 char *esc = curl_easy_escape(sn->handle, node.ptr, node.length);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
750 cxBufferPut(&url, '/');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
751 cxBufferWrite(esc, 1, strlen(esc), &url);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
752 curl_free(esc);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
753 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
754 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
755
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
756 if(path[p.length-1] == '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
757 cxBufferPut(&url, '/');
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
758 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
759 cxBufferPut(&url, 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
760
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
761 return url.space;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
762 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
763
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
764 char* util_parent_path(const char *path) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
765 const char *name = util_resource_name(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
766 size_t namelen = strlen(name);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
767 size_t pathlen = strlen(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
768 size_t parentlen = pathlen - namelen;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
769 char *parent = malloc(parentlen + 1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
770 memcpy(parent, path, parentlen);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
771 parent[parentlen] = '\0';
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
772 return parent;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
773 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
774
18
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
775 char* util_sys_parent_path(const char *path) {
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
776 const char *name = util_path_file_name(path);
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
777 size_t namelen = strlen(name);
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
778 size_t pathlen = strlen(path);
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
779 size_t parentlen = pathlen - namelen;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
780 char *parent = malloc(parentlen + 1);
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
781 memcpy(parent, path, parentlen);
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
782 parent[parentlen] = '\0';
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
783 return parent;
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
784 }
af411868ab9b implement dnd upload (without progressbar)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 2
diff changeset
785
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
786 char* util_size_str(DavBool iscollection, uint64_t contentlength) {
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
787 return util_size_str2(iscollection, contentlength, contentlength, 1);
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
788 }
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
789
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
790 char* util_size_str2(DavBool iscollection, uint64_t contentlength, uint64_t dimension, int precision) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
791 char *str = malloc(16);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
792 uint64_t size = contentlength;
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
793
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
794 if(iscollection) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
795 str[0] = '\0'; // currently no information for collections
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
796 } else if(dimension < 0x400) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
797 snprintf(str, 16, "%" PRIu64 " bytes", size);
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
798 } else if(dimension < 0x100000) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
799 float s = (float)size/0x400;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
800 int diff = (s*100 - (int)s*100);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
801 if(diff > 90) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
802 diff = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
803 s += 0.10f;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
804 }
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
805 if(dimension < 0x2800 && diff != 0) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
806 // size < 10 KiB
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
807 snprintf(str, 16, "%.*f KiB", precision, s);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
808 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
809 snprintf(str, 16, "%.0f KiB", s);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
810 }
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
811 } else if(dimension < 0x40000000) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
812 float s = (float)size/0x100000;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
813 int diff = (s*100 - (int)s*100);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
814 if(diff > 90) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
815 diff = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
816 s += 0.10f;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
817 }
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
818 if(dimension < 0xa00000 && diff != 0) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
819 // size < 10 MiB
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
820 snprintf(str, 16, "%.*f MiB", precision, s);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
821 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
822 size /= 0x100000;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
823 snprintf(str, 16, "%.0f MiB", s);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
824 }
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
825 } else if(dimension < 0x1000000000ULL) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
826 float s = (float)size/0x40000000;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
827 int diff = (s*100 - (int)s*100);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
828 if(diff > 90) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
829 diff = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
830 s += 0.10f;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
831 }
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
832 if(dimension < 0x280000000 && diff != 0) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
833 // size < 10 GiB
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
834 snprintf(str, 16, "%.*f GiB", precision, s);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
835 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
836 size /= 0x40000000;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
837 snprintf(str, 16, "%.0f GiB", s);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
838 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
839 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
840 size /= 1024;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
841 float s = (float)size/0x40000000;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
842 int diff = (s*100 - (int)s*100);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
843 if(diff > 90) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
844 diff = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
845 s += 0.10f;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
846 }
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
847 if(dimension < 0x280000000 && diff != 0) {
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
848 // size < 10 TiB
20
db263186edf3 improve upload progress window
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 18
diff changeset
849 snprintf(str, 16, "%.*f TiB", precision, s);
1
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
850 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
851 size /= 0x40000000;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
852 snprintf(str, 16, "%.0f TiB", s);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
853 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
854 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
855 return str;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
856 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
857
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
858 char* util_date_str(time_t tm) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
859 struct tm t;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
860 struct tm n;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
861 time_t now = time(NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
862 #ifdef _WIN32
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
863 memcpy(&t, localtime(&tm), sizeof(struct tm));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
864 memcpy(&n, localtime(&now), sizeof(struct tm));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
865 #else
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
866 localtime_r(&tm, &t);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
867 localtime_r(&now, &n);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
868 #endif /* _WIN32 */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
869 char *str = malloc(16);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
870 if(t.tm_year == n.tm_year) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
871 strftime(str, 16, "%b %d %H:%M", &t);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
872 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
873 strftime(str, 16, "%b %d %Y", &t);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
874 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
875 return str;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
876 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
877
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
878
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
879 char* util_xml_get_text(const xmlNode *elm) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
880 xmlNode *node = elm->children;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
881 while(node) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
882 if(node->type == XML_TEXT_NODE) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
883 return (char*)node->content;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
884 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
885 node = node->next;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
886 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
887 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
888 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
889
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
890
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
891 char* util_base64decode(const char *in) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
892 int len = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
893 return util_base64decode_len(in, &len);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
894 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
895
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
896 #define WHITESPACE 64
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
897 #define EQUALS 65
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
898 #define INVALID 66
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
899 static char b64dectable[] = {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
900 66,66,66,66,66,66,66,66,66,66,64,66,66,66,66,66,66,66,66,66,66,66,66,66,66,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
901 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,62,66,66,66,63,52,53,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
902 54,55,56,57,58,59,60,61,66,66,66,65,66,66,66, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
903 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,66,66,66,66,66,66,26,27,28,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
904 29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,66,66,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
905 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
906 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
907 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
908 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
909 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
910 66,66,66,66,66,66
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
911 };
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
912 char* util_base64decode_len(const char* in, int *outlen) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
913 /* code is mostly from wikibooks */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
914
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
915 if(!in) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
916 *outlen = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
917 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
918 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
919
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
920 size_t inlen = strlen(in);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
921 size_t bufsize = (inlen*3) / 4;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
922 char *outbuf = malloc(bufsize+1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
923 *outlen = -1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
924
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
925 unsigned char *out = (unsigned char*)outbuf;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
926
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
927 const char *end = in + inlen;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
928 char iter = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
929 uint32_t buf = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
930 size_t len = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
931
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
932 while (in < end) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
933 unsigned char c = b64dectable[*in++];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
934
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
935 switch (c) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
936 case WHITESPACE: continue; /* skip whitespace */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
937 case INVALID: {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
938 /* invalid input */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
939 outbuf[0] = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
940 return outbuf;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
941 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
942 case EQUALS: {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
943 /* pad character, end of data */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
944 in = end;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
945 continue;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
946 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
947 default: {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
948 buf = buf << 6 | c;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
949 iter++; // increment the number of iteration
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
950 /* If the buffer is full, split it into bytes */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
951 if (iter == 4) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
952 if ((len += 3) > bufsize) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
953 /* buffer overflow */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
954 outbuf[0] = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
955 return outbuf;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
956 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
957 *(out++) = (buf >> 16) & 255;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
958 *(out++) = (buf >> 8) & 255;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
959 *(out++) = buf & 255;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
960 buf = 0; iter = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
961
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
962 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
963 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
964 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
965 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
966
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
967 if (iter == 3) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
968 if ((len += 2) > bufsize) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
969 /* buffer overflow */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
970 outbuf[0] = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
971 return outbuf;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
972 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
973 *(out++) = (buf >> 10) & 255;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
974 *(out++) = (buf >> 2) & 255;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
975 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
976 else if (iter == 2) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
977 if (++len > bufsize) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
978 /* buffer overflow */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
979 outbuf[0] = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
980 return outbuf;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
981 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
982 *(out++) = (buf >> 4) & 255;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
983 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
984
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
985 *outlen = len; /* modify to reflect the actual output size */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
986 outbuf[len] = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
987 return outbuf;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
988 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
989
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
990
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
991 static char* b64enctable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
992 char* util_base64encode(const char *in, size_t len) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
993 // calculate length of base64 output and create buffer
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
994 size_t outlen = 4 * ((len + 2) / 3);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
995 int pad = len % 3;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
996
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
997 char *out = malloc(outlen + 1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
998 out[outlen] = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
999 size_t pos = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1000
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1001 // encode blocks of 3 bytes
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1002 size_t i;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1003 size_t blockend = len - pad;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1004 for(i=0;i<blockend;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1005 unsigned char b1 = in[i++];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1006 unsigned char b2 = in[i++];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1007 unsigned char b3 = in[i];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1008 uint32_t inb = b1 << 16 | (b2 << 8) | b3;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1009 out[pos++] = b64enctable[(inb >> 18) & 63];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1010 out[pos++] = b64enctable[(inb >> 12) & 63];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1011 out[pos++] = b64enctable[(inb >> 6) & 63];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1012 out[pos++] = b64enctable[(inb) & 63];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1013 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1014
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1015 // encode last bytes
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1016 if(pad > 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1017 char p[3] = {0, 0, 0};
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1018 for(int j=0;i<len;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1019 p[j++] = in[i];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1020 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1021 unsigned char b1 = p[0];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1022 unsigned char b2 = p[1];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1023 unsigned char b3 = p[2];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1024 uint32_t inb = (b1 << 16) | (b2 << 8) | b3;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1025 out[pos++] = b64enctable[(inb >> 18) & 63];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1026 out[pos++] = b64enctable[(inb >> 12) & 63];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1027 out[pos++] = b64enctable[(inb >> 6) & 63];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1028 out[pos++] = b64enctable[(inb) & 63];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1029 for(int k=outlen-1;k>=outlen-(3-pad);k--) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1030 out[k] = '=';
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1031 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1032 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1033
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1034 return out;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1035 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1036
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1037 char* util_encrypt_str(DavSession *sn, const char *str, const char *key) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1038 DavKey *k = dav_context_get_key(sn->context, key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1039 if(!k) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1040 sn->error = DAV_ERROR;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1041 cxmutstr err = cx_asprintf("Key %s not found", key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1042 dav_session_set_errstr(sn, err.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1043 free(err.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1044 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1045 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1046
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1047 return util_encrypt_str_k(sn, str, k);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1048 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1049
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1050 char* util_encrypt_str_k(DavSession *sn, const char *str, DavKey *key) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1051 char *enc_str = aes_encrypt(str, strlen(str), key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1052 char *ret_str = dav_session_strdup(sn, enc_str);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1053 free(enc_str);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1054 return ret_str;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1055 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1056
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1057 char* util_decrypt_str(DavSession *sn, const char *str, const char *key) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1058 DavKey *k = dav_context_get_key(sn->context, key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1059 if(!k) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1060 sn->error = DAV_ERROR;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1061 cxmutstr err = cx_asprintf("Key %s not found", key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1062 dav_session_set_errstr(sn, err.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1063 free(err.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1064 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1065 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1066
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1067 return util_decrypt_str_k(sn, str, k);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1068 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1069
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1070 char* util_decrypt_str_k(DavSession *sn, const char *str, DavKey *key) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1071 size_t len = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1072 char *dec_str = aes_decrypt(str, &len, key);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1073 char *ret_str = dav_session_strdup(sn, dec_str);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1074 free(dec_str);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1075 return ret_str;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1076 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1077
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1078 char* util_random_str() {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1079 unsigned char *str = malloc(25);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1080 str[24] = '\0';
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1081
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1082 cxstring t = CX_STR(
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1083 "01234567890"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1084 "abcdefghijklmnopqrstuvwxyz"
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1085 "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1086 const unsigned char *table = (const unsigned char*)t.ptr;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1087
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1088 #ifdef DAV_USE_OPENSSL
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1089 RAND_bytes(str, 24);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1090 #else
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1091 dav_rand_bytes(str, 24);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1092 #endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1093 for(int i=0;i<24;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1094 int c = str[i] % t.length;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1095 str[i] = table[c];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1096 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1097
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1098 return (char*)str;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1099 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1100
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1101 /*
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1102 * gets a substring from 0 to the appearance of the token
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1103 * tokens are separated by space
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1104 * sets sub to the substring and returns the remaining string
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1105 */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1106 // TODO: remove if it isn't used
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1107 /*
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1108 sstr_t util_getsubstr_until_token(sstr_t str, sstr_t token, sstr_t *sub) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1109 int i;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1110 int token_start = -1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1111 int token_end = -1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1112 for(i=0;i<=str.length;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1113 int c;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1114 if(i == str.length) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1115 c = ' ';
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1116 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1117 c = str.ptr[i];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1118 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1119 if(c < 33) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1120 if(token_start != -1) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1121 token_end = i;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1122 size_t len = token_end - token_start;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1123 sstr_t tk = sstrsubsl(str, token_start, len);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1124 //printf("token: {%.*s}\n", token.length, token.ptr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1125 if(!sstrcmp(tk, token)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1126 *sub = sstrtrim(sstrsubsl(str, 0, token_start));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1127 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1128 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1129 token_start = -1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1130 token_end = -1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1131 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1132 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1133 if(token_start == -1) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1134 token_start = i;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1135 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1136 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1137 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1138
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1139 if(i < str.length) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1140 return sstrtrim(sstrsubs(str, i));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1141 } else {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1142 str.ptr = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1143 str.length = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1144 return str;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1145 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1146 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1147 */
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1148
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1149 cxmutstr util_readline(FILE *stream) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1150 CxBuffer buf;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1151 cxBufferInit(&buf, NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1152
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1153 int c;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1154 while((c = fgetc(stream)) != EOF) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1155 if(c == '\n') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1156 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1157 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1158 cxBufferPut(&buf, c);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1159 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1160
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1161 cxmutstr str = cx_strdup(cx_strtrim(cx_strn(buf.space, buf.size)));
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1162 cxBufferDestroy(&buf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1163 return str;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1164 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1165
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1166 char* util_password_input(char *prompt) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1167 fprintf(stderr, "%s", prompt);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1168 fflush(stderr);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1169
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1170 #ifndef _WIN32
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1171 // hide terminal input
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1172 struct termios oflags, nflags;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1173 if(isatty(fileno(stdin))) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1174 tcgetattr(fileno(stdin), &oflags);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1175 nflags = oflags;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1176 nflags.c_lflag &= ~ECHO;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1177 nflags.c_lflag |= ECHONL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1178 if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1179 perror("tcsetattr");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1180 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1181 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1182
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1183 #endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1184
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1185 // read password input
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1186 CxBuffer buf;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1187 cxBufferInit(&buf, NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1188 int c = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1189 while((c = getpasswordchar()) != EOF) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1190 if(c == '\n' || c == '\r') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1191 break;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1192 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1193 cxBufferPut(&buf, c);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1194 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1195 cxBufferPut(&buf, 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1196 fflush(stdin);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1197
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1198 #ifndef _WIN32
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1199 // restore terminal settings
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1200 if (isatty(fileno(stdin)) && tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1201 perror("tcsetattr");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1202 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1203 #endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1204
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1205 return buf.space;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1206 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1207
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1208 int util_exec_command(char *command, CxBuffer *outbuf) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1209 #ifdef _WIN32
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1210 fprintf(stderr, "util_exec_command unsupported\n");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1211 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1212 #else
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1213
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1214 int pout[2];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1215 if(pipe(pout)) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1216 perror("pipe");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1217 return 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1218 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1219
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1220 int ret = 0;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1221
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1222 // close stdin and stderr, use pipe for stdout
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1223 posix_spawn_file_actions_t actions;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1224 posix_spawn_file_actions_init(&actions);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1225 posix_spawn_file_actions_addclose(&actions, 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1226 posix_spawn_file_actions_adddup2(&actions, pout[1], 1);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1227 posix_spawn_file_actions_addclose(&actions, 2);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1228
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1229 char *args[4];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1230 args[0] = "sh";
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1231 args[1] = "-c";
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1232 args[2] = command;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1233 args[3] = NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1234
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1235 pid_t pid; // child pid
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1236 ret = posix_spawn(&pid, "/bin/sh", &actions, NULL, args, NULL);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1237
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1238 close(pout[1]);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1239
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1240 if(!ret) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1241 ssize_t r;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1242 char buf[1024];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1243 while((r = read(pout[0], buf, 1024)) > 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1244 cxBufferWrite(buf, 1, r, outbuf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1245 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1246 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1247
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1248 // wait for child process
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1249 ret = 1;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1250 waitpid(pid, &ret, 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1251
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1252 posix_spawn_file_actions_destroy(&actions);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1253 close(pout[0]);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1254
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1255 return ret;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1256 #endif
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1257 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1258
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1259 char* util_hexstr(const unsigned char *data, size_t len) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1260 size_t buflen = 2*len + 4;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1261 CxBuffer buf;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1262 cxBufferInit(&buf, NULL, buflen + 1, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1263 for(int i=0;i<len;i++) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1264 cx_bprintf(&buf, "%02x", data[i]);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1265 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1266 cxBufferPut(&buf, 0);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1267 return buf.space;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1268 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1269
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1270 void util_remove_trailing_pathseparator(char *path) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1271 size_t len = strlen(path);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1272 if(len < 2) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1273 return;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1274 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1275
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1276 if(path[len-1] == '/') {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1277 path[len-1] = '\0';
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1278 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1279 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1280
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1281 char* util_file_hash(const char *path) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1282 FILE *in = fopen(path, "r");
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1283 if(!in) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1284 return NULL;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1285 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1286
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1287 DAV_SHA_CTX *sha = dav_hash_init();
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1288 char *buf = malloc(16384);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1289
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1290 size_t r;
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1291 while((r = fread(buf, 1, 16384, in)) > 0) {
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1292 dav_hash_update(sha, buf, r);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1293 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1294
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1295 unsigned char hash[DAV_SHA256_DIGEST_LENGTH];
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1296 dav_hash_final(sha, hash);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1297 free(buf);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1298 fclose(in);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1299
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1300 return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH);
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1301 }
b5bb7b3cd597 add libidav code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1302

mercurial