1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/*
Miranda IM: the free IM client for Microsoft* Windows*
Copyright 2000-2003 Miranda ICQ/IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// Miranda Memory-Mapped Secured DataBase
// (C) Artem Shpynov aka FYR, Igonin Vitaliy aka chaos.persei, Victor Pavlychko aka nullbie, 2007 - 2008
#include "commonheaders.h"
/* Public API */
int DBPreset_QuerySetting (const char *szModule, const char *szSetting, DBVARIANT *dbv, BOOL isStatic);
int DBPreset_CompareSetting (const char *szModule, const char *szSetting, DBVARIANT *dbv);
/* Preset cache item */
typedef struct
{
DWORD dwHash;
char *szModule;
char *szSetting;
DBVARIANT dbv;
} DBPresetItem;
static DBPresetItem * DBPresetItem_Create (char *szModule, char *szSetting, BYTE bType);
static void DBPresetItem_Destroy (DBPresetItem *item);
static void DBPresetItem_Hash (DBPresetItem *item);
static int DBPresetItem_Cmp (DBPresetItem *item1, DBPresetItem *item2);
SortedList *lstPresets = NULL;
int InitPreset()
{
char szIniPath[MAX_PATH];
char szLine[2048];
int lineLength;
char szSection[128];
FILE *fp;
GetModuleFileNameA(GetModuleHandle(NULL), szIniPath, SIZEOF(szIniPath));
strcpy(strrchr(szIniPath, '\\')+1, "dbpreset.ini");
fp = fopen(szIniPath,"rt");
// no preset
if (!fp) return 0;
lstPresets = List_Create(0, 50);
lstPresets->sortFunc = (FSortFunc)DBPresetItem_Cmp;
while(!feof(fp))
{
if (fgets(szLine,sizeof(szLine),fp) == NULL) break;
lineLength = lstrlenA(szLine);
while(lineLength && (BYTE)(szLine[lineLength-1]) <= ' ') szLine[--lineLength] = '\0';
if (szLine[0] == ';' || szLine[0] <= ' ') continue;
if (szLine[0] == '[')
{
char *szEnd = strchr(szLine+1,']');
if (szEnd == NULL) continue;
if (szLine[1] == '!')
szSection[0] = '\0';
else
lstrcpynA(szSection, szLine+1, (int)min(sizeof(szSection), szEnd-szLine));
} else
{
char *szValue;
char szName[128];
DBPresetItem *item;
if (szSection[0] == '\0') continue;
szValue = strchr(szLine,' = ');
if (szValue == NULL) continue;
lstrcpynA(szName, szLine, (int)min(sizeof(szName), szValue-szLine+1));
szValue++;
switch(szValue[0])
{
case 'b':
case 'B':
item = DBPresetItem_Create(szSection, szName, DBVT_BYTE);
item->dbv.bVal = (BYTE)strtol(szValue+1,NULL,0);
List_InsertPtr(lstPresets, item);
break;
case 'w':
case 'W':
item = DBPresetItem_Create(szSection, szName, DBVT_WORD);
item->dbv.wVal = (WORD)strtol(szValue+1,NULL,0);
List_InsertPtr(lstPresets, item);
break;
case 'd':
case 'D':
item = DBPresetItem_Create(szSection, szName, DBVT_DWORD);
item->dbv.dVal = (DWORD)strtoul(szValue+1,NULL,0);
List_InsertPtr(lstPresets, item);
break;
case 's':
case 'S':
item = DBPresetItem_Create(szSection, szName, DBVT_ASCIIZ);
item->dbv.pszVal = mir_strdup(szValue+1);
List_InsertPtr(lstPresets, item);
break;
case 'u':
case 'U':
item = DBPresetItem_Create(szSection, szName, DBVT_UTF8);
item->dbv.pszVal = mir_strdup(szValue+1);
List_InsertPtr(lstPresets, item);
break;
case 'n':
case 'N':
{
PBYTE buf;
int len;
char *pszValue,*pszEnd;
buf = (PBYTE)mir_alloc(lstrlenA(szValue+1));
for (len = 0,pszValue = szValue+1;;len++) {
buf[len] = (BYTE)strtol(pszValue,&pszEnd,0x10);
if (pszValue == pszEnd) break;
pszValue = pszEnd;
}
item = DBPresetItem_Create(szSection, szName, DBVT_BLOB);
item->dbv.pbVal = buf;
item->dbv.cpbVal = len;
List_InsertPtr(lstPresets, item);
break;
}
}
}
}
fclose(fp);
return 0;
}
void UninitPreset()
{
int i;
if (!lstPresets) return;
for (i = 0; i < lstPresets->realCount; ++i)
DBPresetItem_Destroy((DBPresetItem *)lstPresets->items[i]);
List_Destroy(lstPresets);
}
int DBPreset_QuerySetting(const char *szModule, const char *szSetting, DBVARIANT *dbv, BOOL isStatic)
{
DBPresetItem *item;
DBPresetItem search = {0};
if (!lstPresets) return FALSE;
search.szModule = (char *)szModule;
search.szSetting = (char *)szSetting;
DBPresetItem_Hash(&search);
item = (DBPresetItem *)List_Find(lstPresets, &search);
if (!item) return FALSE;
dbv->type = item->dbv.type;
switch (item->dbv.type)
{
case DBVT_BYTE: dbv->bVal = item->dbv.bVal; return TRUE;
case DBVT_WORD: dbv->wVal = item->dbv.wVal; return TRUE;
case DBVT_DWORD: dbv->dVal = item->dbv.dVal; return TRUE;
case DBVT_UTF8:
case DBVT_ASCIIZ:
if (isStatic && dbv->pszVal)
lstrcpynA(dbv->pszVal, item->dbv.pszVal, dbv->cchVal);
else if (!isStatic)
dbv->pszVal = mir_strdup(item->dbv.pszVal);
return TRUE;
default:
return FALSE;
}
return FALSE;
}
int DBPreset_CompareSetting(const char *szModule, const char *szSetting, DBVARIANT *dbv)
{
DBPresetItem *item;
DBPresetItem search = {0};
if (!lstPresets) return FALSE;
search.szModule = (char *)szModule;
search.szSetting = (char *)szSetting;
DBPresetItem_Hash(&search);
item = (DBPresetItem *)List_Find(lstPresets, &search);
if (!item) return FALSE;
if (item->dbv.type != item->dbv.type) return FALSE;
switch (item->dbv.type)
{
case DBVT_BYTE: return dbv->bVal == item->dbv.bVal ? TRUE : FALSE;
case DBVT_WORD: return dbv->wVal == item->dbv.wVal ? TRUE : FALSE;
case DBVT_DWORD: return dbv->dVal == item->dbv.dVal ? TRUE : FALSE;
case DBVT_UTF8:
case DBVT_ASCIIZ: return strcmp(dbv->pszVal, item->dbv.pszVal) ? FALSE : TRUE;
}
return FALSE;
}
static DBPresetItem *DBPresetItem_Create(char *szModule, char *szSetting, BYTE bType)
{
DBPresetItem *item = (DBPresetItem *)mir_alloc(sizeof(DBPresetItem));
item->szModule = mir_strdup(szModule);
item->szSetting = mir_strdup(szSetting);
DBPresetItem_Hash(item);
item->dbv.type = bType;
return item;
}
static void DBPresetItem_Destroy(DBPresetItem *item)
{
if (!item) return;
if (item->szModule)
{
mir_free(item->szModule);
item->szModule = NULL;
}
if (item->szSetting)
{
mir_free(item->szSetting);
item->szSetting = NULL;
}
switch (item->dbv.type)
{
case DBVT_ASCIIZ:
case DBVT_UTF8:
case DBVT_WCHAR:
{
if (item->dbv.pszVal)
mir_free(item->dbv.pszVal);
item->dbv.pszVal = 0;
break;
}
case DBVT_BLOB:
{
if (item->dbv.pbVal)
mir_free(item->dbv.pbVal);
item->dbv.pbVal = 0;
break;
}
}
item->dbv.type = 0;
}
static void DBPresetItem_Hash(DBPresetItem *item)
{
int i;
int shift = 0;
item->dwHash = 0;
for (i = 0;item->szModule[i];i++)
{
item->dwHash ^= item->szModule[i]<<shift;
if (shift>24) item->dwHash ^= (item->szModule[i]>>(32-shift))&0x7F;
shift = (shift+5)&0x1F;
}
for (i = 0;item->szSetting[i];i++)
{
item->dwHash ^= item->szSetting[i]<<shift;
if (shift>24) item->dwHash ^= (item->szSetting[i]>>(32-shift))&0x7F;
shift = (shift+5)&0x1F;
}
}
static int DBPresetItem_Cmp(DBPresetItem *item1, DBPresetItem *item2)
{
int cmp;
if (item1->dwHash < item2->dwHash) return -1;
if (item1->dwHash > item2->dwHash) return 1;
if (cmp = strcmp(item1->szModule, item2->szModule)) return cmp;
return strcmp(item1->szSetting, item2->szSetting);
}
|