Lots of if/else stuff, looking up some rules and limits in config files and then calling into some libraries. I have used sqlite3_create_function() in the past for some stuff, but in this case SQL doesn't seem like the best language for the particular calculation/problem.