]>
Commit | Line | Data |
---|---|---|
1848564d MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2010 Michael Tremer & Christian Schmidt # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
97cb552e | 22 | # A simple print statement |
1c6a4e30 | 23 | print() { |
acc9efd5 MT |
24 | local fmt=${1}; shift |
25 | ||
40e3553f | 26 | printf -- "${fmt}\n" "$@" |
97cb552e MT |
27 | } |
28 | ||
cb965348 MT |
29 | # The args() function takes a number of arguments like |
30 | # var1="abc d" var2="abc" var3="abcd e" | |
31 | # and splits them into several arguments, devided by newline | |
1c6a4e30 | 32 | args() { |
cb965348 MT |
33 | echo "$@" | xargs printf "%s\n" |
34 | } | |
35 | ||
1c6a4e30 | 36 | unquote() { |
04854c77 MT |
37 | local var="$@" |
38 | ||
39 | if [ "${var:0:1}" = "\"" ]; then | |
40 | var=${var:1} | |
41 | fi | |
42 | ||
43 | local last=$(( ${#var} - 1 )) | |
44 | if [ ${last} -ge 0 ] && [ "${var:${last}:1}" = "\"" ]; then | |
45 | var=${var:0:${last}} | |
46 | fi | |
47 | ||
48 | print "${var}" | |
49 | } | |
50 | ||
1c6a4e30 | 51 | quote() { |
04854c77 MT |
52 | print "\"%s\"" "$@" |
53 | } | |
54 | ||
1c6a4e30 | 55 | strip() { |
fe52c5e0 MT |
56 | local value="$@" |
57 | ||
58 | # remove leading whitespace characters | |
59 | value="${value#"${value%%[![:space:]]*}"}" | |
60 | ||
61 | # remove trailing whitespace characters | |
62 | value="${value%"${value##*[![:space:]]}"}" | |
63 | ||
64 | print "${value}" | |
65 | } | |
66 | ||
1848564d | 67 | # Print a pretty error message |
1c6a4e30 | 68 | error() { |
fcbf6823 | 69 | echo -e " ${CLR_RED_B}ERROR${CLR_RESET} : $@" >&2 |
1848564d MT |
70 | } |
71 | ||
1c6a4e30 | 72 | error_log() { |
1b7a1578 MT |
73 | log ERROR "$@" |
74 | } | |
75 | ||
1848564d | 76 | # Print a pretty warn message |
1c6a4e30 | 77 | warning() { |
fcbf6823 | 78 | echo -e " ${CLR_YELLOW_B}WARNING${CLR_RESET}: $@" >&2 |
1848564d MT |
79 | } |
80 | ||
1c6a4e30 | 81 | warning_log() { |
1b7a1578 MT |
82 | log WARNING "$@" |
83 | } | |
84 | ||
e726ef8d MT |
85 | # The next three functions are kept for backwards |
86 | # compatibility. The need to be dropped at some time. | |
1c6a4e30 | 87 | listsort() { |
e726ef8d | 88 | list_sort $@ |
1848564d MT |
89 | } |
90 | ||
1c6a4e30 | 91 | listmatch() { |
e726ef8d | 92 | list_match $@ |
711ffac1 MT |
93 | } |
94 | ||
1c6a4e30 | 95 | listlength() { |
e726ef8d | 96 | list_length $@ |
711ffac1 MT |
97 | } |
98 | ||
1848564d | 99 | # Speedup function to avoid a call of the basename binary |
1c6a4e30 | 100 | basename() { |
1848564d MT |
101 | echo "${1##*/}" |
102 | } | |
103 | ||
1c6a4e30 | 104 | format() { |
e5651e17 MT |
105 | local key=${1} |
106 | assert isset key | |
107 | ||
108 | local format=${2} | |
109 | assert isset format | |
110 | ||
111 | shift 2 | |
112 | ||
113 | printf -v "${key}" "${format}" "$@" | |
114 | } | |
115 | ||
d13929d4 MT |
116 | format_time() { |
117 | local s=${1} | |
118 | local ret m | |
119 | ||
120 | local units="s m h" | |
121 | ||
122 | local unit | |
123 | for unit in ${units}; do | |
124 | m=$(( ${s} % 60 )) | |
125 | s=$(( ${s} / 60 )) | |
126 | ||
127 | if [ ${m} -gt 0 ]; then | |
128 | ret="${m}${unit} ${ret}" | |
129 | fi | |
130 | done | |
131 | ||
132 | # Remove whitespace | |
133 | echo ${ret} | |
134 | } | |
135 | ||
b383499d MT |
136 | parse_time() { |
137 | local ret=0 | |
138 | ||
139 | local arg | |
140 | for arg in $@; do | |
141 | local unit | |
142 | ||
143 | case "${arg}" in | |
144 | *h|*m|*s) | |
145 | # Store unit | |
146 | unit="${arg: -1}" | |
147 | ||
148 | # Remove unit | |
149 | arg="${arg:0:-1}" | |
150 | ;; | |
151 | esac | |
152 | ||
153 | if ! isinteger arg; then | |
154 | return ${EXIT_ERROR} | |
155 | fi | |
156 | ||
157 | # Convert hours and minutes into seconds | |
158 | case "${unit}" in | |
159 | h) | |
160 | arg=$(( ${arg} * 3600 )) | |
161 | ;; | |
162 | m) | |
163 | arg=$(( ${arg} * 60 )) | |
164 | ;; | |
165 | esac | |
166 | ||
167 | # Add up everything | |
168 | ret=$(( ${ret} + ${arg} )) | |
169 | done | |
170 | ||
171 | print "${ret}" | |
172 | } | |
173 | ||
1c6a4e30 | 174 | assign() { |
b79ad79b MT |
175 | local key=${1} |
176 | assert isset key | |
177 | shift | |
178 | ||
e5651e17 | 179 | format "${key}" "%s" "$@" |
b79ad79b MT |
180 | } |
181 | ||
1c6a4e30 | 182 | fread() { |
b79ad79b MT |
183 | local file=${1} |
184 | assert isset file | |
185 | ||
186 | [ -r "${file}" ] || return ${EXIT_ERROR} | |
187 | ||
188 | print "$(<${file})" | |
189 | } | |
190 | ||
1c6a4e30 | 191 | fwrite() { |
b79ad79b MT |
192 | local file=${1} |
193 | assert isset file | |
194 | shift | |
195 | ||
8d4e0d52 MT |
196 | if [ ! -w "${file}" ]; then |
197 | log ERROR "${file}: No such file" | |
198 | return ${EXIT_ERROR} | |
199 | fi | |
200 | ||
201 | print "%s" "$@" >> ${file} 2>/dev/null | |
b79ad79b MT |
202 | } |
203 | ||
c041b631 MT |
204 | make_parent_dir() { |
205 | local path="${1}" | |
206 | ||
207 | local dirname="$(dirname "${path}")" | |
208 | mkdir -p "${dirname}" | |
209 | } | |
210 | ||
1c6a4e30 | 211 | enabled() { |
1848564d MT |
212 | local param=${1} |
213 | ||
e726ef8d | 214 | list_match "${!param}" yes on true 1 |
1848564d MT |
215 | } |
216 | ||
1c6a4e30 | 217 | mac_generate() { |
fb1416c6 MT |
218 | local b="$(random 12)" |
219 | ||
220 | # Remove multicast bit | |
221 | # and set address is software assigned | |
222 | local first_byte=$(( 0x${b:0:2} & 0xfe )) | |
223 | first_byte=$(( ${first_byte} | 0x02 )) | |
1848564d MT |
224 | |
225 | local output | |
fb1416c6 | 226 | printf -v output "%02x" "${first_byte}" |
790b7ec9 | 227 | |
fb1416c6 | 228 | output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}" |
1848564d MT |
229 | |
230 | # Check if output is valid | |
fb1416c6 | 231 | assert mac_is_valid "${output}" |
1848564d | 232 | |
790b7ec9 | 233 | echo "${output}" |
1848564d MT |
234 | } |
235 | ||
1c6a4e30 | 236 | mac_format() { |
18b43372 | 237 | local mac=${1} |
48bc31eb | 238 | assert isset mac |
18b43372 | 239 | |
48bc31eb MT |
240 | # Remove all colons and make the rest lowercase. |
241 | mac=${mac//:/} | |
242 | mac=${mac,,} | |
18b43372 | 243 | |
48bc31eb | 244 | local output |
18b43372 MT |
245 | if [ "${#mac}" = "12" ]; then |
246 | # Add colons (:) to mac address | |
247 | output=${mac:0:2} | |
248 | local i | |
249 | for i in 2 4 6 8 10; do | |
250 | output="${output}:${mac:${i}:2}" | |
251 | done | |
48bc31eb MT |
252 | else |
253 | output=${mac} | |
18b43372 MT |
254 | fi |
255 | ||
256 | assert mac_is_valid ${output} | |
257 | ||
48bc31eb | 258 | print "${output}" |
18b43372 MT |
259 | } |
260 | ||
1c6a4e30 | 261 | mac_is_valid() { |
1848564d MT |
262 | local mac=${1} |
263 | ||
264 | [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]] | |
265 | } | |
266 | ||
1c6a4e30 | 267 | uuid() { |
de543653 | 268 | echo $(</proc/sys/kernel/random/uuid) |
1848564d MT |
269 | } |
270 | ||
a24cff8f JS |
271 | abs() { |
272 | local val=${1} | |
273 | ||
274 | if [ ${val} -lt 0 ]; then | |
275 | (( val *= -1 )) | |
276 | fi | |
277 | ||
278 | echo ${val} | |
279 | } | |
280 | ||
fb1416c6 MT |
281 | rand() { |
282 | local uuid="$(uuid)" | |
283 | echo "${uuid//-/}" | |
284 | } | |
285 | ||
286 | random() { | |
287 | local length="${1:-8}" | |
288 | ||
289 | local random | |
290 | while [ ${#random} -lt ${length} ]; do | |
291 | random="${random}$(rand)" | |
292 | done | |
293 | ||
294 | echo "${random:0:${length}}" | |
295 | } | |
296 | ||
1c6a4e30 | 297 | isset() { |
1848564d MT |
298 | local var=${1} |
299 | ||
300 | [ -n "${!var}" ] | |
301 | } | |
302 | ||
1c6a4e30 | 303 | isoneof() { |
1848564d MT |
304 | local var=${!1} |
305 | shift | |
306 | ||
e726ef8d | 307 | list_match "${var}" "$@" |
1848564d MT |
308 | } |
309 | ||
1c6a4e30 | 310 | isbool() { |
1848564d MT |
311 | local var=${1} |
312 | ||
313 | isoneof ${var} 0 1 no yes on off | |
314 | } | |
315 | ||
1c6a4e30 | 316 | isinteger() { |
1848564d MT |
317 | local var=${!1} |
318 | ||
319 | [[ ${var} =~ ^[0-9]+$ ]] | |
320 | } | |
321 | ||
1c6a4e30 | 322 | ismac() { |
1848564d MT |
323 | local mac=${!1} |
324 | ||
325 | mac_is_valid ${mac} | |
326 | } | |
327 | ||
1c6a4e30 | 328 | isipaddress() { |
fef4edaf MT |
329 | local addr=${!1} |
330 | ||
331 | ip_is_valid ${addr} | |
332 | } | |
333 | ||
48a64768 JS |
334 | mtu_is_valid() { |
335 | local proto=${1} | |
336 | local mtu=${2} | |
337 | ||
338 | case ${proto} in | |
339 | ipv4) | |
340 | [ ${mtu} -ge 576 ] && [ ${mtu} -le 9000 ] | |
341 | ;; | |
342 | ipv6) | |
343 | [ ${mtu} -ge 1280 ] && [ ${mtu} -le 9000 ] | |
344 | ;; | |
345 | *) | |
346 | error "${proto} is not a valid proto" | |
347 | return ${EXIT_ERROR} | |
348 | ;; | |
349 | esac | |
350 | } | |
351 | ||
1c6a4e30 | 352 | backtrace() { |
711ffac1 MT |
353 | local start=1 |
354 | ||
355 | echo # Empty line | |
356 | error_log "Backtrace (most recent call in first line):" | |
357 | ||
04608623 | 358 | local i source |
711ffac1 MT |
359 | for i in $(seq ${start} ${#BASH_SOURCE[*]}); do |
360 | [ -z "${FUNCNAME[${i}]}" ] && continue | |
6396ccab MT |
361 | |
362 | # Print called binary with arguments. | |
363 | if [ "${FUNCNAME[${i}]}" == "main" ]; then | |
364 | local args="$(list_reverse ${BASH_ARGV[*]})" | |
365 | printf -v source "%20s" "$0" | |
366 | error_log " ${source} ${args}" | |
367 | continue | |
368 | fi | |
711ffac1 | 369 | |
04608623 MT |
370 | source=${BASH_SOURCE[$(( ${i} + 1 ))]} |
371 | error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}" | |
711ffac1 MT |
372 | done |
373 | } | |
374 | ||
1c6a4e30 | 375 | assert() { |
1848564d MT |
376 | local assertion="$@" |
377 | ||
378 | if ! ${assertion}; then | |
4c670d7c | 379 | error_log "Assertion '${assertion}' failed." |
711ffac1 | 380 | backtrace |
cfbe0802 | 381 | exit ${EXIT_ERROR_ASSERT} |
1848564d MT |
382 | fi |
383 | ||
384 | return ${EXIT_OK} | |
385 | } | |
cad8bd85 | 386 | |
b0b2f995 MT |
387 | # This function checks, if the given argument is an assert error |
388 | # exit code. If this is the case, the script will halt immediately. | |
1c6a4e30 | 389 | assert_check_retval() { |
b0b2f995 MT |
390 | local ret=${1} |
391 | ||
392 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
393 | exit ${EXIT_ERROR_ASSERT} | |
394 | fi | |
395 | ||
396 | return ${ret} | |
397 | } | |
398 | ||
2bb20bbd SS |
399 | # This function executes the given command and inverses the return code |
400 | not() { | |
401 | local command="$@" | |
402 | ||
403 | ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE} | |
404 | } | |
405 | ||
1c6a4e30 | 406 | exec_cmd() { |
711ffac1 MT |
407 | local cmd=$@ |
408 | ||
409 | log DEBUG "Running command: ${cmd}" | |
410 | ||
b816e04b | 411 | DEBUG=${DEBUG} \ |
8c63fa13 MT |
412 | LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \ |
413 | LOG_FACILITY="${LOG_FACILITY}" \ | |
b816e04b | 414 | ${SHELL} ${cmd} |
711ffac1 MT |
415 | local ret=$? |
416 | ||
417 | #log DEBUG "Returned with code '${ret}'" | |
418 | ||
419 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
420 | error_log "Stopping parent process due to assertion error in child process: ${cmd}" | |
421 | exit ${EXIT_ERROR_ASSERT} | |
422 | fi | |
423 | ||
424 | return ${ret} | |
425 | } | |
426 | ||
1c6a4e30 | 427 | cmd() { |
b816e04b MT |
428 | local cmd=$@ |
429 | ||
430 | log DEBUG "Running command: ${cmd}" | |
431 | ||
432 | ${cmd} | |
433 | local ret=$? | |
434 | ||
435 | log DEBUG "Returned with code '${ret}'" | |
436 | ||
437 | return ${ret} | |
438 | } | |
439 | ||
1c6a4e30 | 440 | cmd_quiet() { |
98146c00 | 441 | cmd $@ &>/dev/null |
3efecbb3 MT |
442 | } |
443 | ||
1c6a4e30 | 444 | cmd_exec() { |
f80ce052 MT |
445 | local cmd=$@ |
446 | ||
447 | log DEBUG "Exec'ing command: ${cmd}" | |
448 | ||
449 | exec ${cmd} | |
450 | ||
451 | log ERROR "Could not exec-ute: ${cmd}" | |
452 | exit ${EXIT_ERROR} | |
453 | } | |
454 | ||
1c6a4e30 | 455 | cmd_not_implemented() { |
2181765d MT |
456 | assert false "not implemented" |
457 | } | |
458 | ||
de3cecef MT |
459 | # Runs a command in a clean environment so that no confidential information |
460 | # is leaked to any untrusted commands. | |
461 | cmd_clean_environment() { | |
462 | local cmd=$@ | |
463 | ||
464 | log DEBUG "Running command in a clean environment: ${cmd}" | |
465 | env -i -- ${cmd} | |
466 | local ret=${?} | |
467 | ||
468 | log DEBUG "Returned with code '${ret}'" | |
469 | return ${ret} | |
470 | } | |
471 | ||
f5ee091e MT |
472 | # Executes the given command in background |
473 | cmd_background() { | |
474 | cmd_quiet $@ & | |
475 | } | |
476 | ||
477 | # Prints the PID of the process that was started last | |
478 | cmd_background_get_pid() { | |
479 | print "${!}" | |
480 | } | |
481 | ||
482 | cmd_background_result() { | |
483 | local pids=$@ | |
484 | ||
485 | wait ${pids} | |
486 | } | |
487 | ||
b8026986 | 488 | # Increase security of the read command |
1c6a4e30 | 489 | read() { |
b8026986 MT |
490 | builtin read -r $@ |
491 | } | |
492 | ||
1c6a4e30 | 493 | seq() { |
3efecbb3 MT |
494 | if [ $# -eq 2 ]; then |
495 | eval echo {${1}..${2}} | |
496 | elif [ $# -eq 3 ]; then | |
497 | eval echo {${1}..${3}..${2}} | |
498 | fi | |
499 | } | |
500 | ||
de72bd91 MT |
501 | range() { |
502 | eval echo {0..$(( ${1} - 1 ))} | |
503 | } | |
504 | ||
505 | count() { | |
506 | local i=0 | |
507 | ||
508 | while read; do | |
509 | ((i++)) | |
510 | done | |
511 | ||
512 | echo ${i} | |
513 | } | |
514 | ||
1c6a4e30 | 515 | which() { |
76e6cd51 MT |
516 | type -P $@ |
517 | } | |
518 | ||
fe52c5e0 | 519 | # Prints the number of seconds since epoch. |
1c6a4e30 | 520 | timestamp() { |
fe52c5e0 MT |
521 | date -u "+%s" |
522 | } | |
523 | ||
1c6a4e30 | 524 | beautify_time() { |
d82cf370 MT |
525 | local value=${1} |
526 | ||
527 | local unit | |
528 | local limit | |
529 | for unit in s m h d w; do | |
530 | case "${unit}" in | |
531 | s|m|h) | |
532 | limit=60 | |
533 | ;; | |
534 | d) | |
535 | limit=24 | |
536 | ;; | |
537 | w) | |
538 | limit=7 | |
539 | ;; | |
540 | esac | |
541 | ||
542 | [ ${value} -lt ${limit} ] && break | |
543 | ||
544 | value=$(( ${value} / ${limit} )) | |
545 | done | |
546 | ||
547 | echo "${value}${unit}" | |
548 | } | |
711ffac1 | 549 | |
1c6a4e30 | 550 | beautify_bytes() { |
711ffac1 MT |
551 | local value=${1} |
552 | ||
553 | local unit | |
554 | local limit=1024 | |
555 | for unit in B k M G T; do | |
556 | [ ${value} -lt ${limit} ] && break | |
557 | value=$(( ${value} / ${limit} )) | |
558 | done | |
559 | ||
560 | echo "${value}${unit}" | |
561 | } | |
943e3f7e | 562 | |
1c6a4e30 | 563 | module_load() { |
943e3f7e MT |
564 | local module=${1} |
565 | ||
566 | if ! grep -q "^${module}" /proc/modules; then | |
567 | log DEBUG "Loading module '${module}'." | |
568 | modprobe ${module} | |
569 | fi | |
570 | } | |
6b3f9c85 | 571 | |
1c6a4e30 | 572 | binary_exists() { |
6b3f9c85 MT |
573 | local binary=${1} |
574 | ||
575 | if [ -n "$(type -p ${binary})" ]; then | |
576 | return ${EXIT_OK} | |
577 | fi | |
578 | ||
579 | return ${EXIT_ERROR} | |
580 | } | |
d76f5107 | 581 | |
1c6a4e30 | 582 | function_exists() { |
1e6f187e MT |
583 | local function="${1}" |
584 | ||
585 | if [ "$(type -t "${function}")" = "function" ]; then | |
586 | return ${EXIT_TRUE} | |
587 | fi | |
588 | ||
589 | return ${EXIT_FALSE} | |
590 | } | |
591 | ||
1c6a4e30 | 592 | process_kill() { |
d76f5107 MT |
593 | local process=${1} |
594 | ||
595 | if ! isinteger process; then | |
596 | process=$(pidof ${process}) | |
597 | fi | |
598 | ||
599 | local pid | |
600 | local sig | |
601 | for pid in ${process}; do | |
602 | for sig in 15 9; do | |
603 | [ -d "/proc/${pid}" ] || break | |
604 | ||
605 | kill -${sig} ${pid} | |
606 | sleep 1 | |
607 | done | |
608 | done | |
609 | } | |
feb76eaf | 610 | |
1c6a4e30 | 611 | dec() { |
feb76eaf MT |
612 | local hex=${1} |
613 | ||
614 | if [ "${hex:0:2}" != "0x" ]; then | |
615 | hex="0x${hex}" | |
616 | fi | |
617 | ||
618 | printf "%d\n" "${hex}" | |
619 | } | |
3a7fef62 | 620 | |
1c6a4e30 | 621 | chr() { |
5cf0edf9 MT |
622 | local char="${1}" |
623 | ||
624 | [ ${char} -lt 256 ] || return ${EXIT_ERROR} | |
625 | ||
626 | printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n" | |
627 | } | |
628 | ||
1c6a4e30 | 629 | ord() { |
5cf0edf9 MT |
630 | LC_CTYPE="C" printf "%d\n" "'${1}" |
631 | } | |
632 | ||
1c6a4e30 | 633 | hex() { |
5cf0edf9 MT |
634 | printf "%X\n" "${1}" |
635 | } | |
636 | ||
1c6a4e30 | 637 | network_is_running() { |
3a7fef62 MT |
638 | # Check, if the network service is running. |
639 | service_is_active network | |
640 | } | |
f80ce052 | 641 | |
1c6a4e30 | 642 | contains_spaces() { |
f80ce052 MT |
643 | local var="$@" |
644 | ||
645 | # Eliminate spaces. | |
646 | local var2=${var// /} | |
647 | ||
648 | if [ ${#var} -ne ${#var2} ]; then | |
649 | return ${EXIT_TRUE} | |
650 | fi | |
651 | ||
652 | return ${EXIT_FALSE} | |
653 | } | |
5cf0edf9 | 654 | |
1c6a4e30 | 655 | string_split() { |
5cf0edf9 MT |
656 | local string="$@" |
657 | ||
658 | local pos=0 | |
659 | while [ ${pos} -lt ${#string} ]; do | |
660 | print "${string:${pos}:1}" | |
661 | pos=$(( ${pos} + 1 )) | |
662 | done | |
663 | ||
664 | return ${EXIT_OK} | |
665 | } | |
666 | ||
1c6a4e30 | 667 | string_reverse() { |
5cf0edf9 MT |
668 | local string="$@" |
669 | ||
670 | local output | |
671 | local pos=0 | |
672 | while [ ${pos} -lt ${#string} ]; do | |
673 | output="${string:${pos}:1}${output}" | |
674 | pos=$(( ${pos} + 1 )) | |
675 | done | |
676 | ||
677 | print "${output}" | |
678 | return ${EXIT_OK} | |
679 | } | |
680 | ||
1c6a4e30 | 681 | dec2bin() { |
5cf0edf9 MT |
682 | local number="${1}" |
683 | ||
684 | local output | |
685 | ||
686 | local i div | |
687 | for i in 7 6 5 4 3 2 1; do | |
688 | div=$(( 2 ** ${i} )) | |
689 | ||
690 | if [ $(( ${number} / ${div} )) -eq 1 ]; then | |
691 | output="${output}1" | |
692 | else | |
693 | output="${output}0" | |
694 | fi | |
695 | number="$(( ${number} % ${div} ))" | |
696 | done | |
697 | ||
698 | if [ $(( ${number} % 2 )) -eq 1 ]; then | |
699 | output="${output}1" | |
700 | else | |
701 | output="${output}0" | |
702 | fi | |
703 | ||
704 | print "${output}" | |
705 | } | |
706 | ||
1c6a4e30 | 707 | bin2dec() { |
5cf0edf9 MT |
708 | local string="${1}" |
709 | local number=0 | |
710 | ||
711 | local pos=0 char | |
712 | while [ ${pos} -lt ${#string} ]; do | |
713 | char="${string:${pos}:1}" | |
714 | pos=$(( ${pos} + 1 )) | |
715 | ||
716 | number=$(( ${number} << 1 )) | |
717 | ||
718 | case "${char}" in | |
719 | 0) ;; | |
720 | 1) | |
721 | number=$(( ${number} + 1 )) | |
722 | ;; | |
723 | *) | |
724 | assert false "Invalid character: ${char}" | |
725 | ;; | |
726 | esac | |
727 | done | |
728 | ||
729 | print "${number}" | |
730 | return ${EXIT_OK} | |
731 | } | |
732 | ||
1c6a4e30 | 733 | char2bin() { |
5cf0edf9 MT |
734 | local dec="$(ord "${1}")" |
735 | ||
736 | dec2bin "${dec}" | |
737 | } | |
738 | ||
1c6a4e30 | 739 | bin2char() { |
5cf0edf9 MT |
740 | local dec="$(bin2dec "$@")" |
741 | ||
742 | chr "${dec}" | |
743 | } | |
744 | ||
1c6a4e30 | 745 | bin2hex() { |
5cf0edf9 MT |
746 | local dec="$(bin2dec "$@")" |
747 | ||
748 | dec2hex "${dec}" | |
749 | } | |
750 | ||
1c6a4e30 | 751 | hex2bin() { |
5cf0edf9 MT |
752 | local dec="$(hex2dec "$@")" |
753 | ||
754 | dec2bin "${dec}" | |
755 | } | |
756 | ||
1c6a4e30 | 757 | hex2dec() { |
5cf0edf9 MT |
758 | local hex="${1}" |
759 | ||
760 | # Prepend 0x if necessary. | |
761 | [ "${hex:0:2}" = "0x" ] || hex="0x${hex}" | |
762 | ||
763 | printf "%d\n" "${hex}" | |
764 | } | |
765 | ||
1c6a4e30 | 766 | dec2hex() { |
5cf0edf9 MT |
767 | printf "%02x\n" "${1}" |
768 | } |