add dcd.bit.Bitfield and related goodies
This commit is contained in:
parent
d72659088f
commit
10830999be
3 changed files with 73 additions and 28 deletions
|
@ -3,5 +3,6 @@
|
||||||
|
|
||||||
from . import argv
|
from . import argv
|
||||||
from . import aux
|
from . import aux
|
||||||
|
from . import bit
|
||||||
from . import cfg
|
from . import cfg
|
||||||
from . import dt
|
from . import dt
|
||||||
|
|
42
dcd/aux.py
42
dcd/aux.py
|
@ -18,6 +18,20 @@ def flatten_dict(root, glue=" ", prefix=[]):
|
||||||
|
|
||||||
# ----------------------------------------------------------------
|
# ----------------------------------------------------------------
|
||||||
|
|
||||||
|
def clamp(A, low, high):
|
||||||
|
"""
|
||||||
|
Clamps integer A to <low; high>
|
||||||
|
"""
|
||||||
|
|
||||||
|
if A < low:
|
||||||
|
return low
|
||||||
|
elif A > high:
|
||||||
|
return high
|
||||||
|
else:
|
||||||
|
return A
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
PASSPHRASE_VOWELS = "aeiuAEU"
|
PASSPHRASE_VOWELS = "aeiuAEU"
|
||||||
|
@ -50,34 +64,6 @@ def hash_password(p, hexdigest=True):
|
||||||
|
|
||||||
# ----------------------------------------------------------------
|
# ----------------------------------------------------------------
|
||||||
|
|
||||||
def raw_to_hex(raw, separator=" "):
|
|
||||||
"""
|
|
||||||
Converts a bytearray (or bytes) into its textual hexadecimal representation.
|
|
||||||
"""
|
|
||||||
|
|
||||||
output = []
|
|
||||||
|
|
||||||
for o in raw:
|
|
||||||
output.append(hex(o)[2:].zfill(2))
|
|
||||||
|
|
||||||
return separator.join(output)
|
|
||||||
|
|
||||||
def hex_to_raw(text, separator=" "):
|
|
||||||
"""
|
|
||||||
Converts a hexadecimal representation of a byte array into a bytearray.
|
|
||||||
"""
|
|
||||||
|
|
||||||
output = []
|
|
||||||
|
|
||||||
text = text.replace(separator, "")
|
|
||||||
|
|
||||||
for i in range(len(text)//2):
|
|
||||||
output.append(int(text[2*i:2*i+2], 16))
|
|
||||||
|
|
||||||
return bytearray(output)
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------
|
|
||||||
|
|
||||||
class DeleteOverlay:
|
class DeleteOverlay:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
58
dcd/bit.py
Normal file
58
dcd/bit.py
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
#! /usr/bin/env python3
|
||||||
|
# encoding: utf-8
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------
|
||||||
|
|
||||||
|
class Bitfield:
|
||||||
|
def __init__(self):
|
||||||
|
self.value = 0
|
||||||
|
self.length = 0
|
||||||
|
|
||||||
|
def shift_in(self, value, length):
|
||||||
|
self.value <<= length
|
||||||
|
self.value |= value
|
||||||
|
self.length += length
|
||||||
|
|
||||||
|
def dump(self, align=True):
|
||||||
|
value = self.value
|
||||||
|
length = self.length
|
||||||
|
|
||||||
|
pad = (8 - (length % 8)) % 8
|
||||||
|
value <<= pad
|
||||||
|
length += pad
|
||||||
|
|
||||||
|
Bs = []
|
||||||
|
|
||||||
|
for i in range(length // 8):
|
||||||
|
Bs.insert(0, value & 0xff)
|
||||||
|
value >>= 8
|
||||||
|
|
||||||
|
return bytes(Bs)
|
||||||
|
|
||||||
|
# ----------------------------------------------------------------
|
||||||
|
|
||||||
|
def raw_to_hex(raw, separator=" "):
|
||||||
|
"""
|
||||||
|
Converts a bytearray (or bytes) into its textual hexadecimal representation.
|
||||||
|
"""
|
||||||
|
|
||||||
|
output = []
|
||||||
|
|
||||||
|
for o in raw:
|
||||||
|
output.append(hex(o)[2:].zfill(2))
|
||||||
|
|
||||||
|
return separator.join(output)
|
||||||
|
|
||||||
|
def hex_to_raw(text, separator=" "):
|
||||||
|
"""
|
||||||
|
Converts a hexadecimal representation of a byte array into a bytearray.
|
||||||
|
"""
|
||||||
|
|
||||||
|
output = []
|
||||||
|
|
||||||
|
text = text.replace(separator, "")
|
||||||
|
|
||||||
|
for i in range(len(text)//2):
|
||||||
|
output.append(int(text[2*i:2*i+2], 16))
|
||||||
|
|
||||||
|
return bytearray(output)
|
Loading…
Add table
Add a link
Reference in a new issue