-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite parser for osx-storage from js to ts and simplify it
In previous implementation each entity was separated on lines but currently we get chunks with all lines for each entity. I have to extends Transform this way because of: microsoft/TypeScript#17032 Use _final instead of _flush to avoid an error: stream.push() after EOF Add dependency injection for osx parse.
- Loading branch information
Showing
7 changed files
with
140 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
"use strict"; | ||
|
||
const stream = require("readable-stream"); | ||
const Transform = stream.Transform; | ||
import {injectable} from "inversify"; | ||
|
||
@injectable() | ||
export class OsxSecurityParsingStreamWrapper { | ||
private readonly instance: OsxSecurityParsingStream; | ||
|
||
constructor() { | ||
this.instance = new OsxSecurityParsingStream(); | ||
} | ||
|
||
public get parser(): OsxSecurityParsingStream { | ||
return this.instance; | ||
} | ||
} | ||
|
||
export class OsxSecurityParsingStream extends (Transform as { new(): any; }) { | ||
|
||
private readonly rootFieldMask = /^([^:]+):(?: (?:"([^"]+)")|(.*))?$/; | ||
private readonly propertiesMask = /^ {4}(?:(0x[0-9a-fA-F]+) |"([a-z]{4})")<[^>]+>=(?:(<NULL>)|"([^"]+)"|(0x[0-9a-fA-F]+)(?: {2}"([^"]+)")|(.*)?)/; | ||
|
||
private currentKeychain: string; | ||
private hadAttributesString: boolean = false; | ||
|
||
public constructor() { | ||
super(); | ||
Transform.call(this, { | ||
objectMode: true | ||
}); | ||
|
||
this.currentEntry = undefined; | ||
} | ||
|
||
public _transform(chunk, encoding, callback) { | ||
const chunkData: string = chunk.toString(); | ||
if (!chunkData && chunkData === "") { | ||
callback(); | ||
return; | ||
} | ||
chunkData.split("\n").forEach((line) => { | ||
this.processNextLine(line); | ||
}); | ||
|
||
callback(); | ||
} | ||
|
||
private processNextLine(line: string): void { | ||
if (!this.currentKeychain) { | ||
this.currentKeychain = this.getInitialKeychainValue(line); | ||
if (this.currentKeychain) { | ||
this.currentEntry = { | ||
keychain: this.currentKeychain | ||
}; | ||
} | ||
} else { | ||
if (!this.hadAttributesString) { | ||
if (this.isAttributesString(line)) { | ||
this.hadAttributesString = true; | ||
} else { | ||
this.collectMetaInfo(line); | ||
} | ||
} else { | ||
this.collectProperties(line); | ||
} | ||
} | ||
} | ||
|
||
private getInitialKeychainValue(line: string): string { | ||
let keychain: string; | ||
let match = this.rootFieldMask.exec(line); | ||
if (match && match[1] === "keychain") { | ||
keychain = match[2]; | ||
} | ||
return keychain; | ||
} | ||
|
||
private isAttributesString(line: string): boolean { | ||
const match = this.rootFieldMask.exec(line); | ||
return match && match[1] === "attributes"; | ||
} | ||
|
||
private collectMetaInfo(line: string): void { | ||
let match = this.rootFieldMask.exec(line); | ||
if (match) { | ||
this.currentEntry[match[1]] = match[2]; | ||
} | ||
} | ||
|
||
private collectProperties(line: string): void { | ||
const match = this.propertiesMask.exec(line); | ||
if (match) { | ||
if (match[2]) { | ||
const value = match[6] || match[4]; | ||
if (value) { | ||
this.currentEntry[match[2]] = value; | ||
} | ||
} | ||
} else { | ||
this.finalize(line); | ||
} | ||
} | ||
|
||
private finalize(line ?:string): void { | ||
if (this.currentEntry) { | ||
this.push(this.currentEntry); | ||
this.currentEntry = undefined; | ||
this.currentKeychain = undefined; | ||
this.hadAttributesString = false; | ||
if (line) { | ||
this.processNextLine(line); | ||
} | ||
} | ||
} | ||
|
||
public _final(callback) { | ||
this.finalize(); | ||
callback(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters