diff --git a/tools/bits-info.sh b/tools/bits-info.sh index 779e4764..5bd87cce 100755 --- a/tools/bits-info.sh +++ b/tools/bits-info.sh @@ -248,32 +248,42 @@ file_getprop() grep -m 1 -F -e "${1:?}=" -- "${2:?}" | cut -d '=' -f '2-' -s } +switch_endianness() +{ + local _hex_bytes _switch_cur_line 2> /dev/null + + _hex_bytes="$(cat | grep -o -e '..')" || return "${?}" + + for _switch_cur_line in 4 3 2 1; do + printf '%s\n' "${_hex_bytes:?}" | head -n "${_switch_cur_line:?}" | tail -n "+${_switch_cur_line:?}" | tr -d '\n' || return "${?}" + done + printf '\n' +} + +dump_hex() +{ + if command 1> /dev/null 2>&1 -v hexdump; then + hexdump -v -e '/1 "%02x"' -s "${3:?}" -n "${2:?}" -- "${1:?}" + else + xxd -p -s "${3:?}" -l "${2:?}" -- "${1:?}" + fi +} + check_bitness_of_pe_file() { local _pe_header_pos _pe_header 2> /dev/null -echo '---' -echo ":_${1:?}_" -test -e "${1:?}"; echo "Exist: $?" -command -v hexdump || true; echo "hexdump: $?" -command -v od || true; echo "od: $?" -command -v xxd || true; echo "xxd: $?" -command -v tac || true; echo "tac: $?" -command -v tr || true; echo "tr: $?" -echo '---' - if test ! -e "${1:?}" || ! command -v hexdump; then - : #printf '%s\n' 'unknown' - #return 1 + if test ! -e "${1:?}" || { + ! command 1> /dev/null 2>&1 -v hexdump && ! command 1> /dev/null 2>&1 -v xxd + }; then + printf '%s\n' 'unknown' + return 1 fi # More info: https://learn.microsoft.com/en-us/windows/win32/debug/pe-format -echo '---' -hexdump -v -s 0x3C -n 4 -e '/1 "%02x\n"' -- "${1:?}" | tac | tr -d '\n'; printf "\n$?\n" - if _pe_header_pos="$(hexdump -v -s 0x3C -n 4 -e '/1 "%02x\n"' -- "${1:?}" | tac | tr -d '\n')" && test -n "${_pe_header_pos?}" && _pe_header_pos="0x${_pe_header_pos:?}"; then -echo '---' -hexdump -v -s "${_pe_header_pos:?}" -n 6 -e '/1 "%02x"' -- "${1:?}"; printf "\n$?\n" - if _pe_header="$(hexdump 2> /dev/null -v -s "${_pe_header_pos:?}" -n 6 -e '/1 "%02x"' -- "${1:?}")" && printf '%s\n' "${_pe_header?}" | grep -m 1 -q -e '^50450000'; then + if _pe_header_pos="$(dump_hex "${1:?}" '4' '0x3C' | switch_endianness)" && test -n "${_pe_header_pos?}" && _pe_header_pos="0x${_pe_header_pos:?}"; then + if _pe_header="$(dump_hex "${1:?}" '6' "${_pe_header_pos:?}")" && printf '%s\n' "${_pe_header?}" | grep -m 1 -q -e '^50450000'; then # PE header => PE (0x50 0x45) + 0x00 0x00 + Machine field case "${_pe_header?}" in *'6486') printf '%s\n' '64-bit PE' ;; # AMD64 (0x64 0x86) @@ -284,7 +294,6 @@ hexdump -v -s "${_pe_header_pos:?}" -n 6 -e '/1 "%02x"' -- "${1:?}"; printf "\n$ return 2 ;; esac -echo '---' return 0 fi fi @@ -316,18 +325,14 @@ main() _limits_date='32767 2147480047 2147483647 32535215999 32535244799 67767976233529199 67767976233532799 67768036191673199 67768036191676799 9223372036854775807' _limits_u='65535 2147483647 2147483648 4294967295 18446744073709551615' - shell_exe="$(readlink 2> /dev/null "/proc/${$}/exe")" || shell_exe='x' + shell_exe="$(readlink 2> /dev/null "/proc/${$}/exe")" || shell_exe='' shell_info="$(get_shell_info || true)" shell_name="$(printf '%s\n' "${shell_info:?}" | cut -d ' ' -f '1' || true)" -echo "_${shell_exe?}_" -ls -l /proc/$$/path/a.out || true -stat /proc/$$/path/a.out || true -readlink /proc/$$/path/a.out || true -echo '===' - test -n "${shell_exe?}" && check_bitness_of_pe_file "${shell_exe:?}" +echo '---' + if test -n "${shell_exe?}" && tmp_var="$(hexdump 2> /dev/null -v -n 5 -e '/1 "%02x"' -- "${shell_exe:?}")" && test -n "${tmp_var?}" && printf '%s\n' "${tmp_var:?}" | grep -m 1 -q -e '^7f454c46'; then # On Linux / Android # ELF header => 0x7F + ELF (0x45 0x4C 0x46) + 0x01 for 32-bit or 0x02 for 64-bit @@ -336,8 +341,8 @@ test -n "${shell_exe?}" && check_bitness_of_pe_file "${shell_exe:?}" *'01') shell_bit='32-bit ELF' ;; *) shell_bit='unknown' ;; esac - #elif test -n "${shell_exe?}" && shell_bit="$(check_bitness_of_pe_file "${shell_exe:?}")"; then - #: + elif test -n "${shell_exe?}" && shell_bit="$(check_bitness_of_pe_file "${shell_exe:?}")"; then + : elif tmp_var="$(uname 2> /dev/null -m)"; then case "${tmp_var?}" in x86_64 | ia64 | arm64 | aarch64 | mips64) shell_bit='64-bit' ;;