|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2013 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <time.h> |
|
30 #include <stdio.h> |
|
31 #include <stdlib.h> |
|
32 #include <string.h> |
|
33 #include <ucx/string.h> |
|
34 #include <libxml/tree.h> |
|
35 #include <curl/curl.h> |
|
36 |
|
37 #include <openssl/sha.h> |
|
38 #include <openssl/hmac.h> |
|
39 #include <openssl/evp.h> |
|
40 #include <openssl/bio.h> |
|
41 #include <openssl/buffer.h> |
|
42 |
|
43 #include "utils.h" |
|
44 |
|
45 |
|
46 time_t util_parse_creationdate(char *str) { |
|
47 // example: 2012-11-29T21:35:35Z |
|
48 if(!str) { |
|
49 return 0; |
|
50 } |
|
51 // TODO |
|
52 return 0; |
|
53 } |
|
54 |
|
55 time_t util_parse_lastmodified(char *str) { |
|
56 // example: Thu, 29 Nov 2012 21:35:35 GMT |
|
57 if(!str) { |
|
58 return 0; |
|
59 } else { |
|
60 return curl_getdate(str, NULL); |
|
61 } |
|
62 } |
|
63 |
|
64 int util_getboolean(char *v) { |
|
65 if(v[0] == 'T' || v[0] == 't') { |
|
66 return 1; |
|
67 } |
|
68 return 0; |
|
69 } |
|
70 |
|
71 int util_strtoint(char *str, int64_t *value) { |
|
72 char *end; |
|
73 int64_t val = strtoll(str, &end, 0); |
|
74 if(strlen(end) == 0) { |
|
75 *value = val; |
|
76 return 1; |
|
77 } else { |
|
78 return 0; |
|
79 } |
|
80 } |
|
81 |
|
82 char* util_url_path(char *url) { |
|
83 char *path = NULL; |
|
84 size_t len = strlen(url); |
|
85 int slashcount = 0; |
|
86 int slmax; |
|
87 if(len > 7 && !strncasecmp(url, "http://", 7)) { |
|
88 slmax = 3; |
|
89 } else if(len > 8 && !strncasecmp(url, "https://", 8)) { |
|
90 slmax = 3; |
|
91 } else { |
|
92 slmax = 1; |
|
93 } |
|
94 char c; |
|
95 for(int i=0;i<len;i++) { |
|
96 c = url[i]; |
|
97 if(c == '/') { |
|
98 slashcount++; |
|
99 if(slashcount == slmax) { |
|
100 path = url + i; |
|
101 break; |
|
102 } |
|
103 } |
|
104 } |
|
105 return path; |
|
106 } |
|
107 |
|
108 char* util_resource_name(char *url) { |
|
109 int si = 0; |
|
110 int osi = 0; |
|
111 int i = 0; |
|
112 int p = 0; |
|
113 char c; |
|
114 while((c = url[i]) != 0) { |
|
115 if(c == '/') { |
|
116 osi = si; |
|
117 si = i; |
|
118 p = 1; |
|
119 } |
|
120 i++; |
|
121 } |
|
122 |
|
123 char *name = url + si + p; |
|
124 if(name[0] == 0) { |
|
125 name = url + osi + p; |
|
126 if(name[0] == 0) { |
|
127 return url; |
|
128 } |
|
129 } |
|
130 |
|
131 return name; |
|
132 } |
|
133 |
|
134 int util_mkdir(char *path, mode_t mode) { |
|
135 #ifdef _WIN32 |
|
136 return mkdir(path); |
|
137 #else |
|
138 return mkdir(path, mode); |
|
139 #endif |
|
140 } |
|
141 |
|
142 char* util_concat_path(char *url_base, char *p) { |
|
143 sstr_t base = sstr(url_base); |
|
144 sstr_t path; |
|
145 if(p) { |
|
146 path = sstr(p); |
|
147 } else { |
|
148 path = sstrn("", 0); |
|
149 } |
|
150 |
|
151 int add_separator = 0; |
|
152 if(base.ptr[base.length-1] == '/') { |
|
153 if(path.ptr[0] == '/') { |
|
154 base.length--; |
|
155 } |
|
156 } else { |
|
157 if(path.length == 0 || path.ptr[0] != '/') { |
|
158 add_separator = 1; |
|
159 } |
|
160 } |
|
161 |
|
162 sstr_t url; |
|
163 url.length = base.length + path.length + add_separator; |
|
164 url.ptr = malloc(url.length + 1); |
|
165 url.ptr[url.length] = '\0'; |
|
166 |
|
167 if(add_separator) { |
|
168 url = sstrncat(url, 3, base, sstr("/"), path); |
|
169 } else { |
|
170 url = sstrncat(url, 2, base, path); |
|
171 } |
|
172 |
|
173 return url.ptr; |
|
174 } |
|
175 |
|
176 char* util_parent_path(char *path) { |
|
177 char *name = util_resource_name(path); |
|
178 size_t namelen = strlen(name); |
|
179 size_t pathlen = strlen(path); |
|
180 size_t parentlen = pathlen - namelen; |
|
181 char *parent = malloc(parentlen + 1); |
|
182 memcpy(parent, path, parentlen); |
|
183 parent[parentlen] = '\0'; |
|
184 return parent; |
|
185 } |
|
186 |
|
187 |
|
188 char* util_xml_get_text(xmlNode *elm) { |
|
189 xmlNode *node = elm->children; |
|
190 while(node) { |
|
191 if(node->type == XML_TEXT_NODE) { |
|
192 return (char*)node->content; |
|
193 } |
|
194 node = node->next; |
|
195 } |
|
196 return NULL; |
|
197 } |
|
198 |
|
199 |
|
200 |
|
201 char* util_base64decode(char* in) { |
|
202 size_t len = strlen(in); |
|
203 char *out = calloc(1, len); |
|
204 |
|
205 BIO* b = BIO_new_mem_buf(in, len); |
|
206 BIO *d = BIO_new(BIO_f_base64()); |
|
207 BIO_set_flags(d, BIO_FLAGS_BASE64_NO_NL); |
|
208 b = BIO_push(d, b); |
|
209 |
|
210 BIO_read(b, out, len); |
|
211 BIO_free_all(b); |
|
212 |
|
213 return out; |
|
214 } |
|
215 |
|
216 /* |
|
217 * gets a substring from 0 to the appearance of the token |
|
218 * tokens are separated by space |
|
219 * sets sub to the substring and returns the remaining string |
|
220 */ |
|
221 sstr_t util_getsubstr_until_token(sstr_t str, sstr_t token, sstr_t *sub) { |
|
222 int i; |
|
223 int token_start = -1; |
|
224 int token_end = -1; |
|
225 for(i=0;i<=str.length;i++) { |
|
226 int c; |
|
227 if(i == str.length) { |
|
228 c = ' '; |
|
229 } else { |
|
230 c = str.ptr[i]; |
|
231 } |
|
232 if(c < 33) { |
|
233 if(token_start != -1) { |
|
234 token_end = i; |
|
235 size_t len = token_end - token_start; |
|
236 sstr_t tk = sstrsubsl(str, token_start, len); |
|
237 //printf("token: {%.*s}\n", token.length, token.ptr); |
|
238 if(!sstrcmp(tk, token)) { |
|
239 *sub = sstrtrim(sstrsubsl(str, 0, token_start)); |
|
240 break; |
|
241 } |
|
242 token_start = -1; |
|
243 token_end = -1; |
|
244 } |
|
245 } else { |
|
246 if(token_start == -1) { |
|
247 token_start = i; |
|
248 } |
|
249 } |
|
250 } |
|
251 |
|
252 if(i < str.length) { |
|
253 return sstrtrim(sstrsubs(str, i)); |
|
254 } else { |
|
255 str.ptr = NULL; |
|
256 str.length = 0; |
|
257 return str; |
|
258 } |
|
259 } |