File corrupt after BinaryRead/Write / FileStream.Read/Write /File.Read/WriteAllBytes
By : forhad
Date : March 29 2020, 07:55 AM
it helps some times Your encryption method is not invertible, and so you cannot reliably decrypt a general binary message with it. The problem occurs because it is possible for two different plaintext values to encrypt to the same ciphertext value, and when this happens there's no way for you to determine when decrypting which was the correct input value. Example: code :
if inByte = 100 and keyByte = 100:
ApplyVernam(100, 100) = 100
if inByte = 0, keyByte = 100:
ApplyVernam(0, 100) = 100
this.ResBytes[x] = this.ApplyVernam(this.FileBytes[x], this.FileBytes[1]);
this.ResBytes[x] = x == 1 ? this.FileBytes[x] : this.ApplyVernam(this.FileBytes[x], this.FileBytes[1]);
|
Read pattern from file and write to another file using python
By : user2172181
Date : March 29 2020, 07:55 AM
should help you out You can use this regular expression, assuming all functions start on their own line (function SomeName()\n) and end on a new line (\n}), as in your example: code :
import re
with open('file.js', 'r') as f:
content = f.read()
functions = re.findall(r'(function\s.+?\(.*?\)\n.+?\n\})', content, re.DOTALL)
for i, func in enumerate(functions):
with open('func{}.js'.format(i), 'w') as f:
f.write(func)
function Script1(){
var player = GetPlayer();
}
|
Powershell Read File, Search for Pattern Delete the Pattern, Write out to same or new file
By : neepo
Date : March 29 2020, 07:55 AM
may help you . I can only guess what you are after. See an explanation of the REgEx used on https://regex101.com/r/qen2VS/1 code :
## Q:\Test\2018\06\10\SO_50777935.ps1
$Text=@"
AAA0000XYZZ
BBB0001H0351
CXXXXX ABCUABSS 22/11/1990
YYY0001H03510000001
ZZZ0000XYZZ
AAA0001XYZZ
BBB0001H9561
CXXXXX ABCUABSS 22/11/1990
YYY0001H95610000001
ZZZ0001XYZZ
"@
$Pattern ="(?s)BBB\d+(H0351|H9561).*?YYY\d+\1\d+\r?\n"
$text -replace $pattern
AAA0000XYZZ
ZZZ0000XYZZ
AAA0001XYZZ
ZZZ0001XYZZ
$Pattern ="(?s)BBB\d+(H0351|H9561).*?YYY\d+\1\d+\r?\n"
ForEach($File in (Get-ChildItem *.txt -File)){
(Get-Content $File -Raw) -Replace $Pattern | Set-Content $File
}
|
Android exception - ArrayIndexOutOfBoundsException kotlin when I write to file
By : user2786078
Date : March 29 2020, 07:55 AM
hop of those help? Problem is in the loop itself. -1 is returned when there is no more data
|
Read file, find pattern, edit and write back to same binary file
By : user2861356
Date : March 29 2020, 07:55 AM
may help you . I am trying to read a file which has several lines of data, modify one of the lines and write back the edits into the same file I read from. , Use this to modify according to pattern: code :
import re
from os import rename
path = r'/path/to/binary'
tmp_file = path + r'.mod'
pattern = b'\x55\x4F\x50\x44\x01\x00\x00\x00\x00\x00\x00\x00\x00\x06\x05\x08\x07'
new_pat = b'\x55\x4F\x50\x44\x01\x00\x00\x00\x00\x00\x00\x00\x00\x06\x05\xde\xad'
myre = re.compile(pattern)
with open(path, 'rb') as read_file, open(tmp_file, 'wb') as write_file:
write_file.write(myre.sub(new_pat, read_file.read()))
rename(tmp_file, path)
import struct
from collections import defaultdict
from os import rename
path = r'/tmp/rando_bin'
tmp_file = path + r'.mod'
location_start_hex = 0
location_end_hex = 8
fill_value = 0
mapper = defaultdict(set)
for hex_location in range(location_start_hex, location_end_hex + 1):
mapper[hex_location // 2].add('right' if hex_location % 2 else 'left')
with open(path, 'rb') as read_file, open(tmp_file, 'wb') as write_file:
byte_cnt = 0
x = read_file.read(1)
while x:
if byte_cnt in mapper:
if len(mapper[byte_cnt]) == 2:
write_file.write(bytearray([fill_value]))
elif 'left' in mapper[byte_cnt]:
y = struct.unpack('B', x)[0]
new_byte = y & int('00001111', 2)
write_file.write(bytearray([new_byte]))
else:
y = struct.unpack('B', x)[0]
new_byte = y & int('11110000', 2)
write_file.write(bytearray([new_byte]))
else:
write_file.write(x)
byte_cnt += 1
x = read_file.read(1)
rename(tmp_file, path)
|