libidav/utils.c

Thu, 21 Mar 2019 10:51:14 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 21 Mar 2019 10:51:14 +0100
changeset 533
5b9f20aa88c2
parent 512
3320a015a3bc
child 534
9a4857d6444e
permissions
-rw-r--r--

adds option to restore previous file versions

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 *
404
5c08b8e14df8 updates copyright notice
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 372
diff changeset
4 * Copyright 2018 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
349
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
49 #include "webdav.h"
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
50 #include "utils.h"
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
51 #include "crypto.h"
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
52 #include "session.h"
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
53
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
54 /*
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 #include <openssl/hmac.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 #include <openssl/evp.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 #include <openssl/bio.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 #include <openssl/buffer.h>
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
59 #include <openssl/rand.h>
349
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
60 */
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
62 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
63 size_t n = 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
64 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
65 if(isdigit(str.ptr[i])) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
66 result[n++] = str.ptr[i];
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
67 } else if(str.ptr[i] != delim) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
68 return 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
69 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
70 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
71 result[n] = '\0';
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
72 return n;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
73 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
74
186
05d11fbcaeb3 improved copy/move
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
75 static time_t parse_iso8601(char *iso8601str) {
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
76
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
77 // safety
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
78 if(!iso8601str) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
79 return 0;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
80 }
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
81
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
82 // local vars
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
83 struct tm tparts;
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
84 memset(&tparts, 0, sizeof(struct tm));
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
85 long val;
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
86 char conv[16];
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
87
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
88 // work on the trimmed string
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
89 sstr_t date = sstrtrim(sstr(iso8601str));
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
90
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
91 sstr_t time = sstrchr(date, 'T');
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
92 if(time.length == 0) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
93 return 0;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
94 }
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
95 date.length = time.ptr - date.ptr;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
96 time.ptr++; time.length--;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
97
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
98 sstr_t tzinfo;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
99 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
100 (tzinfo = sstrchr(time, '+')).length > 0 ||
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
101 (tzinfo = sstrchr(time, '-')).length > 0) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
102
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
103 time.length = tzinfo.ptr - time.ptr;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
104 }
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
105
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
106 // parse date
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
107 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
108 || extractval(date, conv , '-') != 8) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
109 return 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
110 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
111 val = atol(conv);
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
112 if(val < 19000000L) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
113 return 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
114 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
115 tparts.tm_mday = val % 100;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
116 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
117 tparts.tm_year = val / 10000 - 1900;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
118
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
119 // 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
120 sstr_t frac;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
121 if((frac = sstrchr(time, '.')).length > 0 ||
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
122 (frac = sstrchr(time, ',')).length > 0) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
123 time.length = frac.ptr - time.ptr;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
124 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
125 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
126 || extractval(time, conv , ':') != 6) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
127 return 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
128 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
129 val = atol(conv);
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
130 tparts.tm_sec = val % 100;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
131 tparts.tm_min = (val % 10000) / 100;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
132 tparts.tm_hour = val / 10000;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
133
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
134
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
135 // parse time zone (if any)
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
136 if(tzinfo.length == 0) {
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
137 // local time
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
138 tparts.tm_isdst = -1;
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
139 return mktime(&tparts);
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
140 } else if(!sstrcmp(tzinfo, S("Z"))) {
184
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
141 #ifdef __FreeBSD__
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
142 return timegm(&tparts);
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
143 #else
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
144 return mktime(&tparts) - timezone;
184
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
145 #endif
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
146 } 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
147 int sign = (tzinfo.ptr[0] == '+') ? -1 : 1;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
148
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
149 if(tzinfo.length > 6) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
150 return 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
151 } else {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
152 tzinfo.ptr++; tzinfo.length--;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
153 extractval(tzinfo, conv, ':');
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
154 val = atol(conv);
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
155 val = 60 * (val / 100) + (val % 100);
184
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
156 #ifdef __FreeBSD__
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
157 return timegm(&tparts) + (time_t) (60 * val * sign);
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
158 #else
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
159 return mktime(&tparts) - timezone + (time_t) (60 * val * sign);
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
160 #endif
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
161 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
162 } else {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
163 return 0;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
164 }
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
165 }
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
166
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 time_t util_parse_creationdate(char *str) {
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
169 // parse a ISO-8601 date (rfc-3339)
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 // example: 2012-11-29T21:35:35Z
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 if(!str) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 }
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
174
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
175 return parse_iso8601(str);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 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
179 // parse a rfc-1123 date
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 // 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
181 if(!str) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 } 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
184 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
185 if(result == -1) {
329
2dc61cc5a8ac adds error message to dav, when an invalid date format is specified for the -u option
Mike Becker <universe@uap-core.de>
parents: 254
diff changeset
186 // fall back to the ISO-8601 format (e.g. Microsoft Sharepoint
2dc61cc5a8ac adds error message to dav, when an invalid date format is specified for the -u option
Mike Becker <universe@uap-core.de>
parents: 254
diff changeset
187 // illegally uses this format for lastmodified, but also some
2dc61cc5a8ac adds error message to dav, when an invalid date format is specified for the -u option
Mike Becker <universe@uap-core.de>
parents: 254
diff changeset
188 // users might want to give an ISO-8601 date)
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
189 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
190 } 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
191 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
192 }
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 int util_getboolean(char *v) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 if(v[0] == 'T' || v[0] == 't') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 return 1;
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 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202
512
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
203 int util_strtouint(char *str, uint64_t *value) {
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
204 char *end;
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
205 errno = 0;
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
206 uint64_t val = strtoull(str, &end, 0);
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
207 if(errno == 0) {
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
208 *value = val;
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
209 return 1;
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
210 } else {
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
211 return 0;
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
212 }
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
213 }
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
214
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 int util_strtoint(char *str, int64_t *value) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 char *end;
124
41939c8f3f9c expression compiler generates byte code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
217 errno = 0;
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 int64_t val = strtoll(str, &end, 0);
124
41939c8f3f9c expression compiler generates byte code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
219 if(errno == 0) {
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 *value = val;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226
151
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
227 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
228 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
229 int len = u.length;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
230 int slashcount = 0;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
231 int slmax;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
232 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
233 slmax = 3;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
234 } 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
235 slmax = 3;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
236 } else {
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
237 slmax = 1;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
238 }
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
239 char c;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
240 int i = 0;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
241 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
242 c = url[i];
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
243 if(c == '/') {
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
244 slashcount++;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
245 if(slashcount == slmax) {
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
246 i++;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
247 break;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
248 }
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
249 }
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
250 }
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
251 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
252 server = sstrdup(server);
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
253 return server.ptr;
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
254 }
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
255
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 char* util_url_path(char *url) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 char *path = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 size_t len = strlen(url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 int slashcount = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 int slmax;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 if(len > 7 && !strncasecmp(url, "http://", 7)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 slmax = 3;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 } else if(len > 8 && !strncasecmp(url, "https://", 8)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 slmax = 3;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 slmax = 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 char c;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 for(int i=0;i<len;i++) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 c = url[i];
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 if(c == '/') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 slashcount++;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 if(slashcount == slmax) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 path = url + i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 break;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 return path;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281
66
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
282 char* util_url_decode(DavSession *sn, char *url) {
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
283 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
284 char *ret = strdup(unesc);
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
285 curl_free(unesc);
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
286 return ret;
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
287 }
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
288
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
289 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
290 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
291
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
292 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
293
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
294 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
295
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
296 // 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
297 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
298 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
299 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
300 return size*nitems;
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
301 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
302
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
303 // 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
304 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
305 return 2;
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
306 }
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 sstr_t key = sbuffer;
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
309 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
310
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
311 if(value.length == 0) {
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
312 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
313 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
314
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
315 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
316 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
317
174
e7e56c56d126 updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents: 173
diff changeset
318 key = sstrlower(sstrtrim(key));
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
319 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
320
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
321 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
322
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
323 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
324
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
325 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
326 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
327
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
328 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
329 if(map) {
747f3796eddd improved util_capture_header when deactivating the capturing
Mike Becker <universe@uap-core.de>
parents: 174
diff changeset
330 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
331 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
332 } else {
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
333 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
334 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
335 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
336 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
337
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 char* util_resource_name(char *url) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 int si = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 int osi = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 int i = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 int p = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 char c;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 while((c = url[i]) != 0) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 if(c == '/') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 osi = si;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 si = i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 p = 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 i++;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 char *name = url + si + p;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 if(name[0] == 0) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355 name = url + osi + p;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 if(name[0] == 0) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357 return url;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 return name;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 int util_mkdir(char *path, mode_t mode) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365 #ifdef _WIN32
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366 return mkdir(path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
367 #else
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368 return mkdir(path, mode);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 #endif
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
372
2e15ff88a0ab adds tag management commands to dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 349
diff changeset
372 char* util_concat_path(const char *url_base, const char *p) {
2e15ff88a0ab adds tag management commands to dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 349
diff changeset
373 sstr_t base = sstr((char*)url_base);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 sstr_t path;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 if(p) {
372
2e15ff88a0ab adds tag management commands to dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 349
diff changeset
376 path = sstr((char*)p);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 path = sstrn("", 0);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 int add_separator = 0;
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
382 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
383 if(path.ptr[0] == '/') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 base.length--;
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 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387 if(path.length == 0 || path.ptr[0] != '/') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 add_separator = 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 sstr_t url;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 if(add_separator) {
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
394 url = sstrcat(3, base, sstr("/"), path);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 } else {
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
396 url = sstrcat(2, base, path);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 return url.ptr;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
402 char* util_get_url(DavSession *sn, const char *href) {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
403 scstr_t base = scstr(sn->base_url);
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
404 scstr_t href_str = scstr(href);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
405
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
406 char *base_path = util_url_path(sn->base_url);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
407 base.length -= strlen(base_path);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
408
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
409 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
410 return url.ptr;
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
411 }
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
412
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
413 void util_set_url(DavSession *sn, const char *href) {
181
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
414 char *url = util_get_url(sn, href);
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
415 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
416 free(url);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
417 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
418
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
419 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
420 char *space = malloc(256);
0dbdd7e8c1fc fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
421 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
422
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
423 // add base url
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
424 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
425 // remove trailing slash
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
426 ucx_buffer_seek(url, -1, SEEK_CUR);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
427
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
428 sstr_t p = sstr(path);
74
da079dc0724c fixed many memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
429 ssize_t ntk = 0;
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
430 sstr_t *tks = sstrsplit(p, S("/"), &ntk);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
431
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
432 for(int i=0;i<ntk;i++) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
433 sstr_t node = tks[i];
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
434 if(node.length > 0) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
435 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
436 ucx_buffer_putc(url, '/');
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
437 ucx_buffer_write(esc, 1, strlen(esc), url);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
438 curl_free(esc);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
439 }
74
da079dc0724c fixed many memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
440 free(node.ptr);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
441 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
442 free(tks);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
443 if(path[p.length-1] == '/') {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
444 ucx_buffer_putc(url, '/');
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
445 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
446 ucx_buffer_putc(url, 0);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
447
69
0dbdd7e8c1fc fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
448 space = url->space;
0dbdd7e8c1fc fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
449 ucx_buffer_free(url);
0dbdd7e8c1fc fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
450
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
451 return space;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
452 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
453
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
454 char* util_parent_path(char *path) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 char *name = util_resource_name(path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456 size_t namelen = strlen(name);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
457 size_t pathlen = strlen(path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
458 size_t parentlen = pathlen - namelen;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
459 char *parent = malloc(parentlen + 1);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
460 memcpy(parent, path, parentlen);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
461 parent[parentlen] = '\0';
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
462 return parent;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
463 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
464
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
465
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
466 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
467 xmlNode *node = elm->children;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
468 while(node) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
469 if(node->type == XML_TEXT_NODE) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470 return (char*)node->content;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
471 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
472 node = node->next;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
473 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
474 return NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
475 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
476
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
477
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
478 char* util_base64decode(const char *in) {
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
479 int len = 0;
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
480 return util_base64decode_len(in, &len);
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
481 }
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
482
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
483 #define WHITESPACE 64
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
484 #define EQUALS 65
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
485 #define INVALID 66
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
486 static char b64dectable[] = {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
487 66,66,66,66,66,66,66,66,66,66,64,66,66,66,66,66,66,66,66,66,66,66,66,66,66,
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
488 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,62,66,66,66,63,52,53,
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
489 54,55,56,57,58,59,60,61,66,66,66,65,66,66,66, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
490 10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,66,66,66,66,66,66,26,27,28,
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
491 29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,66,66,
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
492 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
493 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
494 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
495 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
496 66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,66,
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
497 66,66,66,66,66,66
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
498 };
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
499 char* util_base64decode_len(const char* in, int *outlen) {
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
500 /* code is mostly from wikibooks */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
501
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
502 size_t inlen = strlen(in);
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
503 size_t bufsize = (inlen*3) / 4;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
504 char *outbuf = malloc(bufsize+1);
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
505 *outlen = -1;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
506
349
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
507 unsigned char *out = (unsigned char*)outbuf;
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
508
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
509 const char *end = in + inlen;
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
510 char iter = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
511 uint32_t buf = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
512 size_t len = 0;
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
513
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
514 while (in < end) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
515 unsigned char c = b64dectable[*in++];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
516
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
517 switch (c) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
518 case WHITESPACE: continue; /* skip whitespace */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
519 case INVALID: {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
520 /* invalid input */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
521 outbuf[0] = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
522 return outbuf;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
523 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
524 case EQUALS: {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
525 /* pad character, end of data */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
526 in = end;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
527 continue;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
528 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
529 default: {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
530 buf = buf << 6 | c;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
531 iter++; // increment the number of iteration
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
532 /* If the buffer is full, split it into bytes */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
533 if (iter == 4) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
534 if ((len += 3) > bufsize) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
535 /* buffer overflow */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
536 outbuf[0] = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
537 return outbuf;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
538 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
539 *(out++) = (buf >> 16) & 255;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
540 *(out++) = (buf >> 8) & 255;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
541 *(out++) = buf & 255;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
542 buf = 0; iter = 0;
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
543
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
544 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
545 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
546 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
547 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
548
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
549 if (iter == 3) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
550 if ((len += 2) > bufsize) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
551 /* buffer overflow */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
552 outbuf[0] = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
553 return outbuf;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
554 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
555 *(out++) = (buf >> 10) & 255;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
556 *(out++) = (buf >> 2) & 255;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
557 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
558 else if (iter == 2) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
559 if (++len > bufsize) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
560 /* buffer overflow */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
561 outbuf[0] = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
562 return outbuf;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
563 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
564 *(out++) = (buf >> 4) & 255;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
565 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
566
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
567 *outlen = len; /* modify to reflect the actual output size */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
568 outbuf[len] = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
569 return outbuf;
33
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
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
572
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
573 static char* b64enctable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
574 char* util_base64encode(const char *in, size_t len) {
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
575 // calculate length of base64 output and create buffer
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
576 size_t outlen = 4 * ((len + 2) / 3);
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
577 int pad = len % 3;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
578
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
579 char *out = malloc(outlen + 1);
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
580 out[outlen] = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
581 size_t pos = 0;
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
582
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
583 // encode blocks of 3 bytes
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
584 size_t i;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
585 size_t blockend = len - pad;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
586 for(i=0;i<blockend;i++) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
587 unsigned char b1 = in[i++];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
588 unsigned char b2 = in[i++];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
589 unsigned char b3 = in[i];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
590 uint32_t inb = b1 << 16 | (b2 << 8) | b3;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
591 out[pos++] = b64enctable[(inb >> 18) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
592 out[pos++] = b64enctable[(inb >> 12) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
593 out[pos++] = b64enctable[(inb >> 6) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
594 out[pos++] = b64enctable[(inb) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
595 }
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
596
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
597 // encode last bytes
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
598 if(pad > 0) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
599 char p[3] = {0, 0, 0};
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
600 for(int j=0;i<len;i++) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
601 p[j++] = in[i];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
602 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
603 unsigned char b1 = p[0];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
604 unsigned char b2 = p[1];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
605 unsigned char b3 = p[2];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
606 uint32_t inb = (b1 << 16) | (b2 << 8) | b3;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
607 out[pos++] = b64enctable[(inb >> 18) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
608 out[pos++] = b64enctable[(inb >> 12) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
609 out[pos++] = b64enctable[(inb >> 6) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
610 out[pos++] = b64enctable[(inb) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
611 for(int k=outlen-1;k>=outlen-(3-pad);k--) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
612 out[k] = '=';
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
613 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
614 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
615
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
616 return out;
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
617 }
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
618
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
619 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
620 DavKey *k = dav_context_get_key(sn->context, key);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
621 if(!k) {
225
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
622 sn->error = DAV_ERROR;
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
623 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
624 dav_session_set_errstr(sn, err.ptr);
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
625 free(err.ptr);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
626 return NULL;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
627 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
628
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
629 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
630 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
631
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
632 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
633 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
634 char *ret_str = dav_session_strdup(sn, enc_str);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
635 free(enc_str);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
636 return ret_str;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
637 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
638
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
639 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
640 DavKey *k = dav_context_get_key(sn->context, key);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
641 if(!k) {
225
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
642 sn->error = DAV_ERROR;
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
643 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
644 dav_session_set_errstr(sn, err.ptr);
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
645 free(err.ptr);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
646 return NULL;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
647 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
648
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
649 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
650 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
651
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
652 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
653 size_t len = 0;
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
654 char *dec_str = aes_decrypt(str, &len, key);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
655 char *ret_str = dav_session_strdup(sn, dec_str);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
656 free(dec_str);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
657 return ret_str;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
658 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
659
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
660 char* util_random_str() {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
661 unsigned char *str = malloc(25);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
662 str[24] = '\0';
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
663
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
664 sstr_t t = S(
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
665 "01234567890"
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
666 "abcdefghijklmnopqrstuvwxyz"
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
667 "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
668 const unsigned char *table = (const unsigned char*)t.ptr;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
669
349
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
670 #ifdef DAV_USE_OPENSSL
337
d7bda914d120 replaces RAND_pseudo_bytes with RAND_bytes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 329
diff changeset
671 RAND_bytes(str, 24);
349
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
672 #else
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
673 dav_rand_bytes(str, 24);
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
674 #endif
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
675 for(int i=0;i<24;i++) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
676 int c = str[i] % t.length;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
677 str[i] = table[c];
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
678 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
679
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
680 return (char*)str;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
681 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
682
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
683 /*
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
684 * 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
685 * tokens are separated by space
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
686 * 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
687 */
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
688 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
689 int i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
690 int token_start = -1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
691 int token_end = -1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
692 for(i=0;i<=str.length;i++) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
693 int c;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
694 if(i == str.length) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
695 c = ' ';
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
696 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
697 c = str.ptr[i];
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
698 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
699 if(c < 33) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
700 if(token_start != -1) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
701 token_end = i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
702 size_t len = token_end - token_start;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
703 sstr_t tk = sstrsubsl(str, token_start, len);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
704 //printf("token: {%.*s}\n", token.length, token.ptr);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
705 if(!sstrcmp(tk, token)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
706 *sub = sstrtrim(sstrsubsl(str, 0, token_start));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
707 break;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
708 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
709 token_start = -1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
710 token_end = -1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
711 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
712 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
713 if(token_start == -1) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
714 token_start = i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
715 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
716 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
717 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
718
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
719 if(i < str.length) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
720 return sstrtrim(sstrsubs(str, i));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
721 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
722 str.ptr = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
723 str.length = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
724 return str;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
725 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
726 }
119
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
727
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
728 sstr_t util_readline(FILE *stream) {
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
729 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
730
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
731 int c;
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
732 while((c = fgetc(stream)) != '\n') {
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
733 ucx_buffer_putc(buf, c);
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
734 }
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
735
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
736 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
737 ucx_buffer_free(buf);
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
738 return str;
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
739 }
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
740
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
741 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
742 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
743 fflush(stderr);
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
744
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
745 #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
746 // 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
747 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
748 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
749 nflags = oflags;
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
750 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
751 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
752 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
753 perror("tcsetattr");
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
754 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
755 #endif
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
756
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
757 // 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
758 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
759 int c = 0;
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
760 while((c = getpasswordchar()) != EOF) {
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
761 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
762 break;
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
763 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
764 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
765 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
766 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
767 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
768
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
769 #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
770 // 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
771 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
772 perror("tcsetattr");
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
773 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
774 #endif
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
775
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
776 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
777 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
778 return str;
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
779 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
780
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
781
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
782 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
783 size_t buflen = 2*len + 4;
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
784 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
785 for(int i=0;i<len;i++) {
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
786 ucx_bprintf(buf, "%x", data[i]);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
787 }
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
788 ucx_buffer_putc(buf, 0);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
789 char *str = buf->space;
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
790 ucx_buffer_free(buf);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
791 return str;
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
792 }

mercurial