#! /bin/sh
# file://~/bin/file-analyse.sh # @author: www.RzR.online.FR
#------------------------------------------------------------------------------
# @author:  www.Philippe.COVAL.online.FR - Rev:$Author: rzr$
# Copyright and Licence : http://RzR.online.fr/license.htm
# Version: 0.0.20040426 
# url: http://rzr.online.fr/linux.htm
#------------------------------------------------------------------------------

usage_ () 
{
cat<<EOF
#Usage: $0 [-options] <filename>
#Copyright & licence @ http://rzr.online.fr/licence.htm

# tries to guess what data types the file is made of
# Usefull for reverse engineering
EOF
}

# [ Press Return ]
#read t

size_()
{
 stat "$1" \
 | grep "Size:" | sed -e "s/ *Size: \([0-9]*\).*/\1/g" | head -1
}

file_analyze_iter_()
{
#echo "+ file_analyze_iter_ $*"
OFF=$1
T=${TMP_DIR}/${FILE}-${OFF}
dd ibs=1 if=${FILE} of=$T skip=${OFF}  2>> /dev/null
file ${T} | sed -e "s/.*${FILE}-\(.*\)/\1/g" | grep -v ": data$"
rm ${T} 2>> /dev/null
#echo "- file_analyze_iter_ $* - FILE=$FILE SIZE=$SIZE OFF=$OFF"
}
#main_ $*


file_analyze_()
{
FILE="$1"
OFF="$2"
SIZE="$3"
[ ! -z $2 ] && OFF=$2
[ -z $3 ] && SIZE=`size_ "${FILE}" `

#SIZE=`stat $FILE |grep "Size:" | sed -e "s/ *Size: \([0-9]*\).*/\1/g"|head -1 `


#TMP=/tmp/$(mktemp)-skip/$HOME/var/skip/
TMP_DIR=/tmp/$HOME/var/skip/$(mktemp)-${FILE}/
mkdir -p ${TMP_DIR} 2>> /dev/null


if [ ! -z $2 ] 
then
file_analyze_iter_ $OFF
else
OFF=0
while [  0${OFF} -le 0${SIZE} ] 
do 
file_analyze_iter_ ${OFF}
OFF=`expr ${OFF} + 1` 
done
fi
}

main_()
{
    case "$1" in
	"") 
            usage_;;
        -*)	
            usage_;;
        *)
            file_analyze_ $*
        break;;
    esac
}

main_ $*

#___[ EOF ]____________________________________________________________________

