-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegMap.cpp
More file actions
419 lines (379 loc) · 10.5 KB
/
RegMap.cpp
File metadata and controls
419 lines (379 loc) · 10.5 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright (c) 2004, Steven Scott (progoth@gmail.com)
//
// This file is part of iSnooze.
//
// iSnooze 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.
//
// iSnooze 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 iSnooze; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include "regmap.h"
RegMap::RegMap( HKEY root, const tstring &key )
: m_root(root), m_isval(false), m_type(KEY), m_create(false), m_key(key)
{
}
RegMap::RegMap( const tstring &key, HKEY root, bool create )
: m_root(root), m_key(key), m_isval(false), m_type(KEY), m_create(create)
{
}
void RegMap::clean()
{
if( m_isval )
{
switch( m_type )
{
case SZ:
delete m_val.str;
break;
case BIN:
delete m_val.bin;
break;
case MV_SZ:
delete m_val.strarray;
break;
}
}
else
{
std::map<tstring,RegMap*>::iterator end = m_regcache.end();
for( std::map<tstring,RegMap*>::iterator i = m_regcache.begin(); i != end; ++i )
delete i->second;
m_regcache.clear();
}
}
RegMap& RegMap::operator[]( const TCHAR *item ) const
{
HKEY key;
LONG hr;
if( _tcslen( item ) )
{
tstring newkey;
if( !m_key.empty() ) newkey = m_key + _T("\\") + item;
else newkey = item;
hr = RegOpenKeyEx( m_root, newkey.c_str(), 0, KEY_QUERY_VALUE | KEY_READ, &key );
if( hr == ERROR_SUCCESS )
{
RegCloseKey( key );
if( m_regcache.find( item ) != m_regcache.end() )
delete m_regcache[item];
m_regcache[item] = new RegMap( newkey, m_root );
return *(m_regcache[item]);
}
}
hr = RegOpenKeyEx( m_root, m_key.c_str(), 0, KEY_QUERY_VALUE | KEY_READ, &key );
if( hr == ERROR_SUCCESS )
{
ULONG newbufsize, bufsize = 512;
unsigned char *buf;
ULONG type;
do {
bufsize *= 2;
buf = new unsigned char[bufsize];
newbufsize = bufsize;
hr = RegQueryValueEx( key, item, NULL, &type, buf, &newbufsize );
if( hr != ERROR_SUCCESS )
delete[] buf;
} while( hr == ERROR_MORE_DATA );
if( hr == ERROR_SUCCESS )
{
RegCloseKey( key );
if( m_regcache.find( item ) != m_regcache.end() )
delete m_regcache[item];
tstring newkey;
if( !m_key.empty() ) newkey = m_key + _T("\\") + item;
else newkey = item;
switch( type )
{
case REG_BINARY:
{
binary nb;
nb.data = std::string( (char*)buf, newbufsize );
m_regcache[item] = new RegMap( nb, newkey.c_str(), m_root );
delete[] buf;
return *(m_regcache[item]);
}
case REG_DWORD:
m_regcache[item] = new RegMap( ((DWORD*)buf)[0], newkey.c_str(), m_root );
delete[] buf;
return *(m_regcache[item]);
case REG_SZ:
m_regcache[item] = new RegMap( (TCHAR*)buf, newkey.c_str(), m_root );
delete[] buf;
return *(m_regcache[item]);
case REG_MULTI_SZ:
{
TCHAR *str = (TCHAR*)buf;
std::vector<tstring> out;
while( *str != _T('\0') )
{
out.push_back( str );
str += _tcslen( str );
}
m_regcache[item] = new RegMap( out, newkey.c_str(), m_root );
delete[] buf;
return *(m_regcache[item]);
}
default:
delete[] buf;
throw trterror( _T("Type not supported (yet)") );
}
}
RegCloseKey( key );
}
//else
{
if( m_regcache.find( item ) != m_regcache.end() )
delete m_regcache[item];
tstring newkey;
if( !m_key.empty() ) newkey = m_key + _T("\\") + item;
else newkey = item;
m_regcache[item] = new RegMap( newkey, m_root, true );
return *(m_regcache[item]);
}
throw trterror( _T("this key doesn't exist") );
}
void RegMap::setValue( const RegMap &o )
{
TCHAR *buf = new TCHAR[m_key.size() + 1];
TCHAR *parent = new TCHAR[m_key.size() + 1];
_tcscpy( buf, m_key.c_str() );
_tcscpy( parent, buf );
TCHAR *child;
if( ( child = _tcsrchr( parent, _T('\\') ) ) == NULL )
{
child = buf;
parent[0] = _T('\0');
}
else
{
*child = _T('\0');
child++;
}
try
{
setValue( parent, child, o );
}
catch( ... )
{
delete[] buf;
delete[] parent;
throw;
}
delete[] buf;
delete[] parent;
}
RegMap &RegMap::operator=( const RegMap &o )
{
if( this == &o ) return *this;
if( m_create )
{
setValue( o );
}
if( !m_create )
{
m_key = o.m_key;
m_root = o.m_root;
return *this;
}
return *this;
}
void RegMap::setValue( const TCHAR *parent, const TCHAR *child, const RegMap &o )
{
HKEY parentkey, key;
LONG hr;
hr = RegOpenKeyEx( m_root, parent, 0, KEY_WRITE | KEY_CREATE_SUB_KEY, &parentkey );
if( hr != ERROR_SUCCESS )
{
TCHAR msg[1024];
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0, msg, 1023, NULL );
msg[1023] = _T('\0');
throw trterror( msg );
}
switch( o.getType() )
{
case KEY:
{
hr = RegCreateKeyEx( parentkey, child, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
if( hr != ERROR_SUCCESS )
{
RegCloseKey( parentkey );
TCHAR msg[1024];
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0, msg, 1023, NULL );
msg[1023] = _T('\0');
throw trterror( msg );
}
RegCloseKey( key );
break;
}
case NUMBER:
{
hr = RegSetValueEx( parentkey, child, 0, REG_DWORD, (const BYTE*)&(o.m_val.dword), sizeof(DWORD) );
if( hr != ERROR_SUCCESS )
{
RegCloseKey( parentkey );
TCHAR msg[1024];
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0, msg, 1023, NULL );
msg[1023] = _T('\0');
throw trterror( msg );
}
break;
}
case BIN:
{
hr = RegSetValueEx( parentkey, child, 0, REG_BINARY, (const BYTE*)(o.m_val.bin->data.c_str()), o.m_val.bin->data.size() );
if( hr != ERROR_SUCCESS )
{
RegCloseKey( parentkey );
TCHAR msg[1024];
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0, msg, 1023, NULL );
msg[1023] = _T('\0');
throw trterror( msg );
}
break;
}
case SZ:
{
hr = RegSetValueEx( parentkey, child, 0, REG_SZ, (const BYTE*)o.m_val.str->c_str(),
sizeof(TCHAR) * (DWORD)(o.m_val.str->size() + 1) );
if( hr != ERROR_SUCCESS )
{
RegCloseKey( parentkey );
TCHAR msg[1024];
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0, msg, 1023, NULL );
msg[1023] = _T('\0');
throw trterror( msg );
}
break;
}
case MV_SZ:
{
ULONG len = 0;
std::vector<tstring>::iterator i, end = o.m_val.strarray->end();
for( i = o.m_val.strarray->begin(); i != end; ++i )
len += 1 + (ULONG)o.m_val.strarray->size();
TCHAR *tbuf = new TCHAR[len+1];
len = 0;
for( i = o.m_val.strarray->begin(); i != end; ++i )
{
_tcscpy( tbuf + len, (*i).c_str() );
len += 1 + (*i).size();
}
tbuf[len] = _T('\0');
hr = RegSetValueEx( parentkey, child, 0, REG_MULTI_SZ, (const BYTE*)tbuf, sizeof(TCHAR) * (len + 1) );
if( hr != ERROR_SUCCESS )
{
delete[] tbuf;
RegCloseKey( parentkey );
TCHAR msg[1024];
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0, msg, 1023, NULL );
msg[1023] = _T('\0');
throw trterror( msg );
}
delete[] tbuf;
break;
}
}
RegCloseKey( parentkey );
}
void RegMap::keys( std::vector<tstring> &out ) const
{
HKEY key;
LONG hr = RegOpenKeyEx( m_root, m_key.c_str(), 0,
KEY_QUERY_VALUE | KEY_READ | KEY_ENUMERATE_SUB_KEYS, &key );
if( hr != ERROR_SUCCESS )
{
TCHAR msg[1024];
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0, msg, 1023, NULL );
msg[1023] = _T('\0');
throw trterror( msg );
}
DWORD len = 16383;
TCHAR buf[16383];
ULONG i = 0;
while( RegEnumValue( key, i++, buf, &len, NULL, NULL, NULL, NULL ) != ERROR_NO_MORE_ITEMS )
{
out.push_back( _tcslwr( buf ) );
len = 16383;
}
i = 0;
FILETIME ft;
len = 16383;
while( RegEnumKeyEx( key, i++, buf, &len, NULL, NULL, NULL, &ft ) != ERROR_NO_MORE_ITEMS )
{
out.push_back( _tcslwr( buf ) );
len = 16383;
}
std::sort( out.begin(), out.end() );
RegCloseKey( key );
}
bool RegMap::has_key( const tstring &key ) const
{
TCHAR buf[16383];
if( key.size() > 16382 )
throw trterror( _T("has_key: value too long") );
_tcscpy( buf, key.c_str() );
std::vector<tstring> keylist;
keys( keylist );
return (std::find( keylist.begin(), keylist.end(), _tcslwr( buf ) ) != keylist.end());
}
void RegMap::deleteValue( const TCHAR *val ) const
{
if( !has_key( val ) )
throw trterror( tstring(val) + _T(" is not an existing value") );
RegMap r = (*this)[val];
if( r.getType() == KEY )
throw trterror( tstring(val) + _T(" is a key, not a value") );
HKEY key;
LONG hr = RegOpenKeyEx( m_root, m_key.c_str(), 0, KEY_WRITE, &key );
if( hr != ERROR_SUCCESS )
{
TCHAR msg[1024];
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0, msg, 1023, NULL );
msg[1023] = _T('\0');
throw trterror( msg );
}
RegDeleteValue( key, val );
RegCloseKey( key );
}
void RegMap::deleteKey() const
{
TCHAR *buf = new TCHAR[m_key.size() + 1];
TCHAR *todel = new TCHAR[m_key.size() + 1];
_tcscpy( buf, m_key.c_str() );
while( buf[ _tcslen( buf ) - 1 ] == _T('\\') )
buf[ _tcslen( buf ) - 1 ] = _T('\0');
_tcscpy( todel, buf );
TCHAR *p;
if( ( p = _tcsrchr( buf, _T('\\') ) ) == NULL )
{
buf[0] = _T('\0');
}
else
{
*p = _T('\0');
_tcscpy( todel, p + 1 );
}
HKEY key;
LONG hr = RegOpenKeyEx( m_root, buf, 0, KEY_WRITE, &key );
if( hr != ERROR_SUCCESS )
{
delete[] buf;
delete[] todel;
TCHAR msg[1024];
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM, NULL, hr, 0, msg, 1023, NULL );
msg[1023] = _T('\0');
throw trterror( msg );
}
RegDeleteKey( key, todel );
RegCloseKey( key );
delete[] buf;
delete[] todel;
}