Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/Canonicalize-CIDR/Jq/canonicalize-cidr-1.jq
Normal file
28
Task/Canonicalize-CIDR/Jq/canonicalize-cidr-1.jq
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
# For gojq and fq
|
||||
def _nwise($n):
|
||||
def nw: if length <= $n then . else .[0:$n] , (.[$n:] | nw) end;
|
||||
nw;
|
||||
|
||||
def lpad($len; $fill): tostring | ($len - length) as $l | ($fill * $l)[:$l] + .;
|
||||
def lpad($len): lpad($len;" ");
|
||||
|
||||
# Convert the input integer to a string in the specified base (2 to 36 inclusive)
|
||||
def convert(base):
|
||||
def stream:
|
||||
recurse(if . >= base then ./base|floor else empty end) | . % base ;
|
||||
[stream] | reverse
|
||||
| if base < 10 then map(tostring) | join("")
|
||||
elif base <= 36 then map(if . < 10 then 48 + . else . + 87 end) | implode
|
||||
else error("base too large")
|
||||
end;
|
||||
|
||||
# input string is converted from "base" to an integer, within limits
|
||||
# of the underlying arithmetic operations, and without error-checking:
|
||||
def to_i(base):
|
||||
explode
|
||||
| reverse
|
||||
| map(if . > 96 then . - 87 else . - 48 end) # "a" ~ 97 => 10 ~ 87
|
||||
| reduce .[] as $c
|
||||
# state: [power, ans]
|
||||
([1,0]; (.[0] * base) as $b | [$b, .[1] + (.[0] * $c)])
|
||||
| .[1];
|
||||
29
Task/Canonicalize-CIDR/Jq/canonicalize-cidr-2.jq
Normal file
29
Task/Canonicalize-CIDR/Jq/canonicalize-cidr-2.jq
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# Canonicalize a CIDR block: make sure none of the host bits are set
|
||||
def canonicalize:
|
||||
# dotted-decimal / bits in network part
|
||||
(. / "/") as [$dotted, $bits]
|
||||
| ($bits | tonumber) as $size
|
||||
|
||||
# get IP as binary string
|
||||
| {binary: (($dotted / ".") | map( tonumber | convert(2) | lpad(8; "0") ) | join("") )}
|
||||
|
||||
# replace the host part with all zeros
|
||||
| .binary |= .[0:$size] + "0" * (32 - $size)
|
||||
|
||||
# convert back to dotted-decimal
|
||||
| [.binary | explode | _nwise(8) | implode]
|
||||
| (map( to_i(2) | tostring ) | join(".")) as $canon
|
||||
|
||||
| $canon + "/" + $bits;
|
||||
|
||||
def tests:
|
||||
"87.70.141.1/22",
|
||||
"36.18.154.103/12",
|
||||
"62.62.197.11/29",
|
||||
"67.137.119.181/4",
|
||||
"161.214.74.21/24",
|
||||
"184.232.176.184/18"
|
||||
;
|
||||
|
||||
tests
|
||||
| "\(lpad(18)) -> \(canonicalize)"
|
||||
Loading…
Add table
Add a link
Reference in a new issue