// Obfuscate all local symbols // - enumerate all local symbols and tell the symbol allocator // that it can be obfuscated. // - where there are `holes` in the rank mapping, tell the symbol // allocator to reserve those symbols. These holes are to be // filled by higher frequency symbols on the inner scopes. public void ObfuscateSymbols(RenderContext ctx, SymbolFrequency SymbolFrequency, SymbolAllocator Allocator, string prefix) { // Walk through local symbols int expectedRank = 0; foreach (var symbol in SymbolFrequency.Sort()) { // Ignore public symbols if (symbol.Accessibility != Accessibility.Private) continue; // Ignore non-local symbols if (symbol.Scope != Symbol.ScopeType.local) continue; // Reserve space for inner, higher frequency symbols if (symbol.Rank > expectedRank) { if (ctx.Compiler.Formatted && ctx.Compiler.SymbolInfo) { for (int r = expectedRank; r < symbol.Rank; r++) { ctx.StartLine(); ctx.AppendFormat("// #{0} reserved", r); } } Allocator.ReserveObfuscatedSymbols(symbol.Rank - expectedRank); } string newSymbol = Allocator.OnfuscateSymbol(symbol.Name); // Show info if (ctx.Compiler.Formatted && ctx.Compiler.SymbolInfo) { ctx.StartLine(); ctx.AppendFormat("// #{0} {3}{1} -> {3}{2}", symbol.Rank, symbol.Name, newSymbol, prefix); } expectedRank = symbol.Rank + 1; } }
// Prepare symbol ranks of all local symbols // - For each inner scope, merge this symbol frequency map // of this scope with that of the inner scope // - Update the rank of each symbol // - Repeat for each inner scope public void Prepare() { // Recurse through all scopes foreach (var i in InnerScopes) { i.Prepare(); } // Apply default accessibility to own local symbols foreach (var i in Symbols) { if (i.Value.Accessibility == Accessibility.Default) { i.Value.Accessibility = DefaultAccessibility; } } // Calculate base rank based on frequency of our own // local symbols Symbols.UpdateRanks(Symbols.Sort()); // Merge each inner scope and update ranks foreach (var i in InnerScopes) { var merged = new SymbolFrequency(); merged.CopyFrom(Symbols); merged.MergeSymbols(i.AllSymbols); Symbols.UpdateRanks(merged.Sort()); } // Member symbol Members.UpdateRanks(Members.Sort()); foreach (var i in InnerScopes) { var merged = new SymbolFrequency(); merged.CopyFrom(Members); merged.MergeSymbols(i.AllMembers); Members.UpdateRanks(merged.Sort()); } }
// Dump to stdout public void Dump(int indent) { Utils.WriteIndentedLine(indent, "Scope: {0}", Node); if (Symbols.Sort().Count>0) { Utils.WriteIndentedLine(indent + 1, "Local Symbols:"); Symbols.Dump(indent + 2); } foreach (var i in InnerScopes) { var merged = new SymbolFrequency(); merged.CopyFrom(Symbols); merged.MergeSymbols(i.AllSymbols); if (merged.Sort().Count > 0) { Utils.WriteIndentedLine(indent + 1, "Symbols after merge with {0}:", i.Node); merged.Dump(indent + 2); } } foreach (var i in InnerScopes) { i.Dump(indent + 1); } }