-
Notifications
You must be signed in to change notification settings - Fork 13
/
Kdb4Node.m
349 lines (284 loc) · 8.01 KB
/
Kdb4Node.m
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
//
// Kdb4Node.m
// KeePass2
//
// Created by Qiang Yu on 2/23/10.
// Copyright 2010 Qiang Yu. All rights reserved.
//
#import "Kdb4Node.h"
#import "Utils.h"
#define K_NOTES "Notes"
#define K_PASSWORD "Password"
#define K_TITLE "Title"
#define K_URL "URL"
#define K_USERNAME "UserName"
@interface Kdb4Group (PrivateMethods)
@end
@implementation Kdb4Group
@synthesize _uuid;
@synthesize _image;
@synthesize _title;
@synthesize _comment;
@synthesize _subGroups;
@synthesize _entries;
-(void)dealloc{
//DLog(@"releas node %@", _title);
[_uuid release];
[_title release];
[_comment release];
[_subGroups release];
[_entries release];
[super dealloc];
}
-(id<KdbGroup>)getParent{
return (Kdb4Group *)self._parent;
}
-(void)setParent:(id<KdbGroup>)parent{
self._parent = parent;
}
-(void)addEntry:(id<KdbEntry>)child{
((Node *)child)._parent = self;
if(!_entries){
_entries = [[NSMutableArray alloc]initWithCapacity:16];
}
[_entries addObject:child];
}
-(void)deleteEntry:(id<KdbEntry>)child{
((Node*)child)._parent = nil;
[_entries removeObject:child];
}
-(void)addSubGroup:(id<KdbGroup>)child{
((Node*)child)._parent = self;
if(!_subGroups){
_subGroups = [[NSMutableArray alloc]initWithCapacity:8];
}
[_subGroups addObject:child];
}
-(void)deleteSubGroup:(id<KdbGroup>)child{
((Node*)child)._parent = nil;
[_subGroups removeObject:child];
}
-(void)postProcess:(id<RandomStream>)rs{
[super postProcess:rs];
NSMutableArray * nodesToRelease = [[NSMutableArray alloc]initWithCapacity:8];
NSMutableArray * nodesToMove = [[NSMutableArray alloc]initWithCapacity:8];
for(Node * n in _children){
if([n._name isEqualToString:@T_UUID]){
//DLog(@"----releasing node:%@ %d %d", n._name, [n retainCount], [n._children count]);
self._uuid = n._text;
[nodesToRelease addObject:n];
//DLog(@"----releasing node:%@ %d %d", n._name, [n retainCount], [n._children count]);
}else if([n._name isEqualToString:@T_NAME]){
self._title = n._text;
[nodesToRelease addObject:n];
}else if([n._name isEqualToString:@T_ICONID]){
if([Utils emptyString:n._text]){
self._image = -1;
}else{
self._image = [n._text intValue];
}
[nodesToRelease addObject:n];
}else if([n._name isEqualToString:@T_NOTES]){
self._comment = n._text;
[nodesToRelease addObject:n];
}else if([n._name isEqualToString:@T_GROUP]){
//DLog(@"****releasing node:%@ %d %d", n._name, [n retainCount], [n._children count]);
[self addSubGroup:(id<KdbGroup>)n];
[nodesToMove addObject:n];
}else if([n._name isEqualToString:@T_ENTRY]){
//DLog(@"****releasing node:%@ %d %d", n._name, [n retainCount], [n._children count]);
[self addEntry:(id<KdbEntry>)n];
[nodesToMove addObject:n];
}
}
for(Node * n in nodesToRelease){
//DLog(@"====releasing node:%@ %d %d", n._name, [n retainCount], [n._children count]);
[n breakCyclcReference];
[self removeChild:n];
//DLog(@"====releasing node:%@ %d %d", n._name, [n retainCount], [n._children count]);
}
//DLog(@"UUID====%d", [self._uuid retainCount]);
[nodesToRelease release];
//DLog(@"UUID====%@", self._uuid);
for(Node * n in nodesToMove){
[self removeChild:n];
n._parent = self;
}
[nodesToMove release];
//DLog(@"Group Title ==> %@", _title);
}
-(NSString *)description{
NSString * descr = [NSString stringWithFormat:@"[UUID:%@ title:%@ \ncomment:%@]",
_uuid, _title, _comment];
return descr;
}
//break cyclic references
-(void)breakCyclcReference{
[super breakCyclcReference];
for(Node * child in _subGroups){
[child breakCyclcReference];
}
for(Node * child in _entries){
[child breakCyclcReference];
}
}
// KDB4 is readonly so far, no need to implement these functions
-(void)setCreation:(NSDate *) date{}
-(void)setLastMod:(NSDate *) date{}
-(void)setLastAccess:(NSDate *) date{}
-(void)setExpiry:(NSDate *) date{}
@end
@interface Kdb4Entry (PrivateMethods)
-(void)processCustomAttributes:(Node *)node withRandomStream:(id<RandomStream>)rs;
@end
@implementation Kdb4Entry
@synthesize _uuid;
@synthesize _image;
@synthesize _title;
@synthesize _url;
@synthesize _username;
@synthesize _password;
@synthesize _comment;
@synthesize _customeAttributeKeys;
-(void)dealloc{
//DLog(@"releas node %@", _title);
[_uuid release];
[_title release];
[_url release];
[_username release];
[_password release];
[_comment release];
[_customeAttributes release];
[_customeAttributeKeys release];
[super dealloc];
}
-(id<KdbGroup>)getParent{
return (Kdb4Group *)self._parent;
}
-(void)setParent:(id<KdbGroup>)parent{
self._parent = parent;
}
-(NSUInteger)getNumberOfCustomAttributes{
return [_customeAttributeKeys count];
}
-(NSString *)getCustomAttributeName:(NSUInteger) index{
return [_customeAttributeKeys objectAtIndex:index];
}
-(NSString *)getCustomAttributeValue:(NSUInteger) index{
return [_customeAttributes objectForKey:[_customeAttributeKeys objectAtIndex:index]];
}
-(void)releaseNode:(Node *)node{
[node breakCyclcReference];
[node release];
}
-(void)processCustomAttributes:(Node *)node withRandomStream:(id<RandomStream>)rs{
NSString * key = nil;
NSString * value = nil;
for(Node * n in node._children){
if([n._name isEqualToString:@T_KEY]){
key = n._text;
}else if([n._name isEqualToString:@T_VALUE]){
value = n._text;
}
}
if([key isEqualToString:@K_NOTES]){
self._comment = value;
}else if([key isEqualToString:@K_PASSWORD]){
self._password = value;
}else if([key isEqualToString:@K_TITLE]){
self._title = value;
}else if([key isEqualToString:@K_URL]){
self._url = value;
}else if([key isEqualToString:@K_USERNAME]){
self._username = value;
}else{
if(!_customeAttributes){
_customeAttributes = [[NSMutableDictionary alloc]initWithCapacity:4];
}
[_customeAttributes setObject:value forKey:key];
}
}
-(void)postProcess:(id<RandomStream>)rs{
[super postProcess:rs];
NSMutableArray * nodesToRelease = [[NSMutableArray alloc]initWithCapacity:8];
for(Node * n in _children){
if([n._name isEqualToString:@T_UUID]){
self._uuid = n._text;
[nodesToRelease addObject:n];
}else if([n._name isEqualToString:@T_ICONID]){
if([Utils emptyString:n._text]){
self._image = -1;
}else{
self._image = [n._text intValue];
}
[nodesToRelease addObject:n];
}else if([n._name isEqualToString:@T_NOTES]){
self._comment = n._text;
[nodesToRelease addObject:n];
}else if([n._name isEqualToString:@T_STRING]){
[self processCustomAttributes:n withRandomStream:rs];
[nodesToRelease addObject:n];
}
}
for(Node * n in nodesToRelease){
[self removeChild:n];
[n breakCyclcReference];
}
[nodesToRelease release];
//customer attributes
self._customeAttributeKeys = [_customeAttributes keysSortedByValueUsingSelector:@selector(caseInsensitiveCompare:)];
}
-(NSString *)description{
NSString * descr = [NSString stringWithFormat:@"[UUID:%@ title:%@ \nusername:%@ \npassword:%@ \nurl:%@ \ncomment:%@]",
_uuid, _title, _username, _password, _url, _comment];
return descr;
}
// KDB4 is readonly so far, no need to implement these functions
-(void)setCreation:(NSDate *) date{}
-(void)setLastMod:(NSDate *) date{}
-(void)setLastAccess:(NSDate *) date{}
-(void)setExpiry:(NSDate *) date{}
@end
@implementation Kdb4Tree
@synthesize _meta;
//
// return the KDB Root
//
-(id<KdbGroup>)getRoot{
for(Node * n in _root._children){
if([n._name isEqualToString:@T_ROOT]){
return (id<KdbGroup>)n;
}
}
return nil;
}
-(id)init{
if(self=[super init]){
_meta = [[NSMutableDictionary alloc]initWithCapacity:4];
}
return self;
}
-(void)dealloc{
[_meta release];
[super dealloc];
}
-(NSString *)getMetaInfo:(NSString *)key{
NSString * value = [_meta objectForKey:key];
if(value) return value;
for(Node * n in _root._children){
if([n._name isEqualToString:@T_META]){
for(Node * m in n._children){
if([m._name isEqualToString:key]){
[_meta setObject:m._text forKey:key];
break;
}
}
}
break;
}
return [_meta objectForKey:key];
}
-(BOOL)isRecycleBin:(id<KdbGroup>)group{
return [((Kdb4Group *)group)._uuid isEqualToString:[self getMetaInfo:@T_RECYCLEBINUUID]];
}
@end