def eval(self, context, params=None): assert (len(params) == 3) r = '' try: red = int_convert(params[0]) green = int_convert(params[1]) blue = int_convert(params[2]) r = red + (green * 256) + (blue * 65536) except: pass log.debug("RGB: %r returns %r" % (self, r)) return r
def eval(self, context, params=None): if (len(params) > 2): params = params[-2:] assert len(params) == 2 s = params[0] # "If String contains the data value Null, Null is returned." if s == None: return None if not isinstance(s, basestring): s = str(s) start = 0 try: start = int_convert(params[1]) except: pass # "If Start is greater than the number of characters in String, # Right returns the whole string. if start > len(s): log.debug('Right: start>len(s) => return s') return s # Return empty string if start <= 0. if start <= 0: return "" # Return characters from end of string. r = s[(len(s) - start):] log.debug('Right: return s[%d:]=%r' % (start, r)) return r
def eval(self, context, params=None): assert (len(params) >= 3) r = '' try: # Pull out the arguments. rate = float(params[0]) nper = int_convert(params[1]) + 0.0 pv = float(params[2]) fv = 0 if (len(params) >= 4): fv = float(params[3]) typ = 0 if (len(params) >= 5): typ = float(params[4]) # Compute the payments. if (((1 + rate * typ) * (pow(1 + rate, nper) - 1)) != 0): r = ((-fv - pv * pow(1 + rate, nper)) * rate) / ( (1 + rate * typ) * (pow(1 + rate, nper) - 1)) else: r = 0 except: pass log.debug("Pmt: %r returns %r" % (self, r)) return r
def eval(self, context, params=None): assert len(params) > 0 # TODO: Actually implement this properly. # Get the conversion type to perform. conv = None if (len(params) > 1): conv = int_convert(params[1]) # Do the conversion. r = params[0] if (isinstance(r, str)): if (conv): if (conv == 1): r = r.upper() if (conv == 2): r = r.lower() if (conv == 64): # We are converting the string to unicode. ViperMonkey assumes # unless otherwise noted that all strings are unicode. Make sure # that the string is represented as a regular str object so that # it is treated as unicode. r = str(r) if (conv == 128): # The string is being converted from unicode to ascii. Mark this # by representing the string with the from_unicode_str class. r = from_unicode_str(r) elif (isinstance(r, list)): # Handle list of ASCII values. all_int = True for i in r: if (not isinstance(i, int)): all_int = False break if (all_int): tmp = "" for i in r: if (i < 0): continue try: tmp += chr(i) #if (conv == 64): # tmp += "\0" except: pass r = tmp else: log.error("StrConv: Unhandled type.") r = '' log.debug("StrConv: return %r" % r) return r
def eval(self, context, params=None): if ((len(params) > 0) and (params[0] == "ActiveDocument")): params = params[1:] assert len(params) in (2, 3) s = params[0] # "If String contains the data value Null, Null is returned." if s == None: return None if not isinstance(s, basestring): s = str(s) start = 0 try: start = int_convert(params[1]) except: pass # "If Start is greater than the number of characters in String, # Mid returns a zero-length string ("")." if start > len(s): log.debug('Mid: start>len(s) => return ""') return '' # What to do when start<=0 is not specified: if start <= 0: start = 1 # If length not specified, return up to the end of the string: if len(params) == 2: log.debug('Mid: no length specified, return s[%d:]=%r' % (start - 1, s[start - 1:])) return s[start - 1:] length = 0 try: length = int_convert(params[2]) except: pass # "If omitted or if there are fewer than Length characters in the text # (including the character at start), all characters from the start # position to the end of the string are returned." if start + length - 1 > len(s): log.debug('Mid: start+length-1>len(s), return s[%d:]' % (start - 1)) return s[start - 1:] # What to do when length<=0 is not specified: if length <= 0: return '' log.debug( 'Mid: return s[%d:%d]=%r' % (start - 1, start - 1 + length, s[start - 1:start - 1 + length])) return s[start - 1:start - 1 + length]
def eval(self, context, params=None): assert (len(params) == 1) r = '' try: num = int_convert(params[0]) r = hex(num).replace("0x", "").upper() except: pass log.debug("Hex: %r returns %r" % (self, r)) return r
def eval(self, context, params=None): assert (len(params) == 1) r = '' try: num = int_convert(params[0]) r = abs(num) except: pass log.debug("Abs: %r returns %r" % (self, r)) return r
def eval(self, context, params=None): assert (len(params) == 1) r = '' try: num = int_convert(params[0]) + 0.0 r = math.sqrt(num) except: pass log.debug("Sqr: %r returns %r" % (self, r)) return r
def eval(self, context, params=None): assert (len(params) == 1) num = params[0] r = '' try: r = int(math.copysign(1, int_convert(num))) except: pass log.debug("Sgn: %r returns %r" % (self, r)) return r
def eval(self, context, params=None): assert (len(params) == 2) r = '' try: num = int_convert(params[0]) char = params[1] r = char * num except: pass log.debug("String: %r returns %r" % (self, r)) return r
def eval(self, context, params=None): assert ((len(params) == 1) or (len(params) == 2)) r = '' try: num = float(params[0]) sig = 0 if (len(params) == 2): sig = int_convert(params(1)) r = round(num, sig) except: pass log.debug("Round: %r returns %r" % (self, r)) return r
def eval(self, context, params=None): assert len(params) > 0 # TODO: Actually implement this properly. val = params[0] try: if (isinstance(val, str) and (("e" in val) or ("E" in val))): r = int(decimal.Decimal(val)) else: r = int_convert(val) if ((r > 2147483647) or (r < -2147483647)): r = "ERROR" log.debug("Int: return %r" % r) return r except Exception as e: log.error("Int(): Invalid call int(%r) [%s]. Returning ''." % (val, str(e))) return ''
def eval(self, context, params=None): assert len(params) >= 2 s1 = params[0] s2 = params[1] method = 0 if (len(params) >= 3): try: method = int_convert(params[2]) except Exception as e: log.error("StrComp: Invalid comparison method. " + str(e)) pass if (method == 0): s1 = s1.lower() s2 = s2.lower() if (s1 == s2): return 0 if (s1 < s2): return -1 return 1
def eval(self, context, params=None): params = eval_args(self.params, context=context) str_params = repr(params)[1:-1] if (len(str_params) > 80): str_params = str_params[:80] + "..." log.info('calling Function: %s(%s)' % (self.name, str_params)) save = False for func in Function_Call.log_funcs: if (self.name.lower().endswith(func.lower())): save = True break if (save): context.report_action(self.name, params, 'Interesting Function Call') try: f = context.get(self.name) # Is this actually an array access? if (isinstance(f, list)): # Are we accessing an element? if (len(params) > 0): tmp = f # Try to guess whether we are accessing a character in a string. if ((len(f) == 1) and (isinstance(f[0], str))): tmp = f[0] log.debug('Array Access: %r[%r]' % (tmp, params[0])) index = int_convert(params[0]) try: r = tmp[index] log.debug('Returning: %r' % r) return r except: log.error('Array Access Failed: %r[%r]' % (tmp, params[0])) return 0 # Looks like we want the whole array (ex. foo()). else: return f log.debug('Calling: %r' % f) if (f is not None): if (not(isinstance(f, str)) and not(isinstance(f, list)) and not(isinstance(f, unicode))): try: # Call function. r = f.eval(context=context, params=params) # Set the values of the arguments passed as ByRef parameters. if (hasattr(f, "byref_params")): for byref_param_info in f.byref_params.keys(): arg_var_name = str(self.params[byref_param_info[1]]) context.set(arg_var_name, f.byref_params[byref_param_info]) # Return result. return r except AttributeError as e: log.error(str(f) + " has no eval() method. " + str(e)) return f elif (len(params) > 0): # Looks like this is actually an array access. log.debug("Looks like array access.") try: i = int_convert(params[0]) r = f[i] if (isinstance(f, str)): r = ord(r) log.debug("Return " + str(r)) return r except: log.error("Array access %r[%r] failed." % (f, params[0])) return 0 else: log.error("Improper type for function.") return None else: log.error('Function %r resolves to None' % self.name) return None except KeyError: # If something like Application.Run("foo", 12) is called, foo(12) will be run. # Try to handle that. func_name = str(self.name) if ((func_name == "Application.Run") or (func_name == "Run")): # Pull the name of what is being run from the 1st arg. new_func = params[0] # The remaining params are passed as arguments to the other function. new_params = params[1:] # See if we can run the other function. log.debug("Try indirect run of function '" + new_func + "'") try: s = context.get(new_func) return s.eval(context=context, params=new_params) except KeyError: pass log.warning('Function %r not found' % self.name) return None
def eval(self, context, params=None): n = int_convert(params[0]) r = " " * n return r