Canonicalize CIDR: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: fixed the default IPv4 address.) |
(→{{header|REXX}}: used a more generic approach.) |
||
Line 304: | Line 304: | ||
parse arg z . /*obtain optional argument from the CL.*/ |
parse arg z . /*obtain optional argument from the CL.*/ |
||
if z=='' | z=="," then z= '87.70.141.1/22' /*Not specified? Then use the default.*/ |
if z=='' | z=="," then z= '87.70.141.1/22' /*Not specified? Then use the default.*/ |
||
parse var z # '/' -0 mask /*get the address nodes & |
parse var z # '/' -0 mask /*get the address nodes & netaddr mask.*/ |
||
size= d2b( substr(mask, 2) ) /*size of binary network mask in bytes.*/ |
|||
shift= length(size % 2**8) /*compute the size of the binary shift.*/ |
|||
#= translate(#, , .) /*elide periods from IPv4 address nodes*/ |
#= translate(#, , .) /*elide periods from IPv4 address nodes*/ |
||
$= /*initialize the to-be reassembled IPv4*/ |
$= /*initialize the to-be reassembled IPv4*/ |
||
do j=1 for words(#) |
do j=1 for max( words(#), 4) /*process each of IP node, a min of 4. */ |
||
x= word( word(#, j) 0, 1) /*use a zero for omitted IP nodes. */ |
|||
if j>shift then do /*possibly elide some low─order bits. */ |
|||
_= right( d2b(x), 2**5, 0) /*convert X to binary, pad with zeros.*/ |
|||
x= b2d( (left(_, length(_)-shift) || left('', shift, 0) ) ) |
|||
⚫ | |||
$= $ x /*reconstruct (by abutment) IPv4 nodes.*/ |
$= $ x /*reconstruct (by abutment) IPv4 nodes.*/ |
||
end /*j*/ /*(REXX stores it's numbers in decimal)*/ |
end /*j*/ /*(REXX stores it's numbers in decimal)*/ |
||
⚫ | |||
say ' original IPv4 address: ' z /*display the original IPv4 address. */ |
say ' original IPv4 address: ' z /*display the original IPv4 address. */ |
||
say ' canonicalized address: ' translate( space($), ., " ") |
say ' canonicalized address: ' translate( space($), ., " ")mask /*canonicalized.*/ |
||
exit 0 /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
b2d: return x2d( b2x( arg(1) ) ) + 0 /*convert binary ───► decimal number.*/ |
|||
d2b: return x2b( d2x( arg(1) ) ) + 0 /* " decimal ───► binary " */</lang> |
|||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |