def GenerateValidDominos(Start = 0, End = 4095): logging.debug("Checking Domino Values from {} to {}" . format(Start, End)) bPass = 1 Row1 = 0 Row2 = 0 ValidValues = [] for x in range(Start,(End+1),1): bPass = True DPips = BitArray('0x000') DPips.uint = x Row1 = DPips[:6] Row2 = DPips[-6:] logging.debug("Index Value:{} Binary Value:{}" .format(x,DPips.bin)) #Row 1 and 2 Pip Count R1_PC = len(list(Row1.findall([1]))) R2_PC = len(list(Row2.findall([1]))) #Total pips between rows must be 6. if (R1_PC + R2_PC) != 6: bPass = False logging.debug("Row 1 Value:{} Binary Value:{} Pip Count:{}" .format(Row1.uint, Row1.bin, R1_PC)) logging.debug("Row 2 Value:{} Binary Value:{} Pip Count:{}" .format(Row2.uint, Row2.bin, R2_PC)) logging.debug("Pip count validity: {}" . format(bPass)) #Generate the reverse of the bit stream. DFlip = BitArray('0x000') DFlip.uint = x DFlip.reverse() ReversedValue = DFlip.uint logging.debug("Flipped Row Value:{} Binary Value:{}" .format(ReversedValue, DFlip.bin)) #The domino is a mirror iamge of itself and is invalid. if (ReversedValue == x): logging.debug("Fail: Mirror Domino") bPass = False #The rotated domino exists as a prior value. if (ReversedValue in ValidValues): logging.debug("Fail: Exists previously (rotated)") bPass = False #The specified domino passed all rules and is valid. if (bPass == True): ValidValues.append(x) logging.debug("Passed: Added Value {}({}) to list." . format(x, DPips.bin)) logging.debug("---") logging.debug(ValidValues) return ValidValues
def testCreationFromUint(self): s = BitArray(uint=15, length=6) self.assertEqual(s.bin, "001111") s = BitArray(uint=0, length=1) self.assertEqual(s.bin, "0") s.uint = 1 self.assertEqual(s.uint, 1) s = BitArray(length=8) s.uint = 0 self.assertEqual(s.uint, 0) s.uint = 255 self.assertEqual(s.uint, 255) self.assertEqual(s.len, 8) self.assertRaises(bitstring.CreationError, s._setuint, 256)
def testCreationFromUint(self): s = BitArray(uint=15, length=6) self.assertEqual(s.bin, '0b001111') s = BitArray(uint=0, length=1) self.assertEqual(s.bin, '0b0') s.uint = 1 self.assertEqual(s.uint, 1) s = BitArray(length=8) s.uint = 0 self.assertEqual(s.uint, 0) s.uint = 255 self.assertEqual(s.uint, 255) self.assertEqual(s.len, 8) self.assertRaises(bitstring.CreationError, s._setuint, 256)
def joshua_saxby_scrambler(): """ My own implementation of the CD sector scrambler, based partly on commentary and analysis from Kris Kaspersky's aforementioned book, and also based on my interpretation of the ECMA-130 Annex B scrambler description """ # start value has least significant bit set to 1 register = BitArray(uint=0b0000000000000001, length=16) # NOTE: BitArray library addresses bits in big-endian order # Bit clock = XOR least two significant bits register[0] = register[14] ^ register[15] yield register.uint while True: # XOR the register with a copy of itself shifted one right copy = register.uint >> 1 register.uint = register.uint ^ copy # shift the result to the right again register.uint >>= 1 # Bit clock = XOR least two significant bits register[0] = register[14] ^ register[15] # Kris Kaspersky claims that if the first bit (LSB) is set to 1, we # should toggle the second MSB (bit right of MSB) # The following if block does this, and it DOES cause the output of # this implementation to produce sequences identical to other third # party produced sequences, but it is not clear yet how this relates # to the reference material, ECMA-130 Annex B if register[15]: register[1] = not register[1] yield register.uint
def __place_domino(self, x, y, value, canvas): #Paints Domino, X,Y @ Left Corner of Domino #PYX Library if (self.RadiusCorners == True): w = self.Domino_Width h = self.Domino_Height r = self.CornerRadius pyx_circle = pyx.path.circle(x + r, y + r, r) canvas.fill(pyx_circle, [pyx.color.rgb.black]) pyx_circle = pyx.path.circle((x + w) - r, y + r, r) canvas.fill(pyx_circle, [pyx.color.rgb.black]) pyx_circle = pyx.path.circle(x + r, (y + h) - r, r) canvas.fill(pyx_circle, [pyx.color.rgb.black]) pyx_circle = pyx.path.circle((x + w) - r, (y + h) - r, r) canvas.fill(pyx_circle, [pyx.color.rgb.black]) pyx_rec = pyx.path.rect(x, y + r, w, h - (2 * r)) canvas.fill(pyx_rec, [pyx.color.rgb.black]) pyx_rec = pyx.path.rect(x + r, y, w - (2 * r), h) canvas.fill(pyx_rec, [pyx.color.rgb.black]) else: pyx_rec = pyx.path.rect(x, y, self.Domino_Width, self.Domino_Height) canvas.fill(pyx_rec, [pyx.color.rgb.black]) px = 0 py = 0 DPips = BitArray('0x000') DPips.uint = int(value) #BitArrays are big endian by default BitRow = [BitArray(6), BitArray(6)] BitRow[0] = DPips[:6] # First Row BitRow[1] = DPips[-6:] # Second Row for py in range(2): for px in range(8): #First and list pip are always placed #Pip 1-6 based on bitrow if (px == 0) or (px == 7) or (BitRow[py][px - 1] is True): YCord = y + (self.Pip_Padding + (self.Pip_Radius) + ((py * 2) * (self.Pip_Diameter))) XCord = x + (self.Pip_Padding + (self.Pip_Radius) + ((px * 2) * (self.Pip_Diameter))) pyx_circle = pyx.path.circle(XCord, YCord, self.Pip_Radius) canvas.fill(pyx_circle, [pyx.color.rgb.white]) return ()
def encode(word): # we build a dictionnary with all possible symbols and associate each of them with a number symbols ={} for letter in string.printable: bits = BitArray(BITS_USED) bits.uint = len(symbols) symbols[letter] = bits # the code we'll return code = BitStream() # a sequence of characters sequence = "" # for each character in the word we are encoding for c in word: # we add it to the sequence we are currently considering temp = sequence + c # if we know the result we just keep it and parse the next character if temp in symbols: sequence = temp # otherwise else: # we add the previously known sequence to the code code.append(symbols[sequence]) # and add the new sequence to our dictionnary of known sequences if len(symbols) < 1<<BITS_USED: bits = BitArray(BITS_USED) bits.uint = len(symbols) symbols[temp] = bits # the sequence is reset to only the new character sequence = c # there is usually a last unincoded sequence to be added code.append(symbols[sequence]) return code