libidav/utils.c

Sun, 15 Dec 2019 18:08:14 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 15 Dec 2019 18:08:14 +0100
changeset 707
33c9b0f17cb2
parent 696
ec1509240080
child 723
5ca174b3247a
permissions
-rw-r--r--

change version to 1.3.0

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 *
576
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
4 * Copyright 2019 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()
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
44 #define IS_PATH_SEPARATOR(c) (c == '/' || c == '\\')
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
45 #define PATH_SEPARATOR '\\'
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
46 #else
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
47 #include <termios.h>
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
48 #define getpasswordchar() getchar()
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
49 #define IS_PATH_SEPARATOR(c) (c == '/')
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
50 #define PATH_SEPARATOR '/'
152
ff854d3df20c fixed windows build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 151
diff changeset
51 #endif
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
349
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
53 #include "webdav.h"
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
54 #include "utils.h"
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
55 #include "crypto.h"
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
56 #include "session.h"
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
57
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
58 /*
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 #include <openssl/hmac.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 #include <openssl/evp.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 #include <openssl/bio.h>
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 #include <openssl/buffer.h>
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
63 #include <openssl/rand.h>
349
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
64 */
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
66 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
67 size_t n = 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
68 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
69 if(isdigit(str.ptr[i])) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
70 result[n++] = str.ptr[i];
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
71 } else if(str.ptr[i] != delim) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
72 return 0;
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 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
75 result[n] = '\0';
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
76 return n;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
77 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
78
186
05d11fbcaeb3 improved copy/move
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
79 static time_t parse_iso8601(char *iso8601str) {
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 // safety
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
82 if(!iso8601str) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
83 return 0;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
84 }
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
85
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
86 // local vars
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
87 struct tm tparts;
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
88 memset(&tparts, 0, sizeof(struct tm));
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
89 long val;
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
90 char conv[16];
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
91
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
92 // work on the trimmed string
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
93 sstr_t date = sstrtrim(sstr(iso8601str));
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 sstr_t time = sstrchr(date, 'T');
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
96 if(time.length == 0) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
97 return 0;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
98 }
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
99 date.length = time.ptr - date.ptr;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
100 time.ptr++; time.length--;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
101
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
102 sstr_t tzinfo;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
103 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
104 (tzinfo = sstrchr(time, '+')).length > 0 ||
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
105 (tzinfo = sstrchr(time, '-')).length > 0) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
106
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
107 time.length = tzinfo.ptr - time.ptr;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
108 }
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
109
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
110 // parse date
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
111 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
112 || extractval(date, conv , '-') != 8) {
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 val = atol(conv);
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
116 if(val < 19000000L) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
117 return 0;
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 tparts.tm_mday = val % 100;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
120 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
121 tparts.tm_year = val / 10000 - 1900;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
122
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
123 // 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
124 sstr_t frac;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
125 if((frac = sstrchr(time, '.')).length > 0 ||
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
126 (frac = sstrchr(time, ',')).length > 0) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
127 time.length = frac.ptr - time.ptr;
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 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
130 || extractval(time, conv , ':') != 6) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
131 return 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
132 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
133 val = atol(conv);
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
134 tparts.tm_sec = val % 100;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
135 tparts.tm_min = (val % 10000) / 100;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
136 tparts.tm_hour = val / 10000;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
137
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
138
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
139 // parse time zone (if any)
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
140 if(tzinfo.length == 0) {
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
141 // local time
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
142 tparts.tm_isdst = -1;
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
143 return mktime(&tparts);
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
144 } else if(!sstrcmp(tzinfo, S("Z"))) {
184
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
145 #ifdef __FreeBSD__
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
146 return timegm(&tparts);
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
147 #else
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
148 return mktime(&tparts) - timezone;
184
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
149 #endif
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
150 } 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
151 int sign = (tzinfo.ptr[0] == '+') ? -1 : 1;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
152
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
153 if(tzinfo.length > 6) {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
154 return 0;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
155 } else {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
156 tzinfo.ptr++; tzinfo.length--;
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
157 extractval(tzinfo, conv, ':');
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
158 val = atol(conv);
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
159 val = 60 * (val / 100) + (val % 100);
184
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
160 #ifdef __FreeBSD__
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
161 return timegm(&tparts) + (time_t) (60 * val * sign);
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
162 #else
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
163 return mktime(&tparts) - timezone + (time_t) (60 * val * sign);
7ba3c97b31ff fixed FreeBSD build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 181
diff changeset
164 #endif
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
165 }
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
166 } else {
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
167 return 0;
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
168 }
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
169 }
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
170
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 time_t util_parse_creationdate(char *str) {
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
173 // parse a ISO-8601 date (rfc-3339)
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 // example: 2012-11-29T21:35:35Z
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 if(!str) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 }
160
ba8f1691c0ee added parser for iso8601 dates
Mike Becker <universe@uap-core.de>
parents: 159
diff changeset
178
177
3c0734eeab33 better and better and better iso8601 parser
Mike Becker <universe@uap-core.de>
parents: 176
diff changeset
179 return parse_iso8601(str);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 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
183 // parse a rfc-1123 date
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 // 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
185 if(!str) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 } 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
188 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
189 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
190 // 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
191 // 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
192 // 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
193 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
194 } 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
195 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
196 }
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199
535
d814ee31c04f adds blocksize also to the sync db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 534
diff changeset
200 int util_getboolean(const char *v) {
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 if(v[0] == 'T' || v[0] == 't') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206
535
d814ee31c04f adds blocksize also to the sync db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 534
diff changeset
207 int util_strtouint(const char *str, uint64_t *value) {
512
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
208 char *end;
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
209 errno = 0;
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
210 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
211 if(errno == 0) {
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
212 *value = val;
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
213 return 1;
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
214 } else {
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
215 return 0;
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
216 }
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
217 }
3320a015a3bc fixes signed vs unsigned inaccuracy for the -T option
Mike Becker <universe@uap-core.de>
parents: 505
diff changeset
218
535
d814ee31c04f adds blocksize also to the sync db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 534
diff changeset
219 int util_strtoint(const char *str, int64_t *value) {
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 char *end;
124
41939c8f3f9c expression compiler generates byte code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
221 errno = 0;
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 int64_t val = strtoll(str, &end, 0);
124
41939c8f3f9c expression compiler generates byte code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 119
diff changeset
223 if(errno == 0) {
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 *value = val;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 return 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 return 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230
534
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
231 int util_szstrtouint(const char *str, uint64_t *value) {
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
232 char *end;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
233 errno = 0;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
234 size_t len = strlen(str);
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
235 uint64_t val = strtoull(str, &end, 0);
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
236 if(end == str+len) {
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
237 *value = val;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
238 return 1;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
239 } else if(end == str+len-1) {
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
240 uint64_t mul = 1;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
241 switch(end[0]) {
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
242 case 'k':
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
243 case 'K': mul = 1024; break;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
244 case 'm':
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
245 case 'M': mul = 1024*1024; break;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
246 case 'g':
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
247 case 'G': mul = 1024*1024*1024; break;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
248 default: return 0;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
249 }
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
250
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
251 uint64_t result = 0;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
252 if(util_uint_mul(val, mul, &result)) {
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
253 return 0;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
254 }
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
255 *value = result;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
256 return 1;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
257 }
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
258 return 0;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
259 }
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
260
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
261 int util_uint_mul(uint64_t a, uint64_t b, uint64_t *result) {
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
262 if(a == 0 || b == 0) {
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
263 *result = 0;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
264 return 0;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
265 }
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
266 uint64_t r = a * b;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
267 if(r / b == a) {
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
268 *result = r;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
269 return 0;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
270 } else {
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
271 *result = 0;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
272 return 1;
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
273 }
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
274 }
9a4857d6444e adds dav-sync splitconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 512
diff changeset
275
673
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
276 char* util_url_base_s(sstr_t url) {
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
277 size_t i = 0;
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
278 if(url.length > 0) {
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
279 int slmax;
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
280 if(sstrprefix(url, SC("http://"))) {
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
281 slmax = 3;
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
282 } else if(sstrprefix(url, SC("https://"))) {
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
283 slmax = 3;
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
284 } else {
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
285 slmax = 1;
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
286 }
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
287 int slashcount = 0;
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
288 for(i=0;i<url.length;i++) {
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
289 if(url.ptr[i] == '/') {
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
290 slashcount++;
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
291 if(slashcount == slmax) {
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
292 i++;
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
293 break;
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
294 }
151
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
295 }
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
296 }
673
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
297 }
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
298 sstr_t server = sstrsubsl(url, 0, i);
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
299 return sstrdup(server).ptr;
151
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
300 }
a316613205dc fixed some memory leaks, content verification and dav info
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 150
diff changeset
301
673
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
302 char* util_url_base(char *url) {
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
303 return util_url_base_s(sstr(url));
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
304 }
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
305
8e7e56cfc103 adds support for quotes in dav bash completion
Mike Becker <universe@uap-core.de>
parents: 641
diff changeset
306 char* util_url_path(char *url) {
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 char *path = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 size_t len = strlen(url);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 int slashcount = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 int slmax;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 if(len > 7 && !strncasecmp(url, "http://", 7)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 slmax = 3;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 } else if(len > 8 && !strncasecmp(url, "https://", 8)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 slmax = 3;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 slmax = 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318 char c;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 for(int i=0;i<len;i++) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 c = url[i];
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 if(c == '/') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 slashcount++;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 if(slashcount == slmax) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 path = url + i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 break;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 }
609
dc3d70848c7c implement encrypted properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 590
diff changeset
329 if(!path) {
dc3d70848c7c implement encrypted properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 590
diff changeset
330 path = url + len; // empty string
dc3d70848c7c implement encrypted properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 590
diff changeset
331 }
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 return path;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334
66
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
335 char* util_url_decode(DavSession *sn, char *url) {
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
336 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
337 char *ret = strdup(unesc);
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
338 curl_free(unesc);
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
339 return ret;
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
340 }
f8c1f685e08e some bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 43
diff changeset
341
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
342 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
343 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
344
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
345 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
346
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
347 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
348
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
349 // 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
350 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
351 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
352 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
353 return size*nitems;
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
354 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
355
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
356 // 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
357 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
358 return 2;
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
359 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
360
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
361 sstr_t key = sbuffer;
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
362 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
363
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
364 if(value.length == 0) {
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
365 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
366 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
367
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
368 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
369 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
370
174
e7e56c56d126 updated ucx and used new sstrlower in util_header_callback
Mike Becker <universe@uap-core.de>
parents: 173
diff changeset
371 key = sstrlower(sstrtrim(key));
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
372 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
373
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
374 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
375
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
376 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
377
173
947843245525 improved util_header_callback by using sstr_t
Mike Becker <universe@uap-core.de>
parents: 170
diff changeset
378 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
379 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
380
543
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
381 int util_path_isrelated(const char *path1, const char *path2) {
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
382 scstr_t p1 = scstr(path1);
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
383 scstr_t p2 = scstr(path2);
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
384
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
385 if(IS_PATH_SEPARATOR(p1.ptr[p1.length-1])) {
543
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
386 p1.length--;
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
387 }
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
388 if(IS_PATH_SEPARATOR(p2.ptr[p2.length-1])) {
543
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
389 p2.length--;
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
390 }
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
391
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
392 if(p2.length < p1.length) {
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
393 return 0;
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
394 }
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
395
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
396 if(!sstrcmp(p1, p2)) {
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
397 return 1;
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
398 }
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
399
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
400 if(sstrprefix(p2, p1)) {
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
401 if(IS_PATH_SEPARATOR(p2.ptr[p1.length])) {
543
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
402 return 1;
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
403 }
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
404 }
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
405
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
406 return 0;
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
407 }
2f85df8cd35e fixes that pull downloads the history collection
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 542
diff changeset
408
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
409 #ifdef _WIN32
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
410 int util_path_isabsolut(const char *path) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
411 if(strlen(path) < 3) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
412 return 0;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
413 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
414
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
415 // check if first char is A-Z or a-z
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
416 char c = path[0];
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
417 if(!((c >= 65 && c <= 90) || (c >= 97 && c <= 122))) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
418 return 0;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
419 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
420
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
421 if(path[1] == ':' && path[2] == '\\') {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
422 return 1;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
423 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
424 return 0;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
425 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
426 #else
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
427 int util_path_isabsolut(const char *path) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
428 return path[0] == '/';
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
429 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
430 #endif
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
431
575
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
432 char* util_path_normalize(const char *path) {
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
433 size_t len = strlen(path);
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
434 UcxBuffer *buf = ucx_buffer_new(NULL, len+1, UCX_BUFFER_AUTOEXTEND);
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
435
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
436 if(path[0] == '/') {
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
437 ucx_buffer_putc(buf, '/');
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
438 }
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
439
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
440 int add_separator = 0;
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
441 int seg_start = 0;
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
442 for(int i=0;i<=len;i++) {
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
443 char c = path[i];
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
444 if(IS_PATH_SEPARATOR(c) || c == '\0') {
575
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
445 const char *seg_ptr = path+seg_start;
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
446 int seg_len = i - seg_start;
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
447 if(IS_PATH_SEPARATOR(seg_ptr[0])) {
575
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
448 seg_ptr++;
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
449 seg_len--;
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
450 }
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
451
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
452 if(seg_len > 0) {
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
453 scstr_t seg = scstrn(seg_ptr, seg_len);
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
454 if(!sstrcmp(seg, SC(".."))) {
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
455 for(int j=buf->pos;j>=0;j--) {
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
456 char t = buf->space[j];
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
457 if(IS_PATH_SEPARATOR(t) || j == 0) {
575
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
458 buf->pos = j;
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
459 buf->size = j;
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
460 buf->space[j] = 0;
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
461 add_separator = IS_PATH_SEPARATOR(t) ? 1 : 0;
575
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
462 break;
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
463 }
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
464 }
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
465 } else if(!sstrcmp(seg, SC("."))) {
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
466 // ignore
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
467 } else {
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
468 if(add_separator) {
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
469 ucx_buffer_putc(buf, PATH_SEPARATOR);
575
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
470 }
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
471 ucx_buffer_write(seg_ptr, 1, seg_len, buf);
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
472 add_separator = 1;
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
473 }
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
474 }
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
475
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
476 seg_start = i;
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
477 }
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
478 }
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
479
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
480 ucx_buffer_putc(buf, 0);
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
481
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
482
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
483 char *space = buf->space;
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
484 buf->flags = 0; // disable autofree
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
485 ucx_buffer_free(buf);
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
486 return space;
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
487 }
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
488
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
489 static char* create_relative_path(const char *abspath, const char *base) {
576
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
490 size_t path_len = strlen(abspath);
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
491 size_t base_len = strlen(base);
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
492
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
493 if(IS_PATH_SEPARATOR(abspath[path_len-1])) {
576
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
494 path_len--;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
495 }
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
496 if(IS_PATH_SEPARATOR(base[base_len-1])) {
576
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
497 base_len--;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
498 }
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
499 // get base parent
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
500 for(int i=base_len-1;i>=0;i--) {
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
501 if(IS_PATH_SEPARATOR(base[i])) {
576
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
502 base_len = i+1;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
503 break;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
504 }
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
505 }
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
506
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
507 size_t max = path_len > base_len ? base_len : path_len;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
508
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
509 // get prefix of abspath and base
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
510 // this dir is the root of the link
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
511 size_t i;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
512 size_t last_dir = 0;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
513 for(i=0;i<max;i++) {
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
514 char c = abspath[i];
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
515 if(c != base[i]) {
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
516 break;
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
517 } else if(IS_PATH_SEPARATOR(c)) {
576
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
518 last_dir = i;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
519 }
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
520 }
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
521
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
522 char *ret = NULL;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
523 UcxBuffer *out = NULL;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
524 if(last_dir+1 < base_len) {
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
525 // base is deeper than the link root, we have to go backwards
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
526 int dircount = 0;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
527 for(int i=last_dir+1;i<base_len;i++) {
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
528 if(IS_PATH_SEPARATOR(base[i])) {
576
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
529 dircount++;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
530 }
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
531 }
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
532
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
533 out = ucx_buffer_new(NULL, dircount*3+path_len-last_dir, UCX_BUFFER_AUTOEXTEND);
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
534
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
535 for(int i=0;i<dircount;i++) {
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
536 ucx_buffer_puts(out, "../");
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
537 }
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
538 } else {
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
539 out = ucx_buffer_new(NULL, 1024, path_len - last_dir);
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
540 }
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
541
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
542 ucx_buffer_puts(out, abspath + last_dir + 1);
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
543 ucx_buffer_putc(out, 0);
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
544 out->flags = 0;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
545 ret = out->space;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
546 ucx_buffer_free(out);
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
547
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
548 return ret;
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
549 }
62cc92445234 store relative link in LocalResource
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 575
diff changeset
550
611
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
551 #ifdef _WIN32
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
552 char* util_create_relative_path(const char *abspath, const char *base) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
553 char *abspath_converted = strdup(abspath);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
554 char *base_converted = strdup(base);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
555 size_t abs_len = strlen(abspath_converted);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
556 size_t base_len = strlen(base_converted);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
557
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
558 for(int i=0;i<abs_len;i++) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
559 if(abspath_converted[i] == '\\') {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
560 abspath_converted[i] = '/';
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
561 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
562 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
563 for(int i=0;i<base_len;i++) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
564 if(base_converted[i] == '\\') {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
565 base_converted[i] = '/';
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
566 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
567 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
568
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
569 char *ret = create_relative_path(abspath_converted, base_converted);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
570 free(abspath_converted);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
571 free(base_converted);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
572 return ret;
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
573 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
574 #else
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
575 char* util_create_relative_path(const char *abspath, const char *base) {
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
576 return create_relative_path(abspath, base);
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
577 }
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
578 #endif
a7c48e0dca88 implement links on Windows (shelllink)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 609
diff changeset
579
575
f746f601c35c add util_path_normalize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 558
diff changeset
580
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
581 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
582 if(map) {
747f3796eddd improved util_capture_header when deactivating the capturing
Mike Becker <universe@uap-core.de>
parents: 174
diff changeset
583 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
584 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
585 } else {
170
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
586 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
587 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
588 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
589 }
cf054cded046 added depth header workaround for Microsoft IIS (tested with sharepoint)
Mike Becker <universe@uap-core.de>
parents: 161
diff changeset
590
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
591 char* util_resource_name(char *url) {
590
cda8093116f2 reduces code complexity of util_resource_name()
Mike Becker <universe@uap-core.de>
parents: 578
diff changeset
592 sstr_t urlstr = sstr(url);
cda8093116f2 reduces code complexity of util_resource_name()
Mike Becker <universe@uap-core.de>
parents: 578
diff changeset
593 if(urlstr.ptr[urlstr.length-1] == '/') {
cda8093116f2 reduces code complexity of util_resource_name()
Mike Becker <universe@uap-core.de>
parents: 578
diff changeset
594 urlstr.length--;
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
595 }
590
cda8093116f2 reduces code complexity of util_resource_name()
Mike Becker <universe@uap-core.de>
parents: 578
diff changeset
596 sstr_t resname = sstrrchr(urlstr, '/');
cda8093116f2 reduces code complexity of util_resource_name()
Mike Becker <universe@uap-core.de>
parents: 578
diff changeset
597 if(resname.length > 1) {
cda8093116f2 reduces code complexity of util_resource_name()
Mike Becker <universe@uap-core.de>
parents: 578
diff changeset
598 return resname.ptr+1;
cda8093116f2 reduces code complexity of util_resource_name()
Mike Becker <universe@uap-core.de>
parents: 578
diff changeset
599 } else {
cda8093116f2 reduces code complexity of util_resource_name()
Mike Becker <universe@uap-core.de>
parents: 578
diff changeset
600 return url;
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
601 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
602 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
603
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
604 int util_mkdir(char *path, mode_t mode) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
605 #ifdef _WIN32
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
606 return mkdir(path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
607 #else
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
608 return mkdir(path, mode);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
609 #endif
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
610 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
611
372
2e15ff88a0ab adds tag management commands to dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 349
diff changeset
612 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
613 sstr_t base = sstr((char*)url_base);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
614 sstr_t path;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
615 if(p) {
372
2e15ff88a0ab adds tag management commands to dav-sync
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 349
diff changeset
616 path = sstr((char*)p);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
617 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
618 path = sstrn("", 0);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
619 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
620
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
621 int add_separator = 0;
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
622 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
623 if(path.ptr[0] == '/') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
624 base.length--;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
625 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
626 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
627 if(path.length == 0 || path.ptr[0] != '/') {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
628 add_separator = 1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
629 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
630 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
631
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
632 sstr_t url;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
633 if(add_separator) {
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
634 url = sstrcat(3, base, sstr("/"), path);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
635 } else {
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
636 url = sstrcat(2, base, path);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
637 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
638
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
639 return url.ptr;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
640 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
641
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
642 char* util_get_url(DavSession *sn, const char *href) {
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
643 scstr_t base = scstr(sn->base_url);
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
644 scstr_t href_str = scstr(href);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
645
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
646 char *base_path = util_url_path(sn->base_url);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
647 base.length -= strlen(base_path);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
648
70
88092b88ec00 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 69
diff changeset
649 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
650 return url.ptr;
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
651 }
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
652
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
653 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
654 char *url = util_get_url(sn, href);
a8f8cdbf85df implemented webdav copy and move method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
655 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
656 free(url);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
657 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
658
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
659 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
660 char *space = malloc(256);
0dbdd7e8c1fc fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
661 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
662
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
663 // add base url
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
664 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
665 // remove trailing slash
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
666 ucx_buffer_seek(url, -1, SEEK_CUR);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
667
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
668 sstr_t p = sstr(path);
74
da079dc0724c fixed many memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
669 ssize_t ntk = 0;
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
670 sstr_t *tks = sstrsplit(p, S("/"), &ntk);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
671
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
672 for(int i=0;i<ntk;i++) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
673 sstr_t node = tks[i];
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
674 if(node.length > 0) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
675 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
676 ucx_buffer_putc(url, '/');
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
677 ucx_buffer_write(esc, 1, strlen(esc), url);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
678 curl_free(esc);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
679 }
74
da079dc0724c fixed many memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 71
diff changeset
680 free(node.ptr);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
681 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
682 free(tks);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
683 if(path[p.length-1] == '/') {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
684 ucx_buffer_putc(url, '/');
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
685 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
686 ucx_buffer_putc(url, 0);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
687
69
0dbdd7e8c1fc fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
688 space = url->space;
0dbdd7e8c1fc fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
689 ucx_buffer_free(url);
0dbdd7e8c1fc fixed incorrect use of UcxBuffer
Mike Becker <universe@uap-core.de>
parents: 66
diff changeset
690
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
691 return space;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
692 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 39
diff changeset
693
578
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
694 char* util_parent_path(const char *path) {
bb1e60fada74 create symlinks on pull
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 576
diff changeset
695 char *name = util_resource_name((char*)path);
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
696 size_t namelen = strlen(name);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
697 size_t pathlen = strlen(path);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
698 size_t parentlen = pathlen - namelen;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
699 char *parent = malloc(parentlen + 1);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
700 memcpy(parent, path, parentlen);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
701 parent[parentlen] = '\0';
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
702 return parent;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
703 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
704
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
705
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
706 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
707 xmlNode *node = elm->children;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
708 while(node) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
709 if(node->type == XML_TEXT_NODE) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
710 return (char*)node->content;
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 node = node->next;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
713 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
714 return NULL;
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
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
718 char* util_base64decode(const char *in) {
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
719 int len = 0;
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
720 return util_base64decode_len(in, &len);
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
721 }
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
722
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
723 #define WHITESPACE 64
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
724 #define EQUALS 65
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
725 #define INVALID 66
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
726 static char b64dectable[] = {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
727 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
728 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
729 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
730 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
731 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
732 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
733 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
734 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
735 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
736 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
737 66,66,66,66,66,66
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
738 };
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
739 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
740 /* code is mostly from wikibooks */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
741
609
dc3d70848c7c implement encrypted properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 590
diff changeset
742 if(!in) {
dc3d70848c7c implement encrypted properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 590
diff changeset
743 *outlen = 0;
dc3d70848c7c implement encrypted properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 590
diff changeset
744 return NULL;
dc3d70848c7c implement encrypted properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 590
diff changeset
745 }
dc3d70848c7c implement encrypted properties
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 590
diff changeset
746
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
747 size_t inlen = strlen(in);
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
748 size_t bufsize = (inlen*3) / 4;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
749 char *outbuf = malloc(bufsize+1);
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
750 *outlen = -1;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
751
349
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
752 unsigned char *out = (unsigned char*)outbuf;
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
753
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
754 const char *end = in + inlen;
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
755 char iter = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
756 uint32_t buf = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
757 size_t len = 0;
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
758
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
759 while (in < end) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
760 unsigned char c = b64dectable[*in++];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
761
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
762 switch (c) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
763 case WHITESPACE: continue; /* skip whitespace */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
764 case INVALID: {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
765 /* invalid input */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
766 outbuf[0] = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
767 return outbuf;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
768 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
769 case EQUALS: {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
770 /* pad character, end of data */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
771 in = end;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
772 continue;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
773 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
774 default: {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
775 buf = buf << 6 | c;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
776 iter++; // increment the number of iteration
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
777 /* 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
778 if (iter == 4) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
779 if ((len += 3) > bufsize) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
780 /* buffer overflow */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
781 outbuf[0] = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
782 return outbuf;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
783 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
784 *(out++) = (buf >> 16) & 255;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
785 *(out++) = (buf >> 8) & 255;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
786 *(out++) = buf & 255;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
787 buf = 0; iter = 0;
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
788
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
789 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
790 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
791 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
792 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
793
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
794 if (iter == 3) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
795 if ((len += 2) > bufsize) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
796 /* buffer overflow */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
797 outbuf[0] = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
798 return outbuf;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
799 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
800 *(out++) = (buf >> 10) & 255;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
801 *(out++) = (buf >> 2) & 255;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
802 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
803 else if (iter == 2) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
804 if (++len > bufsize) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
805 /* buffer overflow */
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
806 outbuf[0] = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
807 return outbuf;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
808 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
809 *(out++) = (buf >> 4) & 255;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
810 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
811
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
812 *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
813 outbuf[len] = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
814 return outbuf;
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
815 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
816
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
817
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
818 static char* b64enctable = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
505
481802342fdf ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 404
diff changeset
819 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
820 // calculate length of base64 output and create buffer
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
821 size_t outlen = 4 * ((len + 2) / 3);
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
822 int pad = len % 3;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
823
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
824 char *out = malloc(outlen + 1);
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
825 out[outlen] = 0;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
826 size_t pos = 0;
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
827
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
828 // encode blocks of 3 bytes
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
829 size_t i;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
830 size_t blockend = len - pad;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
831 for(i=0;i<blockend;i++) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
832 unsigned char b1 = in[i++];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
833 unsigned char b2 = in[i++];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
834 unsigned char b3 = in[i];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
835 uint32_t inb = b1 << 16 | (b2 << 8) | b3;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
836 out[pos++] = b64enctable[(inb >> 18) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
837 out[pos++] = b64enctable[(inb >> 12) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
838 out[pos++] = b64enctable[(inb >> 6) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
839 out[pos++] = b64enctable[(inb) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
840 }
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
841
344
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
842 // encode last bytes
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
843 if(pad > 0) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
844 char p[3] = {0, 0, 0};
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
845 for(int j=0;i<len;i++) {
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
846 p[j++] = in[i];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
847 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
848 unsigned char b1 = p[0];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
849 unsigned char b2 = p[1];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
850 unsigned char b3 = p[2];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
851 uint32_t inb = (b1 << 16) | (b2 << 8) | b3;
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
852 out[pos++] = b64enctable[(inb >> 18) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
853 out[pos++] = b64enctable[(inb >> 12) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
854 out[pos++] = b64enctable[(inb >> 6) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
855 out[pos++] = b64enctable[(inb) & 63];
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
856 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
857 out[k] = '=';
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
858 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
859 }
72791e299d64 removes openssl dependencies for base64
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 337
diff changeset
860
39
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
861 return out;
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
862 }
3e55bed345f9 added some aes functions + ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 33
diff changeset
863
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
864 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
865 DavKey *k = dav_context_get_key(sn->context, key);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
866 if(!k) {
225
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
867 sn->error = DAV_ERROR;
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
868 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
869 dav_session_set_errstr(sn, err.ptr);
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
870 free(err.ptr);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
871 return NULL;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
872 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
873
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
874 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
875 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
876
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
877 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
878 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
879 char *ret_str = dav_session_strdup(sn, enc_str);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
880 free(enc_str);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
881 return ret_str;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
882 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
883
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
884 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
885 DavKey *k = dav_context_get_key(sn->context, key);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
886 if(!k) {
225
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
887 sn->error = DAV_ERROR;
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
888 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
889 dav_session_set_errstr(sn, err.ptr);
a297c2e28fa1 improved error handling in libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 189
diff changeset
890 free(err.ptr);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
891 return NULL;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
892 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
893
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
894 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
895 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
896
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
897 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
898 size_t len = 0;
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
899 char *dec_str = aes_decrypt(str, &len, key);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
900 char *ret_str = dav_session_strdup(sn, dec_str);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
901 free(dec_str);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
902 return ret_str;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
903 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
904
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
905 char* util_random_str() {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
906 unsigned char *str = malloc(25);
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
907 str[24] = '\0';
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
908
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
909 sstr_t t = S(
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
910 "01234567890"
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
911 "abcdefghijklmnopqrstuvwxyz"
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
912 "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
913 const unsigned char *table = (const unsigned char*)t.ptr;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
914
349
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
915 #ifdef DAV_USE_OPENSSL
337
d7bda914d120 replaces RAND_pseudo_bytes with RAND_bytes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 329
diff changeset
916 RAND_bytes(str, 24);
349
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
917 #else
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
918 dav_rand_bytes(str, 24);
0b4ecadaf3f9 ports openssl code to commoncrypto (macos)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 344
diff changeset
919 #endif
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
920 for(int i=0;i<24;i++) {
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
921 int c = str[i] % t.length;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
922 str[i] = table[c];
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
923 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
924
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
925 return (char*)str;
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
926 }
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
927
33
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
928 /*
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
929 * 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
930 * tokens are separated by space
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
931 * 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
932 */
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
933 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
934 int i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
935 int token_start = -1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
936 int token_end = -1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
937 for(i=0;i<=str.length;i++) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
938 int c;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
939 if(i == str.length) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
940 c = ' ';
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
941 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
942 c = str.ptr[i];
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
943 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
944 if(c < 33) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
945 if(token_start != -1) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
946 token_end = i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
947 size_t len = token_end - token_start;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
948 sstr_t tk = sstrsubsl(str, token_start, len);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
949 //printf("token: {%.*s}\n", token.length, token.ptr);
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
950 if(!sstrcmp(tk, token)) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
951 *sub = sstrtrim(sstrsubsl(str, 0, token_start));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
952 break;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
953 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
954 token_start = -1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
955 token_end = -1;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
956 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
957 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
958 if(token_start == -1) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
959 token_start = i;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
960 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
961 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
962 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
963
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
964 if(i < str.length) {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
965 return sstrtrim(sstrsubs(str, i));
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
966 } else {
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
967 str.ptr = NULL;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
968 str.length = 0;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
969 return str;
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
970 }
0bbbb0341606 some code cleanup and bug fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
971 }
119
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
972
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
973 sstr_t util_readline(FILE *stream) {
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
974 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
975
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
976 int c;
696
ec1509240080 fix util_readline
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 673
diff changeset
977 while((c = fgetc(stream)) != EOF) {
ec1509240080 fix util_readline
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 673
diff changeset
978 if(c == '\n') {
ec1509240080 fix util_readline
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 673
diff changeset
979 break;
ec1509240080 fix util_readline
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 673
diff changeset
980 }
119
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
981 ucx_buffer_putc(buf, c);
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
982 }
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
983
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
984 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
985 ucx_buffer_free(buf);
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
986 return str;
451607eeff05 added initial code for config commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
987 }
147
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
988
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
989 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
990 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
991 fflush(stderr);
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
992
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
993 #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
994 // 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
995 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
996 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
997 nflags = oflags;
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
998 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
999 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
1000 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
1001 perror("tcsetattr");
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
1002 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
1003 #endif
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
1004
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
1005 // 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
1006 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
1007 int c = 0;
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
1008 while((c = getpasswordchar()) != EOF) {
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
1009 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
1010 break;
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
1011 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
1012 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
1013 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
1014 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
1015 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
1016
153
272173064319 implemented hidden password input for windows by using conio
Mike Becker <universe@uap-core.de>
parents: 152
diff changeset
1017 #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
1018 // 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
1019 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
1020 perror("tcsetattr");
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
1021 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
1022 #endif
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
1023
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
1024 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
1025 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
1026 return str;
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
1027 }
458a8dc68048 added password based keys and fixed crash when keys are missing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 124
diff changeset
1028
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
1029
542
060a8cda7f62 adds wrapper functions for setting/getting the content hash
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 535
diff changeset
1030 char* util_hexstr(const unsigned char *data, size_t len) {
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
1031 size_t buflen = 2*len + 4;
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
1032 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
1033 for(int i=0;i<len;i++) {
641
b138d1241e68 finish hashing tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 624
diff changeset
1034 ucx_bprintf(buf, "%02x", data[i]);
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
1035 }
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
1036 ucx_buffer_putc(buf, 0);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
1037 char *str = buf->space;
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
1038 ucx_buffer_free(buf);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
1039 return str;
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 149
diff changeset
1040 }
544
9e85e1ec1155 adds semi functional download of splitted files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
1041
9e85e1ec1155 adds semi functional download of splitted files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
1042 void util_remove_trailing_pathseparator(char *path) {
9e85e1ec1155 adds semi functional download of splitted files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
1043 size_t len = strlen(path);
9e85e1ec1155 adds semi functional download of splitted files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
1044 if(len < 2) {
9e85e1ec1155 adds semi functional download of splitted files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
1045 return;
9e85e1ec1155 adds semi functional download of splitted files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
1046 }
9e85e1ec1155 adds semi functional download of splitted files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
1047
9e85e1ec1155 adds semi functional download of splitted files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
1048 if(path[len-1] == '/') {
9e85e1ec1155 adds semi functional download of splitted files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
1049 path[len-1] = '\0';
9e85e1ec1155 adds semi functional download of splitted files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
1050 }
9e85e1ec1155 adds semi functional download of splitted files
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 543
diff changeset
1051 }
545
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1052
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1053 char* util_file_hash(const char *path) {
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1054 FILE *in = fopen(path, "r");
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1055 if(!in) {
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1056 return NULL;
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1057 }
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1058
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1059 DAV_SHA_CTX *sha = dav_hash_init();
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1060 char *buf = malloc(16384);
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1061
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1062 size_t r;
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1063 while((r = fread(buf, 1, 16384, in)) > 0) {
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1064 dav_hash_update(sha, buf, r);
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1065 }
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1066
558
1a9e6a5c1e79 fixes several signedness issues
Mike Becker <universe@uap-core.de>
parents: 545
diff changeset
1067 unsigned char hash[DAV_SHA256_DIGEST_LENGTH];
545
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1068 dav_hash_final(sha, hash);
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1069 free(buf);
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1070 fclose(in);
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1071
268157722a0e store content hash for splitted resources in the db
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 544
diff changeset
1072 return util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH);
624
27985062cd2c fix build on Solaris 10
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 611
diff changeset
1073 }

mercurial