libidav/utils.c

Wed, 24 Jul 2024 23:45:31 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 24 Jul 2024 23:45:31 +0200
changeset 826
b6e9fd3f1951
parent 816
839fefbdedc7
child 834
6a466635eace
permissions
-rw-r--r--

fix dav add-repo crash in case .dav/config.xml doesn't exist

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

mercurial