-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for LZNT1 decompression.
- Loading branch information
1 parent
6ed9d45
commit 30b1456
Showing
8 changed files
with
161 additions
and
2 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* | ||
* LZNT1 Decompress. | ||
* | ||
* @author 0xThiebaut [thiebaut.dev] | ||
* @copyright Crown Copyright 2023 | ||
* @license Apache-2.0 | ||
* | ||
* https://github.com/Velocidex/go-ntfs/blob/master/parser%2Flznt1.go | ||
*/ | ||
|
||
import Utils from "../Utils.mjs"; | ||
import OperationError from "../errors/OperationError.mjs"; | ||
|
||
const COMPRESSED_MASK = 1 << 15, | ||
SIZE_MASK = (1 << 12) - 1; | ||
|
||
/** | ||
* @param {number} offset | ||
* @returns {number} | ||
*/ | ||
function getDisplacement(offset) { | ||
let result = 0; | ||
while (offset >= 0x10) { | ||
offset >>= 1; | ||
result += 1; | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* @param {byteArray} compressed | ||
* @returns {byteArray} | ||
*/ | ||
export function decompress(compressed) { | ||
const decompressed = Array(); | ||
let coffset = 0; | ||
|
||
while (coffset + 2 <= compressed.length) { | ||
const doffset = decompressed.length; | ||
|
||
const block_header = Utils.byteArrayToInt(compressed.slice(coffset, coffset + 2), "little"); | ||
coffset += 2; | ||
|
||
const size = block_header & SIZE_MASK; | ||
const block_end = coffset + size + 1; | ||
|
||
if (size === 0) { | ||
break; | ||
} else if (compressed.length < coffset + size) { | ||
throw new OperationError("Malformed LZNT1 stream: Block too small! Has the stream been truncated?"); | ||
} | ||
|
||
if ((block_header & COMPRESSED_MASK) !== 0) { | ||
while (coffset < block_end) { | ||
let header = compressed[coffset++]; | ||
|
||
for (let mask_idx = 0; mask_idx < 8 && coffset < block_end; mask_idx++) { | ||
Check failure on line 58 in src/core/lib/LZNT1.mjs GitHub Actions / main
Check failure on line 58 in src/core/lib/LZNT1.mjs GitHub Actions / main
Check failure on line 58 in src/core/lib/LZNT1.mjs GitHub Actions / main
|
||
if ((header & 1) === 0) { | ||
decompressed.push(compressed[coffset++]); | ||
} else { | ||
const pointer = Utils.byteArrayToInt(compressed.slice(coffset, coffset + 2), "little"); | ||
coffset += 2; | ||
|
||
const displacement = getDisplacement(decompressed.length - doffset - 1); | ||
const symbol_offset = (pointer >> (12 - displacement)) + 1; | ||
const symbol_length = (pointer & (0xFFF >> displacement)) + 2; | ||
const shift_offset = decompressed.length - symbol_offset; | ||
|
||
for (let shift_delta = 0; shift_delta < symbol_length + 1; shift_delta++) { | ||
const shift = shift_offset + shift_delta; | ||
if (shift < 0 || decompressed.length <= shift) { | ||
throw new OperationError("Malformed LZNT1 stream: Invalid shift!"); | ||
} | ||
decompressed.push(decompressed[shift]); | ||
} | ||
} | ||
header >>= 1; | ||
} | ||
} | ||
} else { | ||
decompressed.push(...compressed.slice(coffset, coffset + size + 1)); | ||
coffset += size + 1; | ||
} | ||
} | ||
|
||
return decompressed; | ||
} |
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,41 @@ | ||
/** | ||
* @author 0xThiebaut [thiebaut.dev] | ||
* @copyright Crown Copyright 2023 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation.mjs"; | ||
import {decompress} from "../lib/LZNT1.mjs"; | ||
|
||
/** | ||
* LZNT1 Decompress operation | ||
*/ | ||
class LZNT1Decompress extends Operation { | ||
|
||
/** | ||
* LZNT1 Decompress constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "LZNT1 Decompress"; | ||
this.module = "Compression"; | ||
this.description = "Decompresses data using the LZNT1 algorithm.<br><br>Similar to the Windows API <code>RtlDecompressBuffer</code>."; | ||
this.infoURL = "https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-xca/5655f4a3-6ba4-489b-959f-e1f407c52f15"; | ||
this.inputType = "byteArray"; | ||
this.outputType = "byteArray"; | ||
this.args = []; | ||
} | ||
|
||
/** | ||
* @param {byteArray} input | ||
* @param {Object[]} args | ||
* @returns {byteArray} | ||
*/ | ||
run(input, args) { | ||
return decompress(input); | ||
} | ||
|
||
} | ||
|
||
export default LZNT1Decompress; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* LZNT1 Decompress tests. | ||
* | ||
* @author 0xThiebaut [thiebaut.dev] | ||
* @copyright Crown Copyright 2023 | ||
* @license Apache-2.0 | ||
*/ | ||
import TestRegister from "../../lib/TestRegister.mjs"; | ||
|
||
TestRegister.addTests([ | ||
{ | ||
name: "LZNT1 Decompress", | ||
input: "\x1a\xb0\x00compress\x00edtestda\x04ta\x07\x88alot", | ||
expectedOutput: "compressedtestdatacompressedalot", | ||
recipeConfig: [ | ||
{ | ||
op: "LZNT1 Decompress", | ||
args: [] | ||
} | ||
], | ||
} | ||
]); |