Merge lp:~psiphon-inc/psiphon/psiphon-1.6 into lp:psiphon
- psiphon-1.6
- Merge into trunk
Proposed by
مندس جرثومي
Status: | Needs review |
---|---|
Proposed branch: | lp:~psiphon-inc/psiphon/psiphon-1.6 |
Merge into: | lp:psiphon |
Diff against target: |
30229 lines (+29308/-0) 170 files modified
appWeb/CONFIGURE_DEBUG.TXT (+5/-0) appWeb/CONFIGURE_RELEASE.TXT (+5/-0) appWeb/openSslModule.cpp (+896/-0) appWeb/thread.cpp (+768/-0) appweb.cnf (+27/-0) bake-w32.bat (+1/-0) changes.txt (+99/-0) common/CppSQLite3.cpp (+1383/-0) common/CppSQLite3.h (+270/-0) common/SHA1.cpp (+282/-0) common/SHA1.h (+150/-0) common/SetupInfo.cpp (+512/-0) common/SetupInfo.h (+110/-0) common/User.cpp (+598/-0) common/User.h (+94/-0) common/bookmarks.cpp (+213/-0) common/bookmarks.h (+30/-0) common/convert.cpp (+176/-0) common/convert.h (+47/-0) common/cookies.cpp (+947/-0) common/cookies.h (+73/-0) common/logDetails.cpp (+252/-0) common/logDetails.h (+70/-0) common/parsedUrl.cpp (+312/-0) common/parsedUrl.h (+78/-0) common/sessions.cpp (+134/-0) common/sessions.h (+29/-0) common/sqlite3.h (+1161/-0) common/strfunc.cpp (+109/-0) common/strfunc.h (+39/-0) common/timefunc.h (+21/-0) common/tracer.cpp (+89/-0) common/tracer.h (+39/-0) html/en/errors/403.html (+15/-0) html/en/errors/404.html (+15/-0) html/en/errors/504.html (+15/-0) html/en/index.tpl.html (+1/-0) html/en/language.ini (+1/-0) html/en/login.tpl.html (+62/-0) html/en/login_err_db_insert_sess.tpl.html (+62/-0) html/en/login_err_db_update_sess.tpl.html (+62/-0) html/en/login_err_no_match.tpl.html (+62/-0) html/en/login_err_pwd.tpl.html (+62/-0) html/en/login_err_user.tpl.html (+62/-0) html/en/main.tpl.html (+15/-0) html/en/reset_pwd.tpl.html (+66/-0) html/en/reset_pwd_err_conf_fail_pwd.tpl.html (+66/-0) html/en/reset_pwd_err_conf_pwd.tpl.html (+66/-0) html/en/reset_pwd_err_cur_pwd.tpl.html (+66/-0) html/en/reset_pwd_err_db_update.tpl.html (+66/-0) html/en/reset_pwd_err_new_pwd.tpl.html (+66/-0) html/en/reset_pwd_err_no_match.tpl.html (+66/-0) html/en/reset_pwd_err_user.tpl.html (+66/-0) html/en/reset_pwd_success.tpl.html (+27/-0) html/en/topbar.tpl.html (+511/-0) html/fr/errors/403.html (+15/-0) html/fr/errors/404.html (+15/-0) html/fr/errors/504.html (+15/-0) html/fr/index.tpl.html (+1/-0) html/fr/language.ini (+1/-0) html/fr/login.tpl.html (+62/-0) html/fr/login_err_db_insert_sess.tpl.html (+62/-0) html/fr/login_err_db_update_sess.tpl.html (+62/-0) html/fr/login_err_no_match.tpl.html (+62/-0) html/fr/login_err_pwd.tpl.html (+62/-0) html/fr/login_err_user.tpl.html (+62/-0) html/fr/main.tpl.html (+15/-0) html/fr/reset_pwd.tpl.html (+66/-0) html/fr/reset_pwd_err_conf_fail_pwd.tpl.html (+66/-0) html/fr/reset_pwd_err_conf_pwd.tpl.html (+66/-0) html/fr/reset_pwd_err_cur_pwd.tpl.html (+66/-0) html/fr/reset_pwd_err_db_update.tpl.html (+66/-0) html/fr/reset_pwd_err_new_pwd.tpl.html (+66/-0) html/fr/reset_pwd_err_no_match.tpl.html (+66/-0) html/fr/reset_pwd_err_user.tpl.html (+66/-0) html/fr/reset_pwd_success.tpl.html (+27/-0) html/fr/topbar.tpl.html (+511/-0) html/fr/topbar_text.tpl.html (+461/-0) html/ru/language.ini (+1/-0) html/ru/login.tpl.html (+62/-0) html/ru/login_err_db_insert_sess.tpl.html (+62/-0) html/ru/login_err_db_update_sess.tpl.html (+62/-0) html/ru/login_err_no_match.tpl.html (+62/-0) html/ru/login_err_pwd.tpl.html (+62/-0) html/ru/login_err_user.tpl.html (+62/-0) html/ru/reset_pwd.tpl.html (+66/-0) html/ru/reset_pwd_err_conf_fail_pwd.tpl.html (+66/-0) html/ru/reset_pwd_err_conf_pwd.tpl.html (+66/-0) html/ru/reset_pwd_err_cur_pwd.tpl.html (+66/-0) html/ru/reset_pwd_err_db_update.tpl.html (+66/-0) html/ru/reset_pwd_err_new_pwd.tpl.html (+67/-0) html/ru/reset_pwd_err_no_match.tpl.html (+66/-0) html/ru/reset_pwd_err_user.tpl.html (+66/-0) html/ru/reset_pwd_success.tpl.html (+27/-0) html/ru/topbar.tpl.html (+517/-0) install-ubuntu.txt (+98/-0) install-w32.txt (+111/-0) installer.aip (+221/-0) mime.types (+110/-0) psiphon.bkl (+296/-0) psiphonHandler/CSSFlexLexer.h (+24/-0) psiphonHandler/CSSLexer.l (+61/-0) psiphonHandler/FlexLexer.h (+202/-0) psiphonHandler/HTMLFlexLexer.h (+24/-0) psiphonHandler/HTMLlexer.l (+315/-0) psiphonHandler/lex.CSS.cpp (+1451/-0) psiphonHandler/lex.HTML.cpp (+1852/-0) psiphonHandler/myclient.cpp (+1084/-0) psiphonHandler/myclient.h (+239/-0) psiphonHandler/psiphonContFilter.cpp (+394/-0) psiphonHandler/psiphonContFilter.h (+84/-0) psiphonHandler/psiphonHandler.cpp (+1797/-0) psiphonHandler/psiphonHandler.def (+3/-0) psiphonHandler/psiphonHandler.h (+204/-0) psiphonHandler/psiphonUrl.cpp (+293/-0) psiphonHandler/psiphonUrl.h (+49/-0) psiphonHandler/psiphonUtils.cpp (+390/-0) psiphonHandler/psiphonUtils.h (+55/-0) psiphonHandler/tokensDefs.h (+48/-0) psiphonPanel/CertDlg.cpp (+309/-0) psiphonPanel/CertDlg.h (+64/-0) psiphonPanel/CodeCountry.cpp (+266/-0) psiphonPanel/CodeCountry.h (+30/-0) psiphonPanel/DownloadEvent.cpp (+96/-0) psiphonPanel/DownloadEvent.h (+168/-0) psiphonPanel/DownloadFile.cpp (+167/-0) psiphonPanel/DownloadFile.h (+80/-0) psiphonPanel/GetIP.cpp (+101/-0) psiphonPanel/GetIP.h (+23/-0) psiphonPanel/LinkDlg.cpp (+302/-0) psiphonPanel/LinkDlg.h (+63/-0) psiphonPanel/ListCtrlSort.cpp (+88/-0) psiphonPanel/ListCtrlSort.h (+74/-0) psiphonPanel/LogDlg.cpp (+223/-0) psiphonPanel/LogDlg.h (+61/-0) psiphonPanel/PrefDlg.cpp (+121/-0) psiphonPanel/PrefDlg.h (+45/-0) psiphonPanel/PsiphonApp.cpp (+99/-0) psiphonPanel/PsiphonApp.h (+43/-0) psiphonPanel/PsiphonDlg.cpp (+574/-0) psiphonPanel/PsiphonDlg.h (+99/-0) psiphonPanel/PsiphonLog.cpp (+51/-0) psiphonPanel/PsiphonLog.h (+39/-0) psiphonPanel/PsiphonNameDlg.cpp (+133/-0) psiphonPanel/PsiphonNameDlg.h (+42/-0) psiphonPanel/Server.cpp (+215/-0) psiphonPanel/Server.h (+80/-0) psiphonPanel/SetupDlg.cpp (+123/-0) psiphonPanel/SetupDlg.h (+61/-0) psiphonPanel/UpdateDlg.cpp (+292/-0) psiphonPanel/UpdateDlg.h (+59/-0) psiphonPanel/UserDetailsDlg.cpp (+230/-0) psiphonPanel/UserDetailsDlg.h (+46/-0) psiphonPanel/UserDlg.cpp (+336/-0) psiphonPanel/UserDlg.h (+69/-0) psiphonPanel/cert.cpp (+102/-0) psiphonPanel/cert.h (+22/-0) psiphonPanel/psiphonoff.xpm (+189/-0) psiphonPanel/psiphonon.xpm (+189/-0) psiphonPanel/resources.h (+33/-0) psiphonPanel/sessionAccess.cpp (+172/-0) psiphonPanel/sessionAccess.h (+35/-0) psiphonPanel/stdwx.cpp (+19/-0) psiphonPanel/stdwx.h (+53/-0) psiphonPanel/taskbaricon.cpp (+91/-0) psiphonPanel/taskbaricon.h (+39/-0) psiphonPanel/update.cpp (+79/-0) psiphonPanel/update.h (+20/-0) psiphonPanel/wxPsiphon.rc (+23/-0) userlicense.txt (+39/-0) |
To merge this branch: | bzr merge lp:~psiphon-inc/psiphon/psiphon-1.6 |
Related bugs: | |
Related blueprints: |
Psiphon 2 Design Overview
(Undefined)
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Psiphon Inc. | Pending | ||
Review via email: mp+83096@code.launchpad.net |
Commit message
Description of the change
To post a comment you must log in.
Unmerged revisions
- 1. By jamyang <email address hidden>
-
psiphon-1.6
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === added file '.DS_Store' | |||
2 | 0 | Binary files .DS_Store 1970-01-01 00:00:00 +0000 and .DS_Store 2011-11-23 01:01:25 +0000 differ | 0 | Binary files .DS_Store 1970-01-01 00:00:00 +0000 and .DS_Store 2011-11-23 01:01:25 +0000 differ |
3 | === added directory 'appWeb' | |||
4 | === added file 'appWeb/CONFIGURE_DEBUG.TXT' | |||
5 | --- appWeb/CONFIGURE_DEBUG.TXT 1970-01-01 00:00:00 +0000 | |||
6 | +++ appWeb/CONFIGURE_DEBUG.TXT 2011-11-23 01:01:25 +0000 | |||
7 | @@ -0,0 +1,5 @@ | |||
8 | 1 | ./configure --reset | ||
9 | 2 | ./configure --disable-samples --without-upload --without-xdb --disable-test --type=DEBUG \ | ||
10 | 3 | --with-openssl=loadable \ | ||
11 | 4 | --with-openssl-dir=../../openssl \ | ||
12 | 5 | --with-openssl-libs="ssl crypto" | ||
13 | 0 | \ No newline at end of file | 6 | \ No newline at end of file |
14 | 1 | 7 | ||
15 | === added file 'appWeb/CONFIGURE_RELEASE.TXT' | |||
16 | --- appWeb/CONFIGURE_RELEASE.TXT 1970-01-01 00:00:00 +0000 | |||
17 | +++ appWeb/CONFIGURE_RELEASE.TXT 2011-11-23 01:01:25 +0000 | |||
18 | @@ -0,0 +1,5 @@ | |||
19 | 1 | ./configure --reset | ||
20 | 2 | ./configure --disable-samples --without-upload --without-xdb --disable-test --type=RELEASE \ | ||
21 | 3 | --with-openssl=loadable \ | ||
22 | 4 | --with-openssl-dir=../../openssl \ | ||
23 | 5 | --with-openssl-libs="ssl crypto" | ||
24 | 0 | \ No newline at end of file | 6 | \ No newline at end of file |
25 | 1 | 7 | ||
26 | === added file 'appWeb/openSslModule.cpp' | |||
27 | --- appWeb/openSslModule.cpp 1970-01-01 00:00:00 +0000 | |||
28 | +++ appWeb/openSslModule.cpp 2011-11-23 01:01:25 +0000 | |||
29 | @@ -0,0 +1,896 @@ | |||
30 | 1 | /// | ||
31 | 2 | /// @file openSslModule.cpp | ||
32 | 3 | /// @brief Support for secure sockets via OpenSSL | ||
33 | 4 | /// @overview This module integrates support for OpenSSL into AppWeb | ||
34 | 5 | /// via a dynamically loadable module. | ||
35 | 6 | // | ||
36 | 7 | /////////////////////////////////// Copyright ////////////////////////////////// | ||
37 | 8 | // | ||
38 | 9 | // @copy default | ||
39 | 10 | // | ||
40 | 11 | // Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved. | ||
41 | 12 | // Copyright (c) Michael O'Brien, 1994-2006. All Rights Reserved. | ||
42 | 13 | // | ||
43 | 14 | // This software is distributed under commercial and open source licenses. | ||
44 | 15 | // You may use the GPL open source license described below or you may acquire | ||
45 | 16 | // a commercial license from Mbedthis Software. You agree to be fully bound | ||
46 | 17 | // by the terms of either license. Consult the LICENSE.TXT distributed with | ||
47 | 18 | // this software for full details. | ||
48 | 19 | // | ||
49 | 20 | // This software is open source; you can redistribute it and/or modify it | ||
50 | 21 | // under the terms of the GNU General Public License as published by the | ||
51 | 22 | // Free Software Foundation; either version 2 of the License, or (at your | ||
52 | 23 | // option) any later version. See the GNU General Public License for more | ||
53 | 24 | // details at: http://www.mbedthis.com/downloads/gplLicense.html | ||
54 | 25 | // | ||
55 | 26 | // This program is distributed WITHOUT ANY WARRANTY; without even the | ||
56 | 27 | // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
57 | 28 | // | ||
58 | 29 | // This GPL license does NOT permit incorporating this software into | ||
59 | 30 | // proprietary programs. If you are unable to comply with the GPL, you must | ||
60 | 31 | // acquire a commercial license to use this software. Commercial licenses | ||
61 | 32 | // for this software and support services are available from Mbedthis | ||
62 | 33 | // Software at http://www.mbedthis.com | ||
63 | 34 | // | ||
64 | 35 | // @end | ||
65 | 36 | // | ||
66 | 37 | ////////////////////////////////// Includes //////////////////////////////////// | ||
67 | 38 | |||
68 | 39 | #include "openSslModule.h" | ||
69 | 40 | |||
70 | 41 | //////////////////////////////////// Locals //////////////////////////////////// | ||
71 | 42 | #if BLD_FEATURE_OPENSSL_MODULE | ||
72 | 43 | |||
73 | 44 | #include "openSslDh.h" | ||
74 | 45 | |||
75 | 46 | #if BLD_FEATURE_MULTITHREAD | ||
76 | 47 | static MprMutex **locks; | ||
77 | 48 | static int numLocks; | ||
78 | 49 | #endif | ||
79 | 50 | |||
80 | 51 | ////////////////////////////// Forward Declarations //////////////////////////// | ||
81 | 52 | |||
82 | 53 | #if BLD_FEATURE_MULTITHREAD | ||
83 | 54 | static DynLock *sslCreateDynLock(const char *file, int line); | ||
84 | 55 | static void sslDynLock(int mode, DynLock *dl, const char *file, int line); | ||
85 | 56 | static void sslDestroyDynLock(DynLock *dl, const char *file, int line); | ||
86 | 57 | static void sslStaticLock(int mode, int n, const char *file, int line); | ||
87 | 58 | static ulong sslThreadId(); | ||
88 | 59 | #endif | ||
89 | 60 | |||
90 | 61 | static int configureCertificates(SSL_CTX *ctx, char *key, char *cert); | ||
91 | 62 | static RSA *rsaCallback(SSL *ssl, int isExport, int keyLength); | ||
92 | 63 | static DH *dhCallback(SSL *ssl, int isExport, int keyLength); | ||
93 | 64 | static int verifyX509Certificate(int ok, X509_STORE_CTX *ctx); | ||
94 | 65 | |||
95 | 66 | //////////////////////////////////// Locals //////////////////////////////////// | ||
96 | 67 | |||
97 | 68 | static MaOpenSslModule *openSslModule; | ||
98 | 69 | static MaOpenSslProvider *openSslProvider; | ||
99 | 70 | |||
100 | 71 | //////////////////////////////////////////////////////////////////////////////// | ||
101 | 72 | /////////////////////////////// MaOpenSslModule //////////////////////////////// | ||
102 | 73 | //////////////////////////////////////////////////////////////////////////////// | ||
103 | 74 | |||
104 | 75 | int mprOpenSslInit(void *handle) | ||
105 | 76 | { | ||
106 | 77 | if (maGetHttp() == 0) { | ||
107 | 78 | return MPR_ERR_NOT_INITIALIZED; | ||
108 | 79 | } | ||
109 | 80 | new MaOpenSslModule(handle); | ||
110 | 81 | return 0; | ||
111 | 82 | } | ||
112 | 83 | |||
113 | 84 | //////////////////////////////////////////////////////////////////////////////// | ||
114 | 85 | |||
115 | 86 | MaOpenSslModule::MaOpenSslModule(void *handle) : MaModule("openSsl", handle) | ||
116 | 87 | { | ||
117 | 88 | openSslModule = this; | ||
118 | 89 | openSslProvider = new MaOpenSslProvider("openSsl"); | ||
119 | 90 | } | ||
120 | 91 | |||
121 | 92 | //////////////////////////////////////////////////////////////////////////////// | ||
122 | 93 | |||
123 | 94 | MaOpenSslModule::~MaOpenSslModule() | ||
124 | 95 | { | ||
125 | 96 | delete openSslProvider; | ||
126 | 97 | } | ||
127 | 98 | |||
128 | 99 | //////////////////////////////////////////////////////////////////////////////// | ||
129 | 100 | // | ||
130 | 101 | // This is called from Http::start after all the modules are loaded. | ||
131 | 102 | // The code here is global for this handler and is not required per handler | ||
132 | 103 | // instance. | ||
133 | 104 | // | ||
134 | 105 | int MaOpenSslModule::start() | ||
135 | 106 | { | ||
136 | 107 | RandBuf randBuf; | ||
137 | 108 | static int once = 0; | ||
138 | 109 | |||
139 | 110 | // | ||
140 | 111 | // Depending on the order in the configuration file, we will get called | ||
141 | 112 | // by the module mechanism and by OpenSslConfig::start(). But we only | ||
142 | 113 | // want to run once. | ||
143 | 114 | // | ||
144 | 115 | if (once++ > 0) { | ||
145 | 116 | return 0; | ||
146 | 117 | } | ||
147 | 118 | randBuf.pid = getpid(); | ||
148 | 119 | randBuf.time = time(0); | ||
149 | 120 | randBuf.msec = mprGetTime(0); | ||
150 | 121 | RAND_seed((void*) &randBuf, sizeof(randBuf)); | ||
151 | 122 | |||
152 | 123 | #if SOLARIS || LINUX || MACOSX | ||
153 | 124 | mprLog(6, "OpenSsl: Before calling RAND_load_file\n"); | ||
154 | 125 | RAND_load_file("/dev/urandom", 256); | ||
155 | 126 | mprLog(6, "OpenSsl: After calling RAND_load_file\n"); | ||
156 | 127 | #endif | ||
157 | 128 | |||
158 | 129 | #if BLD_FEATURE_MULTITHREAD | ||
159 | 130 | numLocks = CRYPTO_num_locks(); | ||
160 | 131 | locks = (MprMutex**) mprMalloc(numLocks * sizeof(MprMutex*)); | ||
161 | 132 | for (int i = 0; i < numLocks; i++) { | ||
162 | 133 | locks[i] = new MprMutex(); | ||
163 | 134 | } | ||
164 | 135 | CRYPTO_set_id_callback(sslThreadId); | ||
165 | 136 | CRYPTO_set_locking_callback(sslStaticLock); | ||
166 | 137 | |||
167 | 138 | CRYPTO_set_dynlock_create_callback(sslCreateDynLock); | ||
168 | 139 | CRYPTO_set_dynlock_destroy_callback(sslDestroyDynLock); | ||
169 | 140 | CRYPTO_set_dynlock_lock_callback(sslDynLock); | ||
170 | 141 | #endif | ||
171 | 142 | |||
172 | 143 | #if WIN | ||
173 | 144 | // _fmode=_O_BINARY; | ||
174 | 145 | // CRYPTO_malloc_init(); | ||
175 | 146 | // SSLC_add_all_algorithms(); | ||
176 | 147 | #else | ||
177 | 148 | OpenSSL_add_all_algorithms(); | ||
178 | 149 | #endif | ||
179 | 150 | |||
180 | 151 | SSL_library_init(); | ||
181 | 152 | return 0; | ||
182 | 153 | } | ||
183 | 154 | |||
184 | 155 | //////////////////////////////////////////////////////////////////////////////// | ||
185 | 156 | |||
186 | 157 | void MaOpenSslModule::stop() | ||
187 | 158 | { | ||
188 | 159 | #if BLD_FEATURE_MULTITHREAD | ||
189 | 160 | int i; | ||
190 | 161 | |||
191 | 162 | if (locks) { | ||
192 | 163 | for (i = 0; i < numLocks; i++) { | ||
193 | 164 | delete locks[i]; | ||
194 | 165 | } | ||
195 | 166 | mprFree(locks); | ||
196 | 167 | locks = 0; | ||
197 | 168 | numLocks = 0; | ||
198 | 169 | |||
199 | 170 | CRYPTO_set_id_callback(0); | ||
200 | 171 | CRYPTO_set_locking_callback(0); | ||
201 | 172 | |||
202 | 173 | CRYPTO_set_dynlock_create_callback(0); | ||
203 | 174 | CRYPTO_set_dynlock_destroy_callback(0); | ||
204 | 175 | CRYPTO_set_dynlock_lock_callback(0); | ||
205 | 176 | } | ||
206 | 177 | #endif | ||
207 | 178 | } | ||
208 | 179 | |||
209 | 180 | //////////////////////////////////////////////////////////////////////////////// | ||
210 | 181 | /////////////////////////////// MaOpenSslProvider ////////////////////////////// | ||
211 | 182 | //////////////////////////////////////////////////////////////////////////////// | ||
212 | 183 | // | ||
213 | 184 | // The SSL provider class implements and decouples the interface between | ||
214 | 185 | // AppWeb and any SSL stack. | ||
215 | 186 | // | ||
216 | 187 | |||
217 | 188 | MaOpenSslProvider::MaOpenSslProvider(char *name) : MaSslProvider(name) | ||
218 | 189 | { | ||
219 | 190 | } | ||
220 | 191 | |||
221 | 192 | //////////////////////////////////////////////////////////////////////////////// | ||
222 | 193 | |||
223 | 194 | MaOpenSslProvider::~MaOpenSslProvider() | ||
224 | 195 | { | ||
225 | 196 | } | ||
226 | 197 | |||
227 | 198 | //////////////////////////////////////////////////////////////////////////////// | ||
228 | 199 | |||
229 | 200 | MaSslConfig *MaOpenSslProvider::newConfig(MaHost *host) | ||
230 | 201 | { | ||
231 | 202 | return new MaOpenSslConfig(host); | ||
232 | 203 | } | ||
233 | 204 | |||
234 | 205 | //////////////////////////////////////////////////////////////////////////////// | ||
235 | 206 | //////////////////////////////// MaOpenSslConfig /////////////////////////////// | ||
236 | 207 | //////////////////////////////////////////////////////////////////////////////// | ||
237 | 208 | // | ||
238 | 209 | // An instance is created for each SSL server | ||
239 | 210 | // | ||
240 | 211 | |||
241 | 212 | MaOpenSslConfig::MaOpenSslConfig(MaHost *host) : MaSslConfig(host) | ||
242 | 213 | { | ||
243 | 214 | context = 0; | ||
244 | 215 | dhKey512 = 0; | ||
245 | 216 | dhKey1024 = 0; | ||
246 | 217 | rsaKey512 = 0; | ||
247 | 218 | rsaKey1024 = 0; | ||
248 | 219 | } | ||
249 | 220 | |||
250 | 221 | //////////////////////////////////////////////////////////////////////////////// | ||
251 | 222 | |||
252 | 223 | MaOpenSslConfig::~MaOpenSslConfig() | ||
253 | 224 | { | ||
254 | 225 | if (context != 0) { | ||
255 | 226 | SSL_CTX_free(context); | ||
256 | 227 | } | ||
257 | 228 | if (rsaKey512) { | ||
258 | 229 | RSA_free(rsaKey512); | ||
259 | 230 | } | ||
260 | 231 | if (rsaKey1024) { | ||
261 | 232 | RSA_free(rsaKey1024); | ||
262 | 233 | } | ||
263 | 234 | if (dhKey512) { | ||
264 | 235 | DH_free(dhKey512); | ||
265 | 236 | } | ||
266 | 237 | if (dhKey1024) { | ||
267 | 238 | DH_free(dhKey1024); | ||
268 | 239 | } | ||
269 | 240 | } | ||
270 | 241 | |||
271 | 242 | //////////////////////////////////////////////////////////////////////////////// | ||
272 | 243 | // | ||
273 | 244 | // This method is called to open listening sockets using this SslConfig setup. | ||
274 | 245 | // | ||
275 | 246 | |||
276 | 247 | MprSocket *MaOpenSslConfig::newSocket() | ||
277 | 248 | { | ||
278 | 249 | MaOpenSslSocket *socket; | ||
279 | 250 | |||
280 | 251 | socket = new MaOpenSslSocket(this); | ||
281 | 252 | |||
282 | 253 | return (MprSocket*) socket; | ||
283 | 254 | } | ||
284 | 255 | |||
285 | 256 | //////////////////////////////////////////////////////////////////////////////// | ||
286 | 257 | // | ||
287 | 258 | // This is started for each SSL connection. With Keep-alive this may service | ||
288 | 259 | // many requests. | ||
289 | 260 | // | ||
290 | 261 | |||
291 | 262 | int MaOpenSslConfig::start() | ||
292 | 263 | { | ||
293 | 264 | SSL_METHOD *meth; | ||
294 | 265 | char *hostName; | ||
295 | 266 | |||
296 | 267 | if (keyFile == 0) { | ||
297 | 268 | mprError(MPR_L, MPR_LOG, "Cant start SSL: missing key file"); | ||
298 | 269 | return MPR_ERR_CANT_INITIALIZE; | ||
299 | 270 | } | ||
300 | 271 | if (certFile == 0) { | ||
301 | 272 | mprError(MPR_L, MPR_LOG, "Cant start SSL: missing certificate file"); | ||
302 | 273 | return MPR_ERR_CANT_INITIALIZE; | ||
303 | 274 | } | ||
304 | 275 | |||
305 | 276 | // | ||
306 | 277 | // Depending on the order in the configuration file, we may get called | ||
307 | 278 | // by sslModule::start() before OpenSslModule::start has run. So we | ||
308 | 279 | // must initialize here. | ||
309 | 280 | // | ||
310 | 281 | openSslModule->start(); | ||
311 | 282 | |||
312 | 283 | hostName = host->getName(); | ||
313 | 284 | |||
314 | 285 | if (protocols == MPR_HTTP_PROTO_SSLV2) { | ||
315 | 286 | meth = SSLv2_server_method(); | ||
316 | 287 | } else { | ||
317 | 288 | meth = SSLv23_server_method(); | ||
318 | 289 | } | ||
319 | 290 | context = SSL_CTX_new(meth); | ||
320 | 291 | mprAssert(context); | ||
321 | 292 | if (context == 0) { | ||
322 | 293 | mprError(MPR_L, MPR_LOG, "Unable to create SSL context"); | ||
323 | 294 | return MPR_ERR_CANT_CREATE; | ||
324 | 295 | } | ||
325 | 296 | |||
326 | 297 | SSL_CTX_set_app_data(context, (void*) this); | ||
327 | 298 | SSL_CTX_set_quiet_shutdown(context, 1); | ||
328 | 299 | SSL_CTX_sess_set_cache_size(context, 512); | ||
329 | 300 | |||
330 | 301 | // | ||
331 | 302 | // Configure the certificate for this host | ||
332 | 303 | // | ||
333 | 304 | if (configureCertificates(context, keyFile, certFile) != 0) { | ||
334 | 305 | SSL_CTX_free(context); | ||
335 | 306 | context = 0; | ||
336 | 307 | return MPR_ERR_CANT_INITIALIZE; | ||
337 | 308 | } | ||
338 | 309 | |||
339 | 310 | mprLog(4, "SSL: %s: Using ciphers %s\n", hostName, ciphers); | ||
340 | 311 | SSL_CTX_set_cipher_list(context, ciphers); | ||
341 | 312 | |||
342 | 313 | // | ||
343 | 314 | // Configure the client verification certificate locations | ||
344 | 315 | // | ||
345 | 316 | if (verifyClient) { | ||
346 | 317 | if (caFile == 0 && caPath == 0) { | ||
347 | 318 | mprError(MPR_L, MPR_LOG, | ||
348 | 319 | "Must define CA certificates if using client verification"); | ||
349 | 320 | SSL_CTX_free(context); | ||
350 | 321 | context = 0; | ||
351 | 322 | return MPR_ERR_BAD_STATE; | ||
352 | 323 | } | ||
353 | 324 | if (caFile || caPath) { | ||
354 | 325 | if ((!SSL_CTX_load_verify_locations(context, caFile, caPath)) || | ||
355 | 326 | (!SSL_CTX_set_default_verify_paths(context))) { | ||
356 | 327 | mprError(MPR_L, MPR_LOG, | ||
357 | 328 | "Unable to set certificate locations"); | ||
358 | 329 | SSL_CTX_free(context); | ||
359 | 330 | context = 0; | ||
360 | 331 | return MPR_ERR_CANT_ACCESS; | ||
361 | 332 | } | ||
362 | 333 | if (caFile) { | ||
363 | 334 | STACK_OF(X509_NAME) *certNames; | ||
364 | 335 | certNames = SSL_load_client_CA_file(caFile); | ||
365 | 336 | if (certNames == 0) { | ||
366 | 337 | } else { | ||
367 | 338 | // | ||
368 | 339 | // Define the list of CA certificates to send to the client | ||
369 | 340 | // before they send their client certificate for validation | ||
370 | 341 | // | ||
371 | 342 | SSL_CTX_set_client_CA_list(context, certNames); | ||
372 | 343 | } | ||
373 | 344 | } | ||
374 | 345 | } | ||
375 | 346 | mprLog(4, "SSL: %s: is verifying client connections\n", hostName); | ||
376 | 347 | if (caFile) { | ||
377 | 348 | mprLog(4, "SSL: %s: Using certificates from %s\n", hostName, | ||
378 | 349 | caFile); | ||
379 | 350 | } else if (caPath) { | ||
380 | 351 | mprLog(4, "SSL: %s: Using certificates from directory %s\n", | ||
381 | 352 | hostName, caPath); | ||
382 | 353 | } | ||
383 | 354 | SSL_CTX_set_verify(context, | ||
384 | 355 | SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, | ||
385 | 356 | verifyX509Certificate); | ||
386 | 357 | SSL_CTX_set_verify_depth(context, verifyDepth); | ||
387 | 358 | |||
388 | 359 | } else { | ||
389 | 360 | SSL_CTX_set_verify(context, SSL_VERIFY_NONE, verifyX509Certificate); | ||
390 | 361 | } | ||
391 | 362 | |||
392 | 363 | // | ||
393 | 364 | // Define callbacks | ||
394 | 365 | // | ||
395 | 366 | SSL_CTX_set_tmp_rsa_callback(context, rsaCallback); | ||
396 | 367 | SSL_CTX_set_tmp_dh_callback(context, dhCallback); | ||
397 | 368 | |||
398 | 369 | // | ||
399 | 370 | // Enable all buggy client work-arounds | ||
400 | 371 | // | ||
401 | 372 | SSL_CTX_set_options(context, SSL_OP_ALL); | ||
402 | 373 | |||
403 | 374 | // | ||
404 | 375 | // Select the required protocols | ||
405 | 376 | // | ||
406 | 377 | if (!(protocols & MPR_HTTP_PROTO_SSLV2)) { | ||
407 | 378 | SSL_CTX_set_options(context, SSL_OP_NO_SSLv2); | ||
408 | 379 | mprLog(4, "SSL: %s: Disabling SSLv2\n", hostName); | ||
409 | 380 | } | ||
410 | 381 | if (!(protocols & MPR_HTTP_PROTO_SSLV3)) { | ||
411 | 382 | SSL_CTX_set_options(context, SSL_OP_NO_SSLv3); | ||
412 | 383 | mprLog(4, "SSL: %s: Disabling SSLv3\n", hostName); | ||
413 | 384 | } | ||
414 | 385 | if (!(protocols & MPR_HTTP_PROTO_TLSV1)) { | ||
415 | 386 | SSL_CTX_set_options(context, SSL_OP_NO_TLSv1); | ||
416 | 387 | mprLog(4, "SSL: %s: Disabling TLSv1\n", hostName); | ||
417 | 388 | } | ||
418 | 389 | |||
419 | 390 | // | ||
420 | 391 | // Ensure we generate a new private key for each connection | ||
421 | 392 | // | ||
422 | 393 | SSL_CTX_set_options(context, SSL_OP_SINGLE_DH_USE); | ||
423 | 394 | |||
424 | 395 | // | ||
425 | 396 | // Pre-generate some keys that are slow to compute | ||
426 | 397 | // | ||
427 | 398 | rsaKey512 = RSA_generate_key(512, RSA_F4, 0, 0); | ||
428 | 399 | rsaKey1024 = RSA_generate_key(1024, RSA_F4, 0, 0); | ||
429 | 400 | |||
430 | 401 | dhKey512 = get_dh512(); | ||
431 | 402 | dhKey1024 = get_dh1024(); | ||
432 | 403 | |||
433 | 404 | return 0; | ||
434 | 405 | } | ||
435 | 406 | |||
436 | 407 | //////////////////////////////////////////////////////////////////////////////// | ||
437 | 408 | |||
438 | 409 | void MaOpenSslConfig::stop() | ||
439 | 410 | { | ||
440 | 411 | } | ||
441 | 412 | |||
442 | 413 | //////////////////////////////////////////////////////////////////////////////// | ||
443 | 414 | |||
444 | 415 | SSL_CTX *MaOpenSslConfig::getContext() | ||
445 | 416 | { | ||
446 | 417 | return context; | ||
447 | 418 | } | ||
448 | 419 | |||
449 | 420 | //////////////////////////////////////////////////////////////////////////////// | ||
450 | 421 | |||
451 | 422 | /// | ||
452 | 423 | /// Called to verify X509 client certificates | ||
453 | 424 | /// | ||
454 | 425 | static int verifyX509Certificate(int ok, X509_STORE_CTX *xContext) | ||
455 | 426 | { | ||
456 | 427 | X509 *cert; | ||
457 | 428 | SSL *ssl; | ||
458 | 429 | MaOpenSslSocket *sslSocket; | ||
459 | 430 | MaOpenSslConfig *config; | ||
460 | 431 | char subject[260], issuer[260], peer[260]; | ||
461 | 432 | int error, depth; | ||
462 | 433 | |||
463 | 434 | subject[0] = issuer[0] = '\0'; | ||
464 | 435 | |||
465 | 436 | ssl = (SSL*) X509_STORE_CTX_get_app_data(xContext); | ||
466 | 437 | sslSocket = (MaOpenSslSocket*) SSL_get_app_data(ssl); | ||
467 | 438 | config = (MaOpenSslConfig*) sslSocket->getConfig(); | ||
468 | 439 | |||
469 | 440 | cert = X509_STORE_CTX_get_current_cert(xContext); | ||
470 | 441 | depth = X509_STORE_CTX_get_error_depth(xContext); | ||
471 | 442 | error = X509_STORE_CTX_get_error(xContext); | ||
472 | 443 | |||
473 | 444 | if (X509_NAME_oneline(X509_get_subject_name(cert), subject, | ||
474 | 445 | sizeof(subject) - 1) < 0) { | ||
475 | 446 | ok = 0; | ||
476 | 447 | } | ||
477 | 448 | // | ||
478 | 449 | // FUTURE -- should compare subject name and host name. Need smart compare | ||
479 | 450 | // | ||
480 | 451 | if (X509_NAME_oneline(X509_get_issuer_name(xContext->current_cert), issuer, | ||
481 | 452 | sizeof(issuer) - 1) < 0) { | ||
482 | 453 | ok = 0; | ||
483 | 454 | } | ||
484 | 455 | if (X509_NAME_get_text_by_NID(X509_get_subject_name(xContext->current_cert), | ||
485 | 456 | NID_commonName, peer, sizeof(peer) - 1) < 0) { | ||
486 | 457 | ok = 0; | ||
487 | 458 | } | ||
488 | 459 | |||
489 | 460 | // | ||
490 | 461 | // Customizers: add your own code here to validate client certificates | ||
491 | 462 | // | ||
492 | 463 | if (ok && config->verifyDepth < depth) { | ||
493 | 464 | if (error == 0) { | ||
494 | 465 | error = X509_V_ERR_CERT_CHAIN_TOO_LONG; | ||
495 | 466 | } | ||
496 | 467 | ok = 0; | ||
497 | 468 | } | ||
498 | 469 | |||
499 | 470 | if (error != 0) { | ||
500 | 471 | mprAssert(!ok); | ||
501 | 472 | } | ||
502 | 473 | |||
503 | 474 | #if UNUSED | ||
504 | 475 | switch (error) { | ||
505 | 476 | case X509_V_ERR_CERT_HAS_EXPIRED: | ||
506 | 477 | case X509_V_ERR_CERT_NOT_YET_VALID: | ||
507 | 478 | case X509_V_ERR_CERT_REJECTED: | ||
508 | 479 | case X509_V_ERR_CERT_SIGNATURE_FAILURE: | ||
509 | 480 | case X509_V_ERR_CERT_UNTRUSTED: | ||
510 | 481 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: | ||
511 | 482 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: | ||
512 | 483 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: | ||
513 | 484 | case X509_V_ERR_INVALID_CA: | ||
514 | 485 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: | ||
515 | 486 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: | ||
516 | 487 | default: | ||
517 | 488 | ok = 0; | ||
518 | 489 | break; | ||
519 | 490 | } | ||
520 | 491 | #endif | ||
521 | 492 | |||
522 | 493 | if (!ok) { | ||
523 | 494 | mprLog(0, "SSL: Certification failed: subject %s\n", subject); | ||
524 | 495 | mprLog(4, "SSL: Issuer: %s\n", issuer); | ||
525 | 496 | mprLog(4, "SSL: Peer: %s\n", peer); | ||
526 | 497 | mprLog(4, "SSL: Error: %d: %s\n", error, | ||
527 | 498 | X509_verify_cert_error_string(error)); | ||
528 | 499 | |||
529 | 500 | } else { | ||
530 | 501 | mprLog(0, "SSL: Certificate verified: subject %s\n", subject); | ||
531 | 502 | mprLog(4, "SSL: Issuer: %s\n", issuer); | ||
532 | 503 | mprLog(4, "SSL: Peer: %s\n", peer); | ||
533 | 504 | } | ||
534 | 505 | return ok; | ||
535 | 506 | } | ||
536 | 507 | |||
537 | 508 | //////////////////////////////////////////////////////////////////////////////// | ||
538 | 509 | |||
539 | 510 | /// | ||
540 | 511 | /// Configure the SSL certificate information | ||
541 | 512 | /// | ||
542 | 513 | |||
543 | 514 | static int configureCertificates(SSL_CTX *ctx, char *key, char *cert) | ||
544 | 515 | { | ||
545 | 516 | mprAssert(ctx); | ||
546 | 517 | mprAssert(cert); | ||
547 | 518 | |||
548 | 519 | if (cert == 0) { | ||
549 | 520 | return 0; | ||
550 | 521 | } | ||
551 | 522 | |||
552 | 523 | if (SSL_CTX_use_certificate_file(ctx, cert, SSL_FILETYPE_PEM) <= 0){ | ||
553 | 524 | mprError(MPR_L, MPR_LOG, "Can't define certificate file: %s", cert); | ||
554 | 525 | return -1; | ||
555 | 526 | } | ||
556 | 527 | |||
557 | 528 | key = (key == 0) ? cert : key; | ||
558 | 529 | if (SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM) <= 0) { | ||
559 | 530 | mprError(MPR_L, MPR_LOG, "Can't define private key file: %s", key); | ||
560 | 531 | return -1; | ||
561 | 532 | } | ||
562 | 533 | |||
563 | 534 | if (!SSL_CTX_check_private_key(ctx)) { | ||
564 | 535 | mprError(MPR_L, MPR_LOG, "Check of private key file failed: %s", key); | ||
565 | 536 | return -1; | ||
566 | 537 | } | ||
567 | 538 | return 0; | ||
568 | 539 | } | ||
569 | 540 | |||
570 | 541 | //////////////////////////////////////////////////////////////////////////////// | ||
571 | 542 | //////////////////////////////// MaOpenSslSocket /////////////////////////////// | ||
572 | 543 | //////////////////////////////////////////////////////////////////////////////// | ||
573 | 544 | |||
574 | 545 | MaOpenSslSocket::MaOpenSslSocket(MaOpenSslConfig *config) : MaSslSocket(config) | ||
575 | 546 | { | ||
576 | 547 | ssl = 0; | ||
577 | 548 | bio = 0; | ||
578 | 549 | context = config->getContext(); | ||
579 | 550 | } | ||
580 | 551 | |||
581 | 552 | //////////////////////////////////////////////////////////////////////////////// | ||
582 | 553 | |||
583 | 554 | MaOpenSslSocket::~MaOpenSslSocket() | ||
584 | 555 | { | ||
585 | 556 | mprAssert(ssl == 0); | ||
586 | 557 | mprAssert(bio == 0); | ||
587 | 558 | } | ||
588 | 559 | |||
589 | 560 | //////////////////////////////////////////////////////////////////////////////// | ||
590 | 561 | // | ||
591 | 562 | // Called to accept new connections. When a new connection arrives, this | ||
592 | 563 | // method is called to create a new socket object using the same SSL "config" | ||
593 | 564 | // as the listening socket. | ||
594 | 565 | // | ||
595 | 566 | |||
596 | 567 | MprSocket *MaOpenSslSocket::newSocket() | ||
597 | 568 | { | ||
598 | 569 | MaOpenSslSocket *socket; | ||
599 | 570 | |||
600 | 571 | socket = new MaOpenSslSocket((MaOpenSslConfig*) config); | ||
601 | 572 | |||
602 | 573 | return (MprSocket*) socket; | ||
603 | 574 | } | ||
604 | 575 | |||
605 | 576 | //////////////////////////////////////////////////////////////////////////////// | ||
606 | 577 | // | ||
607 | 578 | // initConnection is called on the first I/O on a new connection. It must | ||
608 | 579 | // setup the SSL structures ready to exchange data. | ||
609 | 580 | // | ||
610 | 581 | // Init connection must return -1 on errors. It must also be able to reject | ||
611 | 582 | // subsequent calls to read()/write(). On errors we leave bio == 0. This | ||
612 | 583 | // will cause any calls to read() to error. | ||
613 | 584 | // | ||
614 | 585 | |||
615 | 586 | int MaOpenSslSocket::initConnection() | ||
616 | 587 | { | ||
617 | 588 | BIO *bioSSL, *bioSock; | ||
618 | 589 | |||
619 | 590 | if (bio) { | ||
620 | 591 | return 0; | ||
621 | 592 | } | ||
622 | 593 | bio = BIO_new(BIO_f_buffer()); | ||
623 | 594 | if (bio == 0) { | ||
624 | 595 | return MPR_ERR_CANT_INITIALIZE; | ||
625 | 596 | } | ||
626 | 597 | |||
627 | 598 | BIO_set_write_buffer_size(bio, 128); | ||
628 | 599 | ssl = (SSL*) SSL_new(context); | ||
629 | 600 | mprAssert(ssl); | ||
630 | 601 | if (ssl == 0) { | ||
631 | 602 | return MPR_ERR_CANT_INITIALIZE; | ||
632 | 603 | } | ||
633 | 604 | SSL_set_app_data(ssl, (void*) this); | ||
634 | 605 | |||
635 | 606 | SSL_set_session(ssl, 0); | ||
636 | 607 | bioSSL = BIO_new(BIO_f_ssl()); | ||
637 | 608 | mprAssert(bioSSL); | ||
638 | 609 | |||
639 | 610 | bioSock = BIO_new_socket(sock, BIO_NOCLOSE); | ||
640 | 611 | mprAssert(bioSock); | ||
641 | 612 | |||
642 | 613 | SSL_set_bio(ssl, bioSock, bioSock); | ||
643 | 614 | SSL_set_accept_state(ssl); | ||
644 | 615 | |||
645 | 616 | BIO_set_ssl(bioSSL, ssl, BIO_CLOSE); | ||
646 | 617 | BIO_push(bio, bioSSL); | ||
647 | 618 | return 0; | ||
648 | 619 | } | ||
649 | 620 | |||
650 | 621 | //////////////////////////////////////////////////////////////////////////////// | ||
651 | 622 | |||
652 | 623 | bool MaOpenSslSocket::dispose() | ||
653 | 624 | { | ||
654 | 625 | if (ssl) { | ||
655 | 626 | SSL_set_shutdown(ssl, SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN); | ||
656 | 627 | ssl = 0; | ||
657 | 628 | } | ||
658 | 629 | if (bio) { | ||
659 | 630 | BIO_free_all(bio); | ||
660 | 631 | bio = 0; | ||
661 | 632 | } | ||
662 | 633 | |||
663 | 634 | // | ||
664 | 635 | // Disposing a socket is a thread-safe way of calling a destructor. It | ||
665 | 636 | // uses reference counting and will be deleted when the last reference | ||
666 | 637 | // is released. | ||
667 | 638 | // | ||
668 | 639 | return this->MprSocket::dispose(); | ||
669 | 640 | } | ||
670 | 641 | |||
671 | 642 | //////////////////////////////////////////////////////////////////////////////// | ||
672 | 643 | |||
673 | 644 | void MaOpenSslSocket::close(int how) | ||
674 | 645 | { | ||
675 | 646 | BIO_flush(bio); | ||
676 | 647 | this->MprSocket::close(how); | ||
677 | 648 | } | ||
678 | 649 | |||
679 | 650 | //////////////////////////////////////////////////////////////////////////////// | ||
680 | 651 | // | ||
681 | 652 | // Write data. Return the number of bytes written or -1 on errors. | ||
682 | 653 | // | ||
683 | 654 | |||
684 | 655 | int MaOpenSslSocket::write(char *buf, int len) | ||
685 | 656 | { | ||
686 | 657 | int rc, written, totalWritten; | ||
687 | 658 | |||
688 | 659 | if (bio == 0 || ssl == 0 || len < 0) { | ||
689 | 660 | return -1; | ||
690 | 661 | } | ||
691 | 662 | BIO_clear_retry_flags(bio); | ||
692 | 663 | totalWritten = 0; | ||
693 | 664 | do { | ||
694 | 665 | written = BIO_write(bio, buf, len); | ||
695 | 666 | mprLog(7, "written %d, len %d\n", written, len); | ||
696 | 667 | if (written >= 0) { | ||
697 | 668 | do { | ||
698 | 669 | rc = BIO_flush(bio); | ||
699 | 670 | mprLog(7, "BIO_flush rc %d\n", rc); | ||
700 | 671 | if (rc > 0) { | ||
701 | 672 | // Success | ||
702 | 673 | break; | ||
703 | 674 | } | ||
704 | 675 | // | ||
705 | 676 | // Nap to prevent busy waiting. | ||
706 | 677 | // | ||
707 | 678 | mprSleep(10); | ||
708 | 679 | } while (rc <= 0 && BIO_should_retry(bio)); | ||
709 | 680 | totalWritten += written; | ||
710 | 681 | buf += written; | ||
711 | 682 | len -= written; | ||
712 | 683 | } | ||
713 | 684 | mprLog(7, "write: len %d, written %d, total %d, should_retry %d\n", | ||
714 | 685 | len, written, totalWritten, BIO_should_retry(bio)); | ||
715 | 686 | } while (len > 0 && (written > 0 || BIO_should_retry(bio))); | ||
716 | 687 | |||
717 | 688 | //AFF | ||
718 | 689 | if(written < 0 && BIO_should_retry(bio) == 0) | ||
719 | 690 | return -1; | ||
720 | 691 | // | ||
721 | 692 | |||
722 | 693 | return totalWritten; | ||
723 | 694 | } | ||
724 | 695 | |||
725 | 696 | //////////////////////////////////////////////////////////////////////////////// | ||
726 | 697 | |||
727 | 698 | int MaOpenSslSocket::flush() | ||
728 | 699 | { | ||
729 | 700 | return BIO_flush(bio); | ||
730 | 701 | } | ||
731 | 702 | |||
732 | 703 | //////////////////////////////////////////////////////////////////////////////// | ||
733 | 704 | |||
734 | 705 | int MaOpenSslSocket::read(char *buf, int len) | ||
735 | 706 | { | ||
736 | 707 | int rc; | ||
737 | 708 | |||
738 | 709 | if (bio == 0 || ssl == 0) { | ||
739 | 710 | return -1; | ||
740 | 711 | } | ||
741 | 712 | |||
742 | 713 | rc = BIO_read(bio, buf, len); | ||
743 | 714 | |||
744 | 715 | #if DEBUG | ||
745 | 716 | if (rc > 0 && !connTraced) { | ||
746 | 717 | X509_NAME *xSubject; | ||
747 | 718 | X509 *cert; | ||
748 | 719 | char subject[260], issuer[260], peer[260]; | ||
749 | 720 | |||
750 | 721 | mprLog(4, "%d: SSL Connected using: \"%s\"\n", | ||
751 | 722 | sock, SSL_get_cipher(ssl)); | ||
752 | 723 | |||
753 | 724 | cert = SSL_get_peer_certificate(ssl); | ||
754 | 725 | if (cert == 0) { | ||
755 | 726 | mprLog(4, "%d: SSL Details: client supplied no certificate\n", | ||
756 | 727 | sock); | ||
757 | 728 | } else { | ||
758 | 729 | xSubject = X509_get_subject_name(cert); | ||
759 | 730 | X509_NAME_oneline(xSubject, subject, sizeof(subject) -1); | ||
760 | 731 | X509_NAME_oneline(X509_get_issuer_name(cert), issuer, | ||
761 | 732 | sizeof(issuer) -1); | ||
762 | 733 | X509_NAME_get_text_by_NID(xSubject, NID_commonName, peer, | ||
763 | 734 | sizeof(peer) - 1); | ||
764 | 735 | mprLog(4, "%d: SSL Subject %s\n", sock, subject); | ||
765 | 736 | mprLog(4, "%d: SSL Issuer: %s\n", sock, issuer); | ||
766 | 737 | mprLog(4, "%d: SSL Peer: %s\n", sock, peer); | ||
767 | 738 | X509_free(cert); | ||
768 | 739 | } | ||
769 | 740 | connTraced = 1; | ||
770 | 741 | } | ||
771 | 742 | #endif | ||
772 | 743 | |||
773 | 744 | if (rc > 0) { | ||
774 | 745 | return rc; | ||
775 | 746 | |||
776 | 747 | } else if (rc == 0) { | ||
777 | 748 | if (BIO_should_retry(bio)) { | ||
778 | 749 | return 0; | ||
779 | 750 | } | ||
780 | 751 | flags |= MPR_SOCKET_EOF; | ||
781 | 752 | return 0; | ||
782 | 753 | } | ||
783 | 754 | if (BIO_should_retry(bio)) { | ||
784 | 755 | return 0; | ||
785 | 756 | } | ||
786 | 757 | return rc; | ||
787 | 758 | } | ||
788 | 759 | |||
789 | 760 | //////////////////////////////////////////////////////////////////////////////// | ||
790 | 761 | #if UNUSED | ||
791 | 762 | // | ||
792 | 763 | // Return true if end of file | ||
793 | 764 | // | ||
794 | 765 | |||
795 | 766 | bool MaOpenSslSocket::getEof() | ||
796 | 767 | { | ||
797 | 768 | bool rc; | ||
798 | 769 | |||
799 | 770 | rc = (BIO_eof(bio) != 0); | ||
800 | 771 | return rc; | ||
801 | 772 | } | ||
802 | 773 | |||
803 | 774 | #endif | ||
804 | 775 | |||
805 | 776 | //////////////////////////////////////////////////////////////////////////////// | ||
806 | 777 | #if BLD_FEATURE_MULTITHREAD | ||
807 | 778 | |||
808 | 779 | static ulong sslThreadId() | ||
809 | 780 | { | ||
810 | 781 | return (long) mprGetCurrentThread(); | ||
811 | 782 | } | ||
812 | 783 | |||
813 | 784 | //////////////////////////////////////////////////////////////////////////////// | ||
814 | 785 | |||
815 | 786 | static void sslStaticLock(int mode, int n, const char *file, int line) | ||
816 | 787 | { | ||
817 | 788 | mprAssert(0 <= n && n < numLocks); | ||
818 | 789 | if (mode & CRYPTO_LOCK) { | ||
819 | 790 | locks[n]->lock(); | ||
820 | 791 | } else { | ||
821 | 792 | locks[n]->unlock(); | ||
822 | 793 | } | ||
823 | 794 | } | ||
824 | 795 | |||
825 | 796 | //////////////////////////////////////////////////////////////////////////////// | ||
826 | 797 | |||
827 | 798 | static DynLock *sslCreateDynLock(const char *file, int line) | ||
828 | 799 | { | ||
829 | 800 | DynLock *dl; | ||
830 | 801 | |||
831 | 802 | dl = (DynLock*) mprMalloc(sizeof(DynLock)); | ||
832 | 803 | dl->mutex = new MprMutex(); | ||
833 | 804 | return dl; | ||
834 | 805 | } | ||
835 | 806 | |||
836 | 807 | //////////////////////////////////////////////////////////////////////////////// | ||
837 | 808 | |||
838 | 809 | static void sslDestroyDynLock(DynLock *dl, const char *file, int line) | ||
839 | 810 | { | ||
840 | 811 | delete dl->mutex; | ||
841 | 812 | mprFree(dl); | ||
842 | 813 | } | ||
843 | 814 | |||
844 | 815 | //////////////////////////////////////////////////////////////////////////////// | ||
845 | 816 | |||
846 | 817 | static void sslDynLock(int mode, DynLock *dl, const char *file, int line) | ||
847 | 818 | { | ||
848 | 819 | if (mode & CRYPTO_LOCK) { | ||
849 | 820 | dl->mutex->lock(); | ||
850 | 821 | } else { | ||
851 | 822 | dl->mutex->unlock(); | ||
852 | 823 | } | ||
853 | 824 | } | ||
854 | 825 | |||
855 | 826 | #endif // BLD_FEATURE_MULTITHREAD | ||
856 | 827 | //////////////////////////////////////////////////////////////////////////////// | ||
857 | 828 | // | ||
858 | 829 | // Used for ephemeral RSA keys | ||
859 | 830 | // | ||
860 | 831 | |||
861 | 832 | static RSA *rsaCallback(SSL *ssl, int isExport, int keyLength) | ||
862 | 833 | { | ||
863 | 834 | MaOpenSslSocket *sslSocket; | ||
864 | 835 | MaOpenSslConfig *config; | ||
865 | 836 | RSA *key; | ||
866 | 837 | |||
867 | 838 | sslSocket = (MaOpenSslSocket*) SSL_get_app_data(ssl); | ||
868 | 839 | config = (MaOpenSslConfig*) sslSocket->getConfig(); | ||
869 | 840 | |||
870 | 841 | key = 0; | ||
871 | 842 | switch (keyLength) { | ||
872 | 843 | case 512: | ||
873 | 844 | key = config->rsaKey512; | ||
874 | 845 | break; | ||
875 | 846 | |||
876 | 847 | case 1024: | ||
877 | 848 | default: | ||
878 | 849 | key = config->rsaKey1024; | ||
879 | 850 | } | ||
880 | 851 | return key; | ||
881 | 852 | } | ||
882 | 853 | |||
883 | 854 | |||
884 | 855 | //////////////////////////////////////////////////////////////////////////////// | ||
885 | 856 | // | ||
886 | 857 | // Used for ephemeral DH keys | ||
887 | 858 | // | ||
888 | 859 | |||
889 | 860 | static DH *dhCallback(SSL *ssl, int isExport, int keyLength) | ||
890 | 861 | { | ||
891 | 862 | MaOpenSslSocket *sslSocket; | ||
892 | 863 | MaOpenSslConfig *config; | ||
893 | 864 | DH *key; | ||
894 | 865 | |||
895 | 866 | sslSocket = (MaOpenSslSocket*) SSL_get_app_data(ssl); | ||
896 | 867 | config = (MaOpenSslConfig*) sslSocket->getConfig(); | ||
897 | 868 | |||
898 | 869 | key = 0; | ||
899 | 870 | switch (keyLength) { | ||
900 | 871 | case 512: | ||
901 | 872 | key = config->dhKey512; | ||
902 | 873 | break; | ||
903 | 874 | |||
904 | 875 | case 1024: | ||
905 | 876 | default: | ||
906 | 877 | key = config->dhKey1024; | ||
907 | 878 | } | ||
908 | 879 | return key; | ||
909 | 880 | } | ||
910 | 881 | |||
911 | 882 | //////////////////////////////////////////////////////////////////////////////// | ||
912 | 883 | #else | ||
913 | 884 | void mprOpenSslModuleDummy() {} | ||
914 | 885 | |||
915 | 886 | #endif // BLD_FEATURE_OPENSSL_MODULE | ||
916 | 887 | |||
917 | 888 | // | ||
918 | 889 | // Local variables: | ||
919 | 890 | // tab-width: 4 | ||
920 | 891 | // c-basic-offset: 4 | ||
921 | 892 | // End: | ||
922 | 893 | // vim:tw=78 | ||
923 | 894 | // vim600: sw=4 ts=4 fdm=marker | ||
924 | 895 | // vim<600: sw=4 ts=4 | ||
925 | 896 | // | ||
926 | 0 | 897 | ||
927 | === added file 'appWeb/thread.cpp' | |||
928 | --- appWeb/thread.cpp 1970-01-01 00:00:00 +0000 | |||
929 | +++ appWeb/thread.cpp 2011-11-23 01:01:25 +0000 | |||
930 | @@ -0,0 +1,768 @@ | |||
931 | 1 | /// | ||
932 | 2 | /// @file WIN/thread.cpp | ||
933 | 3 | /// @brief Primitive multi-threading support for Linux | ||
934 | 4 | /// @overview This module provides threading, mutex and condition | ||
935 | 5 | /// variable APIs for Windows. | ||
936 | 6 | // | ||
937 | 7 | ////////////////////////////////// Copyright /////////////////////////////////// | ||
938 | 8 | // | ||
939 | 9 | // @copy default | ||
940 | 10 | // | ||
941 | 11 | // Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved. | ||
942 | 12 | // Copyright (c) Michael O'Brien, 1994-2006. All Rights Reserved. | ||
943 | 13 | // | ||
944 | 14 | // This software is distributed under commercial and open source licenses. | ||
945 | 15 | // You may use the GPL open source license described below or you may acquire | ||
946 | 16 | // a commercial license from Mbedthis Software. You agree to be fully bound | ||
947 | 17 | // by the terms of either license. Consult the LICENSE.TXT distributed with | ||
948 | 18 | // this software for full details. | ||
949 | 19 | // | ||
950 | 20 | // This software is open source; you can redistribute it and/or modify it | ||
951 | 21 | // under the terms of the GNU General Public License as published by the | ||
952 | 22 | // Free Software Foundation; either version 2 of the License, or (at your | ||
953 | 23 | // option) any later version. See the GNU General Public License for more | ||
954 | 24 | // details at: http://www.mbedthis.com/downloads/gplLicense.html | ||
955 | 25 | // | ||
956 | 26 | // This program is distributed WITHOUT ANY WARRANTY; without even the | ||
957 | 27 | // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
958 | 28 | // | ||
959 | 29 | // This GPL license does NOT permit incorporating this software into | ||
960 | 30 | // proprietary programs. If you are unable to comply with the GPL, you must | ||
961 | 31 | // acquire a commercial license to use this software. Commercial licenses | ||
962 | 32 | // for this software and support services are available from Mbedthis | ||
963 | 33 | // Software at http://www.mbedthis.com | ||
964 | 34 | // | ||
965 | 35 | // @end | ||
966 | 36 | // | ||
967 | 37 | ////////////////////////////////// Includes /////////////////////////////////// | ||
968 | 38 | |||
969 | 39 | #include "mpr/mpr.h" | ||
970 | 40 | |||
971 | 41 | //////////////////////////////////// Locals /////////////////////////////////// | ||
972 | 42 | #if BLD_FEATURE_MULTITHREAD | ||
973 | 43 | |||
974 | 44 | #if BLD_DEBUG | ||
975 | 45 | CRITICAL_SECTION debugCs; | ||
976 | 46 | static MprList mutexList; | ||
977 | 47 | static int numMutex = -1; // Must initialize to -1 (see addMutex) | ||
978 | 48 | MprList condList; | ||
979 | 49 | |||
980 | 50 | #define DEBUG_TIME 0 // Spare[0] == time | ||
981 | 51 | #define DEBUG_ID 1 // Spare[1] == thread id | ||
982 | 52 | #endif | ||
983 | 53 | |||
984 | 54 | //////////////////////////// Forward Declarations ///////////////////////////// | ||
985 | 55 | |||
986 | 56 | static uint __stdcall threadProcWrapper(void *arg); | ||
987 | 57 | |||
988 | 58 | #if BLD_DEBUG | ||
989 | 59 | #if UNUSED | ||
990 | 60 | static void lockBuster(void *data, MprThread *tp); | ||
991 | 61 | #endif | ||
992 | 62 | static void removeMutex(MprMutex *mp); | ||
993 | 63 | static void addMutex(MprMutex *mp); | ||
994 | 64 | #endif | ||
995 | 65 | |||
996 | 66 | ///////////////////////////////////// Code //////////////////////////////////// | ||
997 | 67 | // | ||
998 | 68 | // Initialize thread service | ||
999 | 69 | // | ||
1000 | 70 | // | ||
1001 | 71 | |||
1002 | 72 | MprThreadService::MprThreadService() | ||
1003 | 73 | { | ||
1004 | 74 | mutex = new MprMutex(); | ||
1005 | 75 | |||
1006 | 76 | // | ||
1007 | 77 | // Don't actually create the thread. Just create a thread object for this | ||
1008 | 78 | // main thread. | ||
1009 | 79 | // | ||
1010 | 80 | mainThread = new MprThread(MPR_NORMAL_PRIORITY, "main"); | ||
1011 | 81 | mainThread->setId(GetCurrentThreadId()); | ||
1012 | 82 | |||
1013 | 83 | insertThread(mainThread); | ||
1014 | 84 | } | ||
1015 | 85 | |||
1016 | 86 | /////////////////////////////////////////////////////////////////////////////// | ||
1017 | 87 | // | ||
1018 | 88 | // Terminate the thread service | ||
1019 | 89 | // | ||
1020 | 90 | |||
1021 | 91 | MprThreadService::~MprThreadService() | ||
1022 | 92 | { | ||
1023 | 93 | delete mainThread; | ||
1024 | 94 | |||
1025 | 95 | #if BLD_DEBUG | ||
1026 | 96 | if (threads.getNumItems() > 0) { | ||
1027 | 97 | mprError(MPR_L, MPR_LOG, "Exiting with %d thread(s) unfreed", | ||
1028 | 98 | threads.getNumItems()); | ||
1029 | 99 | } | ||
1030 | 100 | if (condList.getNumItems() > 0) { | ||
1031 | 101 | mprError(MPR_L, MPR_LOG, "Exiting with %d cond var(s) unfreed", | ||
1032 | 102 | condList.getNumItems()); | ||
1033 | 103 | } | ||
1034 | 104 | // | ||
1035 | 105 | // We allow one open mutex for the log service which has not yet shutdown, | ||
1036 | 106 | // and one for the libmpr DLL (malloc), we also need one for our mutex | ||
1037 | 107 | // is freed below. | ||
1038 | 108 | // | ||
1039 | 109 | if (mutexList.getNumItems() > 3) { | ||
1040 | 110 | mprError(MPR_L, MPR_LOG, "Exiting with %d mutex(es) unfreed", | ||
1041 | 111 | mutexList.getNumItems() - 3); | ||
1042 | 112 | } | ||
1043 | 113 | #endif | ||
1044 | 114 | delete mutex; | ||
1045 | 115 | } | ||
1046 | 116 | |||
1047 | 117 | //////////////////////////////////////////////////////////////////////////////// | ||
1048 | 118 | // | ||
1049 | 119 | // Add a thread to the list | ||
1050 | 120 | // | ||
1051 | 121 | |||
1052 | 122 | void MprThreadService::insertThread(MprThread *tp) | ||
1053 | 123 | { | ||
1054 | 124 | lock(); | ||
1055 | 125 | threads.insert(tp); | ||
1056 | 126 | unlock(); | ||
1057 | 127 | } | ||
1058 | 128 | |||
1059 | 129 | //////////////////////////////////////////////////////////////////////////////// | ||
1060 | 130 | // | ||
1061 | 131 | // Remove a thread from the list | ||
1062 | 132 | // | ||
1063 | 133 | |||
1064 | 134 | void MprThreadService::removeThread(MprThread *tp) | ||
1065 | 135 | { | ||
1066 | 136 | lock(); | ||
1067 | 137 | threads.remove(tp); | ||
1068 | 138 | unlock(); | ||
1069 | 139 | } | ||
1070 | 140 | |||
1071 | 141 | //////////////////////////////////////////////////////////////////////////////// | ||
1072 | 142 | // | ||
1073 | 143 | // Nothing to do | ||
1074 | 144 | // | ||
1075 | 145 | |||
1076 | 146 | int MprThreadService::start() | ||
1077 | 147 | { | ||
1078 | 148 | #if BLD_DEBUG && UNUSED | ||
1079 | 149 | MprThread *tp; | ||
1080 | 150 | |||
1081 | 151 | if (!mprGetDebugMode()) { | ||
1082 | 152 | tp = new MprThread(lockBuster, MPR_LOW_PRIORITY, 0, "watch"); | ||
1083 | 153 | tp->start(); | ||
1084 | 154 | } | ||
1085 | 155 | #endif | ||
1086 | 156 | return 0; | ||
1087 | 157 | } | ||
1088 | 158 | |||
1089 | 159 | //////////////////////////////////////////////////////////////////////////////// | ||
1090 | 160 | // | ||
1091 | 161 | // Nothing to do. We expect the threads to be self terminating. | ||
1092 | 162 | // | ||
1093 | 163 | |||
1094 | 164 | int MprThreadService::stop(int timeout) | ||
1095 | 165 | { | ||
1096 | 166 | // | ||
1097 | 167 | // Wait until all threads (except main thread) have exited | ||
1098 | 168 | // | ||
1099 | 169 | while (threads.getNumItems() > 1 && timeout > 0) { | ||
1100 | 170 | mprSleep(10); | ||
1101 | 171 | timeout -= 10; | ||
1102 | 172 | } | ||
1103 | 173 | return 0; | ||
1104 | 174 | } | ||
1105 | 175 | |||
1106 | 176 | //////////////////////////////////////////////////////////////////////////////// | ||
1107 | 177 | // | ||
1108 | 178 | // Return the current thread object | ||
1109 | 179 | // | ||
1110 | 180 | |||
1111 | 181 | MprThread *MprThreadService::getCurrentThread() | ||
1112 | 182 | { | ||
1113 | 183 | MprThread *tp; | ||
1114 | 184 | int id; | ||
1115 | 185 | |||
1116 | 186 | lock(); | ||
1117 | 187 | id = (int) GetCurrentThreadId(); | ||
1118 | 188 | tp = (MprThread*) threads.getFirst(); | ||
1119 | 189 | while (tp) { | ||
1120 | 190 | if (tp->getId() == id) { | ||
1121 | 191 | unlock(); | ||
1122 | 192 | return tp; | ||
1123 | 193 | } | ||
1124 | 194 | tp = (MprThread*) threads.getNext(tp); | ||
1125 | 195 | } | ||
1126 | 196 | unlock(); | ||
1127 | 197 | return 0; | ||
1128 | 198 | } | ||
1129 | 199 | |||
1130 | 200 | //////////////////////////////////////////////////////////////////////////////// | ||
1131 | 201 | ////////////////////////////////// MprThread /////////////////////////////////// | ||
1132 | 202 | //////////////////////////////////////////////////////////////////////////////// | ||
1133 | 203 | // | ||
1134 | 204 | // Create a main thread | ||
1135 | 205 | // | ||
1136 | 206 | |||
1137 | 207 | MprThread::MprThread(int priority, char *name) | ||
1138 | 208 | { | ||
1139 | 209 | osThreadId = 0; | ||
1140 | 210 | pid = getpid(); | ||
1141 | 211 | threadHandle = 0; | ||
1142 | 212 | this->priority = priority; | ||
1143 | 213 | entry = 0; | ||
1144 | 214 | data = 0; | ||
1145 | 215 | this->name = mprStrdup(name); | ||
1146 | 216 | |||
1147 | 217 | mutex = new MprMutex(); | ||
1148 | 218 | // | ||
1149 | 219 | // Inserted into the thread list in MprThreadService() | ||
1150 | 220 | // | ||
1151 | 221 | } | ||
1152 | 222 | |||
1153 | 223 | //////////////////////////////////////////////////////////////////////////////// | ||
1154 | 224 | // | ||
1155 | 225 | // Create a thread | ||
1156 | 226 | // | ||
1157 | 227 | |||
1158 | 228 | MprThread::MprThread(MprThreadProc entry, int priority, void *data, | ||
1159 | 229 | char *name, int stackSize) | ||
1160 | 230 | { | ||
1161 | 231 | osThreadId = 0; | ||
1162 | 232 | pid = getpid(); | ||
1163 | 233 | threadHandle = 0; | ||
1164 | 234 | this->priority = priority; | ||
1165 | 235 | this->entry = entry; | ||
1166 | 236 | this->data = data; | ||
1167 | 237 | this->name = mprStrdup(name); | ||
1168 | 238 | |||
1169 | 239 | mutex = new MprMutex(); | ||
1170 | 240 | mpr->threadService->insertThread(this); | ||
1171 | 241 | } | ||
1172 | 242 | |||
1173 | 243 | //////////////////////////////////////////////////////////////////////////////// | ||
1174 | 244 | // | ||
1175 | 245 | // Destroy a thread | ||
1176 | 246 | // | ||
1177 | 247 | |||
1178 | 248 | MprThread::~MprThread() | ||
1179 | 249 | { | ||
1180 | 250 | lock(); | ||
1181 | 251 | mprLog(MPR_INFO, "Thread exiting %s (%x)\n", name, osThreadId); | ||
1182 | 252 | |||
1183 | 253 | mpr->threadService->removeThread(this); | ||
1184 | 254 | mprFree(name); | ||
1185 | 255 | if (threadHandle) { | ||
1186 | 256 | CloseHandle(threadHandle); | ||
1187 | 257 | } | ||
1188 | 258 | delete mutex; | ||
1189 | 259 | } | ||
1190 | 260 | |||
1191 | 261 | //////////////////////////////////////////////////////////////////////////////// | ||
1192 | 262 | // | ||
1193 | 263 | // Start a thread | ||
1194 | 264 | // | ||
1195 | 265 | |||
1196 | 266 | int MprThread::start() | ||
1197 | 267 | { | ||
1198 | 268 | HANDLE h; | ||
1199 | 269 | uint threadId; | ||
1200 | 270 | |||
1201 | 271 | lock(); | ||
1202 | 272 | h = (HANDLE) _beginthreadex(NULL, 0, threadProcWrapper, (void*) this, | ||
1203 | 273 | 0, &threadId); | ||
1204 | 274 | if (h == NULL) { | ||
1205 | 275 | unlock(); | ||
1206 | 276 | return MPR_ERR_CANT_INITIALIZE; | ||
1207 | 277 | } | ||
1208 | 278 | osThreadId = (ulong) threadId; | ||
1209 | 279 | threadHandle = (HANDLE) h; | ||
1210 | 280 | |||
1211 | 281 | setPriority(priority); | ||
1212 | 282 | mprLog(MPR_INFO, "Created thread %s (%x)\n", name, threadId); | ||
1213 | 283 | unlock(); | ||
1214 | 284 | return 0; | ||
1215 | 285 | } | ||
1216 | 286 | |||
1217 | 287 | //////////////////////////////////////////////////////////////////////////////// | ||
1218 | 288 | // | ||
1219 | 289 | // Thread entry function | ||
1220 | 290 | // | ||
1221 | 291 | |||
1222 | 292 | static uint __stdcall threadProcWrapper(void *data) | ||
1223 | 293 | { | ||
1224 | 294 | //AFF gracefull crash, main app should exit | ||
1225 | 295 | #if BLD_DEBUG | ||
1226 | 296 | #else | ||
1227 | 297 | try | ||
1228 | 298 | { | ||
1229 | 299 | #endif | ||
1230 | 300 | MprThread *tp; | ||
1231 | 301 | |||
1232 | 302 | tp = (MprThread*) data; | ||
1233 | 303 | tp->threadProc(); | ||
1234 | 304 | delete tp; | ||
1235 | 305 | |||
1236 | 306 | #if BLD_DEBUG | ||
1237 | 307 | #else | ||
1238 | 308 | } | ||
1239 | 309 | catch(...) | ||
1240 | 310 | { | ||
1241 | 311 | mprLog((char*)"ERROR: catch(...) threadProcWrapper crashed. terminating gracefully..."); | ||
1242 | 312 | mprGetMpr()->terminate(1); | ||
1243 | 313 | } | ||
1244 | 314 | #endif | ||
1245 | 315 | |||
1246 | 316 | return 0; | ||
1247 | 317 | } | ||
1248 | 318 | |||
1249 | 319 | //////////////////////////////////////////////////////////////////////////////// | ||
1250 | 320 | // | ||
1251 | 321 | // Thread procedure | ||
1252 | 322 | // | ||
1253 | 323 | |||
1254 | 324 | void MprThread::threadProc() | ||
1255 | 325 | { | ||
1256 | 326 | osThreadId = GetCurrentThreadId(); | ||
1257 | 327 | pid = getpid(); | ||
1258 | 328 | (entry)(data, this); | ||
1259 | 329 | } | ||
1260 | 330 | |||
1261 | 331 | //////////////////////////////////////////////////////////////////////////////// | ||
1262 | 332 | |||
1263 | 333 | void MprThread::setPriority(int newPriority) | ||
1264 | 334 | { | ||
1265 | 335 | int osPri; | ||
1266 | 336 | |||
1267 | 337 | lock(); | ||
1268 | 338 | if (priority == newPriority) { | ||
1269 | 339 | unlock(); | ||
1270 | 340 | return; | ||
1271 | 341 | } | ||
1272 | 342 | |||
1273 | 343 | osPri = mapMprPriorityToOs(newPriority); | ||
1274 | 344 | SetThreadPriority(threadHandle, osPri); | ||
1275 | 345 | priority = newPriority; | ||
1276 | 346 | unlock(); | ||
1277 | 347 | } | ||
1278 | 348 | |||
1279 | 349 | //////////////////////////////////////////////////////////////////////////////// | ||
1280 | 350 | |||
1281 | 351 | void MprThread::setId(int id) | ||
1282 | 352 | { | ||
1283 | 353 | osThreadId = (ulong) id; | ||
1284 | 354 | } | ||
1285 | 355 | |||
1286 | 356 | //////////////////////////////////////////////////////////////////////////////// | ||
1287 | 357 | // | ||
1288 | 358 | // Map Mpr priority to Windows native priority. Windows priorities range from | ||
1289 | 359 | // -15 to +15 (zero is normal). Warning: +15 will not yield the CPU, -15 may | ||
1290 | 360 | // get starved. We should be very wary going above +11. | ||
1291 | 361 | // | ||
1292 | 362 | |||
1293 | 363 | int MprThread::mapMprPriorityToOs(int mprPriority) | ||
1294 | 364 | { | ||
1295 | 365 | mprAssert(mprPriority >= 0 && mprPriority <= 100); | ||
1296 | 366 | |||
1297 | 367 | if (mprPriority <= MPR_BACKGROUND_PRIORITY) { | ||
1298 | 368 | return THREAD_PRIORITY_LOWEST; | ||
1299 | 369 | } else if (mprPriority <= MPR_LOW_PRIORITY) { | ||
1300 | 370 | return THREAD_PRIORITY_BELOW_NORMAL; | ||
1301 | 371 | } else if (mprPriority <= MPR_NORMAL_PRIORITY) { | ||
1302 | 372 | return THREAD_PRIORITY_NORMAL; | ||
1303 | 373 | } else if (mprPriority <= MPR_HIGH_PRIORITY) { | ||
1304 | 374 | return THREAD_PRIORITY_ABOVE_NORMAL; | ||
1305 | 375 | } else { | ||
1306 | 376 | return THREAD_PRIORITY_HIGHEST; | ||
1307 | 377 | } | ||
1308 | 378 | } | ||
1309 | 379 | |||
1310 | 380 | /////////////////////////////////////////////////////////////////////////////// | ||
1311 | 381 | // | ||
1312 | 382 | // Map Windows priority to Mpr priority | ||
1313 | 383 | // | ||
1314 | 384 | |||
1315 | 385 | int MprThread::mapOsPriorityToMpr(int nativePriority) | ||
1316 | 386 | { | ||
1317 | 387 | int priority; | ||
1318 | 388 | |||
1319 | 389 | priority = (45 * nativePriority) + 50; | ||
1320 | 390 | if (priority < 0) { | ||
1321 | 391 | priority = 0; | ||
1322 | 392 | } | ||
1323 | 393 | if (priority >= 100) { | ||
1324 | 394 | priority = 99; | ||
1325 | 395 | } | ||
1326 | 396 | return priority; | ||
1327 | 397 | } | ||
1328 | 398 | |||
1329 | 399 | //////////////////////////////////////////////////////////////////////////////// | ||
1330 | 400 | /////////////////////////////////// MprMutex /////////////////////////////////// | ||
1331 | 401 | //////////////////////////////////////////////////////////////////////////////// | ||
1332 | 402 | // | ||
1333 | 403 | // Create a mutex | ||
1334 | 404 | // | ||
1335 | 405 | |||
1336 | 406 | MprMutex::MprMutex() | ||
1337 | 407 | { | ||
1338 | 408 | memset(&cs, 0, sizeof(cs)); | ||
1339 | 409 | InitializeCriticalSectionAndSpinCount(&cs, 5000); | ||
1340 | 410 | |||
1341 | 411 | #if BLD_DEBUG | ||
1342 | 412 | addMutex(this); | ||
1343 | 413 | cs.DebugInfo->Spare[DEBUG_ID] = -1; // Thread ID goes here | ||
1344 | 414 | cs.DebugInfo->Spare[DEBUG_TIME] = 0; // Time goes here | ||
1345 | 415 | #endif | ||
1346 | 416 | } | ||
1347 | 417 | |||
1348 | 418 | /////////////////////////////////////////////////////////////////////////////// | ||
1349 | 419 | // | ||
1350 | 420 | // Destroy a mutex. Must be locked on entrance. | ||
1351 | 421 | // | ||
1352 | 422 | |||
1353 | 423 | MprMutex::~MprMutex() | ||
1354 | 424 | { | ||
1355 | 425 | DeleteCriticalSection(&cs); | ||
1356 | 426 | #if BLD_DEBUG | ||
1357 | 427 | removeMutex(this); | ||
1358 | 428 | #endif | ||
1359 | 429 | } | ||
1360 | 430 | |||
1361 | 431 | /////////////////////////////////////////////////////////////////////////////// | ||
1362 | 432 | // | ||
1363 | 433 | // Lock a mutex | ||
1364 | 434 | // | ||
1365 | 435 | |||
1366 | 436 | void MprMutex::lock() | ||
1367 | 437 | { | ||
1368 | 438 | EnterCriticalSection(&cs); | ||
1369 | 439 | |||
1370 | 440 | #if BLD_DEBUG | ||
1371 | 441 | mprAssert(cs.RecursionCount >= 0); | ||
1372 | 442 | mprAssert(cs.RecursionCount < MPR_MAX_RECURSION); | ||
1373 | 443 | mprAssert(cs.LockCount < MPR_MAX_BLOCKED_LOCKS); | ||
1374 | 444 | |||
1375 | 445 | cs.DebugInfo->Spare[DEBUG_ID] = (ulong) GetCurrentThreadId(); | ||
1376 | 446 | cs.DebugInfo->Spare[DEBUG_TIME] = GetTickCount(); | ||
1377 | 447 | #endif | ||
1378 | 448 | } | ||
1379 | 449 | |||
1380 | 450 | /////////////////////////////////////////////////////////////////////////////// | ||
1381 | 451 | // | ||
1382 | 452 | // Try to lock a mutex. Do not block! | ||
1383 | 453 | // | ||
1384 | 454 | |||
1385 | 455 | int MprMutex::tryLock() | ||
1386 | 456 | { | ||
1387 | 457 | mprAssert(cs.RecursionCount >= 0); | ||
1388 | 458 | if (TryEnterCriticalSection(&cs) == 0) { | ||
1389 | 459 | return MPR_ERR_BUSY; | ||
1390 | 460 | } | ||
1391 | 461 | #if BLD_DEBUG | ||
1392 | 462 | cs.DebugInfo->Spare[DEBUG_ID] = (ulong) GetCurrentThreadId(); | ||
1393 | 463 | cs.DebugInfo->Spare[DEBUG_TIME] = GetTickCount(); | ||
1394 | 464 | #endif | ||
1395 | 465 | return 0; | ||
1396 | 466 | } | ||
1397 | 467 | |||
1398 | 468 | /////////////////////////////////////////////////////////////////////////////// | ||
1399 | 469 | // | ||
1400 | 470 | // Unlock a mutex. structure | ||
1401 | 471 | // | ||
1402 | 472 | |||
1403 | 473 | void MprMutex::unlock() | ||
1404 | 474 | { | ||
1405 | 475 | #if BLD_DEBUG | ||
1406 | 476 | mprAssert(cs.DebugInfo->Spare[DEBUG_ID] == (ulong) GetCurrentThreadId()); | ||
1407 | 477 | mprAssert(cs.RecursionCount > 0); | ||
1408 | 478 | mprAssert(cs.RecursionCount < MPR_MAX_RECURSION); | ||
1409 | 479 | mprAssert(cs.LockCount < MPR_MAX_LOCKS); | ||
1410 | 480 | cs.DebugInfo->Spare[DEBUG_TIME] = GetTickCount(); | ||
1411 | 481 | #endif | ||
1412 | 482 | LeaveCriticalSection(&cs); | ||
1413 | 483 | } | ||
1414 | 484 | |||
1415 | 485 | /////////////////////////////////////////////////////////////////////////////// | ||
1416 | 486 | /////////////////////////////////// MprCond //////////////////////////////////// | ||
1417 | 487 | //////////////////////////////////////////////////////////////////////////////// | ||
1418 | 488 | // | ||
1419 | 489 | // Create a condition variable for use by single or multiple waiters | ||
1420 | 490 | // | ||
1421 | 491 | |||
1422 | 492 | MprCond::MprCond() | ||
1423 | 493 | { | ||
1424 | 494 | #if UNUSED | ||
1425 | 495 | wakeAll = 0; | ||
1426 | 496 | numWaiting = 0; | ||
1427 | 497 | #endif | ||
1428 | 498 | triggered = 0; | ||
1429 | 499 | mutex = new MprMutex(); | ||
1430 | 500 | cv = CreateEvent(NULL, FALSE, FALSE, NULL); | ||
1431 | 501 | } | ||
1432 | 502 | |||
1433 | 503 | |||
1434 | 504 | //////////////////////////////////////////////////////////////////////////////// | ||
1435 | 505 | // | ||
1436 | 506 | // Destroy a condition variable | ||
1437 | 507 | // | ||
1438 | 508 | |||
1439 | 509 | MprCond::~MprCond() | ||
1440 | 510 | { | ||
1441 | 511 | mutex->lock(); | ||
1442 | 512 | CloseHandle(cv); | ||
1443 | 513 | delete mutex; | ||
1444 | 514 | } | ||
1445 | 515 | |||
1446 | 516 | //////////////////////////////////////////////////////////////////////////////// | ||
1447 | 517 | // | ||
1448 | 518 | // Wait for the event to be triggered. Should only be used when there are | ||
1449 | 519 | // single waiters. If the event is already triggered, then it will return | ||
1450 | 520 | // immediately. | ||
1451 | 521 | // | ||
1452 | 522 | |||
1453 | 523 | int MprCond::waitForCond(long timeout) | ||
1454 | 524 | { | ||
1455 | 525 | if (timeout < 0) { | ||
1456 | 526 | timeout = MAXINT; | ||
1457 | 527 | } | ||
1458 | 528 | |||
1459 | 529 | if (WaitForSingleObject(cv, timeout) != WAIT_OBJECT_0) { | ||
1460 | 530 | return -1; | ||
1461 | 531 | } | ||
1462 | 532 | |||
1463 | 533 | // | ||
1464 | 534 | // Reset the event | ||
1465 | 535 | // | ||
1466 | 536 | mutex->lock(); | ||
1467 | 537 | mprAssert(triggered != 0); | ||
1468 | 538 | triggered = 0; | ||
1469 | 539 | ResetEvent(cv); | ||
1470 | 540 | mutex->unlock(); | ||
1471 | 541 | |||
1472 | 542 | return 0; | ||
1473 | 543 | } | ||
1474 | 544 | |||
1475 | 545 | //////////////////////////////////////////////////////////////////////////////// | ||
1476 | 546 | #if UNUSED | ||
1477 | 547 | // | ||
1478 | 548 | // Wait for a condition variable to be signalled. Suitable for when there are | ||
1479 | 549 | // multiple waiters. It will work for single waiters, but is slower than | ||
1480 | 550 | // waitForCond() above. Depending on whether signalCond() or signalAll() is | ||
1481 | 551 | // used, a single waiter or multiple waiters may awake with one invocation of | ||
1482 | 552 | // signal. If the condition is already triggered, this routine will not block | ||
1483 | 553 | // for the first waiter. NOTE: the externalMutex must be defined and locked on | ||
1484 | 554 | // entry to this routine | ||
1485 | 555 | // | ||
1486 | 556 | |||
1487 | 557 | int MprCond::multiWait(MprMutex *externalMutex, long timeout) | ||
1488 | 558 | { | ||
1489 | 559 | int now, deadline; | ||
1490 | 560 | |||
1491 | 561 | if (timeout < 0) { | ||
1492 | 562 | timeout = MAXINT; | ||
1493 | 563 | } | ||
1494 | 564 | now = mprGetTime(0); | ||
1495 | 565 | deadline = now + timeout; | ||
1496 | 566 | |||
1497 | 567 | while (now <= deadline) { | ||
1498 | 568 | mutex->lock(); | ||
1499 | 569 | numWaiting++; | ||
1500 | 570 | mutex->unlock(); | ||
1501 | 571 | |||
1502 | 572 | externalMutex->unlock(); | ||
1503 | 573 | |||
1504 | 574 | if (WaitForSingleObject(cv, timeout) != WAIT_OBJECT_0) { | ||
1505 | 575 | return -1; | ||
1506 | 576 | } | ||
1507 | 577 | |||
1508 | 578 | mutex->lock(); | ||
1509 | 579 | --numWaiting; | ||
1510 | 580 | if (wakeAll) { | ||
1511 | 581 | // | ||
1512 | 582 | // Last thread to awake must reset the event | ||
1513 | 583 | // | ||
1514 | 584 | if (numWaiting == 0) { | ||
1515 | 585 | wakeAll = 0; | ||
1516 | 586 | ResetEvent(cv); | ||
1517 | 587 | } | ||
1518 | 588 | mutex->unlock(); | ||
1519 | 589 | break; | ||
1520 | 590 | } | ||
1521 | 591 | if (triggered) { | ||
1522 | 592 | triggered = 0; | ||
1523 | 593 | ResetEvent(cv); | ||
1524 | 594 | mutex->unlock(); | ||
1525 | 595 | break; | ||
1526 | 596 | } | ||
1527 | 597 | mutex->unlock(); | ||
1528 | 598 | now = mprGetTime(0); | ||
1529 | 599 | } | ||
1530 | 600 | externalMutex->lock(); | ||
1531 | 601 | return 0; | ||
1532 | 602 | } | ||
1533 | 603 | |||
1534 | 604 | #endif | ||
1535 | 605 | //////////////////////////////////////////////////////////////////////////////// | ||
1536 | 606 | // | ||
1537 | 607 | // Signal a condition and wakeup the waiter. Note: this may be called | ||
1538 | 608 | // prior to the waiter waiting. | ||
1539 | 609 | // | ||
1540 | 610 | |||
1541 | 611 | void MprCond::signalCond() | ||
1542 | 612 | { | ||
1543 | 613 | mutex->lock(); | ||
1544 | 614 | triggered = 1; | ||
1545 | 615 | #if UNUSED | ||
1546 | 616 | wakeAll = 0; | ||
1547 | 617 | #endif | ||
1548 | 618 | SetEvent(cv); | ||
1549 | 619 | mutex->unlock(); | ||
1550 | 620 | } | ||
1551 | 621 | |||
1552 | 622 | /////////////////////////////////////////////////////////////////////////////// | ||
1553 | 623 | #if UNUSED | ||
1554 | 624 | // | ||
1555 | 625 | // Signal all waiters. Note: this may be called prior to anyone waiting. | ||
1556 | 626 | // | ||
1557 | 627 | |||
1558 | 628 | void MprCond::signalAll() | ||
1559 | 629 | { | ||
1560 | 630 | mutex->lock(); | ||
1561 | 631 | triggered = 1; | ||
1562 | 632 | #if UNUSED | ||
1563 | 633 | wakeAll = 1; | ||
1564 | 634 | #endif | ||
1565 | 635 | SetEvent(cv); | ||
1566 | 636 | mutex->unlock(); | ||
1567 | 637 | } | ||
1568 | 638 | |||
1569 | 639 | /////////////////////////////////////////////////////////////////////////////// | ||
1570 | 640 | // | ||
1571 | 641 | // Use very carefully when you really know what you are doing. More dangerous | ||
1572 | 642 | // than it looks | ||
1573 | 643 | // | ||
1574 | 644 | |||
1575 | 645 | void MprCond::reset() | ||
1576 | 646 | { | ||
1577 | 647 | mutex->lock(); | ||
1578 | 648 | triggered = 0; | ||
1579 | 649 | #if UNUSED | ||
1580 | 650 | wakeAll = 0; | ||
1581 | 651 | #endif | ||
1582 | 652 | ResetEvent(cv); | ||
1583 | 653 | mutex->unlock(); | ||
1584 | 654 | } | ||
1585 | 655 | |||
1586 | 656 | #endif | ||
1587 | 657 | /////////////////////////////////////////////////////////////////////////////// | ||
1588 | 658 | ////////////////////////////////////// Debug ////////////////////////////////// | ||
1589 | 659 | /////////////////////////////////////////////////////////////////////////////// | ||
1590 | 660 | #if BLD_DEBUG | ||
1591 | 661 | #if UNUSED | ||
1592 | 662 | |||
1593 | 663 | static void lockBuster(void *data, MprThread *tp) | ||
1594 | 664 | { | ||
1595 | 665 | MprMutex *mp; | ||
1596 | 666 | int duration; | ||
1597 | 667 | uint tid, whenLocked, ticks; | ||
1598 | 668 | |||
1599 | 669 | tid = (int) GetCurrentThreadId(); | ||
1600 | 670 | while (! mpr->isExiting()) { | ||
1601 | 671 | if (numMutex > MPR_MAX_LOCKS) { | ||
1602 | 672 | mprError(MPR_L, MPR_LOG, "Too many mutexes"); | ||
1603 | 673 | } | ||
1604 | 674 | |||
1605 | 675 | mp = (MprMutex*) mutexList.getFirst(); | ||
1606 | 676 | while (mp) { | ||
1607 | 677 | whenLocked = mp->cs.DebugInfo->Spare[DEBUG_TIME]; | ||
1608 | 678 | |||
1609 | 679 | if (mp->cs.RecursionCount > 0) { | ||
1610 | 680 | ticks = GetTickCount(); | ||
1611 | 681 | if (ticks < whenLocked) { | ||
1612 | 682 | duration = 0xFFFFFFFF - whenLocked + 1 + ticks; | ||
1613 | 683 | } else { | ||
1614 | 684 | duration = ticks - whenLocked; | ||
1615 | 685 | } | ||
1616 | 686 | if (duration > MPR_MAX_LOCK_TIME) { | ||
1617 | 687 | mprError(MPR_L, MPR_LOG, "Mutex held too long"); | ||
1618 | 688 | // FUTURE -- could bust the lock here ... mp->unlock(); | ||
1619 | 689 | } | ||
1620 | 690 | } | ||
1621 | 691 | mp = (MprMutex*) mutexList.getNext(&mp->link); | ||
1622 | 692 | } | ||
1623 | 693 | mprSleep(500); | ||
1624 | 694 | } | ||
1625 | 695 | } | ||
1626 | 696 | |||
1627 | 697 | #endif // UNUSED | ||
1628 | 698 | /////////////////////////////////////////////////////////////////////////////// | ||
1629 | 699 | // | ||
1630 | 700 | // Add this mutex to the list | ||
1631 | 701 | // | ||
1632 | 702 | |||
1633 | 703 | static void addMutex(MprMutex *mp) | ||
1634 | 704 | { | ||
1635 | 705 | // | ||
1636 | 706 | // First time initialization | ||
1637 | 707 | // | ||
1638 | 708 | if (numMutex < 0) { | ||
1639 | 709 | memset(&debugCs, 0, sizeof(debugCs)); | ||
1640 | 710 | InitializeCriticalSectionAndSpinCount(&debugCs, 4000); | ||
1641 | 711 | } | ||
1642 | 712 | EnterCriticalSection(&debugCs); | ||
1643 | 713 | mutexList.insert(&mp->link); | ||
1644 | 714 | numMutex++; | ||
1645 | 715 | LeaveCriticalSection(&debugCs); | ||
1646 | 716 | } | ||
1647 | 717 | |||
1648 | 718 | /////////////////////////////////////////////////////////////////////////////// | ||
1649 | 719 | // | ||
1650 | 720 | // Remove this mutex from the list | ||
1651 | 721 | // | ||
1652 | 722 | |||
1653 | 723 | static void removeMutex(MprMutex *mp) | ||
1654 | 724 | { | ||
1655 | 725 | EnterCriticalSection(&debugCs); | ||
1656 | 726 | numMutex--; | ||
1657 | 727 | mutexList.remove(&mp->link); | ||
1658 | 728 | LeaveCriticalSection(&debugCs); | ||
1659 | 729 | } | ||
1660 | 730 | |||
1661 | 731 | /////////////////////////////////////////////////////////////////////////////// | ||
1662 | 732 | // | ||
1663 | 733 | // Return the number of mutexes owned by this thread | ||
1664 | 734 | // | ||
1665 | 735 | |||
1666 | 736 | int MprDebug::getMutexNum() | ||
1667 | 737 | { | ||
1668 | 738 | MprMutex *mp; | ||
1669 | 739 | int num; | ||
1670 | 740 | uint tid; | ||
1671 | 741 | |||
1672 | 742 | tid = (uint) GetCurrentThreadId(); | ||
1673 | 743 | |||
1674 | 744 | num = 0; | ||
1675 | 745 | mp = (MprMutex*) mutexList.getFirst(); | ||
1676 | 746 | while (mp) { | ||
1677 | 747 | |||
1678 | 748 | if (mp->cs.DebugInfo->Spare[DEBUG_ID] == tid) { | ||
1679 | 749 | num++; | ||
1680 | 750 | } | ||
1681 | 751 | mp = (MprMutex*) mutexList.getNext(&mp->link); | ||
1682 | 752 | } | ||
1683 | 753 | return num; | ||
1684 | 754 | } | ||
1685 | 755 | |||
1686 | 756 | #endif // BLD_DEBUG | ||
1687 | 757 | #endif // BLD_FEATURE_MULTITHREAD | ||
1688 | 758 | /////////////////////////////////////////////////////////////////////////////// | ||
1689 | 759 | |||
1690 | 760 | // | ||
1691 | 761 | // Local variables: | ||
1692 | 762 | // tab-width: 4 | ||
1693 | 763 | // c-basic-offset: 4 | ||
1694 | 764 | // End: | ||
1695 | 765 | // vim:tw=78 | ||
1696 | 766 | // vim600: sw=4 ts=4 fdm=marker | ||
1697 | 767 | // vim<600: sw=4 ts=4 | ||
1698 | 768 | // | ||
1699 | 0 | 769 | ||
1700 | === added file 'appweb.cnf' | |||
1701 | --- appweb.cnf 1970-01-01 00:00:00 +0000 | |||
1702 | +++ appweb.cnf 2011-11-23 01:01:25 +0000 | |||
1703 | @@ -0,0 +1,27 @@ | |||
1704 | 1 | ServerRoot "." | ||
1705 | 2 | DocumentRoot "." | ||
1706 | 3 | |||
1707 | 4 | LoadModulePath $SERVER_ROOT | ||
1708 | 5 | |||
1709 | 6 | LoadModule psiphonHandler libpsiphonHandler | ||
1710 | 7 | LoadModule ssl libsslModule | ||
1711 | 8 | LoadModule openSsl libopenSslModule | ||
1712 | 9 | |||
1713 | 10 | ThreadLimit 100 | ||
1714 | 11 | KeepAlive on | ||
1715 | 12 | |||
1716 | 13 | <VirtualHost *:*> | ||
1717 | 14 | AddHandler psiphonHandler | ||
1718 | 15 | DocumentRoot "." | ||
1719 | 16 | SSLEngine on | ||
1720 | 17 | SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL | ||
1721 | 18 | SSLProtocol ALL -SSLV2 | ||
1722 | 19 | |||
1723 | 20 | SSLCertificateFile "$SERVER_ROOT/cert.pem" | ||
1724 | 21 | SSLCertificateKeyFile "$SERVER_ROOT/key.pem" | ||
1725 | 22 | |||
1726 | 23 | <Location /> | ||
1727 | 24 | HttpChunking off | ||
1728 | 25 | SetHandler psiphonHandler | ||
1729 | 26 | </Location> | ||
1730 | 27 | </VirtualHost> | ||
1731 | 0 | 28 | ||
1732 | === added file 'bake-w32.bat' | |||
1733 | --- bake-w32.bat 1970-01-01 00:00:00 +0000 | |||
1734 | +++ bake-w32.bat 2011-11-23 01:01:25 +0000 | |||
1735 | @@ -0,0 +1,1 @@ | |||
1736 | 1 | bakefile -f msvc6prj psiphon.bkl | ||
1737 | 0 | \ No newline at end of file | 2 | \ No newline at end of file |
1738 | 1 | 3 | ||
1739 | === added file 'changes.txt' | |||
1740 | --- changes.txt 1970-01-01 00:00:00 +0000 | |||
1741 | +++ changes.txt 2011-11-23 01:01:25 +0000 | |||
1742 | @@ -0,0 +1,99 @@ | |||
1743 | 1 | ------------------------------------------- | ||
1744 | 2 | Changes in version 1.6 (09 Mar 2007) | ||
1745 | 3 | - added fingerprint to SSL certificate page | ||
1746 | 4 | - added update downloading | ||
1747 | 5 | - minor code revisions | ||
1748 | 6 | |||
1749 | 7 | ------------------------------------------- | ||
1750 | 8 | Changes in version 1.5 (09 Feb 2007) | ||
1751 | 9 | |||
1752 | 10 | - fixed '100% CPU usage' bug | ||
1753 | 11 | - added psiphon will find available port if 443 is not | ||
1754 | 12 | - added double click on a link in the log window opens it in default browser | ||
1755 | 13 | - added column sorting | ||
1756 | 14 | - added status icons | ||
1757 | 15 | - added choise for "Get My IP" server | ||
1758 | 16 | - added SLL country combo sorting | ||
1759 | 17 | - changed System Tray icon to be always available | ||
1760 | 18 | - minor GUI revisions | ||
1761 | 19 | - minor code revisions | ||
1762 | 20 | |||
1763 | 21 | ------------------------------------------- | ||
1764 | 22 | Changes in version 1.4 (26 Jan 2007) | ||
1765 | 23 | |||
1766 | 24 | - added minimize to task bar | ||
1767 | 25 | - added single bakefile file for both platforms(WIN, LINUX) | ||
1768 | 26 | - added detailed build instructions for both platforms | ||
1769 | 27 | - fixed compatibility with Shaw Secure | ||
1770 | 28 | - improved HTML entities decoding | ||
1771 | 29 | - minor code revisions | ||
1772 | 30 | |||
1773 | 31 | ------------------------------------------- | ||
1774 | 32 | Changes in version 1.3 (29 Dec 2006) | ||
1775 | 33 | |||
1776 | 34 | Added: | ||
1777 | 35 | - HTML entities decoding in the HTML filter | ||
1778 | 36 | |||
1779 | 37 | Changed: | ||
1780 | 38 | - Improved CSS filtering | ||
1781 | 39 | |||
1782 | 40 | - Improved error handling | ||
1783 | 41 | |||
1784 | 42 | - Limited user input | ||
1785 | 43 | |||
1786 | 44 | - Delete user related info when delete user | ||
1787 | 45 | |||
1788 | 46 | - Password overwrite bug fixed | ||
1789 | 47 | |||
1790 | 48 | - Switched to prepared SQL statements throughout the code | ||
1791 | 49 | |||
1792 | 50 | - Minor code revisions | ||
1793 | 51 | |||
1794 | 52 | ------------------------------------------- | ||
1795 | 53 | Changes in version 1.2 (15 Dec 2006) | ||
1796 | 54 | |||
1797 | 55 | Added: | ||
1798 | 56 | - Custom SSL certificate | ||
1799 | 57 | |||
1800 | 58 | - Log level persistence | ||
1801 | 59 | |||
1802 | 60 | - Log can be disabled | ||
1803 | 61 | |||
1804 | 62 | Changed: | ||
1805 | 63 | - Image on/off handling through "Javascript:void(0);" in order to avoid extra HTTP requests | ||
1806 | 64 | |||
1807 | 65 | - Updated wxWidgets framework to 2.8 | ||
1808 | 66 | |||
1809 | 67 | - Updated Advanced Installer to 4.6 | ||
1810 | 68 | |||
1811 | 69 | - Fixed several buffer overflow vulnerabilities | ||
1812 | 70 | |||
1813 | 71 | ------------------------------------------- | ||
1814 | 72 | Changes in version 1.1 (8 Dec 2006) | ||
1815 | 73 | |||
1816 | 74 | Added: | ||
1817 | 75 | - psiphon access is forbidden (HTTP 403) to domains that resolve to localhost or | ||
1818 | 76 | private network | ||
1819 | 77 | |||
1820 | 78 | - Multiple network interface cards support | ||
1821 | 79 | |||
1822 | 80 | - Our own external IP detection service | ||
1823 | 81 | |||
1824 | 82 | - Version check on startup | ||
1825 | 83 | |||
1826 | 84 | - Users can toggle images on/off in the blue bar | ||
1827 | 85 | |||
1828 | 86 | Changed: | ||
1829 | 87 | - URLs in HTTP requests to psiphon must be prepended with psiphonode name | ||
1830 | 88 | set by psiphon admin at setup, i.e. https://<IP>:<port>/<psiphonode>/login/.., | ||
1831 | 89 | https://<IP>:<port>/<psiphonode>/fetch/.. etc. | ||
1832 | 90 | |||
1833 | 91 | - Replacement tags (<%varname%>) added in HTML templates. | ||
1834 | 92 | |||
1835 | 93 | - wxWidgets HTTP headers are replaced with generic headers | ||
1836 | 94 | |||
1837 | 95 | - New database design in order to minimize SQLite locking | ||
1838 | 96 | |||
1839 | 97 | - GUI changes | ||
1840 | 98 | |||
1841 | 99 | - Minor bug fixes | ||
1842 | 0 | \ No newline at end of file | 100 | \ No newline at end of file |
1843 | 1 | 101 | ||
1844 | === added directory 'common' | |||
1845 | === added file 'common/CppSQLite3.cpp' | |||
1846 | --- common/CppSQLite3.cpp 1970-01-01 00:00:00 +0000 | |||
1847 | +++ common/CppSQLite3.cpp 2011-11-23 01:01:25 +0000 | |||
1848 | @@ -0,0 +1,1383 @@ | |||
1849 | 1 | //////////////////////////////////////////////////////////////////////////////// | ||
1850 | 2 | // CppSQLite3 - A C++ wrapper around the SQLite3 embedded database library. | ||
1851 | 3 | // | ||
1852 | 4 | // Copyright (c) 2004 Rob Groves. All Rights Reserved. rob.groves@btinternet.com | ||
1853 | 5 | // | ||
1854 | 6 | // Permission to use, copy, modify, and distribute this software and its | ||
1855 | 7 | // documentation for any purpose, without fee, and without a written | ||
1856 | 8 | // agreement, is hereby granted, provided that the above copyright notice, | ||
1857 | 9 | // this paragraph and the following two paragraphs appear in all copies, | ||
1858 | 10 | // modifications, and distributions. | ||
1859 | 11 | // | ||
1860 | 12 | // IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, | ||
1861 | 13 | // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST | ||
1862 | 14 | // PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, | ||
1863 | 15 | // EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
1864 | 16 | // | ||
1865 | 17 | // THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT | ||
1866 | 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
1867 | 19 | // PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF | ||
1868 | 20 | // ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". THE AUTHOR HAS NO OBLIGATION | ||
1869 | 21 | // TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | ||
1870 | 22 | // | ||
1871 | 23 | // V3.0 03/08/2004 -Initial Version for sqlite3 | ||
1872 | 24 | // | ||
1873 | 25 | // V3.1 16/09/2004 -Implemented getXXXXField using sqlite3 functions | ||
1874 | 26 | // -Added CppSQLiteDB3::tableExists() | ||
1875 | 27 | //////////////////////////////////////////////////////////////////////////////// | ||
1876 | 28 | #include "CppSQLite3.h" | ||
1877 | 29 | #include <cstdlib> | ||
1878 | 30 | |||
1879 | 31 | // Named constant for passing to CppSQLite3Exception when passing it a string | ||
1880 | 32 | // that cannot be deleted. | ||
1881 | 33 | static const bool DONT_DELETE_MSG = false; | ||
1882 | 34 | |||
1883 | 35 | //////////////////////////////////////////////////////////////////////////////// | ||
1884 | 36 | // Prototypes for SQLite functions not included in SQLite DLL, but copied below | ||
1885 | 37 | // from SQLite encode.c | ||
1886 | 38 | //////////////////////////////////////////////////////////////////////////////// | ||
1887 | 39 | int sqlite3_encode_binary(const unsigned char* in, int n, unsigned char* out); | ||
1888 | 40 | int sqlite3_decode_binary(const unsigned char* in, unsigned char* out); | ||
1889 | 41 | |||
1890 | 42 | //////////////////////////////////////////////////////////////////////////////// | ||
1891 | 43 | |||
1892 | 44 | //////////////////////////////////////////////////////////////////////////////// | ||
1893 | 45 | |||
1894 | 46 | CppSQLite3Exception::CppSQLite3Exception(const int nErrCode, char* szErrMess, bool bDeleteMsg/*=true*/) : mnErrCode(nErrCode) | ||
1895 | 47 | { | ||
1896 | 48 | mpszErrMess = sqlite3_mprintf("%s[%d]: %s", errorCodeAsString(nErrCode), nErrCode, szErrMess ? szErrMess : ""); | ||
1897 | 49 | |||
1898 | 50 | if(bDeleteMsg && szErrMess) | ||
1899 | 51 | { | ||
1900 | 52 | sqlite3_free(szErrMess); | ||
1901 | 53 | } | ||
1902 | 54 | } | ||
1903 | 55 | |||
1904 | 56 | CppSQLite3Exception::CppSQLite3Exception(const CppSQLite3Exception& e) : mnErrCode(e.mnErrCode) | ||
1905 | 57 | { | ||
1906 | 58 | mpszErrMess = 0; | ||
1907 | 59 | if(e.mpszErrMess) | ||
1908 | 60 | { | ||
1909 | 61 | mpszErrMess = sqlite3_mprintf("%s", e.mpszErrMess); | ||
1910 | 62 | } | ||
1911 | 63 | } | ||
1912 | 64 | |||
1913 | 65 | const char* CppSQLite3Exception::errorCodeAsString(int nErrCode) | ||
1914 | 66 | { | ||
1915 | 67 | switch(nErrCode) | ||
1916 | 68 | { | ||
1917 | 69 | case SQLITE_OK : | ||
1918 | 70 | return "SQLITE_OK"; | ||
1919 | 71 | case SQLITE_ERROR : | ||
1920 | 72 | return "SQLITE_ERROR"; | ||
1921 | 73 | case SQLITE_INTERNAL : | ||
1922 | 74 | return "SQLITE_INTERNAL"; | ||
1923 | 75 | case SQLITE_PERM : | ||
1924 | 76 | return "SQLITE_PERM"; | ||
1925 | 77 | case SQLITE_ABORT : | ||
1926 | 78 | return "SQLITE_ABORT"; | ||
1927 | 79 | case SQLITE_BUSY : | ||
1928 | 80 | return "SQLITE_BUSY"; | ||
1929 | 81 | case SQLITE_LOCKED : | ||
1930 | 82 | return "SQLITE_LOCKED"; | ||
1931 | 83 | case SQLITE_NOMEM : | ||
1932 | 84 | return "SQLITE_NOMEM"; | ||
1933 | 85 | case SQLITE_READONLY : | ||
1934 | 86 | return "SQLITE_READONLY"; | ||
1935 | 87 | case SQLITE_INTERRUPT : | ||
1936 | 88 | return "SQLITE_INTERRUPT"; | ||
1937 | 89 | case SQLITE_IOERR : | ||
1938 | 90 | return "SQLITE_IOERR"; | ||
1939 | 91 | case SQLITE_CORRUPT : | ||
1940 | 92 | return "SQLITE_CORRUPT"; | ||
1941 | 93 | case SQLITE_NOTFOUND : | ||
1942 | 94 | return "SQLITE_NOTFOUND"; | ||
1943 | 95 | case SQLITE_FULL : | ||
1944 | 96 | return "SQLITE_FULL"; | ||
1945 | 97 | case SQLITE_CANTOPEN : | ||
1946 | 98 | return "SQLITE_CANTOPEN"; | ||
1947 | 99 | case SQLITE_PROTOCOL : | ||
1948 | 100 | return "SQLITE_PROTOCOL"; | ||
1949 | 101 | case SQLITE_EMPTY : | ||
1950 | 102 | return "SQLITE_EMPTY"; | ||
1951 | 103 | case SQLITE_SCHEMA : | ||
1952 | 104 | return "SQLITE_SCHEMA"; | ||
1953 | 105 | case SQLITE_TOOBIG : | ||
1954 | 106 | return "SQLITE_TOOBIG"; | ||
1955 | 107 | case SQLITE_CONSTRAINT : | ||
1956 | 108 | return "SQLITE_CONSTRAINT"; | ||
1957 | 109 | case SQLITE_MISMATCH : | ||
1958 | 110 | return "SQLITE_MISMATCH"; | ||
1959 | 111 | case SQLITE_MISUSE : | ||
1960 | 112 | return "SQLITE_MISUSE"; | ||
1961 | 113 | case SQLITE_NOLFS : | ||
1962 | 114 | return "SQLITE_NOLFS"; | ||
1963 | 115 | case SQLITE_AUTH : | ||
1964 | 116 | return "SQLITE_AUTH"; | ||
1965 | 117 | case SQLITE_FORMAT : | ||
1966 | 118 | return "SQLITE_FORMAT"; | ||
1967 | 119 | case SQLITE_RANGE : | ||
1968 | 120 | return "SQLITE_RANGE"; | ||
1969 | 121 | case SQLITE_ROW : | ||
1970 | 122 | return "SQLITE_ROW"; | ||
1971 | 123 | case SQLITE_DONE : | ||
1972 | 124 | return "SQLITE_DONE"; | ||
1973 | 125 | case CPPSQLITE_ERROR : | ||
1974 | 126 | return "CPPSQLITE_ERROR"; | ||
1975 | 127 | default: | ||
1976 | 128 | return "UNKNOWN_ERROR"; | ||
1977 | 129 | } | ||
1978 | 130 | } | ||
1979 | 131 | |||
1980 | 132 | CppSQLite3Exception::~CppSQLite3Exception() | ||
1981 | 133 | { | ||
1982 | 134 | if(mpszErrMess) | ||
1983 | 135 | { | ||
1984 | 136 | sqlite3_free(mpszErrMess); | ||
1985 | 137 | mpszErrMess = 0; | ||
1986 | 138 | } | ||
1987 | 139 | } | ||
1988 | 140 | |||
1989 | 141 | //////////////////////////////////////////////////////////////////////////////// | ||
1990 | 142 | |||
1991 | 143 | CppSQLite3Buffer::CppSQLite3Buffer() | ||
1992 | 144 | { | ||
1993 | 145 | mpBuf = 0; | ||
1994 | 146 | } | ||
1995 | 147 | |||
1996 | 148 | CppSQLite3Buffer::~CppSQLite3Buffer() | ||
1997 | 149 | { | ||
1998 | 150 | clear(); | ||
1999 | 151 | } | ||
2000 | 152 | |||
2001 | 153 | void CppSQLite3Buffer::clear() | ||
2002 | 154 | { | ||
2003 | 155 | if(mpBuf) | ||
2004 | 156 | { | ||
2005 | 157 | sqlite3_free(mpBuf); | ||
2006 | 158 | mpBuf = 0; | ||
2007 | 159 | } | ||
2008 | 160 | } | ||
2009 | 161 | |||
2010 | 162 | const char* CppSQLite3Buffer::format(const char* szFormat, ...) | ||
2011 | 163 | { | ||
2012 | 164 | clear(); | ||
2013 | 165 | va_list va; | ||
2014 | 166 | va_start(va, szFormat); | ||
2015 | 167 | mpBuf = sqlite3_vmprintf(szFormat, va); | ||
2016 | 168 | va_end(va); | ||
2017 | 169 | return mpBuf; | ||
2018 | 170 | } | ||
2019 | 171 | |||
2020 | 172 | //////////////////////////////////////////////////////////////////////////////// | ||
2021 | 173 | |||
2022 | 174 | CppSQLite3Binary::CppSQLite3Binary() : mpBuf(0), mnBinaryLen(0), mnBufferLen(0), mnEncodedLen(0), mbEncoded(false) | ||
2023 | 175 | { | ||
2024 | 176 | } | ||
2025 | 177 | |||
2026 | 178 | CppSQLite3Binary::~CppSQLite3Binary() | ||
2027 | 179 | { | ||
2028 | 180 | clear(); | ||
2029 | 181 | } | ||
2030 | 182 | |||
2031 | 183 | void CppSQLite3Binary::setBinary(const unsigned char* pBuf, int nLen) | ||
2032 | 184 | { | ||
2033 | 185 | mpBuf = allocBuffer(nLen); | ||
2034 | 186 | memcpy(mpBuf, pBuf, nLen); | ||
2035 | 187 | } | ||
2036 | 188 | |||
2037 | 189 | void CppSQLite3Binary::setEncoded(const unsigned char* pBuf) | ||
2038 | 190 | { | ||
2039 | 191 | clear(); | ||
2040 | 192 | |||
2041 | 193 | mnEncodedLen = strlen((const char*)pBuf); | ||
2042 | 194 | mnBufferLen = mnEncodedLen + 1; // Allow for NULL terminator | ||
2043 | 195 | |||
2044 | 196 | mpBuf = (unsigned char*)malloc(mnBufferLen); | ||
2045 | 197 | |||
2046 | 198 | if(!mpBuf) | ||
2047 | 199 | { | ||
2048 | 200 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Cannot allocate memory", DONT_DELETE_MSG); | ||
2049 | 201 | } | ||
2050 | 202 | |||
2051 | 203 | memcpy(mpBuf, pBuf, mnBufferLen); | ||
2052 | 204 | mbEncoded = true; | ||
2053 | 205 | } | ||
2054 | 206 | |||
2055 | 207 | const unsigned char* CppSQLite3Binary::getEncoded() | ||
2056 | 208 | { | ||
2057 | 209 | if(!mbEncoded) | ||
2058 | 210 | { | ||
2059 | 211 | unsigned char* ptmp = (unsigned char*)malloc(mnBinaryLen); | ||
2060 | 212 | memcpy(ptmp, mpBuf, mnBinaryLen); | ||
2061 | 213 | mnEncodedLen = sqlite3_encode_binary(ptmp, mnBinaryLen, mpBuf); | ||
2062 | 214 | free(ptmp); | ||
2063 | 215 | mbEncoded = true; | ||
2064 | 216 | } | ||
2065 | 217 | |||
2066 | 218 | return mpBuf; | ||
2067 | 219 | } | ||
2068 | 220 | |||
2069 | 221 | const unsigned char* CppSQLite3Binary::getBinary() | ||
2070 | 222 | { | ||
2071 | 223 | if(mbEncoded) | ||
2072 | 224 | { | ||
2073 | 225 | // in/out buffers can be the same | ||
2074 | 226 | mnBinaryLen = sqlite3_decode_binary(mpBuf, mpBuf); | ||
2075 | 227 | |||
2076 | 228 | if(mnBinaryLen == -1) | ||
2077 | 229 | { | ||
2078 | 230 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Cannot decode binary", DONT_DELETE_MSG); | ||
2079 | 231 | } | ||
2080 | 232 | |||
2081 | 233 | mbEncoded = false; | ||
2082 | 234 | } | ||
2083 | 235 | |||
2084 | 236 | return mpBuf; | ||
2085 | 237 | } | ||
2086 | 238 | |||
2087 | 239 | int CppSQLite3Binary::getBinaryLength() | ||
2088 | 240 | { | ||
2089 | 241 | getBinary(); | ||
2090 | 242 | return mnBinaryLen; | ||
2091 | 243 | } | ||
2092 | 244 | |||
2093 | 245 | unsigned char* CppSQLite3Binary::allocBuffer(int nLen) | ||
2094 | 246 | { | ||
2095 | 247 | clear(); | ||
2096 | 248 | |||
2097 | 249 | // Allow extra space for encoded binary as per comments in | ||
2098 | 250 | // SQLite encode.c See bottom of this file for implementation | ||
2099 | 251 | // of SQLite functions use 3 instead of 2 just to be sure ;-) | ||
2100 | 252 | mnBinaryLen = nLen; | ||
2101 | 253 | mnBufferLen = 3 + (257 * nLen) / 254; | ||
2102 | 254 | |||
2103 | 255 | mpBuf = (unsigned char*)malloc(mnBufferLen); | ||
2104 | 256 | |||
2105 | 257 | if(!mpBuf) | ||
2106 | 258 | { | ||
2107 | 259 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Cannot allocate memory", DONT_DELETE_MSG); | ||
2108 | 260 | } | ||
2109 | 261 | |||
2110 | 262 | mbEncoded = false; | ||
2111 | 263 | |||
2112 | 264 | return mpBuf; | ||
2113 | 265 | } | ||
2114 | 266 | |||
2115 | 267 | void CppSQLite3Binary::clear() | ||
2116 | 268 | { | ||
2117 | 269 | if(mpBuf) | ||
2118 | 270 | { | ||
2119 | 271 | mnBinaryLen = 0; | ||
2120 | 272 | mnBufferLen = 0; | ||
2121 | 273 | free(mpBuf); | ||
2122 | 274 | mpBuf = 0; | ||
2123 | 275 | } | ||
2124 | 276 | } | ||
2125 | 277 | |||
2126 | 278 | //////////////////////////////////////////////////////////////////////////////// | ||
2127 | 279 | |||
2128 | 280 | CppSQLite3Query::CppSQLite3Query() | ||
2129 | 281 | { | ||
2130 | 282 | mpVM = 0; | ||
2131 | 283 | mbEof = true; | ||
2132 | 284 | mnCols = 0; | ||
2133 | 285 | mbOwnVM = false; | ||
2134 | 286 | } | ||
2135 | 287 | |||
2136 | 288 | CppSQLite3Query::CppSQLite3Query(const CppSQLite3Query& rQuery) | ||
2137 | 289 | { | ||
2138 | 290 | mpVM = rQuery.mpVM; | ||
2139 | 291 | // Only one object can own the VM | ||
2140 | 292 | const_cast<CppSQLite3Query&>(rQuery).mpVM = 0; | ||
2141 | 293 | mbEof = rQuery.mbEof; | ||
2142 | 294 | mnCols = rQuery.mnCols; | ||
2143 | 295 | mbOwnVM = rQuery.mbOwnVM; | ||
2144 | 296 | } | ||
2145 | 297 | |||
2146 | 298 | CppSQLite3Query::CppSQLite3Query(sqlite3* pDB, sqlite3_stmt* pVM, bool bEof, bool bOwnVM/*=true*/) | ||
2147 | 299 | { | ||
2148 | 300 | mpDB = pDB; | ||
2149 | 301 | mpVM = pVM; | ||
2150 | 302 | mbEof = bEof; | ||
2151 | 303 | mnCols = sqlite3_column_count(mpVM); | ||
2152 | 304 | mbOwnVM = bOwnVM; | ||
2153 | 305 | } | ||
2154 | 306 | |||
2155 | 307 | CppSQLite3Query::~CppSQLite3Query() | ||
2156 | 308 | { | ||
2157 | 309 | try | ||
2158 | 310 | { | ||
2159 | 311 | finalize(); | ||
2160 | 312 | } | ||
2161 | 313 | catch(...) | ||
2162 | 314 | { | ||
2163 | 315 | } | ||
2164 | 316 | } | ||
2165 | 317 | |||
2166 | 318 | CppSQLite3Query& CppSQLite3Query::operator=(const CppSQLite3Query& rQuery) | ||
2167 | 319 | { | ||
2168 | 320 | try | ||
2169 | 321 | { | ||
2170 | 322 | finalize(); | ||
2171 | 323 | } | ||
2172 | 324 | catch(...) | ||
2173 | 325 | { | ||
2174 | 326 | } | ||
2175 | 327 | mpVM = rQuery.mpVM; | ||
2176 | 328 | // Only one object can own the VM | ||
2177 | 329 | const_cast<CppSQLite3Query&>(rQuery).mpVM = 0; | ||
2178 | 330 | mbEof = rQuery.mbEof; | ||
2179 | 331 | mnCols = rQuery.mnCols; | ||
2180 | 332 | mbOwnVM = rQuery.mbOwnVM; | ||
2181 | 333 | return *this; | ||
2182 | 334 | } | ||
2183 | 335 | |||
2184 | 336 | int CppSQLite3Query::numFields() | ||
2185 | 337 | { | ||
2186 | 338 | checkVM(); | ||
2187 | 339 | return mnCols; | ||
2188 | 340 | } | ||
2189 | 341 | |||
2190 | 342 | const char* CppSQLite3Query::fieldValue(int nField) | ||
2191 | 343 | { | ||
2192 | 344 | checkVM(); | ||
2193 | 345 | |||
2194 | 346 | if(nField < 0 || nField > mnCols - 1) | ||
2195 | 347 | { | ||
2196 | 348 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid field index requested", DONT_DELETE_MSG); | ||
2197 | 349 | } | ||
2198 | 350 | |||
2199 | 351 | return (const char*)sqlite3_column_text(mpVM, nField); | ||
2200 | 352 | } | ||
2201 | 353 | |||
2202 | 354 | const char* CppSQLite3Query::fieldValue(const char* szField) | ||
2203 | 355 | { | ||
2204 | 356 | int nField = fieldIndex(szField); | ||
2205 | 357 | return (const char*)sqlite3_column_text(mpVM, nField); | ||
2206 | 358 | } | ||
2207 | 359 | |||
2208 | 360 | int CppSQLite3Query::getIntField(int nField, int nNullValue/*=0*/) | ||
2209 | 361 | { | ||
2210 | 362 | if(fieldDataType(nField) == SQLITE_NULL) | ||
2211 | 363 | { | ||
2212 | 364 | return nNullValue; | ||
2213 | 365 | } | ||
2214 | 366 | else | ||
2215 | 367 | { | ||
2216 | 368 | return sqlite3_column_int(mpVM, nField); | ||
2217 | 369 | } | ||
2218 | 370 | } | ||
2219 | 371 | |||
2220 | 372 | int CppSQLite3Query::getIntField(const char* szField, int nNullValue/*=0*/) | ||
2221 | 373 | { | ||
2222 | 374 | int nField = fieldIndex(szField); | ||
2223 | 375 | return getIntField(nField, nNullValue); | ||
2224 | 376 | } | ||
2225 | 377 | |||
2226 | 378 | double CppSQLite3Query::getFloatField(int nField, double fNullValue/*=0.0*/) | ||
2227 | 379 | { | ||
2228 | 380 | if(fieldDataType(nField) == SQLITE_NULL) | ||
2229 | 381 | { | ||
2230 | 382 | return fNullValue; | ||
2231 | 383 | } | ||
2232 | 384 | else | ||
2233 | 385 | { | ||
2234 | 386 | return sqlite3_column_double(mpVM, nField); | ||
2235 | 387 | } | ||
2236 | 388 | } | ||
2237 | 389 | |||
2238 | 390 | double CppSQLite3Query::getFloatField(const char* szField, double fNullValue/*=0.0*/) | ||
2239 | 391 | { | ||
2240 | 392 | int nField = fieldIndex(szField); | ||
2241 | 393 | return getFloatField(nField, fNullValue); | ||
2242 | 394 | } | ||
2243 | 395 | |||
2244 | 396 | const char* CppSQLite3Query::getStringField(int nField, const char* szNullValue/*=""*/) | ||
2245 | 397 | { | ||
2246 | 398 | if(fieldDataType(nField) == SQLITE_NULL) | ||
2247 | 399 | { | ||
2248 | 400 | return szNullValue; | ||
2249 | 401 | } | ||
2250 | 402 | else | ||
2251 | 403 | { | ||
2252 | 404 | return (const char*)sqlite3_column_text(mpVM, nField); | ||
2253 | 405 | } | ||
2254 | 406 | } | ||
2255 | 407 | |||
2256 | 408 | const char* CppSQLite3Query::getStringField(const char* szField, const char* szNullValue/*=""*/) | ||
2257 | 409 | { | ||
2258 | 410 | int nField = fieldIndex(szField); | ||
2259 | 411 | return getStringField(nField, szNullValue); | ||
2260 | 412 | } | ||
2261 | 413 | |||
2262 | 414 | const unsigned char* CppSQLite3Query::getBlobField(int nField, int& nLen) | ||
2263 | 415 | { | ||
2264 | 416 | checkVM(); | ||
2265 | 417 | |||
2266 | 418 | if(nField < 0 || nField > mnCols - 1) | ||
2267 | 419 | { | ||
2268 | 420 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid field index requested", DONT_DELETE_MSG); | ||
2269 | 421 | } | ||
2270 | 422 | |||
2271 | 423 | nLen = sqlite3_column_bytes(mpVM, nField); | ||
2272 | 424 | return (const unsigned char*)sqlite3_column_blob(mpVM, nField); | ||
2273 | 425 | } | ||
2274 | 426 | |||
2275 | 427 | const unsigned char* CppSQLite3Query::getBlobField(const char* szField, int& nLen) | ||
2276 | 428 | { | ||
2277 | 429 | int nField = fieldIndex(szField); | ||
2278 | 430 | return getBlobField(nField, nLen); | ||
2279 | 431 | } | ||
2280 | 432 | |||
2281 | 433 | bool CppSQLite3Query::fieldIsNull(int nField) | ||
2282 | 434 | { | ||
2283 | 435 | return (fieldDataType(nField) == SQLITE_NULL); | ||
2284 | 436 | } | ||
2285 | 437 | |||
2286 | 438 | bool CppSQLite3Query::fieldIsNull(const char* szField) | ||
2287 | 439 | { | ||
2288 | 440 | int nField = fieldIndex(szField); | ||
2289 | 441 | return (fieldDataType(nField) == SQLITE_NULL); | ||
2290 | 442 | } | ||
2291 | 443 | |||
2292 | 444 | int CppSQLite3Query::fieldIndex(const char* szField) | ||
2293 | 445 | { | ||
2294 | 446 | checkVM(); | ||
2295 | 447 | |||
2296 | 448 | if(szField) | ||
2297 | 449 | { | ||
2298 | 450 | for(int nField = 0;nField < mnCols;nField++) | ||
2299 | 451 | { | ||
2300 | 452 | const char* szTemp = sqlite3_column_name(mpVM, nField); | ||
2301 | 453 | |||
2302 | 454 | if(strcmp(szField, szTemp) == 0) | ||
2303 | 455 | { | ||
2304 | 456 | return nField; | ||
2305 | 457 | } | ||
2306 | 458 | } | ||
2307 | 459 | } | ||
2308 | 460 | |||
2309 | 461 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid field name requested", DONT_DELETE_MSG); | ||
2310 | 462 | } | ||
2311 | 463 | |||
2312 | 464 | const char* CppSQLite3Query::fieldName(int nCol) | ||
2313 | 465 | { | ||
2314 | 466 | checkVM(); | ||
2315 | 467 | |||
2316 | 468 | if(nCol < 0 || nCol > mnCols - 1) | ||
2317 | 469 | { | ||
2318 | 470 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid field index requested", DONT_DELETE_MSG); | ||
2319 | 471 | } | ||
2320 | 472 | |||
2321 | 473 | return sqlite3_column_name(mpVM, nCol); | ||
2322 | 474 | } | ||
2323 | 475 | |||
2324 | 476 | const char* CppSQLite3Query::fieldDeclType(int nCol) | ||
2325 | 477 | { | ||
2326 | 478 | checkVM(); | ||
2327 | 479 | |||
2328 | 480 | if(nCol < 0 || nCol > mnCols - 1) | ||
2329 | 481 | { | ||
2330 | 482 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid field index requested", DONT_DELETE_MSG); | ||
2331 | 483 | } | ||
2332 | 484 | |||
2333 | 485 | return sqlite3_column_decltype(mpVM, nCol); | ||
2334 | 486 | } | ||
2335 | 487 | |||
2336 | 488 | int CppSQLite3Query::fieldDataType(int nCol) | ||
2337 | 489 | { | ||
2338 | 490 | checkVM(); | ||
2339 | 491 | |||
2340 | 492 | if(nCol < 0 || nCol > mnCols - 1) | ||
2341 | 493 | { | ||
2342 | 494 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid field index requested", DONT_DELETE_MSG); | ||
2343 | 495 | } | ||
2344 | 496 | |||
2345 | 497 | return sqlite3_column_type(mpVM, nCol); | ||
2346 | 498 | } | ||
2347 | 499 | |||
2348 | 500 | bool CppSQLite3Query::eof() | ||
2349 | 501 | { | ||
2350 | 502 | checkVM(); | ||
2351 | 503 | return mbEof; | ||
2352 | 504 | } | ||
2353 | 505 | |||
2354 | 506 | void CppSQLite3Query::nextRow() | ||
2355 | 507 | { | ||
2356 | 508 | checkVM(); | ||
2357 | 509 | |||
2358 | 510 | int nRet = sqlite3_step(mpVM); | ||
2359 | 511 | |||
2360 | 512 | if(nRet == SQLITE_DONE) | ||
2361 | 513 | { | ||
2362 | 514 | // no rows | ||
2363 | 515 | mbEof = true; | ||
2364 | 516 | } | ||
2365 | 517 | else if(nRet == SQLITE_ROW) | ||
2366 | 518 | { | ||
2367 | 519 | // more rows, nothing to do | ||
2368 | 520 | } | ||
2369 | 521 | else | ||
2370 | 522 | { | ||
2371 | 523 | nRet = sqlite3_finalize(mpVM); | ||
2372 | 524 | mpVM = 0; | ||
2373 | 525 | const char* szError = sqlite3_errmsg(mpDB); | ||
2374 | 526 | throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG); | ||
2375 | 527 | } | ||
2376 | 528 | } | ||
2377 | 529 | |||
2378 | 530 | void CppSQLite3Query::finalize() | ||
2379 | 531 | { | ||
2380 | 532 | if(mpVM && mbOwnVM) | ||
2381 | 533 | { | ||
2382 | 534 | int nRet = sqlite3_finalize(mpVM); | ||
2383 | 535 | mpVM = 0; | ||
2384 | 536 | if(nRet != SQLITE_OK) | ||
2385 | 537 | { | ||
2386 | 538 | const char* szError = sqlite3_errmsg(mpDB); | ||
2387 | 539 | throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG); | ||
2388 | 540 | } | ||
2389 | 541 | } | ||
2390 | 542 | } | ||
2391 | 543 | |||
2392 | 544 | void CppSQLite3Query::checkVM() | ||
2393 | 545 | { | ||
2394 | 546 | if(mpVM == 0) | ||
2395 | 547 | { | ||
2396 | 548 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Null Virtual Machine pointer", DONT_DELETE_MSG); | ||
2397 | 549 | } | ||
2398 | 550 | } | ||
2399 | 551 | |||
2400 | 552 | //////////////////////////////////////////////////////////////////////////////// | ||
2401 | 553 | |||
2402 | 554 | CppSQLite3Table::CppSQLite3Table() | ||
2403 | 555 | { | ||
2404 | 556 | mpaszResults = 0; | ||
2405 | 557 | mnRows = 0; | ||
2406 | 558 | mnCols = 0; | ||
2407 | 559 | mnCurrentRow = 0; | ||
2408 | 560 | } | ||
2409 | 561 | |||
2410 | 562 | CppSQLite3Table::CppSQLite3Table(const CppSQLite3Table& rTable) | ||
2411 | 563 | { | ||
2412 | 564 | mpaszResults = rTable.mpaszResults; | ||
2413 | 565 | // Only one object can own the results | ||
2414 | 566 | const_cast<CppSQLite3Table&>(rTable).mpaszResults = 0; | ||
2415 | 567 | mnRows = rTable.mnRows; | ||
2416 | 568 | mnCols = rTable.mnCols; | ||
2417 | 569 | mnCurrentRow = rTable.mnCurrentRow; | ||
2418 | 570 | } | ||
2419 | 571 | |||
2420 | 572 | CppSQLite3Table::CppSQLite3Table(char** paszResults, int nRows, int nCols) | ||
2421 | 573 | { | ||
2422 | 574 | mpaszResults = paszResults; | ||
2423 | 575 | mnRows = nRows; | ||
2424 | 576 | mnCols = nCols; | ||
2425 | 577 | mnCurrentRow = 0; | ||
2426 | 578 | } | ||
2427 | 579 | |||
2428 | 580 | CppSQLite3Table::~CppSQLite3Table() | ||
2429 | 581 | { | ||
2430 | 582 | try | ||
2431 | 583 | { | ||
2432 | 584 | finalize(); | ||
2433 | 585 | } | ||
2434 | 586 | catch(...) | ||
2435 | 587 | { | ||
2436 | 588 | } | ||
2437 | 589 | } | ||
2438 | 590 | |||
2439 | 591 | CppSQLite3Table& CppSQLite3Table::operator=(const CppSQLite3Table& rTable) | ||
2440 | 592 | { | ||
2441 | 593 | try | ||
2442 | 594 | { | ||
2443 | 595 | finalize(); | ||
2444 | 596 | } | ||
2445 | 597 | catch(...) | ||
2446 | 598 | { | ||
2447 | 599 | } | ||
2448 | 600 | mpaszResults = rTable.mpaszResults; | ||
2449 | 601 | // Only one object can own the results | ||
2450 | 602 | const_cast<CppSQLite3Table&>(rTable).mpaszResults = 0; | ||
2451 | 603 | mnRows = rTable.mnRows; | ||
2452 | 604 | mnCols = rTable.mnCols; | ||
2453 | 605 | mnCurrentRow = rTable.mnCurrentRow; | ||
2454 | 606 | return *this; | ||
2455 | 607 | } | ||
2456 | 608 | |||
2457 | 609 | void CppSQLite3Table::finalize() | ||
2458 | 610 | { | ||
2459 | 611 | if(mpaszResults) | ||
2460 | 612 | { | ||
2461 | 613 | sqlite3_free_table(mpaszResults); | ||
2462 | 614 | mpaszResults = 0; | ||
2463 | 615 | } | ||
2464 | 616 | } | ||
2465 | 617 | |||
2466 | 618 | int CppSQLite3Table::numFields() | ||
2467 | 619 | { | ||
2468 | 620 | checkResults(); | ||
2469 | 621 | return mnCols; | ||
2470 | 622 | } | ||
2471 | 623 | |||
2472 | 624 | int CppSQLite3Table::numRows() | ||
2473 | 625 | { | ||
2474 | 626 | checkResults(); | ||
2475 | 627 | return mnRows; | ||
2476 | 628 | } | ||
2477 | 629 | |||
2478 | 630 | const char* CppSQLite3Table::fieldValue(int nField) | ||
2479 | 631 | { | ||
2480 | 632 | checkResults(); | ||
2481 | 633 | |||
2482 | 634 | if(nField < 0 || nField > mnCols - 1) | ||
2483 | 635 | { | ||
2484 | 636 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid field index requested", DONT_DELETE_MSG); | ||
2485 | 637 | } | ||
2486 | 638 | |||
2487 | 639 | int nIndex = (mnCurrentRow* mnCols) + mnCols + nField; | ||
2488 | 640 | return mpaszResults[nIndex]; | ||
2489 | 641 | } | ||
2490 | 642 | |||
2491 | 643 | const char* CppSQLite3Table::fieldValue(const char* szField) | ||
2492 | 644 | { | ||
2493 | 645 | checkResults(); | ||
2494 | 646 | |||
2495 | 647 | if(szField) | ||
2496 | 648 | { | ||
2497 | 649 | for(int nField = 0;nField < mnCols;nField++) | ||
2498 | 650 | { | ||
2499 | 651 | if(strcmp(szField, mpaszResults[nField]) == 0) | ||
2500 | 652 | { | ||
2501 | 653 | int nIndex = (mnCurrentRow* mnCols) + mnCols + nField; | ||
2502 | 654 | return mpaszResults[nIndex]; | ||
2503 | 655 | } | ||
2504 | 656 | } | ||
2505 | 657 | } | ||
2506 | 658 | |||
2507 | 659 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid field name requested", DONT_DELETE_MSG); | ||
2508 | 660 | } | ||
2509 | 661 | |||
2510 | 662 | int CppSQLite3Table::getIntField(int nField, int nNullValue/*=0*/) | ||
2511 | 663 | { | ||
2512 | 664 | if(fieldIsNull(nField)) | ||
2513 | 665 | { | ||
2514 | 666 | return nNullValue; | ||
2515 | 667 | } | ||
2516 | 668 | else | ||
2517 | 669 | { | ||
2518 | 670 | return atoi(fieldValue(nField)); | ||
2519 | 671 | } | ||
2520 | 672 | } | ||
2521 | 673 | |||
2522 | 674 | int CppSQLite3Table::getIntField(const char* szField, int nNullValue/*=0*/) | ||
2523 | 675 | { | ||
2524 | 676 | if(fieldIsNull(szField)) | ||
2525 | 677 | { | ||
2526 | 678 | return nNullValue; | ||
2527 | 679 | } | ||
2528 | 680 | else | ||
2529 | 681 | { | ||
2530 | 682 | return atoi(fieldValue(szField)); | ||
2531 | 683 | } | ||
2532 | 684 | } | ||
2533 | 685 | |||
2534 | 686 | double CppSQLite3Table::getFloatField(int nField, double fNullValue/*=0.0*/) | ||
2535 | 687 | { | ||
2536 | 688 | if(fieldIsNull(nField)) | ||
2537 | 689 | { | ||
2538 | 690 | return fNullValue; | ||
2539 | 691 | } | ||
2540 | 692 | else | ||
2541 | 693 | { | ||
2542 | 694 | return atof(fieldValue(nField)); | ||
2543 | 695 | } | ||
2544 | 696 | } | ||
2545 | 697 | |||
2546 | 698 | double CppSQLite3Table::getFloatField(const char* szField, double fNullValue/*=0.0*/) | ||
2547 | 699 | { | ||
2548 | 700 | if(fieldIsNull(szField)) | ||
2549 | 701 | { | ||
2550 | 702 | return fNullValue; | ||
2551 | 703 | } | ||
2552 | 704 | else | ||
2553 | 705 | { | ||
2554 | 706 | return atof(fieldValue(szField)); | ||
2555 | 707 | } | ||
2556 | 708 | } | ||
2557 | 709 | |||
2558 | 710 | const char* CppSQLite3Table::getStringField(int nField, const char* szNullValue/*=""*/) | ||
2559 | 711 | { | ||
2560 | 712 | if(fieldIsNull(nField)) | ||
2561 | 713 | { | ||
2562 | 714 | return szNullValue; | ||
2563 | 715 | } | ||
2564 | 716 | else | ||
2565 | 717 | { | ||
2566 | 718 | return fieldValue(nField); | ||
2567 | 719 | } | ||
2568 | 720 | } | ||
2569 | 721 | |||
2570 | 722 | const char* CppSQLite3Table::getStringField(const char* szField, const char* szNullValue/*=""*/) | ||
2571 | 723 | { | ||
2572 | 724 | if(fieldIsNull(szField)) | ||
2573 | 725 | { | ||
2574 | 726 | return szNullValue; | ||
2575 | 727 | } | ||
2576 | 728 | else | ||
2577 | 729 | { | ||
2578 | 730 | return fieldValue(szField); | ||
2579 | 731 | } | ||
2580 | 732 | } | ||
2581 | 733 | |||
2582 | 734 | bool CppSQLite3Table::fieldIsNull(int nField) | ||
2583 | 735 | { | ||
2584 | 736 | checkResults(); | ||
2585 | 737 | return (fieldValue(nField) == 0); | ||
2586 | 738 | } | ||
2587 | 739 | |||
2588 | 740 | bool CppSQLite3Table::fieldIsNull(const char* szField) | ||
2589 | 741 | { | ||
2590 | 742 | checkResults(); | ||
2591 | 743 | return (fieldValue(szField) == 0); | ||
2592 | 744 | } | ||
2593 | 745 | |||
2594 | 746 | const char* CppSQLite3Table::fieldName(int nCol) | ||
2595 | 747 | { | ||
2596 | 748 | checkResults(); | ||
2597 | 749 | |||
2598 | 750 | if(nCol < 0 || nCol > mnCols - 1) | ||
2599 | 751 | { | ||
2600 | 752 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid field index requested", DONT_DELETE_MSG); | ||
2601 | 753 | } | ||
2602 | 754 | |||
2603 | 755 | return mpaszResults[nCol]; | ||
2604 | 756 | } | ||
2605 | 757 | |||
2606 | 758 | void CppSQLite3Table::setRow(int nRow) | ||
2607 | 759 | { | ||
2608 | 760 | checkResults(); | ||
2609 | 761 | |||
2610 | 762 | if(nRow < 0 || nRow > mnRows - 1) | ||
2611 | 763 | { | ||
2612 | 764 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid row index requested", DONT_DELETE_MSG); | ||
2613 | 765 | } | ||
2614 | 766 | |||
2615 | 767 | mnCurrentRow = nRow; | ||
2616 | 768 | } | ||
2617 | 769 | |||
2618 | 770 | void CppSQLite3Table::checkResults() | ||
2619 | 771 | { | ||
2620 | 772 | if(mpaszResults == 0) | ||
2621 | 773 | { | ||
2622 | 774 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Null Results pointer", DONT_DELETE_MSG); | ||
2623 | 775 | } | ||
2624 | 776 | } | ||
2625 | 777 | |||
2626 | 778 | //////////////////////////////////////////////////////////////////////////////// | ||
2627 | 779 | |||
2628 | 780 | CppSQLite3Statement::CppSQLite3Statement() | ||
2629 | 781 | { | ||
2630 | 782 | mpDB = 0; | ||
2631 | 783 | mpVM = 0; | ||
2632 | 784 | } | ||
2633 | 785 | |||
2634 | 786 | CppSQLite3Statement::CppSQLite3Statement(const CppSQLite3Statement& rStatement) | ||
2635 | 787 | { | ||
2636 | 788 | mpDB = rStatement.mpDB; | ||
2637 | 789 | mpVM = rStatement.mpVM; | ||
2638 | 790 | // Only one object can own VM | ||
2639 | 791 | const_cast<CppSQLite3Statement&>(rStatement).mpVM = 0; | ||
2640 | 792 | } | ||
2641 | 793 | |||
2642 | 794 | CppSQLite3Statement::CppSQLite3Statement(sqlite3* pDB, sqlite3_stmt* pVM) | ||
2643 | 795 | { | ||
2644 | 796 | mpDB = pDB; | ||
2645 | 797 | mpVM = pVM; | ||
2646 | 798 | } | ||
2647 | 799 | |||
2648 | 800 | CppSQLite3Statement::~CppSQLite3Statement() | ||
2649 | 801 | { | ||
2650 | 802 | try | ||
2651 | 803 | { | ||
2652 | 804 | finalize(); | ||
2653 | 805 | } | ||
2654 | 806 | catch(...) | ||
2655 | 807 | { | ||
2656 | 808 | } | ||
2657 | 809 | } | ||
2658 | 810 | |||
2659 | 811 | CppSQLite3Statement& CppSQLite3Statement::operator=(const CppSQLite3Statement& rStatement) | ||
2660 | 812 | { | ||
2661 | 813 | mpDB = rStatement.mpDB; | ||
2662 | 814 | mpVM = rStatement.mpVM; | ||
2663 | 815 | // Only one object can own VM | ||
2664 | 816 | const_cast<CppSQLite3Statement&>(rStatement).mpVM = 0; | ||
2665 | 817 | return *this; | ||
2666 | 818 | } | ||
2667 | 819 | |||
2668 | 820 | int CppSQLite3Statement::execDML() | ||
2669 | 821 | { | ||
2670 | 822 | checkDB(); | ||
2671 | 823 | checkVM(); | ||
2672 | 824 | |||
2673 | 825 | const char* szError = 0; | ||
2674 | 826 | |||
2675 | 827 | int nRet = sqlite3_step(mpVM); | ||
2676 | 828 | |||
2677 | 829 | if(nRet == SQLITE_DONE) | ||
2678 | 830 | { | ||
2679 | 831 | int nRowsChanged = sqlite3_changes(mpDB); | ||
2680 | 832 | |||
2681 | 833 | nRet = sqlite3_reset(mpVM); | ||
2682 | 834 | |||
2683 | 835 | if(nRet != SQLITE_OK) | ||
2684 | 836 | { | ||
2685 | 837 | szError = sqlite3_errmsg(mpDB); | ||
2686 | 838 | throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG); | ||
2687 | 839 | } | ||
2688 | 840 | |||
2689 | 841 | return nRowsChanged; | ||
2690 | 842 | } | ||
2691 | 843 | else | ||
2692 | 844 | { | ||
2693 | 845 | nRet = sqlite3_reset(mpVM); | ||
2694 | 846 | szError = sqlite3_errmsg(mpDB); | ||
2695 | 847 | throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG); | ||
2696 | 848 | } | ||
2697 | 849 | } | ||
2698 | 850 | |||
2699 | 851 | CppSQLite3Query CppSQLite3Statement::execQuery() | ||
2700 | 852 | { | ||
2701 | 853 | checkDB(); | ||
2702 | 854 | checkVM(); | ||
2703 | 855 | |||
2704 | 856 | int nRet = sqlite3_step(mpVM); | ||
2705 | 857 | |||
2706 | 858 | if(nRet == SQLITE_DONE) | ||
2707 | 859 | { | ||
2708 | 860 | // no rows | ||
2709 | 861 | return CppSQLite3Query(mpDB, mpVM, true/*eof*/, false); | ||
2710 | 862 | } | ||
2711 | 863 | else if(nRet == SQLITE_ROW) | ||
2712 | 864 | { | ||
2713 | 865 | // at least 1 row | ||
2714 | 866 | return CppSQLite3Query(mpDB, mpVM, false/*eof*/, false); | ||
2715 | 867 | } | ||
2716 | 868 | else | ||
2717 | 869 | { | ||
2718 | 870 | nRet = sqlite3_reset(mpVM); | ||
2719 | 871 | const char* szError = sqlite3_errmsg(mpDB); | ||
2720 | 872 | throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG); | ||
2721 | 873 | } | ||
2722 | 874 | } | ||
2723 | 875 | |||
2724 | 876 | void CppSQLite3Statement::bind(int nParam, const char* szValue) | ||
2725 | 877 | { | ||
2726 | 878 | checkVM(); | ||
2727 | 879 | int nRes = sqlite3_bind_text(mpVM, nParam, szValue, -1, SQLITE_TRANSIENT); | ||
2728 | 880 | |||
2729 | 881 | if(nRes != SQLITE_OK) | ||
2730 | 882 | { | ||
2731 | 883 | throw CppSQLite3Exception(nRes, "Error binding string param", DONT_DELETE_MSG); | ||
2732 | 884 | } | ||
2733 | 885 | } | ||
2734 | 886 | |||
2735 | 887 | void CppSQLite3Statement::bind(int nParam, const int nValue) | ||
2736 | 888 | { | ||
2737 | 889 | checkVM(); | ||
2738 | 890 | int nRes = sqlite3_bind_int(mpVM, nParam, nValue); | ||
2739 | 891 | |||
2740 | 892 | if(nRes != SQLITE_OK) | ||
2741 | 893 | { | ||
2742 | 894 | throw CppSQLite3Exception(nRes, "Error binding int param", DONT_DELETE_MSG); | ||
2743 | 895 | } | ||
2744 | 896 | } | ||
2745 | 897 | |||
2746 | 898 | void CppSQLite3Statement::bind(int nParam, const double dValue) | ||
2747 | 899 | { | ||
2748 | 900 | checkVM(); | ||
2749 | 901 | int nRes = sqlite3_bind_double(mpVM, nParam, dValue); | ||
2750 | 902 | |||
2751 | 903 | if(nRes != SQLITE_OK) | ||
2752 | 904 | { | ||
2753 | 905 | throw CppSQLite3Exception(nRes, "Error binding double param", DONT_DELETE_MSG); | ||
2754 | 906 | } | ||
2755 | 907 | } | ||
2756 | 908 | |||
2757 | 909 | void CppSQLite3Statement::bind(int nParam, const unsigned char* blobValue, int nLen) | ||
2758 | 910 | { | ||
2759 | 911 | checkVM(); | ||
2760 | 912 | int nRes = sqlite3_bind_blob(mpVM, nParam, (const void*)blobValue, nLen, SQLITE_TRANSIENT); | ||
2761 | 913 | |||
2762 | 914 | if(nRes != SQLITE_OK) | ||
2763 | 915 | { | ||
2764 | 916 | throw CppSQLite3Exception(nRes, "Error binding blob param", DONT_DELETE_MSG); | ||
2765 | 917 | } | ||
2766 | 918 | } | ||
2767 | 919 | |||
2768 | 920 | void CppSQLite3Statement::bindNull(int nParam) | ||
2769 | 921 | { | ||
2770 | 922 | checkVM(); | ||
2771 | 923 | int nRes = sqlite3_bind_null(mpVM, nParam); | ||
2772 | 924 | |||
2773 | 925 | if(nRes != SQLITE_OK) | ||
2774 | 926 | { | ||
2775 | 927 | throw CppSQLite3Exception(nRes, "Error binding NULL param", DONT_DELETE_MSG); | ||
2776 | 928 | } | ||
2777 | 929 | } | ||
2778 | 930 | |||
2779 | 931 | void CppSQLite3Statement::reset() | ||
2780 | 932 | { | ||
2781 | 933 | if(mpVM) | ||
2782 | 934 | { | ||
2783 | 935 | int nRet = sqlite3_reset(mpVM); | ||
2784 | 936 | |||
2785 | 937 | if(nRet != SQLITE_OK) | ||
2786 | 938 | { | ||
2787 | 939 | const char* szError = sqlite3_errmsg(mpDB); | ||
2788 | 940 | throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG); | ||
2789 | 941 | } | ||
2790 | 942 | } | ||
2791 | 943 | } | ||
2792 | 944 | |||
2793 | 945 | void CppSQLite3Statement::finalize() | ||
2794 | 946 | { | ||
2795 | 947 | if(mpVM) | ||
2796 | 948 | { | ||
2797 | 949 | int nRet = sqlite3_finalize(mpVM); | ||
2798 | 950 | mpVM = 0; | ||
2799 | 951 | |||
2800 | 952 | if(nRet != SQLITE_OK) | ||
2801 | 953 | { | ||
2802 | 954 | const char* szError = sqlite3_errmsg(mpDB); | ||
2803 | 955 | throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG); | ||
2804 | 956 | } | ||
2805 | 957 | } | ||
2806 | 958 | } | ||
2807 | 959 | |||
2808 | 960 | void CppSQLite3Statement::checkDB() | ||
2809 | 961 | { | ||
2810 | 962 | if(mpDB == 0) | ||
2811 | 963 | { | ||
2812 | 964 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Database not open", DONT_DELETE_MSG); | ||
2813 | 965 | } | ||
2814 | 966 | } | ||
2815 | 967 | |||
2816 | 968 | void CppSQLite3Statement::checkVM() | ||
2817 | 969 | { | ||
2818 | 970 | if(mpVM == 0) | ||
2819 | 971 | { | ||
2820 | 972 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Null Virtual Machine pointer", DONT_DELETE_MSG); | ||
2821 | 973 | } | ||
2822 | 974 | } | ||
2823 | 975 | |||
2824 | 976 | //////////////////////////////////////////////////////////////////////////////// | ||
2825 | 977 | |||
2826 | 978 | CppSQLite3DB::CppSQLite3DB() | ||
2827 | 979 | { | ||
2828 | 980 | mpDB = 0; | ||
2829 | 981 | mnBusyTimeoutMs = 60000; // 60 seconds | ||
2830 | 982 | } | ||
2831 | 983 | |||
2832 | 984 | CppSQLite3DB::CppSQLite3DB(const CppSQLite3DB& db) | ||
2833 | 985 | { | ||
2834 | 986 | mpDB = db.mpDB; | ||
2835 | 987 | mnBusyTimeoutMs = 60000; // 60 seconds | ||
2836 | 988 | } | ||
2837 | 989 | |||
2838 | 990 | CppSQLite3DB::~CppSQLite3DB() | ||
2839 | 991 | { | ||
2840 | 992 | Close(); | ||
2841 | 993 | } | ||
2842 | 994 | |||
2843 | 995 | CppSQLite3DB& CppSQLite3DB::operator=(const CppSQLite3DB& db) | ||
2844 | 996 | { | ||
2845 | 997 | mpDB = db.mpDB; | ||
2846 | 998 | mnBusyTimeoutMs = 60000; // 60 seconds | ||
2847 | 999 | return *this; | ||
2848 | 1000 | } | ||
2849 | 1001 | |||
2850 | 1002 | void CppSQLite3DB::Open(const char* szFile) | ||
2851 | 1003 | { | ||
2852 | 1004 | int nRet = sqlite3_open(szFile, &mpDB); | ||
2853 | 1005 | |||
2854 | 1006 | if(nRet != SQLITE_OK) | ||
2855 | 1007 | { | ||
2856 | 1008 | const char* szError = sqlite3_errmsg(mpDB); | ||
2857 | 1009 | throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG); | ||
2858 | 1010 | } | ||
2859 | 1011 | |||
2860 | 1012 | setBusyTimeout(mnBusyTimeoutMs); | ||
2861 | 1013 | } | ||
2862 | 1014 | |||
2863 | 1015 | void CppSQLite3DB::Close() | ||
2864 | 1016 | { | ||
2865 | 1017 | if(mpDB) | ||
2866 | 1018 | { | ||
2867 | 1019 | sqlite3_close(mpDB); | ||
2868 | 1020 | mpDB = 0; | ||
2869 | 1021 | } | ||
2870 | 1022 | } | ||
2871 | 1023 | |||
2872 | 1024 | CppSQLite3Statement CppSQLite3DB::compileStatement(const char* szSQL) | ||
2873 | 1025 | { | ||
2874 | 1026 | checkDB(); | ||
2875 | 1027 | |||
2876 | 1028 | sqlite3_stmt* pVM = compile(szSQL); | ||
2877 | 1029 | return CppSQLite3Statement(mpDB, pVM); | ||
2878 | 1030 | } | ||
2879 | 1031 | |||
2880 | 1032 | bool CppSQLite3DB::tableExists(const char* szTable) | ||
2881 | 1033 | { | ||
2882 | 1034 | char szSQL[128]; | ||
2883 | 1035 | sprintf(szSQL, "select count(*) from sqlite_master where type='table' and name='%s'", szTable); | ||
2884 | 1036 | int nRet = execScalar(szSQL); | ||
2885 | 1037 | return (nRet > 0); | ||
2886 | 1038 | } | ||
2887 | 1039 | |||
2888 | 1040 | int CppSQLite3DB::execDML(const char* szSQL) | ||
2889 | 1041 | { | ||
2890 | 1042 | checkDB(); | ||
2891 | 1043 | |||
2892 | 1044 | char* szError = 0; | ||
2893 | 1045 | |||
2894 | 1046 | int nRet = sqlite3_exec(mpDB, szSQL, 0, 0, &szError); | ||
2895 | 1047 | |||
2896 | 1048 | if(nRet == SQLITE_OK) | ||
2897 | 1049 | { | ||
2898 | 1050 | return sqlite3_changes(mpDB); | ||
2899 | 1051 | } | ||
2900 | 1052 | else | ||
2901 | 1053 | { | ||
2902 | 1054 | throw CppSQLite3Exception(nRet, szError); | ||
2903 | 1055 | } | ||
2904 | 1056 | } | ||
2905 | 1057 | |||
2906 | 1058 | CppSQLite3Query CppSQLite3DB::execQuery(const char* szSQL) | ||
2907 | 1059 | { | ||
2908 | 1060 | checkDB(); | ||
2909 | 1061 | |||
2910 | 1062 | sqlite3_stmt* pVM = compile(szSQL); | ||
2911 | 1063 | |||
2912 | 1064 | int nRet = sqlite3_step(pVM); | ||
2913 | 1065 | |||
2914 | 1066 | if(nRet == SQLITE_DONE) | ||
2915 | 1067 | { | ||
2916 | 1068 | // no rows | ||
2917 | 1069 | return CppSQLite3Query(mpDB, pVM, true/*eof*/); | ||
2918 | 1070 | } | ||
2919 | 1071 | else if(nRet == SQLITE_ROW) | ||
2920 | 1072 | { | ||
2921 | 1073 | // at least 1 row | ||
2922 | 1074 | return CppSQLite3Query(mpDB, pVM, false/*eof*/); | ||
2923 | 1075 | } | ||
2924 | 1076 | else | ||
2925 | 1077 | { | ||
2926 | 1078 | nRet = sqlite3_finalize(pVM); | ||
2927 | 1079 | const char* szError = sqlite3_errmsg(mpDB); | ||
2928 | 1080 | throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG); | ||
2929 | 1081 | } | ||
2930 | 1082 | } | ||
2931 | 1083 | |||
2932 | 1084 | int CppSQLite3DB::execScalar(const char* szSQL) | ||
2933 | 1085 | { | ||
2934 | 1086 | CppSQLite3Query q = execQuery(szSQL); | ||
2935 | 1087 | |||
2936 | 1088 | if(q.eof() || q.numFields() < 1) | ||
2937 | 1089 | { | ||
2938 | 1090 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid scalar query", DONT_DELETE_MSG); | ||
2939 | 1091 | } | ||
2940 | 1092 | |||
2941 | 1093 | return atoi(q.fieldValue(0)); | ||
2942 | 1094 | } | ||
2943 | 1095 | |||
2944 | 1096 | CppSQLite3Table CppSQLite3DB::getTable(const char* szSQL) | ||
2945 | 1097 | { | ||
2946 | 1098 | checkDB(); | ||
2947 | 1099 | |||
2948 | 1100 | char* szError = 0; | ||
2949 | 1101 | char** paszResults = 0; | ||
2950 | 1102 | int nRet; | ||
2951 | 1103 | int nRows(0); | ||
2952 | 1104 | int nCols(0); | ||
2953 | 1105 | |||
2954 | 1106 | nRet = sqlite3_get_table(mpDB, szSQL, &paszResults, &nRows, &nCols, &szError); | ||
2955 | 1107 | |||
2956 | 1108 | if(nRet == SQLITE_OK) | ||
2957 | 1109 | { | ||
2958 | 1110 | return CppSQLite3Table(paszResults, nRows, nCols); | ||
2959 | 1111 | } | ||
2960 | 1112 | else | ||
2961 | 1113 | { | ||
2962 | 1114 | throw CppSQLite3Exception(nRet, szError); | ||
2963 | 1115 | } | ||
2964 | 1116 | } | ||
2965 | 1117 | |||
2966 | 1118 | sqlite_int64 CppSQLite3DB::lastRowId() | ||
2967 | 1119 | { | ||
2968 | 1120 | return sqlite3_last_insert_rowid(mpDB); | ||
2969 | 1121 | } | ||
2970 | 1122 | |||
2971 | 1123 | void CppSQLite3DB::setBusyTimeout(int nMillisecs) | ||
2972 | 1124 | { | ||
2973 | 1125 | mnBusyTimeoutMs = nMillisecs; | ||
2974 | 1126 | sqlite3_busy_timeout(mpDB, mnBusyTimeoutMs); | ||
2975 | 1127 | } | ||
2976 | 1128 | |||
2977 | 1129 | void CppSQLite3DB::checkDB() | ||
2978 | 1130 | { | ||
2979 | 1131 | if(!mpDB) | ||
2980 | 1132 | { | ||
2981 | 1133 | throw CppSQLite3Exception(CPPSQLITE_ERROR, "Database not open", DONT_DELETE_MSG); | ||
2982 | 1134 | } | ||
2983 | 1135 | } | ||
2984 | 1136 | |||
2985 | 1137 | sqlite3_stmt* CppSQLite3DB::compile(const char* szSQL) | ||
2986 | 1138 | { | ||
2987 | 1139 | checkDB(); | ||
2988 | 1140 | |||
2989 | 1141 | char* szError = 0; | ||
2990 | 1142 | const char* szTail = 0; | ||
2991 | 1143 | sqlite3_stmt* pVM; | ||
2992 | 1144 | |||
2993 | 1145 | int nRet = sqlite3_prepare(mpDB, szSQL, -1, &pVM, &szTail); | ||
2994 | 1146 | |||
2995 | 1147 | if(nRet != SQLITE_OK) | ||
2996 | 1148 | { | ||
2997 | 1149 | throw CppSQLite3Exception(nRet, szError); | ||
2998 | 1150 | } | ||
2999 | 1151 | |||
3000 | 1152 | return pVM; | ||
3001 | 1153 | } | ||
3002 | 1154 | |||
3003 | 1155 | //////////////////////////////////////////////////////////////////////////////// | ||
3004 | 1156 | // SQLite encode.c reproduced here, containing implementation notes and source | ||
3005 | 1157 | // for sqlite3_encode_binary() and sqlite3_decode_binary() | ||
3006 | 1158 | //////////////////////////////////////////////////////////////////////////////// | ||
3007 | 1159 | |||
3008 | 1160 | /* | ||
3009 | 1161 | ** 2002 April 25 | ||
3010 | 1162 | ** | ||
3011 | 1163 | ** The author disclaims copyright to this source code. In place of | ||
3012 | 1164 | ** a legal notice, here is a blessing: | ||
3013 | 1165 | ** | ||
3014 | 1166 | ** May you do good and not evil. | ||
3015 | 1167 | ** May you find forgiveness for yourself and forgive others. | ||
3016 | 1168 | ** May you share freely, never taking more than you give. | ||
3017 | 1169 | ** | ||
3018 | 1170 | ************************************************************************* | ||
3019 | 1171 | ** This file contains helper routines used to translate binary data into | ||
3020 | 1172 | ** a null-terminated string (suitable for use in SQLite) and back again. | ||
3021 | 1173 | ** These are convenience routines for use by people who want to store binary | ||
3022 | 1174 | ** data in an SQLite database. The code in this file is not used by any other | ||
3023 | 1175 | ** part of the SQLite library. | ||
3024 | 1176 | ** | ||
3025 | 1177 | ** $Id: encode.c,v 1.10 2004/01/14 21:59:23 drh Exp $ | ||
3026 | 1178 | */ | ||
3027 | 1179 | |||
3028 | 1180 | /* | ||
3029 | 1181 | ** How This Encoder Works | ||
3030 | 1182 | ** | ||
3031 | 1183 | ** The output is allowed to contain any character except 0x27 (') and | ||
3032 | 1184 | ** 0x00. This is accomplished by using an escape character to encode | ||
3033 | 1185 | ** 0x27 and 0x00 as a two-byte sequence. The escape character is always | ||
3034 | 1186 | ** 0x01. An 0x00 is encoded as the two byte sequence 0x01 0x01. The | ||
3035 | 1187 | ** 0x27 character is encoded as the two byte sequence 0x01 0x03. Finally, | ||
3036 | 1188 | ** the escape character itself is encoded as the two-character sequence | ||
3037 | 1189 | ** 0x01 0x02. | ||
3038 | 1190 | ** | ||
3039 | 1191 | ** To summarize, the encoder works by using an escape sequences as follows: | ||
3040 | 1192 | ** | ||
3041 | 1193 | ** 0x00 -> 0x01 0x01 | ||
3042 | 1194 | ** 0x01 -> 0x01 0x02 | ||
3043 | 1195 | ** 0x27 -> 0x01 0x03 | ||
3044 | 1196 | ** | ||
3045 | 1197 | ** If that were all the encoder did, it would work, but in certain cases | ||
3046 | 1198 | ** it could double the size of the encoded string. For example, to | ||
3047 | 1199 | ** encode a string of 100 0x27 characters would require 100 instances of | ||
3048 | 1200 | ** the 0x01 0x03 escape sequence resulting in a 200-character output. | ||
3049 | 1201 | ** We would prefer to keep the size of the encoded string smaller than | ||
3050 | 1202 | ** this. | ||
3051 | 1203 | ** | ||
3052 | 1204 | ** To minimize the encoding size, we first add a fixed offset value to each | ||
3053 | 1205 | ** byte in the sequence. The addition is modulo 256. (That is to say, if | ||
3054 | 1206 | ** the sum of the original character value and the offset exceeds 256, then | ||
3055 | 1207 | ** the higher order bits are truncated.) The offset is chosen to minimize | ||
3056 | 1208 | ** the number of characters in the string that need to be escaped. For | ||
3057 | 1209 | ** example, in the case above where the string was composed of 100 0x27 | ||
3058 | 1210 | ** characters, the offset might be 0x01. Each of the 0x27 characters would | ||
3059 | 1211 | ** then be converted into an 0x28 character which would not need to be | ||
3060 | 1212 | ** escaped at all and so the 100 character input string would be converted | ||
3061 | 1213 | ** into just 100 characters of output. Actually 101 characters of output - | ||
3062 | 1214 | ** we have to record the offset used as the first byte in the sequence so | ||
3063 | 1215 | ** that the string can be decoded. Since the offset value is stored as | ||
3064 | 1216 | ** part of the output string and the output string is not allowed to contain | ||
3065 | 1217 | ** characters 0x00 or 0x27, the offset cannot be 0x00 or 0x27. | ||
3066 | 1218 | ** | ||
3067 | 1219 | ** Here, then, are the encoding steps: | ||
3068 | 1220 | ** | ||
3069 | 1221 | ** (1) Choose an offset value and make it the first character of | ||
3070 | 1222 | ** output. | ||
3071 | 1223 | ** | ||
3072 | 1224 | ** (2) Copy each input character into the output buffer, one by | ||
3073 | 1225 | ** one, adding the offset value as you copy. | ||
3074 | 1226 | ** | ||
3075 | 1227 | ** (3) If the value of an input character plus offset is 0x00, replace | ||
3076 | 1228 | ** that one character by the two-character sequence 0x01 0x01. | ||
3077 | 1229 | ** If the sum is 0x01, replace it with 0x01 0x02. If the sum | ||
3078 | 1230 | ** is 0x27, replace it with 0x01 0x03. | ||
3079 | 1231 | ** | ||
3080 | 1232 | ** (4) Put a 0x00 terminator at the end of the output. | ||
3081 | 1233 | ** | ||
3082 | 1234 | ** Decoding is obvious: | ||
3083 | 1235 | ** | ||
3084 | 1236 | ** (5) Copy encoded characters except the first into the decode | ||
3085 | 1237 | ** buffer. Set the first encoded character aside for use as | ||
3086 | 1238 | ** the offset in step 7 below. | ||
3087 | 1239 | ** | ||
3088 | 1240 | ** (6) Convert each 0x01 0x01 sequence into a single character 0x00. | ||
3089 | 1241 | ** Convert 0x01 0x02 into 0x01. Convert 0x01 0x03 into 0x27. | ||
3090 | 1242 | ** | ||
3091 | 1243 | ** (7) Subtract the offset value that was the first character of | ||
3092 | 1244 | ** the encoded buffer from all characters in the output buffer. | ||
3093 | 1245 | ** | ||
3094 | 1246 | ** The only tricky part is step (1) - how to compute an offset value to | ||
3095 | 1247 | ** minimize the size of the output buffer. This is accomplished by testing | ||
3096 | 1248 | ** all offset values and picking the one that results in the fewest number | ||
3097 | 1249 | ** of escapes. To do that, we first scan the entire input and count the | ||
3098 | 1250 | ** number of occurances of each character value in the input. Suppose | ||
3099 | 1251 | ** the number of 0x00 characters is N(0), the number of occurances of 0x01 | ||
3100 | 1252 | ** is N(1), and so forth up to the number of occurances of 0xff is N(255). | ||
3101 | 1253 | ** An offset of 0 is not allowed so we don't have to test it. The number | ||
3102 | 1254 | ** of escapes required for an offset of 1 is N(1)+N(2)+N(40). The number | ||
3103 | 1255 | ** of escapes required for an offset of 2 is N(2)+N(3)+N(41). And so forth. | ||
3104 | 1256 | ** In this way we find the offset that gives the minimum number of escapes, | ||
3105 | 1257 | ** and thus minimizes the length of the output string. | ||
3106 | 1258 | */ | ||
3107 | 1259 | |||
3108 | 1260 | /* | ||
3109 | 1261 | ** Encode a binary buffer "in" of size n bytes so that it contains | ||
3110 | 1262 | ** no instances of characters '\'' or '\000'. The output is | ||
3111 | 1263 | ** null-terminated and can be used as a string value in an INSERT | ||
3112 | 1264 | ** or UPDATE statement. Use sqlite3_decode_binary() to convert the | ||
3113 | 1265 | ** string back into its original binary. | ||
3114 | 1266 | ** | ||
3115 | 1267 | ** The result is written into a preallocated output buffer "out". | ||
3116 | 1268 | ** "out" must be able to hold at least 2 +(257*n)/254 bytes. | ||
3117 | 1269 | ** In other words, the output will be expanded by as much as 3 | ||
3118 | 1270 | ** bytes for every 254 bytes of input plus 2 bytes of fixed overhead. | ||
3119 | 1271 | ** (This is approximately 2 + 1.0118*n or about a 1.2% size increase.) | ||
3120 | 1272 | ** | ||
3121 | 1273 | ** The return value is the number of characters in the encoded | ||
3122 | 1274 | ** string, excluding the "\000" terminator. | ||
3123 | 1275 | */ | ||
3124 | 1276 | int sqlite3_encode_binary(const unsigned char* in, int n, unsigned char* out) | ||
3125 | 1277 | { | ||
3126 | 1278 | int i = 0; | ||
3127 | 1279 | int j = 0; | ||
3128 | 1280 | int e = 0; | ||
3129 | 1281 | int m = 0; | ||
3130 | 1282 | |||
3131 | 1283 | int cnt[256]; | ||
3132 | 1284 | |||
3133 | 1285 | if(n <= 0) | ||
3134 | 1286 | { | ||
3135 | 1287 | out[0] = 'x'; | ||
3136 | 1288 | out[1] = 0; | ||
3137 | 1289 | return 1; | ||
3138 | 1290 | } | ||
3139 | 1291 | memset(cnt, 0, sizeof(cnt)); | ||
3140 | 1292 | for(i = n - 1;i >= 0;i--) | ||
3141 | 1293 | { | ||
3142 | 1294 | cnt[in[i]]++; | ||
3143 | 1295 | } | ||
3144 | 1296 | m = n; | ||
3145 | 1297 | for(i = 1;i < 256;i++) | ||
3146 | 1298 | { | ||
3147 | 1299 | int sum; | ||
3148 | 1300 | if(i == '\'') | ||
3149 | 1301 | continue; | ||
3150 | 1302 | sum = cnt[i] + cnt[(i + 1) & 0xff] + cnt[(i + '\'') & 0xff]; | ||
3151 | 1303 | if(sum < m) | ||
3152 | 1304 | { | ||
3153 | 1305 | m = sum; | ||
3154 | 1306 | e = i; | ||
3155 | 1307 | if(m == 0) | ||
3156 | 1308 | break; | ||
3157 | 1309 | } | ||
3158 | 1310 | } | ||
3159 | 1311 | out[0] = e; | ||
3160 | 1312 | j = 1; | ||
3161 | 1313 | for(i = 0;i < n;i++) | ||
3162 | 1314 | { | ||
3163 | 1315 | int c = (in[i] - e) & 0xff; | ||
3164 | 1316 | if(c == 0) | ||
3165 | 1317 | { | ||
3166 | 1318 | out[j++] = 1; | ||
3167 | 1319 | out[j++] = 1; | ||
3168 | 1320 | } | ||
3169 | 1321 | else if(c == 1) | ||
3170 | 1322 | { | ||
3171 | 1323 | out[j++] = 1; | ||
3172 | 1324 | out[j++] = 2; | ||
3173 | 1325 | } | ||
3174 | 1326 | else if(c == '\'') | ||
3175 | 1327 | { | ||
3176 | 1328 | out[j++] = 1; | ||
3177 | 1329 | out[j++] = 3; | ||
3178 | 1330 | } | ||
3179 | 1331 | else | ||
3180 | 1332 | { | ||
3181 | 1333 | out[j++] = c; | ||
3182 | 1334 | } | ||
3183 | 1335 | } | ||
3184 | 1336 | out[j] = 0; | ||
3185 | 1337 | return j; | ||
3186 | 1338 | } | ||
3187 | 1339 | |||
3188 | 1340 | /* | ||
3189 | 1341 | ** Decode the string "in" into binary data and write it into "out". | ||
3190 | 1342 | ** This routine reverses the encoding created by sqlite3_encode_binary(). | ||
3191 | 1343 | ** The output will always be a few bytes less than the input. The number | ||
3192 | 1344 | ** of bytes of output is returned. If the input is not a well-formed | ||
3193 | 1345 | ** encoding, -1 is returned. | ||
3194 | 1346 | ** | ||
3195 | 1347 | ** The "in" and "out" parameters may point to the same buffer in order | ||
3196 | 1348 | ** to decode a string in place. | ||
3197 | 1349 | */ | ||
3198 | 1350 | int sqlite3_decode_binary(const unsigned char* in, unsigned char* out) | ||
3199 | 1351 | { | ||
3200 | 1352 | int i = 0; | ||
3201 | 1353 | int c = 0; | ||
3202 | 1354 | int e = 0; | ||
3203 | 1355 | |||
3204 | 1356 | e = *(in++); | ||
3205 | 1357 | i = 0; | ||
3206 | 1358 | while((c = *(in++)) != 0) | ||
3207 | 1359 | { | ||
3208 | 1360 | if(c == 1) | ||
3209 | 1361 | { | ||
3210 | 1362 | c = *(in++); | ||
3211 | 1363 | if(c == 1) | ||
3212 | 1364 | { | ||
3213 | 1365 | c = 0; | ||
3214 | 1366 | } | ||
3215 | 1367 | else if(c == 2) | ||
3216 | 1368 | { | ||
3217 | 1369 | c = 1; | ||
3218 | 1370 | } | ||
3219 | 1371 | else if(c == 3) | ||
3220 | 1372 | { | ||
3221 | 1373 | c = '\''; | ||
3222 | 1374 | } | ||
3223 | 1375 | else | ||
3224 | 1376 | { | ||
3225 | 1377 | return -1; | ||
3226 | 1378 | } | ||
3227 | 1379 | } | ||
3228 | 1380 | out[i++] = (c + e) & 0xff; | ||
3229 | 1381 | } | ||
3230 | 1382 | return i; | ||
3231 | 1383 | } | ||
3232 | 0 | 1384 | ||
3233 | === added file 'common/CppSQLite3.h' | |||
3234 | --- common/CppSQLite3.h 1970-01-01 00:00:00 +0000 | |||
3235 | +++ common/CppSQLite3.h 2011-11-23 01:01:25 +0000 | |||
3236 | @@ -0,0 +1,270 @@ | |||
3237 | 1 | #pragma once | ||
3238 | 2 | |||
3239 | 3 | #include "sqlite3.h" | ||
3240 | 4 | #include <cstdio> | ||
3241 | 5 | #include <cstring> | ||
3242 | 6 | |||
3243 | 7 | #define CPPSQLITE_ERROR 1000 | ||
3244 | 8 | |||
3245 | 9 | class CppSQLite3Exception | ||
3246 | 10 | { | ||
3247 | 11 | public: | ||
3248 | 12 | |||
3249 | 13 | CppSQLite3Exception(const int nErrCode, | ||
3250 | 14 | char* szErrMess, | ||
3251 | 15 | bool bDeleteMsg=true); | ||
3252 | 16 | |||
3253 | 17 | CppSQLite3Exception(const CppSQLite3Exception& e); | ||
3254 | 18 | |||
3255 | 19 | virtual ~CppSQLite3Exception(); | ||
3256 | 20 | |||
3257 | 21 | const int errorCode() { return mnErrCode; } | ||
3258 | 22 | |||
3259 | 23 | const char* errorMessage() { return mpszErrMess; } | ||
3260 | 24 | |||
3261 | 25 | static const char* errorCodeAsString(int nErrCode); | ||
3262 | 26 | |||
3263 | 27 | private: | ||
3264 | 28 | |||
3265 | 29 | int mnErrCode; | ||
3266 | 30 | char* mpszErrMess; | ||
3267 | 31 | }; | ||
3268 | 32 | |||
3269 | 33 | class CppSQLite3Buffer | ||
3270 | 34 | { | ||
3271 | 35 | public: | ||
3272 | 36 | |||
3273 | 37 | CppSQLite3Buffer(); | ||
3274 | 38 | |||
3275 | 39 | ~CppSQLite3Buffer(); | ||
3276 | 40 | |||
3277 | 41 | const char* format(const char* szFormat, ...); | ||
3278 | 42 | |||
3279 | 43 | operator const char*() { return mpBuf; } | ||
3280 | 44 | |||
3281 | 45 | void clear(); | ||
3282 | 46 | |||
3283 | 47 | private: | ||
3284 | 48 | |||
3285 | 49 | char* mpBuf; | ||
3286 | 50 | }; | ||
3287 | 51 | |||
3288 | 52 | class CppSQLite3Binary | ||
3289 | 53 | { | ||
3290 | 54 | public: | ||
3291 | 55 | |||
3292 | 56 | CppSQLite3Binary(); | ||
3293 | 57 | |||
3294 | 58 | ~CppSQLite3Binary(); | ||
3295 | 59 | |||
3296 | 60 | void setBinary(const unsigned char* pBuf, int nLen); | ||
3297 | 61 | void setEncoded(const unsigned char* pBuf); | ||
3298 | 62 | |||
3299 | 63 | const unsigned char* getEncoded(); | ||
3300 | 64 | const unsigned char* getBinary(); | ||
3301 | 65 | |||
3302 | 66 | int getBinaryLength(); | ||
3303 | 67 | |||
3304 | 68 | unsigned char* allocBuffer(int nLen); | ||
3305 | 69 | |||
3306 | 70 | void clear(); | ||
3307 | 71 | |||
3308 | 72 | private: | ||
3309 | 73 | |||
3310 | 74 | unsigned char* mpBuf; | ||
3311 | 75 | int mnBinaryLen; | ||
3312 | 76 | int mnBufferLen; | ||
3313 | 77 | int mnEncodedLen; | ||
3314 | 78 | bool mbEncoded; | ||
3315 | 79 | }; | ||
3316 | 80 | |||
3317 | 81 | class CppSQLite3Query | ||
3318 | 82 | { | ||
3319 | 83 | public: | ||
3320 | 84 | |||
3321 | 85 | CppSQLite3Query(); | ||
3322 | 86 | |||
3323 | 87 | CppSQLite3Query(const CppSQLite3Query& rQuery); | ||
3324 | 88 | |||
3325 | 89 | CppSQLite3Query(sqlite3* pDB, | ||
3326 | 90 | sqlite3_stmt* pVM, | ||
3327 | 91 | bool bEof, | ||
3328 | 92 | bool bOwnVM=true); | ||
3329 | 93 | |||
3330 | 94 | CppSQLite3Query& operator=(const CppSQLite3Query& rQuery); | ||
3331 | 95 | |||
3332 | 96 | virtual ~CppSQLite3Query(); | ||
3333 | 97 | |||
3334 | 98 | int numFields(); | ||
3335 | 99 | |||
3336 | 100 | int fieldIndex(const char* szField); | ||
3337 | 101 | const char* fieldName(int nCol); | ||
3338 | 102 | |||
3339 | 103 | const char* fieldDeclType(int nCol); | ||
3340 | 104 | int fieldDataType(int nCol); | ||
3341 | 105 | |||
3342 | 106 | const char* fieldValue(int nField); | ||
3343 | 107 | const char* fieldValue(const char* szField); | ||
3344 | 108 | |||
3345 | 109 | int getIntField(int nField, int nNullValue=0); | ||
3346 | 110 | int getIntField(const char* szField, int nNullValue=0); | ||
3347 | 111 | |||
3348 | 112 | double getFloatField(int nField, double fNullValue=0.0); | ||
3349 | 113 | double getFloatField(const char* szField, double fNullValue=0.0); | ||
3350 | 114 | |||
3351 | 115 | const char* getStringField(int nField, const char* szNullValue=""); | ||
3352 | 116 | const char* getStringField(const char* szField, const char* szNullValue=""); | ||
3353 | 117 | |||
3354 | 118 | const unsigned char* getBlobField(int nField, int& nLen); | ||
3355 | 119 | const unsigned char* getBlobField(const char* szField, int& nLen); | ||
3356 | 120 | |||
3357 | 121 | bool fieldIsNull(int nField); | ||
3358 | 122 | bool fieldIsNull(const char* szField); | ||
3359 | 123 | |||
3360 | 124 | bool eof(); | ||
3361 | 125 | |||
3362 | 126 | void nextRow(); | ||
3363 | 127 | |||
3364 | 128 | void finalize(); | ||
3365 | 129 | |||
3366 | 130 | private: | ||
3367 | 131 | |||
3368 | 132 | void checkVM(); | ||
3369 | 133 | |||
3370 | 134 | sqlite3* mpDB; | ||
3371 | 135 | sqlite3_stmt* mpVM; | ||
3372 | 136 | bool mbEof; | ||
3373 | 137 | int mnCols; | ||
3374 | 138 | bool mbOwnVM; | ||
3375 | 139 | }; | ||
3376 | 140 | |||
3377 | 141 | class CppSQLite3Table | ||
3378 | 142 | { | ||
3379 | 143 | public: | ||
3380 | 144 | |||
3381 | 145 | CppSQLite3Table(); | ||
3382 | 146 | |||
3383 | 147 | CppSQLite3Table(const CppSQLite3Table& rTable); | ||
3384 | 148 | |||
3385 | 149 | CppSQLite3Table(char** paszResults, int nRows, int nCols); | ||
3386 | 150 | |||
3387 | 151 | virtual ~CppSQLite3Table(); | ||
3388 | 152 | |||
3389 | 153 | CppSQLite3Table& operator=(const CppSQLite3Table& rTable); | ||
3390 | 154 | |||
3391 | 155 | int numFields(); | ||
3392 | 156 | |||
3393 | 157 | int numRows(); | ||
3394 | 158 | |||
3395 | 159 | const char* fieldName(int nCol); | ||
3396 | 160 | |||
3397 | 161 | const char* fieldValue(int nField); | ||
3398 | 162 | const char* fieldValue(const char* szField); | ||
3399 | 163 | |||
3400 | 164 | int getIntField(int nField, int nNullValue=0); | ||
3401 | 165 | int getIntField(const char* szField, int nNullValue=0); | ||
3402 | 166 | |||
3403 | 167 | double getFloatField(int nField, double fNullValue=0.0); | ||
3404 | 168 | double getFloatField(const char* szField, double fNullValue=0.0); | ||
3405 | 169 | |||
3406 | 170 | const char* getStringField(int nField, const char* szNullValue=""); | ||
3407 | 171 | const char* getStringField(const char* szField, const char* szNullValue=""); | ||
3408 | 172 | |||
3409 | 173 | bool fieldIsNull(int nField); | ||
3410 | 174 | bool fieldIsNull(const char* szField); | ||
3411 | 175 | |||
3412 | 176 | void setRow(int nRow); | ||
3413 | 177 | |||
3414 | 178 | void finalize(); | ||
3415 | 179 | |||
3416 | 180 | private: | ||
3417 | 181 | |||
3418 | 182 | void checkResults(); | ||
3419 | 183 | |||
3420 | 184 | int mnCols; | ||
3421 | 185 | int mnRows; | ||
3422 | 186 | int mnCurrentRow; | ||
3423 | 187 | char** mpaszResults; | ||
3424 | 188 | }; | ||
3425 | 189 | |||
3426 | 190 | class CppSQLite3Statement | ||
3427 | 191 | { | ||
3428 | 192 | public: | ||
3429 | 193 | |||
3430 | 194 | CppSQLite3Statement(); | ||
3431 | 195 | |||
3432 | 196 | CppSQLite3Statement(const CppSQLite3Statement& rStatement); | ||
3433 | 197 | |||
3434 | 198 | CppSQLite3Statement(sqlite3* pDB, sqlite3_stmt* pVM); | ||
3435 | 199 | |||
3436 | 200 | virtual ~CppSQLite3Statement(); | ||
3437 | 201 | |||
3438 | 202 | CppSQLite3Statement& operator=(const CppSQLite3Statement& rStatement); | ||
3439 | 203 | |||
3440 | 204 | int execDML(); | ||
3441 | 205 | |||
3442 | 206 | CppSQLite3Query execQuery(); | ||
3443 | 207 | |||
3444 | 208 | void bind(int nParam, const char* szValue); | ||
3445 | 209 | void bind(int nParam, const int nValue); | ||
3446 | 210 | void bind(int nParam, const double dwValue); | ||
3447 | 211 | void bind(int nParam, const unsigned char* blobValue, int nLen); | ||
3448 | 212 | void bindNull(int nParam); | ||
3449 | 213 | |||
3450 | 214 | void reset(); | ||
3451 | 215 | |||
3452 | 216 | void finalize(); | ||
3453 | 217 | |||
3454 | 218 | private: | ||
3455 | 219 | |||
3456 | 220 | void checkDB(); | ||
3457 | 221 | void checkVM(); | ||
3458 | 222 | |||
3459 | 223 | sqlite3* mpDB; | ||
3460 | 224 | sqlite3_stmt* mpVM; | ||
3461 | 225 | }; | ||
3462 | 226 | |||
3463 | 227 | class CppSQLite3DB | ||
3464 | 228 | { | ||
3465 | 229 | public: | ||
3466 | 230 | |||
3467 | 231 | CppSQLite3DB(); | ||
3468 | 232 | |||
3469 | 233 | virtual ~CppSQLite3DB(); | ||
3470 | 234 | |||
3471 | 235 | void Open(const char* szFile); | ||
3472 | 236 | |||
3473 | 237 | void Close(); | ||
3474 | 238 | |||
3475 | 239 | bool tableExists(const char* szTable); | ||
3476 | 240 | |||
3477 | 241 | int execDML(const char* szSQL); | ||
3478 | 242 | |||
3479 | 243 | CppSQLite3Query execQuery(const char* szSQL); | ||
3480 | 244 | |||
3481 | 245 | int execScalar(const char* szSQL); | ||
3482 | 246 | |||
3483 | 247 | CppSQLite3Table getTable(const char* szSQL); | ||
3484 | 248 | |||
3485 | 249 | CppSQLite3Statement compileStatement(const char* szSQL); | ||
3486 | 250 | |||
3487 | 251 | sqlite_int64 lastRowId(); | ||
3488 | 252 | |||
3489 | 253 | void interrupt() { sqlite3_interrupt(mpDB); } | ||
3490 | 254 | |||
3491 | 255 | void setBusyTimeout(int nMillisecs); | ||
3492 | 256 | |||
3493 | 257 | static const char* SQLiteVersion() { return SQLITE_VERSION; } | ||
3494 | 258 | |||
3495 | 259 | private: | ||
3496 | 260 | |||
3497 | 261 | CppSQLite3DB(const CppSQLite3DB& db); | ||
3498 | 262 | CppSQLite3DB& operator=(const CppSQLite3DB& db); | ||
3499 | 263 | |||
3500 | 264 | sqlite3_stmt* compile(const char* szSQL); | ||
3501 | 265 | |||
3502 | 266 | void checkDB(); | ||
3503 | 267 | |||
3504 | 268 | sqlite3* mpDB; | ||
3505 | 269 | int mnBusyTimeoutMs; | ||
3506 | 270 | }; | ||
3507 | 0 | 271 | ||
3508 | === added file 'common/SHA1.cpp' | |||
3509 | --- common/SHA1.cpp 1970-01-01 00:00:00 +0000 | |||
3510 | +++ common/SHA1.cpp 2011-11-23 01:01:25 +0000 | |||
3511 | @@ -0,0 +1,282 @@ | |||
3512 | 1 | /* | ||
3513 | 2 | 100% free public domain implementation of the SHA-1 algorithm | ||
3514 | 3 | by Dominik Reichl <dominik.reichl@t-online.de> | ||
3515 | 4 | Web: http://www.dominik-reichl.de/ | ||
3516 | 5 | |||
3517 | 6 | Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches) | ||
3518 | 7 | - You can set the endianness in your files, no need to modify the | ||
3519 | 8 | header file of the CSHA1 class any more | ||
3520 | 9 | - Aligned data support | ||
3521 | 10 | - Made support/compilation of the utility functions (ReportHash | ||
3522 | 11 | and HashFile) optional (useful, if bytes count, for example in | ||
3523 | 12 | embedded environments) | ||
3524 | 13 | |||
3525 | 14 | Version 1.5 - 2005-01-01 | ||
3526 | 15 | - 64-bit compiler compatibility added | ||
3527 | 16 | - Made variable wiping optional (define SHA1_WIPE_VARIABLES) | ||
3528 | 17 | - Removed unnecessary variable initializations | ||
3529 | 18 | - ROL32 improvement for the Microsoft compiler (using _rotl) | ||
3530 | 19 | |||
3531 | 20 | ======== Test Vectors (from FIPS PUB 180-1) ======== | ||
3532 | 21 | |||
3533 | 22 | SHA1("abc") = | ||
3534 | 23 | A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D | ||
3535 | 24 | |||
3536 | 25 | SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") = | ||
3537 | 26 | 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 | ||
3538 | 27 | |||
3539 | 28 | SHA1(A million repetitions of "a") = | ||
3540 | 29 | 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F | ||
3541 | 30 | */ | ||
3542 | 31 | |||
3543 | 32 | #include "SHA1.h" | ||
3544 | 33 | |||
3545 | 34 | #ifdef SHA1_UTILITY_FUNCTIONS | ||
3546 | 35 | #define SHA1_MAX_FILE_BUFFER 8000 | ||
3547 | 36 | #endif | ||
3548 | 37 | |||
3549 | 38 | // Rotate x bits to the left | ||
3550 | 39 | #ifndef ROL32 | ||
3551 | 40 | #ifdef _MSC_VER | ||
3552 | 41 | #define ROL32(_val32, _nBits) _rotl(_val32, _nBits) | ||
3553 | 42 | #else | ||
3554 | 43 | #define ROL32(_val32, _nBits) (((_val32)<<(_nBits))|((_val32)>>(32-(_nBits)))) | ||
3555 | 44 | #endif | ||
3556 | 45 | #endif | ||
3557 | 46 | |||
3558 | 47 | #ifdef SHA1_LITTLE_ENDIAN | ||
3559 | 48 | #define SHABLK0(i) (m_block->l[i] = \ | ||
3560 | 49 | (ROL32(m_block->l[i],24) & 0xFF00FF00) | (ROL32(m_block->l[i],8) & 0x00FF00FF)) | ||
3561 | 50 | #else | ||
3562 | 51 | #define SHABLK0(i) (m_block->l[i]) | ||
3563 | 52 | #endif | ||
3564 | 53 | |||
3565 | 54 | #define SHABLK(i) (m_block->l[i&15] = ROL32(m_block->l[(i+13)&15] ^ m_block->l[(i+8)&15] \ | ||
3566 | 55 | ^ m_block->l[(i+2)&15] ^ m_block->l[i&15],1)) | ||
3567 | 56 | |||
3568 | 57 | // SHA-1 rounds | ||
3569 | 58 | #define _R0(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK0(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); } | ||
3570 | 59 | #define _R1(v,w,x,y,z,i) { z+=((w&(x^y))^y)+SHABLK(i)+0x5A827999+ROL32(v,5); w=ROL32(w,30); } | ||
3571 | 60 | #define _R2(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0x6ED9EBA1+ROL32(v,5); w=ROL32(w,30); } | ||
3572 | 61 | #define _R3(v,w,x,y,z,i) { z+=(((w|x)&y)|(w&x))+SHABLK(i)+0x8F1BBCDC+ROL32(v,5); w=ROL32(w,30); } | ||
3573 | 62 | #define _R4(v,w,x,y,z,i) { z+=(w^x^y)+SHABLK(i)+0xCA62C1D6+ROL32(v,5); w=ROL32(w,30); } | ||
3574 | 63 | |||
3575 | 64 | CSHA1::CSHA1() | ||
3576 | 65 | { | ||
3577 | 66 | m_block = (SHA1_WORKSPACE_BLOCK *)m_workspace; | ||
3578 | 67 | |||
3579 | 68 | Reset(); | ||
3580 | 69 | } | ||
3581 | 70 | |||
3582 | 71 | CSHA1::~CSHA1() | ||
3583 | 72 | { | ||
3584 | 73 | Reset(); | ||
3585 | 74 | } | ||
3586 | 75 | |||
3587 | 76 | void CSHA1::Reset() | ||
3588 | 77 | { | ||
3589 | 78 | // SHA1 initialization constants | ||
3590 | 79 | m_state[0] = 0x67452301; | ||
3591 | 80 | m_state[1] = 0xEFCDAB89; | ||
3592 | 81 | m_state[2] = 0x98BADCFE; | ||
3593 | 82 | m_state[3] = 0x10325476; | ||
3594 | 83 | m_state[4] = 0xC3D2E1F0; | ||
3595 | 84 | |||
3596 | 85 | m_count[0] = 0; | ||
3597 | 86 | m_count[1] = 0; | ||
3598 | 87 | } | ||
3599 | 88 | |||
3600 | 89 | void CSHA1::Transform(UINT_32* state, UINT_8* buffer) | ||
3601 | 90 | { | ||
3602 | 91 | // Copy state[] to working vars | ||
3603 | 92 | UINT_32 a = state[0], b = state[1], c = state[2], d = state[3], e = state[4]; | ||
3604 | 93 | |||
3605 | 94 | memcpy(m_block, buffer, 64); | ||
3606 | 95 | |||
3607 | 96 | // 4 rounds of 20 operations each. Loop unrolled. | ||
3608 | 97 | _R0(a, b, c, d, e, 0); _R0(e, a, b, c, d, 1); _R0(d, e, a, b, c, 2); _R0(c, d, e, a, b, 3); | ||
3609 | 98 | _R0(b, c, d, e, a, 4); _R0(a, b, c, d, e, 5); _R0(e, a, b, c, d, 6); _R0(d, e, a, b, c, 7); | ||
3610 | 99 | _R0(c, d, e, a, b, 8); _R0(b, c, d, e, a, 9); _R0(a, b, c, d, e, 10); _R0(e, a, b, c, d, 11); | ||
3611 | 100 | _R0(d, e, a, b, c, 12); _R0(c, d, e, a, b, 13); _R0(b, c, d, e, a, 14); _R0(a, b, c, d, e, 15); | ||
3612 | 101 | _R1(e, a, b, c, d, 16); _R1(d, e, a, b, c, 17); _R1(c, d, e, a, b, 18); _R1(b, c, d, e, a, 19); | ||
3613 | 102 | _R2(a, b, c, d, e, 20); _R2(e, a, b, c, d, 21); _R2(d, e, a, b, c, 22); _R2(c, d, e, a, b, 23); | ||
3614 | 103 | _R2(b, c, d, e, a, 24); _R2(a, b, c, d, e, 25); _R2(e, a, b, c, d, 26); _R2(d, e, a, b, c, 27); | ||
3615 | 104 | _R2(c, d, e, a, b, 28); _R2(b, c, d, e, a, 29); _R2(a, b, c, d, e, 30); _R2(e, a, b, c, d, 31); | ||
3616 | 105 | _R2(d, e, a, b, c, 32); _R2(c, d, e, a, b, 33); _R2(b, c, d, e, a, 34); _R2(a, b, c, d, e, 35); | ||
3617 | 106 | _R2(e, a, b, c, d, 36); _R2(d, e, a, b, c, 37); _R2(c, d, e, a, b, 38); _R2(b, c, d, e, a, 39); | ||
3618 | 107 | _R3(a, b, c, d, e, 40); _R3(e, a, b, c, d, 41); _R3(d, e, a, b, c, 42); _R3(c, d, e, a, b, 43); | ||
3619 | 108 | _R3(b, c, d, e, a, 44); _R3(a, b, c, d, e, 45); _R3(e, a, b, c, d, 46); _R3(d, e, a, b, c, 47); | ||
3620 | 109 | _R3(c, d, e, a, b, 48); _R3(b, c, d, e, a, 49); _R3(a, b, c, d, e, 50); _R3(e, a, b, c, d, 51); | ||
3621 | 110 | _R3(d, e, a, b, c, 52); _R3(c, d, e, a, b, 53); _R3(b, c, d, e, a, 54); _R3(a, b, c, d, e, 55); | ||
3622 | 111 | _R3(e, a, b, c, d, 56); _R3(d, e, a, b, c, 57); _R3(c, d, e, a, b, 58); _R3(b, c, d, e, a, 59); | ||
3623 | 112 | _R4(a, b, c, d, e, 60); _R4(e, a, b, c, d, 61); _R4(d, e, a, b, c, 62); _R4(c, d, e, a, b, 63); | ||
3624 | 113 | _R4(b, c, d, e, a, 64); _R4(a, b, c, d, e, 65); _R4(e, a, b, c, d, 66); _R4(d, e, a, b, c, 67); | ||
3625 | 114 | _R4(c, d, e, a, b, 68); _R4(b, c, d, e, a, 69); _R4(a, b, c, d, e, 70); _R4(e, a, b, c, d, 71); | ||
3626 | 115 | _R4(d, e, a, b, c, 72); _R4(c, d, e, a, b, 73); _R4(b, c, d, e, a, 74); _R4(a, b, c, d, e, 75); | ||
3627 | 116 | _R4(e, a, b, c, d, 76); _R4(d, e, a, b, c, 77); _R4(c, d, e, a, b, 78); _R4(b, c, d, e, a, 79); | ||
3628 | 117 | |||
3629 | 118 | // Add the working vars back into state | ||
3630 | 119 | state[0] += a; | ||
3631 | 120 | state[1] += b; | ||
3632 | 121 | state[2] += c; | ||
3633 | 122 | state[3] += d; | ||
3634 | 123 | state[4] += e; | ||
3635 | 124 | |||
3636 | 125 | // Wipe variables | ||
3637 | 126 | #ifdef SHA1_WIPE_VARIABLES | ||
3638 | 127 | a = b = c = d = e = 0; | ||
3639 | 128 | #endif | ||
3640 | 129 | } | ||
3641 | 130 | |||
3642 | 131 | // Use this function to hash in binary data and strings | ||
3643 | 132 | void CSHA1::Update(UINT_8* data, UINT_32 len) | ||
3644 | 133 | { | ||
3645 | 134 | UINT_32 i, j; | ||
3646 | 135 | |||
3647 | 136 | j = (m_count[0] >> 3) & 63; | ||
3648 | 137 | |||
3649 | 138 | if((m_count[0] += len << 3) < (len << 3)) | ||
3650 | 139 | m_count[1]++; | ||
3651 | 140 | |||
3652 | 141 | m_count[1] += (len >> 29); | ||
3653 | 142 | |||
3654 | 143 | if((j + len) > 63) | ||
3655 | 144 | { | ||
3656 | 145 | i = 64 - j; | ||
3657 | 146 | memcpy(&m_buffer[j], data, i); | ||
3658 | 147 | Transform(m_state, m_buffer); | ||
3659 | 148 | |||
3660 | 149 | for(;i + 63 < len;i += 64) | ||
3661 | 150 | Transform(m_state, &data[i]); | ||
3662 | 151 | |||
3663 | 152 | j = 0; | ||
3664 | 153 | } | ||
3665 | 154 | else | ||
3666 | 155 | i = 0; | ||
3667 | 156 | |||
3668 | 157 | memcpy(&m_buffer[j], &data[i], len - i); | ||
3669 | 158 | } | ||
3670 | 159 | |||
3671 | 160 | #ifdef SHA1_UTILITY_FUNCTIONS | ||
3672 | 161 | // Hash in file contents | ||
3673 | 162 | bool CSHA1::HashFile(char* szFileName) | ||
3674 | 163 | { | ||
3675 | 164 | unsigned long ulFileSize, ulRest, ulBlocks; | ||
3676 | 165 | unsigned long i; | ||
3677 | 166 | UINT_8 uData[SHA1_MAX_FILE_BUFFER]; | ||
3678 | 167 | FILE* fIn; | ||
3679 | 168 | |||
3680 | 169 | if(szFileName == NULL) | ||
3681 | 170 | return false; | ||
3682 | 171 | |||
3683 | 172 | fIn = fopen(szFileName, "rb"); | ||
3684 | 173 | if(fIn == NULL) | ||
3685 | 174 | return false; | ||
3686 | 175 | |||
3687 | 176 | fseek(fIn, 0, SEEK_END); | ||
3688 | 177 | ulFileSize = (unsigned long)ftell(fIn); | ||
3689 | 178 | fseek(fIn, 0, SEEK_SET); | ||
3690 | 179 | |||
3691 | 180 | if(ulFileSize != 0) | ||
3692 | 181 | { | ||
3693 | 182 | ulBlocks = ulFileSize / SHA1_MAX_FILE_BUFFER; | ||
3694 | 183 | ulRest = ulFileSize % SHA1_MAX_FILE_BUFFER; | ||
3695 | 184 | } | ||
3696 | 185 | else | ||
3697 | 186 | { | ||
3698 | 187 | ulBlocks = 0; | ||
3699 | 188 | ulRest = 0; | ||
3700 | 189 | } | ||
3701 | 190 | |||
3702 | 191 | for(i = 0;i < ulBlocks;i++) | ||
3703 | 192 | { | ||
3704 | 193 | fread(uData, 1, SHA1_MAX_FILE_BUFFER, fIn); | ||
3705 | 194 | Update((UINT_8 *)uData, SHA1_MAX_FILE_BUFFER); | ||
3706 | 195 | } | ||
3707 | 196 | |||
3708 | 197 | if(ulRest != 0) | ||
3709 | 198 | { | ||
3710 | 199 | fread(uData, 1, ulRest, fIn); | ||
3711 | 200 | Update((UINT_8 *)uData, ulRest); | ||
3712 | 201 | } | ||
3713 | 202 | |||
3714 | 203 | fclose(fIn); fIn = NULL; | ||
3715 | 204 | return true; | ||
3716 | 205 | } | ||
3717 | 206 | |||
3718 | 207 | #endif | ||
3719 | 208 | |||
3720 | 209 | void CSHA1::Final() | ||
3721 | 210 | { | ||
3722 | 211 | UINT_32 i; | ||
3723 | 212 | UINT_8 finalcount[8]; | ||
3724 | 213 | |||
3725 | 214 | for(i = 0;i < 8;i++) | ||
3726 | 215 | finalcount[i] = (UINT_8)((m_count[((i >= 4) ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); // Endian independent | ||
3727 | 216 | |||
3728 | 217 | Update((UINT_8 *)"\200", 1); | ||
3729 | 218 | |||
3730 | 219 | while((m_count[0] & 504) != 448) | ||
3731 | 220 | Update((UINT_8 *)"\0", 1); | ||
3732 | 221 | |||
3733 | 222 | Update(finalcount, 8); // Cause a SHA1Transform() | ||
3734 | 223 | |||
3735 | 224 | for(i = 0;i < 20;i++) | ||
3736 | 225 | { | ||
3737 | 226 | m_digest[i] = (UINT_8)((m_state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255); | ||
3738 | 227 | } | ||
3739 | 228 | |||
3740 | 229 | // Wipe variables for security reasons | ||
3741 | 230 | #ifdef SHA1_WIPE_VARIABLES | ||
3742 | 231 | i = 0; | ||
3743 | 232 | memset(m_buffer, 0, 64); | ||
3744 | 233 | memset(m_state, 0, 20); | ||
3745 | 234 | memset(m_count, 0, 8); | ||
3746 | 235 | memset(finalcount, 0, 8); | ||
3747 | 236 | Transform(m_state, m_buffer); | ||
3748 | 237 | #endif | ||
3749 | 238 | } | ||
3750 | 239 | |||
3751 | 240 | #ifdef SHA1_UTILITY_FUNCTIONS | ||
3752 | 241 | // Get the final hash as a pre-formatted string | ||
3753 | 242 | void CSHA1::ReportHash(char* szReport, unsigned char uReportType) | ||
3754 | 243 | { | ||
3755 | 244 | unsigned char i; | ||
3756 | 245 | char szTemp[16]; | ||
3757 | 246 | |||
3758 | 247 | if(szReport == NULL) | ||
3759 | 248 | return; | ||
3760 | 249 | |||
3761 | 250 | if(uReportType == REPORT_HEX) | ||
3762 | 251 | { | ||
3763 | 252 | sprintf(szTemp, "%02X", m_digest[0]); | ||
3764 | 253 | strcat(szReport, szTemp); | ||
3765 | 254 | |||
3766 | 255 | for(i = 1;i < 20;i++) | ||
3767 | 256 | { | ||
3768 | 257 | sprintf(szTemp, " %02X", m_digest[i]); | ||
3769 | 258 | strcat(szReport, szTemp); | ||
3770 | 259 | } | ||
3771 | 260 | } | ||
3772 | 261 | else if(uReportType == REPORT_DIGIT) | ||
3773 | 262 | { | ||
3774 | 263 | sprintf(szTemp, "%u", m_digest[0]); | ||
3775 | 264 | strcat(szReport, szTemp); | ||
3776 | 265 | |||
3777 | 266 | for(i = 1;i < 20;i++) | ||
3778 | 267 | { | ||
3779 | 268 | sprintf(szTemp, " %u", m_digest[i]); | ||
3780 | 269 | strcat(szReport, szTemp); | ||
3781 | 270 | } | ||
3782 | 271 | } | ||
3783 | 272 | else | ||
3784 | 273 | strcpy(szReport, "Error: Unknown report type!"); | ||
3785 | 274 | } | ||
3786 | 275 | |||
3787 | 276 | #endif | ||
3788 | 277 | |||
3789 | 278 | // Get the raw message digest | ||
3790 | 279 | void CSHA1::GetHash(UINT_8* puDest) | ||
3791 | 280 | { | ||
3792 | 281 | memcpy(puDest, m_digest, 20); | ||
3793 | 282 | } | ||
3794 | 0 | 283 | ||
3795 | === added file 'common/SHA1.h' | |||
3796 | --- common/SHA1.h 1970-01-01 00:00:00 +0000 | |||
3797 | +++ common/SHA1.h 2011-11-23 01:01:25 +0000 | |||
3798 | @@ -0,0 +1,150 @@ | |||
3799 | 1 | /* | ||
3800 | 2 | 100% free public domain implementation of the SHA-1 algorithm | ||
3801 | 3 | by Dominik Reichl <dominik.reichl@t-online.de> | ||
3802 | 4 | Web: http://www.dominik-reichl.de/ | ||
3803 | 5 | |||
3804 | 6 | Version 1.6 - 2005-02-07 (thanks to Howard Kapustein for patches) | ||
3805 | 7 | - You can set the endianness in your files, no need to modify the | ||
3806 | 8 | header file of the CSHA1 class any more | ||
3807 | 9 | - Aligned data support | ||
3808 | 10 | - Made support/compilation of the utility functions (ReportHash | ||
3809 | 11 | and HashFile) optional (useful, if bytes count, for example in | ||
3810 | 12 | embedded environments) | ||
3811 | 13 | |||
3812 | 14 | Version 1.5 - 2005-01-01 | ||
3813 | 15 | - 64-bit compiler compatibility added | ||
3814 | 16 | - Made variable wiping optional (define SHA1_WIPE_VARIABLES) | ||
3815 | 17 | - Removed unnecessary variable initializations | ||
3816 | 18 | - ROL32 improvement for the Microsoft compiler (using _rotl) | ||
3817 | 19 | |||
3818 | 20 | ======== Test Vectors (from FIPS PUB 180-1) ======== | ||
3819 | 21 | |||
3820 | 22 | SHA1("abc") = | ||
3821 | 23 | A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D | ||
3822 | 24 | |||
3823 | 25 | SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") = | ||
3824 | 26 | 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 | ||
3825 | 27 | |||
3826 | 28 | SHA1(A million repetitions of "a") = | ||
3827 | 29 | 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F | ||
3828 | 30 | */ | ||
3829 | 31 | |||
3830 | 32 | #ifndef ___SHA1_HDR___ | ||
3831 | 33 | #define ___SHA1_HDR___ | ||
3832 | 34 | |||
3833 | 35 | #if !defined(SHA1_UTILITY_FUNCTIONS) && !defined(SHA1_NO_UTILITY_FUNCTIONS) | ||
3834 | 36 | #define SHA1_UTILITY_FUNCTIONS | ||
3835 | 37 | #endif | ||
3836 | 38 | |||
3837 | 39 | #include <memory.h> // Needed for memset and memcpy | ||
3838 | 40 | |||
3839 | 41 | #ifdef SHA1_UTILITY_FUNCTIONS | ||
3840 | 42 | #include <stdio.h> // Needed for file access and sprintf | ||
3841 | 43 | #include <string.h> // Needed for strcat and strcpy | ||
3842 | 44 | #endif | ||
3843 | 45 | |||
3844 | 46 | #ifdef _MSC_VER | ||
3845 | 47 | #include <stdlib.h> | ||
3846 | 48 | #endif | ||
3847 | 49 | |||
3848 | 50 | // You can define the endian mode in your files, without modifying the SHA1 | ||
3849 | 51 | // source files. Just #define SHA1_LITTLE_ENDIAN or #define SHA1_BIG_ENDIAN | ||
3850 | 52 | // in your files, before including the SHA1.h header file. If you don't | ||
3851 | 53 | // define anything, the class defaults to little endian. | ||
3852 | 54 | |||
3853 | 55 | #if !defined(SHA1_LITTLE_ENDIAN) && !defined(SHA1_BIG_ENDIAN) | ||
3854 | 56 | #define SHA1_LITTLE_ENDIAN | ||
3855 | 57 | #endif | ||
3856 | 58 | |||
3857 | 59 | // Same here. If you want variable wiping, #define SHA1_WIPE_VARIABLES, if | ||
3858 | 60 | // not, #define SHA1_NO_WIPE_VARIABLES. If you don't define anything, it | ||
3859 | 61 | // defaults to wiping. | ||
3860 | 62 | |||
3861 | 63 | #if !defined(SHA1_WIPE_VARIABLES) && !defined(SHA1_NO_WIPE_VARIABLES) | ||
3862 | 64 | #define SHA1_WIPE_VARIABLES | ||
3863 | 65 | #endif | ||
3864 | 66 | |||
3865 | 67 | ///////////////////////////////////////////////////////////////////////////// | ||
3866 | 68 | // Define 8- and 32-bit variables | ||
3867 | 69 | |||
3868 | 70 | #ifndef UINT_32 | ||
3869 | 71 | |||
3870 | 72 | #ifdef _MSC_VER | ||
3871 | 73 | |||
3872 | 74 | #define UINT_8 unsigned __int8 | ||
3873 | 75 | #define UINT_32 unsigned __int32 | ||
3874 | 76 | |||
3875 | 77 | #else | ||
3876 | 78 | |||
3877 | 79 | #define UINT_8 unsigned char | ||
3878 | 80 | /*AFF | ||
3879 | 81 | #if (ULONG_MAX == 0xFFFFFFFF) | ||
3880 | 82 | #define UINT_32 unsigned long | ||
3881 | 83 | #else | ||
3882 | 84 | #define UINT_32 unsigned int | ||
3883 | 85 | #endif | ||
3884 | 86 | */ | ||
3885 | 87 | #define UINT_32 unsigned int | ||
3886 | 88 | |||
3887 | 89 | #endif | ||
3888 | 90 | #endif | ||
3889 | 91 | |||
3890 | 92 | ///////////////////////////////////////////////////////////////////////////// | ||
3891 | 93 | // Declare SHA1 workspace | ||
3892 | 94 | |||
3893 | 95 | typedef union | ||
3894 | 96 | { | ||
3895 | 97 | UINT_8 c[64]; | ||
3896 | 98 | UINT_32 l[16]; | ||
3897 | 99 | } SHA1_WORKSPACE_BLOCK; | ||
3898 | 100 | |||
3899 | 101 | class CSHA1 | ||
3900 | 102 | { | ||
3901 | 103 | public: | ||
3902 | 104 | #ifdef SHA1_UTILITY_FUNCTIONS | ||
3903 | 105 | // Two different formats for ReportHash(...) | ||
3904 | 106 | enum | ||
3905 | 107 | { | ||
3906 | 108 | REPORT_HEX = 0, | ||
3907 | 109 | REPORT_DIGIT = 1 | ||
3908 | 110 | }; | ||
3909 | 111 | #endif | ||
3910 | 112 | |||
3911 | 113 | // Constructor and Destructor | ||
3912 | 114 | CSHA1(); | ||
3913 | 115 | ~CSHA1(); | ||
3914 | 116 | |||
3915 | 117 | UINT_32 m_state[5]; | ||
3916 | 118 | UINT_32 m_count[2]; | ||
3917 | 119 | UINT_32 __reserved1[1]; | ||
3918 | 120 | UINT_8 m_buffer[64]; | ||
3919 | 121 | UINT_8 m_digest[20]; | ||
3920 | 122 | UINT_32 __reserved2[3]; | ||
3921 | 123 | |||
3922 | 124 | void Reset(); | ||
3923 | 125 | |||
3924 | 126 | // Update the hash value | ||
3925 | 127 | void Update(UINT_8* data, UINT_32 len); | ||
3926 | 128 | #ifdef SHA1_UTILITY_FUNCTIONS | ||
3927 | 129 | bool HashFile(char* szFileName); | ||
3928 | 130 | #endif | ||
3929 | 131 | |||
3930 | 132 | // Finalize hash and report | ||
3931 | 133 | void Final(); | ||
3932 | 134 | |||
3933 | 135 | // Report functions: as pre-formatted and raw data | ||
3934 | 136 | #ifdef SHA1_UTILITY_FUNCTIONS | ||
3935 | 137 | void ReportHash(char* szReport, unsigned char uReportType = REPORT_HEX); | ||
3936 | 138 | #endif | ||
3937 | 139 | void GetHash(UINT_8* puDest); | ||
3938 | 140 | |||
3939 | 141 | private: | ||
3940 | 142 | // Private SHA-1 transformation | ||
3941 | 143 | void Transform(UINT_32* state, UINT_8* buffer); | ||
3942 | 144 | |||
3943 | 145 | // Member variables | ||
3944 | 146 | UINT_8 m_workspace[64]; | ||
3945 | 147 | SHA1_WORKSPACE_BLOCK* m_block; // SHA1 pointer to the byte array above | ||
3946 | 148 | }; | ||
3947 | 149 | |||
3948 | 150 | #endif | ||
3949 | 0 | 151 | ||
3950 | === added file 'common/SetupInfo.cpp' | |||
3951 | --- common/SetupInfo.cpp 1970-01-01 00:00:00 +0000 | |||
3952 | +++ common/SetupInfo.cpp 2011-11-23 01:01:25 +0000 | |||
3953 | @@ -0,0 +1,512 @@ | |||
3954 | 1 | //////////////////////////////////////////////////////////////////////////////// | ||
3955 | 2 | // Copyright (c) CitizenLab, 2006. All Rights Reserved. | ||
3956 | 3 | // The latest version of this code is available at http://psiphon.civisec.org/ | ||
3957 | 4 | // | ||
3958 | 5 | // This software is open source; you can redistribute it and/or modify it | ||
3959 | 6 | // under the terms of the GNU General Public License as published by the | ||
3960 | 7 | // Free Software Foundation; either version 2 of the License, or (at your | ||
3961 | 8 | // option) any later version. | ||
3962 | 9 | // | ||
3963 | 10 | // This program is distributed WITHOUT ANY WARRANTY; without even the | ||
3964 | 11 | // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
3965 | 12 | // See the GNU General Public License for more details at: | ||
3966 | 13 | // http://psiphon.civisec.org/ | ||
3967 | 14 | // | ||
3968 | 15 | // This General Public License does NOT permit incorporating this software | ||
3969 | 16 | // into proprietary programs. | ||
3970 | 17 | //////////////////////////////////////////////////////////////////////////////// | ||
3971 | 18 | |||
3972 | 19 | #include "SetupInfo.h" | ||
3973 | 20 | #include <stdio.h> | ||
3974 | 21 | #include <string.h> | ||
3975 | 22 | #include <stdlib.h> | ||
3976 | 23 | #include "strfunc.h" | ||
3977 | 24 | #include <sys/stat.h> | ||
3978 | 25 | |||
3979 | 26 | #define SETUP_FUNC "psiphon.cnf" | ||
3980 | 27 | |||
3981 | 28 | #define _T(X) X | ||
3982 | 29 | |||
3983 | 30 | CSetupInfo g_info; | ||
3984 | 31 | |||
3985 | 32 | CSetupInfo::CSetupInfo() | ||
3986 | 33 | { | ||
3987 | 34 | Init(); | ||
3988 | 35 | } | ||
3989 | 36 | |||
3990 | 37 | CSetupInfo::CSetupInfo(const char* sIn) | ||
3991 | 38 | { | ||
3992 | 39 | Init(); | ||
3993 | 40 | Parse(sIn); | ||
3994 | 41 | } | ||
3995 | 42 | |||
3996 | 43 | CSetupInfo::~CSetupInfo() | ||
3997 | 44 | { | ||
3998 | 45 | delete m_sIP; | ||
3999 | 46 | delete m_sName; | ||
4000 | 47 | delete m_Common; | ||
4001 | 48 | delete m_Country; | ||
4002 | 49 | delete m_State; | ||
4003 | 50 | delete m_Email; | ||
4004 | 51 | delete m_Unit; | ||
4005 | 52 | delete m_Organization; | ||
4006 | 53 | delete m_sIPServer; | ||
4007 | 54 | delete m_sFinger; | ||
4008 | 55 | } | ||
4009 | 56 | |||
4010 | 57 | void CSetupInfo::Init() | ||
4011 | 58 | { | ||
4012 | 59 | m_sIP = strdup(_T("127.0.0.1")); | ||
4013 | 60 | m_nPort = 443; | ||
4014 | 61 | m_nShowImages = 1; | ||
4015 | 62 | m_nAutoUpdateIP = 1; | ||
4016 | 63 | m_nTestPort = 1; | ||
4017 | 64 | m_nStartOnStartUp = 0; | ||
4018 | 65 | m_sName = NULL; | ||
4019 | 66 | m_nCheckForUpdatesOnStartUp = 1; | ||
4020 | 67 | m_Common = strdup(_T("somename.somewhere.com")); | ||
4021 | 68 | m_Country = strdup(_T("US")); | ||
4022 | 69 | m_State = strdup(_T("Some State")); | ||
4023 | 70 | m_Email = strdup(_T("root@somename.somewhere.com")); | ||
4024 | 71 | m_Unit = strdup(_T("My Subunit of Large Organization")); | ||
4025 | 72 | m_Organization = strdup(_T("My Large Organization Name")); | ||
4026 | 73 | m_nRegenrateOnStartUp = 0; | ||
4027 | 74 | m_nLogLevel = 0; | ||
4028 | 75 | m_sIPServer = strdup(_T("http://psiphon.civisec.org/ip.php")); | ||
4029 | 76 | m_sFinger = NULL; | ||
4030 | 77 | } | ||
4031 | 78 | |||
4032 | 79 | void CSetupInfo::Out(char*& sOut) | ||
4033 | 80 | { | ||
4034 | 81 | char FORMAT[] = _T("%s\t%d\t%d\t%d\t%d\t%d\t%s\t%d\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%d\t%s"); | ||
4035 | 82 | |||
4036 | 83 | int nSize = GetStrLength(FORMAT) | ||
4037 | 84 | + GetStrLength(m_sIP) | ||
4038 | 85 | + GetStrLength(m_nPort) | ||
4039 | 86 | + GetStrLength(m_nShowImages) | ||
4040 | 87 | + GetStrLength(m_nAutoUpdateIP) | ||
4041 | 88 | + GetStrLength(m_nTestPort) | ||
4042 | 89 | + GetStrLength(m_nStartOnStartUp) | ||
4043 | 90 | + GetStrLength(m_sName) | ||
4044 | 91 | + GetStrLength(m_nCheckForUpdatesOnStartUp) | ||
4045 | 92 | + GetStrLength(m_Common) | ||
4046 | 93 | + GetStrLength(m_Country) | ||
4047 | 94 | + GetStrLength(m_State) | ||
4048 | 95 | + GetStrLength(m_Email) | ||
4049 | 96 | + GetStrLength(m_Organization) | ||
4050 | 97 | + GetStrLength(m_Unit) | ||
4051 | 98 | + GetStrLength(m_nRegenrateOnStartUp) | ||
4052 | 99 | + GetStrLength(m_nLogLevel) | ||
4053 | 100 | + GetStrLength(m_sIPServer); | ||
4054 | 101 | |||
4055 | 102 | sOut = new char[nSize]; | ||
4056 | 103 | |||
4057 | 104 | int nLen = _snprintf(sOut, nSize, FORMAT | ||
4058 | 105 | , m_sIP | ||
4059 | 106 | , m_nPort | ||
4060 | 107 | , m_nShowImages | ||
4061 | 108 | , m_nAutoUpdateIP | ||
4062 | 109 | , m_nTestPort | ||
4063 | 110 | , m_nStartOnStartUp | ||
4064 | 111 | , m_sName | ||
4065 | 112 | , m_nCheckForUpdatesOnStartUp | ||
4066 | 113 | , m_Common | ||
4067 | 114 | , m_Country | ||
4068 | 115 | , m_State | ||
4069 | 116 | , m_Email | ||
4070 | 117 | , m_Organization | ||
4071 | 118 | , m_Unit | ||
4072 | 119 | , m_nRegenrateOnStartUp | ||
4073 | 120 | , m_nLogLevel | ||
4074 | 121 | , m_sIPServer); | ||
4075 | 122 | |||
4076 | 123 | if(nLen <= 0 || nLen >= nSize) | ||
4077 | 124 | { | ||
4078 | 125 | delete sOut; | ||
4079 | 126 | sOut = NULL; | ||
4080 | 127 | } | ||
4081 | 128 | } | ||
4082 | 129 | |||
4083 | 130 | void CSetupInfo::Parse(const char* sIn) | ||
4084 | 131 | { | ||
4085 | 132 | if(!sIn || !*sIn) | ||
4086 | 133 | return; | ||
4087 | 134 | |||
4088 | 135 | char* _sIn = strdup(sIn); | ||
4089 | 136 | |||
4090 | 137 | char seps[] = _T("\t"); | ||
4091 | 138 | char* token = strtok(_sIn, seps); | ||
4092 | 139 | |||
4093 | 140 | if(!token) | ||
4094 | 141 | return; | ||
4095 | 142 | |||
4096 | 143 | if(strcmp(token, _T("NULL")) != 0) | ||
4097 | 144 | SetIP(token); | ||
4098 | 145 | |||
4099 | 146 | token = strtok(NULL, seps); | ||
4100 | 147 | if(!token) | ||
4101 | 148 | { | ||
4102 | 149 | delete _sIn; | ||
4103 | 150 | return; | ||
4104 | 151 | } | ||
4105 | 152 | SetPort(atoi(token)); | ||
4106 | 153 | |||
4107 | 154 | token = strtok(NULL, seps); | ||
4108 | 155 | if(!token) | ||
4109 | 156 | { | ||
4110 | 157 | delete _sIn; | ||
4111 | 158 | return; | ||
4112 | 159 | } | ||
4113 | 160 | SetShowImages(atoi(token)); | ||
4114 | 161 | |||
4115 | 162 | token = strtok(NULL, seps); | ||
4116 | 163 | if(!token) | ||
4117 | 164 | { | ||
4118 | 165 | delete _sIn; | ||
4119 | 166 | return; | ||
4120 | 167 | } | ||
4121 | 168 | SetAutoUpdateIP(atoi(token)); | ||
4122 | 169 | |||
4123 | 170 | token = strtok(NULL, seps); | ||
4124 | 171 | if(!token) | ||
4125 | 172 | { | ||
4126 | 173 | delete _sIn; | ||
4127 | 174 | return; | ||
4128 | 175 | } | ||
4129 | 176 | SetTestPort(atoi(token)); | ||
4130 | 177 | |||
4131 | 178 | token = strtok(NULL, seps); | ||
4132 | 179 | if(!token) | ||
4133 | 180 | { | ||
4134 | 181 | delete _sIn; | ||
4135 | 182 | return; | ||
4136 | 183 | } | ||
4137 | 184 | SetStartOnStartUp(atoi(token)); | ||
4138 | 185 | |||
4139 | 186 | token = strtok(NULL, seps); | ||
4140 | 187 | if(!token) | ||
4141 | 188 | { | ||
4142 | 189 | delete _sIn; | ||
4143 | 190 | return; | ||
4144 | 191 | } | ||
4145 | 192 | SetName(token); | ||
4146 | 193 | |||
4147 | 194 | token = strtok(NULL, seps); | ||
4148 | 195 | if(!token) | ||
4149 | 196 | { | ||
4150 | 197 | delete _sIn; | ||
4151 | 198 | return; | ||
4152 | 199 | } | ||
4153 | 200 | SetCheckForUpdatesOnStartUp(atoi(token)); | ||
4154 | 201 | |||
4155 | 202 | token = strtok(NULL, seps); | ||
4156 | 203 | if(!token) | ||
4157 | 204 | { | ||
4158 | 205 | delete _sIn; | ||
4159 | 206 | return; | ||
4160 | 207 | } | ||
4161 | 208 | SetCommon(token); | ||
4162 | 209 | |||
4163 | 210 | token = strtok(NULL, seps); | ||
4164 | 211 | if(!token) | ||
4165 | 212 | { | ||
4166 | 213 | delete _sIn; | ||
4167 | 214 | return; | ||
4168 | 215 | } | ||
4169 | 216 | SetCountry(token); | ||
4170 | 217 | |||
4171 | 218 | token = strtok(NULL, seps); | ||
4172 | 219 | if(!token) | ||
4173 | 220 | { | ||
4174 | 221 | delete _sIn; | ||
4175 | 222 | return; | ||
4176 | 223 | } | ||
4177 | 224 | SetState(token); | ||
4178 | 225 | |||
4179 | 226 | token = strtok(NULL, seps); | ||
4180 | 227 | if(!token) | ||
4181 | 228 | { | ||
4182 | 229 | delete _sIn; | ||
4183 | 230 | return; | ||
4184 | 231 | } | ||
4185 | 232 | SetEmail(token); | ||
4186 | 233 | |||
4187 | 234 | token = strtok(NULL, seps); | ||
4188 | 235 | if(!token) | ||
4189 | 236 | { | ||
4190 | 237 | delete _sIn; | ||
4191 | 238 | return; | ||
4192 | 239 | } | ||
4193 | 240 | SetOrganization(token); | ||
4194 | 241 | |||
4195 | 242 | token = strtok(NULL, seps); | ||
4196 | 243 | if(!token) | ||
4197 | 244 | { | ||
4198 | 245 | delete _sIn; | ||
4199 | 246 | return; | ||
4200 | 247 | } | ||
4201 | 248 | SetUnit(token); | ||
4202 | 249 | |||
4203 | 250 | token = strtok(NULL, seps); | ||
4204 | 251 | if(!token) | ||
4205 | 252 | { | ||
4206 | 253 | delete _sIn; | ||
4207 | 254 | return; | ||
4208 | 255 | } | ||
4209 | 256 | SetRegenrateOnStartUp(atoi(token)); | ||
4210 | 257 | |||
4211 | 258 | token = strtok(NULL, seps); | ||
4212 | 259 | if(!token) | ||
4213 | 260 | { | ||
4214 | 261 | delete _sIn; | ||
4215 | 262 | return; | ||
4216 | 263 | } | ||
4217 | 264 | SetLogLevel(atoi(token)); | ||
4218 | 265 | |||
4219 | 266 | token = strtok(NULL, seps); | ||
4220 | 267 | if(!token) | ||
4221 | 268 | { | ||
4222 | 269 | delete _sIn; | ||
4223 | 270 | return; | ||
4224 | 271 | } | ||
4225 | 272 | SetIPServer(token); | ||
4226 | 273 | |||
4227 | 274 | delete _sIn; | ||
4228 | 275 | } | ||
4229 | 276 | |||
4230 | 277 | bool CSetupInfo::Store() | ||
4231 | 278 | { | ||
4232 | 279 | FILE* pFile = fopen(SETUP_FUNC, _T("w")); | ||
4233 | 280 | if(pFile == NULL) | ||
4234 | 281 | return false; | ||
4235 | 282 | |||
4236 | 283 | char* pOut = NULL; | ||
4237 | 284 | Out(pOut); | ||
4238 | 285 | if(pOut == NULL) | ||
4239 | 286 | return false; | ||
4240 | 287 | |||
4241 | 288 | fwrite(pOut, sizeof(char), strlen(pOut), pFile); | ||
4242 | 289 | |||
4243 | 290 | delete pOut; | ||
4244 | 291 | |||
4245 | 292 | fclose(pFile); | ||
4246 | 293 | |||
4247 | 294 | return true; | ||
4248 | 295 | } | ||
4249 | 296 | |||
4250 | 297 | void CSetupInfo::Load() | ||
4251 | 298 | { | ||
4252 | 299 | struct stat results; | ||
4253 | 300 | if(stat(SETUP_FUNC, &results) != 0) | ||
4254 | 301 | return; | ||
4255 | 302 | |||
4256 | 303 | FILE* pFile = fopen(SETUP_FUNC, _T("r")); | ||
4257 | 304 | if(pFile == NULL) | ||
4258 | 305 | return; | ||
4259 | 306 | |||
4260 | 307 | int nSize = results.st_size/sizeof(char) + 1; | ||
4261 | 308 | |||
4262 | 309 | char* pIn = new char[nSize]; | ||
4263 | 310 | memset(pIn, 0, nSize * sizeof(char)); | ||
4264 | 311 | |||
4265 | 312 | fread(pIn, sizeof(char), nSize, pFile); | ||
4266 | 313 | |||
4267 | 314 | pIn[nSize - 1] = 0; | ||
4268 | 315 | |||
4269 | 316 | Parse(pIn); | ||
4270 | 317 | |||
4271 | 318 | delete pIn; | ||
4272 | 319 | |||
4273 | 320 | fclose(pFile); | ||
4274 | 321 | } | ||
4275 | 322 | |||
4276 | 323 | char* CSetupInfo::GetIP() | ||
4277 | 324 | { | ||
4278 | 325 | return m_sIP; | ||
4279 | 326 | } | ||
4280 | 327 | |||
4281 | 328 | void CSetupInfo::SetIP(const char* _sIP) | ||
4282 | 329 | { | ||
4283 | 330 | delete m_sIP; | ||
4284 | 331 | m_sIP = strdup(_sIP); | ||
4285 | 332 | } | ||
4286 | 333 | |||
4287 | 334 | int CSetupInfo::GetPort() | ||
4288 | 335 | { | ||
4289 | 336 | return m_nPort; | ||
4290 | 337 | } | ||
4291 | 338 | |||
4292 | 339 | void CSetupInfo::SetPort(int _nPort) | ||
4293 | 340 | { | ||
4294 | 341 | m_nPort = _nPort; | ||
4295 | 342 | } | ||
4296 | 343 | |||
4297 | 344 | int CSetupInfo::GetShowImages() | ||
4298 | 345 | { | ||
4299 | 346 | return m_nShowImages; | ||
4300 | 347 | } | ||
4301 | 348 | |||
4302 | 349 | void CSetupInfo::SetShowImages(int _nShowImages) | ||
4303 | 350 | { | ||
4304 | 351 | m_nShowImages = _nShowImages; | ||
4305 | 352 | } | ||
4306 | 353 | |||
4307 | 354 | int CSetupInfo::GetAutoUpdateIP() | ||
4308 | 355 | { | ||
4309 | 356 | return m_nAutoUpdateIP; | ||
4310 | 357 | } | ||
4311 | 358 | |||
4312 | 359 | void CSetupInfo::SetAutoUpdateIP(int _nAutoUpdateIP) | ||
4313 | 360 | { | ||
4314 | 361 | m_nAutoUpdateIP = _nAutoUpdateIP; | ||
4315 | 362 | } | ||
4316 | 363 | |||
4317 | 364 | int CSetupInfo::GetTestPort() | ||
4318 | 365 | { | ||
4319 | 366 | return m_nTestPort; | ||
4320 | 367 | } | ||
4321 | 368 | |||
4322 | 369 | void CSetupInfo::SetTestPort(int _nTestPort) | ||
4323 | 370 | { | ||
4324 | 371 | m_nTestPort = _nTestPort; | ||
4325 | 372 | } | ||
4326 | 373 | |||
4327 | 374 | int CSetupInfo::GetStartOnStartUp() | ||
4328 | 375 | { | ||
4329 | 376 | return m_nStartOnStartUp; | ||
4330 | 377 | } | ||
4331 | 378 | |||
4332 | 379 | void CSetupInfo::SetStartOnStartUp(int _nStartOnStartUp) | ||
4333 | 380 | { | ||
4334 | 381 | m_nStartOnStartUp = _nStartOnStartUp; | ||
4335 | 382 | } | ||
4336 | 383 | |||
4337 | 384 | char* CSetupInfo::GetName() | ||
4338 | 385 | { | ||
4339 | 386 | return m_sName; | ||
4340 | 387 | } | ||
4341 | 388 | |||
4342 | 389 | void CSetupInfo::SetName(const char* _sName) | ||
4343 | 390 | { | ||
4344 | 391 | delete m_sName; | ||
4345 | 392 | m_sName = strdup(_sName); | ||
4346 | 393 | } | ||
4347 | 394 | |||
4348 | 395 | int CSetupInfo::GetCheckForUpdatesOnStartUp() | ||
4349 | 396 | { | ||
4350 | 397 | return m_nCheckForUpdatesOnStartUp; | ||
4351 | 398 | } | ||
4352 | 399 | |||
4353 | 400 | void CSetupInfo::SetCheckForUpdatesOnStartUp(int _nCheckForUpdatesOnStartUp) | ||
4354 | 401 | { | ||
4355 | 402 | m_nCheckForUpdatesOnStartUp = _nCheckForUpdatesOnStartUp; | ||
4356 | 403 | } | ||
4357 | 404 | |||
4358 | 405 | void CSetupInfo::SetCommon(const char* _Common) | ||
4359 | 406 | { | ||
4360 | 407 | delete m_Common; | ||
4361 | 408 | m_Common = strdup(_Common); | ||
4362 | 409 | } | ||
4363 | 410 | |||
4364 | 411 | void CSetupInfo::SetUnit(const char* _Unit) | ||
4365 | 412 | { | ||
4366 | 413 | delete m_Unit; | ||
4367 | 414 | m_Unit = strdup(_Unit); | ||
4368 | 415 | } | ||
4369 | 416 | |||
4370 | 417 | void CSetupInfo::SetOrganization(const char* _Organization) | ||
4371 | 418 | { | ||
4372 | 419 | delete m_Organization; | ||
4373 | 420 | m_Organization = strdup(_Organization); | ||
4374 | 421 | } | ||
4375 | 422 | |||
4376 | 423 | void CSetupInfo::SetEmail(const char* _Email) | ||
4377 | 424 | { | ||
4378 | 425 | delete m_Email; | ||
4379 | 426 | m_Email = strdup(_Email); | ||
4380 | 427 | } | ||
4381 | 428 | |||
4382 | 429 | void CSetupInfo::SetCountry(const char* _Country) | ||
4383 | 430 | { | ||
4384 | 431 | delete m_Country; | ||
4385 | 432 | m_Country = strdup(_Country); | ||
4386 | 433 | } | ||
4387 | 434 | |||
4388 | 435 | void CSetupInfo::SetState(const char* _State) | ||
4389 | 436 | { | ||
4390 | 437 | delete m_State; | ||
4391 | 438 | m_State = strdup(_State); | ||
4392 | 439 | } | ||
4393 | 440 | |||
4394 | 441 | void CSetupInfo::SetRegenrateOnStartUp(int _nRegenrateOnStartUp) | ||
4395 | 442 | { | ||
4396 | 443 | m_nRegenrateOnStartUp = _nRegenrateOnStartUp; | ||
4397 | 444 | } | ||
4398 | 445 | |||
4399 | 446 | char* CSetupInfo::GetCommon() | ||
4400 | 447 | { | ||
4401 | 448 | return m_Common; | ||
4402 | 449 | } | ||
4403 | 450 | |||
4404 | 451 | char* CSetupInfo::GetUnit() | ||
4405 | 452 | { | ||
4406 | 453 | return m_Unit; | ||
4407 | 454 | } | ||
4408 | 455 | |||
4409 | 456 | char* CSetupInfo::GetOrganization() | ||
4410 | 457 | { | ||
4411 | 458 | return m_Organization; | ||
4412 | 459 | } | ||
4413 | 460 | |||
4414 | 461 | char* CSetupInfo::GetEmail() | ||
4415 | 462 | { | ||
4416 | 463 | return m_Email; | ||
4417 | 464 | } | ||
4418 | 465 | |||
4419 | 466 | char* CSetupInfo::GetCountry() | ||
4420 | 467 | { | ||
4421 | 468 | return m_Country; | ||
4422 | 469 | } | ||
4423 | 470 | |||
4424 | 471 | char* CSetupInfo::GetState() | ||
4425 | 472 | { | ||
4426 | 473 | return m_State; | ||
4427 | 474 | } | ||
4428 | 475 | |||
4429 | 476 | int CSetupInfo::GetRegenrateOnStartUp() | ||
4430 | 477 | { | ||
4431 | 478 | return m_nRegenrateOnStartUp; | ||
4432 | 479 | } | ||
4433 | 480 | |||
4434 | 481 | int CSetupInfo::GetLogLevel() | ||
4435 | 482 | { | ||
4436 | 483 | return m_nLogLevel; | ||
4437 | 484 | } | ||
4438 | 485 | |||
4439 | 486 | void CSetupInfo::SetLogLevel(int _nLogLevel) | ||
4440 | 487 | { | ||
4441 | 488 | m_nLogLevel = _nLogLevel; | ||
4442 | 489 | } | ||
4443 | 490 | |||
4444 | 491 | char* CSetupInfo::GetIPServer() | ||
4445 | 492 | { | ||
4446 | 493 | return m_sIPServer; | ||
4447 | 494 | } | ||
4448 | 495 | |||
4449 | 496 | void CSetupInfo::SetIPServer(const char* _sIPServer) | ||
4450 | 497 | { | ||
4451 | 498 | delete m_sIPServer; | ||
4452 | 499 | m_sIPServer = strdup(_sIPServer); | ||
4453 | 500 | } | ||
4454 | 501 | |||
4455 | 502 | char* CSetupInfo::GetFinger() | ||
4456 | 503 | { | ||
4457 | 504 | return m_sFinger; | ||
4458 | 505 | } | ||
4459 | 506 | |||
4460 | 507 | void CSetupInfo::SetFinger(const char* _sFinger) | ||
4461 | 508 | { | ||
4462 | 509 | delete m_sFinger; | ||
4463 | 510 | m_sFinger = strdup(_sFinger); | ||
4464 | 511 | } | ||
4465 | 512 | |||
4466 | 0 | 513 | ||
4467 | === added file 'common/SetupInfo.h' | |||
4468 | --- common/SetupInfo.h 1970-01-01 00:00:00 +0000 | |||
4469 | +++ common/SetupInfo.h 2011-11-23 01:01:25 +0000 | |||
4470 | @@ -0,0 +1,110 @@ | |||
4471 | 1 | //////////////////////////////////////////////////////////////////////////////// | ||
4472 | 2 | // Copyright (c) CitizenLab, 2006. All Rights Reserved. | ||
4473 | 3 | // The latest version of this code is available at http://psiphon.civisec.org/ | ||
4474 | 4 | // | ||
4475 | 5 | // This software is open source; you can redistribute it and/or modify it | ||
4476 | 6 | // under the terms of the GNU General Public License as published by the | ||
4477 | 7 | // Free Software Foundation; either version 2 of the License, or (at your | ||
4478 | 8 | // option) any later version. | ||
4479 | 9 | // | ||
4480 | 10 | // This program is distributed WITHOUT ANY WARRANTY; without even the | ||
4481 | 11 | // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
4482 | 12 | // See the GNU General Public License for more details at: | ||
4483 | 13 | // http://psiphon.civisec.org/ | ||
4484 | 14 | // | ||
4485 | 15 | // This General Public License does NOT permit incorporating this software | ||
4486 | 16 | // into proprietary programs. | ||
4487 | 17 | //////////////////////////////////////////////////////////////////////////////// | ||
4488 | 18 | |||
4489 | 19 | #pragma once | ||
4490 | 20 | |||
4491 | 21 | class CSetupInfo | ||
4492 | 22 | { | ||
4493 | 23 | char* m_sIP; | ||
4494 | 24 | int m_nPort; | ||
4495 | 25 | int m_nShowImages; | ||
4496 | 26 | int m_nAutoUpdateIP; | ||
4497 | 27 | int m_nTestPort; | ||
4498 | 28 | int m_nStartOnStartUp; | ||
4499 | 29 | char* m_sName; | ||
4500 | 30 | int m_nCheckForUpdatesOnStartUp; | ||
4501 | 31 | |||
4502 | 32 | char* m_Common; | ||
4503 | 33 | char* m_Unit; | ||
4504 | 34 | char* m_Organization; | ||
4505 | 35 | char* m_Email; | ||
4506 | 36 | char* m_Country; | ||
4507 | 37 | char* m_State; | ||
4508 | 38 | int m_nRegenrateOnStartUp; | ||
4509 | 39 | int m_nLogLevel; | ||
4510 | 40 | char* m_sIPServer; | ||
4511 | 41 | char* m_sFinger; | ||
4512 | 42 | |||
4513 | 43 | public: | ||
4514 | 44 | CSetupInfo(); | ||
4515 | 45 | |||
4516 | 46 | CSetupInfo(const char* sIn); | ||
4517 | 47 | ~CSetupInfo(); | ||
4518 | 48 | |||
4519 | 49 | public: | ||
4520 | 50 | char* GetIP(); | ||
4521 | 51 | void SetIP(const char* _sIP); | ||
4522 | 52 | |||
4523 | 53 | int GetShowImages(); | ||
4524 | 54 | void SetShowImages(int _nShowImages); | ||
4525 | 55 | |||
4526 | 56 | int GetPort(); | ||
4527 | 57 | void SetPort(int _nPort); | ||
4528 | 58 | |||
4529 | 59 | int GetAutoUpdateIP(); | ||
4530 | 60 | void SetAutoUpdateIP(int _nAutoUpdateIP); | ||
4531 | 61 | |||
4532 | 62 | int GetTestPort(); | ||
4533 | 63 | void SetTestPort(int _nTestPort); | ||
4534 | 64 | |||
4535 | 65 | int GetStartOnStartUp(); | ||
4536 | 66 | void SetStartOnStartUp(int _nStartOnStartUp); | ||
4537 | 67 | |||
4538 | 68 | char* GetName(); | ||
4539 | 69 | void SetName(const char* _sName); | ||
4540 | 70 | |||
4541 | 71 | int GetCheckForUpdatesOnStartUp(); | ||
4542 | 72 | void SetCheckForUpdatesOnStartUp(int _nCheckForUpdatesOnStartUp); | ||
4543 | 73 | |||
4544 | 74 | bool Store(); | ||
4545 | 75 | void Load(); | ||
4546 | 76 | |||
4547 | 77 | void SetCommon(const char* _Common); | ||
4548 | 78 | void SetUnit(const char* _Unit); | ||
4549 | 79 | void SetOrganization(const char* _Organization); | ||
4550 | 80 | void SetEmail(const char* _Email); | ||
4551 | 81 | void SetCountry(const char* _Country); | ||
4552 | 82 | void SetState(const char* _State); | ||
4553 | 83 | void SetRegenrateOnStartUp(int _nRegenrateOnStartUp); | ||
4554 | 84 | |||
4555 | 85 | char* GetCommon(); | ||
4556 | 86 | char* GetUnit(); | ||
4557 | 87 | char* GetOrganization(); | ||
4558 | 88 | char* GetEmail(); | ||
4559 | 89 | char* GetCountry(); | ||
4560 | 90 | char* GetState(); | ||
4561 | 91 | int GetRegenrateOnStartUp(); | ||
4562 | 92 | |||
4563 | 93 | int GetLogLevel(); | ||
4564 | 94 | void SetLogLevel(int _nLogLevel); | ||
4565 | 95 | |||
4566 | 96 | char* GetIPServer(); | ||
4567 | 97 | void SetIPServer(const char* pIPServer); | ||
4568 | 98 | |||
4569 | 99 | char* GetFinger(); | ||
4570 | 100 | void SetFinger(const char* pFinger); | ||
4571 | 101 | |||
4572 | 102 | protected: | ||
4573 | 103 | void Init(); | ||
4574 | 104 | void Out(char*& sOut); | ||
4575 | 105 | |||
4576 | 106 | void Parse(const char* sIn); | ||
4577 | 107 | }; | ||
4578 | 108 | |||
4579 | 109 | extern CSetupInfo g_info; | ||
4580 | 110 | |||
4581 | 0 | 111 | ||
4582 | === added file 'common/User.cpp' | |||
4583 | --- common/User.cpp 1970-01-01 00:00:00 +0000 | |||
4584 | +++ common/User.cpp 2011-11-23 01:01:25 +0000 | |||
4585 | @@ -0,0 +1,598 @@ | |||
4586 | 1 | //////////////////////////////////////////////////////////////////////////////// | ||
4587 | 2 | // Copyright (c) CitizenLab, 2006. All Rights Reserved. | ||
4588 | 3 | // The latest version of this code is available at http://psiphon.civisec.org/ | ||
4589 | 4 | // | ||
4590 | 5 | // This software is open source; you can redistribute it and/or modify it | ||
4591 | 6 | // under the terms of the GNU General Public License as published by the | ||
4592 | 7 | // Free Software Foundation; either version 2 of the License, or (at your | ||
4593 | 8 | // option) any later version. | ||
4594 | 9 | // | ||
4595 | 10 | // This program is distributed WITHOUT ANY WARRANTY; without even the | ||
4596 | 11 | // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
4597 | 12 | // See the GNU General Public License for more details at: | ||
4598 | 13 | // http://psiphon.civisec.org/ | ||
4599 | 14 | // | ||
4600 | 15 | // This General Public License does NOT permit incorporating this software | ||
4601 | 16 | // into proprietary programs. | ||
4602 | 17 | //////////////////////////////////////////////////////////////////////////////// | ||
4603 | 18 | |||
4604 | 19 | #include "convert.h" | ||
4605 | 20 | #include "CppSQLite3.h" | ||
4606 | 21 | #include "User.h" | ||
4607 | 22 | #include "SHA1.h" | ||
4608 | 23 | #include "strfunc.h" | ||
4609 | 24 | |||
4610 | 25 | #define _T(X) X | ||
4611 | 26 | |||
4612 | 27 | void escape(char* pDest, int nSize, const char* pSrc); | ||
4613 | 28 | |||
4614 | 29 | char* CDBUser::m_sDB = _T("users.db"); | ||
4615 | 30 | |||
4616 | 31 | LOG_ROUTINE CDBUser::m_pLogFunc = NULL; | ||
4617 | 32 | |||
4618 | 33 | CDBUser::CDBUser() | ||
4619 | 34 | { | ||
4620 | 35 | Init(); | ||
4621 | 36 | } | ||
4622 | 37 | |||
4623 | 38 | CDBUser::~CDBUser() | ||
4624 | 39 | { | ||
4625 | 40 | Cleanup(); | ||
4626 | 41 | } | ||
4627 | 42 | |||
4628 | 43 | void CDBUser::Init() | ||
4629 | 44 | { | ||
4630 | 45 | m_sUserName = NULL; | ||
4631 | 46 | m_sPassword = NULL; | ||
4632 | 47 | m_sEmail = NULL; | ||
4633 | 48 | m_sFullName = NULL; | ||
4634 | 49 | |||
4635 | 50 | m_nID = 0; | ||
4636 | 51 | m_nDeleted = false; | ||
4637 | 52 | |||
4638 | 53 | m_nLastAccess = 0; | ||
4639 | 54 | m_nWritten = 0; | ||
4640 | 55 | m_nRead = 0; | ||
4641 | 56 | m_nWriteTime = 0; | ||
4642 | 57 | m_nReadTime = 0; | ||
4643 | 58 | } | ||
4644 | 59 | |||
4645 | 60 | void CDBUser::Cleanup() | ||
4646 | 61 | { | ||
4647 | 62 | delete m_sUserName; | ||
4648 | 63 | delete m_sPassword; | ||
4649 | 64 | delete m_sEmail; | ||
4650 | 65 | delete m_sFullName; | ||
4651 | 66 | |||
4652 | 67 | Init(); | ||
4653 | 68 | } | ||
4654 | 69 | |||
4655 | 70 | int CDBUser::Get(int nUserID) | ||
4656 | 71 | { | ||
4657 | 72 | if(nUserID <= 0) | ||
4658 | 73 | return -1; | ||
4659 | 74 | |||
4660 | 75 | try | ||
4661 | 76 | { | ||
4662 | 77 | CppSQLite3DB db; | ||
4663 | 78 | db.Open(m_sDB); | ||
4664 | 79 | |||
4665 | 80 | if(!db.tableExists(_T("users"))) | ||
4666 | 81 | return false; | ||
4667 | 82 | |||
4668 | 83 | CppSQLite3Statement stmt = db.compileStatement(_T("select nIndex, sUsername, sPassword, sEmail, sFullname from users where nIndex = ?;")); | ||
4669 | 84 | |||
4670 | 85 | stmt.bind(1, nUserID); | ||
4671 | 86 | |||
4672 | 87 | CppSQLite3Query q = stmt.execQuery(); | ||
4673 | 88 | |||
4674 | 89 | if(!q.eof()) | ||
4675 | 90 | { | ||
4676 | 91 | SetID(q.getIntField(0)); | ||
4677 | 92 | |||
4678 | 93 | CConverter wUsername(q.fieldValue(1)); | ||
4679 | 94 | SetUsername(wUsername); | ||
4680 | 95 | |||
4681 | 96 | CConverter wPassword(q.fieldValue(2)); | ||
4682 | 97 | SetPassword(wPassword); | ||
4683 | 98 | |||
4684 | 99 | CConverter wEmail(q.fieldValue(3)); | ||
4685 | 100 | SetEmail(wEmail); | ||
4686 | 101 | |||
4687 | 102 | CConverter wFullName(q.fieldValue(4)); | ||
4688 | 103 | SetFullName(wFullName); | ||
4689 | 104 | |||
4690 | 105 | q.nextRow(); | ||
4691 | 106 | |||
4692 | 107 | return 0; | ||
4693 | 108 | } | ||
4694 | 109 | } | ||
4695 | 110 | catch(CppSQLite3Exception& e) | ||
4696 | 111 | { | ||
4697 | 112 | CConverter wMsg(e.errorMessage()); | ||
4698 | 113 | CDBUser::Log(wMsg); | ||
4699 | 114 | } | ||
4700 | 115 | |||
4701 | 116 | return -1; | ||
4702 | 117 | } | ||
4703 | 118 | |||
4704 | 119 | int CDBUser::FindUser(const char* pUserName, const char* pPassword) | ||
4705 | 120 | { | ||
4706 | 121 | if(!pUserName || strlen(pUserName) > 100) | ||
4707 | 122 | return -1; | ||
4708 | 123 | |||
4709 | 124 | if(!pPassword || strlen(pPassword) > 64) | ||
4710 | 125 | return -1; | ||
4711 | 126 | |||
4712 | 127 | try | ||
4713 | 128 | { | ||
4714 | 129 | CppSQLite3DB db; | ||
4715 | 130 | db.Open(m_sDB); | ||
4716 | 131 | |||
4717 | 132 | if(!db.tableExists(_T("users"))) | ||
4718 | 133 | return -1; | ||
4719 | 134 | |||
4720 | 135 | CppSQLite3Statement stmt = db.compileStatement(_T("select nIndex from users where sUsername=? and sPassword=? and nDeleted != 1;")); | ||
4721 | 136 | |||
4722 | 137 | stmt.bind(1, pUserName); | ||
4723 | 138 | stmt.bind(2, pPassword); | ||
4724 | 139 | |||
4725 | 140 | CppSQLite3Query q = stmt.execQuery(); | ||
4726 | 141 | |||
4727 | 142 | if(!q.eof()) | ||
4728 | 143 | { | ||
4729 | 144 | return (q.getIntField(0)); | ||
4730 | 145 | } | ||
4731 | 146 | } | ||
4732 | 147 | catch(CppSQLite3Exception& e) | ||
4733 | 148 | { | ||
4734 | 149 | CConverter wMsg(e.errorMessage()); | ||
4735 | 150 | CDBUser::Log(wMsg); | ||
4736 | 151 | } | ||
4737 | 152 | |||
4738 | 153 | return -1; | ||
4739 | 154 | } | ||
4740 | 155 | |||
4741 | 156 | int CDBUser::FindUsername(const char* pUserName) | ||
4742 | 157 | { | ||
4743 | 158 | if(!pUserName || strlen(pUserName) > 100) | ||
4744 | 159 | return -1; | ||
4745 | 160 | |||
4746 | 161 | try | ||
4747 | 162 | { | ||
4748 | 163 | CppSQLite3DB db; | ||
4749 | 164 | db.Open(m_sDB); | ||
4750 | 165 | |||
4751 | 166 | if(!db.tableExists(_T("users"))) | ||
4752 | 167 | return -1; | ||
4753 | 168 | |||
4754 | 169 | |||
4755 | 170 | CppSQLite3Statement stmt = db.compileStatement(_T("select nIndex from users where sUsername=?;")); | ||
4756 | 171 | |||
4757 | 172 | stmt.bind(1, pUserName); | ||
4758 | 173 | |||
4759 | 174 | CppSQLite3Query q = stmt.execQuery(); | ||
4760 | 175 | |||
4761 | 176 | if(!q.eof()) | ||
4762 | 177 | { | ||
4763 | 178 | return (q.getIntField(0)); | ||
4764 | 179 | } | ||
4765 | 180 | } | ||
4766 | 181 | catch(CppSQLite3Exception& e) | ||
4767 | 182 | { | ||
4768 | 183 | CConverter wMsg(e.errorMessage()); | ||
4769 | 184 | CDBUser::Log(wMsg); | ||
4770 | 185 | } | ||
4771 | 186 | |||
4772 | 187 | return -1; | ||
4773 | 188 | } | ||
4774 | 189 | |||
4775 | 190 | int CDBUser::Insert() | ||
4776 | 191 | { | ||
4777 | 192 | if(!m_sUserName || strlen(m_sUserName) > 100) | ||
4778 | 193 | return -1; | ||
4779 | 194 | |||
4780 | 195 | if(!m_sPassword || strlen(m_sPassword) > 64) | ||
4781 | 196 | return -1; | ||
4782 | 197 | |||
4783 | 198 | if(!m_sEmail || strlen(m_sEmail) > 100) | ||
4784 | 199 | return -1; | ||
4785 | 200 | |||
4786 | 201 | if(!m_sFullName || strlen(m_sFullName) > 100) | ||
4787 | 202 | return -1; | ||
4788 | 203 | try | ||
4789 | 204 | { | ||
4790 | 205 | CppSQLite3DB db; | ||
4791 | 206 | db.Open(m_sDB); | ||
4792 | 207 | |||
4793 | 208 | if(!db.tableExists(_T("users"))) | ||
4794 | 209 | { | ||
4795 | 210 | db.execDML(_T("create table users(nIndex integer primary key, sUsername char(100), sPassword char(64), sEmail char(100), sFullname char(100), nDeleted int);")); | ||
4796 | 211 | } | ||
4797 | 212 | |||
4798 | 213 | CppSQLite3Statement stmt = db.compileStatement(_T("insert into users (sUsername, sPassword, sEmail, sFullname, nDeleted)" | ||
4799 | 214 | " values (?, ?, ?, ?, ?);")); | ||
4800 | 215 | |||
4801 | 216 | stmt.bind(1, m_sUserName); | ||
4802 | 217 | stmt.bind(2, m_sPassword); | ||
4803 | 218 | stmt.bind(3, m_sEmail); | ||
4804 | 219 | stmt.bind(4, m_sFullName); | ||
4805 | 220 | stmt.bind(5, m_nDeleted); | ||
4806 | 221 | |||
4807 | 222 | |||
4808 | 223 | int nRows = stmt.execDML(); | ||
4809 | 224 | |||
4810 | 225 | m_nID = (int)db.lastRowId(); | ||
4811 | 226 | |||
4812 | 227 | return nRows; | ||
4813 | 228 | } | ||
4814 | 229 | catch(CppSQLite3Exception& e) | ||
4815 | 230 | { | ||
4816 | 231 | CConverter wMsg(e.errorMessage()); | ||
4817 | 232 | CDBUser::Log(wMsg); | ||
4818 | 233 | } | ||
4819 | 234 | |||
4820 | 235 | return -1; | ||
4821 | 236 | } | ||
4822 | 237 | |||
4823 | 238 | int CDBUser::Update() | ||
4824 | 239 | { | ||
4825 | 240 | if(m_nID <= 0) | ||
4826 | 241 | return -1; | ||
4827 | 242 | |||
4828 | 243 | if(!m_sPassword || strlen(m_sPassword) > 64) | ||
4829 | 244 | return -1; | ||
4830 | 245 | |||
4831 | 246 | if(!m_sEmail || strlen(m_sEmail) > 100) | ||
4832 | 247 | return -1; | ||
4833 | 248 | |||
4834 | 249 | if(!m_sFullName || strlen(m_sFullName) > 100) | ||
4835 | 250 | return -1; | ||
4836 | 251 | |||
4837 | 252 | try | ||
4838 | 253 | { | ||
4839 | 254 | CppSQLite3DB db; | ||
4840 | 255 | db.Open(m_sDB); | ||
4841 | 256 | |||
4842 | 257 | if(!db.tableExists("users")) | ||
4843 | 258 | return -1; | ||
4844 | 259 | |||
4845 | 260 | CppSQLite3Statement stmt; | ||
4846 | 261 | |||
4847 | 262 | if(strlen(m_sPassword) > 0) | ||
4848 | 263 | { | ||
4849 | 264 | stmt = db.compileStatement(_T("update users set sPassword=?, sEmail=?, sFullname=? where nIndex=?;")); | ||
4850 | 265 | stmt.bind(1, m_sPassword); | ||
4851 | 266 | stmt.bind(2, m_sEmail); | ||
4852 | 267 | stmt.bind(3, m_sFullName); | ||
4853 | 268 | stmt.bind(4, m_nID); | ||
4854 | 269 | } | ||
4855 | 270 | else | ||
4856 | 271 | { | ||
4857 | 272 | stmt = db.compileStatement(_T("update users set sEmail=?, sFullname=? where nIndex=?;")); | ||
4858 | 273 | stmt.bind(1, m_sEmail); | ||
4859 | 274 | stmt.bind(2, m_sFullName); | ||
4860 | 275 | stmt.bind(3, m_nID); | ||
4861 | 276 | } | ||
4862 | 277 | |||
4863 | 278 | int nRows = stmt.execDML(); | ||
4864 | 279 | return nRows; | ||
4865 | 280 | } | ||
4866 | 281 | catch(CppSQLite3Exception& e) | ||
4867 | 282 | { | ||
4868 | 283 | CConverter wMsg(e.errorMessage()); | ||
4869 | 284 | CDBUser::Log(wMsg); | ||
4870 | 285 | } | ||
4871 | 286 | |||
4872 | 287 | return -1; | ||
4873 | 288 | } | ||
4874 | 289 | |||
4875 | 290 | void CDBUser::EncryptPassword(char* pKey, int nKeySize, const char* _pUsername, const char* _pPassword) | ||
4876 | 291 | { | ||
4877 | 292 | if(!pKey) | ||
4878 | 293 | return; | ||
4879 | 294 | |||
4880 | 295 | if(nKeySize == 0) | ||
4881 | 296 | return; | ||
4882 | 297 | |||
4883 | 298 | if(!_pUsername || strlen(_pUsername) > 100) | ||
4884 | 299 | return; | ||
4885 | 300 | if(!_pPassword || strlen(_pPassword) > 64) | ||
4886 | 301 | return; | ||
4887 | 302 | |||
4888 | 303 | CSHA1 sha1; | ||
4889 | 304 | |||
4890 | 305 | CConverter aUsername(_pUsername); | ||
4891 | 306 | CConverter aPassword(_pPassword); | ||
4892 | 307 | |||
4893 | 308 | sha1.Update((UINT_8*)(char*)(const char*)aUsername, strlen(aUsername)); | ||
4894 | 309 | sha1.Update((UINT_8*)(char*)(const char*)aPassword, strlen(aPassword)); | ||
4895 | 310 | sha1.Final(); | ||
4896 | 311 | |||
4897 | 312 | char* aKey = new char[nKeySize]; | ||
4898 | 313 | memset(aKey, 0, nKeySize * sizeof(char)); | ||
4899 | 314 | sha1.ReportHash(aKey, CSHA1::REPORT_HEX); | ||
4900 | 315 | |||
4901 | 316 | CConverter _pKey(aKey); | ||
4902 | 317 | strncpy(pKey, _pKey, nKeySize); | ||
4903 | 318 | |||
4904 | 319 | delete aKey; | ||
4905 | 320 | } | ||
4906 | 321 | |||
4907 | 322 | int CDBUser::Delete(int nUserID) | ||
4908 | 323 | { | ||
4909 | 324 | if(nUserID <= 0) | ||
4910 | 325 | return -1; | ||
4911 | 326 | |||
4912 | 327 | try | ||
4913 | 328 | { | ||
4914 | 329 | CppSQLite3DB db; | ||
4915 | 330 | db.Open(m_sDB); | ||
4916 | 331 | |||
4917 | 332 | if(!db.tableExists(_T("users"))) | ||
4918 | 333 | return false; | ||
4919 | 334 | |||
4920 | 335 | CppSQLite3Statement stmt = db.compileStatement(_T("update users set nDeleted=1 where nIndex=?;")); | ||
4921 | 336 | stmt.bind(1, nUserID); | ||
4922 | 337 | stmt.execDML(); | ||
4923 | 338 | |||
4924 | 339 | return 0; | ||
4925 | 340 | } | ||
4926 | 341 | catch(CppSQLite3Exception& e) | ||
4927 | 342 | { | ||
4928 | 343 | CConverter wMsg(e.errorMessage()); | ||
4929 | 344 | CDBUser::Log(wMsg); | ||
4930 | 345 | } | ||
4931 | 346 | |||
4932 | 347 | return -1; | ||
4933 | 348 | } | ||
4934 | 349 | |||
4935 | 350 | int CDBUser::GetUserCount() | ||
4936 | 351 | { | ||
4937 | 352 | try | ||
4938 | 353 | { | ||
4939 | 354 | CppSQLite3DB db; | ||
4940 | 355 | db.Open(m_sDB); | ||
4941 | 356 | |||
4942 | 357 | if(!db.tableExists(_T("users"))) | ||
4943 | 358 | return 0; | ||
4944 | 359 | |||
4945 | 360 | int nRet = db.execScalar(_T("select count(*) from users where nDeleted = 0")); | ||
4946 | 361 | |||
4947 | 362 | return nRet; | ||
4948 | 363 | } | ||
4949 | 364 | catch(CppSQLite3Exception& e) | ||
4950 | 365 | { | ||
4951 | 366 | CConverter wMsg(e.errorMessage()); | ||
4952 | 367 | CDBUser::Log(wMsg); | ||
4953 | 368 | } | ||
4954 | 369 | |||
4955 | 370 | return -1; | ||
4956 | 371 | } | ||
4957 | 372 | |||
4958 | 373 | int CDBUser::LoadUsers(CDBUser**& pUsers) | ||
4959 | 374 | { | ||
4960 | 375 | int nUserCount = GetUserCount(); | ||
4961 | 376 | if(nUserCount <= 0) | ||
4962 | 377 | return 0; | ||
4963 | 378 | |||
4964 | 379 | pUsers = new CDBUser * [nUserCount]; | ||
4965 | 380 | |||
4966 | 381 | try | ||
4967 | 382 | { | ||
4968 | 383 | CppSQLite3DB db; | ||
4969 | 384 | db.Open(m_sDB); | ||
4970 | 385 | |||
4971 | 386 | if(!db.tableExists(_T("users"))) | ||
4972 | 387 | return 0; | ||
4973 | 388 | |||
4974 | 389 | CppSQLite3Query q = db.execQuery(_T("select * from users where nDeleted = 0;")); | ||
4975 | 390 | |||
4976 | 391 | int i = 0; | ||
4977 | 392 | |||
4978 | 393 | while(!q.eof()) | ||
4979 | 394 | { | ||
4980 | 395 | CDBUser* pUser = new CDBUser; | ||
4981 | 396 | |||
4982 | 397 | pUser->SetID(q.getIntField(0)); | ||
4983 | 398 | |||
4984 | 399 | CConverter wUsername(q.fieldValue(1)); | ||
4985 | 400 | pUser->SetUsername(wUsername); | ||
4986 | 401 | |||
4987 | 402 | CConverter wPassword(q.fieldValue(2)); | ||
4988 | 403 | pUser->SetPassword(wPassword); | ||
4989 | 404 | |||
4990 | 405 | CConverter wEmail(q.fieldValue(3)); | ||
4991 | 406 | pUser->SetEmail(wEmail); | ||
4992 | 407 | |||
4993 | 408 | CConverter wFullName(q.fieldValue(4)); | ||
4994 | 409 | pUser->SetFullName(wFullName); | ||
4995 | 410 | |||
4996 | 411 | pUsers[i++] = pUser; | ||
4997 | 412 | |||
4998 | 413 | q.nextRow(); | ||
4999 | 414 | } | ||
5000 | 415 |
The diff has been truncated for viewing.