xref: /AOO41X/main/codemaker/source/javamaker/classfile.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_codemaker.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "classfile.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "codemaker/global.hxx"
34*cdf0e10cSrcweir #include "codemaker/options.hxx"
35*cdf0e10cSrcweir #include "codemaker/unotype.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include "boost/static_assert.hpp"
38*cdf0e10cSrcweir #include "osl/diagnose.h"
39*cdf0e10cSrcweir #include "rtl/string.h"
40*cdf0e10cSrcweir #include "rtl/string.hxx"
41*cdf0e10cSrcweir #include "sal/types.h"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <map>
44*cdf0e10cSrcweir #include <utility>
45*cdf0e10cSrcweir #include <vector>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir using codemaker::javamaker::ClassFile;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace {
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir void appendU1(std::vector< unsigned char > & stream, sal_uInt8 data) {
52*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >(data));
53*cdf0e10cSrcweir }
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir void appendU2(std::vector< unsigned char > & stream, sal_uInt16 data) {
56*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >(data >> 8));
57*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >(data & 0xFF));
58*cdf0e10cSrcweir }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir void appendU4(std::vector< unsigned char > & stream, sal_uInt32 data) {
61*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >(data >> 24));
62*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >((data >> 16) & 0xFF));
63*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >((data >> 8) & 0xFF));
64*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >(data & 0xFF));
65*cdf0e10cSrcweir }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir void appendU8(std::vector< unsigned char > & stream, sal_uInt64 data) {
68*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >(data >> 56));
69*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >((data >> 48) & 0xFF));
70*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >((data >> 40) & 0xFF));
71*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >((data >> 32) & 0xFF));
72*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >((data >> 24) & 0xFF));
73*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >((data >> 16) & 0xFF));
74*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >((data >> 8) & 0xFF));
75*cdf0e10cSrcweir     stream.push_back(static_cast< unsigned char >(data & 0xFF));
76*cdf0e10cSrcweir }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir void appendStream(
79*cdf0e10cSrcweir     std::vector< unsigned char > & stream,
80*cdf0e10cSrcweir     std::vector< unsigned char > const & data)
81*cdf0e10cSrcweir {
82*cdf0e10cSrcweir     stream.insert(stream.end(), data.begin(), data.end());
83*cdf0e10cSrcweir }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir void write(FileStream & file, void const * buffer, sal_uInt64 size) {
86*cdf0e10cSrcweir     if (!file.write(buffer, size)) {
87*cdf0e10cSrcweir         throw CannotDumpException(
88*cdf0e10cSrcweir             rtl::OString(RTL_CONSTASCII_STRINGPARAM("Error writing file")));
89*cdf0e10cSrcweir     }
90*cdf0e10cSrcweir }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir void writeU1(FileStream & file, sal_uInt8 data) {
93*cdf0e10cSrcweir     unsigned char buf[] = { static_cast< unsigned char >(data) };
94*cdf0e10cSrcweir     write(file, &buf, sizeof buf);
95*cdf0e10cSrcweir }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir void writeU2(FileStream & file, sal_uInt16 data) {
98*cdf0e10cSrcweir     unsigned char buf[] = {
99*cdf0e10cSrcweir         static_cast< unsigned char >(data >> 8),
100*cdf0e10cSrcweir         static_cast< unsigned char >(data & 0xFF) };
101*cdf0e10cSrcweir     write(file, buf, sizeof buf);
102*cdf0e10cSrcweir }
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir void writeU4(FileStream & file, sal_uInt32 data) {
105*cdf0e10cSrcweir     unsigned char buf[] = {
106*cdf0e10cSrcweir         static_cast< unsigned char >(data >> 24),
107*cdf0e10cSrcweir         static_cast< unsigned char >((data >> 16) & 0xFF),
108*cdf0e10cSrcweir         static_cast< unsigned char >((data >> 8) & 0xFF),
109*cdf0e10cSrcweir         static_cast< unsigned char >(data & 0xFF) };
110*cdf0e10cSrcweir     write(file, buf, sizeof buf);
111*cdf0e10cSrcweir }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir void writeStream(FileStream & file, std::vector< unsigned char > const & stream)
114*cdf0e10cSrcweir {
115*cdf0e10cSrcweir     std::vector< unsigned char >::size_type n = stream.size();
116*cdf0e10cSrcweir     BOOST_STATIC_ASSERT(
117*cdf0e10cSrcweir         sizeof (std::vector< unsigned char >::size_type)
118*cdf0e10cSrcweir         <= sizeof (sal_uInt64));
119*cdf0e10cSrcweir         // both unsigned integral, so sizeof is a practically sufficient
120*cdf0e10cSrcweir         // approximation of std::numeric_limits<T1>::max() <=
121*cdf0e10cSrcweir         // std::numeric_limits<T2>::max()
122*cdf0e10cSrcweir     if (n != 0) {
123*cdf0e10cSrcweir         write(file, &stream[0], static_cast< sal_uInt64 >(n));
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir ClassFile::Code::~Code() {}
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir void ClassFile::Code::instrAastore() {
132*cdf0e10cSrcweir     // aastore:
133*cdf0e10cSrcweir     appendU1(m_code, 0x53);
134*cdf0e10cSrcweir }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir void ClassFile::Code::instrAconstNull() {
137*cdf0e10cSrcweir     // aconst_null:
138*cdf0e10cSrcweir     appendU1(m_code, 0x01);
139*cdf0e10cSrcweir }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir void ClassFile::Code::instrAnewarray(rtl::OString const & type) {
142*cdf0e10cSrcweir     // anewarray <indexbyte1> <indexbyte2>:
143*cdf0e10cSrcweir     appendU1(m_code, 0xBD);
144*cdf0e10cSrcweir     appendU2(m_code, m_classFile.addClassInfo(type));
145*cdf0e10cSrcweir }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir void ClassFile::Code::instrAreturn() {
148*cdf0e10cSrcweir     // areturn:
149*cdf0e10cSrcweir     appendU1(m_code, 0xB0);
150*cdf0e10cSrcweir }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir void ClassFile::Code::instrAthrow() {
153*cdf0e10cSrcweir     // athrow:
154*cdf0e10cSrcweir     appendU1(m_code, 0xBF);
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir void ClassFile::Code::instrCheckcast(rtl::OString const & type) {
158*cdf0e10cSrcweir     // checkcast <indexbyte1> <indexbyte2>:
159*cdf0e10cSrcweir     appendU1(m_code, 0xC0);
160*cdf0e10cSrcweir     appendU2(m_code, m_classFile.addClassInfo(type));
161*cdf0e10cSrcweir }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir void ClassFile::Code::instrDup() {
164*cdf0e10cSrcweir     // dup:
165*cdf0e10cSrcweir     appendU1(m_code, 0x59);
166*cdf0e10cSrcweir }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir void ClassFile::Code::instrGetstatic(
169*cdf0e10cSrcweir     rtl::OString const & type, rtl::OString const & name,
170*cdf0e10cSrcweir     rtl::OString const & descriptor)
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir     // getstatic <indexbyte1> <indexbyte2>:
173*cdf0e10cSrcweir     appendU1(m_code, 0xB2);
174*cdf0e10cSrcweir     appendU2(m_code, m_classFile.addFieldrefInfo(type, name, descriptor));
175*cdf0e10cSrcweir }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir ClassFile::Code::Branch ClassFile::Code::instrIfAcmpne() {
178*cdf0e10cSrcweir     // if_acmpne <branchbyte1> <branchbyte2>:
179*cdf0e10cSrcweir     Branch branch = m_code.size();
180*cdf0e10cSrcweir     appendU1(m_code, 0xA6);
181*cdf0e10cSrcweir     appendU2(m_code, 0);
182*cdf0e10cSrcweir     return branch;
183*cdf0e10cSrcweir }
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir ClassFile::Code::Branch ClassFile::Code::instrIfeq() {
186*cdf0e10cSrcweir     // ifeq <branchbyte1> <branchbyte2>:
187*cdf0e10cSrcweir     Branch branch = m_code.size();
188*cdf0e10cSrcweir     appendU1(m_code, 0x99);
189*cdf0e10cSrcweir     appendU2(m_code, 0);
190*cdf0e10cSrcweir     return branch;
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir ClassFile::Code::Branch ClassFile::Code::instrIfnull() {
194*cdf0e10cSrcweir     // ifnull <branchbyte1> <branchbyte2>:
195*cdf0e10cSrcweir     Branch branch = m_code.size();
196*cdf0e10cSrcweir     appendU1(m_code, 0xC6);
197*cdf0e10cSrcweir     appendU2(m_code, 0);
198*cdf0e10cSrcweir     return branch;
199*cdf0e10cSrcweir }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir void ClassFile::Code::instrInstanceof(rtl::OString const & type) {
202*cdf0e10cSrcweir     // instanceof <indexbyte1> <indexbyte2>:
203*cdf0e10cSrcweir     appendU1(m_code, 0xC1);
204*cdf0e10cSrcweir     appendU2(m_code, m_classFile.addClassInfo(type));
205*cdf0e10cSrcweir }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir void ClassFile::Code::instrInvokeinterface(
208*cdf0e10cSrcweir     rtl::OString const & type, rtl::OString const & name,
209*cdf0e10cSrcweir     rtl::OString const & descriptor, sal_uInt8 args)
210*cdf0e10cSrcweir {
211*cdf0e10cSrcweir     // invokeinterface <indexbyte1> <indexbyte2> <nargs> 0:
212*cdf0e10cSrcweir     appendU1(m_code, 0xB9);
213*cdf0e10cSrcweir     appendU2(
214*cdf0e10cSrcweir         m_code, m_classFile.addInterfaceMethodrefInfo(type, name, descriptor));
215*cdf0e10cSrcweir     appendU1(m_code, args);
216*cdf0e10cSrcweir     appendU1(m_code, 0);
217*cdf0e10cSrcweir }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir void ClassFile::Code::instrInvokespecial(
220*cdf0e10cSrcweir     rtl::OString const & type, rtl::OString const & name,
221*cdf0e10cSrcweir     rtl::OString const & descriptor)
222*cdf0e10cSrcweir {
223*cdf0e10cSrcweir     // invokespecial <indexbyte1> <indexbyte2>:
224*cdf0e10cSrcweir     appendU1(m_code, 0xB7);
225*cdf0e10cSrcweir     appendU2(m_code, m_classFile.addMethodrefInfo(type, name, descriptor));
226*cdf0e10cSrcweir }
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir void ClassFile::Code::instrInvokestatic(
229*cdf0e10cSrcweir     rtl::OString const & type, rtl::OString const & name,
230*cdf0e10cSrcweir     rtl::OString const & descriptor)
231*cdf0e10cSrcweir {
232*cdf0e10cSrcweir     // invokestatic <indexbyte1> <indexbyte2>:
233*cdf0e10cSrcweir     appendU1(m_code, 0xB8);
234*cdf0e10cSrcweir     appendU2(m_code, m_classFile.addMethodrefInfo(type, name, descriptor));
235*cdf0e10cSrcweir }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir void ClassFile::Code::instrInvokevirtual(
238*cdf0e10cSrcweir     rtl::OString const & type, rtl::OString const & name,
239*cdf0e10cSrcweir     rtl::OString const & descriptor)
240*cdf0e10cSrcweir {
241*cdf0e10cSrcweir     // invokevirtual <indexbyte1> <indexbyte2>:
242*cdf0e10cSrcweir     appendU1(m_code, 0xB6);
243*cdf0e10cSrcweir     appendU2(m_code, m_classFile.addMethodrefInfo(type, name, descriptor));
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir void ClassFile::Code::instrLookupswitch(
247*cdf0e10cSrcweir     Code const * defaultBlock,
248*cdf0e10cSrcweir     std::list< std::pair< sal_Int32, Code * > > const & blocks)
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir     // lookupswitch <0--3 byte pad> <defaultbyte1> <defaultbyte2> <defaultbyte3>
251*cdf0e10cSrcweir     // <defaultbyte4> <npairs1> <npairs2> <npairs3> <npairs4>
252*cdf0e10cSrcweir     // <match--offset pairs...>:
253*cdf0e10cSrcweir     std::list< std::pair< sal_Int32, Code * > >::size_type size = blocks.size();
254*cdf0e10cSrcweir     if (size > SAL_MAX_INT32) {
255*cdf0e10cSrcweir         throw CannotDumpException(
256*cdf0e10cSrcweir             rtl::OString(
257*cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM(
258*cdf0e10cSrcweir                     "Lookup-switch too large for Java class file format")));
259*cdf0e10cSrcweir     }
260*cdf0e10cSrcweir     Position pos1 = m_code.size();
261*cdf0e10cSrcweir     appendU1(m_code, 0xAB);
262*cdf0e10cSrcweir     int pad = (pos1 + 1) % 4;
263*cdf0e10cSrcweir     {for (int i = 0; i < pad; ++i) {
264*cdf0e10cSrcweir         appendU1(m_code, 0);
265*cdf0e10cSrcweir     }}
266*cdf0e10cSrcweir     Position pos2 = pos1 + 1 + pad + 8 + blocks.size() * 8; //FIXME: overflow
267*cdf0e10cSrcweir     appendU4(m_code, static_cast< sal_uInt32 >(pos2 - pos1)); //FIXME: overflow
268*cdf0e10cSrcweir     pos2 += defaultBlock->m_code.size(); //FIXME: overflow
269*cdf0e10cSrcweir     appendU4(m_code, static_cast< sal_uInt32 >(size));
270*cdf0e10cSrcweir     {for (std::list< std::pair< sal_Int32, Code * > >::const_iterator i(
271*cdf0e10cSrcweir               blocks.begin());
272*cdf0e10cSrcweir           i != blocks.end(); ++i)
273*cdf0e10cSrcweir     {
274*cdf0e10cSrcweir         appendU4(m_code, static_cast< sal_uInt32 >(i->first));
275*cdf0e10cSrcweir         appendU4(m_code, static_cast< sal_uInt32 >(pos2 - pos1));
276*cdf0e10cSrcweir             //FIXME: overflow
277*cdf0e10cSrcweir         pos2 += i->second->m_code.size(); //FIXME: overflow
278*cdf0e10cSrcweir     }}
279*cdf0e10cSrcweir     appendStream(m_code, defaultBlock->m_code);
280*cdf0e10cSrcweir     {for (std::list< std::pair< sal_Int32, Code * > >::const_iterator i(
281*cdf0e10cSrcweir               blocks.begin());
282*cdf0e10cSrcweir           i != blocks.end(); ++i)
283*cdf0e10cSrcweir     {
284*cdf0e10cSrcweir         appendStream(m_code, i->second->m_code);
285*cdf0e10cSrcweir     }}
286*cdf0e10cSrcweir }
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir void ClassFile::Code::instrNew(rtl::OString const & type) {
289*cdf0e10cSrcweir     // new <indexbyte1> <indexbyte2>:
290*cdf0e10cSrcweir     appendU1(m_code, 0xBB);
291*cdf0e10cSrcweir     appendU2(m_code, m_classFile.addClassInfo(type));
292*cdf0e10cSrcweir }
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir void ClassFile::Code::instrNewarray(codemaker::UnoType::Sort sort) {
295*cdf0e10cSrcweir     OSL_ASSERT(
296*cdf0e10cSrcweir         sort >= codemaker::UnoType::SORT_BOOLEAN
297*cdf0e10cSrcweir         && sort <= codemaker::UnoType::SORT_CHAR);
298*cdf0e10cSrcweir     // newarray <atype>:
299*cdf0e10cSrcweir     appendU1(m_code, 0xBC);
300*cdf0e10cSrcweir     static sal_uInt8 const atypes[codemaker::UnoType::SORT_CHAR] = {
301*cdf0e10cSrcweir         0x04, 0x08, 0x09, 0x09, 0x0A, 0x0A, 0x0B, 0x0B, 0x06, 0x07, 0x05 };
302*cdf0e10cSrcweir     appendU1(m_code, atypes[sort - 1]);
303*cdf0e10cSrcweir }
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir void ClassFile::Code::instrPop() {
306*cdf0e10cSrcweir     // pop:
307*cdf0e10cSrcweir     appendU1(m_code, 0x57);
308*cdf0e10cSrcweir }
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir void ClassFile::Code::instrPutfield(
311*cdf0e10cSrcweir     rtl::OString const & type, rtl::OString const & name,
312*cdf0e10cSrcweir     rtl::OString const & descriptor)
313*cdf0e10cSrcweir {
314*cdf0e10cSrcweir     // putfield <indexbyte1> <indexbyte2>:
315*cdf0e10cSrcweir     appendU1(m_code, 0xB5);
316*cdf0e10cSrcweir     appendU2(m_code, m_classFile.addFieldrefInfo(type, name, descriptor));
317*cdf0e10cSrcweir }
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir void ClassFile::Code::instrPutstatic(
320*cdf0e10cSrcweir     rtl::OString const & type, rtl::OString const & name,
321*cdf0e10cSrcweir     rtl::OString const & descriptor)
322*cdf0e10cSrcweir {
323*cdf0e10cSrcweir     // putstatic <indexbyte1> <indexbyte2>:
324*cdf0e10cSrcweir     appendU1(m_code, 0xB3);
325*cdf0e10cSrcweir     appendU2(m_code, m_classFile.addFieldrefInfo(type, name, descriptor));
326*cdf0e10cSrcweir }
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir void ClassFile::Code::instrReturn() {
329*cdf0e10cSrcweir     // return:
330*cdf0e10cSrcweir     appendU1(m_code, 0xB1);
331*cdf0e10cSrcweir }
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir void ClassFile::Code::instrSwap() {
334*cdf0e10cSrcweir     // swap:
335*cdf0e10cSrcweir     appendU1(m_code, 0x5F);
336*cdf0e10cSrcweir }
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir void ClassFile::Code::instrTableswitch(
339*cdf0e10cSrcweir     Code const * defaultBlock, sal_Int32 low,
340*cdf0e10cSrcweir     std::list< Code * > const & blocks)
341*cdf0e10cSrcweir {
342*cdf0e10cSrcweir     // tableswitch <0--3 byte pad> <defaultbyte1> <defaultbyte2> <defaultbyte3>
343*cdf0e10cSrcweir     // <defaultbyte4> <lowbyte1> <lowbyte2> <lowbyte3> <lowbyte4> <highbyte1>
344*cdf0e10cSrcweir     // <highbyte2> <highbyte3> <highbyte4> <jump offsets...>:
345*cdf0e10cSrcweir     Position pos1 = m_code.size();
346*cdf0e10cSrcweir     appendU1(m_code, 0xAA);
347*cdf0e10cSrcweir     int pad = (pos1 + 1) % 4;
348*cdf0e10cSrcweir     {for (int i = 0; i < pad; ++i) {
349*cdf0e10cSrcweir         appendU1(m_code, 0);
350*cdf0e10cSrcweir     }}
351*cdf0e10cSrcweir     std::list< Code * >::size_type size = blocks.size();
352*cdf0e10cSrcweir     Position pos2 = pos1 + 1 + pad + 12 + size * 4; //FIXME: overflow
353*cdf0e10cSrcweir     sal_uInt32 defaultOffset = static_cast< sal_uInt32 >(pos2 - pos1);
354*cdf0e10cSrcweir         //FIXME: overflow
355*cdf0e10cSrcweir     appendU4(m_code, defaultOffset);
356*cdf0e10cSrcweir     pos2 += defaultBlock->m_code.size(); //FIXME: overflow
357*cdf0e10cSrcweir     appendU4(m_code, static_cast< sal_uInt32 >(low));
358*cdf0e10cSrcweir     appendU4(m_code, static_cast< sal_uInt32 >(low + (size - 1)));
359*cdf0e10cSrcweir     {for (std::list< Code * >::const_iterator i(blocks.begin());
360*cdf0e10cSrcweir           i != blocks.end(); ++i)
361*cdf0e10cSrcweir     {
362*cdf0e10cSrcweir         if (*i == 0) {
363*cdf0e10cSrcweir             appendU4(m_code, defaultOffset);
364*cdf0e10cSrcweir         } else {
365*cdf0e10cSrcweir             appendU4(m_code, static_cast< sal_uInt32 >(pos2 - pos1));
366*cdf0e10cSrcweir                 //FIXME: overflow
367*cdf0e10cSrcweir             pos2 += (*i)->m_code.size(); //FIXME: overflow
368*cdf0e10cSrcweir         }
369*cdf0e10cSrcweir     }}
370*cdf0e10cSrcweir     appendStream(m_code, defaultBlock->m_code);
371*cdf0e10cSrcweir     {for (std::list< Code * >::const_iterator i(blocks.begin());
372*cdf0e10cSrcweir           i != blocks.end(); ++i)
373*cdf0e10cSrcweir     {
374*cdf0e10cSrcweir         if (*i != 0) {
375*cdf0e10cSrcweir             appendStream(m_code, (*i)->m_code);
376*cdf0e10cSrcweir         }
377*cdf0e10cSrcweir     }}
378*cdf0e10cSrcweir }
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir void ClassFile::Code::loadIntegerConstant(sal_Int32 value) {
381*cdf0e10cSrcweir     if (value >= -1 && value <= 5) {
382*cdf0e10cSrcweir         // iconst_<i>:
383*cdf0e10cSrcweir         appendU1(m_code, static_cast< sal_uInt8 >(0x02 + value + 1));
384*cdf0e10cSrcweir     } else if (value >= -128 && value <= 127) {
385*cdf0e10cSrcweir         // bipush <byte>:
386*cdf0e10cSrcweir         appendU1(m_code, 0x10);
387*cdf0e10cSrcweir         appendU1(m_code, static_cast< sal_uInt8 >(value));
388*cdf0e10cSrcweir     } else if (value >= -32768 && value <= 32767) {
389*cdf0e10cSrcweir         // sipush <byte1> <byte2>:
390*cdf0e10cSrcweir         appendU1(m_code, 0x11);
391*cdf0e10cSrcweir         appendU2(m_code, static_cast< sal_uInt16 >(value));
392*cdf0e10cSrcweir     } else {
393*cdf0e10cSrcweir         ldc(m_classFile.addIntegerInfo(value));
394*cdf0e10cSrcweir     }
395*cdf0e10cSrcweir }
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir void ClassFile::Code::loadStringConstant(rtl::OString const & value) {
398*cdf0e10cSrcweir     ldc(m_classFile.addStringInfo(value));
399*cdf0e10cSrcweir }
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir void ClassFile::Code::loadLocalInteger(sal_uInt16 index) {
402*cdf0e10cSrcweir     accessLocal(index, 0x1A, 0x15); // iload_<n>, iload
403*cdf0e10cSrcweir }
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir void ClassFile::Code::loadLocalLong(sal_uInt16 index) {
406*cdf0e10cSrcweir     accessLocal(index, 0x1E, 0x16); // load_<n>, load
407*cdf0e10cSrcweir }
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir void ClassFile::Code::loadLocalFloat(sal_uInt16 index) {
410*cdf0e10cSrcweir     accessLocal(index, 0x22, 0x17); // load_<n>, load
411*cdf0e10cSrcweir }
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir void ClassFile::Code::loadLocalDouble(sal_uInt16 index) {
414*cdf0e10cSrcweir     accessLocal(index, 0x26, 0x18); // load_<n>, load
415*cdf0e10cSrcweir }
416*cdf0e10cSrcweir 
417*cdf0e10cSrcweir void ClassFile::Code::loadLocalReference(sal_uInt16 index) {
418*cdf0e10cSrcweir     accessLocal(index, 0x2A, 0x19); // aload_<n>, aload
419*cdf0e10cSrcweir }
420*cdf0e10cSrcweir 
421*cdf0e10cSrcweir void ClassFile::Code::storeLocalReference(sal_uInt16 index) {
422*cdf0e10cSrcweir     accessLocal(index, 0x4B, 0x3A); // astore_<n>, astore
423*cdf0e10cSrcweir }
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir void ClassFile::Code::branchHere(Branch branch) {
426*cdf0e10cSrcweir     std::vector< unsigned char >::size_type n = m_code.size();
427*cdf0e10cSrcweir     OSL_ASSERT(n > branch && n - branch <= SAL_MAX_INT16);
428*cdf0e10cSrcweir     n -= branch;
429*cdf0e10cSrcweir     m_code[branch + 1] = static_cast< sal_uInt8 >(n >> 8);
430*cdf0e10cSrcweir     m_code[branch + 2] = static_cast< sal_uInt8 >(n & 0xFF);
431*cdf0e10cSrcweir }
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir void ClassFile::Code::addException(
434*cdf0e10cSrcweir     Position start, Position end, Position handler, rtl::OString const & type)
435*cdf0e10cSrcweir {
436*cdf0e10cSrcweir     OSL_ASSERT(start < end && end <= m_code.size() && handler <= m_code.size());
437*cdf0e10cSrcweir     if (m_exceptionTableLength == SAL_MAX_UINT16) {
438*cdf0e10cSrcweir         throw CannotDumpException(
439*cdf0e10cSrcweir             rtl::OString(
440*cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM(
441*cdf0e10cSrcweir                     "Too many exception handlers for Java class file format")));
442*cdf0e10cSrcweir     }
443*cdf0e10cSrcweir     ++m_exceptionTableLength;
444*cdf0e10cSrcweir     appendU2(m_exceptionTable, static_cast< sal_uInt16 >(start));
445*cdf0e10cSrcweir         //FIXME: overflow
446*cdf0e10cSrcweir     appendU2(m_exceptionTable, static_cast< sal_uInt16 >(end));
447*cdf0e10cSrcweir         //FIXME: overflow
448*cdf0e10cSrcweir     appendU2(m_exceptionTable, static_cast< sal_uInt16 >(handler));
449*cdf0e10cSrcweir         //FIXME: overflow
450*cdf0e10cSrcweir     appendU2(m_exceptionTable, m_classFile.addClassInfo(type));
451*cdf0e10cSrcweir }
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir ClassFile::Code::Position ClassFile::Code::getPosition() const {
454*cdf0e10cSrcweir     return m_code.size();
455*cdf0e10cSrcweir }
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir ClassFile::Code::Code(ClassFile & classFile):
458*cdf0e10cSrcweir     m_classFile(classFile), m_exceptionTableLength(0)
459*cdf0e10cSrcweir {}
460*cdf0e10cSrcweir 
461*cdf0e10cSrcweir void ClassFile::Code::ldc(sal_uInt16 index) {
462*cdf0e10cSrcweir     if (index <= 0xFF) {
463*cdf0e10cSrcweir         // ldc <index>:
464*cdf0e10cSrcweir         appendU1(m_code, 0x12);
465*cdf0e10cSrcweir         appendU1(m_code, static_cast< sal_uInt8 >(index));
466*cdf0e10cSrcweir     } else {
467*cdf0e10cSrcweir         // ldc_w <indexbyte1> <indexbyte2>:
468*cdf0e10cSrcweir         appendU1(m_code, 0x13);
469*cdf0e10cSrcweir         appendU2(m_code, index);
470*cdf0e10cSrcweir     }
471*cdf0e10cSrcweir }
472*cdf0e10cSrcweir 
473*cdf0e10cSrcweir void ClassFile::Code::accessLocal(
474*cdf0e10cSrcweir     sal_uInt16 index, sal_uInt8 fastOp, sal_uInt8 normalOp)
475*cdf0e10cSrcweir {
476*cdf0e10cSrcweir     if (index <= 3) {
477*cdf0e10cSrcweir         // ...load/store_<n>:
478*cdf0e10cSrcweir         appendU1(m_code, static_cast< sal_uInt8 >(fastOp + index));
479*cdf0e10cSrcweir     } else if (index <= 0xFF) {
480*cdf0e10cSrcweir         // ...load/store <index>:
481*cdf0e10cSrcweir         appendU1(m_code, normalOp);
482*cdf0e10cSrcweir         appendU1(m_code, static_cast< sal_uInt8 >(index));
483*cdf0e10cSrcweir     } else {
484*cdf0e10cSrcweir         // wide ...load/store <indexbyte1> <indexbyte2>:
485*cdf0e10cSrcweir         appendU1(m_code, 0xC4);
486*cdf0e10cSrcweir         appendU1(m_code, normalOp);
487*cdf0e10cSrcweir         appendU2(m_code, index);
488*cdf0e10cSrcweir     }
489*cdf0e10cSrcweir }
490*cdf0e10cSrcweir 
491*cdf0e10cSrcweir ClassFile::ClassFile(
492*cdf0e10cSrcweir     AccessFlags accessFlags, rtl::OString const & thisClass,
493*cdf0e10cSrcweir     rtl::OString const & superClass, rtl::OString const & signature):
494*cdf0e10cSrcweir     m_constantPoolCount(1), m_accessFlags(accessFlags), m_interfacesCount(0),
495*cdf0e10cSrcweir     m_fieldsCount(0), m_methodsCount(0), m_attributesCount(0)
496*cdf0e10cSrcweir {
497*cdf0e10cSrcweir     m_thisClass = addClassInfo(thisClass);
498*cdf0e10cSrcweir     m_superClass = addClassInfo(superClass);
499*cdf0e10cSrcweir     if (signature.getLength() != 0) {
500*cdf0e10cSrcweir         ++m_attributesCount;
501*cdf0e10cSrcweir         appendU2(
502*cdf0e10cSrcweir             m_attributes,
503*cdf0e10cSrcweir             addUtf8Info(rtl::OString(RTL_CONSTASCII_STRINGPARAM("Signature"))));
504*cdf0e10cSrcweir         appendU4(m_attributes, 2);
505*cdf0e10cSrcweir         appendU2(m_attributes, addUtf8Info(signature));
506*cdf0e10cSrcweir     }
507*cdf0e10cSrcweir }
508*cdf0e10cSrcweir 
509*cdf0e10cSrcweir ClassFile::~ClassFile() {}
510*cdf0e10cSrcweir 
511*cdf0e10cSrcweir ClassFile::Code * ClassFile::newCode() {
512*cdf0e10cSrcweir     return new Code(*this);
513*cdf0e10cSrcweir }
514*cdf0e10cSrcweir 
515*cdf0e10cSrcweir sal_uInt16 ClassFile::addIntegerInfo(sal_Int32 value) {
516*cdf0e10cSrcweir     std::map< sal_Int32, sal_uInt16 >::iterator i(m_integerInfos.find(value));
517*cdf0e10cSrcweir     if (i != m_integerInfos.end()) {
518*cdf0e10cSrcweir         return i->second;
519*cdf0e10cSrcweir     }
520*cdf0e10cSrcweir     sal_uInt16 index = nextConstantPoolIndex(1);
521*cdf0e10cSrcweir     appendU1(m_constantPool, 3);
522*cdf0e10cSrcweir     appendU4(m_constantPool, static_cast< sal_uInt32 >(value));
523*cdf0e10cSrcweir     if (!m_integerInfos.insert(
524*cdf0e10cSrcweir             std::map< sal_Int32, sal_uInt16 >::value_type(value, index)).second)
525*cdf0e10cSrcweir     {
526*cdf0e10cSrcweir         OSL_ASSERT(false);
527*cdf0e10cSrcweir     }
528*cdf0e10cSrcweir     return index;
529*cdf0e10cSrcweir }
530*cdf0e10cSrcweir 
531*cdf0e10cSrcweir sal_uInt16 ClassFile::addFloatInfo(float value) {
532*cdf0e10cSrcweir     std::map< float, sal_uInt16 >::iterator i(m_floatInfos.find(value));
533*cdf0e10cSrcweir     if (i != m_floatInfos.end()) {
534*cdf0e10cSrcweir         return i->second;
535*cdf0e10cSrcweir     }
536*cdf0e10cSrcweir     sal_uInt16 index = nextConstantPoolIndex(1);
537*cdf0e10cSrcweir     appendU1(m_constantPool, 4);
538*cdf0e10cSrcweir     union { float floatBytes; sal_uInt32 uint32Bytes; } bytes;
539*cdf0e10cSrcweir     bytes.floatBytes = value;
540*cdf0e10cSrcweir     appendU4(m_constantPool, bytes.uint32Bytes);
541*cdf0e10cSrcweir     if (!m_floatInfos.insert(
542*cdf0e10cSrcweir             std::map< float, sal_uInt16 >::value_type(value, index)).second)
543*cdf0e10cSrcweir     {
544*cdf0e10cSrcweir         OSL_ASSERT(false);
545*cdf0e10cSrcweir     }
546*cdf0e10cSrcweir     return index;
547*cdf0e10cSrcweir }
548*cdf0e10cSrcweir 
549*cdf0e10cSrcweir sal_uInt16 ClassFile::addLongInfo(sal_Int64 value) {
550*cdf0e10cSrcweir     std::map< sal_Int64, sal_uInt16 >::iterator i(m_longInfos.find(value));
551*cdf0e10cSrcweir     if (i != m_longInfos.end()) {
552*cdf0e10cSrcweir         return i->second;
553*cdf0e10cSrcweir     }
554*cdf0e10cSrcweir     sal_uInt16 index = nextConstantPoolIndex(2);
555*cdf0e10cSrcweir     appendU1(m_constantPool, 5);
556*cdf0e10cSrcweir     appendU8(m_constantPool, static_cast< sal_uInt64 >(value));
557*cdf0e10cSrcweir     if (!m_longInfos.insert(
558*cdf0e10cSrcweir             std::map< sal_Int64, sal_uInt16 >::value_type(value, index)).second)
559*cdf0e10cSrcweir     {
560*cdf0e10cSrcweir         OSL_ASSERT(false);
561*cdf0e10cSrcweir     }
562*cdf0e10cSrcweir     return index;
563*cdf0e10cSrcweir }
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir sal_uInt16 ClassFile::addDoubleInfo(double value) {
566*cdf0e10cSrcweir     std::map< double, sal_uInt16 >::iterator i(m_doubleInfos.find(value));
567*cdf0e10cSrcweir     if (i != m_doubleInfos.end()) {
568*cdf0e10cSrcweir         return i->second;
569*cdf0e10cSrcweir     }
570*cdf0e10cSrcweir     sal_uInt16 index = nextConstantPoolIndex(2);
571*cdf0e10cSrcweir     appendU1(m_constantPool, 6);
572*cdf0e10cSrcweir     union { double doubleBytes; sal_uInt64 uint64Bytes; } bytes;
573*cdf0e10cSrcweir     bytes.doubleBytes = value;
574*cdf0e10cSrcweir     appendU8(m_constantPool, bytes.uint64Bytes);
575*cdf0e10cSrcweir     if (!m_doubleInfos.insert(
576*cdf0e10cSrcweir             std::map< double, sal_uInt16 >::value_type(value, index)).second)
577*cdf0e10cSrcweir     {
578*cdf0e10cSrcweir         OSL_ASSERT(false);
579*cdf0e10cSrcweir     }
580*cdf0e10cSrcweir     return index;
581*cdf0e10cSrcweir }
582*cdf0e10cSrcweir 
583*cdf0e10cSrcweir void ClassFile::addInterface(rtl::OString const & interface) {
584*cdf0e10cSrcweir     if (m_interfacesCount == SAL_MAX_UINT16) {
585*cdf0e10cSrcweir         throw CannotDumpException(
586*cdf0e10cSrcweir             rtl::OString(
587*cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM(
588*cdf0e10cSrcweir                     "Too many interfaces for Java class file format")));
589*cdf0e10cSrcweir     }
590*cdf0e10cSrcweir     ++m_interfacesCount;
591*cdf0e10cSrcweir     appendU2(m_interfaces, addClassInfo(interface));
592*cdf0e10cSrcweir }
593*cdf0e10cSrcweir 
594*cdf0e10cSrcweir void ClassFile::addField(
595*cdf0e10cSrcweir     AccessFlags accessFlags, rtl::OString const & name,
596*cdf0e10cSrcweir     rtl::OString const & descriptor, sal_uInt16 constantValueIndex,
597*cdf0e10cSrcweir     rtl::OString const & signature)
598*cdf0e10cSrcweir {
599*cdf0e10cSrcweir     if (m_fieldsCount == SAL_MAX_UINT16) {
600*cdf0e10cSrcweir         throw CannotDumpException(
601*cdf0e10cSrcweir             rtl::OString(
602*cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM(
603*cdf0e10cSrcweir                     "Too many fields for Java class file format")));
604*cdf0e10cSrcweir     }
605*cdf0e10cSrcweir     ++m_fieldsCount;
606*cdf0e10cSrcweir     appendU2(m_fields, static_cast< sal_uInt16 >(accessFlags));
607*cdf0e10cSrcweir     appendU2(m_fields, addUtf8Info(name));
608*cdf0e10cSrcweir     appendU2(m_fields, addUtf8Info(descriptor));
609*cdf0e10cSrcweir     appendU2(
610*cdf0e10cSrcweir         m_fields,
611*cdf0e10cSrcweir         ((constantValueIndex == 0 ? 0 : 1)
612*cdf0e10cSrcweir          + (signature.getLength() == 0 ? 0 : 1)));
613*cdf0e10cSrcweir     if (constantValueIndex != 0) {
614*cdf0e10cSrcweir         appendU2(
615*cdf0e10cSrcweir             m_fields,
616*cdf0e10cSrcweir             addUtf8Info(
617*cdf0e10cSrcweir                 rtl::OString(RTL_CONSTASCII_STRINGPARAM("ConstantValue"))));
618*cdf0e10cSrcweir         appendU4(m_fields, 2);
619*cdf0e10cSrcweir         appendU2(m_fields, constantValueIndex);
620*cdf0e10cSrcweir     }
621*cdf0e10cSrcweir     appendSignatureAttribute(m_fields, signature);
622*cdf0e10cSrcweir }
623*cdf0e10cSrcweir 
624*cdf0e10cSrcweir void ClassFile::addMethod(
625*cdf0e10cSrcweir     AccessFlags accessFlags, rtl::OString const & name,
626*cdf0e10cSrcweir     rtl::OString const & descriptor, Code const * code,
627*cdf0e10cSrcweir     std::vector< rtl::OString > const & exceptions,
628*cdf0e10cSrcweir     rtl::OString const & signature)
629*cdf0e10cSrcweir {
630*cdf0e10cSrcweir     if (m_methodsCount == SAL_MAX_UINT16) {
631*cdf0e10cSrcweir         throw CannotDumpException(
632*cdf0e10cSrcweir             rtl::OString(
633*cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM(
634*cdf0e10cSrcweir                     "Too many methods for Java class file format")));
635*cdf0e10cSrcweir     }
636*cdf0e10cSrcweir     ++m_methodsCount;
637*cdf0e10cSrcweir     appendU2(m_methods, static_cast< sal_uInt16 >(accessFlags));
638*cdf0e10cSrcweir     appendU2(m_methods, addUtf8Info(name));
639*cdf0e10cSrcweir     appendU2(m_methods, addUtf8Info(descriptor));
640*cdf0e10cSrcweir     std::vector< rtl::OString >::size_type excs = exceptions.size();
641*cdf0e10cSrcweir     if (excs > SAL_MAX_UINT16) {
642*cdf0e10cSrcweir         throw CannotDumpException(
643*cdf0e10cSrcweir             rtl::OString(
644*cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM(
645*cdf0e10cSrcweir                     "Too many exception specifications for Java class file"
646*cdf0e10cSrcweir                     " format")));
647*cdf0e10cSrcweir     }
648*cdf0e10cSrcweir     appendU2(
649*cdf0e10cSrcweir         m_methods,
650*cdf0e10cSrcweir         ((code == 0 ? 0 : 1) + (exceptions.empty() ? 0 : 1)
651*cdf0e10cSrcweir          + (signature.getLength() == 0 ? 0 : 1)));
652*cdf0e10cSrcweir     if (code != 0) {
653*cdf0e10cSrcweir         std::vector< unsigned char >::size_type codeSize = code->m_code.size();
654*cdf0e10cSrcweir         std::vector< unsigned char >::size_type exceptionTableSize
655*cdf0e10cSrcweir             = code->m_exceptionTable.size();
656*cdf0e10cSrcweir         if (codeSize > SAL_MAX_UINT32 - (2 + 2 + 4 + 2 + 2)
657*cdf0e10cSrcweir             || (exceptionTableSize
658*cdf0e10cSrcweir                 > (SAL_MAX_UINT32 - (2 + 2 + 4 + 2 + 2)
659*cdf0e10cSrcweir                    - static_cast< sal_uInt32 >(codeSize))))
660*cdf0e10cSrcweir         {
661*cdf0e10cSrcweir             throw CannotDumpException(
662*cdf0e10cSrcweir                 rtl::OString(
663*cdf0e10cSrcweir                     RTL_CONSTASCII_STRINGPARAM(
664*cdf0e10cSrcweir                         "Code block is too big for Java class file format")));
665*cdf0e10cSrcweir         }
666*cdf0e10cSrcweir         appendU2(
667*cdf0e10cSrcweir             m_methods,
668*cdf0e10cSrcweir             addUtf8Info(rtl::OString(RTL_CONSTASCII_STRINGPARAM("Code"))));
669*cdf0e10cSrcweir         appendU4(
670*cdf0e10cSrcweir             m_methods,
671*cdf0e10cSrcweir             (2 + 2 + 4 + static_cast< sal_uInt32 >(codeSize) + 2
672*cdf0e10cSrcweir              + static_cast< sal_uInt32 >(exceptionTableSize) + 2));
673*cdf0e10cSrcweir         appendU2(m_methods, code->m_maxStack);
674*cdf0e10cSrcweir         appendU2(m_methods, code->m_maxLocals);
675*cdf0e10cSrcweir         appendU4(m_methods, static_cast< sal_uInt32 >(codeSize));
676*cdf0e10cSrcweir         appendStream(m_methods, code->m_code);
677*cdf0e10cSrcweir         appendU2(m_methods, code->m_exceptionTableLength);
678*cdf0e10cSrcweir         appendStream(m_methods, code->m_exceptionTable);
679*cdf0e10cSrcweir         appendU2(m_methods, 0);
680*cdf0e10cSrcweir     }
681*cdf0e10cSrcweir     if (!exceptions.empty()) {
682*cdf0e10cSrcweir         appendU2(
683*cdf0e10cSrcweir             m_methods,
684*cdf0e10cSrcweir             addUtf8Info(
685*cdf0e10cSrcweir                 rtl::OString(RTL_CONSTASCII_STRINGPARAM("Exceptions"))));
686*cdf0e10cSrcweir         appendU4(
687*cdf0e10cSrcweir             m_methods,
688*cdf0e10cSrcweir             static_cast< sal_uInt32 >(2 + 2 * static_cast< sal_uInt32 >(excs)));
689*cdf0e10cSrcweir         appendU2(m_methods, static_cast< sal_uInt16 >(excs));
690*cdf0e10cSrcweir         for (std::vector< rtl::OString >::const_iterator i(exceptions.begin());
691*cdf0e10cSrcweir              i != exceptions.end(); ++i)
692*cdf0e10cSrcweir         {
693*cdf0e10cSrcweir             appendU2(m_methods, addClassInfo(*i));
694*cdf0e10cSrcweir         }
695*cdf0e10cSrcweir     }
696*cdf0e10cSrcweir     appendSignatureAttribute(m_methods, signature);
697*cdf0e10cSrcweir }
698*cdf0e10cSrcweir 
699*cdf0e10cSrcweir void ClassFile::write(FileStream & file) const {
700*cdf0e10cSrcweir     writeU4(file, 0xCAFEBABE);
701*cdf0e10cSrcweir     writeU2(file, 0);
702*cdf0e10cSrcweir     writeU2(file, 46);
703*cdf0e10cSrcweir     writeU2(file, m_constantPoolCount);
704*cdf0e10cSrcweir     writeStream(file, m_constantPool);
705*cdf0e10cSrcweir     writeU2(file, static_cast< sal_uInt16 >(m_accessFlags));
706*cdf0e10cSrcweir     writeU2(file, m_thisClass);
707*cdf0e10cSrcweir     writeU2(file, m_superClass);
708*cdf0e10cSrcweir     writeU2(file, m_interfacesCount);
709*cdf0e10cSrcweir     writeStream(file, m_interfaces);
710*cdf0e10cSrcweir     writeU2(file, m_fieldsCount);
711*cdf0e10cSrcweir     writeStream(file, m_fields);
712*cdf0e10cSrcweir     writeU2(file, m_methodsCount);
713*cdf0e10cSrcweir     writeStream(file, m_methods);
714*cdf0e10cSrcweir     writeU2(file, m_attributesCount);
715*cdf0e10cSrcweir     writeStream(file, m_attributes);
716*cdf0e10cSrcweir }
717*cdf0e10cSrcweir 
718*cdf0e10cSrcweir sal_uInt16 ClassFile::nextConstantPoolIndex(sal_uInt16 width) {
719*cdf0e10cSrcweir     OSL_ASSERT(width == 1 || width == 2);
720*cdf0e10cSrcweir     if (m_constantPoolCount > SAL_MAX_UINT16 - width) {
721*cdf0e10cSrcweir         throw CannotDumpException(
722*cdf0e10cSrcweir             rtl::OString(
723*cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM(
724*cdf0e10cSrcweir                     "Too many constant pool items for Java class file"
725*cdf0e10cSrcweir                     " format")));
726*cdf0e10cSrcweir     }
727*cdf0e10cSrcweir     sal_uInt16 index = m_constantPoolCount;
728*cdf0e10cSrcweir     m_constantPoolCount = m_constantPoolCount + width;
729*cdf0e10cSrcweir     return index;
730*cdf0e10cSrcweir }
731*cdf0e10cSrcweir 
732*cdf0e10cSrcweir sal_uInt16 ClassFile::addUtf8Info(rtl::OString const & value) {
733*cdf0e10cSrcweir     std::map< rtl::OString, sal_uInt16 >::iterator i(m_utf8Infos.find(value));
734*cdf0e10cSrcweir     if (i != m_utf8Infos.end()) {
735*cdf0e10cSrcweir         return i->second;
736*cdf0e10cSrcweir     }
737*cdf0e10cSrcweir     if (value.getLength() > SAL_MAX_UINT16) {
738*cdf0e10cSrcweir         throw CannotDumpException(
739*cdf0e10cSrcweir             rtl::OString(
740*cdf0e10cSrcweir                 RTL_CONSTASCII_STRINGPARAM(
741*cdf0e10cSrcweir                     "UTF-8 string too long for Java class file format")));
742*cdf0e10cSrcweir     }
743*cdf0e10cSrcweir     sal_uInt16 index = nextConstantPoolIndex(1);
744*cdf0e10cSrcweir     appendU1(m_constantPool, 1);
745*cdf0e10cSrcweir     appendU2(m_constantPool, static_cast< sal_uInt16 >(value.getLength()));
746*cdf0e10cSrcweir     for (sal_Int32 j = 0; j < value.getLength(); ++j) {
747*cdf0e10cSrcweir         appendU1(m_constantPool, static_cast< sal_uInt8 >(value[j]));
748*cdf0e10cSrcweir     }
749*cdf0e10cSrcweir     if (!m_utf8Infos.insert(
750*cdf0e10cSrcweir             std::map< rtl::OString, sal_uInt16 >::value_type(value, index)).
751*cdf0e10cSrcweir         second)
752*cdf0e10cSrcweir     {
753*cdf0e10cSrcweir         OSL_ASSERT(false);
754*cdf0e10cSrcweir     }
755*cdf0e10cSrcweir     return index;
756*cdf0e10cSrcweir }
757*cdf0e10cSrcweir 
758*cdf0e10cSrcweir sal_uInt16 ClassFile::addClassInfo(rtl::OString const & type) {
759*cdf0e10cSrcweir     sal_uInt16 nameIndex = addUtf8Info(type);
760*cdf0e10cSrcweir     std::map< sal_uInt16, sal_uInt16 >::iterator i(
761*cdf0e10cSrcweir         m_classInfos.find(nameIndex));
762*cdf0e10cSrcweir     if (i != m_classInfos.end()) {
763*cdf0e10cSrcweir         return i->second;
764*cdf0e10cSrcweir     }
765*cdf0e10cSrcweir     sal_uInt16 index = nextConstantPoolIndex(1);
766*cdf0e10cSrcweir     appendU1(m_constantPool, 7);
767*cdf0e10cSrcweir     appendU2(m_constantPool, nameIndex);
768*cdf0e10cSrcweir     if (!m_classInfos.insert(
769*cdf0e10cSrcweir             std::map< sal_uInt16, sal_uInt16 >::value_type(nameIndex, index)).
770*cdf0e10cSrcweir         second)
771*cdf0e10cSrcweir     {
772*cdf0e10cSrcweir         OSL_ASSERT(false);
773*cdf0e10cSrcweir     }
774*cdf0e10cSrcweir     return index;
775*cdf0e10cSrcweir }
776*cdf0e10cSrcweir 
777*cdf0e10cSrcweir sal_uInt16 ClassFile::addStringInfo(rtl::OString const & value) {
778*cdf0e10cSrcweir     sal_uInt16 stringIndex = addUtf8Info(value);
779*cdf0e10cSrcweir     std::map< sal_uInt16, sal_uInt16 >::iterator i(
780*cdf0e10cSrcweir         m_stringInfos.find(stringIndex));
781*cdf0e10cSrcweir     if (i != m_stringInfos.end()) {
782*cdf0e10cSrcweir         return i->second;
783*cdf0e10cSrcweir     }
784*cdf0e10cSrcweir     sal_uInt16 index = nextConstantPoolIndex(1);
785*cdf0e10cSrcweir     appendU1(m_constantPool, 8);
786*cdf0e10cSrcweir     appendU2(m_constantPool, stringIndex);
787*cdf0e10cSrcweir     if (!m_stringInfos.insert(
788*cdf0e10cSrcweir             std::map< sal_uInt16, sal_uInt16 >::value_type(stringIndex, index)).
789*cdf0e10cSrcweir         second)
790*cdf0e10cSrcweir     {
791*cdf0e10cSrcweir         OSL_ASSERT(false);
792*cdf0e10cSrcweir     }
793*cdf0e10cSrcweir     return index;
794*cdf0e10cSrcweir }
795*cdf0e10cSrcweir 
796*cdf0e10cSrcweir sal_uInt16 ClassFile::addFieldrefInfo(
797*cdf0e10cSrcweir     rtl::OString const & type, rtl::OString const & name,
798*cdf0e10cSrcweir     rtl::OString const & descriptor)
799*cdf0e10cSrcweir {
800*cdf0e10cSrcweir     sal_uInt16 classIndex = addClassInfo(type);
801*cdf0e10cSrcweir     sal_uInt16 nameAndTypeIndex = addNameAndTypeInfo(name, descriptor);
802*cdf0e10cSrcweir     sal_uInt32 key = (static_cast< sal_uInt32 >(classIndex) << 16)
803*cdf0e10cSrcweir         | nameAndTypeIndex;
804*cdf0e10cSrcweir     std::map< sal_uInt32, sal_uInt16 >::iterator i(m_fieldrefInfos.find(key));
805*cdf0e10cSrcweir     if (i != m_fieldrefInfos.end()) {
806*cdf0e10cSrcweir         return i->second;
807*cdf0e10cSrcweir     }
808*cdf0e10cSrcweir     sal_uInt16 index = nextConstantPoolIndex(1);
809*cdf0e10cSrcweir     appendU1(m_constantPool, 9);
810*cdf0e10cSrcweir     appendU2(m_constantPool, classIndex);
811*cdf0e10cSrcweir     appendU2(m_constantPool, nameAndTypeIndex);
812*cdf0e10cSrcweir     if (!m_fieldrefInfos.insert(
813*cdf0e10cSrcweir             std::map< sal_uInt32, sal_uInt16 >::value_type(key, index)).second)
814*cdf0e10cSrcweir     {
815*cdf0e10cSrcweir         OSL_ASSERT(false);
816*cdf0e10cSrcweir     }
817*cdf0e10cSrcweir     return index;
818*cdf0e10cSrcweir }
819*cdf0e10cSrcweir 
820*cdf0e10cSrcweir sal_uInt16 ClassFile::addMethodrefInfo(
821*cdf0e10cSrcweir     rtl::OString const & type, rtl::OString const & name,
822*cdf0e10cSrcweir     rtl::OString const & descriptor)
823*cdf0e10cSrcweir {
824*cdf0e10cSrcweir     sal_uInt16 classIndex = addClassInfo(type);
825*cdf0e10cSrcweir     sal_uInt16 nameAndTypeIndex = addNameAndTypeInfo(name, descriptor);
826*cdf0e10cSrcweir     sal_uInt32 key = (static_cast< sal_uInt32 >(classIndex) << 16)
827*cdf0e10cSrcweir         | nameAndTypeIndex;
828*cdf0e10cSrcweir     std::map< sal_uInt32, sal_uInt16 >::iterator i(m_methodrefInfos.find(key));
829*cdf0e10cSrcweir     if (i != m_methodrefInfos.end()) {
830*cdf0e10cSrcweir         return i->second;
831*cdf0e10cSrcweir     }
832*cdf0e10cSrcweir     sal_uInt16 index = nextConstantPoolIndex(1);
833*cdf0e10cSrcweir     appendU1(m_constantPool, 10);
834*cdf0e10cSrcweir     appendU2(m_constantPool, classIndex);
835*cdf0e10cSrcweir     appendU2(m_constantPool, nameAndTypeIndex);
836*cdf0e10cSrcweir     if (!m_methodrefInfos.insert(
837*cdf0e10cSrcweir             std::map< sal_uInt32, sal_uInt16 >::value_type(key, index)).second)
838*cdf0e10cSrcweir     {
839*cdf0e10cSrcweir         OSL_ASSERT(false);
840*cdf0e10cSrcweir     }
841*cdf0e10cSrcweir     return index;
842*cdf0e10cSrcweir }
843*cdf0e10cSrcweir 
844*cdf0e10cSrcweir sal_uInt16 ClassFile::addInterfaceMethodrefInfo(
845*cdf0e10cSrcweir     rtl::OString const & type, rtl::OString const & name,
846*cdf0e10cSrcweir     rtl::OString const & descriptor)
847*cdf0e10cSrcweir {
848*cdf0e10cSrcweir     sal_uInt16 classIndex = addClassInfo(type);
849*cdf0e10cSrcweir     sal_uInt16 nameAndTypeIndex = addNameAndTypeInfo(name, descriptor);
850*cdf0e10cSrcweir     sal_uInt32 key = (static_cast< sal_uInt32 >(classIndex) << 16)
851*cdf0e10cSrcweir         | nameAndTypeIndex;
852*cdf0e10cSrcweir     std::map< sal_uInt32, sal_uInt16 >::iterator i(
853*cdf0e10cSrcweir         m_interfaceMethodrefInfos.find(key));
854*cdf0e10cSrcweir     if (i != m_interfaceMethodrefInfos.end()) {
855*cdf0e10cSrcweir         return i->second;
856*cdf0e10cSrcweir     }
857*cdf0e10cSrcweir     sal_uInt16 index = nextConstantPoolIndex(1);
858*cdf0e10cSrcweir     appendU1(m_constantPool, 11);
859*cdf0e10cSrcweir     appendU2(m_constantPool, classIndex);
860*cdf0e10cSrcweir     appendU2(m_constantPool, nameAndTypeIndex);
861*cdf0e10cSrcweir     if (!m_interfaceMethodrefInfos.insert(
862*cdf0e10cSrcweir             std::map< sal_uInt32, sal_uInt16 >::value_type(key, index)).second)
863*cdf0e10cSrcweir     {
864*cdf0e10cSrcweir         OSL_ASSERT(false);
865*cdf0e10cSrcweir     }
866*cdf0e10cSrcweir     return index;
867*cdf0e10cSrcweir }
868*cdf0e10cSrcweir 
869*cdf0e10cSrcweir sal_uInt16 ClassFile::addNameAndTypeInfo(
870*cdf0e10cSrcweir     rtl::OString const & name, rtl::OString const & descriptor)
871*cdf0e10cSrcweir {
872*cdf0e10cSrcweir     sal_uInt16 nameIndex = addUtf8Info(name);
873*cdf0e10cSrcweir     sal_uInt16 descriptorIndex = addUtf8Info(descriptor);
874*cdf0e10cSrcweir     sal_uInt32 key = (static_cast< sal_uInt32 >(nameIndex) << 16)
875*cdf0e10cSrcweir         | descriptorIndex;
876*cdf0e10cSrcweir     std::map< sal_uInt32, sal_uInt16 >::iterator i(
877*cdf0e10cSrcweir         m_nameAndTypeInfos.find(key));
878*cdf0e10cSrcweir     if (i != m_nameAndTypeInfos.end()) {
879*cdf0e10cSrcweir         return i->second;
880*cdf0e10cSrcweir     }
881*cdf0e10cSrcweir     sal_uInt16 index = nextConstantPoolIndex(1);
882*cdf0e10cSrcweir     appendU1(m_constantPool, 12);
883*cdf0e10cSrcweir     appendU2(m_constantPool, nameIndex);
884*cdf0e10cSrcweir     appendU2(m_constantPool, descriptorIndex);
885*cdf0e10cSrcweir     if (!m_nameAndTypeInfos.insert(
886*cdf0e10cSrcweir             std::map< sal_uInt32, sal_uInt16 >::value_type(key, index)).second)
887*cdf0e10cSrcweir     {
888*cdf0e10cSrcweir         OSL_ASSERT(false);
889*cdf0e10cSrcweir     }
890*cdf0e10cSrcweir     return index;
891*cdf0e10cSrcweir }
892*cdf0e10cSrcweir 
893*cdf0e10cSrcweir void ClassFile::appendSignatureAttribute(
894*cdf0e10cSrcweir     std::vector< unsigned char > & stream, rtl::OString const & signature)
895*cdf0e10cSrcweir {
896*cdf0e10cSrcweir     if (signature.getLength() != 0) {
897*cdf0e10cSrcweir         appendU2(
898*cdf0e10cSrcweir             stream,
899*cdf0e10cSrcweir             addUtf8Info(rtl::OString(RTL_CONSTASCII_STRINGPARAM("Signature"))));
900*cdf0e10cSrcweir         appendU4(stream, 2);
901*cdf0e10cSrcweir         appendU2(stream, addUtf8Info(signature));
902*cdf0e10cSrcweir     }
903*cdf0e10cSrcweir }
904