3 # check that $1 is only base 10 digits, and that it doesn't
8 if [ -z "$__n" -o -n "${__n//[0-9]/}" ]; then
9 printf "Not a decimal integer (%s)\n" "$__n ">&2
13 if [ "$__n" -gt $uint_max ]; then
14 printf "Out of range (%s)\n" "$__n" >&2
18 if [ "$((__n + 0))" != "$__n" ]; then
19 printf "Not normalized notation (%s)\n" "$__n" >&2
26 # return a count of the number of bits set in $1
28 local __var
="$1" __c
="$2"
29 assert_uint32
"$__c" ||
return 1
31 __c
=$
((((__c
>> 1) & 0x55555555) + (__c
& 0x55555555)))
32 __c
=$
((((__c
>> 2) & 0x33333333) + (__c
& 0x33333333)))
33 __c
=$
((((__c
>> 4) & 0x0f0f0f0f) + (__c
& 0x0f0f0f0f)))
34 __c
=$
((((__c
>> 8) & 0x00ff00ff) + (__c
& 0x00ff00ff)))
35 __c
=$
((((__c
>> 16) & 0x0000ffff) + (__c
& 0x0000ffff)))
37 export -- "$__var=$__c"
40 # tedious but portable with busybox's limited shell
41 # we check each octet to be in the range of 0..255,
42 # and also make sure there's no extaneous characters.
44 local __var
="$1" __ip
="$2" __n __val
=0
55 1[0-9][0-9].
*|
2[0-4][0-9].
*|
25[0-5].
*)
60 printf "Not a dotted quad (%s)\n" "$2" >&2
76 1[0-9][0-9].*|2[0-4][0-9].*|25[0-5].*)
81 printf "Not a dotted quad (%s)\n" "$2" >&2
86 __val=$((__val + (__n << 16)))
97 1[0-9][0-9].*|2[0-4][0-9].*|25[0-5].*)
102 printf "Not a dotted quad (%s)\n" "$2" >&2
107 __val=$((__val + (__n << 8)))
118 1[0-9][0-9]|2[0-4][0-9]|25[0-5])
123 printf "Not a dotted quad (%s)\n" "$2" >&2
128 __val=$((__val + __n))
130 if [ -n "$__ip" ]; then
131 printf "Not a dotted quad (%s)\n" "$2" >&2
135 export -- "$__var=$__val"
139 # convert back from an integer to dotted-quad.
141 local __var="$1" __n="$2"
142 assert_uint32 "$__n" || return 1
144 export -- "$__var=$((__n >> 24)).$
(((__n
>> 16) & 255)).$
(((__n
>> 8) & 255)).$
((__n
& 255))"