forked from mykeepass/KeePassLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kdb3Parser.m
325 lines (299 loc) · 11.2 KB
/
Kdb3Parser.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
//
// Kdb3Parser.m
// KeePass2
//
// Created by Qiang Yu on 2/13/10.
// Copyright 2010 Qiang Yu. All rights reserved.
//
#import "Kdb3Parser.h"
#import "Utils.h"
#import "Kdb3Node.h"
#import "Kdb3Date.h"
@interface Kdb3Parser(PrivateMethods)
-(void)read:(id<InputDataSource>)input toGroups:(NSMutableArray *)groups levels:(NSMutableArray *)levels numOfGroups:(uint32_t)numGroups;
-(void)read:(id<InputDataSource>)input toEntries:(NSMutableArray *)entries numOfEntries:(uint32_t)numEntries withGroups:(NSArray *)groups;
-(id<KdbTree>)buildTree:(NSArray *)groups levels:(NSArray *)levels entries:(NSArray *)entries;
@end
@implementation Kdb3Parser
-(id<KdbTree>)parse:(id<InputDataSource>)input numGroups:(uint32_t)numGroups numEntris:(uint32_t)numEntries{
NSMutableArray * levels = [[NSMutableArray alloc]initWithCapacity:numGroups];
NSMutableArray * groups = [[NSMutableArray alloc]initWithCapacity:numGroups];
[self read:input toGroups:groups levels:levels numOfGroups:numGroups];
NSMutableArray * entries = [[NSMutableArray alloc]initWithCapacity:numEntries];
[self read:input toEntries:entries numOfEntries:numEntries withGroups:groups];
id<KdbTree> rv = [self buildTree:groups levels:levels entries:entries];
[groups release];
[levels release];
[entries release];
return rv;
}
-(void)read:(id<InputDataSource>)input toGroups:(NSMutableArray *)groups levels:(NSMutableArray *)levels numOfGroups:(uint32_t)numGroups{
uint16_t fieldType;
uint32_t fieldSize;
uint8_t dateBuffer[5];
//read groups
for(uint32_t curGroup=0; curGroup<numGroups; curGroup++){
Kdb3Group * group = [[Kdb3Group alloc] init];
do{
fieldType = [Utils readInt16LE:input];
//DLog(@"fieldType %X", fieldType);
fieldSize = [Utils readInt32LE:input];
//DLog(@"fieldSize %X", fieldSize);
switch(fieldType){
case 0x0000:{ [input moveReadOffset:fieldSize]; break; }
case 0x0001:{
group._id = [Utils readInt32LE:input];
break;
}
case 0x0002: {
ByteBuffer * buffer = [[ByteBuffer alloc]initWithSize:fieldSize dataSource:input];
NSString * groupTitle = [[NSString alloc]initWithCString:(const char *)buffer._bytes encoding:NSUTF8StringEncoding];
group._title = groupTitle;
//DLog(@"grouptitle: %@", groupTitle);
[groupTitle release];
[buffer release];
break;
}
case 0x0003: {
[input readBytes:dateBuffer length:fieldSize];
[Kdb3Date date:[group getCreation] fromPacked:dateBuffer];
//DLog(@"creation:%d-%d-%d %d:%d:%d", [entry getCreation][0]*100+[entry getCreation][1], [entry getCreation][2], [entry getCreation][3],
// [entry getCreation][4], [entry getCreation][5], [entry getCreation][6]);
break;
}
case 0x0004: {
[input readBytes:dateBuffer length:fieldSize];
[Kdb3Date date:[group getLastMod] fromPacked:dateBuffer];
//DLog(@"creation:%d-%d-%d %d:%d:%d", [entry getCreation][0]*100+[entry getCreation][1], [entry getCreation][2], [entry getCreation][3],
// [entry getCreation][4], [entry getCreation][5], [entry getCreation][6]);
break;
}
case 0x0005: {
[input readBytes:dateBuffer length:fieldSize];
[Kdb3Date date:[group getLastAccess] fromPacked:dateBuffer];
//DLog(@"creation:%d-%d-%d %d:%d:%d", [entry getCreation][0]*100+[entry getCreation][1], [entry getCreation][2], [entry getCreation][3],
// [entry getCreation][4], [entry getCreation][5], [entry getCreation][6]);
break;
}
case 0x0006: {
[input readBytes:dateBuffer length:fieldSize];
[Kdb3Date date:[group getExpiry] fromPacked:dateBuffer];
//DLog(@"creation:%d-%d-%d %d:%d:%d", [entry getCreation][0]*100+[entry getCreation][1], [entry getCreation][2], [entry getCreation][3],
// [entry getCreation][4], [entry getCreation][5], [entry getCreation][6]);
break;
}
case 0x0007: {
group._image = [Utils readInt32LE:input];
break;
}
case 0x0008: {
NSNumber * level = [NSNumber numberWithUnsignedInteger:[Utils readInt16LE:input]];
[levels addObject:level];
break;
}
case 0x0009: {
group._flags = [Utils readInt32LE:input];
break;
}
case 0xFFFF: { [input moveReadOffset:fieldSize];break; }
default:{
//DLog(@"fieldType %X", fieldType);
@throw [NSException exceptionWithName:@"ParseError" reason:@"ParseGroup" userInfo:nil];
}
}
if(fieldType == 0xFFFF){
[groups addObject:group];
break;
}
}while(true);
[group release];
}
}
-(void)read:(id<InputDataSource>)input toEntries:(NSMutableArray *)entries numOfEntries:(uint32_t)numEntries withGroups:(NSArray *)groups {
uint16_t fieldType;
uint32_t fieldSize;
uint8_t dateBuffer[5];
for(uint32_t curEntry=0; curEntry<numEntries; curEntry++){
Kdb3Entry * entry = [[Kdb3Entry alloc]init];
uint32_t groupId;
do{
fieldType = [Utils readInt16LE:input];
fieldSize = [Utils readInt32LE:input];
switch(fieldType){
case 0x0000: { [input moveReadOffset:fieldSize];break; }
case 0x0001: {
UUID * uuid = [[UUID alloc]initWithSize:fieldSize dataSource:input];
entry._uuid = uuid;
//DLog(@"uuid: %@", uuid);
[uuid release];
break;
}
case 0x0002: {
groupId = [Utils readInt32LE:input];
/*for(Kdb3Group * g in groups){
//find the father
if(g._id == groupId){
[g addEntry:entry];
//DLog(@"found parent:%@", g);
break;
}
}*/
break;
}
case 0x0003:{
entry._image = [Utils readInt32LE:input];
break;
}
case 0x0004: {
ByteBuffer * buffer = [[ByteBuffer alloc]initWithSize:fieldSize dataSource:input];
NSString * title = [[NSString alloc]initWithCString:(const char *)buffer._bytes encoding:NSUTF8StringEncoding];
entry._title = title;
//DLog(@"title: %@", title);
[title release];
[buffer release];
break;
}
case 0x0005: {
ByteBuffer * buffer = [[ByteBuffer alloc]initWithSize:fieldSize dataSource:input];
NSString * url = [[NSString alloc]initWithCString:(const char *)buffer._bytes encoding:NSUTF8StringEncoding];
entry._url = url;
//DLog(@"url: %@", url);
[url release];
[buffer release];
break;
}
case 0x0006: {
ByteBuffer * buffer = [[ByteBuffer alloc]initWithSize:fieldSize dataSource:input];
NSString * username = [[NSString alloc]initWithCString:(const char *)buffer._bytes encoding:NSUTF8StringEncoding];
entry._username = username;
//DLog(@"username: %@", username);
[username release];
[buffer release];
break;
}
case 0x0007:{
ByteBuffer * buffer = [[ByteBuffer alloc]initWithSize:fieldSize dataSource:input];
NSString * password = [[NSString alloc]initWithCString:(const char *)buffer._bytes encoding:NSUTF8StringEncoding];
entry._password = password;
//DLog(@"password: %@", password);
[password release];
[buffer release];
break;
}
case 0x0008:{
ByteBuffer * buffer = [[ByteBuffer alloc]initWithSize:fieldSize dataSource:input];
NSString * comment = [[NSString alloc]initWithCString:(const char *)buffer._bytes encoding:NSUTF8StringEncoding];
entry._comment = comment;
//DLog(@"comment: %@", comment);
[comment release];
[buffer release];
break;
}
case 0x0009:{
[input readBytes:dateBuffer length:fieldSize];
[Kdb3Date date:[entry getCreation] fromPacked:dateBuffer];
//DLog(@"creation:%d-%d-%d %d:%d:%d", [entry getCreation][0]*100+[entry getCreation][1], [entry getCreation][2], [entry getCreation][3],
// [entry getCreation][4], [entry getCreation][5], [entry getCreation][6]);
break;
}
case 0x000A:{
[input readBytes:dateBuffer length:fieldSize];
[Kdb3Date date:[entry getLastMod] fromPacked:dateBuffer];
//DLog(@"lastMod:%d-%d-%d %d:%d:%d", [entry getLastMod][0]*100+[entry getLastMod][1], [entry getLastMod][2], [entry getLastMod][3],
// [entry getLastMod][4], [entry getLastMod][5], [entry getLastMod][6]);
break;
}
case 0x000B:{
[input readBytes:dateBuffer length:fieldSize];
[Kdb3Date date:[entry getLastAccess] fromPacked:dateBuffer];
//DLog(@"lastAccess:%d-%d-%d %d:%d:%d", [entry getLastAccess][0]*100+[entry getLastAccess][1], [entry getLastAccess][2], [entry getLastAccess][3],
// [entry getLastAccess][4], [entry getLastAccess][5], [entry getLastAccess][6]);
break;
}
case 0x000C:{
[input readBytes:dateBuffer length:fieldSize];
[Kdb3Date date:[entry getExpiry] fromPacked:dateBuffer];
//DLog(@"expiry:%d-%d-%d %d:%d:%d", [entry getExpiry][0]*100+[entry getExpiry][1], [entry getExpiry][2], [entry getExpiry][3],
// [entry getCreation][4], [entry getExpiry][5], [entry getExpiry][6]);
break;
}
case 0x000D:{
ByteBuffer * buffer = [[ByteBuffer alloc]initWithSize:fieldSize dataSource:input];
NSString * binaryDesc = [[NSString alloc]initWithCString:(const char *)buffer._bytes encoding:NSUTF8StringEncoding];
entry._binaryDesc = binaryDesc;
//DLog(@"binaryDesc %@", binaryDesc);
[binaryDesc release];
[buffer release];
break;
}
case 0x000E:
entry._binarySize = fieldSize;
if(fieldSize){
MemoryBinaryContainer * container = [[MemoryBinaryContainer alloc]init];
[container storeBinary:input size:fieldSize];
entry._binary = container;
[container release];
}
break;
case 0xFFFF:{
[input moveReadOffset:fieldSize];
//all fields of the entry have been retrieved,
//find the parent.
for(Kdb3Group * g in groups){
//find the father
if(g._id == groupId){
[g addEntry:entry];
break;
}
}
break;
}
default:
@throw [NSException exceptionWithName:@"ParseError" reason:@"ParseEntry" userInfo:nil];
}
if(fieldType == 0xFFFF){
[entries addObject:entry];
break;
}
}while(true);
[entry release];
}
}
-(id<KdbTree>)buildTree:(NSArray *)groups levels:(NSArray *)levels entries:(NSArray *)entries{
///
uint32_t level = [[levels objectAtIndex:0]unsignedIntValue];
if(level!=0) @throw [NSException exceptionWithName:@"InvalidData" reason:@"InvalidTree" userInfo:nil];
id<KdbTree> tree = [[Kdb3Tree alloc]init];
Kdb3Group * rootGroup = [[Kdb3Group alloc] init];
rootGroup._title = @"$ROOT$";
rootGroup._parent = nil;
((Kdb3Tree *)tree)._root = rootGroup;
[rootGroup release];
//find the parent for every group
for(int i=0; i<[groups count]; i++){
Kdb3Group * group = [groups objectAtIndex:i];
level = [[levels objectAtIndex:i]unsignedIntValue];
if(level==0){
[rootGroup addSubGroup:group];
continue;
}
uint32_t level2;
int j;
//the first item with a lower level is the parent
for(j=i-1; j>=0; j--){
level2 = [[levels objectAtIndex:j]unsignedIntValue];
if(level2<level){
if(level-level2!=1)
@throw [NSException exceptionWithName:@"InvalidData" reason:@"InvalidTree" userInfo:nil];
else
break;
}
if(j==0)
@throw [NSException exceptionWithName:@"InvalidData" reason:@"InvalidTree" userInfo:nil];
}
Kdb3Group * parent = [groups objectAtIndex:j];
[parent addSubGroup:group];
}
return [tree autorelease];
}
@end