Obfuscator.cpp File Reference

#include "AstNode.h"
#include "tree.h"
#include "AstContainer.h"
#include "Obfuscator.h"
#include "Utils.h"
#include <cstdlib>
#include <string>
#include <map>
#include <iostream>

Include dependency graph for Obfuscator.cpp:

Go to the source code of this file.

Defines

#define srand48   srand
#define lrand48   rand

Functions

string generateName ()
void applyModification (Ast &ast)
void insert_branch (tree< AstNode >::iterator where, tree< AstNode >::iterator from, tree< AstNode > &tr_where, const tree< AstNode > &tr_from)
void copy_branch (tree< AstNode >::iterator where, tree< AstNode >::iterator from, tree< AstNode > &tr_where)
void move_branch (const tree< AstNode >::iterator &where, const tree< AstNode >::iterator &from, tree< AstNode > &tr_where)
tree< AstNode >::iterator rewind (const tree< AstNode >::iterator &from, const string &untilStr, const tree< AstNode > &tr)
tree< AstNode >::iterator forward (const tree< AstNode >::iterator &from, const string &untilStr, const tree< AstNode > &tr)
void clean_pattern (tree< AstNode > &tr)
bool insert_statement (const tree< AstNode >::iterator &where, const tree< AstNode >::iterator &what, tree< AstNode > &tr)

Variables

const unsigned length = 16


Define Documentation

#define lrand48   rand

Referenced by generateName().

#define srand48   srand

Referenced by generateName().


Function Documentation

void applyModification ( Ast ast  ) 

Apply some modification to the current tree. This is used to load the external definition and change some names

Definition at line 55 of file Obfuscator.cpp.

References tree< T, tree_node_allocator >::begin(), tree< T, tree_node_allocator >::end(), generateName(), Ast::getTreePtr(), utils::replace(), and utils::start_with().

Referenced by ControlFlow::operator()().

00055                                  {
00056     tree<AstNode> *tr = ast.getTreePtr();
00057     map<string, string> randNames;
00058     string randName = "";
00059     string randFunc = "";
00060     for (tree<AstNode>::iterator iter=tr->begin(); iter!=tr->end(); ++iter) {
00061         string valueType = iter->getValue();
00062         if (utils::start_with(valueType,"$rand_name")
00063         ||  utils::start_with(valueType,"rand_func_name")
00064         ||  utils::start_with(valueType,"rand_class_name")) {
00065             string genValue = generateName();
00066             if (valueType[0] == '$')
00067                 genValue = "$" + genValue;
00068             if (randNames.find(valueType) == randNames.end())
00069                 randNames.insert(make_pair(valueType, genValue));
00070             iter->setValue(randNames[valueType]);
00071         }
00072         else if (utils::start_with(valueType,"$param_")) {
00073             string name = valueType;
00074             utils::replace(name, "param_","");
00075             iter->setValue(name);
00076         }
00077     }
00078 }

Here is the call graph for this function:

Here is the caller graph for this function:

void clean_pattern ( tree< AstNode > &  tr  ) 

Clean the possible patterns annotations: $enter_new_statement ...

Definition at line 155 of file Obfuscator.cpp.

References tree< T, tree_node_allocator >::begin(), tree< T, tree_node_allocator >::end(), tree< T, tree_node_allocator >::erase(), and rewind().

Referenced by ControlFlow::operator()().

00156 {
00157     for (tree<AstNode>::iterator iter=tr.begin(); iter!=tr.end(); ++iter) {
00158         if (iter->getType() == "text" && iter->getValue() == "$enter_the_new_statement") {
00159             iter = rewind(iter, "statement", tr);
00160             tr.erase(iter);
00161             break;
00162         }
00163     }
00164 }

Here is the call graph for this function:

Here is the caller graph for this function:

void copy_branch ( tree< AstNode >::iterator  where,
tree< AstNode >::iterator  from,
tree< AstNode > &  tr_where 
)

Copy for duplicating a branch

Definition at line 103 of file Obfuscator.cpp.

References tree< T, tree_node_allocator >::append_child(), tree< T, tree_node_allocator >::begin(), tree< T, tree_node_allocator >::end(), and tree< T, tree_node_allocator >::number_of_children().

Referenced by move_branch().

00104 {
00105     if (tr_where.number_of_children(from) == 0) {
00106         // simply add the node to it
00107         tr_where.append_child(where, *from);
00108     }
00109     else {
00110         where = tr_where.append_child(where, *from);
00111         tree<AstNode>::sibling_iterator cter;
00112         for (cter = tr_where.begin(from); cter != tr_where.end(from); ++cter)
00113         {
00114             copy_branch(where, cter, tr_where);
00115         }
00116     }
00117 }

Here is the call graph for this function:

Here is the caller graph for this function:

tree<AstNode>::iterator forward ( const tree< AstNode >::iterator &  from,
const string &  untilStr,
const tree< AstNode > &  tr 
)

Forward in a tree until the value

