Skip to content

Commit 6f32c3f

Browse files
committed
improved JSON
1 parent 09f444a commit 6f32c3f

File tree

8 files changed

+70
-161
lines changed

8 files changed

+70
-161
lines changed

AppDelegate.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,11 +1263,11 @@ -(NSMutableString*)scanToJSON
12631263
{
12641264

12651265
//iterate over all items
1266-
// ->convert to JSON/append to output
1266+
// convert to JSON/append to output
12671267
for(ItemBase* item in items)
12681268
{
12691269
//add item
1270-
[json appendFormat:@"{%@},", [item toJSON]];
1270+
[json appendFormat:@"%@,", [item toJSON]];
12711271

12721272
}//all plugin items
12731273

@@ -1468,7 +1468,7 @@ -(IBAction)compareScans:(id)sender
14681468
}
14691469

14701470
//convert to dictionary
1471-
addedItem = [[NSJSONSerialization JSONObjectWithData:[[NSString stringWithFormat:@"{%@}", [currentItem toJSON]] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil] mutableCopy];
1471+
addedItem = [[NSJSONSerialization JSONObjectWithData:[[NSString stringWithFormat:@"%@", [currentItem toJSON]] dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil] mutableCopy];
14721472
if(nil == addedItem)
14731473
{
14741474
//skip

Results/Command.m

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,31 @@ -(id)initWithParams:(NSDictionary*)params
2828
return self;
2929
}
3030

31-
//convert object to JSON string
31+
//convert obj to JSON
3232
-(NSString*)toJSON
3333
{
34-
//json string
35-
NSString *json = nil;
34+
NSString* json = nil;
35+
NSData* jsonData = nil;
36+
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
3637

37-
//init json
38-
// ->note: command is escaped to make sure its valid JSON
39-
json = [NSString stringWithFormat:@"\"command\": \"%@\", \"file\": \"%@\"", escapeString(self.command), self.path];
38+
dict[@"command"] = self.command ?: @"unknown";
39+
dict[@"file"] = self.path ?: @"unknown";
4040

41-
return json;
41+
@try
42+
{
43+
jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:NULL];
44+
if(jsonData) {
45+
json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
46+
}
47+
}
48+
@catch(NSException* exception)
49+
{
50+
json = @"{\"error\": \"serialization failed\"}";
51+
}
52+
53+
return json ?: @"{\"error\": \"serialization failed\"}";
4254
}
4355

44-
4556
//description
4657
-(NSString*)description
4758
{

Results/Extension.m

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,33 +36,32 @@ -(id)initWithParams:(NSDictionary*)params
3636
return self;
3737
}
3838

39-
//convert object to JSON string
39+
//convert obj to JSON
4040
-(NSString*)toJSON
4141
{
42-
//json string
43-
NSString *json = nil;
42+
NSString* json = nil;
43+
NSData* jsonData = nil;
44+
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
4445

45-
//name ...escaped
46-
NSString* escapedName = nil;
46+
dict[@"name"] = self.name ?: @"unknown";
47+
dict[@"path"] = self.path ?: @"unknown";
48+
dict[@"identifier"] = self.identifier ?: @"unknown";
49+
dict[@"details"] = self.details ?: @"unknown";
50+
dict[@"browser"] = self.browser ?: @"unknown";
4751

48-
//details ...escaped
49-
NSString* escapedDetails = nil;
50-
51-
//escape name
52-
escapedName = [self.name stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
53-
54-
//escape details
55-
// remove newlines
56-
escapedDetails = [[self.details componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];
57-
58-
//escape details
59-
// replace " with \"
60-
escapedDetails = [escapedDetails stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
61-
62-
//init json
63-
json = [NSString stringWithFormat:@"\"name\": \"%@\", \"path\": \"%@\", \"identifier\": \"%@\", \"details\": \"%@\", \"browser\": \"%@\"", escapedName, self.path, self.identifier, escapedDetails, self.browser];
52+
@try
53+
{
54+
jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:NULL];
55+
if(jsonData) {
56+
json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
57+
}
58+
}
59+
@catch(NSException* exception)
60+
{
61+
json = @"{\"error\": \"serialization failed\"}";
62+
}
6463

65-
return json;
64+
return json ?: @"{\"error\": \"serialization failed\"}";
6665
}
6766

6867
//description

Results/File.m

Lines changed: 25 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -289,111 +289,53 @@ -(NSString*)formatSigningInfo
289289
return prettyPrint;
290290
}
291291

292-
//convert object to JSON string
292+
//convert obj to JSON
293293
-(NSString*)toJSON
294294
{
295-
//json string
296-
NSString *json = nil;
295+
NSData* jsonData = nil;
296+
NSString* json = nil;
297+
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
297298

298-
//json data
299-
// ->for intermediate conversions
300-
NSData *jsonData = nil;
301-
302-
//plist
303-
NSString* filePlist = nil;
299+
//basic fields
300+
dict[@"name"] = self.name ?: @"unknown";
301+
dict[@"path"] = self.path ?: @"unknown";
302+
dict[@"plist"] = self.plist ?: @"n/a";
304303

305304
//hashes
306-
NSString* fileHashes = nil;
305+
dict[@"hashes"] = self.hashes ?: @"unknown";
307306

308307
//signing info
309-
NSString* fileSigs = nil;
310-
311-
//init file hash to default string
312-
// ->used when hashes are nil, or serialization fails
313-
fileHashes = @"\"unknown\"";
308+
dict[@"signature(s)"] = self.signingInfo ?: @"unknown";
314309

315-
//init file signature to default string
316-
// ->used when signatures are nil, or serialization fails
317-
fileSigs = @"\"unknown\"";
318-
319-
//convert hashes to JSON
320-
if(nil != self.hashes)
310+
//include VT?
311+
if( [NSProcessInfo.processInfo.arguments containsObject:@"-key"] &&
312+
![NSProcessInfo.processInfo.arguments containsObject:@"-skipVT"] )
321313
{
322-
//convert hash dictionary
323-
// ->wrap since we are serializing JSON
324-
@try
325-
{
326-
//convert
327-
jsonData = [NSJSONSerialization dataWithJSONObject:self.hashes options:kNilOptions error:NULL];
328-
if(nil != jsonData)
329-
{
330-
//convert data to string
331-
fileHashes = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
332-
}
333-
}
334-
//ignore exceptions
335-
// ->file hashes will just be 'unknown'
336-
@catch(NSException *exception)
337-
{
338-
;
339-
}
314+
dict[@"VT detection"] = [NSString stringWithFormat:@"%lu/%lu",
315+
(unsigned long)[self.vtInfo[VT_RESULTS_POSITIVES] unsignedIntegerValue],
316+
(unsigned long)[self.vtInfo[VT_RESULTS_TOTAL] unsignedIntegerValue]];
340317
}
341318

342-
//convert signing dictionary to JSON
343-
if(nil != self.signingInfo)
319+
//serialize
320+
@try
344321
{
345-
//convert signing dictionary
346-
// ->wrap since we are serializing JSON
347-
@try
348-
{
349-
//convert
350-
jsonData = [NSJSONSerialization dataWithJSONObject:self.signingInfo options:kNilOptions error:NULL];
351-
if(nil != jsonData)
352-
{
353-
//convert data to string
354-
fileSigs = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
355-
}
356-
}
357-
//ignore exceptions
358-
// ->file sigs will just be 'unknown'
359-
@catch(NSException *exception)
360-
{
361-
;
322+
jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:NULL];
323+
if(jsonData) {
324+
json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
362325
}
363326
}
364-
365-
//provide a default string if the file doesn't have a plist
366-
if(nil == self.plist)
367-
{
368-
//set
369-
filePlist = @"n/a";
370-
}
371-
//use plist as is
372-
else
373-
{
374-
//set
375-
filePlist = self.plist;
376-
}
377-
378-
//init json
379-
json = [NSString stringWithFormat:@"\"name\": \"%@\", \"path\": \"%@\", \"plist\": \"%@\", \"hashes\": %@, \"signature(s)\": %@", self.name, self.path, filePlist, fileHashes, fileSigs];
380-
381-
//include VT?
382-
// append detection
383-
if( [NSProcessInfo.processInfo.arguments containsObject:@"-key"] &&
384-
![NSProcessInfo.processInfo.arguments containsObject:@"-skipVT"] )
327+
@catch(NSException* exception)
385328
{
386-
//append VT detection
387-
json = [json stringByAppendingFormat:@", \"VT detection\": \"%lu/%lu\"", (unsigned long)[self.vtInfo[VT_RESULTS_POSITIVES] unsignedIntegerValue], (unsigned long)[self.vtInfo[VT_RESULTS_TOTAL] unsignedIntegerValue]];
329+
json = @"{\"error\": \"serialization failed\"}";
388330
}
389331

390-
return json;
332+
return json ?: @"{\"error\": \"serialization failed\"}";
391333
}
392334

393335
//description
394336
-(NSString*)description
395337
{
396-
return [NSString stringWithFormat:@"name: %@, path: %@, plist: %@, hashes: %@, signature(s): %@, VT detection: %@", self.name, self.path, self.plist, self.hashes, self.signingInfo, self.vtInfo];;
338+
return [NSString stringWithFormat:@"name: %@, path: %@, plist: %@, hashes: %@, signature(s): %@, VT detection: %@", self.name, self.path, self.plist, self.hashes, self.signingInfo, self.vtInfo];
397339
}
398340

399341

Results/ItemBase.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,4 @@
4343
//convert object to JSON string
4444
-(NSString*)toJSON;
4545

46-
47-
48-
4946
@end

main.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ void cmdlineScan(NSArray* args)
278278
if(0 == (unsigned long)plugin.allItems.count)
279279
{
280280
//msg
281-
printf(" No %s found\n", plugin.name.UTF8String);
281+
printf(" no %s found\n", plugin.name.UTF8String);
282282
}
283283
//found items
284284
else
@@ -324,7 +324,7 @@ void cmdlineScan(NSArray* args)
324324
displayedItems++;
325325

326326
//add item
327-
[output appendFormat:@"{%@},", [item toJSON]];
327+
[output appendFormat:@"%@,", [item toJSON]];
328328
}
329329

330330
//remove last ','

utilities.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@ NSData* execTask(NSString* binaryPath, NSArray* arguments, int* exitCode);
5858
//check if computer has network connection
5959
BOOL isNetworkConnected(void);
6060

61-
//escape \ and "s in a string
62-
NSString* escapeString(NSString* unescapedString);
63-
6461
//find a constraint (by name) of a view
6562
NSLayoutConstraint* findConstraint(NSView* view, NSString* constraintName);
6663

utilities.m

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -723,43 +723,6 @@ BOOL isNetworkConnected(void)
723723
return isConnected;
724724
}
725725

726-
//escape \ and "s in a string
727-
NSString* escapeString(NSString* unescapedString)
728-
{
729-
//return string
730-
NSMutableString *escapedString = nil;
731-
732-
//char
733-
unichar c = 0;
734-
735-
//alloc escaped string
736-
escapedString = [[NSMutableString alloc] init];
737-
738-
//check each char
739-
// ->escape as needed
740-
for(int i = 0; i < [unescapedString length]; i++) {
741-
742-
//grab char
743-
c = [unescapedString characterAtIndex:i];
744-
745-
//escape chars
746-
if( ('\'' == c) ||
747-
('"' == c) )
748-
{
749-
//escape
750-
[escapedString appendFormat:@"\\%c", c];
751-
}
752-
//no need to escape
753-
else
754-
{
755-
//use as is
756-
[escapedString appendFormat:@"%c", c];
757-
}
758-
}
759-
760-
return escapedString;
761-
}
762-
763726
//find a constraint (by name) of a view
764727
NSLayoutConstraint* findConstraint(NSView* view, NSString* constraintName)
765728
{

0 commit comments

Comments
 (0)