Skip to content

Commit

Permalink
Improved compatibility of random generation, ref #91
Browse files Browse the repository at this point in the history
  • Loading branch information
ale5000-git committed Apr 20, 2024
1 parent 017a51c commit fe67169
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions zip-content/META-INF/com/google/android/update-binary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set -u || true

export OUTFD="${2:?}"
export ZIPFILE="${3:?}"
unset RANDOM_IS_SEEDED

### PREVENTIVE CHECKS ###

Expand Down Expand Up @@ -135,22 +136,29 @@ package_extract_file()
if ! test -e "${2:?}"; then ui_error "Failed to extract the file '${1}' from this archive"; fi
}

seed_random_generator()
{
# Seed the RANDOM variable
RANDOM="${$:?}${$:?}"
}

generate_random()
{
local _seed

if test "${RANDOM_IS_SEEDED:-false}" = 'false'; then
# Seed the RANDOM variable
RANDOM="${$:?}${$:?}"
RANDOM_IS_SEEDED='true'
fi

# shellcheck disable=SC3028
LAST_RANDOM="${RANDOM?}" # Both BusyBox and Toybox support ${RANDOM}
LAST_RANDOM="${RANDOM-}"

if test "${LAST_RANDOM:?}" != "${$:?}${$:?}"; then
if test -n "${LAST_RANDOM?}" && test "${LAST_RANDOM:?}" != "${$:?}${$:?}"; then
: # OK
elif command 1> /dev/null -v shuf && LAST_RANDOM="$(shuf -n '1' -i '0-32767')"; then
: # OK
elif command 1> /dev/null -v awk && command 1> /dev/null -v date && _seed="$(LC_ALL=C date -u -- '+%N')" && test -n "${_seed?}" && test "${_seed:?}" != 'N' && LAST_RANDOM="$(awk -v seed="${_seed:?}" -- 'BEGIN { srand(seed); print int( rand()*(32767+1) ) }')"; then
: # OK
elif test -e '/dev/urandom' && command 1> /dev/null -v tr && command 1> /dev/null -v head && LAST_RANDOM="$(tr 0< '/dev/urandom' 2> /dev/null -d -c '[:digit:]' | head -c 5)"; then
elif test -e '/dev/urandom' && command 1> /dev/null -v tr && command 1> /dev/null -v head && LAST_RANDOM="$(tr 0< '/dev/urandom' 2> /dev/null -d -c '[:digit:]' | head -c 5)" && test -n "${LAST_RANDOM?}"; then
: # OK
else
LAST_RANDOM=''
ui_error 'Unable to generate a random number'
fi
}
Expand Down Expand Up @@ -206,7 +214,6 @@ _ub_we_mounted_tmp=false
export TMPDIR
}

seed_random_generator
generate_random
_ub_our_main_script="${TMPDIR:?}/${LAST_RANDOM:?}-customize.sh"

Expand Down

0 comments on commit fe67169

Please sign in to comment.