Definition at line 142 of file Obfuscator.cpp.

References tree< T, tree_node_allocator >::child().

00142                                                                                                                   {
00143     tree<AstNode>::iterator ret = from;
00144     while(ret->getType() != untilStr) { 
00145         ret = tr.child(ret, 0);
00146     }
00147     return ret;
00148 }

Here is the call graph for this function:

string generateName (  ) 

Generate a randome name

Definition at line 34 of file Obfuscator.cpp.

References length, lrand48, and srand48.

Referenced by applyModification(), RenameVariable::operator()(), RenameFunction::operator()(), and RenameClass::operator()().

00034                       {
00035     static const string ref = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
00036     static const unsigned refL = ref.length();
00037     string ret;
00038 #ifndef srand48
00039     #define srand48 srand
00040     #define lrand48 rand
00041 #endif
00042     srand48(time(NULL) + lrand48() % 65527);
00043     ret += ref[((lrand48() % (refL-36))) + 36];
00044     for (unsigned i=1; i < length; i++) {
00045         ret += ref[((lrand48() % refL))];
00046     }
00047     return ret;
00048 }

Here is the caller graph for this function:

void insert_branch ( tree< AstNode >::iterator  where,
tree< AstNode >::iterator  from,
tree< AstNode > &  tr_where,
const tree< AstNode > &  tr_from 
)

Insert all sub-jt into the sub-it

Definition at line 83 of file Obfuscator.cpp.

References tree< T, tree_node_allocator >::append_child(), tree< T, tree_node_allocator >::begin(), tree< T, tree_node_allocator >::end(), and tree< T, tree_node_allocator >::number_of_children().

Referenced by ControlFlow::operator()().

00085 {
00086     if (tr_from.number_of_children(from) == 0) {
00087         // simply add the node to it
00088         tr_where.append_child(where, *from);
00089     }
00090     else {
00091         where = tr_where.append_child(where, *from);
00092         tree<AstNode>::sibling_iterator cter;
00093         for (cter = tr_from.begin(from); cter != tr_from.end(from); ++cter) 
00094         {
00095             insert_branch(where, cter, tr_where, tr_from);
00096         }
00097     }
00098 }

Here is the call graph for this function:

Here is the caller graph for this function:

bool insert_statement ( const tree< AstNode >::iterator &  where,
const tree< AstNode >::iterator &  what,
tree< AstNode > &  tr 
)

Insert a new statement.

Definition at line 172 of file Obfuscator.cpp.

References tree< T, tree_node_allocator >::append_child(), tree< T, tree_node_allocator >::child(), move_branch(), and tree< T, tree_node_allocator >::parent().

Referenced by ControlFlow::operator()().

00173 {
00174     if (where->getType() == "text" && where->getValue() == "$enter_the_new_statement") {
00175         // rewind to the inner_statement_list
00176         tree<AstNode>::iterator top = where;
00177         do {
00178             top = tr.parent(top);
00179         } while (top->getType() != "inner_statement" && tr.child(top,0)->getType() != "statement");
00180         top = tr.parent(top);
00181         if (top->getType() != "inner_statement_list")
00182             return false;
00183         else {
00184             top = tr.append_child(top, AstNode("inner_statement_list"));
00185             top = tr.append_child(top, AstNode("inner_statement"));
00186             if (what->getType() == "statement") {
00187                 top = tr.append_child(top, AstNode("statement"));
00188                 //cout << "Seems to be okay..." << endl;
00189                 move_branch(top, what, tr);
00190             }
00191         }
00192         return true;
00193     }
00194     return false;
00195 }

Here is the call graph for this function:

Here is the caller graph for this function:

void move_branch ( const tree< AstNode >::iterator &  where,
const tree< AstNode >::iterator &  from,
tree< AstNode > &  tr_where 
)

Move all the branches from 'from' to 'where' on the tree 'tr_where'

Definition at line 122 of file Obfuscator.cpp.

References copy_branch(), and tree< T, tree_node_allocator >::erase().

Referenced by insert_statement().

00123 {
00124     copy_branch(where, from, tr_where);
00125     tr_where.erase(from);
00126 }

Here is the call graph for this function:

Here is the caller graph for this function:

tree<AstNode>::iterator rewind ( const tree< AstNode >::iterator &  from,
const string &  untilStr,
const tree< AstNode > &  tr 
)

Rewind in a tree until the value

Definition at line 131 of file Obfuscator.cpp.

References tree< T, tree_node_allocator >::parent().

Referenced by clean_pattern().

00131                                                                                                                  {
00132     tree<AstNode>::iterator ret = from;
00133     while(ret->getType() != untilStr) { 
00134         ret = tr.parent(ret);
00135     }
00136     return ret;
00137 }

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

const unsigned length = 16

Definition at line 29 of file Obfuscator.cpp.

Referenced by generateName().


Generated on Wed Feb 27 20:31:21 2008 for php.ast.svn.src. by  doxygen 1.5.3