LevT
Platinum Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору Мне копилот в бинге родил такое: Код: # Define your binary file $filePath = "Path to your binary file" # Read the binary file $content = [System.IO.File]::ReadAllBytes($filePath) # Define your pairs of hex patterns and corresponding replacements $hexPairs = @{ "0x123" = "0x789" "0xabc" = "0xdef" "0x456" = "0xghi" } # Convert the binary content to a hex string $hexString = [System.BitConverter]::ToString($content) -replace '-','' # Loop through each pair of hex pattern and its replacement and replace it in the hex string foreach ($hexPair in $hexPairs.GetEnumerator()) { $hexString = $hexString -replace $hexPair.Key, $hexPair.Value } # Convert the modified hex string back to a byte array $modifiedContent = [System.Linq.Enumerable]::Range(0, $hexString.Length - 1 | Where-Object { $_ % 2 -eq 0 } | ForEach-Object { [System.Convert]::ToByte($hexString.Substring($_, 2), 16) }) # Write the modified content back to the binary file [System.IO.File]::WriteAllBytes($filePath, $modifiedContent) | Добавлено: Последовательность запросов, уточняющих контекст: --1 powershell code. provide several hex patterns to match exactly. replace all matches with provided hex substitute in one run. --2 use pairs of search patterns and corresponding replacements --3 Modify binary file instead of fixed input string Можете сами поиграться в своих болталках, интересно было бы сравнить результаты Добавлено: Гуглобард родил такое (с незначительными модификациями промптов) Код: $replacementPairs = @( ("0xAB12CD", "0xDEADBEEF"), ("0xEF3456", "0xCAFEBABE"), ("0x1A2B3C", "0xFEEDFACE") ) # Define pattern-replacement pairs $data = Get-Content "your_file.txt" -Raw foreach ($pair in $replacementPairs) { $pattern = $pair[0] -replace "\\", "\\" # Escape backslashes in pattern $substitute = $pair[1] $data = $data -replace $pattern, $substitute } Set-Content "your_file.txt" $modifiedData -NoNewline | Альтернативный ответ гуглобарда (все промпты я объединил в один, чуть-чуть перефразировав) Код: $replacementPairs = @( ("0xAB12CD", "0xDEADBEEF"), ("0xEF3456", "0xCAFEBABE"), ("0x1A2B3C", "0xFEEDFACE") ) $data = [System.IO.File]::ReadAllBytes("your_binary_file.bin") foreach ($pair in $replacementPairs) { $pattern = $pair[0].ToCharArray() # Convert pattern to individual bytes $substitute = $pair[1].ToCharArray() # Convert substitute to individual bytes $offset = 0 while (($index = $data.IndexOf($pattern, $offset)) -ge 0) { $data[$index..($index + $pattern.Length - 1)] = $substitute $offset = $index + $pattern.Length } } [System.IO.File]::WriteAllBytes("your_binary_file.bin", $data) | Контексты у меня открыты, предлагайте свои уточнения Добавлено: Промпт: powershell code. Modify a binary file provide several hex patterns to match exactly. use pairs of search patterns and corresponding replacements evaluate every single match with provided hex substitute but don't apply immediately Combine all the replacements and apply them in a single pass гуглобард: Код: $replacementPairs = @( ("0xAB12CD", "0xDEADBEEF"), ("0xEF3456", "0xCAFEBABE"), ("0x1A2B3C", "0xFEEDFACE") ) $filePath = "your_binary_file.bin" # Replace with the actual file path $fileBytes = [System.IO.File]::ReadAllBytes($filePath) $modifiedBytes = $fileBytes $offset = 0 $replacementIndexes = @() foreach ($pair in $replacementPairs) { $patternBytes = [System.Text.Encoding]::ASCII.GetBytes($pair[0]) $substituteBytes = [System.Text.Encoding]::ASCII.GetBytes($pair[1]) while ($offset -lt $fileBytes.Length) { $index = $fileBytes.IndexOf($patternBytes, $offset) if ($index -ge 0) { $replacementIndexes += $index $offset = $index + $patternBytes.Length } else { break } } } $replacementIndexes = $replacementIndexes | Sort-Object -Descending foreach ($index in $replacementIndexes) { [Array]::Copy($substituteBytes, 0, $modifiedBytes, $index, $substituteBytes.Length) } [System.IO.File]::WriteAllBytes($filePath, $modifiedBytes) | бинг/копилот.... повис... пришлось сбросить контекст, в новом контексте задумалсо... и родил какую-то чушь, явно не поняв что от него теперь хотят По идее, Array::Copy предложенное наконец гуглобардом должно быть самым быстрым из-за оптимизаций внутри .NET CLR
| Всего записей: 17231 | Зарегистр. 14-10-2001 | Отправлено: 18:07 06-02-2024 | Исправлено: LevT, 19:01 06-02-2024 |
|