Vor*_*rac 3 bash shell-script binary files
我需要大量损坏的.png
文件来测试我的项目。为此,我需要将所有字节从 0x054-th 到 0xa00-th 设置为 0。
.png
文件包含带有校验和的块,我想更改图像数据块 (IDAT),而不更新校验和。此外,我想损坏大量字节,以便在显示图像时出现可见(黑色)区域(前提是查看程序忽略校验和不匹配)。
这是我到目前为止所得到的:
#!/bin/sh
# This script reads all .png or .PNG files in the current folder,
# sets all bytes at offsets [0x054, 0xa00] to 0
# and overwrites the files back.
temp_file_ascii=outfile.txt
temp_file_bin=outfile.png
target_dir=.
start_bytes=0x054
stop_bytes=0xa00
len_bytes=$stop_bytes-$start_bytes
for file in $(find "$target_dir" -name "*.png") #TODO: -name "*.PNG")
do
# Copy first part of the file unchanged.
xxd -p -s $start_bytes "$file" > $temp_file_ascii
# Create some zero bytes, followed by 'a',
# because I don't know how to add just zeroes.
echo "$len_bytes: 41" | xxd -r >> $temp_file_ascii
# Copy the rest of the input file.
# ??
mv outfile.png "$file"
done
Run Code Online (Sandbox Code Playgroud)
编辑:完成的脚本,使用接受的答案:
#!/bin/sh
if [ "$#" != 3 ]
then
echo "Usage: "
echo "break_png.sh <target_dir> <start_offset> <num_zeroed_bytes>"
exit
fi
for file in $(find "$1" -name "*.png")
do
dd if=/dev/zero of=$file bs=1 seek=$(($2)) count=$(($3)) conv=notrunc
done
Run Code Online (Sandbox Code Playgroud)
你可以做一些更简单的事情dd
:
dd if=/dev/zero \
of="$your_target_file" \
bs=1 \
seek="$((start_offset))" \
count="$((num_zeros))" \
conv=notrunc
Run Code Online (Sandbox Code Playgroud)
与$start_offset
被你的字节范围的开始到(从n从零开始,如在以擦除归零个字节,使用N-1),以及$num_zeros
该范围的长度。在$((...))
将十六进制转换为十进制的照顾。
(您可以运行的其他测试将设置if
为/dev/urandom
而不是/dev/zero
,或者也用随机数据覆盖校验和。)