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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
|
/*
* Samba Unix/Linux SMB client library
* Registry Editor
* Copyright (C) Christopher Davis 2012
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include "regedit_treeview.h"
#include "regedit_list.h"
#include "lib/registry/registry.h"
#define HEADING_X 3
struct tree_node *tree_node_new(TALLOC_CTX *ctx, struct tree_node *parent,
const char *name, struct registry_key *key)
{
struct tree_node *node;
node = talloc_zero(ctx, struct tree_node);
if (!node) {
return NULL;
}
node->name = talloc_strdup(node, name);
if (!node->name) {
talloc_free(node);
return NULL;
}
node->key = talloc_steal(node, key);
if (parent) {
/* Check if this node is the first descendant of parent. */
if (!parent->child_head) {
parent->child_head = node;
}
node->parent = parent;
}
return node;
}
void tree_node_append(struct tree_node *left, struct tree_node *right)
{
if (left->next) {
right->next = left->next;
left->next->previous = right;
}
left->next = right;
right->previous = left;
}
void tree_node_append_last(struct tree_node *list, struct tree_node *node)
{
tree_node_append(tree_node_last(list), node);
}
struct tree_node *tree_node_pop(struct tree_node **plist)
{
struct tree_node *node;
node = *plist;
if (node == NULL)
return NULL;
*plist = node->previous;
if (*plist == NULL) {
*plist = node->next;
}
if (node->previous) {
node->previous->next = node->next;
}
if (node->next) {
node->next->previous = node->previous;
}
if (node->parent && node->parent->child_head == node) {
node->parent->child_head = node->next;
}
node->next = NULL;
node->previous = NULL;
return node;
}
struct tree_node *tree_node_first(struct tree_node *list)
{
/* Grab the first node in this list from the parent if available. */
if (list->parent) {
return list->parent->child_head;
}
while (list && list->previous) {
list = list->previous;
}
return list;
}
struct tree_node *tree_node_last(struct tree_node *list)
{
while (list && list->next) {
list = list->next;
}
return list;
}
static uint32_t get_num_subkeys(struct tree_node *node)
{
const char *classname;
uint32_t num_subkeys;
uint32_t num_values;
NTTIME last_change_time;
uint32_t max_subkeynamelen;
uint32_t max_valnamelen;
uint32_t max_valbufsize;
WERROR rv;
rv = reg_key_get_info(node, node->key, &classname, &num_subkeys,
&num_values, &last_change_time,
&max_subkeynamelen, &max_valnamelen,
&max_valbufsize);
if (W_ERROR_IS_OK(rv)) {
return num_subkeys;
}
return 0;
}
WERROR tree_node_reopen_key(struct tree_node *node)
{
SMB_ASSERT(node->parent != NULL);
SMB_ASSERT(node->name != NULL);
TALLOC_FREE(node->key);
return reg_open_key(node->parent, node->parent->key, node->name,
&node->key);
}
bool tree_node_has_children(struct tree_node *node)
{
if (node->child_head) {
return true;
}
return get_num_subkeys(node) > 0;
}
static int node_cmp(struct tree_node **a, struct tree_node **b)
{
return strcmp((*a)->name, (*b)->name);
}
void tree_node_insert_sorted(struct tree_node *list, struct tree_node *node)
{
list = tree_node_first(list);
if (node_cmp(&list, &node) >= 0) {
tree_node_append(node, list);
if (list->parent) {
list->parent->child_head = node;
}
return;
}
while (list->next && node_cmp(&list->next, &node) < 0) {
list = list->next;
}
tree_node_append(list, node);
}
WERROR tree_node_load_children(struct tree_node *node)
{
struct registry_key *key;
const char *key_name, *klass;
NTTIME modified;
uint32_t i, nsubkeys, count;
WERROR rv;
struct tree_node *prev, **array;
/* does this node already have it's children loaded? */
if (node->child_head)
return WERR_OK;
nsubkeys = get_num_subkeys(node);
if (nsubkeys == 0)
return WERR_OK;
array = talloc_zero_array(node, struct tree_node *, nsubkeys);
if (array == NULL) {
return WERR_NOMEM;
}
for (count = 0, i = 0; i < nsubkeys; ++i) {
rv = reg_key_get_subkey_by_index(node, node->key, i,
&key_name, &klass,
&modified);
if (!W_ERROR_IS_OK(rv)) {
goto finish;
}
rv = reg_open_key(node, node->key, key_name, &key);
if (!W_ERROR_IS_OK(rv)) {
continue;
}
array[count] = tree_node_new(node, node, key_name, key);
if (array[count] == NULL) {
rv = WERR_NOMEM;
goto finish;
}
++count;
}
if (count) {
TYPESAFE_QSORT(array, count, node_cmp);
for (i = 1, prev = array[0]; i < count; ++i) {
tree_node_append(prev, array[i]);
prev = array[i];
}
node->child_head = array[0];
rv = WERR_OK;
}
finish:
if (!W_ERROR_IS_OK(rv)) {
for (i = 0; i < nsubkeys; ++i) {
talloc_free(array[i]);
}
node->child_head = NULL;
}
talloc_free(array);
return rv;
}
void tree_node_free_recursive(struct tree_node *list)
{
struct tree_node *node;
if (list == NULL) {
return;
}
while ((node = tree_node_pop(&list)) != NULL) {
if (node->child_head) {
tree_node_free_recursive(node->child_head);
}
node->child_head = NULL;
talloc_free(node);
}
}
void tree_view_clear(struct tree_view *view)
{
multilist_set_data(view->list, NULL);
}
WERROR tree_view_update(struct tree_view *view, struct tree_node *list)
{
WERROR rv;
rv = multilist_set_data(view->list, list);
if (W_ERROR_IS_OK(rv)) {
multilist_refresh(view->list);
}
return rv;
}
/* is this node in the current level? */
bool tree_view_is_node_visible(struct tree_view *view, struct tree_node *node)
{
const struct tree_node *first;
first = multilist_get_data(view->list);
return first && first->parent == node->parent;
}
void tree_view_set_current_node(struct tree_view *view, struct tree_node *node)
{
multilist_set_current_row(view->list, node);
}
struct tree_node *tree_view_get_current_node(struct tree_view *view)
{
const void *row = multilist_get_current_row(view->list);
return talloc_get_type_abort(row, struct tree_node);
}
void tree_view_driver(struct tree_view *view, int c)
{
multilist_driver(view->list, c);
}
void tree_view_set_selected(struct tree_view *view, bool select)
{
attr_t attr = A_NORMAL;
if (select) {
attr = A_REVERSE;
}
mvwchgat(view->window, 0, HEADING_X, 3, attr, 0, NULL);
}
void tree_view_show(struct tree_view *view)
{
multilist_refresh(view->list);
touchwin(view->window);
wnoutrefresh(view->window);
wnoutrefresh(view->sub);
}
static int tree_view_free(struct tree_view *view)
{
if (view->panel) {
del_panel(view->panel);
}
if (view->sub) {
delwin(view->sub);
}
if (view->window) {
delwin(view->window);
}
tree_node_free_recursive(view->root);
return 0;
}
static const char *tv_get_column_header(const void *data, unsigned col)
{
SMB_ASSERT(col == 0);
return "Name";
}
static const void *tv_get_first_row(const void *data)
{
if (data == NULL) {
return NULL;
}
return talloc_get_type_abort(data, struct tree_node);
}
static const void *tv_get_next_row(const void *data, const void *row)
{
const struct tree_node *node;
SMB_ASSERT(row != NULL);
node = talloc_get_type_abort(row, struct tree_node);
return node->next;
}
static const void *tv_get_prev_row(const void *data, const void *row)
{
const struct tree_node *node;
SMB_ASSERT(row != NULL);
node = talloc_get_type_abort(row, struct tree_node);
return node->previous;
}
static const char *tv_get_item_prefix(const void *row, unsigned col)
{
struct tree_node *node;
SMB_ASSERT(col == 0);
SMB_ASSERT(row != NULL);
node = talloc_get_type_abort(row, struct tree_node);
if (tree_node_has_children(node)) {
return "+";
}
return " ";
}
static const char *tv_get_item_label(const void *row, unsigned col)
{
const struct tree_node *node;
SMB_ASSERT(col == 0);
SMB_ASSERT(row != NULL);
node = talloc_get_type_abort(row, struct tree_node);
return node->name;
}
static struct multilist_accessors tv_accessors = {
.get_column_header = tv_get_column_header,
.get_first_row = tv_get_first_row,
.get_next_row = tv_get_next_row,
.get_prev_row = tv_get_prev_row,
.get_item_prefix = tv_get_item_prefix,
.get_item_label = tv_get_item_label
};
struct tree_view *tree_view_new(TALLOC_CTX *ctx, struct tree_node *root,
int nlines, int ncols, int begin_y,
int begin_x)
{
struct tree_view *view;
view = talloc_zero(ctx, struct tree_view);
if (view == NULL) {
return NULL;
}
talloc_set_destructor(view, tree_view_free);
view->window = newwin(nlines, ncols, begin_y, begin_x);
if (view->window == NULL) {
goto fail;
}
view->sub = subwin(view->window, nlines - 2, ncols - 2,
begin_y + 1, begin_x + 1);
if (view->sub == NULL) {
goto fail;
}
box(view->window, 0, 0);
mvwprintw(view->window, 0, HEADING_X, "Key");
view->panel = new_panel(view->window);
if (view->panel == NULL) {
goto fail;
}
view->root = root;
view->list = multilist_new(view, view->sub, &tv_accessors, 1);
if (view->list == NULL) {
goto fail;
}
tree_view_update(view, root);
return view;
fail:
talloc_free(view);
return NULL;
}
void tree_view_resize(struct tree_view *view, int nlines, int ncols,
int begin_y, int begin_x)
{
WINDOW *nwin, *nsub;
nwin = newwin(nlines, ncols, begin_y, begin_x);
if (nwin == NULL) {
return;
}
nsub = subwin(nwin, nlines - 2, ncols - 2, begin_y + 1, begin_x + 1);
if (nsub == NULL) {
delwin(nwin);
return;
}
replace_panel(view->panel, nwin);
delwin(view->sub);
delwin(view->window);
view->window = nwin;
view->sub = nsub;
box(view->window, 0, 0);
mvwprintw(view->window, 0, HEADING_X, "Key");
multilist_set_window(view->list, view->sub);
tree_view_show(view);
}
static void print_path_recursive(WINDOW *label, struct tree_node *node,
size_t *len)
{
if (node->parent)
print_path_recursive(label, node->parent, len);
wprintw(label, "%s/", node->name);
*len += 1 + strlen(node->name);
}
/* print the path of node to label */
size_t tree_node_print_path(WINDOW *label, struct tree_node *node)
{
size_t len = 1;
if (node == NULL)
return 0;
werase(label);
wprintw(label, "/");
if (node->parent)
print_path_recursive(label, node->parent, &len);
return len;
}
|