1 #ifndef BZS_DB_PROTOCOL_TDAP_URI_H
2 #define BZS_DB_PROTOCOL_TDAP_URI_H
21 #include <bzs/env/compiler.h>
22 #include <bzs/env/crosscompile.h>
24 #include <boost/version.hpp>
26 #if BOOST_VERSION > 103901
27 #include <boost/uuid/sha1.hpp>
29 #include <global/boost/sha1.hpp>
31 #include <bzs/db/protocol/tdap/tdapcapi.h>
44 inline const _TCHAR* protocol(
const _TCHAR* uri)
46 const _TCHAR* st = _tcsstr(uri, _T(
"tdap://"));
51 st = _tcsstr(uri, _T(
"btrv://"));
58 inline void endPoint(
const _TCHAR* uri,
59 _TCHAR* host,
size_t hostSize,
60 _TCHAR* port,
size_t portSize)
62 if (host) host[0] = 0x00;
63 if (port) port[0] = 0x00;
64 const _TCHAR* st = _tcsstr(uri, _T(
"@"));
69 st = _tcsstr(uri, _T(
"://"));
74 const _TCHAR* en = _tcsstr(st, _T(
":"));
79 _tcsncpy_s(host, hostSize, st, en - st);
85 en = _tcsstr(st, _T(
"/"));
88 _tcsncpy_s(port, portSize, st, en - st);
95 en = _tcsstr(st, _T(
"/"));
98 _tcsncpy_s(host, hostSize, st, en - st);
105 inline const _TCHAR* hostName(
const _TCHAR* uri, _TCHAR* buf,
size_t size)
107 endPoint(uri, buf, size, NULL, 0);
111 inline const _TCHAR* port(
const _TCHAR* uri, _TCHAR* buf,
size_t size)
113 endPoint(uri, NULL, 0, buf, size);
117 inline const _TCHAR* dbname(
const _TCHAR* uri, _TCHAR* buf,
size_t size)
120 const _TCHAR* st = _tcsstr(uri, _T(
"://"));
123 st = _tcsstr(st + 3, _T(
"/"));
126 const _TCHAR* en = _tcsstr(st, _T(
"?"));
128 en = _tcslen(st) + st;
131 _tcsncpy_s(buf, size, st, en - st);
133 int n = (int)(en - st -1);
134 if (n >= 0 && buf[n] ==
'/') buf[n] = 0x00;
141 inline const _TCHAR* schemaTable(
const _TCHAR* uri, _TCHAR* buf,
size_t size)
144 _TCHAR* st = _tcsstr((_TCHAR*)uri, _T(
"dbfile="));
148 _tcscpy_s(buf, size, st);
149 st = _tcschr(buf, _T(
'&'));
152 const _TCHAR* en = _tcsrchr(st, _T(
'.'));
159 inline const _TCHAR* userName(
const _TCHAR* uri, _TCHAR* buf,
size_t size)
162 const _TCHAR* st = _tcsstr(uri, _T(
"://"));
167 const _TCHAR* en = _tcsstr(st, _T(
"@"));
170 _tcsncpy_s(buf, size, st, en - st);
177 inline const _TCHAR* passwd(
const _TCHAR* uri, _TCHAR* buf,
size_t size)
180 _TCHAR* st = _tcsstr((_TCHAR*)uri, _T(
"pwd="));
183 _tcscpy_s(buf, size, st+4);
184 st = _tcschr(buf, _T(
'&'));
191 inline const _TCHAR* connectName(
const _TCHAR* uri, _TCHAR* buf,
size_t size)
193 _TCHAR pwd[MAX_PATH];
194 passwd(uri, pwd, MAX_PATH);
197 const _TCHAR* st = _tcsstr(uri, _T(
"://"));
200 st = _tcsstr(st + 3, _T(
"/"));
201 _tcsncpy_s(buf, size, uri, ++st - uri);
202 buf[st - uri] = 0x00;
205 _tcscat_s(buf, MAX_PATH, _T(
"?pwd="));
206 _tcscat_s(buf, MAX_PATH, pwd);
213 inline _TCHAR* stripParam(
const _TCHAR* uri, _TCHAR* buf,
size_t size)
216 _TCHAR* st = _tcsstr((_TCHAR*)uri, _T(
"://"));
219 st = _tcsstr(st + 3, _T(
"/"));
222 _tcscpy_s(buf, size, uri);
223 _TCHAR* en = _tcschr(buf, _T(
'?'));
230 inline const _TCHAR* stripPasswd(
const _TCHAR* uri, _TCHAR* buf,
size_t size)
232 _tcscpy_s(buf, size, uri);
233 _TCHAR* st = _tcsstr(buf, _T(
"://"));
236 st = _tcsstr(st, _T(
"pwd="));
243 inline const _TCHAR* stripAuth(
const _TCHAR* uri, _TCHAR* buf,
size_t size)
245 _tcscpy_s(buf, size, uri);
246 _TCHAR* st = _tcsstr(buf, _T(
"://"));
249 const _TCHAR* en = _tcsstr(uri, _T(
"@"));
252 _tcscpy_s(st+3, size - (st - buf + 3), ++en);
253 _TCHAR* st2 = _tcsstr(st, _T(
"&pwd="));
258 st2 = _tcsstr(st, _T(
"?pwd="));
259 if (st2) *(st2 + 1) = 0x00;
266 inline const _TCHAR* appendAuth(
const _TCHAR* uri,
const _TCHAR* user,
const _TCHAR* passwd,
267 _TCHAR* buf,
size_t size)
269 memset(buf, 0, size);
270 if (_tcslen(uri) + _tcslen(user) + _tcslen(passwd) + _tcslen(_T(
"@&pwd=")) > size -1)
273 const _TCHAR* st = _tcsstr(uri, _T(
"://"));
274 _tcsncpy_s(buf, size, uri, st + 3 - uri);
275 _tcscat_s(buf, size, user);
276 _tcscat_s(buf, size, _T(
"@"));
277 _tcscat_s(buf, size, st+3);
278 _tcscat_s(buf, size, _T(
"&pwd="));
279 _tcscat_s(buf, size, passwd);
283 inline void reorder_digest(boost::uuids::detail::sha1::digest_type val)
285 for (
int i=0;i<5;++i)
287 unsigned char* p = (
unsigned char*)&val[i];
288 std::swap(*p, *(p+3));
289 std::swap(*(p+1), *(p+2));
293 inline void sha1Passwd(boost::uuids::detail::sha1::digest_type retVal ,
296 boost::uuids::detail::sha1 sha1;
298 sha1.process_bytes(src, strlen(src));
299 sha1.get_digest(retVal);
300 reorder_digest(retVal);
303 inline void cryptKeyGen(boost::uuids::detail::sha1::digest_type retVal,
304 const unsigned int* sha1Passwd,
const unsigned char *scramble)
306 boost::uuids::detail::sha1 sha1;
308 sha1.process_bytes(sha1Passwd, MYSQL_SCRAMBLE_LENGTH);
309 sha1.get_digest(retVal);
310 reorder_digest(retVal);
312 sha1.process_bytes(scramble, MYSQL_SCRAMBLE_LENGTH);
313 sha1.process_bytes(retVal, MYSQL_SCRAMBLE_LENGTH);
314 sha1.get_digest(retVal);
315 reorder_digest(retVal);
318 inline void xorCrypt(
char *retVal,
const unsigned char *src,
const unsigned char *key,
321 const unsigned char *end = src + len;
323 *retVal++ = *src++ ^ *key++;
329 inline void mysqlCryptPwd(
char *retVal,
const char* src,
const unsigned char *scramble)
331 unsigned int sha1pwd[5];
333 sha1Passwd(sha1pwd, src);
334 cryptKeyGen(key, sha1pwd, scramble);
335 xorCrypt(retVal, (
const unsigned char*)sha1pwd, (
const unsigned char*)key,
336 MYSQL_SCRAMBLE_LENGTH);
340 inline char* binToHex(
char* retVal,
const unsigned char *src,
int size)
342 const unsigned char* end = src + size;
344 for (
const unsigned char* n = src; n != end; ++n)
346 unsigned int v = (*n >> 4);
347 *p++ = (v < 10) ?
'0' + v :
'A' + (v - 10);
349 *p++ = (v < 10) ?
'0' + v :
'A' + (v - 10);
356 inline unsigned char* hexTobin(
unsigned char* retVal,
const char *src,
int size)
358 const char* end = src + size;
359 unsigned char* p = retVal;
360 for (
const char* n = src; n != end; ++n)
362 if (*n >=
'0' && *n <=
'9')
368 if (*n >=
'0' && *n <=
'9')
383 #endif // BZS_DB_PROTOCOL_TDAP_URI_H