summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/bytecode/BytecodeIndexIterator.java
blob: aadbeb25fcd451e837a2a4c820bc6cc8b55667e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************************************
 * Copyright (c) 2014 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Jeff Martin - initial API and implementation
 ******************************************************************************/
package cuchaz.enigma.bytecode;

import java.util.Iterator;

import javassist.bytecode.BadBytecode;
import javassist.bytecode.Bytecode;
import javassist.bytecode.CodeAttribute;
import javassist.bytecode.CodeIterator;
import javassist.bytecode.Opcode;

public class BytecodeIndexIterator implements Iterator<BytecodeIndexIterator.Index>
{
	public static class Index
	{
		private CodeIterator m_iter;
		private int m_pos;
		private boolean m_isWide;
		
		protected Index( CodeIterator iter, int pos, boolean isWide )
		{
			m_iter = iter;
			m_pos = pos;
			m_isWide = isWide;
		}
		
		public int getIndex( )
		{
			if( m_isWide )
			{
				return m_iter.s16bitAt( m_pos );
			}
			else
			{
				return m_iter.byteAt( m_pos );
			}
		}
		
		public void setIndex( int val )
		throws BadBytecode
		{
			if( m_isWide )
			{
				m_iter.write16bit( val, m_pos );
			}
			else
			{
				if( val < 256 )
				{
					// we can write the byte
					m_iter.writeByte( val, m_pos );
				}
				else
				{
					// we need to upgrade this instruction to LDC_W
					assert( m_iter.byteAt( m_pos - 1 ) == Opcode.LDC );
					m_iter.insertGap( m_pos - 1, 1 );
					m_iter.writeByte( Opcode.LDC_W, m_pos - 1 );
					m_iter.write16bit( val, m_pos );
					m_isWide = true;
					
					// move the iterator to the next opcode
					m_iter.move( m_pos + 2 );
				}
			}
			
			// sanity check
			assert( val == getIndex() );
		}
		
		public boolean isValid( Bytecode bytecode )
		{
			return getIndex() >= 0 && getIndex() < bytecode.getConstPool().getSize();
		}
	}
	
	private Bytecode m_bytecode;
	private CodeAttribute m_attribute;
	private CodeIterator m_iter;
	private Index m_next;
	
	public BytecodeIndexIterator( Bytecode bytecode )
	throws BadBytecode
	{
		m_bytecode = bytecode;
		m_attribute = bytecode.toCodeAttribute();
		m_iter = m_attribute.iterator();
		
		m_next = getNext();
	}
	
	@Override
	public boolean hasNext( )
	{
		return m_next != null;
	}

	@Override
	public Index next( )
	{
		Index out = m_next;
		try
		{
			m_next = getNext();
		}
		catch( BadBytecode ex )
		{
			throw new Error( ex );
		}
		return out;
	}

	@Override
	public void remove( )
	{
		throw new UnsupportedOperationException();
	}
	
	private Index getNext( )
	throws BadBytecode
	{
		while( m_iter.hasNext() )
		{
			int pos = m_iter.next();
			int opcode = m_iter.byteAt( pos );
			switch( opcode )
			{
				// for only these opcodes, the next two bytes are a const pool reference
				case Opcode.ANEWARRAY:
				case Opcode.CHECKCAST:
				case Opcode.INSTANCEOF:
				case Opcode.INVOKEDYNAMIC:
				case Opcode.INVOKEINTERFACE:
				case Opcode.INVOKESPECIAL:
				case Opcode.INVOKESTATIC:
				case Opcode.INVOKEVIRTUAL:
				case Opcode.LDC_W:
				case Opcode.LDC2_W:
				case Opcode.MULTIANEWARRAY:
				case Opcode.NEW:
				case Opcode.PUTFIELD:
				case Opcode.PUTSTATIC:
				case Opcode.GETFIELD:
				case Opcode.GETSTATIC:
					return new Index( m_iter, pos + 1, true );
				
				case Opcode.LDC:
					return new Index( m_iter, pos + 1, false );
			}
		}
		
		return null;
	}
	
	public Iterable<Index> indices( )
	{
		return new Iterable<Index>( )
		{
			@Override
			public Iterator<Index> iterator( )
			{
				return BytecodeIndexIterator.this;
			}
		};
	}
	
	public void saveChangesToBytecode( )
	{
		BytecodeTools.setBytecode( m_bytecode, m_attribute.getCode() );
	}
}