-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaseGenerator_trace_disk_partition.pas
More file actions
351 lines (310 loc) · 12.2 KB
/
Copy pathcaseGenerator_trace_disk_partition.pas
File metadata and controls
351 lines (310 loc) · 12.2 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
unit caseGenerator_trace_disk_partition;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.DateTimeCtrls, FMX.Calendar, FMX.Edit, FMX.StdCtrls, FMX.Layouts,
FMX.ListBox, FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo,
System.JSON.Readers, System.JSON.Types, System.JSON, caseGenerator_util;
type
TformTraceDiskPartition = class(TForm)
Label1: TLabel;
Label2: TLabel;
btnClose: TButton;
btnAddTrace: TButton;
btnDeleteTrace: TButton;
lbTrace: TListBox;
panelFile: TPanel;
Label6: TLabel;
Label3: TLabel;
panelHash: TPanel;
Label9: TLabel;
Label10: TLabel;
cbHashMethod: TComboBox;
Label11: TLabel;
edHashValue: TEdit;
Label12: TLabel;
edHashSize: TEdit;
cbType: TComboBox;
Label13: TLabel;
edOffset: TEdit;
Label14: TLabel;
edLength: TEdit;
lbPartition: TListBox;
btnAddPartition: TButton;
btnRemovePartition: TButton;
btnCancel: TButton;
btnModifyTrace: TButton;
edID: TEdit;
procedure btnAddTraceClick(Sender: TObject);
procedure btnDeleteTraceClick(Sender: TObject);
procedure btnCloseClick(Sender: TObject);
procedure btnAddPartitionClick(Sender: TObject);
procedure btnRemovePartitionClick(Sender: TObject);
procedure lbTraceChange(Sender: TObject);
procedure btnCancelClick(Sender: TObject);
procedure btnModifyTraceClick(Sender: TObject);
private
FuuidCase: string;
FpathCase: String;
procedure SetuuidCase(const Value: string);
procedure SetpathCase(const Value: String);
property uuidCase: string read FuuidCase write SetuuidCase;
property pathCase: String read FpathCase write SetpathCase;
function JsonTokenToString(const t: TJsonToken): string;
function prepareTrace(operation: String): String;
{ Private declarations }
public
procedure ShowWithParamater(pathCase: String; uuidCase: String);
{ Public declarations }
end;
var
formTraceDiskPartition: TformTraceDiskPartition;
implementation
{$R *.fmx}
uses StrUtils;
{ TForm1 }
procedure TformTraceDiskPartition.btnDeleteTraceClick(Sender: TObject);
begin
lbTrace.Items.Delete(lbTrace.ItemIndex);
end;
procedure TformTraceDiskPartition.btnModifyTraceClick(Sender: TObject);
begin
if lbTrace.ItemIndex > - 1 then
lbTrace.Items[lbTrace.ItemIndex] := prepareTrace('modify');
end;
procedure TformTraceDiskPartition.btnRemovePartitionClick(Sender: TObject);
begin
if lbPartition.ItemIndex > - 1 then
lbPartition.Items.Delete(lbPartition.ItemIndex);
end;
function TformTraceDiskPartition.JsonTokenToString(const t: TJsonToken): string;
begin
case t of
TJsonToken.None: Result := 'None';
TJsonToken.StartObject: Result := 'StartObject' ;
TJsonToken.StartArray: Result := 'StartArray' ;
TJsonToken.StartConstructor: Result := 'StartConstructor' ;
TJsonToken.PropertyName: Result := 'PropertyName' ;
TJsonToken.Comment: Result := 'Comment' ;
TJsonToken.Raw: Result := 'Raw' ;
TJsonToken.Integer: Result := 'Integer' ;
TJsonToken.Float: Result := 'Float' ;
TJsonToken.String: Result := 'String' ;
TJsonToken.Boolean: Result := 'Boolean' ;
TJsonToken.Null: Result := 'Null' ;
TJsonToken.Undefined: Result := 'Undefined' ;
TJsonToken.EndObject: Result := 'EndObject' ;
TJsonToken.EndArray: Result := 'EndArray' ;
TJsonToken.EndConstructor: Result := 'EndConstructor' ;
TJsonToken.Date: Result := 'Date' ;
TJsonToken.Bytes: Result := 'Bytes' ;
TJsonToken.Oid: Result := 'Oid' ;
TJsonToken.RegEx: Result := 'RegEx' ;
TJsonToken.DBRef: Result := 'DBRef' ;
TJsonToken.CodeWScope: Result := 'CodeWScope' ;
TJsonToken.MinKey: Result := 'MinKey' ;
TJsonToken.MaxKey: Result := 'MaxKey' ;
end;
end;
procedure TformTraceDiskPartition.lbTraceChange(Sender: TObject);
var
line, recSep, linePartition, partitionType: String;
nPosLength, idx: Integer;
begin
if lbTrace.ItemIndex > - 1 then
begin
recSep := #30 + #30;
line := lbTrace.Items[lbTrace.ItemIndex];
edHashValue.Text := ExtractField(line, '"uco-observable:hashValue":"');
edHashSize.Text := ExtractField(line, '"uco-observable:sizeInBytes":"');
partitionType := ExtractField(line, '"uco-observable:diskPartitionType":"');
(*
nPosLength := Pos('partitionLength',line);
while nPosLength > 0 do
begin
linePartition := '{"@type":"DiskPartition", ' + recSep;
linePartition := linePartition + '"diskPartitionType":"' + ExtractField(line, '"diskPartitionType":"') + '", ' + recSep;
linePartition := linePartition + '"partitionID":"' + ExtractField(line, '"partitionID":"') + '",' + recSep;
linePartition := linePartition + '"partitionOffset":"' + ExtractField(line, '"partitionOffset":"') + '",' + recSep;
linePartition := linePartition + '"partitionLength":"' + ExtractField(line, '"partitionLength":"') + '"}' + recSep;
lbPartition.Items.Add(linePartition);
line := Copy(line, nPosLength + 14, Length(line));
nPosLength := Pos('partitionLength',line);
end;
*)
end;
(* if lbPartition.Items.Count > 0 then *)
(* begin *)
edID.Text := ExtractField(line, '"partitionID":"');
edOffset.Text := ExtractField(line, '"partitionOffset":"');
edLength.Text := ExtractField(line, '"partitionLength":"');
(* end; *)
for idx:= 0 to cbType.Items.Count - 1 do
begin
if cbType.Items[idx]= partitionType then
begin
cbType.ItemIndex := idx;
break;
end;
end;
end;
function TformTraceDiskPartition.prepareTrace(operation: String): String;
var
line, recSep, indent, guidNoBraces, lineConfiguration, sConfiguration: string;
Uid: TGUID;
idx: integer;
begin
//cr := #13 +#10;
recSep := #30 + #30;
indent := ' ';
line := '{' + recSep;
idx := 0;
if operation = 'add' then
begin
CreateGUID(Uid);
guidNoBraces := 'kb:' + Copy(GuidToString(Uid), 2, Length(GuidToString(Uid)) - 2);
end
else
guidNoBraces := ExtractField(lbTrace.Items[lbTrace.ItemIndex], '"@id":"');
line := line + indent + '"@id":"' + guidNoBraces + '", ' + recSep;
line := line + indent + '"@type":"uco-observable:CyberItem",' + recSep;
line := line + indent + '"uco-core:facets":[' + recSep;
line := line + indent + '{' + recSep;
line := line + RepeatString(indent, 2) + '"@type":"uco-observable:DiskPartition", ' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:partitionID":"' + edID.Text + '",' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:partitionOffset":"' + edOffset.Text + '",' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable::partitionLength":"' + edLength.Text + '"' + recSep;
line := line + indent + '},' + recSep;
line := line + indent + '{';
line := line + RepeatString(indent, 2) + '"@type":uco-observable:FileSystem",' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:diskPartitionType":"' + cbType.Items[cbType.ItemIndex] + '" ' + recSep;
line := line + indent + '},';
line := line + indent + '{' + recSep;
line := line + RepeatString(indent, 2) + '"type":"uco-observable:ContentData",' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:SizeInBytes":"' + edHashSize.Text + '",' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:hash":[' + recSep;
line := line + RepeatString(indent, 2) + '{' + recSep;
line := line + RepeatString(indent, 2) + '"@type":"Hash",' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:hashMethod":"' + cbHashMethod.Items[cbHashMethod.ItemIndex] + '",' + recSep;
line := line + RepeatString(indent, 2) + '"uco-observable:hashValue":"' + edHashValue.Text + '"' + recSep;
line := line + indent + '}' + recSep;
line := line + indent + '] ' + recSep;
line := line + indent + '}' + recSep + ']' + recSep + '}';
Result := line;
end;
procedure TformTraceDiskPartition.btnCancelClick(Sender: TObject);
begin
formTraceDiskPartition.Close;
end;
procedure TformTraceDiskPartition.btnCloseClick(Sender: TObject);
var
fileJSON: TextFile;
line, recSep, crlf:string;
idx: integer;
begin
if lbTrace.Items.Count > 0 then
begin
crlf := #13 + #10;
recSep := #30 + #30;
idx:= 0;
//dir := GetCurrentDir;
AssignFile(fileJSON, FpathCase + FuuidCase + '-traceDISK_PARTITION.json');
Rewrite(fileJSON); // create new file
WriteLn(fileJSON, '{');
line := #9 + '"OBJECTS_DISK_PARTITION":[';
WriteLn(fileJSON, line);
for idx:= 0 to lbTrace.Items.Count - 2 do
WriteLn(fileJSON, lbTrace.Items[idx] + ',');
WriteLn(fileJSON, lbTrace.Items[idx]);
WriteLn(fileJSON, #9#9 + ']'); // it's important write in separate lines
WriteLn(fileJSON, #9#9 + '}');
// the last ] and } must be written on two separate lines !!!
//WriteLn(fileJSON, #9#9 + ']}');
CloseFile(fileJSON);
end
else
deleteFile(FpathCase + FuuidCase + '-traceDISK_PARTITION.json');
formTraceDiskPartition.Close;
end;
procedure TformTraceDiskPartition.btnAddPartitionClick(Sender: TObject);
var
recSep, line, indent : String;
begin
recSep := #30 + #30;
indent := ' ';
if (Trim(edID.Text) = '') or (Trim(edOffset.Text) = '') or (Trim(edLength.Text) = '') then
ShowMessage('ID and/or Offset and/or Length are missing')
else
begin
line := indent + '{' + recSep;
line := line + RepeatString(indent, 2) + '"@type":"DiskPartition", ' + recSep;
line := line + RepeatString(indent, 2) + '"partitionID":"' + edID.Text + '",' + recSep;
line := line + RepeatString(indent, 2) + '"partitionOffset":"' + edOffset.Text + '",' + recSep;
line := line + RepeatString(indent, 2) + '"partitionLength":"' + edLength.Text + '"' + recSep;
line := line + indent + '},' + recSep;
line := line + indent + '{';
line := line + RepeatString(indent, 2) + '"@type":"FileSystem",' + recSep;
line := line + RepeatString(indent, 2) + '"diskPartitionType":"' + cbType.Items[cbType.ItemIndex] + '" ' + recSep;
line := line + indent + '}';
lbPartition.Items.Add(line);
end;
end;
procedure TformTraceDiskPartition.btnAddTraceClick(Sender: TObject);
begin
if (Trim(edID.Text) = '') or (Trim(edHashValue.Text) = '') then
ShowMessage('Partition data and/or Hash value are missing!')
else
begin
lbTrace.Items.Add(prepareTrace('add'));
edID.Text := '';
edOffset.Text := '';
edLength.Text := '';
lbPartition.Items.Clear;
edHashValue.Text := '';
edHashSize.Text := '';
cbHashMethod.ItemIndex := 0;
end;
end;
procedure TformTraceDiskPartition.SetpathCase(const Value: String);
begin
FpathCase := Value;
end;
procedure TformTraceDiskPartition.SetuuidCase(const Value: string);
begin
FuuidCase := Value;
end;
procedure TformTraceDiskPartition.ShowWithParamater(pathCase: String; uuidCase: String);
var
fileJSON: TextFile;
line, subLine, dir:string;
begin
SetUuidCase(uuidCase);
SetPathCase(pathCase);
//dir := GetCurrentDir;
// read file JSON uuidCase-identity.json
if FileExists(FpathCase + FuuidCase + '-traceDISK_PARTITION.json') then
begin
AssignFile(fileJSON, FpathCase + FuuidCase + '-traceDISK_PARTITION.json');
Reset(fileJSON);
lbTrace.Items.Clear;
while not Eof(fileJSON) do
begin
ReadLn(fileJSON, line);
line := Trim(line);
if (line = '{') or (line = '}') or (line = ']') or (AnsiContainsStr(line, 'OBJECTS_')) then // first or last line or root element
else
begin
subLine := Copy(line, Length(line), 1); // rule out of comma
if subLine = ',' then
line := Copy(line, 1, Length(line) - 1);
lbTrace.Items.Add(line);
end;
end;
CloseFile(fileJSON);
end;
// else
// ShowMessage(dir + uuidCase + '-identity.json' + ' doesn''t exist');
formTraceDiskPartition.ShowModal;
end;
end.