| 1 19 20 25 26 27 package soot.tagkit; 28 import soot.options.*; 29 30 import java.util.*; 31 import soot.baf.*; 32 import soot.*; 33 34 35 39 public class CodeAttribute extends JasminAttribute 40 { 41 protected List mUnits; 42 protected List mTags; 43 44 private byte[] value; 45 46 private String name = "CodeAtribute"; 47 48 public CodeAttribute(){} 49 50 51 public CodeAttribute(String name) 52 { 53 this.name = name; 54 } 55 56 57 public CodeAttribute(String name, List units, List tags) 58 { 59 this.name = name; 60 this.mUnits = units; 61 this.mTags = tags; 62 } 63 64 65 public String toString() 66 { 67 return name; 68 } 69 70 71 public String getName() 72 { 73 return name; 74 } 75 76 77 public void setValue(byte[] v) 78 { 79 this.value = v; 80 } 81 82 83 public byte[] getValue() throws AttributeValueException 84 { 85 if (value == null) 86 throw new AttributeValueException(); 87 else 88 return value; 89 } 90 91 92 public String getJasminValue(Map instToLabel) 93 { 94 StringBuffer buf = new StringBuffer (); 97 98 if (mTags.size() != mUnits.size()) 99 throw new RuntimeException ("Sizes must match!"); 100 101 Iterator tagIt = mTags.iterator(); 102 Iterator unitIt = mUnits.iterator(); 103 104 while (tagIt.hasNext()) 105 { 106 Object unit = unitIt.next(); 107 Object tag = tagIt.next(); 108 109 buf.append("%"+instToLabel.get(unit) + "%"+ 110 new String (Base64.encode(((Tag)tag).getValue()))); 111 } 112 113 return buf.toString(); 114 } 115 116 117 public List getUnitBoxes() 118 { 119 List unitBoxes = new ArrayList(mUnits.size()); 120 121 Iterator it = mUnits.iterator(); 122 123 while(it.hasNext()) { 124 unitBoxes.add(Baf.v().newInstBox((Unit)it.next())); 125 } 126 127 return unitBoxes; 128 } 129 130 public byte[] decode(String attr, Hashtable labelToPc) 131 { 132 if (Options.v().verbose()) 133 G.v().out.println("[] JasminAttribute decode..."); 134 135 List attributeHunks = new LinkedList(); 136 int attributeSize = 0; 137 138 StringTokenizer st = new StringTokenizer(attr, "%"); 139 boolean isLabel = false; 140 if(attr.startsWith("%")) 141 isLabel = true; 142 143 int tablesize = 0; 144 145 byte[] pcArray; 146 while(st.hasMoreTokens()) { 147 String token = st.nextToken(); 148 if(isLabel) { 149 Integer pc = (Integer ) labelToPc.get(token); 150 151 if(pc == null) 152 throw new RuntimeException ("PC is null, the token is "+token); 153 154 int pcvalue = pc.intValue(); 155 if(pcvalue > 65535) 156 throw new RuntimeException ("PC great than 65535, the token is "+token+" : " +pcvalue); 157 158 pcArray = new byte[2]; 159 160 pcArray[1] = (byte)(pcvalue&0x0FF); 161 162 pcArray[0] = (byte)((pcvalue>>8)&0x0FF); 163 164 attributeHunks.add(pcArray); 165 attributeSize += 2; 166 tablesize++; 167 } else { 168 169 byte[] hunk = Base64.decode(token.toCharArray()); 170 attributeSize += hunk.length; 171 172 attributeHunks.add(hunk); 173 } 174 isLabel = !isLabel; 175 } 176 177 178 attributeSize += 2; 179 byte[] attributeValue = new byte[attributeSize]; 180 { 181 attributeValue[0] = (byte)((tablesize>>8)&0x0FF); 182 attributeValue[1] = (byte)(tablesize&0x0FF); 183 } 184 int index=2; 185 Iterator it = attributeHunks.iterator(); 186 while(it.hasNext()) { 187 byte[] hunk = (byte[]) it.next(); 188 for(int i = 0; i < hunk.length; i++) { 189 attributeValue[index++] = hunk[i]; 190 } 191 } 192 193 if(index != (attributeSize)) 194 throw new RuntimeException ("Index does not euqal to attrubute size :"+index+" -- "+attributeSize); 195 196 if (Options.v().verbose()) 197 G.v().out.println("[] Jasmin.decode finished..."); 198 199 return attributeValue; 200 } 201 } 202 203 | Popular Tags |