]> WPIA git - gigi.git/blob - util-testing/club/wpia/gigi/localisation/TaintSource.java
upd: rename package name and all references to it
[gigi.git] / util-testing / club / wpia / gigi / localisation / TaintSource.java
1 package club.wpia.gigi.localisation;
2
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5
6 import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
7
8 public class TaintSource {
9
10     private String pack, cls, meth;
11
12     private int tgt;
13
14     private TaintSource maskOnly;
15
16     public TaintSource(String pack, String cls, String meth, int tgt) {
17         this(pack, cls, meth, tgt, null);
18     }
19
20     public TaintSource(String pack, String cls, String meth, int tgt, TaintSource maskOnly) {
21         this.pack = pack;
22         this.cls = cls;
23         this.meth = meth;
24         this.tgt = tgt;
25         this.maskOnly = maskOnly;
26
27     }
28
29     public TaintSource(MethodBinding mb) {
30         pack = new String(mb.declaringClass.qualifiedPackageName());
31         cls = new String(mb.declaringClass.qualifiedSourceName());
32         meth = new String(mb.readableName());
33     }
34
35     @Override
36     public int hashCode() {
37         final int prime = 31;
38         int result = 1;
39         result = prime * result + ((cls == null) ? 0 : cls.hashCode());
40         result = prime * result + ((meth == null) ? 0 : meth.hashCode());
41         result = prime * result + ((pack == null) ? 0 : pack.hashCode());
42         return result;
43     }
44
45     @Override
46     public String toString() {
47         StringBuffer res = new StringBuffer();
48         res.append("new TaintSource(");
49         res.append("\"" + pack + "\",");
50         res.append("\"" + cls + "\",");
51         res.append("\"" + meth + "\",0);");
52         return res.toString();
53     }
54
55     public String toConfLine() {
56         return pack + " " + cls + "." + meth + "," + tgt + (maskOnly == null ? "" : "=>" + maskOnly.toConfLine());
57     }
58
59     @Override
60     public boolean equals(Object obj) {
61         if (this == obj) {
62             return true;
63         }
64         if (obj == null) {
65             return false;
66         }
67         if (getClass() != obj.getClass()) {
68             return false;
69         }
70         TaintSource other = (TaintSource) obj;
71         if (cls == null) {
72             if (other.cls != null) {
73                 return false;
74             }
75         } else if ( !cls.equals(other.cls)) {
76             return false;
77         }
78         if (pack == null) {
79             if (other.pack != null) {
80                 return false;
81             }
82         } else if ( !pack.equals(other.pack)) {
83             return false;
84         }
85         if (meth == null) {
86             if (other.meth != null) {
87                 return false;
88             }
89         } else if ( !meth.equals(other.meth)) {
90             return false;
91         }
92         return true;
93     }
94
95     public static TaintSource parseTaint(String confline) {
96         // Pattern matches "Taint-lines"
97         // first part is package name up to space (may not include space or
98         // equals sign)
99         // second part is Class name [with inner class name] (may not include
100         // "=" but may include ".")
101         // third part is method name including params (may not include "=" or
102         // ".")
103         // fourth is index of tainted argument (seperated by "," from the rest)
104         Pattern p = Pattern.compile("^([^= ]*) ([^=]*)\\.([^=.]*\\([^)]*\\)),([0-9]+)");
105         Matcher m = p.matcher(confline);
106         if ( !m.find()) {
107             throw new Error(confline);
108         }
109         String pack = m.group(1);
110         String cls = m.group(2);
111         String meth = m.group(3);
112         int tgt = Integer.parseInt(m.group(4));
113         TaintSource mask = null;
114         if (m.end() != confline.length()) {
115             String s = confline.substring(m.end(), m.end() + 2);
116             if ( !s.equals("=>")) {
117                 throw new Error("malformed");
118             }
119             mask = parseTaint(confline.substring(m.end() + 2));
120         }
121         return new TaintSource(pack, cls, meth, tgt, mask);
122     }
123
124     public TaintSource getMaskOnly() {
125         return maskOnly;
126     }
127
128     public int getTgt() {
129         return tgt;
130     }
131
132 }