Skip to content

Instantly share code, notes, and snippets.

@nyango
Created December 30, 2015 08:39
Show Gist options
  • Save nyango/6cd8441edc16c060763a to your computer and use it in GitHub Desktop.
Save nyango/6cd8441edc16c060763a to your computer and use it in GitHub Desktop.
PNGファイルを読み込み評価する
#!/bin/bash
set -eu
pngFile=$1
headPtr=0
if [ "89504E470D0A1A0A" != $(xxd -ps -u -s $headPtr -l 8 $pngFile) ]
then
echo "pngファイルではありません"
exit 1
fi
headPtr=$((headPtr+8))
chunkName=""
while [ "IEND" != "$chunkName" ]
do
chunkLength=$((0x$(xxd -ps -u -s $headPtr -l 4 $pngFile)))
chunkName=$(dd if=$pngFile ibs=1 skip=$((headPtr+4)) count=4 2> /dev/null)
checkSum=$(xxd -ps -u -s $((headPtr+4+4+chunkLength)) -l 4 $pngFile)
printf "%04x: %s %04x\n" $headPtr $chunkName $chunkLength
recount=$(printf "%08X" 0x$(dd if=$pngFile ibs=1 skip=$((headPtr+4)) count=$((4+chunkLength)) 2> /dev/null | cksum -o3 | cut -d " " -f 1 | xargs -I ZZ dc -e "16oAi ZZ p"))
if [ "$recount" != "$checkSum" ]
then
echo "ファイルが壊れています"
echo $recount is not equal to $checkSum
fi
headPtr=$((headPtr+4+4+chunkLength+4)) #to next chunk
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment