全て クラス 名前空間 関数 変数 型定義 列挙型 列挙値 ページ
uri.h
1 #ifndef BZS_DB_PROTOCOL_TDAP_URI_H
2 #define BZS_DB_PROTOCOL_TDAP_URI_H
3 /* =================================================================
4  Copyright (C) 2015 BizStation Corp All rights reserved.
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  as published by the Free Software Foundation; either version 2
9  of the License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19  02111-1307, USA.
20  ================================================================= */
21 #include <bzs/env/compiler.h>
22 #include <bzs/env/crosscompile.h>
23 #include <tchar.h>
24 #include <boost/version.hpp>
25 
26 #if BOOST_VERSION > 103901
27 #include <boost/uuid/sha1.hpp>
28 #else
29 #include <global/boost/sha1.hpp>
30 #endif
31 #include <bzs/db/protocol/tdap/tdapcapi.h>
32 
33 namespace bzs
34 {
35 namespace db
36 {
37 namespace protocol
38 {
39 namespace tdap
40 {
41 
42 /** @cond INTERNAL */
43 
44 inline const _TCHAR* protocol(const _TCHAR* uri)
45 {
46  const _TCHAR* st = _tcsstr(uri, _T("tdap://"));
47  if (st)
48  return _T("tdap");
49  else
50  {
51  st = _tcsstr(uri, _T("btrv://"));
52  if (st)
53  return _T("btrv");
54  }
55  return NULL;
56 }
57 
58 inline void endPoint(const _TCHAR* uri,
59  _TCHAR* host, size_t hostSize,
60  _TCHAR* port, size_t portSize)
61 {
62  if (host) host[0] = 0x00;
63  if (port) port[0] = 0x00;
64  const _TCHAR* st = _tcsstr(uri, _T("@"));
65  if (st)
66  ++st;
67  else
68  {
69  st = _tcsstr(uri, _T("://"));
70  if (st) st+=3;
71  }
72  if (st)
73  {
74  const _TCHAR* en = _tcsstr(st, _T(":"));
75  if (en && en > st)
76  {
77  if (host)
78  {
79  _tcsncpy_s(host, hostSize, st, en - st);
80  host[en - st] = 0x00;
81  }
82  if (port)
83  {
84  st = en + 1;
85  en = _tcsstr(st, _T("/"));
86  if (en && en > st)
87  {
88  _tcsncpy_s(port, portSize, st, en - st);
89  port[en - st] = 0x00;
90  }
91  }
92  }
93  else if (host)
94  {
95  en = _tcsstr(st, _T("/"));
96  if (en && en > st)
97  {
98  _tcsncpy_s(host, hostSize, st, en - st);
99  host[en - st] = 0x00;
100  }
101  }
102  }
103 }
104 
105 inline const _TCHAR* hostName(const _TCHAR* uri, _TCHAR* buf, size_t size)
106 {
107  endPoint(uri, buf, size, NULL, 0);
108  return buf;
109 }
110 
111 inline const _TCHAR* port(const _TCHAR* uri, _TCHAR* buf, size_t size)
112 {
113  endPoint(uri, NULL, 0, buf, size);
114  return buf;
115 }
116 
117 inline const _TCHAR* dbname(const _TCHAR* uri, _TCHAR* buf, size_t size)
118 {
119  buf[0] = 0x00;
120  const _TCHAR* st = _tcsstr(uri, _T("://"));
121  if (st)
122  {
123  st = _tcsstr(st + 3, _T("/"));
124  if (st && *(++st))
125  {
126  const _TCHAR* en = _tcsstr(st, _T("?"));
127  if (!en)
128  en = _tcslen(st) + st;
129  if (en && en > st)
130  {
131  _tcsncpy_s(buf, size, st, en - st);
132  buf[en - st] = 0x00;
133  int n = (int)(en - st -1);
134  if (n >= 0 && buf[n] == '/') buf[n] = 0x00;
135  }
136  }
137  }
138  return buf;
139 }
140 
141 inline const _TCHAR* schemaTable(const _TCHAR* uri, _TCHAR* buf, size_t size)
142 {
143  buf[0] = 0x00;
144  _TCHAR* st = _tcsstr((_TCHAR*)uri, _T("dbfile="));
145  if (st)
146  {
147  st+= 7;
148  _tcscpy_s(buf, size, st);
149  st = _tcschr(buf, _T('&'));
150  if (st) *st = 0x00;
151  st = buf;
152  const _TCHAR* en = _tcsrchr(st, _T('.'));
153  if (en && en > st)
154  buf[en - st] = 0x00;
155  }
156  return buf;
157 }
158 
159 inline const _TCHAR* userName(const _TCHAR* uri, _TCHAR* buf, size_t size)
160 {
161  buf[0] = 0x00;
162  const _TCHAR* st = _tcsstr(uri, _T("://"));
163  if (st) st+=3;
164 
165  if (st)
166  {
167  const _TCHAR* en = _tcsstr(st, _T("@"));
168  if (en && en > st)
169  {
170  _tcsncpy_s(buf, size, st, en - st);
171  buf[en - st] = 0x00;
172  }
173  }
174  return buf;
175 }
176 
177 inline const _TCHAR* passwd(const _TCHAR* uri, _TCHAR* buf, size_t size)
178 {
179  buf[0] = 0x00;
180  _TCHAR* st = _tcsstr((_TCHAR*)uri, _T("pwd="));
181  if (st)
182  {
183  _tcscpy_s(buf, size, st+4);
184  st = _tcschr(buf, _T('&'));
185  if (st) *st = 0x00;
186  }
187  return buf;
188 }
189 
190 
191 inline const _TCHAR* connectName(const _TCHAR* uri, _TCHAR* buf, size_t size)
192 {
193  _TCHAR pwd[MAX_PATH];
194  passwd(uri, pwd, MAX_PATH);
195 
196  buf[0] = 0x00;
197  const _TCHAR* st = _tcsstr(uri, _T("://"));
198  if (st)
199  {
200  st = _tcsstr(st + 3, _T("/"));
201  _tcsncpy_s(buf, size, uri, ++st - uri);
202  buf[st - uri] = 0x00;
203  if (pwd[0])
204  {
205  _tcscat_s(buf, MAX_PATH, _T("?pwd="));
206  _tcscat_s(buf, MAX_PATH, pwd);
207  }
208 
209  }
210  return buf;
211 }
212 
213 inline _TCHAR* stripParam(const _TCHAR* uri, _TCHAR* buf, size_t size)
214 {
215  buf[0] = 0x00;
216  _TCHAR* st = _tcsstr((_TCHAR*)uri, _T("://"));
217  if (st)
218  {
219  st = _tcsstr(st + 3, _T("/"));
220  if (st && *(++st))
221  {
222  _tcscpy_s(buf, size, uri);
223  _TCHAR* en = _tcschr(buf, _T('?'));
224  if (en) *en = 0x00;
225  }
226  }
227  return buf;
228 }
229 
230 inline const _TCHAR* stripPasswd(const _TCHAR* uri, _TCHAR* buf, size_t size)
231 {
232  _tcscpy_s(buf, size, uri);
233  _TCHAR* st = _tcsstr(buf, _T("://"));
234  if (st)
235  {
236  st = _tcsstr(st, _T("pwd="));
237  if (st)
238  *(st + 4) = 0x00;
239  }
240  return buf;
241 }
242 
243 inline const _TCHAR* stripAuth(const _TCHAR* uri, _TCHAR* buf, size_t size)
244 {
245  _tcscpy_s(buf, size, uri);
246  _TCHAR* st = _tcsstr(buf, _T("://"));
247  if (st)
248  {
249  const _TCHAR* en = _tcsstr(uri, _T("@"));
250  if (en)
251  {
252  _tcscpy_s(st+3, size - (st - buf + 3), ++en);
253  _TCHAR* st2 = _tcsstr(st, _T("&pwd="));
254  if (st2)
255  *st2 = 0x00;
256  else
257  {
258  st2 = _tcsstr(st, _T("?pwd="));
259  if (st2) *(st2 + 1) = 0x00;
260  }
261  }
262  }
263  return buf;
264 }
265 
266 inline const _TCHAR* appendAuth(const _TCHAR* uri, const _TCHAR* user, const _TCHAR* passwd,
267  _TCHAR* buf, size_t size)
268 {
269  memset(buf, 0, size);
270  if (_tcslen(uri) + _tcslen(user) + _tcslen(passwd) + _tcslen(_T("@&pwd=")) > size -1)
271  return buf;
272 
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);
280  return buf;
281 }
282 
283 inline void reorder_digest(boost::uuids::detail::sha1::digest_type val)
284 {
285  for (int i=0;i<5;++i)
286  {
287  unsigned char* p = (unsigned char*)&val[i];
288  std::swap(*p, *(p+3));
289  std::swap(*(p+1), *(p+2));
290  }
291 }
292 
293 inline void sha1Passwd(boost::uuids::detail::sha1::digest_type retVal ,
294  const char* src)
295 {
296  boost::uuids::detail::sha1 sha1;
297  sha1.reset();
298  sha1.process_bytes(src, strlen(src));
299  sha1.get_digest(retVal);
300  reorder_digest(retVal);
301 }
302 
303 inline void cryptKeyGen(boost::uuids::detail::sha1::digest_type retVal,
304  const unsigned int* sha1Passwd, const unsigned char *scramble)
305 {
306  boost::uuids::detail::sha1 sha1;
307  sha1.reset();
308  sha1.process_bytes(sha1Passwd, MYSQL_SCRAMBLE_LENGTH);
309  sha1.get_digest(retVal);
310  reorder_digest(retVal);
311  sha1.reset();
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);
316 }
317 
318 inline void xorCrypt(char *retVal, const unsigned char *src, const unsigned char *key,
319  unsigned int len)
320 {
321  const unsigned char *end = src + len;
322  while (src < end)
323  *retVal++ = *src++ ^ *key++;
324 }
325 
326 /* scramble size = MYSQL_SCRAMBLE_LENGTH only
327  retVal size = MYSQL_SCRAMBLE_LENGTH
328 */
329 inline void mysqlCryptPwd(char *retVal, const char* src, const unsigned char *scramble)
330 {
331  unsigned int sha1pwd[5];
332  unsigned int key[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);
337 }
338 
339 // binary to ascii return bytes = size * 2
340 inline char* binToHex(char* retVal, const unsigned char *src, int size)
341 {
342  const unsigned char* end = src + size;
343  char* p = retVal;
344  for (const unsigned char* n = src; n != end; ++n)
345  {
346  unsigned int v = (*n >> 4);
347  *p++ = (v < 10) ? '0' + v : 'A' + (v - 10);
348  v = *n & 0x0F;
349  *p++ = (v < 10) ? '0' + v : 'A' + (v - 10);
350  }
351  *p = 0x00;
352  return retVal;
353 }
354 
355 // ascii to binary return bytes = size / 2
356 inline unsigned char* hexTobin(unsigned char* retVal, const char *src, int size)
357 {
358  const char* end = src + size;
359  unsigned char* p = retVal;
360  for (const char* n = src; n != end; ++n)
361  {
362  if (*n >= '0' && *n <= '9')
363  *p = *n - '0';
364  else
365  *p = (*n - 'A')+10;
366  ++n;
367  *p = *p << 4;
368  if (*n >= '0' && *n <= '9')
369  *p += *n - '0';
370  else
371  *p += (*n - 'A')+10;
372  ++p;
373  }
374  return retVal;
375 }
376 
377 /** @endcond */
378 
379 } // namespace tdap
380 } // namespace protocol
381 } // namespace db
382 } // namespace bzs
383 #endif // BZS_DB_PROTOCOL_TDAP_URI_H

Transactd SDK 2018年07月31日(火) 19時40分24秒 doxygen