summaryrefslogtreecommitdiffstats
path: root/netaddr/ip/rfc1924.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 17:45:09 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 17:45:09 +0000
commitda1a8f12d7a38f67f3f464aaaffa851f929ae4ea (patch)
tree677688f3aeab7f324f266d106770165708522c2c /netaddr/ip/rfc1924.py
parentInitial commit. (diff)
downloadpython-netaddr-7e869254c654d0b5d5b8673ed3f143942f70296b.tar.xz
python-netaddr-7e869254c654d0b5d5b8673ed3f143942f70296b.zip
Adding upstream version 0.10.1.upstream/0.10.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--netaddr/ip/rfc1924.py61
1 files changed, 61 insertions, 0 deletions
diff --git a/netaddr/ip/rfc1924.py b/netaddr/ip/rfc1924.py
new file mode 100644
index 0000000..54fb96c
--- /dev/null
+++ b/netaddr/ip/rfc1924.py
@@ -0,0 +1,61 @@
+#-----------------------------------------------------------------------------
+# Copyright (c) 2008 by David P. D. Moss. All rights reserved.
+#
+# Released under the BSD license. See the LICENSE file for details.
+#-----------------------------------------------------------------------------
+"""A basic implementation of RFC 1924 ;-)"""
+
+from netaddr.core import AddrFormatError
+from netaddr.ip import IPAddress
+
+from netaddr.compat import _zip
+
+
+def chr_range(low, high):
+ """Returns all characters between low and high chars."""
+ return [chr(i) for i in range(ord(low), ord(high) + 1)]
+
+#: Base 85 integer index to character lookup table.
+BASE_85 = (
+ chr_range('0', '9') + chr_range('A', 'Z') +
+ chr_range('a', 'z') +
+ ['!', '#', '$', '%', '&', '(', ')', '*', '+', '-', ';', '<', '=', '>',
+ '?', '@', '^', '_', '`', '{', '|', '}', '~']
+)
+
+#: Base 85 digit to integer lookup table.
+BASE_85_DICT = dict(_zip(BASE_85, range(0, 86)))
+
+
+def ipv6_to_base85(addr):
+ """Convert a regular IPv6 address to base 85."""
+ ip = IPAddress(addr)
+ int_val = int(ip)
+
+ remainder = []
+ while int_val > 0:
+ remainder.append(int_val % 85)
+ int_val //= 85
+
+ encoded = ''.join([BASE_85[w] for w in reversed(remainder)])
+ leading_zeroes = (20 - len(encoded)) * "0"
+ return leading_zeroes + encoded
+
+
+def base85_to_ipv6(addr):
+ """
+ Convert a base 85 IPv6 address to its hexadecimal format.
+ """
+ tokens = list(addr)
+
+ if len(tokens) != 20:
+ raise AddrFormatError('Invalid base 85 IPv6 address: %r' % (addr,))
+
+ result = 0
+ for i, num in enumerate(reversed(tokens)):
+ num = BASE_85_DICT[num]
+ result += (num * 85 ** i)
+
+ ip = IPAddress(result, 6)
+
+ return str(ip)