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