예제 #1
0
파일: policy.py 프로젝트: dusty141/pyPEBEL
def convertNumericalComparison(name, gt, value, nbits=32):
    """Given a numerical comparison in base-10, this function will construct a
    boolean formula representing the comparison in base-2.

    Unless specified the default word size for integers will be 32.

    @type name: str
    @param name: The name of attribute being compared.

    @type gt: bool
    @param gt: True if greater than else False

    @type value: int
    @param value: The value being compared against the attribute.

    @type nbits: int
    @param nbits: The word size used to represent integers.

    @rtype: str
    @return: Returns a string containing the comparison in Base-2.
    """
    # Find right most used bit
    i = 0
    while bool(1 << i & value) if gt else not bool(1 << i & value):
       i += 1

    p = leaf_policy(bitmarker(name, nbits, i, int(gt)))

    # For each remaining used bit in string
    for i in range(i+1, nbits):
        if gt:
            # if > then AND if bit is used else OR
            node_type = 2 if bool(1 << i & value) else 1
        else:
            # if < then OR if bit is used else AND
            node_type = 1 if bool(1 << i & value) else 2
        p = kof2_policy(node_type, p,
                        leaf_policy(bitmarker(name, nbits, i, int(gt))))

    return policyToString(p)
예제 #2
0
파일: policy.py 프로젝트: zjstone/pyPEBEL
def convertNumericalComparison(name, gt, value, nbits=32):
    """Given a numerical comparison in base-10, this function will construct a
    boolean formula representing the comparison in base-2.

    Unless specified the default word size for integers will be 32.

    @type name: str
    @param name: The name of attribute being compared.

    @type gt: bool
    @param gt: True if greater than else False

    @type value: int
    @param value: The value being compared against the attribute.

    @type nbits: int
    @param nbits: The word size used to represent integers.

    @rtype: str
    @return: Returns a string containing the comparison in Base-2.
    """
    # Find right most used bit
    i = 0
    while bool(1 << i & value) if gt else not bool(1 << i & value):
        i += 1

    p = leaf_policy(bitmarker(name, nbits, i, int(gt)))

    # For each remaining used bit in string
    for i in range(i + 1, nbits):
        if gt:
            # if > then AND if bit is used else OR
            node_type = 2 if bool(1 << i & value) else 1
        else:
            # if < then OR if bit is used else AND
            node_type = 1 if bool(1 << i & value) else 2
        p = kof2_policy(node_type, p,
                        leaf_policy(bitmarker(name, nbits, i, int(gt))))

    return policyToString(p)
예제 #3
0
파일: policy.py 프로젝트: dusty141/pyPEBEL
def constructNumericalAttribute(name, value, nbits):
    """Transforms an attribute assignment into the base-2 bit masking
    representation.

    @type name: str
    @param name: The name of the attribute.

    @type value: int
    @param value: The value being assigned to the attribute.

    @type nbits: int
    @param nbits: The word size used to represent integers.

    @rtype: List[str]
    @return: A list of bit markers representing the value of each bit
    in the base-2 representaiton.
    """
    attributes = [];
    for i in range(0,nbits):
        bit = int(bool(1 << i & value))
        attributes.append(bitmarker(name,nbits,i,bit))
    return attributes
예제 #4
0
파일: policy.py 프로젝트: zjstone/pyPEBEL
def constructNumericalAttribute(name, value, nbits):
    """Transforms an attribute assignment into the base-2 bit masking
    representation.

    @type name: str
    @param name: The name of the attribute.

    @type value: int
    @param value: The value being assigned to the attribute.

    @type nbits: int
    @param nbits: The word size used to represent integers.

    @rtype: List[str]
    @return: A list of bit markers representing the value of each bit
    in the base-2 representaiton.
    """
    attributes = []
    for i in range(0, nbits):
        bit = int(bool(1 << i & value))
        attributes.append(bitmarker(name, nbits, i, bit))
    return attributes