全て クラス 名前空間 関数 変数 型定義 列挙型 列挙値 フレンド ページ
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 inline const _TCHAR* protocol(const _TCHAR* uri)
43 {
44  const _TCHAR* st = _tcsstr(uri, _T("tdap://"));
45  if (st)
46  return _T("tdap");
47  else
48  {
49  st = _tcsstr(uri, _T("btrv://"));
50  return _T("btrv");
51  }
52 }
53 
54 inline void endPoint(const _TCHAR* uri,
55  _TCHAR* host, size_t hostSize,
56  _TCHAR* port, size_t portSize)
57 {
58  if (host) host[0] = 0x00;
59  if (port) port[0] = 0x00;
60  const _TCHAR* st = _tcsstr(uri, _T("@"));
61  if (st)
62  ++st;
63  else
64  {
65  st = _tcsstr(uri, _T("://"));
66  if (st) st+=3;
67  }
68  if (st)
69  {
70  const _TCHAR* en = _tcsstr(st, _T(":"));
71  if (en && en > st)
72  {
73  if (host)
74  {
75  _tcsncpy_s(host, hostSize, st, en - st);
76  host[en - st] = 0x00;
77  }
78  if (port)
79  {
80  st = en + 1;
81  en = _tcsstr(st, _T("/"));
82  if (en && en > st)
83  {
84  _tcsncpy_s(port, portSize, st, en - st);
85  port[en - st] = 0x00;
86  }
87  }
88  }
89  else if (host)
90  {
91  en = _tcsstr(st, _T("/"));
92  if (en && en > st)
93  {
94  _tcsncpy_s(host, hostSize, st, en - st);
95  host[en - st] = 0x00;
96  }
97  }
98  }
99 }
100 
101 inline const _TCHAR* hostName(const _TCHAR* uri, _TCHAR* buf, size_t size)
102 {
103  endPoint(uri, buf, size, NULL, 0);
104  return buf;
105 }
106 
107 inline const _TCHAR* port(const _TCHAR* uri, _TCHAR* buf, size_t size)
108 {
109  endPoint(uri, NULL, 0, buf, size);
110  return buf;
111 }
112 
113 inline const _TCHAR* dbname(const _TCHAR* uri, _TCHAR* buf, size_t size)
114 {
115  buf[0] = 0x00;
116  const _TCHAR* st = _tcsstr(uri, _T("://"));
117  if (st)
118  {
119  st = _tcsstr(st + 3, _T("/"));
120  if (st)
121  {
122  const _TCHAR* en = _tcsstr(st + 1, _T("?"));
123  if (en && en > st)
124  {
125  _tcsncpy_s(buf, size, st + 1, en - (st + 1));
126  buf[en - (st + 1)] = 0x00;
127  }
128  }
129  }
130  return buf;
131 }
132 
133 inline const _TCHAR* schemaTable(const _TCHAR* uri, _TCHAR* buf, size_t size)
134 {
135  buf[0] = 0x00;
136  const _TCHAR* st = _tcsstr(uri, _T("dbfile="));
137  if (st)
138  {
139  st+= 7;
140  const _TCHAR* en = _tcsrchr(uri, _T('.'));
141  if (en && en > st)
142  {
143  _tcsncpy_s(buf, size, st, en - st);
144  buf[en - st] = 0x00;
145  }else if (_tcsstr(st, TRANSACTD_SCHEMANAME))
146  _tcscpy_s(buf, size, TRANSACTD_SCHEMANAME);
147  }
148  return buf;
149 }
150 
151 inline const _TCHAR* userName(const _TCHAR* uri, _TCHAR* buf, size_t size)
152 {
153  buf[0] = 0x00;
154  const _TCHAR* st = _tcsstr(uri, _T("://"));
155  if (st) st+=3;
156 
157  if (st)
158  {
159  const _TCHAR* en = _tcsstr(st, _T("@"));
160  if (en && en > st)
161  {
162  _tcsncpy_s(buf, size, st, en - st);
163  buf[en - st] = 0x00;
164  }
165  }
166  return buf;
167 }
168 
169 inline const _TCHAR* passwd(const _TCHAR* uri, _TCHAR* buf, size_t size)
170 {
171  buf[0] = 0x00;
172  const _TCHAR* st = _tcsstr(uri, _T("pwd="));
173  if (st)
174  _tcscpy_s(buf, size, st+4);
175  return buf;
176 }
177 
178 inline const _TCHAR* stripAuth(const _TCHAR* uri, _TCHAR* buf, size_t size)
179 {
180  _tcscpy_s(buf, size, uri);
181  _TCHAR* st = _tcsstr(buf, _T("://"));
182  if (st)
183  {
184  const _TCHAR* en = _tcsstr(uri, _T("@"));
185  if (en)
186  {
187  _tcscpy_s(st+3, size, ++en);
188  _TCHAR* st2 = _tcsstr(st, _T("&pwd="));
189  if (st2)
190  *st2 = 0x00;
191  else
192  {
193  st2 = _tcsstr(st, _T("?pwd="));
194  if (st2) *st2 = 0x00;
195  }
196  }
197  }
198  return buf;
199 }
200 
201 inline const _TCHAR* stripPasswd(const _TCHAR* uri, _TCHAR* buf, size_t size)
202 {
203  _tcscpy_s(buf, size, uri);
204  _TCHAR* st = _tcsstr(buf, _T("://"));
205  if (st)
206  {
207  st = _tcsstr(st, _T("pwd="));
208  if (st)
209  *(st + 4) = 0x00;
210  }
211  return buf;
212 }
213 
214 inline const _TCHAR* stripPasswdParam(const _TCHAR* uri, _TCHAR* buf, size_t size)
215 {
216  _tcscpy_s(buf, size, uri);
217  _TCHAR* st = _tcsstr(buf, _T("://"));
218  if (st)
219  {
220  _TCHAR* st2 = _tcsstr(st, _T("&pwd="));
221  if (st2)
222  *(st2) = 0x00;
223  else
224  {
225  st2 = _tcsstr(st, _T("?pwd="));
226  if (st2)
227  *(st2) = 0x00;
228  }
229  }
230  return buf;
231 }
232 
233 inline void reorder_digest(boost::uuids::detail::sha1::digest_type val)
234 {
235  for (int i=0;i<5;++i)
236  {
237  unsigned char* p = (unsigned char*)&val[i];
238  std::swap(*p, *(p+3));
239  std::swap(*(p+1), *(p+2));
240  }
241 }
242 
243 inline void sha1Passwd(boost::uuids::detail::sha1::digest_type retVal ,
244  const char* src)
245 {
246  boost::uuids::detail::sha1 sha1;
247  sha1.reset();
248  sha1.process_bytes(src, strlen(src));
249  sha1.get_digest(retVal);
250  reorder_digest(retVal);
251 }
252 
253 inline void cryptKeyGen(boost::uuids::detail::sha1::digest_type retVal,
254  const unsigned int* sha1Passwd, const unsigned char *scramble)
255 {
256  boost::uuids::detail::sha1 sha1;
257  sha1.reset();
258  sha1.process_bytes(sha1Passwd, MYSQL_SCRAMBLE_LENGTH);
259  sha1.get_digest(retVal);
260  reorder_digest(retVal);
261  sha1.reset();
262  sha1.process_bytes(scramble, MYSQL_SCRAMBLE_LENGTH);
263  sha1.process_bytes(retVal, MYSQL_SCRAMBLE_LENGTH);
264  sha1.get_digest(retVal);
265  reorder_digest(retVal);
266 }
267 
268 inline void xorCrypt(char *retVal, const unsigned char *src, const unsigned char *key,
269  unsigned int len)
270 {
271  const unsigned char *end = src + len;
272  while (src < end)
273  *retVal++ = *src++ ^ *key++;
274 }
275 
276 /* scramble size = MYSQL_SCRAMBLE_LENGTH only
277  retVal size = MYSQL_SCRAMBLE_LENGTH
278 */
279 inline void mysqlCryptPwd(char *retVal, const char* src, const unsigned char *scramble)
280 {
281  unsigned int sha1pwd[5];
282  unsigned int key[5];
283  sha1Passwd(sha1pwd, src);
284  cryptKeyGen(key, sha1pwd, scramble);
285  xorCrypt(retVal, (const unsigned char*)sha1pwd, (const unsigned char*)key,
286  MYSQL_SCRAMBLE_LENGTH);
287 }
288 
289 // binary to ascii return bytes = size * 2
290 inline char* binToHex(char* retVal, const unsigned char *src, int size)
291 {
292  const unsigned char* end = src + size;
293  char* p = retVal;
294  for (const unsigned char* n = src; n != end; ++n)
295  {
296  unsigned int v = (*n >> 4);
297  *p++ = (v < 10) ? '0' + v : 'A' + (v - 10);
298  v = *n & 0x0F;
299  *p++ = (v < 10) ? '0' + v : 'A' + (v - 10);
300  }
301  *p = 0x00;
302  return retVal;
303 }
304 
305 // ascii to binary return bytes = size / 2
306 inline unsigned char* hexTobin(unsigned char* retVal, const char *src, int size)
307 {
308  const char* end = src + size;
309  unsigned char* p = retVal;
310  for (const char* n = src; n != end; ++n)
311  {
312  if (*n >= '0' && *n <= '9')
313  *p = *n - '0';
314  else
315  *p = (*n - 'A')+10;
316  ++n;
317  *p = *p << 4;
318  if (*n >= '0' && *n <= '9')
319  *p += *n - '0';
320  else
321  *p += (*n - 'A')+10;
322  ++p;
323  }
324  return retVal;
325 }
326 
327 
328 } // namespace tdap
329 } // namespace protocol
330 } // namespace db
331 } // namespace bzs
332 #endif // BZS_DB_PROTOCOL_TDAP_URI_H
const _TCHAR * protocol(const _TCHAR *uri)
Definition: uri.h:42
const _TCHAR * port(const _TCHAR *uri, _TCHAR *buf, size_t size)
Definition: uri.h:107
char * binToHex(char *retVal, const unsigned char *src, int size)
Definition: uri.h:290
const _TCHAR * passwd(const _TCHAR *uri, _TCHAR *buf, size_t size)
Definition: uri.h:169
void mysqlCryptPwd(char *retVal, const char *src, const unsigned char *scramble)
Definition: uri.h:279
const _TCHAR * dbname(const _TCHAR *uri, _TCHAR *buf, size_t size)
Definition: uri.h:113
void reorder_digest(boost::uuids::detail::sha1::digest_type val)
Definition: uri.h:233
void endPoint(const _TCHAR *uri, _TCHAR *host, size_t hostSize, _TCHAR *port, size_t portSize)
Definition: uri.h:54
unsigned char * hexTobin(unsigned char *retVal, const char *src, int size)
Definition: uri.h:306
const _TCHAR * stripPasswd(const _TCHAR *uri, _TCHAR *buf, size_t size)
Definition: uri.h:201
void sha1Passwd(boost::uuids::detail::sha1::digest_type retVal, const char *src)
Definition: uri.h:243
const _TCHAR * schemaTable(const _TCHAR *uri, _TCHAR *buf, size_t size)
Definition: uri.h:133
const _TCHAR * stripPasswdParam(const _TCHAR *uri, _TCHAR *buf, size_t size)
Definition: uri.h:214
const _TCHAR * stripAuth(const _TCHAR *uri, _TCHAR *buf, size_t size)
Definition: uri.h:178
void xorCrypt(char *retVal, const unsigned char *src, const unsigned char *key, unsigned int len)
Definition: uri.h:268
void cryptKeyGen(boost::uuids::detail::sha1::digest_type retVal, const unsigned int *sha1Passwd, const unsigned char *scramble)
Definition: uri.h:253
const _TCHAR * userName(const _TCHAR *uri, _TCHAR *buf, size_t size)
Definition: uri.h:151
const _TCHAR * hostName(const _TCHAR *uri, _TCHAR *buf, size_t size)
Definition: uri.h:101

Transactd SDK 2015年09月08日(火) 19時13分36秒 doxygen