LCOV - code coverage report
Current view: top level - codemaker/source/javamaker - classfile.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 472 498 94.8 %
Date: 2014-11-03 Functions: 68 69 98.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10