]>
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 | ||
1c6a4e30 | 136 | assign() { |
b79ad79b MT |
137 | local key=${1} |
138 | assert isset key | |
139 | shift | |
140 | ||
e5651e17 | 141 | format "${key}" "%s" "$@" |
b79ad79b MT |
142 | } |
143 | ||
1c6a4e30 | 144 | fread() { |
b79ad79b MT |
145 | local file=${1} |
146 | assert isset file | |
147 | ||
148 | [ -r "${file}" ] || return ${EXIT_ERROR} | |
149 | ||
150 | print "$(<${file})" | |
151 | } | |
152 | ||
1c6a4e30 | 153 | fwrite() { |
b79ad79b MT |
154 | local file=${1} |
155 | assert isset file | |
156 | shift | |
157 | ||
8d4e0d52 MT |
158 | if [ ! -w "${file}" ]; then |
159 | log ERROR "${file}: No such file" | |
160 | return ${EXIT_ERROR} | |
161 | fi | |
162 | ||
163 | print "%s" "$@" >> ${file} 2>/dev/null | |
b79ad79b MT |
164 | } |
165 | ||
c041b631 MT |
166 | make_parent_dir() { |
167 | local path="${1}" | |
168 | ||
169 | local dirname="$(dirname "${path}")" | |
170 | mkdir -p "${dirname}" | |
171 | } | |
172 | ||
1c6a4e30 | 173 | enabled() { |
1848564d MT |
174 | local param=${1} |
175 | ||
e726ef8d | 176 | list_match "${!param}" yes on true 1 |
1848564d MT |
177 | } |
178 | ||
1c6a4e30 | 179 | mac_generate() { |
fb1416c6 MT |
180 | local b="$(random 12)" |
181 | ||
182 | # Remove multicast bit | |
183 | # and set address is software assigned | |
184 | local first_byte=$(( 0x${b:0:2} & 0xfe )) | |
185 | first_byte=$(( ${first_byte} | 0x02 )) | |
1848564d MT |
186 | |
187 | local output | |
fb1416c6 | 188 | printf -v output "%02x" "${first_byte}" |
790b7ec9 | 189 | |
fb1416c6 | 190 | output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}" |
1848564d MT |
191 | |
192 | # Check if output is valid | |
fb1416c6 | 193 | assert mac_is_valid "${output}" |
1848564d | 194 | |
790b7ec9 | 195 | echo "${output}" |
1848564d MT |
196 | } |
197 | ||
1c6a4e30 | 198 | mac_format() { |
18b43372 | 199 | local mac=${1} |
48bc31eb | 200 | assert isset mac |
18b43372 | 201 | |
48bc31eb MT |
202 | # Remove all colons and make the rest lowercase. |
203 | mac=${mac//:/} | |
204 | mac=${mac,,} | |
18b43372 | 205 | |
48bc31eb | 206 | local output |
18b43372 MT |
207 | if [ "${#mac}" = "12" ]; then |
208 | # Add colons (:) to mac address | |
209 | output=${mac:0:2} | |
210 | local i | |
211 | for i in 2 4 6 8 10; do | |
212 | output="${output}:${mac:${i}:2}" | |
213 | done | |
48bc31eb MT |
214 | else |
215 | output=${mac} | |
18b43372 MT |
216 | fi |
217 | ||
218 | assert mac_is_valid ${output} | |
219 | ||
48bc31eb | 220 | print "${output}" |
18b43372 MT |
221 | } |
222 | ||
1c6a4e30 | 223 | mac_is_valid() { |
1848564d MT |
224 | local mac=${1} |
225 | ||
226 | [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]] | |
227 | } | |
228 | ||
1c6a4e30 | 229 | uuid() { |
de543653 | 230 | echo $(</proc/sys/kernel/random/uuid) |
1848564d MT |
231 | } |
232 | ||
fb1416c6 MT |
233 | rand() { |
234 | local uuid="$(uuid)" | |
235 | echo "${uuid//-/}" | |
236 | } | |
237 | ||
238 | random() { | |
239 | local length="${1:-8}" | |
240 | ||
241 | local random | |
242 | while [ ${#random} -lt ${length} ]; do | |
243 | random="${random}$(rand)" | |
244 | done | |
245 | ||
246 | echo "${random:0:${length}}" | |
247 | } | |
248 | ||
1c6a4e30 | 249 | isset() { |
1848564d MT |
250 | local var=${1} |
251 | ||
252 | [ -n "${!var}" ] | |
253 | } | |
254 | ||
1c6a4e30 | 255 | isoneof() { |
1848564d MT |
256 | local var=${!1} |
257 | shift | |
258 | ||
e726ef8d | 259 | list_match "${var}" "$@" |
1848564d MT |
260 | } |
261 | ||
1c6a4e30 | 262 | isbool() { |
1848564d MT |
263 | local var=${1} |
264 | ||
265 | isoneof ${var} 0 1 no yes on off | |
266 | } | |
267 | ||
1c6a4e30 | 268 | isinteger() { |
1848564d MT |
269 | local var=${!1} |
270 | ||
271 | [[ ${var} =~ ^[0-9]+$ ]] | |
272 | } | |
273 | ||
1c6a4e30 | 274 | ismac() { |
1848564d MT |
275 | local mac=${!1} |
276 | ||
277 | mac_is_valid ${mac} | |
278 | } | |
279 | ||
1c6a4e30 | 280 | isipaddress() { |
fef4edaf MT |
281 | local addr=${!1} |
282 | ||
283 | ip_is_valid ${addr} | |
284 | } | |
285 | ||
1c6a4e30 | 286 | backtrace() { |
711ffac1 MT |
287 | local start=1 |
288 | ||
289 | echo # Empty line | |
290 | error_log "Backtrace (most recent call in first line):" | |
291 | ||
04608623 | 292 | local i source |
711ffac1 MT |
293 | for i in $(seq ${start} ${#BASH_SOURCE[*]}); do |
294 | [ -z "${FUNCNAME[${i}]}" ] && continue | |
6396ccab MT |
295 | |
296 | # Print called binary with arguments. | |
297 | if [ "${FUNCNAME[${i}]}" == "main" ]; then | |
298 | local args="$(list_reverse ${BASH_ARGV[*]})" | |
299 | printf -v source "%20s" "$0" | |
300 | error_log " ${source} ${args}" | |
301 | continue | |
302 | fi | |
711ffac1 | 303 | |
04608623 MT |
304 | source=${BASH_SOURCE[$(( ${i} + 1 ))]} |
305 | error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}" | |
711ffac1 MT |
306 | done |
307 | } | |
308 | ||
1c6a4e30 | 309 | assert() { |
1848564d MT |
310 | local assertion="$@" |
311 | ||
312 | if ! ${assertion}; then | |
4c670d7c | 313 | error_log "Assertion '${assertion}' failed." |
711ffac1 | 314 | backtrace |
cfbe0802 | 315 | exit ${EXIT_ERROR_ASSERT} |
1848564d MT |
316 | fi |
317 | ||
318 | return ${EXIT_OK} | |
319 | } | |
cad8bd85 | 320 | |
b0b2f995 MT |
321 | # This function checks, if the given argument is an assert error |
322 | # exit code. If this is the case, the script will halt immediately. | |
1c6a4e30 | 323 | assert_check_retval() { |
b0b2f995 MT |
324 | local ret=${1} |
325 | ||
326 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
327 | exit ${EXIT_ERROR_ASSERT} | |
328 | fi | |
329 | ||
330 | return ${ret} | |
331 | } | |
332 | ||
2bb20bbd SS |
333 | # This function executes the given command and inverses the return code |
334 | not() { | |
335 | local command="$@" | |
336 | ||
337 | ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE} | |
338 | } | |
339 | ||
1c6a4e30 | 340 | exec_cmd() { |
711ffac1 MT |
341 | local cmd=$@ |
342 | ||
343 | log DEBUG "Running command: ${cmd}" | |
344 | ||
b816e04b | 345 | DEBUG=${DEBUG} \ |
8c63fa13 MT |
346 | LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \ |
347 | LOG_FACILITY="${LOG_FACILITY}" \ | |
b816e04b | 348 | ${SHELL} ${cmd} |
711ffac1 MT |
349 | local ret=$? |
350 | ||
351 | #log DEBUG "Returned with code '${ret}'" | |
352 | ||
353 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
354 | error_log "Stopping parent process due to assertion error in child process: ${cmd}" | |
355 | exit ${EXIT_ERROR_ASSERT} | |
356 | fi | |
357 | ||
358 | return ${ret} | |
359 | } | |
360 | ||
1c6a4e30 | 361 | cmd() { |
b816e04b MT |
362 | local cmd=$@ |
363 | ||
364 | log DEBUG "Running command: ${cmd}" | |
365 | ||
366 | ${cmd} | |
367 | local ret=$? | |
368 | ||
369 | log DEBUG "Returned with code '${ret}'" | |
370 | ||
371 | return ${ret} | |
372 | } | |
373 | ||
1c6a4e30 | 374 | cmd_quiet() { |
98146c00 | 375 | cmd $@ &>/dev/null |
3efecbb3 MT |
376 | } |
377 | ||
1c6a4e30 | 378 | cmd_exec() { |
f80ce052 MT |
379 | local cmd=$@ |
380 | ||
381 | log DEBUG "Exec'ing command: ${cmd}" | |
382 | ||
383 | exec ${cmd} | |
384 | ||
385 | log ERROR "Could not exec-ute: ${cmd}" | |
386 | exit ${EXIT_ERROR} | |
387 | } | |
388 | ||
1c6a4e30 | 389 | cmd_not_implemented() { |
2181765d MT |
390 | assert false "not implemented" |
391 | } | |
392 | ||
de3cecef MT |
393 | # Runs a command in a clean environment so that no confidential information |
394 | # is leaked to any untrusted commands. | |
395 | cmd_clean_environment() { | |
396 | local cmd=$@ | |
397 | ||
398 | log DEBUG "Running command in a clean environment: ${cmd}" | |
399 | env -i -- ${cmd} | |
400 | local ret=${?} | |
401 | ||
402 | log DEBUG "Returned with code '${ret}'" | |
403 | return ${ret} | |
404 | } | |
405 | ||
f5ee091e MT |
406 | # Executes the given command in background |
407 | cmd_background() { | |
408 | cmd_quiet $@ & | |
409 | } | |
410 | ||
411 | # Prints the PID of the process that was started last | |
412 | cmd_background_get_pid() { | |
413 | print "${!}" | |
414 | } | |
415 | ||
416 | cmd_background_result() { | |
417 | local pids=$@ | |
418 | ||
419 | wait ${pids} | |
420 | } | |
421 | ||
b8026986 | 422 | # Increase security of the read command |
1c6a4e30 | 423 | read() { |
b8026986 MT |
424 | builtin read -r $@ |
425 | } | |
426 | ||
1c6a4e30 | 427 | seq() { |
3efecbb3 MT |
428 | if [ $# -eq 2 ]; then |
429 | eval echo {${1}..${2}} | |
430 | elif [ $# -eq 3 ]; then | |
431 | eval echo {${1}..${3}..${2}} | |
432 | fi | |
433 | } | |
434 | ||
1c6a4e30 | 435 | which() { |
76e6cd51 MT |
436 | type -P $@ |
437 | } | |
438 | ||
fe52c5e0 | 439 | # Prints the number of seconds since epoch. |
1c6a4e30 | 440 | timestamp() { |
fe52c5e0 MT |
441 | date -u "+%s" |
442 | } | |
443 | ||
1c6a4e30 | 444 | beautify_time() { |
d82cf370 MT |
445 | local value=${1} |
446 | ||
447 | local unit | |
448 | local limit | |
449 | for unit in s m h d w; do | |
450 | case "${unit}" in | |
451 | s|m|h) | |
452 | limit=60 | |
453 | ;; | |
454 | d) | |
455 | limit=24 | |
456 | ;; | |
457 | w) | |
458 | limit=7 | |
459 | ;; | |
460 | esac | |
461 | ||
462 | [ ${value} -lt ${limit} ] && break | |
463 | ||
464 | value=$(( ${value} / ${limit} )) | |
465 | done | |
466 | ||
467 | echo "${value}${unit}" | |
468 | } | |
711ffac1 | 469 | |
1c6a4e30 | 470 | beautify_bytes() { |
711ffac1 MT |
471 | local value=${1} |
472 | ||
473 | local unit | |
474 | local limit=1024 | |
475 | for unit in B k M G T; do | |
476 | [ ${value} -lt ${limit} ] && break | |
477 | value=$(( ${value} / ${limit} )) | |
478 | done | |
479 | ||
480 | echo "${value}${unit}" | |
481 | } | |
943e3f7e | 482 | |
1c6a4e30 | 483 | module_load() { |
943e3f7e MT |
484 | local module=${1} |
485 | ||
486 | if ! grep -q "^${module}" /proc/modules; then | |
487 | log DEBUG "Loading module '${module}'." | |
488 | modprobe ${module} | |
489 | fi | |
490 | } | |
6b3f9c85 | 491 | |
1c6a4e30 | 492 | binary_exists() { |
6b3f9c85 MT |
493 | local binary=${1} |
494 | ||
495 | if [ -n "$(type -p ${binary})" ]; then | |
496 | return ${EXIT_OK} | |
497 | fi | |
498 | ||
499 | return ${EXIT_ERROR} | |
500 | } | |
d76f5107 | 501 | |
1c6a4e30 | 502 | function_exists() { |
1e6f187e MT |
503 | local function="${1}" |
504 | ||
505 | if [ "$(type -t "${function}")" = "function" ]; then | |
506 | return ${EXIT_TRUE} | |
507 | fi | |
508 | ||
509 | return ${EXIT_FALSE} | |
510 | } | |
511 | ||
1c6a4e30 | 512 | process_kill() { |
d76f5107 MT |
513 | local process=${1} |
514 | ||
515 | if ! isinteger process; then | |
516 | process=$(pidof ${process}) | |
517 | fi | |
518 | ||
519 | local pid | |
520 | local sig | |
521 | for pid in ${process}; do | |
522 | for sig in 15 9; do | |
523 | [ -d "/proc/${pid}" ] || break | |
524 | ||
525 | kill -${sig} ${pid} | |
526 | sleep 1 | |
527 | done | |
528 | done | |
529 | } | |
feb76eaf | 530 | |
1c6a4e30 | 531 | dec() { |
feb76eaf MT |
532 | local hex=${1} |
533 | ||
534 | if [ "${hex:0:2}" != "0x" ]; then | |
535 | hex="0x${hex}" | |
536 | fi | |
537 | ||
538 | printf "%d\n" "${hex}" | |
539 | } | |
3a7fef62 | 540 | |
1c6a4e30 | 541 | chr() { |
5cf0edf9 MT |
542 | local char="${1}" |
543 | ||
544 | [ ${char} -lt 256 ] || return ${EXIT_ERROR} | |
545 | ||
546 | printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n" | |
547 | } | |
548 | ||
1c6a4e30 | 549 | ord() { |
5cf0edf9 MT |
550 | LC_CTYPE="C" printf "%d\n" "'${1}" |
551 | } | |
552 | ||
1c6a4e30 | 553 | hex() { |
5cf0edf9 MT |
554 | printf "%X\n" "${1}" |
555 | } | |
556 | ||
1c6a4e30 | 557 | network_is_running() { |
3a7fef62 MT |
558 | # Check, if the network service is running. |
559 | service_is_active network | |
560 | } | |
f80ce052 | 561 | |
1c6a4e30 | 562 | contains_spaces() { |
f80ce052 MT |
563 | local var="$@" |
564 | ||
565 | # Eliminate spaces. | |
566 | local var2=${var// /} | |
567 | ||
568 | if [ ${#var} -ne ${#var2} ]; then | |
569 | return ${EXIT_TRUE} | |
570 | fi | |
571 | ||
572 | return ${EXIT_FALSE} | |
573 | } | |
5cf0edf9 | 574 | |
1c6a4e30 | 575 | string_split() { |
5cf0edf9 MT |
576 | local string="$@" |
577 | ||
578 | local pos=0 | |
579 | while [ ${pos} -lt ${#string} ]; do | |
580 | print "${string:${pos}:1}" | |
581 | pos=$(( ${pos} + 1 )) | |
582 | done | |
583 | ||
584 | return ${EXIT_OK} | |
585 | } | |
586 | ||
1c6a4e30 | 587 | string_reverse() { |
5cf0edf9 MT |
588 | local string="$@" |
589 | ||
590 | local output | |
591 | local pos=0 | |
592 | while [ ${pos} -lt ${#string} ]; do | |
593 | output="${string:${pos}:1}${output}" | |
594 | pos=$(( ${pos} + 1 )) | |
595 | done | |
596 | ||
597 | print "${output}" | |
598 | return ${EXIT_OK} | |
599 | } | |
600 | ||
1c6a4e30 | 601 | dec2bin() { |
5cf0edf9 MT |
602 | local number="${1}" |
603 | ||
604 | local output | |
605 | ||
606 | local i div | |
607 | for i in 7 6 5 4 3 2 1; do | |
608 | div=$(( 2 ** ${i} )) | |
609 | ||
610 | if [ $(( ${number} / ${div} )) -eq 1 ]; then | |
611 | output="${output}1" | |
612 | else | |
613 | output="${output}0" | |
614 | fi | |
615 | number="$(( ${number} % ${div} ))" | |
616 | done | |
617 | ||
618 | if [ $(( ${number} % 2 )) -eq 1 ]; then | |
619 | output="${output}1" | |
620 | else | |
621 | output="${output}0" | |
622 | fi | |
623 | ||
624 | print "${output}" | |
625 | } | |
626 | ||
1c6a4e30 | 627 | bin2dec() { |
5cf0edf9 MT |
628 | local string="${1}" |
629 | local number=0 | |
630 | ||
631 | local pos=0 char | |
632 | while [ ${pos} -lt ${#string} ]; do | |
633 | char="${string:${pos}:1}" | |
634 | pos=$(( ${pos} + 1 )) | |
635 | ||
636 | number=$(( ${number} << 1 )) | |
637 | ||
638 | case "${char}" in | |
639 | 0) ;; | |
640 | 1) | |
641 | number=$(( ${number} + 1 )) | |
642 | ;; | |
643 | *) | |
644 | assert false "Invalid character: ${char}" | |
645 | ;; | |
646 | esac | |
647 | done | |
648 | ||
649 | print "${number}" | |
650 | return ${EXIT_OK} | |
651 | } | |
652 | ||
1c6a4e30 | 653 | char2bin() { |
5cf0edf9 MT |
654 | local dec="$(ord "${1}")" |
655 | ||
656 | dec2bin "${dec}" | |
657 | } | |
658 | ||
1c6a4e30 | 659 | bin2char() { |
5cf0edf9 MT |
660 | local dec="$(bin2dec "$@")" |
661 | ||
662 | chr "${dec}" | |
663 | } | |
664 | ||
1c6a4e30 | 665 | bin2hex() { |
5cf0edf9 MT |
666 | local dec="$(bin2dec "$@")" |
667 | ||
668 | dec2hex "${dec}" | |
669 | } | |
670 | ||
1c6a4e30 | 671 | hex2bin() { |
5cf0edf9 MT |
672 | local dec="$(hex2dec "$@")" |
673 | ||
674 | dec2bin "${dec}" | |
675 | } | |
676 | ||
1c6a4e30 | 677 | hex2dec() { |
5cf0edf9 MT |
678 | local hex="${1}" |
679 | ||
680 | # Prepend 0x if necessary. | |
681 | [ "${hex:0:2}" = "0x" ] || hex="0x${hex}" | |
682 | ||
683 | printf "%d\n" "${hex}" | |
684 | } | |
685 | ||
1c6a4e30 | 686 | dec2hex() { |
5cf0edf9 MT |
687 | printf "%02x\n" "${1}" |
688 | } |