gpt4 book ai didi

Powershell 二进制 grep

转载 作者:行者123 更新时间:2023-12-02 23:17:13 28 4
gpt4 key购买 nike

有没有办法在powershell中确定指定的文件是否包含指定的字节数组(在任何位置)?

就像是:

fgrep --binary-files=binary "$data" "$filepath"

当然,我可以写一个幼稚的实现:
function posOfArrayWithinArray {
param ([byte[]] $arrayA, [byte[]]$arrayB)
if ($arrayB.Length -ge $arrayA.Length) {
foreach ($pos in 0..($arrayB.Length - $arrayA.Length)) {
if ([System.Linq.Enumerable]::SequenceEqual(
$arrayA,
[System.Linq.Enumerable]::Skip($arrayB, $pos).Take($arrayA.Length)
)) {return $pos}
}
}
-1
}

function posOfArrayWithinFile {
param ([byte[]] $array, [string]$filepath)
posOfArrayWithinArray $array (Get-Content $filepath -Raw -AsByteStream)
}

// They return position or -1, but simple $false/$true are also enough for me.

— 但它是 减缓。

最佳答案

对不起,补充答案。这样做并不常见,但普遍问题引起了我的兴趣,我最初的“using -Like”答案的方法和信息完全不同。顺便说一句,如果您正在寻找对“我相信它必须存在于 .NET 中”问题的积极回应以接受答案,那么它可能不会发生,StackOverflow 搜索与 C# 相结合存在同样的问题。 , .NetLinq .
无论如何,事实上没有人能够找到唯一的假设 .Net到目前为止,有几个 semi-.Net 是可以理解的。正在使用解决方案,但我相信这会导致通用功能的一些不受欢迎的开销。
假设您字节数组 (正在搜索的字节数组)和 SearchArray (要搜索的字节数组)是完全随机的。 中的每个字节只有 1/256 的机会。字节数组 将匹配 的第一个字节SearchArray .在这种情况下,您不必进一步查看,如果匹配,则第二个字节也匹配的可能性为 1/2562,依此类推。这意味着内循环将仅运行大约 。 1.004 是外循环的倍数。换句话说,内循环之外(但在外循环中)的所有性能几乎与内循环中的性能一样重要!
请注意,这也意味着 500Kb 随机序列存在于 100Mb 随机序列中的可能性几乎为零。 (那么,您给定的二进制序列实际上有多随机?,如果它们远非随机,我认为您需要在问题中添加更多细节)。对于我的假设,更糟糕的情况是 字节数组 存在相同的字节(例如 0, 0, 0, ..., 0, 0, 0 )和 SearchArray 以不同字节结尾的相同字节(例如 0, 0, 0, ..., 0, 0, 1 )。
基于此,它再次表明(我也在其他一些答案中证明了这一点) native PowerShell 命令并没有那么糟糕,甚至可能超过 .Net/ Linq 在某些情况下命令。在我的测试中,以下 Find-Bytes函数大约是您问题中函数的 20% 到两倍:
查找字节
返回 -Search 所在位置的索引字节序列在 -Bytes 中找到字节序列。如果未找到搜索序列 $Null ( [System.Management.Automation.Internal.AutomationNull]::Value ) 被返回。
参数 -Bytes要搜索的字节数组-Search要搜索的字节数组-Start定义在 Bytes 中开始搜索的位置序列(默认: 0 )-All默认情况下,只会返回找到的第一个索引。使用 -All switch 返回找到的任何其他搜索序列的剩余索引。

Function Find-Bytes([byte[]]$Bytes, [byte[]]$Search, [int]$Start, [Switch]$All) {
For ($Index = $Start; $Index -le $Bytes.Length - $Search.Length ; $Index++) {
For ($i = 0; $i -lt $Search.Length -and $Bytes[$Index + $i] -eq $Search[$i]; $i++) {}
If ($i -ge $Search.Length) {
$Index
If (!$All) { Return }
}
}
}
用法示例:
$a = [byte[]]("the quick brown fox jumps over the lazy dog".ToCharArray())
$b = [byte[]]("the".ToCharArray())

Find-Bytes -all $a $b
0
31
基准
请注意,您应该打开一个新的 PowerShell session 以正确地对此进行基准测试,因为 Linq 使用了一个不适用于您的用例的大型缓存。
$a = [byte[]](&{ foreach ($i in (0..500Kb)) { Get-Random -Maximum 256 } })
$b = [byte[]](&{ foreach ($i in (0..500)) { Get-Random -Maximum 256 } })

Measure-Command {
$y = Find-Bytes $a $b
}

Measure-Command {
$x = posOfArrayWithinArray $b $a
}

关于Powershell 二进制 grep,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62400436/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com