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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
/*
Import plugin for Miranda NG
Copyright (C) 2012-24 Miranda NG team (https://miranda-ng.org)
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.
*/
#include "stdafx.h"
#include <memory>
#include <vector>
#define HEADER_STR "HB"
/////////////////////////////////////////////////////////////////////////////////////////
// CDbxMcontacts database driver, read-only
#pragma pack(push, 1)
struct MC_FileHeader
{
uint8_t signature[2];
uint32_t version;
uint32_t dataSize;
};
struct MC_MsgHeader32
{
uint32_t cbSize; // size of the structure in bytes
uint32_t szModule; // pointer to name of the module that 'owns' this
// event, ie the one that is in control of the data format
uint32_t timestamp; // seconds since 00:00, 01/01/1970. Gives us times until
// 2106 unless you use the standard C library which is
// signed and can only do until 2038. In GMT.
uint32_t flags; // the omnipresent flags
uint32_t eventType; // module-defined event type field
uint32_t cbBlob; // size of pBlob in bytes
uint32_t pBlob; // pointer to buffer containing module-defined event data
};
struct MC_MsgHeader64
{
uint64_t cbSize; // size of the structure in bytes
uint64_t szModule; // pointer to name of the module that 'owns' this
// event, ie the one that is in control of the data format
uint32_t timestamp; // seconds since 00:00, 01/01/1970. Gives us times until
// 2106 unless you use the standard C library which is
// signed and can only do until 2038. In GMT.
uint32_t flags; // the omnipresent flags
uint32_t eventType; // module-defined event type field
uint32_t cbBlob; // size of pBlob in bytes
uint64_t pBlob; // pointer to buffer containing module-defined event data
};
#pragma pack(pop)
class CDbxMc : public MDatabaseReadonly, public MZeroedObject
{
HANDLE m_hFile = INVALID_HANDLE_VALUE;
MC_FileHeader m_hdr;
std::vector<uint32_t> m_events;
std::vector<uint32_t>::iterator m_curr;
CMStringA readString()
{
CMStringA res;
char c;
DWORD dwRead;
while (ReadFile(m_hFile, &c, 1, &dwRead, 0)) {
if (c == 0)
break;
res.AppendChar(c);
}
return res;
}
public:
CDbxMc()
{}
~CDbxMc()
{
if (m_hFile != INVALID_HANDLE_VALUE)
::CloseHandle(m_hFile);
}
void Load()
{
// mcontacts operates with the only contact with pseudo id=1
m_cache->AddContactToCache(1);
uint32_t pos = 0;
while (pos < m_hdr.dataSize) {
DWORD dwPos = SetFilePointer(m_hFile, 0, 0, FILE_CURRENT), dwRead, dwSize;
BOOL r = ReadFile(m_hFile, &dwSize, sizeof(dwSize), &dwRead, 0);
if (!r || dwRead < sizeof(dwSize))
return;
if (dwSize != sizeof(MC_MsgHeader32) && dwSize != sizeof(MC_MsgHeader64))
return;
m_events.push_back(dwPos);
SetFilePointer(m_hFile, -4, 0, FILE_CURRENT);
if (dwSize == sizeof(MC_MsgHeader32)) {
MC_MsgHeader32 hdr;
r = ReadFile(m_hFile, &hdr, sizeof(hdr), &dwRead, 0);
if (!r || dwRead < sizeof(hdr))
return;
SetFilePointer(m_hFile, hdr.cbBlob, 0, FILE_CURRENT);
}
else {
MC_MsgHeader64 hdr;
r = ReadFile(m_hFile, &hdr, sizeof(hdr), &dwRead, 0);
if (!r || dwRead < sizeof(hdr))
return;
SetFilePointer(m_hFile, hdr.cbBlob, 0, FILE_CURRENT);
}
pos += dwSize;
}
}
int Open(const wchar_t *profile)
{
m_hFile = CreateFile(profile, GENERIC_READ, 0, 0, OPEN_ALWAYS, 0, 0);
if (m_hFile == INVALID_HANDLE_VALUE)
return EGROKPRF_CANTREAD;
DWORD dwRead;
BOOL r = ReadFile(m_hFile, &m_hdr, sizeof(m_hdr), &dwRead, nullptr);
if (!r)
return EGROKPRF_CANTREAD;
return memcmp(&m_hdr.signature, HEADER_STR, 2) ? EGROKPRF_UNKHEADER : EGROKPRF_NOERROR;
}
// mcontacts format always store history for one contact only
STDMETHODIMP_(int) GetBlobSize(MEVENT dwOffset) override
{
if (INVALID_SET_FILE_POINTER == SetFilePointer(m_hFile, dwOffset, 0, FILE_BEGIN))
return 0;
DWORD dwRead, dwSize;
BOOL r = ReadFile(m_hFile, &dwSize, sizeof(dwSize), &dwRead, nullptr);
if (!r || dwRead != sizeof(dwSize))
return 0;
SetFilePointer(m_hFile, -4, 0, FILE_CURRENT);
if (dwSize == sizeof(MC_MsgHeader32)) {
MC_MsgHeader32 hdr;
r = ReadFile(m_hFile, &hdr, sizeof(hdr), &dwRead, 0);
if (!r || dwRead != sizeof(hdr))
return 0;
return hdr.cbBlob+1;
}
if (dwSize == sizeof(MC_MsgHeader64)) {
MC_MsgHeader64 hdr;
r = ReadFile(m_hFile, &hdr, sizeof(hdr), &dwRead, 0);
if (!r || dwRead != sizeof(hdr))
return 0;
return hdr.cbBlob+1;
}
return 0;
}
STDMETHODIMP_(int) GetContactCount(void) override
{
return 1;
}
STDMETHODIMP_(int) GetEventCount(MCONTACT) override
{
return (int)m_events.size();
}
STDMETHODIMP_(BOOL) GetEvent(MEVENT dwOffset, DBEVENTINFO *dbei) override
{
if (INVALID_SET_FILE_POINTER == SetFilePointer(m_hFile, dwOffset, 0, FILE_BEGIN))
return 1;
DWORD dwRead, dwSize;
BOOL r = ReadFile(m_hFile, &dwSize, sizeof(dwSize), &dwRead, nullptr);
if (!r || dwRead != sizeof(dwSize))
return 1;
SetFilePointer(m_hFile, -4, 0, FILE_CURRENT);
int cbLen;
if (dwSize == sizeof(MC_MsgHeader32)) {
MC_MsgHeader32 hdr;
r = ReadFile(m_hFile, &hdr, sizeof(hdr), &dwRead, 0);
if (!r || dwRead != sizeof(hdr))
return 1;
dbei->eventType = hdr.eventType;
cbLen = hdr.cbBlob;
dbei->flags = hdr.flags;
dbei->timestamp = hdr.timestamp;
}
else if (dwSize == sizeof(MC_MsgHeader64)) {
MC_MsgHeader64 hdr;
r = ReadFile(m_hFile, &hdr, sizeof(hdr), &dwRead, 0);
if (!r || dwRead != sizeof(hdr))
return 1;
dbei->eventType = hdr.eventType;
cbLen = hdr.cbBlob;
dbei->flags = hdr.flags;
dbei->timestamp = hdr.timestamp;
}
else return 1;
if (dbei->cbBlob && cbLen) {
uint32_t copySize = min(uint32_t(cbLen), dbei->cbBlob-1);
if (!ReadFile(m_hFile, dbei->pBlob, copySize, &dwRead, 0) || dwRead != copySize)
return 0;
dbei->cbBlob = copySize;
dbei->pBlob[copySize] = 0;
}
return 0;
}
STDMETHODIMP_(MEVENT) FindFirstEvent(MCONTACT) override
{
if (m_events.empty())
return 0;
m_curr = m_events.begin();
return *m_curr;
}
STDMETHODIMP_(MEVENT) FindNextEvent(MCONTACT, MEVENT) override
{
if (m_curr == m_events.end())
return 0;
++m_curr;
return *m_curr;
}
STDMETHODIMP_(MEVENT) FindLastEvent(MCONTACT) override
{
if (m_events.empty())
return 0;
m_curr = m_events.end();
return *m_curr;
}
STDMETHODIMP_(MEVENT) FindPrevEvent(MCONTACT, MEVENT) override
{
if (m_curr == m_events.begin())
return 0;
--m_curr;
return *m_curr;
}
STDMETHODIMP_(DATABASELINK *) GetDriver() override;
};
/////////////////////////////////////////////////////////////////////////////////////////
// database link functions
static int mc_makeDatabase(const wchar_t*)
{
return 1;
}
static int mc_grokHeader(const wchar_t *profile)
{
return CDbxMc().Open(profile);
}
static MDatabaseCommon* mc_load(const wchar_t *profile, BOOL)
{
std::unique_ptr<CDbxMc> db(new CDbxMc());
if (db->Open(profile))
return nullptr;
db->Load();
return db.release();
}
static DATABASELINK dblink =
{
0,
"mcontacts",
L"mContacts file driver",
mc_makeDatabase,
mc_grokHeader,
mc_load
};
STDMETHODIMP_(DATABASELINK *) CDbxMc::GetDriver()
{
return &dblink;
}
void RegisterMContacts()
{
RegisterDatabasePlugin(&dblink);
}
|