LCOV - code coverage report
Current view: top level - codemaker/source/javamaker - classfile.cxx (source / functions) Hit Total Coverage
Test: commit e02a6cb2c3e2b23b203b422e4e0680877f232636 Lines: 0 497 0.0 %
Date: 2014-04-14 Functions: 0 69 0.0 %
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           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           0 :         throw CannotDumpException("Error writing file");
      78           0 : }
      79             : 
      80           0 : void writeU2(FileStream & file, sal_uInt16 data) {
      81             :     unsigned char buf[] = {
      82           0 :         static_cast< unsigned char >(data >> 8),
      83           0 :         static_cast< unsigned char >(data & 0xFF) };
      84           0 :     write(file, buf, sizeof buf);
      85           0 : }
      86             : 
      87           0 : void writeU4(FileStream & file, sal_uInt32 data) {
      88             :     unsigned char buf[] = {
      89           0 :         static_cast< unsigned char >(data >> 24),
      90           0 :         static_cast< unsigned char >((data >> 16) & 0xFF),
      91           0 :         static_cast< unsigned char >((data >> 8) & 0xFF),
      92           0 :         static_cast< unsigned char >(data & 0xFF) };
      93           0 :     write(file, buf, sizeof buf);
      94           0 : }
      95             : 
      96           0 : void writeStream(FileStream & file, std::vector< unsigned char > const & stream)
      97             : {
      98           0 :     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           0 :     if (n != 0) {
     106           0 :         write(file, &stream[0], static_cast< sal_uInt64 >(n));
     107             :     }
     108           0 : }
     109             : 
     110             : }
     111             : 
     112           0 : ClassFile::Code::~Code() {}
     113             : 
     114           0 : void ClassFile::Code::instrAastore() {
     115             :     // aastore:
     116           0 :     appendU1(m_code, 0x53);
     117           0 : }
     118             : 
     119           0 : void ClassFile::Code::instrAconstNull() {
     120             :     // aconst_null:
     121           0 :     appendU1(m_code, 0x01);
     122           0 : }
     123             : 
     124           0 : void ClassFile::Code::instrAnewarray(OString const & type) {
     125             :     // anewarray <indexbyte1> <indexbyte2>:
     126           0 :     appendU1(m_code, 0xBD);
     127           0 :     appendU2(m_code, m_classFile.addClassInfo(type));
     128           0 : }
     129             : 
     130           0 : void ClassFile::Code::instrAreturn() {
     131             :     // areturn:
     132           0 :     appendU1(m_code, 0xB0);
     133           0 : }
     134             : 
     135           0 : void ClassFile::Code::instrAthrow() {
     136             :     // athrow:
     137           0 :     appendU1(m_code, 0xBF);
     138           0 : }
     139             : 
     140           0 : void ClassFile::Code::instrCheckcast(OString const & type) {
     141             :     // checkcast <indexbyte1> <indexbyte2>:
     142           0 :     appendU1(m_code, 0xC0);
     143           0 :     appendU2(m_code, m_classFile.addClassInfo(type));
     144           0 : }
     145             : 
     146           0 : void ClassFile::Code::instrDup() {
     147             :     // dup:
     148           0 :     appendU1(m_code, 0x59);
     149           0 : }
     150             : 
     151           0 : void ClassFile::Code::instrGetstatic(
     152             :     OString const & type, OString const & name,
     153             :     OString const & descriptor)
     154             : {
     155             :     // getstatic <indexbyte1> <indexbyte2>:
     156           0 :     appendU1(m_code, 0xB2);
     157           0 :     appendU2(m_code, m_classFile.addFieldrefInfo(type, name, descriptor));
     158           0 : }
     159             : 
     160           0 : ClassFile::Code::Branch ClassFile::Code::instrIfAcmpne() {
     161             :     // if_acmpne <branchbyte1> <branchbyte2>:
     162           0 :     Branch branch = m_code.size();
     163           0 :     appendU1(m_code, 0xA6);
     164           0 :     appendU2(m_code, 0);
     165           0 :     return branch;
     166             : }
     167             : 
     168           0 : ClassFile::Code::Branch ClassFile::Code::instrIfeq() {
     169             :     // ifeq <branchbyte1> <branchbyte2>:
     170           0 :     Branch branch = m_code.size();
     171           0 :     appendU1(m_code, 0x99);
     172           0 :     appendU2(m_code, 0);
     173           0 :     return branch;
     174             : }
     175             : 
     176           0 : ClassFile::Code::Branch ClassFile::Code::instrIfnull() {
     177             :     // ifnull <branchbyte1> <branchbyte2>:
     178           0 :     Branch branch = m_code.size();
     179           0 :     appendU1(m_code, 0xC6);
     180           0 :     appendU2(m_code, 0);
     181           0 :     return branch;
     182             : }
     183             : 
     184           0 : void ClassFile::Code::instrInstanceof(OString const & type) {
     185             :     // instanceof <indexbyte1> <indexbyte2>:
     186           0 :     appendU1(m_code, 0xC1);
     187           0 :     appendU2(m_code, m_classFile.addClassInfo(type));
     188           0 : }
     189             : 
     190           0 : 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           0 :     appendU1(m_code, 0xB9);
     196             :     appendU2(
     197           0 :         m_code, m_classFile.addInterfaceMethodrefInfo(type, name, descriptor));
     198           0 :     appendU1(m_code, args);
     199           0 :     appendU1(m_code, 0);
     200           0 : }
     201             : 
     202           0 : void ClassFile::Code::instrInvokespecial(
     203             :     OString const & type, OString const & name,
     204             :     OString const & descriptor)
     205             : {
     206             :     // invokespecial <indexbyte1> <indexbyte2>:
     207           0 :     appendU1(m_code, 0xB7);
     208           0 :     appendU2(m_code, m_classFile.addMethodrefInfo(type, name, descriptor));
     209           0 : }
     210             : 
     211           0 : void ClassFile::Code::instrInvokestatic(
     212             :     OString const & type, OString const & name,
     213             :     OString const & descriptor)
     214             : {
     215             :     // invokestatic <indexbyte1> <indexbyte2>:
     216           0 :     appendU1(m_code, 0xB8);
     217           0 :     appendU2(m_code, m_classFile.addMethodrefInfo(type, name, descriptor));
     218           0 : }
     219             : 
     220           0 : void ClassFile::Code::instrInvokevirtual(
     221             :     OString const & type, OString const & name,
     222             :     OString const & descriptor)
     223             : {
     224             :     // invokevirtual <indexbyte1> <indexbyte2>:
     225           0 :     appendU1(m_code, 0xB6);
     226           0 :     appendU2(m_code, m_classFile.addMethodrefInfo(type, name, descriptor));
     227           0 : }
     228             : 
     229           0 : 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           0 :     std::list< std::pair< sal_Int32, Code * > >::size_type size = blocks.size();
     237           0 :     if (size > SAL_MAX_INT32) {
     238           0 :         throw CannotDumpException("Lookup-switch too large for Java class file format");
     239             :     }
     240           0 :     Position pos1 = m_code.size();
     241           0 :     appendU1(m_code, 0xAB);
     242           0 :     int pad = (pos1 + 1) % 4;
     243           0 :     for (int i = 0; i < pad; ++i) {
     244           0 :         appendU1(m_code, 0);
     245             :     }
     246           0 :     Position pos2 = pos1 + 1 + pad + 8 + blocks.size() * 8; //FIXME: overflow
     247           0 :     appendU4(m_code, static_cast< sal_uInt32 >(pos2 - pos1)); //FIXME: overflow
     248           0 :     pos2 += defaultBlock->m_code.size(); //FIXME: overflow
     249           0 :     appendU4(m_code, static_cast< sal_uInt32 >(size));
     250           0 :     for (std::list< std::pair< sal_Int32, Code * > >::const_iterator i(
     251           0 :              blocks.begin());
     252           0 :          i != blocks.end(); ++i)
     253             :     {
     254           0 :         appendU4(m_code, static_cast< sal_uInt32 >(i->first));
     255           0 :         appendU4(m_code, static_cast< sal_uInt32 >(pos2 - pos1));
     256             :             //FIXME: overflow
     257           0 :         pos2 += i->second->m_code.size(); //FIXME: overflow
     258             :     }
     259           0 :     appendStream(m_code, defaultBlock->m_code);
     260           0 :     for (std::list< std::pair< sal_Int32, Code * > >::const_iterator i(
     261           0 :              blocks.begin());
     262           0 :          i != blocks.end(); ++i)
     263             :     {
     264           0 :         appendStream(m_code, i->second->m_code);
     265             :     }
     266           0 : }
     267             : 
     268           0 : void ClassFile::Code::instrNew(OString const & type) {
     269             :     // new <indexbyte1> <indexbyte2>:
     270           0 :     appendU1(m_code, 0xBB);
     271           0 :     appendU2(m_code, m_classFile.addClassInfo(type));
     272           0 : }
     273             : 
     274           0 : 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           0 :     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           0 :     appendU1(m_code, atypes[sort - 1]);
     283           0 : }
     284             : 
     285           0 : void ClassFile::Code::instrPop() {
     286             :     // pop:
     287           0 :     appendU1(m_code, 0x57);
     288           0 : }
     289             : 
     290           0 : void ClassFile::Code::instrPutfield(
     291             :     OString const & type, OString const & name,
     292             :     OString const & descriptor)
     293             : {
     294             :     // putfield <indexbyte1> <indexbyte2>:
     295           0 :     appendU1(m_code, 0xB5);
     296           0 :     appendU2(m_code, m_classFile.addFieldrefInfo(type, name, descriptor));
     297           0 : }
     298             : 
     299           0 : void ClassFile::Code::instrPutstatic(
     300             :     OString const & type, OString const & name,
     301             :     OString const & descriptor)
     302             : {
     303             :     // putstatic <indexbyte1> <indexbyte2>:
     304           0 :     appendU1(m_code, 0xB3);
     305           0 :     appendU2(m_code, m_classFile.addFieldrefInfo(type, name, descriptor));
     306           0 : }
     307             : 
     308           0 : void ClassFile::Code::instrReturn() {
     309             :     // return:
     310           0 :     appendU1(m_code, 0xB1);
     311           0 : }
     312             : 
     313           0 : void ClassFile::Code::instrSwap() {
     314             :     // swap:
     315           0 :     appendU1(m_code, 0x5F);
     316           0 : }
     317             : 
     318           0 : 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           0 :     Position pos1 = m_code.size();
     326           0 :     appendU1(m_code, 0xAA);
     327           0 :     int pad = (pos1 + 1) % 4;
     328           0 :     for (int i = 0; i < pad; ++i) {
     329           0 :         appendU1(m_code, 0);
     330             :     }
     331           0 :     std::list< Code * >::size_type size = blocks.size();
     332           0 :     Position pos2 = pos1 + 1 + pad + 12 + size * 4; //FIXME: overflow
     333           0 :     sal_uInt32 defaultOffset = static_cast< sal_uInt32 >(pos2 - pos1);
     334             :         //FIXME: overflow
     335           0 :     appendU4(m_code, defaultOffset);
     336           0 :     pos2 += defaultBlock->m_code.size(); //FIXME: overflow
     337           0 :     appendU4(m_code, static_cast< sal_uInt32 >(low));
     338           0 :     appendU4(m_code, static_cast< sal_uInt32 >(low + (size - 1)));
     339           0 :     for (std::list< Code * >::const_iterator i(blocks.begin());
     340           0 :          i != blocks.end(); ++i)
     341             :     {
     342           0 :         if (*i == 0) {
     343           0 :             appendU4(m_code, defaultOffset);
     344             :         } else {
     345           0 :             appendU4(m_code, static_cast< sal_uInt32 >(pos2 - pos1));
     346             :                 //FIXME: overflow
     347           0 :             pos2 += (*i)->m_code.size(); //FIXME: overflow
     348             :         }
     349             :     }
     350           0 :     appendStream(m_code, defaultBlock->m_code);
     351           0 :     for (std::list< Code * >::const_iterator i(blocks.begin());
     352           0 :          i != blocks.end(); ++i)
     353             :     {
     354           0 :         if (*i != 0) {
     355           0 :             appendStream(m_code, (*i)->m_code);
     356             :         }
     357             :     }
     358           0 : }
     359             : 
     360           0 : void ClassFile::Code::loadIntegerConstant(sal_Int32 value) {
     361           0 :     if (value >= -1 && value <= 5) {
     362             :         // iconst_<i>:
     363           0 :         appendU1(m_code, static_cast< sal_uInt8 >(0x02 + value + 1));
     364           0 :     } else if (value >= -128 && value <= 127) {
     365             :         // bipush <byte>:
     366           0 :         appendU1(m_code, 0x10);
     367           0 :         appendU1(m_code, static_cast< sal_uInt8 >(value));
     368           0 :     } else if (value >= -32768 && value <= 32767) {
     369             :         // sipush <byte1> <byte2>:
     370           0 :         appendU1(m_code, 0x11);
     371           0 :         appendU2(m_code, static_cast< sal_uInt16 >(value));
     372             :     } else {
     373           0 :         ldc(m_classFile.addIntegerInfo(value));
     374             :     }
     375           0 : }
     376             : 
     377           0 : void ClassFile::Code::loadStringConstant(OString const & value) {
     378           0 :     ldc(m_classFile.addStringInfo(value));
     379           0 : }
     380             : 
     381           0 : void ClassFile::Code::loadLocalInteger(sal_uInt16 index) {
     382           0 :     accessLocal(index, 0x1A, 0x15); // iload_<n>, iload
     383           0 : }
     384             : 
     385           0 : void ClassFile::Code::loadLocalLong(sal_uInt16 index) {
     386           0 :     accessLocal(index, 0x1E, 0x16); // load_<n>, load
     387           0 : }
     388             : 
     389           0 : void ClassFile::Code::loadLocalFloat(sal_uInt16 index) {
     390           0 :     accessLocal(index, 0x22, 0x17); // load_<n>, load
     391           0 : }
     392             : 
     393           0 : void ClassFile::Code::loadLocalDouble(sal_uInt16 index) {
     394           0 :     accessLocal(index, 0x26, 0x18); // load_<n>, load
     395           0 : }
     396             : 
     397           0 : void ClassFile::Code::loadLocalReference(sal_uInt16 index) {
     398           0 :     accessLocal(index, 0x2A, 0x19); // aload_<n>, aload
     399           0 : }
     400             : 
     401           0 : void ClassFile::Code::storeLocalReference(sal_uInt16 index) {
     402           0 :     accessLocal(index, 0x4B, 0x3A); // astore_<n>, astore
     403           0 : }
     404             : 
     405           0 : void ClassFile::Code::branchHere(Branch branch) {
     406           0 :     std::vector< unsigned char >::size_type n = m_code.size();
     407             :     OSL_ASSERT(n > branch && n - branch <= SAL_MAX_INT16);
     408           0 :     n -= branch;
     409           0 :     m_code[branch + 1] = static_cast< sal_uInt8 >(n >> 8);
     410           0 :     m_code[branch + 2] = static_cast< sal_uInt8 >(n & 0xFF);
     411           0 : }
     412             : 
     413           0 : 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           0 :     if (m_exceptionTableLength == SAL_MAX_UINT16) {
     418           0 :         throw CannotDumpException("Too many exception handlers for Java class file format");
     419             :     }
     420           0 :     ++m_exceptionTableLength;
     421           0 :     appendU2(m_exceptionTable, static_cast< sal_uInt16 >(start));
     422             :         //FIXME: overflow
     423           0 :     appendU2(m_exceptionTable, static_cast< sal_uInt16 >(end));
     424             :         //FIXME: overflow
     425           0 :     appendU2(m_exceptionTable, static_cast< sal_uInt16 >(handler));
     426             :         //FIXME: overflow
     427           0 :     appendU2(m_exceptionTable, m_classFile.addClassInfo(type));
     428           0 : }
     429             : 
     430           0 : ClassFile::Code::Position ClassFile::Code::getPosition() const {
     431           0 :     return m_code.size();
     432             : }
     433             : 
     434           0 : ClassFile::Code::Code(ClassFile & classFile)
     435             :     : m_classFile(classFile)
     436             :     , m_maxStack(0)
     437             :     , m_maxLocals(0)
     438           0 :     , m_exceptionTableLength(0)
     439           0 : {}
     440             : 
     441           0 : void ClassFile::Code::ldc(sal_uInt16 index) {
     442           0 :     if (index <= 0xFF) {
     443             :         // ldc <index>:
     444           0 :         appendU1(m_code, 0x12);
     445           0 :         appendU1(m_code, static_cast< sal_uInt8 >(index));
     446             :     } else {
     447             :         // ldc_w <indexbyte1> <indexbyte2>:
     448           0 :         appendU1(m_code, 0x13);
     449           0 :         appendU2(m_code, index);
     450             :     }
     451           0 : }
     452             : 
     453           0 : void ClassFile::Code::accessLocal(
     454             :     sal_uInt16 index, sal_uInt8 fastOp, sal_uInt8 normalOp)
     455             : {
     456           0 :     if (index <= 3) {
     457             :         // ...load/store_<n>:
     458           0 :         appendU1(m_code, static_cast< sal_uInt8 >(fastOp + index));
     459           0 :     } else if (index <= 0xFF) {
     460             :         // ...load/store <index>:
     461           0 :         appendU1(m_code, normalOp);
     462           0 :         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           0 : }
     470             : 
     471           0 : 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           0 :     m_fieldsCount(0), m_methodsCount(0), m_attributesCount(0)
     476             : {
     477           0 :     m_thisClass = addClassInfo(thisClass);
     478           0 :     m_superClass = addClassInfo(superClass);
     479           0 :     if (!signature.isEmpty()) {
     480           0 :         ++m_attributesCount;
     481           0 :         appendU2(m_attributes, addUtf8Info("Signature"));
     482           0 :         appendU4(m_attributes, 2);
     483           0 :         appendU2(m_attributes, addUtf8Info(signature));
     484             :     }
     485           0 : }
     486             : 
     487           0 : ClassFile::~ClassFile() {}
     488             : 
     489           0 : ClassFile::Code * ClassFile::newCode() {
     490           0 :     return new Code(*this);
     491             : }
     492             : 
     493           0 : sal_uInt16 ClassFile::addIntegerInfo(sal_Int32 value) {
     494           0 :     std::map< sal_Int32, sal_uInt16 >::iterator i(m_integerInfos.find(value));
     495           0 :     if (i != m_integerInfos.end()) {
     496           0 :         return i->second;
     497             :     }
     498           0 :     sal_uInt16 index = nextConstantPoolIndex(1);
     499           0 :     appendU1(m_constantPool, 3);
     500           0 :     appendU4(m_constantPool, static_cast< sal_uInt32 >(value));
     501           0 :     if (!m_integerInfos.insert(
     502           0 :             std::map< sal_Int32, sal_uInt16 >::value_type(value, index)).second)
     503             :     {
     504             :         OSL_ASSERT(false);
     505             :     }
     506           0 :     return index;
     507             : }
     508             : 
     509           0 : sal_uInt16 ClassFile::addFloatInfo(float value) {
     510           0 :     std::map< float, sal_uInt16 >::iterator i(m_floatInfos.find(value));
     511           0 :     if (i != m_floatInfos.end()) {
     512           0 :         return i->second;
     513             :     }
     514           0 :     sal_uInt16 index = nextConstantPoolIndex(1);
     515           0 :     appendU1(m_constantPool, 4);
     516             :     union { float floatBytes; sal_uInt32 uint32Bytes; } bytes;
     517           0 :     bytes.floatBytes = value;
     518           0 :     appendU4(m_constantPool, bytes.uint32Bytes);
     519           0 :     if (!m_floatInfos.insert(
     520           0 :             std::map< float, sal_uInt16 >::value_type(value, index)).second)
     521             :     {
     522             :         OSL_ASSERT(false);
     523             :     }
     524           0 :     return index;
     525             : }
     526             : 
     527           0 : sal_uInt16 ClassFile::addLongInfo(sal_Int64 value) {
     528           0 :     std::map< sal_Int64, sal_uInt16 >::iterator i(m_longInfos.find(value));
     529           0 :     if (i != m_longInfos.end()) {
     530           0 :         return i->second;
     531             :     }
     532           0 :     sal_uInt16 index = nextConstantPoolIndex(2);
     533           0 :     appendU1(m_constantPool, 5);
     534           0 :     appendU8(m_constantPool, static_cast< sal_uInt64 >(value));
     535           0 :     if (!m_longInfos.insert(
     536           0 :             std::map< sal_Int64, sal_uInt16 >::value_type(value, index)).second)
     537             :     {
     538             :         OSL_ASSERT(false);
     539             :     }
     540           0 :     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           0 : void ClassFile::addInterface(OString const & interface) {
     562           0 :     if (m_interfacesCount == SAL_MAX_UINT16) {
     563           0 :         throw CannotDumpException("Too many interfaces for Java class file format");
     564             :     }
     565           0 :     ++m_interfacesCount;
     566           0 :     appendU2(m_interfaces, addClassInfo(interface));
     567           0 : }
     568             : 
     569           0 : void ClassFile::addField(
     570             :     AccessFlags accessFlags, OString const & name,
     571             :     OString const & descriptor, sal_uInt16 constantValueIndex,
     572             :     OString const & signature)
     573             : {
     574           0 :     if (m_fieldsCount == SAL_MAX_UINT16) {
     575           0 :         throw CannotDumpException("Too many fields for Java class file format");
     576             :     }
     577           0 :     ++m_fieldsCount;
     578           0 :     appendU2(m_fields, static_cast< sal_uInt16 >(accessFlags));
     579           0 :     appendU2(m_fields, addUtf8Info(name));
     580           0 :     appendU2(m_fields, addUtf8Info(descriptor));
     581             :     appendU2(
     582             :         m_fields,
     583             :         ((constantValueIndex == 0 ? 0 : 1)
     584           0 :          + (signature.isEmpty() ? 0 : 1)));
     585           0 :     if (constantValueIndex != 0) {
     586           0 :         appendU2(m_fields, addUtf8Info("ConstantValue"));
     587           0 :         appendU4(m_fields, 2);
     588           0 :         appendU2(m_fields, constantValueIndex);
     589             :     }
     590           0 :     appendSignatureAttribute(m_fields, signature);
     591           0 : }
     592             : 
     593           0 : 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           0 :     if (m_methodsCount == SAL_MAX_UINT16) {
     600           0 :         throw CannotDumpException("Too many methods for Java class file format");
     601             :     }
     602           0 :     ++m_methodsCount;
     603           0 :     appendU2(m_methods, static_cast< sal_uInt16 >(accessFlags));
     604           0 :     appendU2(m_methods, addUtf8Info(name));
     605           0 :     appendU2(m_methods, addUtf8Info(descriptor));
     606           0 :     std::vector< OString >::size_type excs = exceptions.size();
     607           0 :     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           0 :         ((code == 0 ? 0 : 1) + (exceptions.empty() ? 0 : 1)
     613           0 :          + (signature.isEmpty() ? 0 : 1)));
     614           0 :     if (code != 0) {
     615           0 :         std::vector< unsigned char >::size_type codeSize = code->m_code.size();
     616             :         std::vector< unsigned char >::size_type exceptionTableSize
     617           0 :             = code->m_exceptionTable.size();
     618           0 :         if (codeSize > SAL_MAX_UINT32 - (2 + 2 + 4 + 2 + 2)
     619           0 :             || (exceptionTableSize
     620             :                 > (SAL_MAX_UINT32 - (2 + 2 + 4 + 2 + 2)
     621           0 :                    - static_cast< sal_uInt32 >(codeSize))))
     622             :         {
     623           0 :             throw CannotDumpException("Code block is too big for Java class file format");
     624             :         }
     625           0 :         appendU2(m_methods, addUtf8Info("Code"));
     626             :         appendU4(
     627             :             m_methods,
     628             :             (2 + 2 + 4 + static_cast< sal_uInt32 >(codeSize) + 2
     629           0 :              + static_cast< sal_uInt32 >(exceptionTableSize) + 2));
     630           0 :         appendU2(m_methods, code->m_maxStack);
     631           0 :         appendU2(m_methods, code->m_maxLocals);
     632           0 :         appendU4(m_methods, static_cast< sal_uInt32 >(codeSize));
     633           0 :         appendStream(m_methods, code->m_code);
     634           0 :         appendU2(m_methods, code->m_exceptionTableLength);
     635           0 :         appendStream(m_methods, code->m_exceptionTable);
     636           0 :         appendU2(m_methods, 0);
     637             :     }
     638           0 :     if (!exceptions.empty()) {
     639           0 :         appendU2(m_methods, addUtf8Info("Exceptions"));
     640             :         appendU4(
     641             :             m_methods,
     642           0 :             static_cast< sal_uInt32 >(2 + 2 * static_cast< sal_uInt32 >(excs)));
     643           0 :         appendU2(m_methods, static_cast< sal_uInt16 >(excs));
     644           0 :         for (std::vector< OString >::const_iterator i(exceptions.begin());
     645           0 :              i != exceptions.end(); ++i)
     646             :         {
     647           0 :             appendU2(m_methods, addClassInfo(*i));
     648             :         }
     649             :     }
     650           0 :     appendSignatureAttribute(m_methods, signature);
     651           0 : }
     652             : 
     653           0 : void ClassFile::write(FileStream & file) const {
     654           0 :     writeU4(file, 0xCAFEBABE);
     655           0 :     writeU2(file, 0);
     656           0 :     writeU2(file, 49); // class file version of JRE 1.5
     657           0 :     writeU2(file, m_constantPoolCount);
     658           0 :     writeStream(file, m_constantPool);
     659           0 :     writeU2(file, static_cast< sal_uInt16 >(m_accessFlags));
     660           0 :     writeU2(file, m_thisClass);
     661           0 :     writeU2(file, m_superClass);
     662           0 :     writeU2(file, m_interfacesCount);
     663           0 :     writeStream(file, m_interfaces);
     664           0 :     writeU2(file, m_fieldsCount);
     665           0 :     writeStream(file, m_fields);
     666           0 :     writeU2(file, m_methodsCount);
     667           0 :     writeStream(file, m_methods);
     668           0 :     writeU2(file, m_attributesCount);
     669           0 :     writeStream(file, m_attributes);
     670           0 : }
     671             : 
     672           0 : sal_uInt16 ClassFile::nextConstantPoolIndex(sal_uInt16 width) {
     673             :     OSL_ASSERT(width == 1 || width == 2);
     674           0 :     if (m_constantPoolCount > SAL_MAX_UINT16 - width) {
     675           0 :         throw CannotDumpException("Too many constant pool items for Java class file format");
     676             :     }
     677           0 :     sal_uInt16 index = m_constantPoolCount;
     678           0 :     m_constantPoolCount = m_constantPoolCount + width;
     679           0 :     return index;
     680             : }
     681             : 
     682           0 : sal_uInt16 ClassFile::addUtf8Info(OString const & value) {
     683           0 :     std::map< OString, sal_uInt16 >::iterator i(m_utf8Infos.find(value));
     684           0 :     if (i != m_utf8Infos.end()) {
     685           0 :         return i->second;
     686             :     }
     687           0 :     if (value.getLength() > SAL_MAX_UINT16) {
     688           0 :         throw CannotDumpException("UTF-8 string too long for Java class file format");
     689             :     }
     690           0 :     sal_uInt16 index = nextConstantPoolIndex(1);
     691           0 :     appendU1(m_constantPool, 1);
     692           0 :     appendU2(m_constantPool, static_cast< sal_uInt16 >(value.getLength()));
     693           0 :     for (sal_Int32 j = 0; j < value.getLength(); ++j) {
     694           0 :         appendU1(m_constantPool, static_cast< sal_uInt8 >(value[j]));
     695             :     }
     696           0 :     if (!m_utf8Infos.insert(
     697           0 :             std::map< OString, sal_uInt16 >::value_type(value, index)).
     698           0 :         second)
     699             :     {
     700             :         OSL_ASSERT(false);
     701             :     }
     702           0 :     return index;
     703             : }
     704             : 
     705           0 : sal_uInt16 ClassFile::addClassInfo(OString const & type) {
     706           0 :     sal_uInt16 nameIndex = addUtf8Info(type);
     707             :     std::map< sal_uInt16, sal_uInt16 >::iterator i(
     708           0 :         m_classInfos.find(nameIndex));
     709           0 :     if (i != m_classInfos.end()) {
     710           0 :         return i->second;
     711             :     }
     712           0 :     sal_uInt16 index = nextConstantPoolIndex(1);
     713           0 :     appendU1(m_constantPool, 7);
     714           0 :     appendU2(m_constantPool, nameIndex);
     715           0 :     if (!m_classInfos.insert(
     716           0 :             std::map< sal_uInt16, sal_uInt16 >::value_type(nameIndex, index)).
     717           0 :         second)
     718             :     {
     719             :         OSL_ASSERT(false);
     720             :     }
     721           0 :     return index;
     722             : }
     723             : 
     724           0 : sal_uInt16 ClassFile::addStringInfo(OString const & value) {
     725           0 :     sal_uInt16 stringIndex = addUtf8Info(value);
     726             :     std::map< sal_uInt16, sal_uInt16 >::iterator i(
     727           0 :         m_stringInfos.find(stringIndex));
     728           0 :     if (i != m_stringInfos.end()) {
     729           0 :         return i->second;
     730             :     }
     731           0 :     sal_uInt16 index = nextConstantPoolIndex(1);
     732           0 :     appendU1(m_constantPool, 8);
     733           0 :     appendU2(m_constantPool, stringIndex);
     734           0 :     if (!m_stringInfos.insert(
     735           0 :             std::map< sal_uInt16, sal_uInt16 >::value_type(stringIndex, index)).
     736           0 :         second)
     737             :     {
     738             :         OSL_ASSERT(false);
     739             :     }
     740           0 :     return index;
     741             : }
     742             : 
     743           0 : sal_uInt16 ClassFile::addFieldrefInfo(
     744             :     OString const & type, OString const & name,
     745             :     OString const & descriptor)
     746             : {
     747           0 :     sal_uInt16 classIndex = addClassInfo(type);
     748           0 :     sal_uInt16 nameAndTypeIndex = addNameAndTypeInfo(name, descriptor);
     749           0 :     sal_uInt32 key = (static_cast< sal_uInt32 >(classIndex) << 16)
     750           0 :         | nameAndTypeIndex;
     751           0 :     std::map< sal_uInt32, sal_uInt16 >::iterator i(m_fieldrefInfos.find(key));
     752           0 :     if (i != m_fieldrefInfos.end()) {
     753           0 :         return i->second;
     754             :     }
     755           0 :     sal_uInt16 index = nextConstantPoolIndex(1);
     756           0 :     appendU1(m_constantPool, 9);
     757           0 :     appendU2(m_constantPool, classIndex);
     758           0 :     appendU2(m_constantPool, nameAndTypeIndex);
     759           0 :     if (!m_fieldrefInfos.insert(
     760           0 :             std::map< sal_uInt32, sal_uInt16 >::value_type(key, index)).second)
     761             :     {
     762             :         OSL_ASSERT(false);
     763             :     }
     764           0 :     return index;
     765             : }
     766             : 
     767           0 : sal_uInt16 ClassFile::addMethodrefInfo(
     768             :     OString const & type, OString const & name,
     769             :     OString const & descriptor)
     770             : {
     771           0 :     sal_uInt16 classIndex = addClassInfo(type);
     772           0 :     sal_uInt16 nameAndTypeIndex = addNameAndTypeInfo(name, descriptor);
     773           0 :     sal_uInt32 key = (static_cast< sal_uInt32 >(classIndex) << 16)
     774           0 :         | nameAndTypeIndex;
     775           0 :     std::map< sal_uInt32, sal_uInt16 >::iterator i(m_methodrefInfos.find(key));
     776           0 :     if (i != m_methodrefInfos.end()) {
     777           0 :         return i->second;
     778             :     }
     779           0 :     sal_uInt16 index = nextConstantPoolIndex(1);
     780           0 :     appendU1(m_constantPool, 10);
     781           0 :     appendU2(m_constantPool, classIndex);
     782           0 :     appendU2(m_constantPool, nameAndTypeIndex);
     783           0 :     if (!m_methodrefInfos.insert(
     784           0 :             std::map< sal_uInt32, sal_uInt16 >::value_type(key, index)).second)
     785             :     {
     786             :         OSL_ASSERT(false);
     787             :     }
     788           0 :     return index;
     789             : }
     790             : 
     791           0 : sal_uInt16 ClassFile::addInterfaceMethodrefInfo(
     792             :     OString const & type, OString const & name,
     793             :     OString const & descriptor)
     794             : {
     795           0 :     sal_uInt16 classIndex = addClassInfo(type);
     796           0 :     sal_uInt16 nameAndTypeIndex = addNameAndTypeInfo(name, descriptor);
     797           0 :     sal_uInt32 key = (static_cast< sal_uInt32 >(classIndex) << 16)
     798           0 :         | nameAndTypeIndex;
     799             :     std::map< sal_uInt32, sal_uInt16 >::iterator i(
     800           0 :         m_interfaceMethodrefInfos.find(key));
     801           0 :     if (i != m_interfaceMethodrefInfos.end()) {
     802           0 :         return i->second;
     803             :     }
     804           0 :     sal_uInt16 index = nextConstantPoolIndex(1);
     805           0 :     appendU1(m_constantPool, 11);
     806           0 :     appendU2(m_constantPool, classIndex);
     807           0 :     appendU2(m_constantPool, nameAndTypeIndex);
     808           0 :     if (!m_interfaceMethodrefInfos.insert(
     809           0 :             std::map< sal_uInt32, sal_uInt16 >::value_type(key, index)).second)
     810             :     {
     811             :         OSL_ASSERT(false);
     812             :     }
     813           0 :     return index;
     814             : }
     815             : 
     816           0 : sal_uInt16 ClassFile::addNameAndTypeInfo(
     817             :     OString const & name, OString const & descriptor)
     818             : {
     819           0 :     sal_uInt16 nameIndex = addUtf8Info(name);
     820           0 :     sal_uInt16 descriptorIndex = addUtf8Info(descriptor);
     821           0 :     sal_uInt32 key = (static_cast< sal_uInt32 >(nameIndex) << 16)
     822           0 :         | descriptorIndex;
     823             :     std::map< sal_uInt32, sal_uInt16 >::iterator i(
     824           0 :         m_nameAndTypeInfos.find(key));
     825           0 :     if (i != m_nameAndTypeInfos.end()) {
     826           0 :         return i->second;
     827             :     }
     828           0 :     sal_uInt16 index = nextConstantPoolIndex(1);
     829           0 :     appendU1(m_constantPool, 12);
     830           0 :     appendU2(m_constantPool, nameIndex);
     831           0 :     appendU2(m_constantPool, descriptorIndex);
     832           0 :     if (!m_nameAndTypeInfos.insert(
     833           0 :             std::map< sal_uInt32, sal_uInt16 >::value_type(key, index)).second)
     834             :     {
     835             :         OSL_ASSERT(false);
     836             :     }
     837           0 :     return index;
     838             : }
     839             : 
     840           0 : void ClassFile::appendSignatureAttribute(
     841             :     std::vector< unsigned char > & stream, OString const & signature)
     842             : {
     843           0 :     if (!signature.isEmpty()) {
     844           0 :         appendU2(stream, addUtf8Info("Signature"));
     845           0 :         appendU4(stream, 2);
     846           0 :         appendU2(stream, addUtf8Info(signature));
     847             :     }
     848           0 : }
     849             : 
     850             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10