<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function <EOL> import sys <EOL> import os <EOL> import struct <EOL> import string <EOL> from . sixing import * <EOL> from . odicting import odict <EOL> from . consoling import getConsole <EOL> console = getConsole ( ) <EOL> def binize ( n = <NUM_LIT:0> , size = <NUM_LIT:8> ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" . join ( [ str ( ( n >> y ) & <NUM_LIT:1> ) for y in range ( size - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) ] ) <EOL> def unbinize ( u = u'<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> n = <NUM_LIT:0> <EOL> for bit in u : <EOL> n <<= <NUM_LIT:1> <EOL> n |= <NUM_LIT:1> if int ( bit ) else <NUM_LIT:0> <EOL> return n <EOL> def hexize ( b = b'<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> h = u'<STR_LIT>' <EOL> for i in range ( len ( b ) ) : <EOL> h += "<STR_LIT>" . format ( ord ( b [ i : i + <NUM_LIT:1> ] ) ) <EOL> return h <EOL> def unhexize ( h = u'<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> hh = h <EOL> for c in hh : <EOL> if c not in string . hexdigits : <EOL> h = h . replace ( c , '<STR_LIT>' ) <EOL> if len ( h ) % <NUM_LIT:2> : <EOL> h = u'<STR_LIT:0>' + h <EOL> b = b'<STR_LIT>' <EOL> for i in xrange ( <NUM_LIT:0> , len ( h ) , <NUM_LIT:2> ) : <EOL> s = h [ i : i + <NUM_LIT:2> ] <EOL> b = b + struct . pack ( '<STR_LIT>' , int ( s , <NUM_LIT:16> ) ) <EOL> return b <EOL> def hexify ( b = bytearray ( [ ] ) ) : <EOL> """<STR_LIT>""" <EOL> b = bytearray ( b ) <EOL> h = u'<STR_LIT>' <EOL> for byte in b : <EOL> h += "<STR_LIT>" . format ( byte ) <EOL> return h <EOL> def unhexify ( h = u'<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> hh = h <EOL> for c in hh : <EOL> if c not in string . hexdigits : <EOL> h = h . replace ( c , '<STR_LIT>' ) <EOL> if len ( h ) % <NUM_LIT:2> : <EOL> h = u'<STR_LIT:0>' + h <EOL> b = bytearray ( [ ] ) <EOL> for i in xrange ( <NUM_LIT:0> , len ( h ) , <NUM_LIT:2> ) : <EOL> s = h [ i : i + <NUM_LIT:2> ] <EOL> b . append ( int ( s , <NUM_LIT:16> ) ) <EOL> return b <EOL> def bytify ( n = <NUM_LIT:0> , size = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> b = bytearray ( [ ] ) <EOL> count = <NUM_LIT:0> <EOL> while n or ( count < size ) : <EOL> b . insert ( <NUM_LIT:0> , n & <NUM_LIT> ) <EOL> count += <NUM_LIT:1> <EOL> n >>= <NUM_LIT:8> <EOL> return b <EOL> def unbytify ( b = bytearray ( [ ] ) ) : <EOL> """<STR_LIT>""" <EOL> b = bytearray ( b ) <EOL> b . reverse ( ) <EOL> n = <NUM_LIT:0> <EOL> while b : <EOL> n <<= <NUM_LIT:8> <EOL> n += b . pop ( ) <EOL> return n <EOL> def packify ( fmt = u'<STR_LIT>' , fields = [ <NUM_LIT> ] , size = None ) : <EOL> """<STR_LIT>""" <EOL> tbfl = sum ( ( int ( x ) for x in fmt . split ( ) ) ) <EOL> if size is None : <EOL> size = ( tbfl // <NUM_LIT:8> ) + <NUM_LIT:1> if tbfl % <NUM_LIT:8> else tbfl // <NUM_LIT:8> <EOL> if not ( <NUM_LIT:0> <= tbfl <= ( size * <NUM_LIT:8> ) ) : <EOL> raise ValueError ( "<STR_LIT>" . format ( size * <NUM_LIT:8> ) ) <EOL> n = <NUM_LIT:0> <EOL> bfp = <NUM_LIT:8> * size <EOL> bu = <NUM_LIT:0> <EOL> for i , bfmt in enumerate ( fmt . split ( ) ) : <EOL> bits = <NUM_LIT> <EOL> bfl = int ( bfmt ) <EOL> bu += bfl <EOL> if bfl == <NUM_LIT:1> : <EOL> if fields [ i ] : <EOL> bits = <NUM_LIT> <EOL> else : <EOL> bits = <NUM_LIT> <EOL> else : <EOL> bits = fields [ i ] & ( <NUM_LIT:2> ** bfl - <NUM_LIT:1> ) <EOL> bits <<= ( bfp - bfl ) <EOL> n |= bits <EOL> bfp -= bfl <EOL> return bytify ( n , size ) <EOL> def packifyInto ( b , fmt = u'<STR_LIT>' , fields = [ <NUM_LIT> ] , size = None , offset = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> tbfl = sum ( ( int ( x ) for x in fmt . split ( ) ) ) <EOL> if size is None : <EOL> size = ( tbfl // <NUM_LIT:8> ) + <NUM_LIT:1> if tbfl % <NUM_LIT:8> else tbfl // <NUM_LIT:8> <EOL> if not ( <NUM_LIT:0> <= tbfl <= ( size * <NUM_LIT:8> ) ) : <EOL> raise ValueError ( "<STR_LIT>" . format ( size * <NUM_LIT:8> ) ) <EOL> if len ( b ) < ( offset + size ) : <EOL> b . extend ( [ <NUM_LIT> ] * ( offset + size - len ( b ) ) ) <EOL> n = <NUM_LIT:0> <EOL> bfp = <NUM_LIT:8> * size <EOL> bu = <NUM_LIT:0> <EOL> for i , bfmt in enumerate ( fmt . split ( ) ) : <EOL> bits = <NUM_LIT> <EOL> bfl = int ( bfmt ) <EOL> bu += bfl <EOL> if bfl == <NUM_LIT:1> : <EOL> if fields [ i ] : <EOL> bits = <NUM_LIT> <EOL> else : <EOL> bits = <NUM_LIT> <EOL> else : <EOL> bits = fields [ i ] & ( <NUM_LIT:2> ** bfl - <NUM_LIT:1> ) <EOL> bits <<= ( bfp - bfl ) <EOL> n |= bits <EOL> bfp -= bfl <EOL> count = <NUM_LIT:0> <EOL> while n or ( count < size ) : <EOL> b [ offset + size - <NUM_LIT:1> - count ] = n & <NUM_LIT> <EOL> count += <NUM_LIT:1> <EOL> n >>= <NUM_LIT:8> <EOL> return size <EOL> def unpackify ( fmt = u'<STR_LIT>' , b = bytearray ( [ <NUM_LIT> ] ) , boolean = False , size = None ) : <EOL> """<STR_LIT>""" <EOL> b = bytearray ( b ) <EOL> tbfl = sum ( ( int ( x ) for x in fmt . split ( ) ) ) <EOL> if size is None : <EOL> size = ( tbfl // <NUM_LIT:8> ) + <NUM_LIT:1> if tbfl % <NUM_LIT:8> else tbfl // <NUM_LIT:8> <EOL> if not ( <NUM_LIT:0> <= tbfl <= ( size * <NUM_LIT:8> ) ) : <EOL> raise ValueError ( "<STR_LIT>" . format ( size * <NUM_LIT:8> ) ) <EOL> b = b [ : size ] <EOL> fields = [ ] <EOL> bfp = <NUM_LIT:8> * size <EOL> bu = <NUM_LIT:0> <EOL> n = unbytify ( b ) <EOL> for i , bfmt in enumerate ( fmt . split ( ) ) : <EOL> bfl = int ( bfmt ) <EOL> bu += bfl <EOL> mask = ( <NUM_LIT:2> ** bfl - <NUM_LIT:1> ) << ( bfp - bfl ) <EOL> bits = n & mask <EOL> bits >>= ( bfp - bfl ) <EOL> if bfl == <NUM_LIT:1> and boolean : <EOL> if bits : <EOL> bits = True <EOL> else : <EOL> bits = False <EOL> fields . append ( bits ) <EOL> bfp -= bfl <EOL> if bfp != <NUM_LIT:0> : <EOL> bfl = bfp <EOL> mask = ( <NUM_LIT:2> ** bfl - <NUM_LIT:1> ) <EOL> bits = n & mask <EOL> if bfl == <NUM_LIT:1> and boolean : <EOL> if bits : <EOL> bits = True <EOL> else : <EOL> bits = False <EOL> fields . append ( bits ) <EOL> return tuple ( fields ) <EOL> def signExtend ( x , n = <NUM_LIT:8> ) : <EOL> """<STR_LIT>""" <EOL> m = <NUM_LIT:1> << ( n - <NUM_LIT:1> ) <EOL> r = ( x ^ m ) - m <EOL> return ( r ) <EOL> def packByte ( fmt = b'<STR_LIT>' , fields = [ <NUM_LIT> ] ) : <EOL> """<STR_LIT>""" <EOL> fmt = bytes ( fmt ) <EOL> byte = <NUM_LIT> <EOL> bfp = <NUM_LIT:8> <EOL> bu = <NUM_LIT:0> <EOL> for i in range ( len ( fmt ) ) : <EOL> bits = <NUM_LIT> <EOL> bfl = int ( fmt [ i : i + <NUM_LIT:1> ] ) <EOL> if not ( <NUM_LIT:0> < bfl <= <NUM_LIT:8> ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> bu += bfl <EOL> if bu > <NUM_LIT:8> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if bfl == <NUM_LIT:1> : <EOL> if fields [ i ] : <EOL> bits = <NUM_LIT> <EOL> else : <EOL> bits = <NUM_LIT> <EOL> else : <EOL> bits = fields [ i ] & ( <NUM_LIT:2> ** bfl - <NUM_LIT:1> ) <EOL> bits <<= ( bfp - bfl ) <EOL> byte |= bits <EOL> bfp -= bfl <EOL> console . profuse ( "<STR_LIT>" . format ( byte ) ) <EOL> return byte <EOL> def unpackByte ( fmt = b'<STR_LIT>' , byte = <NUM_LIT> , boolean = True ) : <EOL> """<STR_LIT>""" <EOL> fmt = bytes ( fmt ) <EOL> fields = [ ] <EOL> bfp = <NUM_LIT:8> <EOL> bu = <NUM_LIT:0> <EOL> byte &= <NUM_LIT> <EOL> for i in range ( len ( fmt ) ) : <EOL> bfl = int ( fmt [ i : i + <NUM_LIT:1> ] ) <EOL> if not ( <NUM_LIT:0> < bfl <= <NUM_LIT:8> ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> bu += bfl <EOL> if bu > <NUM_LIT:8> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> mask = ( <NUM_LIT:2> ** bfl - <NUM_LIT:1> ) << ( bfp - bfl ) <EOL> bits = byte & mask <EOL> bits >>= ( bfp - bfl ) <EOL> if bfl == <NUM_LIT:1> and boolean : <EOL> if bits : <EOL> bits = True <EOL> else : <EOL> bits = False <EOL> fields . append ( bits ) <EOL> bfp -= bfl <EOL> return tuple ( fields ) <EOL> def denary2BinaryStr ( n , l = <NUM_LIT:8> ) : <EOL> """<STR_LIT>""" <EOL> bs = '<STR_LIT>' <EOL> if n < <NUM_LIT:0> : raise ValueError ( "<STR_LIT>" ) <EOL> if n == <NUM_LIT:0> : return '<STR_LIT:0>' <EOL> while n > <NUM_LIT:0> : <EOL> bs = str ( n % <NUM_LIT:2> ) + bs <EOL> n = n >> <NUM_LIT:1> <EOL> return bs . rjust ( l , '<STR_LIT:0>' ) <EOL> def dec2BinStr ( n , count = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" . join ( [ str ( ( n >> y ) & <NUM_LIT:1> ) for y in range ( count - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) ] ) <EOL> def printHex ( s , chunk = <NUM_LIT:0> , chunks = <NUM_LIT:0> , silent = False , separator = '<STR_LIT:.>' ) : <EOL> """<STR_LIT>""" <EOL> if ( chunk < <NUM_LIT:0> ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if ( chunks < <NUM_LIT:0> ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> slen = len ( s ) <EOL> if chunk == <NUM_LIT:0> : <EOL> chunk = slen <EOL> if chunks == <NUM_LIT:0> : <EOL> line = slen <EOL> else : <EOL> line = chunk * chunks <EOL> cc = <NUM_LIT:0> <EOL> ps = '<STR_LIT>' <EOL> for i in range ( len ( s ) ) : <EOL> ps += ( "<STR_LIT>" % ord ( s [ i : i + <NUM_LIT:1> ] ) ) <EOL> if ( ( i + <NUM_LIT:1> ) % line ) and ( ( i + <NUM_LIT:1> ) % slen ) : <EOL> if not ( ( i + <NUM_LIT:1> ) % chunk ) : <EOL> ps += '<STR_LIT:U+0020>' <EOL> else : <EOL> ps += separator <EOL> elif ( i + <NUM_LIT:1> ) != slen : <EOL> ps += '<STR_LIT:\n>' <EOL> if not silent : <EOL> console . terse ( "<STR_LIT>" . format ( ps ) ) <EOL> return ps <EOL> def printDecimal ( s ) : <EOL> """<STR_LIT>""" <EOL> ps = '<STR_LIT>' <EOL> for i in range ( len ( s ) ) : <EOL> ps = ps + ( "<STR_LIT>" % ord ( s [ i : i + <NUM_LIT:1> ] ) ) <EOL> ps = ps [ <NUM_LIT:0> : - <NUM_LIT:1> ] <EOL> print ( ps ) </s>
