Ejemplo n.º 1
0
    def eval(self, string):
        """eval(string) -> value

        Evaluates a string in the context of values of this module.

        Example:

            >>> with context.local(arch = 'i386', os = 'linux'):
            ...    print(13 == constants.eval('SYS_execve + PROT_WRITE'))
            True
            >>> with context.local(arch = 'amd64', os = 'linux'):
            ...    print(61 == constants.eval('SYS_execve + PROT_WRITE'))
            True
            >>> with context.local(arch = 'amd64', os = 'linux'):
            ...    print(61 == constants.eval('SYS_execve + PROT_WRITE'))
            True
        """
        if not isinstance(string, str):
            return string

        simple = getattr(self, string, None)

        if simple is not None:
            return simple

        key = context.os, context.arch
        if key not in self._env_store:
            self._env_store[key] = {
                key: getattr(self, key)
                for key in dir(self) if not key.endswith('__')
            }

        return Constant('(%s)' % string,
                        safeeval.values(string, self._env_store[key]))
Ejemplo n.º 2
0
    def eval(self, string):
        """eval(string) -> value

        Evaluates a string in the context of values of this module.

        Example:

            >>> with context.local(arch = 'i386', os = 'linux'):
            ...    print 13 == constants.eval('SYS_execve + PROT_WRITE')
            True
            >>> with context.local(arch = 'amd64', os = 'linux'):
            ...    print 61 == constants.eval('SYS_execve + PROT_WRITE')
            True
            >>> with context.local(arch = 'amd64', os = 'linux'):
            ...    print 61 == constants.eval('SYS_execve + PROT_WRITE')
            True
        """
        if not isinstance(string, str):
            return string

        simple = getattr(self, string, None)

        if simple is not None:
            return simple

        key = context.os, context.arch
        if key not in self._env_store:
            self._env_store[key] = {key: getattr(self, key) for key in dir(self) if not key.endswith('__')}

        return Constant('(%s)' % string, safeeval.values(string, self._env_store[key]))
Ejemplo n.º 3
0
 def __init__(self, n):
     if isinstance(n, (str, unicode)):
         self.n = 0
         x = BitPolynom(2)
         try:
             for p in n.split('+'):
                 k = safeeval.values(p.strip(), {'x': x, 'X': x})
                 assert isinstance(k, (BitPolynom, int, long))
                 assert k >= 0
                 self.n ^= int(k)
         except (ValueError, NameError, AssertionError):
             raise ValueError("Not a valid polynomial: %s" % n)
     elif isinstance(n, (int, long)):
         if n >= 0:
             self.n = n
         else:
             raise ValueError("Polynomials cannot be negative: %d" % n)
     else:
         raise TypeError("Polynomial must be called with a string or integer")
Ejemplo n.º 4
0
    def eval(self, string):
        """eval(string) -> value

        Evaluates a string in the context of values of this module.

        Example:

            >>> with context.local(arch = 'i386', os = 'linux'):
            ...    print(13 == constants.eval('SYS_execve + PROT_WRITE'))
            True
            >>> with context.local(arch = 'amd64', os = 'linux'):
            ...    print(61 == constants.eval('SYS_execve + PROT_WRITE'))
            True
            >>> with context.local(arch = 'amd64', os = 'linux'):
            ...    print(61 == constants.eval('SYS_execve + PROT_WRITE'))
            True
        """
        if not isinstance(string, str):
            return string

        simple = getattr(self, string, None)

        if simple is not None:
            return simple

        key = context.os, context.arch
        if key not in self._env_store:
            self._env_store[key] = {
                key: getattr(self, key)
                for key in dir(self) if not key.endswith('__')
            }

        val = safeeval.values(string, self._env_store[key])

        # if the expression is not assembly-safe, it is not so vital to preserve it
        if set(string) & (set(bytearray(range(32)).decode())
                          | set('"#$\',.;@[\\]`{}')):
            string = val

        return Constant('(%s)' % string, val)