libidav/utils.c

Tue, 12 Dec 2017 17:41:01 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 12 Dec 2017 17:41:01 +0100
branch
v1.1
changeset 341
7954fdacdb5c
parent 254
d7c4ba50b7d8
child 329
2dc61cc5a8ac
permissions
-rw-r--r--

enable va_list fix only on mingw (this is the real release, as usual)

33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
185
cd42cccee550 updated copyright notice
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 184
diff changeset
4 * Copyright 2016 Olaf Wintermann. All rights reserved.
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0bbbb0341606 some code cleanup and bug fixes
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
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <time.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdio.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <stdlib.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <string.h>
124
41939c8f3f9c expression compiler generates byte code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
33 #include <errno.h>
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
34 #include <ctype.h>
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include <ucx/string.h>
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
36 #include <ucx/buffer.h>
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
37 #include <ucx/utils.h>
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include <libxml/tree.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #include <curl/curl.h>
152
ff854d3df20c fixed windows build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 151
diff changeset
40
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
41 #ifdef _WIN32
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
42 #include <conio.h>
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
43 #define getpasswordchar() getch()
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
44 #else
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
45 #include <termios.h>
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
46 #define getpasswordchar() getchar()
152
ff854d3df20c fixed windows build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 151
diff changeset
47 #endif
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 #include <openssl/hmac.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 #include <openssl/evp.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 #include <openssl/bio.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 #include <openssl/buffer.h>
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
53 #include <openssl/rand.h>
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
225
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
55 #include "webdav.h"
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 #include "utils.h"
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
57 #include "crypto.h"
225
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
58 #include "session.h"
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
60 static size_t extractval(sstr_t str, char *result, char delim) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
61 size_t n = 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
62 for(size_t i = 0; i < str.length ; i++) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
63 if(isdigit(str.ptr[i])) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
64 result[n++] = str.ptr[i];
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
65 } else if(str.ptr[i] != delim) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
66 return 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
67 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
68 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
69 result[n] = '\0';
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
70 return n;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
71 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
72
186
05d11fbcaeb3 improved copy/move
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
73 static time_t parse_iso8601(char *iso8601str) {
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
74
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
75 // safety
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
76 if(!iso8601str) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
77 return 0;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
78 }
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
79
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
80 // local vars
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
81 struct tm tparts;
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
82 memset(&tparts, 0, sizeof(struct tm));
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
83 long val;
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
84 char conv[16];
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
85
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
86 // work on the trimmed string
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
87 sstr_t date = sstrtrim(sstr(iso8601str));
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
88
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
89 sstr_t time = sstrchr(date, 'T');
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
90 if(time.length == 0) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
91 return 0;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
92 }
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
93 date.length = time.ptr - date.ptr;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
94 time.ptr++; time.length--;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
95
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
96 sstr_t tzinfo;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
97 if((tzinfo = sstrchr(time, 'Z')).length > 0 ||
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
98 (tzinfo = sstrchr(time, '+')).length > 0 ||
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
99 (tzinfo = sstrchr(time, '-')).length > 0) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
100
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
101 time.length = tzinfo.ptr - time.ptr;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
102 }
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
103
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
104 // parse date
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
105 if((date.length != 8 && date.length != 10)
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
106 || extractval(date, conv , '-') != 8) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
107 return 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
108 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
109 val = atol(conv);
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
110 if(val < 19000000L) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
111 return 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
112 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
113 tparts.tm_mday = val % 100;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
114 tparts.tm_mon = (val % 10000) / 100 - 1;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
115 tparts.tm_year = val / 10000 - 1900;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
116
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
117 // parse time and skip possible fractional seconds
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
118 sstr_t frac;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
119 if((frac = sstrchr(time, '.')).length > 0 ||
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
120 (frac = sstrchr(time, ',')).length > 0) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
121 time.length = frac.ptr - time.ptr;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
122 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
123 if((time.length != 6 && time.length != 8)
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
124 || extractval(time, conv , ':') != 6) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
125 return 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
126 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
127 val = atol(conv);
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
128 tparts.tm_sec = val % 100;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
129 tparts.tm_min = (val % 10000) / 100;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
130 tparts.tm_hour = val / 10000;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
131
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
132
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
133 // parse time zone (if any)
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
134 if(tzinfo.length == 0) {
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
135 // local time
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
136 tparts.tm_isdst = -1;
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
137 return mktime(&tparts);
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
138 } else if(!sstrcmp(tzinfo, S("Z"))) {
184
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
139 #ifdef __FreeBSD__
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
140 return timegm(&tparts);
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
141 #else
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
142 return mktime(&tparts) - timezone;
184
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
143 #endif
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
144 } else if(tzinfo.ptr[0] == '+' || tzinfo.ptr[0] == '-') {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
145 int sign = (tzinfo.ptr[0] == '+') ? -1 : 1;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
146
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
147 if(tzinfo.length > 6) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
148 return 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
149 } else {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
150 tzinfo.ptr++; tzinfo.length--;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
151 extractval(tzinfo, conv, ':');
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
152 val = atol(conv);
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
153 val = 60 * (val / 100) + (val % 100);
184
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
154 #ifdef __FreeBSD__
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
155 return timegm(&tparts) + (time_t) (60 * val * sign);
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
156 #else
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
157 return mktime(&tparts) - timezone + (time_t) (60 * val * sign);
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
158 #endif
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
159 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
160 } else {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
161 return 0;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
162 }
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
163 }
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
164
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 time_t util_parse_creationdate(char *str) {
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
167 // parse a ISO-8601 date (rfc-3339)
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 // example: 2012-11-29T21:35:35Z
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 if(!str) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 }
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
172
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
173 return parse_iso8601(str);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 time_t util_parse_lastmodified(char *str) {
158
33237261321e added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents: 153
diff changeset
177 // parse a rfc-1123 date
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 // example: Thu, 29 Nov 2012 21:35:35 GMT
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 if(!str) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 } else {
158
33237261321e added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents: 153
diff changeset
182 time_t result = curl_getdate(str, NULL);
33237261321e added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents: 153
diff changeset
183 if(result == -1) {
33237261321e added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents: 153
diff changeset
184 // some shit server don't comply with the WebDAV standard and
33237261321e added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents: 153
diff changeset
185 // use ISO-8601 dates for lastmodified (e.g. Microsoft Sharepoint)
33237261321e added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents: 153
diff changeset
186 return util_parse_creationdate(str);
33237261321e added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents: 153
diff changeset
187 } else {
33237261321e added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents: 153
diff changeset
188 return result;
33237261321e added fallback for util_parse_lastmodified, because some servers incorrectly use ISO-8601 dates for lastmodified - TODO: implement the ISO-8601 parsing
Mike Becker <universe@uap-core.de>
parents: 153
diff changeset
189 }
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 int util_getboolean(char *v) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 if(v[0] == 'T' || v[0] == 't') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 int util_strtoint(char *str, int64_t *value) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 char *end;
124
41939c8f3f9c expression compiler generates byte code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
202 errno = 0;
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 int64_t val = strtoll(str, &end, 0);
124
41939c8f3f9c expression compiler generates byte code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
204 if(errno == 0) {
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 *value = val;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211
151
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
212 char* util_url_base(char *url) {
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
213 sstr_t u = sstr(url);
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
214 int len = u.length;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
215 int slashcount = 0;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
216 int slmax;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
217 if(len > 7 && !strncasecmp(url, "http://", 7)) {
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
218 slmax = 3;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
219 } else if(len > 8 && !strncasecmp(url, "https://", 8)) {
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
220 slmax = 3;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
221 } else {
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
222 slmax = 1;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
223 }
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
224 char c;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
225 int i = 0;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
226 for(i=0;i<len;i++) {
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
227 c = url[i];
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
228 if(c == '/') {
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
229 slashcount++;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
230 if(slashcount == slmax) {
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
231 i++;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
232 break;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
233 }
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
234 }
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
235 }
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
236 sstr_t server = sstrsubsl(u, 0, i);
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
237 server = sstrdup(server);
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
238 return server.ptr;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
239 }
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
240
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 char* util_url_path(char *url) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 char *path = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 size_t len = strlen(url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 int slashcount = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 int slmax;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 if(len > 7 && !strncasecmp(url, "http://", 7)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 slmax = 3;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 } else if(len > 8 && !strncasecmp(url, "https://", 8)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 slmax = 3;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 slmax = 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 char c;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 for(int i=0;i<len;i++) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 c = url[i];
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 if(c == '/') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 slashcount++;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 if(slashcount == slmax) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 path = url + i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 break;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 return path;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266
66
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
267 char* util_url_decode(DavSession *sn, char *url) {
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
268 char *unesc = curl_easy_unescape(sn->handle, url, strlen(url), NULL);
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
269 char *ret = strdup(unesc);
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
270 curl_free(unesc);
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
271 return ret;
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
272 }
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
273
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
274 static size_t util_header_callback(char *buffer, size_t size,
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
275 size_t nitems, void *data) {
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
276
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
277 sstr_t sbuffer = sstrn(buffer, size*nitems);
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
278
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
279 UcxMap *map = (UcxMap*) data;
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
280
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
281 // if we get a status line, clear the map and exit
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
282 if(sstrprefix(sbuffer, S("HTTP/"))) {
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
283 ucx_map_free_content(map, free);
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
284 ucx_map_clear(map);
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
285 return size*nitems;
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
286 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
287
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
288 // if we get the terminating CRLF, just exit
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
289 if(!sstrcmp(sbuffer, S("\r\n"))) {
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
290 return 2;
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
291 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
292
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
293 sstr_t key = sbuffer;
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
294 sstr_t value = sstrchr(sbuffer, ':');
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
295
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
296 if(value.length == 0) {
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
297 return 0; // invalid header line
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
298 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
299
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
300 key.length = value.ptr - key.ptr;
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
301 value.ptr++; value.length--;
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
302
174
e7e56c56d126 updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents: 173
diff changeset
303 key = sstrlower(sstrtrim(key));
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
304 value = sstrdup(sstrtrim(value));
174
e7e56c56d126 updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents: 173
diff changeset
305
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
306 ucx_map_sstr_put(map, key, value.ptr);
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
307
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
308 free(key.ptr);
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
309
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
310 return sbuffer.length;
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
311 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
312
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
313 void util_capture_header(CURL *handle, UcxMap* map) {
176
747f3796eddd improved util_capture_header when deactivating the capturing
Mike Becker <universe@uap-core.de>
parents: 174
diff changeset
314 if(map) {
747f3796eddd improved util_capture_header when deactivating the capturing
Mike Becker <universe@uap-core.de>
parents: 174
diff changeset
315 curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, util_header_callback);
747f3796eddd improved util_capture_header when deactivating the capturing
Mike Becker <universe@uap-core.de>
parents: 174
diff changeset
316 curl_easy_setopt(handle, CURLOPT_HEADERDATA, map);
747f3796eddd improved util_capture_header when deactivating the capturing
Mike Becker <universe@uap-core.de>
parents: 174
diff changeset
317 } else {
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
318 curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, NULL);
176
747f3796eddd improved util_capture_header when deactivating the capturing
Mike Becker <universe@uap-core.de>
parents: 174
diff changeset
319 curl_easy_setopt(handle, CURLOPT_HEADERDATA, NULL);
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
320 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
321 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
322
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 char* util_resource_name(char *url) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 int si = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 int osi = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 int i = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 int p = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 char c;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 while((c = url[i]) != 0) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330 if(c == '/') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 osi = si;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 si = i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 p = 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 i++;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 char *name = url + si + p;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 if(name[0] == 0) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 name = url + osi + p;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 if(name[0] == 0) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 return url;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 return name;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 int util_mkdir(char *path, mode_t mode) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 #ifdef _WIN32
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351 return mkdir(path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 #else
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 return mkdir(path, mode);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 #endif
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357 char* util_concat_path(char *url_base, char *p) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 sstr_t base = sstr(url_base);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 sstr_t path;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360 if(p) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 path = sstr(p);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 path = sstrn("", 0);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366 int add_separator = 0;
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
367 if(base.length != 0 && base.ptr[base.length-1] == '/') {
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368 if(path.ptr[0] == '/') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 base.length--;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 if(path.length == 0 || path.ptr[0] != '/') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373 add_separator = 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 sstr_t url;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 if(add_separator) {
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
379 url = sstrcat(3, base, sstr("/"), path);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 } else {
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
381 url = sstrcat(2, base, path);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 return url.ptr;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386
181
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
387 char* util_get_url(DavSession *sn, char *href) {
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
388 sstr_t base = sstr(sn->base_url);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
389 sstr_t href_str = sstr(href);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
390
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
391 char *base_path = util_url_path(sn->base_url);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
392 base.length -= strlen(base_path);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
393
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
394 sstr_t url = sstrcat(2, base, href_str);
181
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
395 return url.ptr;
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
396 }
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
397
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
398 void util_set_url(DavSession *sn, char *href) {
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
399 char *url = util_get_url(sn, href);
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
400 curl_easy_setopt(sn->handle, CURLOPT_URL, url);
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
401 free(url);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
402 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
403
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
404 char* util_path_to_url(DavSession *sn, char *path) {
69
0dbdd7e8c1fc fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
405 char *space = malloc(256);
0dbdd7e8c1fc fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
406 UcxBuffer *url = ucx_buffer_new(space, 256, UCX_BUFFER_AUTOEXTEND);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
407
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
408 // add base url
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
409 ucx_buffer_write(sn->base_url, 1, strlen(sn->base_url), url);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
410 // remove trailing slash
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
411 ucx_buffer_seek(url, -1, SEEK_CUR);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
412
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
413 sstr_t p = sstr(path);
74
da079dc0724c fixed many memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
414 ssize_t ntk = 0;
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
415 sstr_t *tks = sstrsplit(p, S("/"), &ntk);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
416
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
417 for(int i=0;i<ntk;i++) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
418 sstr_t node = tks[i];
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
419 if(node.length > 0) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
420 char *esc = curl_easy_escape(sn->handle, node.ptr, node.length);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
421 ucx_buffer_putc(url, '/');
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
422 ucx_buffer_write(esc, 1, strlen(esc), url);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
423 curl_free(esc);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
424 }
74
da079dc0724c fixed many memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
425 free(node.ptr);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
426 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
427 free(tks);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
428 if(path[p.length-1] == '/') {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
429 ucx_buffer_putc(url, '/');
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
430 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
431 ucx_buffer_putc(url, 0);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
432
69
0dbdd7e8c1fc fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
433 space = url->space;
0dbdd7e8c1fc fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
434 ucx_buffer_free(url);
0dbdd7e8c1fc fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
435
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
436 return space;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
437 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
438
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 char* util_parent_path(char *path) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440 char *name = util_resource_name(path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
441 size_t namelen = strlen(name);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
442 size_t pathlen = strlen(path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
443 size_t parentlen = pathlen - namelen;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
444 char *parent = malloc(parentlen + 1);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 memcpy(parent, path, parentlen);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
446 parent[parentlen] = '\0';
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
447 return parent;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
448 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
449
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
450
254
d7c4ba50b7d8 implements xmlErrorFunc for dav sync + adds line numbers to error reporting in both config checkers
Mike Becker <universe@uap-core.de>
parents: 228
diff changeset
451 char* util_xml_get_text(const xmlNode *elm) {
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
452 xmlNode *node = elm->children;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
453 while(node) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
454 if(node->type == XML_TEXT_NODE) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 return (char*)node->content;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
457 node = node->next;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
458 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
459 return NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
460 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
461
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
462
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
463 char* util_base64decode(char *in) {
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
464 int len = 0;
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
465 return util_base64decode_len(in, &len);
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
466 }
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
467
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
468 char* util_base64decode_len(char* in, int *outlen) {
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
469 size_t len = strlen(in);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470 char *out = calloc(1, len);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
471
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
472 BIO *b = BIO_new_mem_buf(in, len);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
473 BIO *d = BIO_new(BIO_f_base64());
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
474 BIO_set_flags(d, BIO_FLAGS_BASE64_NO_NL);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
475 b = BIO_push(d, b);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
476
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
477 *outlen = BIO_read(b, out, len);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
478 BIO_free_all(b);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
479
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
480 return out;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
481 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
482
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
483 char* util_base64encode(char *in, size_t len) {
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
484 BIO *b;
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
485 BIO *e;
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
486 BUF_MEM *mem;
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
487
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
488 e = BIO_new(BIO_f_base64());
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
489 b = BIO_new(BIO_s_mem());
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
490 BIO_set_flags(e, BIO_FLAGS_BASE64_NO_NL);
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
491
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
492 e = BIO_push(e, b);
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
493 BIO_write(e, in, len);
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
494 BIO_flush(e);
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
495
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
496 BIO_get_mem_ptr(e, &mem);
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
497 char *out = malloc(mem->length + 1);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
498 memcpy(out, mem->data, mem->length);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
499 out[mem->length] = '\0';
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
500
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
501 BIO_free_all(e);
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
502
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
503 return out;
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
504 }
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
505
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
506 char* util_encrypt_str(DavSession *sn, char *str, char *key) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
507 DavKey *k = dav_context_get_key(sn->context, key);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
508 if(!k) {
225
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
509 sn->error = DAV_ERROR;
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
510 sstr_t err = ucx_sprintf("Key %s not found", key);
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
511 dav_session_set_errstr(sn, err.ptr);
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
512 free(err.ptr);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
513 return NULL;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
514 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
515
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
516 return util_encrypt_str_k(sn, str, k);
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
517 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
518
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
519 char* util_encrypt_str_k(DavSession *sn, char *str, DavKey *key) {
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
520 char *enc_str = aes_encrypt(str, strlen(str), key);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
521 char *ret_str = dav_session_strdup(sn, enc_str);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
522 free(enc_str);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
523 return ret_str;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
524 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
525
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
526 char* util_decrypt_str(DavSession *sn, char *str, char *key) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
527 DavKey *k = dav_context_get_key(sn->context, key);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
528 if(!k) {
225
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
529 sn->error = DAV_ERROR;
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
530 sstr_t err = ucx_sprintf("Key %s not found", key);
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
531 dav_session_set_errstr(sn, err.ptr);
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
532 free(err.ptr);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
533 return NULL;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
534 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
535
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
536 return util_decrypt_str_k(sn, str, k);
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
537 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
538
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
539 char* util_decrypt_str_k(DavSession *sn, char *str, DavKey *key) {
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
540 size_t len = 0;
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
541 char *dec_str = aes_decrypt(str, &len, key);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
542 char *ret_str = dav_session_strdup(sn, dec_str);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
543 free(dec_str);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
544 return ret_str;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
545 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
546
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
547 char* util_random_str() {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
548 unsigned char *str = malloc(25);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
549 str[24] = '\0';
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
550
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
551 sstr_t t = S(
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
552 "01234567890"
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
553 "abcdefghijklmnopqrstuvwxyz"
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
554 "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
555 const unsigned char *table = (const unsigned char*)t.ptr;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
556
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
557 RAND_pseudo_bytes(str, 24);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
558 for(int i=0;i<24;i++) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
559 int c = str[i] % t.length;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
560 str[i] = table[c];
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
561 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
562
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
563 return (char*)str;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
564 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
565
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
566 /*
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
567 * gets a substring from 0 to the appearance of the token
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
568 * tokens are separated by space
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
569 * sets sub to the substring and returns the remaining string
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
570 */
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
571 sstr_t util_getsubstr_until_token(sstr_t str, sstr_t token, sstr_t *sub) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
572 int i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
573 int token_start = -1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
574 int token_end = -1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
575 for(i=0;i<=str.length;i++) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
576 int c;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
577 if(i == str.length) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
578 c = ' ';
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
579 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
580 c = str.ptr[i];
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
581 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
582 if(c < 33) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
583 if(token_start != -1) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
584 token_end = i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
585 size_t len = token_end - token_start;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
586 sstr_t tk = sstrsubsl(str, token_start, len);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
587 //printf("token: {%.*s}\n", token.length, token.ptr);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
588 if(!sstrcmp(tk, token)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
589 *sub = sstrtrim(sstrsubsl(str, 0, token_start));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
590 break;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
591 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
592 token_start = -1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
593 token_end = -1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
594 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
595 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
596 if(token_start == -1) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
597 token_start = i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
598 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
599 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
600 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
601
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
602 if(i < str.length) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
603 return sstrtrim(sstrsubs(str, i));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
604 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
605 str.ptr = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
606 str.length = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
607 return str;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
608 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
609 }
119
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
610
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
611 sstr_t util_readline(FILE *stream) {
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
612 UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
613
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
614 int c;
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
615 while((c = fgetc(stream)) != '\n') {
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
616 ucx_buffer_putc(buf, c);
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
617 }
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
618
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
619 sstr_t str = sstrdup(sstrtrim(sstrn(buf->space, buf->size)));
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
620 ucx_buffer_free(buf);
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
621 return str;
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
622 }
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
623
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
624 char* util_password_input(char *prompt) {
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
625 fprintf(stderr, "%s", prompt);
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
626 fflush(stderr);
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
627
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
628 #ifndef _WIN32
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
629 // hide terminal input
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
630 struct termios oflags, nflags;
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
631 tcgetattr(fileno(stdin), &oflags);
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
632 nflags = oflags;
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
633 nflags.c_lflag &= ~ECHO;
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
634 nflags.c_lflag |= ECHONL;
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
635 if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0) {
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
636 perror("tcsetattr");
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
637 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
638 #endif
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
639
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
640 // read password input
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
641 UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
642 int c = 0;
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
643 while((c = getpasswordchar()) != EOF) {
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
644 if(c == '\n' || c == '\r') {
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
645 break;
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
646 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
647 ucx_buffer_putc(buf, c);
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
648 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
649 ucx_buffer_putc(buf, 0);
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
650 fflush(stdin);
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
651
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
652 #ifndef _WIN32
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
653 // restore terminal settings
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
654 if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0) {
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
655 perror("tcsetattr");
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
656 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
657 #endif
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
658
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
659 char *str = buf->space;
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
660 free(buf); // only free the UcxBuffer struct
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
661 return str;
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
662 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
663
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
664
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
665 char* util_hexstr(unsigned char *data, size_t len) {
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
666 size_t buflen = 2*len + 4;
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
667 UcxBuffer *buf = ucx_buffer_new(malloc(buflen), buflen + 1, 0);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
668 for(int i=0;i<len;i++) {
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
669 ucx_bprintf(buf, "%x", data[i]);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
670 }
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
671 ucx_buffer_putc(buf, 0);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
672 char *str = buf->space;
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
673 ucx_buffer_free(buf);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
674 return str;
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
675 }

mercurial