├── .gitignore ├── CMakeLists.txt ├── COPYING ├── README.md ├── include ├── applet.h ├── base.h ├── cardapplet.h ├── classcomponent.h ├── component.h ├── constantpool.h ├── descriptor.h ├── directory.h ├── export.h ├── header.h ├── import.h ├── installer.h ├── javaclass.h ├── javalang.h ├── jni.h ├── library.h ├── mask.h ├── method.h ├── native.h ├── opcode.h ├── profile.h ├── readijc.h ├── referenceLocation.h ├── staticfield.h └── vm.h ├── src ├── CMakeLists.txt └── vm │ ├── CMakeLists.txt │ ├── installer.cpp │ ├── javaclass.cpp │ ├── javalang.cpp │ ├── jni.cpp │ ├── library.cpp │ ├── openjcvm.cpp │ ├── readijc.cpp │ ├── testreaddescriptor.cpp │ ├── testreadijc.cpp │ ├── testreadux.cpp │ └── vm.cpp └── testdata ├── Descriptor.cap └── ShortAdd.ijc /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5) 2 | 3 | project(openjcvm CXX) 4 | 5 | enable_testing() 6 | 7 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 8 | 9 | include_directories(${CMAKE_SOURCE_DIR}/include) 10 | add_subdirectory(src) 11 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Library General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License 307 | along with this program; if not, write to the Free Software 308 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 309 | 310 | 311 | Also add information on how to contact you by electronic and paper mail. 312 | 313 | If the program is interactive, make it output a short notice like this 314 | when it starts in an interactive mode: 315 | 316 | Gnomovision version 69, Copyright (C) year name of author 317 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 318 | This is free software, and you are welcome to redistribute it 319 | under certain conditions; type `show c' for details. 320 | 321 | The hypothetical commands `show w' and `show c' should show the appropriate 322 | parts of the General Public License. Of course, the commands you use may 323 | be called something other than `show w' and `show c'; they could even be 324 | mouse-clicks or menu items--whatever suits your program. 325 | 326 | You should also get your employer (if you work as a programmer) or your 327 | school, if any, to sign a "copyright disclaimer" for the program, if 328 | necessary. Here is a sample; alter the names: 329 | 330 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 331 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 332 | 333 | , 1 April 1989 334 | Ty Coon, President of Vice 335 | 336 | This General Public License does not permit incorporating your program into 337 | proprietary programs. If your program is a subroutine library, you may 338 | consider it more useful to permit linking proprietary applications with the 339 | library. If this is what you want to do, use the GNU Library General 340 | Public License instead of this License. 341 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Open Java Card Virtual Machine 2 | ============================== 3 | 4 | To compile: 5 | 6 | ``` 7 | mkdir build 8 | cd build 9 | cmake .. 10 | make 11 | make test 12 | ``` 13 | 14 | Invoke with: 15 | 16 | ``` 17 | ./bin/openjcvm 18 | ``` 19 | -------------------------------------------------------------------------------- /include/applet.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef _applet_h_ 13 | #define _applet_h_ 14 | 15 | #include "base.h" 16 | 17 | class Applet :public DebugSupportedClass 18 | { 19 | private: 20 | u1 AIDLength; 21 | public: 22 | u1* pAID; 23 | u2 installMethodOffset; 24 | Applet(int aidlength) 25 | { 26 | this->AIDLength = aidlength; 27 | pAID = new u1[this->AIDLength]; 28 | }; 29 | ~Applet() 30 | { 31 | this->AIDLength = 0; 32 | delete pAID; 33 | pAID = NULL; 34 | }; 35 | void displayInfo() 36 | { 37 | COUT<<"\t\tApplet AID: "<<(int)this->AIDLength<<" "; 38 | for(int i=0;iAIDLength;i++) 39 | { 40 | COUT<<(int)this->pAID[i]<<" "; 41 | } 42 | COUT<installMethodOffset<count = appletCount; 56 | this->applets = new Applet*[this->count]; 57 | }; 58 | virtual ~AppletComponent() 59 | { 60 | this->count = 0; 61 | delete this->applets; 62 | this->applets = NULL; 63 | }; 64 | void displayInfo() 65 | { 66 | COUT<<"Begin Applet Component"<count<count;i++) 69 | { 70 | this->applets[i]->displayInfo(); 71 | } 72 | COUT<<"End Applet Component"<. 10 | **/ 11 | 12 | #ifndef base_h_ 13 | #define base_h_ 14 | 15 | #include 16 | #include 17 | 18 | typedef unsigned char u1; 19 | typedef unsigned short u2; 20 | typedef unsigned int u4; 21 | 22 | typedef signed char s1; 23 | typedef signed short s2; 24 | typedef signed int s4; 25 | 26 | #define NATIVE_METHOD_START 60000 27 | //const int NATIVE_METHOD_START = 63000; 28 | 29 | class DebugSupportedClass{ 30 | public: 31 | virtual void displayInfo()=0; 32 | }; 33 | 34 | u1 inline readU1(u1* dataBuffer, int* iPos) 35 | { 36 | u1 temp = dataBuffer[*iPos]; 37 | (*iPos)++; 38 | return temp; 39 | }; 40 | 41 | s1 inline readS1(u1* dataBuffer, int* iPos) 42 | { 43 | s1 temp = (s1)(dataBuffer[*iPos]); 44 | (*iPos)++; 45 | return temp; 46 | }; 47 | 48 | u2 inline readU2(u1* dataBuffer, int* iPos) 49 | { 50 | u2 temp = (((u2)dataBuffer[*iPos]&0x00FF) << 8) + (u2)dataBuffer[*iPos+1]; 51 | (*iPos)+=2; 52 | return temp; 53 | }; 54 | 55 | s2 inline readS2(u1* dataBuffer, int* iPos) 56 | { 57 | s2 temp = (((s2)dataBuffer[*iPos]) << 8) + (s2)dataBuffer[*iPos+1]; 58 | (*iPos)+=2; 59 | return temp; 60 | }; 61 | 62 | u4 inline readU4(u1* dataBuffer, int* iPos) 63 | { 64 | u4 temp = ((u4)(dataBuffer[*iPos])<<24)+((u4)dataBuffer[*iPos+1]<<16)+((u4)(dataBuffer[*iPos+2])<<8)+(u4)dataBuffer[*iPos+3]; 65 | (*iPos) += 4; 66 | return temp; 67 | }; 68 | 69 | s4 inline readS4(u1* dataBuffer, int* iPos) 70 | { 71 | s4 temp = ((s4)(dataBuffer[*iPos])<<24)+((s4)dataBuffer[*iPos+1]<<16)+((s4)(dataBuffer[*iPos+2])<<8)+(s4)dataBuffer[*iPos+3]; 72 | (*iPos) += 4; 73 | return temp; 74 | }; 75 | 76 | u1 inline readHigh(u1 data) 77 | { 78 | return data & 0xF0; 79 | }; 80 | 81 | u1 inline readLow(u1 data) 82 | { 83 | return data & 0x0F; 84 | }; 85 | 86 | u1 inline readHighShift(u1 data) 87 | { 88 | return (data & 0xF0)>>4; 89 | }; 90 | 91 | u2 inline makeU2(u1 byte1, u1 byte2) 92 | { 93 | return byte1*0x100+byte2; 94 | } 95 | 96 | s4 inline makeInt(s2 short1,s2 short2) 97 | { 98 | return short1*0x10000+short2; 99 | } 100 | 101 | s2 inline getShortHigh(s4 value) 102 | { 103 | return value/0x10000; 104 | } 105 | 106 | s2 inline getShortLow(s4 value) 107 | { 108 | return value % 0x10000; 109 | } 110 | 111 | #define COUT std::cout 112 | #define ENDL std::endl 113 | 114 | #endif /*end base_h_ */ 115 | -------------------------------------------------------------------------------- /include/cardapplet.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef _cardapplet_h_ 13 | #define _cardapplet_h_ 14 | 15 | #include "header.h" 16 | #include "directory.h" 17 | #include "applet.h" 18 | #include "import.h" 19 | #include "constantpool.h" 20 | #include "classcomponent.h" 21 | #include "method.h" 22 | #include "referenceLocation.h" 23 | #include "staticfield.h" 24 | #include "descriptor.h" 25 | #include "export.h" 26 | 27 | class AbstractApplet :public DebugSupportedClass 28 | { 29 | public: 30 | HeaderComponent* pHeader; 31 | DirectoryComponent* pDir; 32 | ImportComponent* pImport; 33 | ClassComponent* pClass; 34 | StaticFieldComponent* pStaticField; 35 | MethodComponent* pMethod; 36 | ReferenceLocationComponent* pRefLoc; 37 | ConstantPoolComponent* pConstPool; 38 | DescriptorComponent* pDescriptor; 39 | ExportComponent* pExport; 40 | 41 | AbstractApplet(){ 42 | pHeader = NULL; 43 | pDir = NULL; 44 | pImport = NULL; 45 | pMethod = NULL; 46 | pStaticField = NULL; 47 | pRefLoc = NULL; 48 | pConstPool = NULL; 49 | pClass = NULL; 50 | pDescriptor = NULL; 51 | pExport = NULL; 52 | } 53 | 54 | virtual ~AbstractApplet(){ 55 | delete pHeader; 56 | delete pDir; 57 | delete pImport; 58 | delete pStaticField; 59 | delete pRefLoc; 60 | delete pMethod; 61 | delete pConstPool; 62 | delete pClass; 63 | delete pDescriptor; 64 | delete pExport; 65 | 66 | pHeader = NULL; 67 | pDir = NULL; 68 | pImport = NULL; 69 | pMethod = NULL; 70 | pStaticField = NULL; 71 | pRefLoc = NULL; 72 | pConstPool = NULL; 73 | pClass = NULL; 74 | pDescriptor = NULL; 75 | pExport = NULL; 76 | } 77 | 78 | virtual void displayInfo() 79 | { 80 | pHeader->displayInfo(); 81 | 82 | if(pDir != NULL) 83 | { 84 | pDir->displayInfo(); 85 | } 86 | 87 | if(pClass != NULL) 88 | { 89 | pClass->displayInfo(); 90 | } 91 | if(pImport != NULL) 92 | { 93 | pImport->displayInfo(); 94 | } 95 | if(pRefLoc != NULL) 96 | { 97 | pRefLoc->displayInfo(); 98 | } 99 | if(pConstPool != NULL) 100 | { 101 | pConstPool->displayInfo(); 102 | } 103 | if(pStaticField !=NULL) 104 | { 105 | pStaticField->displayInfo(); 106 | } 107 | if(pMethod != NULL) 108 | { 109 | pMethod->displayInfo(); 110 | } 111 | if(pExport!=NULL) 112 | { 113 | pExport->displayInfo(); 114 | } 115 | } 116 | }; 117 | 118 | class CardLibrary : public AbstractApplet 119 | { 120 | public: 121 | bool isThisLibrary(PackageInfo* pPI) 122 | { 123 | return (*(this->pHeader->pThisPackage) == *pPI); 124 | } 125 | 126 | void displayInfo() 127 | { 128 | COUT<pHeader = this->pHeader; 156 | pCL->pDir = this->pDir; 157 | pCL->pClass = this->pClass; 158 | pCL->pConstPool= this->pConstPool; 159 | pCL->pDescriptor= this->pDescriptor; 160 | pCL->pImport= this->pImport; 161 | pCL->pMethod= this->pMethod; 162 | pCL->pRefLoc= this->pRefLoc; 163 | pCL->pStaticField= this->pStaticField; 164 | pCL->pExport = this->pExport; 165 | 166 | pHeader = NULL; 167 | pDir = NULL; 168 | pImport = NULL; 169 | pMethod = NULL; 170 | pStaticField = NULL; 171 | pRefLoc = NULL; 172 | pConstPool = NULL; 173 | pClass = NULL; 174 | pDescriptor = NULL; 175 | pExport = NULL; 176 | 177 | return pCL; 178 | } 179 | 180 | void install() 181 | { 182 | COUT<<"Start installing..."<pApplet == NULL) 184 | { 185 | COUT<<"Not an applet!"<pApplet->applets[0]->installMethodOffset<pMethod->executeByteCode((int)this->pApplet->applets[0]->installMethodOffset,this); 193 | } 194 | COUT<<"Install finished!"<displayInfo(); 206 | COUT<<"End applet!"<. 10 | **/ 11 | 12 | #ifndef _classcomponent_h_ 13 | #define _classcomponent_h_ 14 | 15 | #include "component.h" 16 | 17 | class AbstractClassInfo : public DebugSupportedClass{ 18 | public: 19 | bool isShareable; 20 | int iinterfaceCount; 21 | 22 | virtual bool isInterface() = 0; 23 | 24 | AbstractClassInfo(u1 bitfield) 25 | { 26 | isShareable = ((bitfield & 0x40) == 0x40); 27 | iinterfaceCount = (int)(bitfield & 0x0F); 28 | }; 29 | AbstractClassInfo() 30 | { 31 | }; 32 | }; 33 | 34 | class InterfaceInfo :public AbstractClassInfo{ 35 | public: 36 | u2* superinterfaces; 37 | InterfaceInfo(u1 bitfield):AbstractClassInfo(bitfield) 38 | { 39 | superinterfaces = new u2[this->iinterfaceCount]; 40 | }; 41 | virtual ~InterfaceInfo() 42 | { 43 | delete superinterfaces; 44 | superinterfaces = NULL; 45 | } 46 | 47 | virtual bool isInterface() 48 | { 49 | return true; 50 | }; 51 | 52 | virtual void displayInfo() 53 | { 54 | for(int i=0;iiinterfaceCount;i++) 55 | { 56 | COUT<<"\t\tSuper interface ["<superClassRef.classref<declaredInstanceSize = 0; 91 | this->firstReferenceToken = 0; 92 | this->referenceCount = 0; 93 | this->publicMethodTableBase = 0; 94 | this->publicMethodTableCount = 0; 95 | this->packageMethodTableBase = 0; 96 | this->packageMethodTableCount = 0; 97 | }; 98 | 99 | virtual ~ClassInfo() 100 | { 101 | delete publicVirtualMethodTable; 102 | delete packageVirtualMethodTable; 103 | publicVirtualMethodTable = NULL; 104 | packageVirtualMethodTable = NULL; 105 | }; 106 | 107 | virtual bool isInterface() 108 | { 109 | return false; 110 | }; 111 | 112 | void setPublicMethodTable(u1 tableBase, u1 tableCount) 113 | { 114 | this->publicMethodTableBase = tableBase; 115 | this->publicMethodTableCount = tableCount; 116 | this->publicVirtualMethodTable = new u2[tableCount]; 117 | }; 118 | 119 | void setPackageMethodTable(u1 tableBase, u1 tableCount) 120 | { 121 | this->packageMethodTableBase = tableBase; 122 | this->packageMethodTableCount = tableCount; 123 | this->packageVirtualMethodTable = new u2[tableCount]; 124 | }; 125 | 126 | virtual void displayInfo() 127 | { 128 | int i = 0; 129 | COUT<<"\tInterface count: "<<(int)this->iinterfaceCount<superClassRef.classref<declaredInstanceSize<firstReferenceToken<referenceCount<publicMethodTableBase<publicMethodTableCount<packageMethodTableBase<packageMethodTableCount<publicVirtualMethodTable[i]<packageVirtualMethodTable[i]<iIntCount = iInterfaceCount; 168 | this->iClsCount = iClassCount; 169 | pClasses = new AbstractClassInfo*[this->iIntCount+this->iClsCount]; 170 | }; 171 | 172 | static bool isInterface(u1 bitfield) 173 | { 174 | return (bitfield & 0x80) == 0x80; 175 | }; 176 | static bool isShareable(u1 bitfield) 177 | { 178 | return (bitfield & 0x40) == 0x40; 179 | }; 180 | virtual void displayInfo(void){ 181 | COUT<<"Begin Class Component"<displayInfo(); 185 | } 186 | COUT<<"End Class Component"<. 10 | **/ 11 | 12 | #ifndef _component_h_ 13 | #define _component_h_ 14 | 15 | #include "base.h" 16 | 17 | const unsigned char TAG_HEADER_COMP = 0x01; 18 | const unsigned char TAG_DIR_COMP = 0x02; 19 | const unsigned char TAG_APPLET_COMP = 0x03; 20 | const unsigned char TAG_IMPORT_COMP = 0x04; 21 | const unsigned char TAG_CONSTANTPOOL_COMP = 0x05; 22 | const unsigned char TAG_CLASS_COMP = 0x06; 23 | const unsigned char TAG_METHOD_COMP = 0x07; 24 | const unsigned char TAG_STATICFIELD_COMP = 0x08; 25 | const unsigned char TAG_REFERENCELOCATION_COMP = 0x09; 26 | const unsigned char TAG_EXPORT_COMP = 0x0A; 27 | const unsigned char TAG_DESCRIPTOR_COMP = 0x0B; 28 | const unsigned char TAG_DEBUG_COMP = 0x0C; 29 | 30 | const u1 ACC_PUBLIC = 0x01; 31 | const u1 ACC_PRIVATE = 0x02; 32 | const u1 ACC_PROTECTED = 0x04; 33 | const u1 ACC_STATIC = 0x08; 34 | const u1 ACC_FINAL = 0x10; 35 | 36 | const u1 ACC_ABSTRACT = 0x40; 37 | const u1 ACC_INIT = 0x80; 38 | 39 | const u2 TYPE_VOID = 0x0001; 40 | const u2 TYPE_BOOLEAN = 0x0002; 41 | const u2 TYPE_BYTE = 0x0003; 42 | const u2 TYPE_SHORT = 0x0004; 43 | const u2 TYPE_INT = 0x0005; 44 | const u2 TYPE_REFERRENCE = 0x0006; 45 | const u2 TYPE_ARRAY_OF_BOOLEAN = 0x000A; 46 | const u2 TYPE_ARRAY_OF_BYTE = 0x000B; 47 | const u2 TYPE_ARRAY_OF_SHROT = 0x000C; 48 | const u2 TYPE_ARRAY_OF_INT = 0x000D; 49 | const u2 TYPE_ARRAY_OF_REFERENCE = 0x000E; 50 | 51 | class PackageInfo : public DebugSupportedClass{ 52 | public: 53 | u1 minorVersion; 54 | u1 majorVersion; 55 | u1 AIDLength; 56 | u1* AID; 57 | 58 | PackageInfo(u1* AID, u1 AIDLength, u1 majorVersion, u1 minorVersion){ 59 | this->AID = AID; 60 | this->AIDLength = AIDLength; 61 | this->majorVersion = majorVersion; 62 | this->minorVersion = minorVersion; 63 | } 64 | 65 | virtual ~PackageInfo(){ 66 | delete this->AID; 67 | this->AID = NULL; 68 | } 69 | 70 | bool operator == (PackageInfo& p2) 71 | { 72 | if(this->majorVersion != p2.majorVersion) return false; 73 | if(this->minorVersion != p2.minorVersion) return false; 74 | if(this->AIDLength != p2.AIDLength) return false; 75 | for(int i=0;iAIDLength;i++) 76 | { 77 | if(this->AID[i]!=p2.AID[i]) return false; 78 | } 79 | return true; 80 | } 81 | 82 | void displayInfo() 83 | { 84 | std::cout<<"Package AID: "; 85 | for(int i=0;iAIDLength;i++) 86 | { 87 | std::cout<AID[i]<<" "; 88 | } 89 | std::cout<majorVersion; 93 | std::cout<<"."; 94 | std::cout<minorVersion; 95 | 96 | std::cout<classref = classref; 107 | } 108 | ClassRef() 109 | { 110 | this->classref = 0; 111 | }; 112 | bool operator ==(ClassRef& cf) 113 | { 114 | return(this->classref == cf.classref); 115 | }; 116 | }; 117 | 118 | class AccessFlag : public DebugSupportedClass 119 | { 120 | private: 121 | u1 value; 122 | public: 123 | AccessFlag(u1 val) 124 | { 125 | this->value = val; 126 | }; 127 | 128 | bool isPublic() 129 | { 130 | return (this->value & ACC_PUBLIC) == ACC_PUBLIC; 131 | }; 132 | 133 | bool isPrivate() 134 | { 135 | return (this->value & ACC_PRIVATE) == ACC_PRIVATE; 136 | }; 137 | 138 | bool isProtected() 139 | { 140 | return (this->value & ACC_PROTECTED) == ACC_PROTECTED; 141 | }; 142 | 143 | bool isStatic() 144 | { 145 | return (this->value & ACC_STATIC) == ACC_STATIC; 146 | }; 147 | 148 | bool isFinal() 149 | { 150 | return (this->value & ACC_FINAL) == ACC_FINAL; 151 | }; 152 | 153 | bool isAbstract() 154 | { 155 | return (this->value & ACC_ABSTRACT) == ACC_ABSTRACT; 156 | }; 157 | 158 | bool isInit() 159 | { 160 | return (this->value & ACC_INIT) == ACC_INIT; 161 | }; 162 | 163 | virtual ~AccessFlag() 164 | { 165 | }; 166 | 167 | void displayInfo() 168 | { 169 | if(this->isPublic()) 170 | { 171 | COUT<<"Public "; 172 | }; 173 | if(this->isAbstract()) 174 | { 175 | COUT<<"Abstract "; 176 | }; 177 | if(this->isFinal()) 178 | { 179 | COUT<<"Final "; 180 | }; 181 | if(this->isInit()) 182 | { 183 | COUT<<"Init "; 184 | }; 185 | if(this->isPrivate()) 186 | { 187 | COUT<<"Private "; 188 | }; 189 | if(this->isProtected()) 190 | { 191 | COUT<<"Protected "; 192 | }; 193 | if(this->isStatic()) 194 | { 195 | COUT<<"Static "; 196 | }; 197 | COUT<. 10 | **/ 11 | 12 | #ifndef constant_pool_h_ 13 | #define constant_pool_h_ 14 | 15 | #include "component.h" 16 | 17 | class CpInfo{ 18 | public: 19 | u1 Tag; 20 | u1 info[3]; 21 | virtual void displayCpInfo() = 0; 22 | }; 23 | 24 | class ConstantClassInfo : public CpInfo{ 25 | public: 26 | ConstantClassInfo(u1 tag,u1 byte1, u1 byte2) 27 | { 28 | this->Tag = tag; 29 | this->info[0] = byte1; 30 | this->info[1] = byte2; 31 | } 32 | void displayCpInfo() 33 | { 34 | COUT<<"Class ref is "<<(int)info[0]<<" "<<(int)info[1]<Tag = tag; 43 | this->info[0] = byte1; 44 | this->info[1] = byte2; 45 | this->info[2] = byte3; 46 | } 47 | void displayCpInfo() 48 | { 49 | COUT<<"Instance field ref is class -> "<<(int)info[0]<<" "<<(int)info[1]<<" /token -> "<<(int)info[2]<Tag = tag; 58 | this->info[0] = byte1; 59 | this->info[1] = byte2; 60 | this->info[2] = byte3; 61 | } 62 | void displayCpInfo() 63 | { 64 | COUT<<"Virtual Method ref is class -> "<<(int)info[0]<<" "<<(int)info[1]<<" /token -> "<<(int)info[2]<Tag = tag; 73 | this->info[0] = byte1; 74 | this->info[1] = byte2; 75 | this->info[2] = byte3; 76 | } 77 | void displayCpInfo() 78 | { 79 | COUT<<"Super method ref is class -> "<<(int)info[0]<<" "<<(int)info[1]<<" /token -> "<<(int)info[2]<Tag = tag; 88 | this->info[0] = byte1; 89 | this->info[1] = byte2; 90 | this->info[2] = byte3; 91 | } 92 | void displayCpInfo() 93 | { 94 | COUT<<"Static Field Reference is package : "<<(int)info[0]<<" /class: "<<(int)info[1]<<" /token: "<<(int)info[2]<Tag = tag; 103 | this->info[0] = byte1; 104 | this->info[1] = byte2; 105 | this->info[2] = byte3; 106 | } 107 | void displayCpInfo() 108 | { 109 | COUT<<"Static Method Reference is package : "<<(int)info[0]<<" /class: "<<(int)info[1]<<" /token: "<<(int)info[2]<displayCpInfo(); 146 | cpCount++; 147 | }; 148 | 149 | ConstantPoolComponent(u2 count) 150 | { 151 | this->pConstantPool = new CpInfo*[count]; 152 | this->cpCount = 0; 153 | }; 154 | virtual ~ConstantPoolComponent() 155 | { 156 | delete pConstantPool; 157 | pConstantPool = NULL; 158 | this->cpCount = 0; 159 | }; 160 | 161 | virtual void displayInfo() 162 | { 163 | 164 | }; 165 | }; 166 | 167 | #endif 168 | -------------------------------------------------------------------------------- /include/descriptor.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef descriptor_h_ 13 | #define descriptor_h_ 14 | 15 | #include "component.h" 16 | 17 | class FieldRef 18 | { 19 | 20 | }; 21 | 22 | class StaticFieldRef : public FieldRef 23 | { 24 | }; 25 | 26 | class InstanceFieldRef : public FieldRef 27 | { 28 | }; 29 | 30 | class Type : public DebugSupportedClass 31 | { 32 | public: 33 | u2 value; 34 | Type() 35 | { 36 | this->value = 0; 37 | }; 38 | 39 | Type(u2 val) 40 | { 41 | this->value = val; 42 | }; 43 | 44 | bool isPrimitiveType() 45 | { 46 | return (value & 0x80) == 0x80; 47 | }; 48 | 49 | bool isReferenceType() 50 | { 51 | return (value & 0x80) == 0x0; 52 | }; 53 | 54 | void displayInfo() 55 | { 56 | }; 57 | }; 58 | 59 | class PrimitiveType : public Type 60 | { 61 | void displayInfo() 62 | { 63 | Type::displayInfo(); 64 | switch(this->value & 80) 65 | { 66 | case TYPE_BOOLEAN: 67 | COUT<<"Type: boolean"<token = token; 104 | pAF = new AccessFlag(accessflag); 105 | if(pAF->isStatic()) 106 | { 107 | pFieldRef = new StaticFieldRef(); 108 | } 109 | else 110 | { 111 | pFieldRef = new InstanceFieldRef(); 112 | } 113 | }; 114 | virtual ~FieldDescriptorInfo() 115 | { 116 | delete pFieldRef; 117 | pFieldRef = NULL; 118 | delete pFieldtype; 119 | pFieldtype = NULL; 120 | }; 121 | void displayInfo() 122 | { 123 | COUT<<"Field Info:"<token<pAF->displayInfo(); 127 | } 128 | }; 129 | 130 | class MethodDescriptorInfo : public DebugSupportedClass 131 | { 132 | public: 133 | u1 token; 134 | AccessFlag* pAF; 135 | u2 methodOffset; 136 | u2 typeOffset; 137 | u2 bytecodeCount; 138 | u2 exceptionHandlerCount; 139 | u2 exceptionHandlerIndex; 140 | MethodDescriptorInfo(u1 token,u1 af,u2 mo,u2 to,u2 bc,u2 ehc,u2 ehi) 141 | { 142 | this->token = token; 143 | pAF = new AccessFlag(af); 144 | this->methodOffset = mo; 145 | this->typeOffset = to; 146 | this->bytecodeCount = bc; 147 | this->exceptionHandlerCount = ehc; 148 | this->exceptionHandlerIndex = ehi; 149 | } 150 | void displayInfo() 151 | { 152 | COUT<<"\tMethod Info:"<token<displayInfo(); 156 | COUT<<"\tMethod offset "<<(int)this->methodOffset<typeOffset<bytecodeCount<exceptionHandlerCount<exceptionHandlerIndex<token = token; 179 | this->accessFlags = accessflags; 180 | this->thisClassRef = pThisRef; 181 | this->interfaceCount = interfacecount; 182 | this->fieldCount = fieldcount; 183 | this->methodCount = methodcount; 184 | interfaces = new ClassRef*[this->interfaceCount]; 185 | fields = new FieldDescriptorInfo*[this->fieldCount]; 186 | methods = new MethodDescriptorInfo*[this->methodCount]; 187 | }; 188 | 189 | virtual ~ClassDescriptorInfo() 190 | { 191 | this->interfaceCount = 0; 192 | this->fieldCount = 0; 193 | this->methodCount = 0; 194 | delete this->interfaces; 195 | delete this->fields; 196 | delete this->methods; 197 | 198 | this->interfaces = NULL; 199 | this->fields = NULL; 200 | this->methods = NULL; 201 | }; 202 | 203 | void displayInfo() 204 | { 205 | int i =0; 206 | COUT<<"Token is " <<(int)this->token<accessFlags<thisClassRef.classref<interfaceCount<fieldCount<methodCount<interfaceCount;i++) 215 | { 216 | COUT<<"\t Interface - "<<(int)this->interfaces[i]->classref<fieldCount;i++) 220 | { 221 | fields[i]->displayInfo(); 222 | } 223 | for(i=0;imethodCount;i++) 224 | { 225 | this->methods[i]->displayInfo(); 226 | } 227 | }; 228 | }; 229 | 230 | class TypeDesc 231 | { 232 | public: 233 | int nibbleCount; 234 | u1* pTypes; 235 | TypeDesc(int nibblecount) 236 | { 237 | this->nibbleCount = nibblecount; 238 | pTypes = new u1[nibbleCount]; 239 | }; 240 | 241 | virtual ~TypeDesc() 242 | { 243 | this->nibbleCount = 0; 244 | delete pTypes; 245 | pTypes = NULL; 246 | }; 247 | }; 248 | 249 | class TypeDescriptorInfo : public DebugSupportedClass 250 | { 251 | public: 252 | int constPoolCount; 253 | int typeDescCount;//Not a standard member 254 | u2* pConstantPoolTypes; 255 | TypeDesc** pTypeDesc; 256 | TypeDescriptorInfo(u2 cpCount) 257 | { 258 | this->constPoolCount = (int) cpCount; 259 | pConstantPoolTypes = new u2[this->constPoolCount]; 260 | pTypeDesc = NULL; 261 | } 262 | void setTypeDesc(TypeDesc** ptd, int ptdlength) 263 | { 264 | this->pTypeDesc = ptd; 265 | this->typeDescCount = ptdlength; 266 | } 267 | 268 | virtual ~TypeDescriptorInfo() 269 | { 270 | this->constPoolCount = 0; 271 | this->typeDescCount = 0; 272 | delete this->pConstantPoolTypes; 273 | this->pConstantPoolTypes = NULL; 274 | delete this->pTypeDesc; 275 | this->pTypeDesc = NULL; 276 | } 277 | 278 | void displayInfo() 279 | { 280 | } 281 | }; 282 | 283 | class DescriptorComponent : public DebugSupportedClass 284 | { 285 | public: 286 | u1 classCount; 287 | ClassDescriptorInfo** classes; 288 | TypeDescriptorInfo* types; 289 | 290 | DescriptorComponent(int count) 291 | { 292 | this->classCount = count; 293 | this->classes = new ClassDescriptorInfo*[classCount]; 294 | }; 295 | 296 | virtual ~DescriptorComponent() 297 | { 298 | this->classCount = 0; 299 | delete this->classes; 300 | this->classes = NULL; 301 | }; 302 | 303 | void displayInfo(){ 304 | COUT<<"The class count is "<<(int)this->classCount<classCount;i++) 306 | { 307 | this->classes[i]->displayInfo(); 308 | } 309 | } 310 | }; 311 | 312 | #endif 313 | -------------------------------------------------------------------------------- /include/directory.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef _directory_h_ 13 | #define _directory_h_ 14 | 15 | #include "base.h" 16 | 17 | class StaticFieldSizeInfo : public DebugSupportedClass{ 18 | public: 19 | u2 imageSize; 20 | u2 arrayInitCount; 21 | u2 arrayInitSize; 22 | StaticFieldSizeInfo(u2 imageSize, u2 arrayInitCount, u2 arrayInitSize){ 23 | this->imageSize = imageSize; 24 | this->arrayInitCount = arrayInitCount; 25 | this->arrayInitSize = arrayInitSize; 26 | } 27 | virtual void displayInfo() 28 | { 29 | std::cout<<"Static Field Size Info:"<imageSize<componentTag = compTag; 49 | this->size = csize; 50 | this->AID = AID; 51 | this->AIDLength = AIDLength; 52 | } 53 | virtual ~CustomComponentInfo(){ 54 | delete AID; 55 | AID = NULL; 56 | this->AIDLength = 0; 57 | } 58 | 59 | virtual void displayInfo() 60 | { 61 | } 62 | }; 63 | 64 | class DirectoryComponent :public DebugSupportedClass{ 65 | public: 66 | u2 componentSizes[11]; 67 | StaticFieldSizeInfo* pStaticFieldSize; 68 | u1 importCount; 69 | u1 appletCount; 70 | u1 customCount; 71 | CustomComponentInfo** pCustomComponents; 72 | void displayInfo() 73 | { 74 | this->pStaticFieldSize->displayInfo(); 75 | std::cout<<"Import count:"<<(int)this->importCount<appletCount<customCount<pStaticFieldSize = sfsi; 86 | this->importCount = importCount; 87 | this->appletCount = appletCount; 88 | this->customCount = customCount; 89 | 90 | if(customCount>0) 91 | { 92 | pCustomComponents=new CustomComponentInfo*[customCount]; 93 | for(u1 i=0;i. 10 | **/ 11 | 12 | #ifndef _export_h_ 13 | #define _export_h_ 14 | 15 | #include "base.h" 16 | 17 | class ClassExportInfo : public DebugSupportedClass 18 | { 19 | public: 20 | u2 classOffset; 21 | u1 staticFieldCount; 22 | u1 staticMethodCount; 23 | u2* pStaticFieldOffsets; 24 | u2* pStaticMethodOffsets; 25 | ClassExportInfo(u2 classOffset, u1 staticFieldCount, u1 staticMethodCount) 26 | { 27 | this->classOffset = classOffset; 28 | this->staticFieldCount = staticFieldCount; 29 | this->staticMethodCount = staticMethodCount; 30 | pStaticFieldOffsets = new u2[staticFieldCount]; 31 | pStaticMethodOffsets = new u2[staticMethodCount]; 32 | } 33 | 34 | void displayInfo() 35 | { 36 | int i = 0; 37 | 38 | COUT<<"\t\tClass Offset = "<<(int)this->classOffset<pStaticFieldOffsets[i]<staticMethodCount;i++) 46 | { 47 | COUT<<"\t\t\tStatic Method Offset: "<pStaticMethodOffsets[i]<classCount = clc; 61 | pClassExport = new ClassExportInfo*[clc]; 62 | }; 63 | 64 | virtual ~ExportComponent() 65 | { 66 | for(int i=0;iclassCount;i++) 67 | { 68 | delete pClassExport[i]; 69 | pClassExport[i] = NULL; 70 | } 71 | delete pClassExport; 72 | pClassExport = NULL; 73 | }; 74 | 75 | void displayInfo() 76 | { 77 | int i=0; 78 | COUT<<"Begin Export Component"<classCount<classCount;i++) 81 | { 82 | this->pClassExport[i]->displayInfo(); 83 | } 84 | COUT<<"End Export Component"<. 10 | **/ 11 | 12 | #ifndef _header_h_ 13 | #define _header_h_ 14 | 15 | #include "base.h" 16 | #include "component.h" 17 | 18 | class HeaderComponent{ 19 | public: 20 | //Size and magic number are not important for the header component. 21 | 22 | u1 minorVersion; 23 | u1 majorVersion; 24 | u1 flags; 25 | PackageInfo* pThisPackage; 26 | HeaderComponent(PackageInfo* pi,u1 javaCardMajorVersion,u1 javaCardMinorVersion, u1 flag) 27 | { 28 | this->pThisPackage = pi; 29 | this->majorVersion = javaCardMajorVersion; 30 | this->minorVersion = javaCardMinorVersion; 31 | this->flags = flag; 32 | }; 33 | virtual ~HeaderComponent() 34 | { 35 | delete pThisPackage; 36 | }; 37 | void displayInfo() 38 | { 39 | this->pThisPackage->displayInfo(); 40 | //COUT<<"Applet version:"<majorVersion<<"."<<(int)this->minorVersion<majorVersion<<"."<<(int)this->minorVersion<flags<displayInfo(); 48 | } 49 | }; 50 | }; 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /include/import.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef _include_import_H_ 13 | #define _include_import_H_ 14 | 15 | #include "component.h" 16 | 17 | class ImportComponent : public DebugSupportedClass 18 | { 19 | private: 20 | u1 count; 21 | public: 22 | PackageInfo** packages; 23 | ImportComponent(u1 packagecount) 24 | { 25 | this->count = packagecount; 26 | packages = new PackageInfo*[this->count]; 27 | }; 28 | virtual ~ImportComponent() 29 | { 30 | this->count = 0; 31 | delete packages; 32 | packages = NULL; 33 | }; 34 | virtual void displayInfo() 35 | { 36 | COUT<<"Begin Import Component"<count;i++) 38 | { 39 | packages[i]->displayInfo(); 40 | } 41 | COUT<<"End Import Component"<. 10 | **/ 11 | 12 | #ifndef _installer_h_ 13 | #define _installer_h_ 14 | 15 | #include "cardapplet.h" 16 | 17 | AbstractApplet* buildApplet(unsigned char* dataBuffer, int dataLength); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /include/javaclass.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef _java_class_h_ 13 | #define _java_class_h_ 14 | 15 | #include "base.h" 16 | #include "component.h" 17 | 18 | class JavaClass : 19 | public DebugSupportedClass 20 | { 21 | public: 22 | ClassRef thisClass; 23 | ClassRef superClass; 24 | int declaredInstanceSize; 25 | u2* pInstance; 26 | JavaClass(ClassRef thisclass,ClassRef superclass,int instancesize); 27 | ~JavaClass(void); 28 | void displayInfo(); 29 | }; 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /include/javalang.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef _include_java_lang_ 13 | #define _include_java_lang_ 14 | 15 | #include "cardapplet.h" 16 | 17 | CardLibrary* buildJavaLang(); 18 | 19 | void java_lang_Object(); 20 | void java_lang_Object__equals(); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /include/jni.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef _include_jni__ 13 | #define _include_jni__ 14 | 15 | #include "cardapplet.h" 16 | 17 | extern CardLibrary* buildJNI(); 18 | extern void JCFDriver__registerApplet(void); 19 | extern void JCFDriver__methodMissing(void); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /include/library.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef _library_h_ 13 | #define _library_h_ 14 | 15 | #include "cardapplet.h" 16 | 17 | #include 18 | 19 | class CardLibList 20 | { 21 | public: 22 | static std::list libList; 23 | static CardLibrary* findLibrary(PackageInfo* pPI); 24 | }; 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /include/mask.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef mask_h_ 13 | #define mask_h_ 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /include/method.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef _method_h_ 13 | #define _method_h_ 14 | #include "base.h" 15 | #include "vm.h" 16 | #include "native.h" 17 | 18 | class ExceptionHandlerInfo{ 19 | public: 20 | u2 startOffset; 21 | u2 activeLength; 22 | u2 handlerOffset; 23 | u2 catchTypeIndex; 24 | }; 25 | 26 | class MethodHeaderInfo{ 27 | public: 28 | u1 flags; 29 | u1 maxStack; 30 | u1 nargs; 31 | u1 maxLocals; 32 | }; 33 | 34 | class MethodInfo{ 35 | public: 36 | MethodHeaderInfo* pMethodHeaderInfo; 37 | u1* bytecodes; 38 | }; 39 | 40 | class MethodComponent :public DebugSupportedClass{ 41 | public: 42 | u1 handlerCount; 43 | ExceptionHandlerInfo** pExceptionHandlers; 44 | u1* pMethodInfo; 45 | static bool isExtended(u1 flag) 46 | { 47 | return (flag & 0x80) == 0x80; 48 | }; 49 | 50 | static bool isAbstract(u1 flag) 51 | { 52 | return (flag & 0x40) == 0x40; 53 | }; 54 | 55 | MethodComponent(u1 handlerCount) 56 | { 57 | this->handlerCount = handlerCount; 58 | pExceptionHandlers = new ExceptionHandlerInfo*[handlerCount]; 59 | for(int i=0;i<(int)handlerCount;i++) 60 | pExceptionHandlers[i]=new ExceptionHandlerInfo(); 61 | }; 62 | 63 | virtual ~MethodComponent(){ 64 | for(int i=0;i<(int)handlerCount;i++) 65 | { 66 | delete pExceptionHandlers[i]; 67 | pExceptionHandlers[i]= NULL; 68 | } 69 | delete pExceptionHandlers; 70 | pExceptionHandlers = NULL; 71 | }; 72 | 73 | void executeByteCode(u2 offset, AbstractApplet* pCA) 74 | { 75 | //Add for native method execution. 76 | if(offset>=NATIVE_METHOD_START) 77 | { 78 | (*natives[offset-NATIVE_METHOD_START])(); 79 | return; 80 | } 81 | 82 | int iPosm2 = offset; 83 | #define READU1 readU1(pMethodInfo,&iPosm2) 84 | #define READU2 readU2(pMethodInfo,&iPosm2) 85 | 86 | u1 flags = READU1; 87 | 88 | COUT<<"Flag = "<<(int)flags<handlerCount<startOffset<activeLength<handlerOffset<catchTypeIndex<. 10 | **/ 11 | 12 | #ifndef _native_h_ 13 | #define _native_h_ 14 | 15 | //This is only used for indicating the next natives ID. 16 | #define NEXT_AVAILABLE_NATIVE_ID 4 17 | 18 | typedef void (*NativeFunction)(void); 19 | 20 | extern NativeFunction natives[100]; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /include/opcode.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef opcode_h_ 13 | #define opcode_h_ 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /include/profile.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef profile_h_ 13 | #define profile_h_ 14 | 15 | #endif 16 | -------------------------------------------------------------------------------- /include/readijc.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef _readijc_h_ 13 | #define _readijc_h_ 14 | 15 | int readBin(char* srcFileName, unsigned char* dataBuffer); 16 | int outputHex(unsigned char* dataBuffer,int iLength); 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /include/referenceLocation.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #ifndef reference_location_h_ 13 | #define reference_location_h_ 14 | 15 | #include "base.h" 16 | class ReferenceLocationComponent : public DebugSupportedClass 17 | { 18 | public: 19 | u2 byteIndexCount; 20 | u1* offsetsToByteIndices; 21 | u2 byte2IndexCount; 22 | u1* offsetsToByte2Indices; 23 | ReferenceLocationComponent(u2 bic) 24 | { 25 | this->byteIndexCount = bic; 26 | offsetsToByteIndices = new u1[(int)bic]; 27 | } 28 | 29 | void setOffsetsToByte2Indices(u2 bic2) 30 | { 31 | this->byte2IndexCount = bic2; 32 | offsetsToByte2Indices = new u1[(int)bic2]; 33 | } 34 | 35 | ReferenceLocationComponent(u2 bic, u2 bic2) 36 | { 37 | this->byteIndexCount = bic; 38 | this->byte2IndexCount = bic2; 39 | offsetsToByteIndices = new u1[(int)bic]; 40 | offsetsToByte2Indices = new u1[(int)bic2]; 41 | }; 42 | virtual ~ReferenceLocationComponent() 43 | { 44 | delete offsetsToByteIndices; 45 | offsetsToByteIndices=NULL; 46 | delete offsetsToByte2Indices; 47 | offsetsToByteIndices=NULL; 48 | } 49 | 50 | virtual void displayInfo() 51 | { 52 | COUT<<"offsets to Byte indices is - "; 53 | for(int i=0;ibyteIndexCount;i++) 54 | { 55 | COUT<offsetsToByteIndices+i))<<" "; 56 | } 57 | COUT<byte2IndexCount;i1++) 60 | { 61 | COUT<offsetsToByte2Indices+i1))<<" "; 62 | } 63 | COUT<. 10 | **/ 11 | 12 | #ifndef _staticfield_h_ 13 | #define _staticfield_h_ 14 | 15 | class ArrayInitInfo : public DebugSupportedClass 16 | { 17 | private: 18 | u1 type; 19 | u2 count; 20 | public: 21 | u1* pValues; 22 | ArrayInitInfo(u1 type, u2 count) 23 | { 24 | this->type = type; 25 | this->count = count; 26 | this->pValues = new u1[count]; 27 | }; 28 | 29 | virtual ~ArrayInitInfo() 30 | { 31 | this->count = 0; 32 | delete this->pValues; 33 | this->pValues = NULL; 34 | }; 35 | 36 | virtual void displayInfo() 37 | { 38 | COUT<<"\t\tArray Init Info: "; 39 | for(int i=0;i<(int)this->count;i++) 40 | { 41 | COUT<<(int)(this->pValues[i])<<" "; 42 | } 43 | COUT<imageSize = imagesize; 61 | this->referenceCount = refercount; 62 | this->arrayInitCount = arrayinitcount; 63 | this->pArrayInit = new ArrayInitInfo*[this->arrayInitCount]; 64 | this->pStaticFieldImage = new u1[imagesize]; 65 | 66 | //Build segment 1 and segment 2 data. 67 | //Segment 1 - arrays of primitive types initialized by methods. 68 | //Segment 2 - reference types initialized to null, including arrays. 69 | for(imageCounter=0;imageCounter<(int)this->referenceCount;imageCounter++) 70 | { 71 | this->pStaticFieldImage[imageCounter] = 0; 72 | } 73 | }; 74 | void beginBuildNonDefaultValues(u2 defaultvaluecount,u2 nondefaultvaluecount) 75 | { 76 | this->defaultValueCount = defaultvaluecount; 77 | this->nonDefaultValueCount = nondefaultvaluecount; 78 | this->pNonDefaultValues = new u1[nondefaultvaluecount]; 79 | int i; 80 | //Update segment 3 81 | //Segment 3 - primitive types initialized to default values. 82 | for(i=0;idefaultValueCount;i++) 83 | { 84 | this->pStaticFieldImage[imageCounter++] = 0; 85 | } 86 | }; 87 | 88 | void EndBuildNonDefaultValues() 89 | { 90 | //Update segment 4 91 | //Segment 4 - primitive types initialized to non-default values. 92 | for(int i=0;inonDefaultValueCount;i++) 93 | { 94 | this->pStaticFieldImage[imageCounter++] = this->pNonDefaultValues[i]; 95 | } 96 | delete this->pNonDefaultValues; 97 | this->pNonDefaultValues = NULL; 98 | }; 99 | 100 | virtual ~StaticFieldComponent() 101 | { 102 | this->imageSize = 0; 103 | this->referenceCount = 0; 104 | this->arrayInitCount = 0; 105 | this->defaultValueCount = 0; 106 | this->nonDefaultValueCount = 0; 107 | delete this->pArrayInit; 108 | this->pArrayInit = NULL; 109 | delete this->pStaticFieldImage; 110 | this->pStaticFieldImage = NULL; 111 | }; 112 | 113 | virtual void displayInfo() 114 | { 115 | COUT<<"Begin Static Field"<imageSize<referenceCount<arrayInitCount<arrayInitCount;i++) 121 | { 122 | this->pArrayInit[i]->displayInfo(); 123 | } 124 | COUT<<"\tDefault value count : "<<(int)this->defaultValueCount<nonDefaultValueCount<nonDefaultValueCount;i++) 128 | //{ 129 | // COUT<<(int)(this->pNonDefaultValues[i])<<" "; 130 | //} 131 | //COUT<imageSize;i++) 136 | { 137 | COUT<<(int)this->pStaticFieldImage[i]<<" "; 138 | } 139 | COUT<. 10 | **/ 11 | 12 | #ifndef vm_h_ 13 | #define vm_h_ 14 | #include "base.h" 15 | #include "javaclass.h" 16 | 17 | class AbstractApplet; 18 | 19 | #define JSTACK_BOTTOM 256 20 | #define MAX_LOCALS 256 21 | #define MAX_ARRAY 256 22 | 23 | #define MAX_CLASSES 256 24 | 25 | static const int T_BOOLEAN = 10; 26 | static const int T_BYTE = 11; 27 | static const int T_SHORT = 12; 28 | static const int T_INT = 13; 29 | 30 | class VirtualMachine 31 | { 32 | public: 33 | u4 run(); 34 | static s2 jstack[JSTACK_BOTTOM]; 35 | static int jstacktop; 36 | static s2 jlocals[MAX_LOCALS]; 37 | 38 | //Use a new structure for int commands. 39 | static s4 jstackint[JSTACK_BOTTOM]; 40 | static int jstacktopint; 41 | static s4 jlocalsint[MAX_LOCALS]; 42 | 43 | static bool push(s2 value); 44 | static s2 pop(); 45 | 46 | static bool pushint(s4 value); 47 | static s4 popint(); 48 | 49 | static u1* getStaticFieldAddress(u2 sIndex,AbstractApplet* pCA); 50 | 51 | static u4 runStatic(u1* pByteCode, int* pPC,AbstractApplet* pCA,int maxstack,int paras,int maxlocals); 52 | VirtualMachine(void); 53 | virtual ~VirtualMachine(void); 54 | private: 55 | static s1* bytearrays[MAX_ARRAY]; 56 | static s2* shortarrays[MAX_ARRAY]; 57 | static s4* intarrays[MAX_ARRAY]; 58 | static u2* objarrays[MAX_ARRAY]; 59 | 60 | static int baCount; 61 | static int saCount; 62 | static int iaCount; 63 | static int aaCount; 64 | 65 | static JavaClass* javaClasses[MAX_CLASSES]; 66 | static int jcCount; 67 | 68 | static void vm_sconst(short s); 69 | static void vm_sstore(u1 index); 70 | static void vm_sload(u1 index); 71 | static void vm_sadd(); 72 | static void vm_ssub(); 73 | static void vm_smul(); 74 | static void vm_sdiv(); 75 | static void vm_sinc(u1 index,u1 bconst); 76 | static void vm_bspush(s1 b); 77 | static void vm_sspush(short s); 78 | static void vm_s2b(); 79 | 80 | static void vm_newarray(int arrayType); 81 | static void vm_anewarray(u2 sIndex, AbstractApplet* pCA); 82 | static void vm_astore(u1 index); 83 | static void vm_aload(u1 index); 84 | static void vm_sastore(); 85 | static void vm_saload(); 86 | static void vm_aconst_null(); 87 | static void vm_aastore(); 88 | 89 | static void vm_dup(); 90 | static void vm_dup2(); 91 | static void vm_dup_x(); 92 | 93 | static void vm_goto(s1 bValue,int* pPC); 94 | static void vm_ifscmpge(s1 bValue,int* pPC); 95 | static void vm_ifscmplt(s1 bValue,int* pPC); 96 | static void vm_ifne(s1 bValue,int* pPC); 97 | static void vm_ifeq(s1 bValue,int* pPC); 98 | 99 | static void vm_getstatic_s(u2 sIndex, AbstractApplet* pCA); 100 | static void vm_putstatic_s(u2 sIndex,AbstractApplet* pCA); 101 | 102 | static void vm_new(u2 index, AbstractApplet* pCA); 103 | static void vm_invokespecial(u2 index,AbstractApplet* pCA); 104 | static void vm_invokevirtual(u2 index,AbstractApplet* pCA); 105 | static void vm_invokestatic(u2 index,AbstractApplet* pCA); 106 | static void vm_putfield_s(u1 index,AbstractApplet* pCA); 107 | static void vm_getfield_s_this(u1 index,AbstractApplet* pCA); 108 | static void vm_getstatic_a(u2 index,AbstractApplet* pCA); 109 | 110 | static void vm_instanceof(u1 aType, u2 sIndex,AbstractApplet* pCA); 111 | 112 | static void vm_iconst(int iValue); 113 | static void vm_istore(u1 index); 114 | static void vm_iload(u1 index); 115 | static void vm_iadd(); 116 | static void vm_isub(); 117 | static void vm_imul(); 118 | static void vm_idiv(); 119 | static void vm_iinc(u1 index,u1 bconst); 120 | static void vm_sipush(short sValue); 121 | static void vm_iipush(int iValue); 122 | static void vm_i2b(); 123 | static void vm_s2i(); 124 | static void vm_i2s(); 125 | 126 | static void createClass(u2 sOffset, AbstractApplet* pCA); 127 | static inline void invokestaticCommon(u2 index,AbstractApplet* pCA); 128 | }; 129 | 130 | #endif 131 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(vm) 2 | -------------------------------------------------------------------------------- /src/vm/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(VM_SOURCES 2 | installer.cpp 3 | javaclass.cpp 4 | javalang.cpp 5 | jni.cpp 6 | library.cpp 7 | openjcvm.cpp 8 | readijc.cpp 9 | vm.cpp 10 | ) 11 | 12 | set(TEST_READUX_SOURCES 13 | testreadux.cpp 14 | installer.cpp 15 | ) 16 | 17 | set(TEST_READIJC_SOURCES 18 | testreadijc.cpp 19 | installer.cpp 20 | readijc.cpp 21 | ) 22 | 23 | set(TEST_READDESCRIPTOR_SOURCES 24 | testreaddescriptor.cpp 25 | installer.cpp 26 | readijc.cpp 27 | ) 28 | 29 | add_executable(openjcvm ${VM_SOURCES}) 30 | add_executable(test_readux ${TEST_READUX_SOURCES}) 31 | add_executable(test_readijc ${TEST_READIJC_SOURCES}) 32 | add_executable(test_readdescriptor ${TEST_READDESCRIPTOR_SOURCES}) 33 | 34 | set(TEST_DATA_DIR ${CMAKE_SOURCE_DIR}/testdata) 35 | 36 | add_test(ReadUx ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_readux) 37 | add_test(ReadIJC_ShortAdd ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_readijc 38 | ${TEST_DATA_DIR}/ShortAdd.ijc) 39 | add_test(ReadDescriptor ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_readdescriptor 40 | ${TEST_DATA_DIR}/Descriptor.cap) 41 | -------------------------------------------------------------------------------- /src/vm/installer.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #include "installer.h" 13 | #include "vm.h" 14 | #include 15 | 16 | AbstractApplet* buildApplet(unsigned char* dataBuffer, int dataLength) 17 | { 18 | CardApplet* newapplet=new CardApplet(); 19 | int iPos = 0; 20 | u1 c1; 21 | 22 | c1=dataBuffer[iPos]; 23 | 24 | // Read Header Component 25 | 26 | if(c1!=TAG_HEADER_COMP){ 27 | std::cerr<<"Not an applet!"<pHeader = new HeaderComponent(pi,appletMajVer,appletMinVer,appletFlags); 47 | 48 | } 49 | 50 | //Read Directory Component 51 | c1=dataBuffer[iPos]; 52 | std::cout<<"Directory Component Tag is :"<<(int)c1<pDir = new DirectoryComponent(pSfsi,importCount,appletCount,customCount); 76 | 77 | for(i=0;i<11;i++) 78 | { 79 | newapplet->pDir->componentSizes[i]=sizes[i]; 80 | } 81 | 82 | //Split the IJC file into component blocks. 83 | //std::cout<<"Next char is"<<(int)readU1(dataBuffer,&iPos)<pAID[iaid] = READU1; 171 | } 172 | _a_applet->installMethodOffset = READU2; 173 | pAC->applets[ia] = _a_applet; 174 | } 175 | 176 | newapplet->pApplet = pAC; 177 | } 178 | else 179 | { 180 | newapplet->pApplet = NULL; 181 | } 182 | #undef READU2 183 | #undef READU1 184 | 185 | //Parse import component 186 | int iPosimp = 0; 187 | #define READU1 readU1(pImportComponent,&iPosimp) 188 | #define READU2 readU2(pImportComponent,&iPosimp) 189 | if (sizes[TAG_IMPORT_COMP-1] !=0) 190 | { 191 | u1 pcount = READU1; 192 | COUT<<"Imported package count = "<<(int)pcount<packages[ii] = pi; 208 | } 209 | 210 | newapplet->pImport = pIC; 211 | } 212 | #undef READU2 213 | #undef READU1 214 | 215 | //Parse class component 216 | int iPosl = 0; 217 | #define READU1 readU1(pClassComponent,&iPosl) 218 | #define READU2 readU2(pClassComponent,&iPosl) 219 | 220 | int classCompLength = sizes[TAG_CLASS_COMP-1]; 221 | COUT<<"byte code length = "<iinterfaceCount;_c_intercount++) 239 | { 240 | ((InterfaceInfo*)pACI)->superinterfaces[_c_intercount]= READU2; 241 | } 242 | iinterfaceCount++; 243 | } 244 | else 245 | { 246 | pACI = new ClassInfo(_c_bitfield); 247 | ((ClassInfo*)pACI)->superClassRef.classref = READU2; 248 | ((ClassInfo*)pACI)->declaredInstanceSize = READU1; 249 | ((ClassInfo*)pACI)->firstReferenceToken = READU1; 250 | ((ClassInfo*)pACI)->referenceCount = READU1; 251 | u1 _c_publicMethodTableBase = READU1; 252 | u1 _c_publicMethodTableCount = READU1; 253 | u1 _c_packageMethodTableBase = READU1; 254 | u1 _c_packageMethodTableCount = READU1; 255 | ((ClassInfo*)pACI)->setPublicMethodTable(_c_publicMethodTableBase,_c_publicMethodTableCount); 256 | ((ClassInfo*)pACI)->setPackageMethodTable(_c_packageMethodTableBase, _c_packageMethodTableCount); 257 | for(i=0;i<(int)_c_publicMethodTableCount;i++) 258 | { 259 | (((ClassInfo*)pACI)->publicVirtualMethodTable)[i] = READU2; 260 | 261 | } 262 | 263 | for(i=0;i<(int)_c_packageMethodTableCount;i++) 264 | { 265 | (((ClassInfo*)pACI)->packageVirtualMethodTable)[i] = READU2; 266 | } 267 | iclassCount++; 268 | } 269 | pACIs[iabsClassCount++] = pACI; 270 | } 271 | 272 | ClassComponent* pCC = new ClassComponent(iinterfaceCount,iclassCount); 273 | pCC->pClasses = pACIs; 274 | 275 | newapplet->pClass = pCC; 276 | 277 | #undef READU2 278 | #undef READU1 279 | 280 | //Parse constant pool component 281 | int iPosc = 0; 282 | #define READU1 readU1(pConstantPoolComponent,&iPosc) 283 | #define READU2 readU2(pConstantPoolComponent,&iPosc) 284 | u2 constcount = READU2; 285 | u1 _c_tag; 286 | u1 _c_value1; 287 | u1 _c_value2; 288 | u1 _c_value3; 289 | COUT<<"Constant Pool Count = "<addConst(_c_tag,_c_value1,_c_value2,_c_value3); 298 | } 299 | 300 | newapplet->pConstPool = pCPC; 301 | #undef READU2 302 | #undef READU1 303 | 304 | //Parse reference location component 305 | int iPosr = 0; 306 | #define READU1 readU1(pReferenceLocationComponent,&iPosr) 307 | #define READU2 readU2(pReferenceLocationComponent,&iPosr) 308 | u2 byteIndexCount = READU2; 309 | COUT<<"Byte Index Count = "<offsetsToByteIndices+ir) = READU1; 314 | } 315 | 316 | u2 byte2IndexCount = READU2; 317 | COUT<<"Byte2 Index Count = "<setOffsetsToByte2Indices(byte2IndexCount); 319 | for(int ir2=0;ir2<(int)byte2IndexCount;ir2++) 320 | { 321 | *(pRlc->offsetsToByte2Indices+ir2) = READU1; 322 | } 323 | 324 | newapplet->pRefLoc = pRlc; 325 | 326 | #undef READU2 327 | #undef READU1 328 | 329 | //Parse static image component 330 | int iPoss = 0; 331 | #define READU1 readU1(pStaticFieldComponent,&iPoss) 332 | #define READU2 readU2(pStaticFieldComponent,&iPoss) 333 | u2 _s_imagesize = READU2; 334 | u2 _s_referencecount = READU2; 335 | u2 _s_arrayInitCount = READU2; 336 | StaticFieldComponent* pSfc = new StaticFieldComponent(_s_imagesize,_s_referencecount,_s_arrayInitCount); 337 | ArrayInitInfo *pAII; 338 | 339 | u1 _s_type; 340 | u2 _s_count; 341 | //u1 _s_value; 342 | 343 | for(int is=0;is<(int)_s_arrayInitCount;is++) 344 | { 345 | _s_type = READU1; 346 | _s_count = READU2; 347 | pAII = new ArrayInitInfo(_s_type,_s_count); 348 | for(int isc=0;isc<(int)_s_count;isc++) 349 | { 350 | pAII->pValues[isc] = READU1; 351 | } 352 | 353 | pSfc->pArrayInit[is] = pAII; 354 | } 355 | 356 | u2 _s_dvc = READU2; 357 | u2 _s_ndvc = READU2; 358 | 359 | pSfc->beginBuildNonDefaultValues(_s_dvc,_s_ndvc); 360 | for(int isvc=0;isvc<(int)_s_ndvc;isvc++) 361 | { 362 | pSfc->pNonDefaultValues[isvc] = READU1; 363 | } 364 | pSfc->EndBuildNonDefaultValues(); 365 | 366 | newapplet->pStaticField = pSfc; 367 | 368 | #undef READU2 369 | #undef READU1 370 | 371 | //Parse method component 372 | int iPosm = 0; 373 | #define READU1 readU1(pMethodComponent,&iPosm) 374 | #define READU2 readU2(pMethodComponent,&iPosm) 375 | 376 | u1 handlerCount = READU1; 377 | MethodComponent* pmc = new MethodComponent(handlerCount); 378 | 379 | for(int im=0;impExceptionHandlers[im]->startOffset=READU2; 382 | pmc->pExceptionHandlers[im]->activeLength= READU2; 383 | pmc->pExceptionHandlers[im]->handlerOffset = READU2; 384 | pmc->pExceptionHandlers[im]->catchTypeIndex = READU2; 385 | } 386 | 387 | pmc->pMethodInfo = pMethodComponent; 388 | 389 | /* Note: For test only 390 | 391 | int bytecodeLength = sizes[TAG_METHOD_COMP-1]; 392 | COUT<<"byte code length = "<pMethod = pmc; 427 | #undef READU2 428 | #undef READU1 429 | 430 | int iPose = 0; 431 | #define READU1 readU1(pExportComponent,&iPose) 432 | #define READU2 readU2(pExportComponent,&iPose) 433 | if (sizes[TAG_EXPORT_COMP-1] !=0) 434 | { 435 | u1 classCount = READU1; 436 | COUT<<"Class Count = "<<(int)classCount<pClassExport[_ie] = new ClassExportInfo(_co,_sfc,_smc); 446 | for(int _isf=0;_isf<_sfc;_isf++) 447 | { 448 | pEC->pClassExport[_ie]->pStaticFieldOffsets[_isf]= READU2; 449 | } 450 | for(int _ism=0;_ism<_smc;_ism++) 451 | { 452 | pEC->pClassExport[_ie]->pStaticMethodOffsets[_ism]=READU2; 453 | } 454 | } 455 | 456 | newapplet->pExport = pEC; 457 | } 458 | 459 | #undef READU2 460 | #undef READU1 461 | 462 | 463 | int iPosd = 0; 464 | #define READU1 readU1(pDescriptorComponent,&iPosd) 465 | #define READU2 readU2(pDescriptorComponent,&iPosd) 466 | if (sizes[TAG_DESCRIPTOR_COMP-1] !=0){ 467 | u2 dclength = sizes[TAG_DESCRIPTOR_COMP-1]; 468 | 469 | int count= (int)READU1; 470 | DescriptorComponent* _pDesComp = new DescriptorComponent(count); 471 | 472 | ClassDescriptorInfo** pcdis = new ClassDescriptorInfo*[count]; 473 | for(int i=0;iinterfaces[j] = new ClassRef(READU2); 487 | }; 488 | 489 | for(j=0;jfields[j] = _pfdi; 499 | }; 500 | 501 | for(j=0;jmethods[j] = _pmdi; 512 | }; 513 | 514 | pcdis[i] = pcdi; 515 | }; 516 | 517 | _pDesComp->classes = pcdis; 518 | 519 | u2 cpcount = READU2; 520 | COUT<<"Constant pool count = "<pConstantPoolTypes[k] = READU2; 525 | }; 526 | 527 | TypeDesc** ptds = new TypeDesc*[255]; 528 | int icPtds=0; 529 | int itdCount=0; 530 | while(iPosdpTypes[ll++] = _high; 542 | COUT<<(int)_ptd->pTypes[ll-1]<<" "; 543 | if(ll == nbcount) 544 | continue; 545 | _ptd->pTypes[ll++] = readLow(_val); 546 | COUT<<(int)_ptd->pTypes[ll-1]<<" "; 547 | } 548 | COUT<setTypeDesc(ptds,itdCount); 552 | _pDesComp->types = ptdi; 553 | 554 | _pDesComp->displayInfo(); 555 | 556 | newapplet->pDescriptor = _pDesComp; 557 | } 558 | //else{ 559 | // COUT<<"No descriptor.cap!"<pApplet == NULL) 564 | { 565 | CardLibrary* pCL = newapplet->cloneLibrary(); 566 | delete newapplet; 567 | return pCL; 568 | } 569 | else 570 | { 571 | return newapplet; 572 | } 573 | } 574 | -------------------------------------------------------------------------------- /src/vm/javaclass.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #include "javaclass.h" 13 | #include "native.h" 14 | 15 | JavaClass::JavaClass(ClassRef thisclass, ClassRef superclass,int instancesize) 16 | { 17 | this->thisClass = thisclass; 18 | this->superClass = superclass; 19 | this->declaredInstanceSize = instancesize; 20 | this->pInstance = new u2[instancesize]; 21 | }; 22 | 23 | JavaClass::~JavaClass(void) 24 | { 25 | delete this->pInstance; 26 | this->pInstance = NULL; 27 | this->declaredInstanceSize = 0; 28 | }; 29 | 30 | void JavaClass::displayInfo() 31 | { 32 | }; 33 | 34 | NativeFunction natives[100]; 35 | 36 | -------------------------------------------------------------------------------- /src/vm/javalang.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #include "javalang.h" 13 | #include "native.h" 14 | 15 | CardLibrary* buildJavaLang() 16 | { 17 | CardLibrary* pCL = new CardLibrary(); 18 | 19 | u1* pAID = new u1[7]; 20 | pAID[0] = 0xA0; 21 | pAID[1]=0x0; 22 | pAID[2]=0x0; 23 | pAID[3]=0x0; 24 | pAID[4]=0x62; 25 | pAID[5]=0x0; 26 | pAID[6]=0x1; 27 | 28 | PackageInfo* pPI = new PackageInfo(pAID,7,1,0); 29 | 30 | HeaderComponent* pHC = new HeaderComponent(pPI,2,1,0); 31 | 32 | pCL->pHeader = pHC; 33 | 34 | //=== 35 | 36 | ExportComponent* pEC = new ExportComponent(1);//12 should be the correct value. 37 | 38 | ClassExportInfo* pCEI_Object = new ClassExportInfo(0,0,1); 39 | pCEI_Object->pStaticMethodOffsets[0] = NATIVE_METHOD_START + 0; 40 | pEC->pClassExport[0] = pCEI_Object; 41 | 42 | pCL->pExport = pEC; 43 | 44 | //=== 45 | 46 | ClassComponent* pCC = new ClassComponent(0,1); 47 | 48 | ClassInfo* pCI_Object = new ClassInfo(0); 49 | pCI_Object->setPublicMethodTable(0,1); 50 | pCI_Object->publicVirtualMethodTable[0] = NATIVE_METHOD_START + 1; //java/lang/Object.equals(Ljava/lang/Object;)Z 51 | 52 | pCC->pClasses[0] = pCI_Object; 53 | 54 | pCL->pClass = pCC; 55 | 56 | //=== 57 | // Native function table 58 | //=== 59 | 60 | natives[0] = java_lang_Object; 61 | natives[1] = java_lang_Object__equals; 62 | 63 | return pCL; 64 | } 65 | 66 | void java_lang_Object(void) 67 | { 68 | u2 obj = VirtualMachine::pop(); 69 | COUT<<"\tCalling java.lang.Object.()"<. 10 | **/ 11 | 12 | #include "jni.h" 13 | #include "native.h" 14 | 15 | CardLibrary* buildJNI() 16 | { 17 | CardLibrary* pCL = new CardLibrary(); 18 | 19 | u1* pAID = new u1[8]; 20 | pAID[0] = 0xA0; 21 | pAID[1]=0xFF; 22 | pAID[2]=0x0; 23 | pAID[3]=0x0; 24 | pAID[4]=0x0; 25 | pAID[5]=0x0; 26 | pAID[6]=0x0; 27 | pAID[7]=0x1; 28 | //AID on jni package is 0xA0:0xFF:0x00:0x00:0x00:0x00:0x00:0x01 29 | 30 | PackageInfo* pPI = new PackageInfo(pAID,8,1,0);//pAID, length ==8 , version 1.0 31 | 32 | HeaderComponent* pHC = new HeaderComponent(pPI,2,1,0);//version 2.1 33 | 34 | pCL->pHeader = pHC; 35 | 36 | //=== 37 | 38 | ExportComponent* pEC = new ExportComponent(1); 39 | 40 | ClassExportInfo* pCEI_JCFDriver = new ClassExportInfo(0,0,2);//2 static method 41 | pCEI_JCFDriver->pStaticMethodOffsets[0] = NATIVE_METHOD_START + 2; 42 | pCEI_JCFDriver->pStaticMethodOffsets[1] = NATIVE_METHOD_START + 3; 43 | pEC->pClassExport[0] = pCEI_JCFDriver; 44 | 45 | pCL->pExport = pEC; 46 | 47 | //=== 48 | // Native function table 49 | //=== 50 | natives[2] = JCFDriver__methodMissing; 51 | natives[3] = JCFDriver__registerApplet; 52 | 53 | return pCL; 54 | } 55 | 56 | void JCFDriver__registerApplet(void) 57 | { 58 | COUT<<"Calling JCFDriver registerApplet method..."<. 10 | **/ 11 | 12 | #include "library.h" 13 | 14 | //Find a library in the library list. 15 | 16 | std::list CardLibList::libList; 17 | 18 | CardLibrary* CardLibList::findLibrary(PackageInfo* pPI){ 19 | std::list::const_iterator libItr2; 20 | 21 | for(libItr2 = libList.begin(); libItr2 !=libList.end(); ++libItr2) 22 | { 23 | if(((CardLibrary*)(*libItr2))->isThisLibrary(pPI)) 24 | return (CardLibrary*)(*libItr2); 25 | } 26 | 27 | return NULL; 28 | } 29 | -------------------------------------------------------------------------------- /src/vm/openjcvm.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * Copyright 2016 Embecosm Limited 4 | * 5 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 6 | * the terms of the GNU General Public License as published by the Free Software 7 | * Foundation version 2. 8 | * 9 | * You should have received a copy of the GNU General Public License along with 10 | * OpenJCVM; see the file COPYING. If not, see . 11 | **/ 12 | 13 | #include 14 | #include 15 | 16 | //Component headers 17 | #include "header.h" 18 | #include "directory.h" 19 | #include "descriptor.h" 20 | 21 | //Functional headers 22 | #include "installer.h" 23 | #include "library.h" 24 | #include "javalang.h" 25 | #include "jni.h" 26 | #include "readijc.h" 27 | 28 | using namespace std; 29 | 30 | 31 | int testRunApplet(char* ijcFileName) 32 | { 33 | unsigned char* dataBuffer=new unsigned char[10000]; 34 | int iDataLength = readBin(ijcFileName,dataBuffer); 35 | 36 | CardApplet* capp = (CardApplet*)buildApplet(dataBuffer,iDataLength); 37 | capp->displayInfo(); 38 | 39 | capp->install(); 40 | capp->process(); 41 | return 0; 42 | } 43 | 44 | CardApplet* buildAppletEngine(char* ijcFileName) 45 | { 46 | unsigned char* dataBuffer=new unsigned char[10000]; 47 | int iDataLength = readBin(ijcFileName,dataBuffer); 48 | 49 | CardApplet* capp = (CardApplet*)buildApplet(dataBuffer,iDataLength); 50 | capp->displayInfo(); 51 | 52 | return capp; 53 | } 54 | 55 | int main(int argc, char* argv[]) 56 | { 57 | int i=1; 58 | unsigned char* dataBuffer=new unsigned char[10000]; 59 | 60 | list::const_iterator libItr; 61 | 62 | if(argc<2){ 63 | std::cout<<"Usage: "<displayInfo(); 80 | } 81 | 82 | testRunApplet(argv[i]); 83 | 84 | cin.get(); 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /src/vm/readijc.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | 18 | int readBin(char* srcFileName, unsigned char* dataBuffer) 19 | { 20 | int iCount = 0; 21 | int iTotal = 0; 22 | int iLine = 0; 23 | 24 | unsigned char cTemp; 25 | 26 | FILE* fStream; 27 | 28 | if((fStream = fopen(srcFileName, "rb"))==NULL) 29 | { 30 | cerr<<"Cannot open file:" <. 11 | **/ 12 | 13 | #include 14 | 15 | #include "installer.h" 16 | #include "readijc.h" 17 | 18 | using namespace std; 19 | 20 | int testReadDescriptorComp(char* capFileName); 21 | 22 | int main (int argc, char **argv) 23 | { 24 | if (argc < 2) 25 | { 26 | cout << "Usage: test_readdescriptor " << endl; 27 | exit(1); 28 | } 29 | return testReadDescriptorComp(argv[1]); 30 | } 31 | 32 | int testReadDescriptorComp(char* capFileName) 33 | { 34 | unsigned char* dataBuffer=new unsigned char[10000]; 35 | int iDataLength = readBin(capFileName,dataBuffer); 36 | unsigned char* pDescriptorComponent = dataBuffer; 37 | 38 | int iPosd = 0; 39 | 40 | #define READU1 readU1(pDescriptorComponent,&iPosd) 41 | #define READU2 readU2(pDescriptorComponent,&iPosd) 42 | //if (sizes[TAG_DESCRIPTOR_COMP-1] !=0){ 43 | u1 tag = READU1; 44 | cout << "The tag for descriptor component is " << (int)tag << endl; 45 | 46 | if ((int)tag != 11) 47 | { 48 | cout << "Expected tag to be 11." << endl; 49 | return 1; 50 | } 51 | 52 | u2 dclength = READU2; 53 | cout << "The length for the descriptor component is " <<(int)dclength << endl; 54 | 55 | if ((int)dclength != 61) 56 | { 57 | cout << "Expected length to be 61" << endl; 58 | return 1; 59 | } 60 | 61 | int count = (int)READU1; 62 | DescriptorComponent* _pDesComp = new DescriptorComponent(count); 63 | 64 | ClassDescriptorInfo** pcdis = new ClassDescriptorInfo*[count]; 65 | for(int i=0;iinterfaces[j] = new ClassRef(READU2); 79 | }; 80 | 81 | for(j=0;jfields[j] = _pfdi; 91 | }; 92 | 93 | for(j=0;jmethods[j] = _pmdi; 104 | }; 105 | 106 | pcdis[i] = pcdi; 107 | }; 108 | 109 | _pDesComp->classes = pcdis; 110 | 111 | u2 cpcount = READU2; 112 | cout << "Constant pool count = " << cpcount << endl; 113 | 114 | if ((int)cpcount != 2) 115 | { 116 | cout << "Expected constant pool count to be 2." << endl; 117 | return 1; 118 | } 119 | 120 | TypeDescriptorInfo* ptdi = new TypeDescriptorInfo(cpcount); 121 | for(u2 k=0;kpConstantPoolTypes[k] = READU2; 124 | }; 125 | 126 | TypeDesc** ptds = new TypeDesc*[255]; 127 | int icPtds=0; 128 | int itdCount=0; 129 | while(iPosdpTypes[ll++] = _high; 141 | cout << (int)_ptd->pTypes[ll-1] << " "; 142 | if(ll == nbcount) 143 | continue; 144 | _ptd->pTypes[ll++] = readLow(_val); 145 | cout << (int)_ptd->pTypes[ll-1] << " "; 146 | } 147 | cout << endl; 148 | itdCount++; 149 | } 150 | ptdi->setTypeDesc(ptds,itdCount); 151 | _pDesComp->types = ptdi; 152 | 153 | _pDesComp->displayInfo(); 154 | 155 | cout << "Descriptor OK!" << endl; 156 | //} 157 | 158 | return 0; 159 | } 160 | 161 | -------------------------------------------------------------------------------- /src/vm/testreadijc.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * Copyright 2016 Embecosm Limited 4 | * 5 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 6 | * the terms of the GNU General Public License as published by the Free Software 7 | * Foundation version 2. 8 | * 9 | * You should have received a copy of the GNU General Public License along with 10 | * OpenJCVM; see the file COPYING. If not, see . 11 | **/ 12 | 13 | #include 14 | 15 | #include "cardapplet.h" 16 | #include "installer.h" 17 | #include "readijc.h" 18 | 19 | using namespace std; 20 | 21 | int 22 | testReadIJC(char* ijcFileName) 23 | { 24 | unsigned char *dataBuffer = new unsigned char[10000]; 25 | int len = readBin(ijcFileName, dataBuffer); 26 | 27 | if (len == -1) 28 | { 29 | cout << "Error in readBin()" << endl; 30 | return 1; 31 | } 32 | //outputHex(dataBuffer,iDataLength); 33 | CardApplet* capp = (CardApplet*) buildApplet(dataBuffer, len); 34 | if (capp->pHeader == NULL) 35 | { 36 | cout << "Error in buildApplet()" << endl; 37 | return 1; 38 | } 39 | 40 | // Could check some of this info later on 41 | //capp->displayInfo(); 42 | 43 | return 0; 44 | } 45 | 46 | int 47 | main(int argc, char **argv) 48 | { 49 | if (argc < 2) 50 | { 51 | cout << "Usage: test_readijc " << endl; 52 | exit(2); 53 | } 54 | 55 | return testReadIJC(argv[1]); 56 | } 57 | -------------------------------------------------------------------------------- /src/vm/testreadux.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * Copyright 2016 Embecosm Limited 4 | * 5 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 6 | * the terms of the GNU General Public License as published by the Free Software 7 | * Foundation version 2. 8 | * 9 | * You should have received a copy of the GNU General Public License along with 10 | * OpenJCVM; see the file COPYING. If not, see . 11 | **/ 12 | 13 | #include 14 | #include 15 | #include "base.h" 16 | 17 | bool testReadUx(void); 18 | bool testReadU1(void); 19 | bool testReadU2(void); 20 | bool testReadU4(void); 21 | 22 | bool verbose = false; 23 | 24 | int 25 | main(int argc, char **argv) 26 | { 27 | if (argc > 1 && (strcmp(argv[1], "-v") == 0)) 28 | verbose = true; 29 | 30 | if (testReadUx()) 31 | return 0; 32 | else 33 | return 1; 34 | } 35 | 36 | bool 37 | testReadUx() 38 | { 39 | bool res1 = testReadU1(); 40 | bool res2 = testReadU2(); 41 | bool res4 = testReadU4(); 42 | 43 | return res1 && res2 && res4; 44 | } 45 | 46 | template 47 | bool check(T expected, T actual) 48 | { 49 | bool good = (expected == actual); 50 | if (!good || verbose) 51 | std::cout << (good ? "OK" : "NOK") << std::endl; 52 | return good; 53 | } 54 | 55 | bool 56 | testReadU1() 57 | { 58 | u1 dataBuffer[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 59 | u1 expectedBuffer[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 60 | 61 | int pos = 0; 62 | bool good = true; 63 | for (size_t i = 0; i < 10; i++) 64 | { 65 | bool current = check(readU1(dataBuffer, &pos), expectedBuffer[i]); 66 | good = good && current; 67 | } 68 | 69 | return good; 70 | } 71 | 72 | bool 73 | testReadU2() 74 | { 75 | u1 dataBuffer[10] = { 1, 2, 1, 4, 1, 6, 1, 8, 1, 10 }; 76 | u2 expectedBuffer[5] = { 0x102, 0x104, 0x106, 0x108, 0x10A }; 77 | 78 | int pos = 0; 79 | bool good = true; 80 | 81 | for (size_t i = 0; i < 5; i++) 82 | { 83 | bool current = check(readU2(dataBuffer, &pos), expectedBuffer[i]); 84 | good = good && current; 85 | } 86 | 87 | return good; 88 | }; 89 | 90 | bool 91 | testReadU4() 92 | { 93 | u1 dataBuffer[12] = { 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1 }; 94 | u4 expectedBuffer[3] = { 1, 0x101, 0x10101 }; 95 | 96 | int pos = 0; 97 | bool good; 98 | 99 | for (size_t i = 0; i < 3; i++) 100 | { 101 | bool current = check(readU4(dataBuffer, &pos), expectedBuffer[i]); 102 | good = good && current; 103 | } 104 | 105 | return good; 106 | }; 107 | -------------------------------------------------------------------------------- /src/vm/vm.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright 2008 - present Oliver Lusing / Louis Liu 3 | * 4 | * This file is part of OpenJCVM; you can redistribute it and/or modify it under 5 | * the terms of the GNU General Public License as published by the Free Software 6 | * Foundation version 2. 7 | * 8 | * You should have received a copy of the GNU General Public License along with 9 | * OpenJCVM; see the file COPYING. If not, see . 10 | **/ 11 | 12 | #include "vm.h" 13 | 14 | #include "cardapplet.h" 15 | #include "library.h" 16 | 17 | VirtualMachine::VirtualMachine(void) 18 | { 19 | } 20 | 21 | VirtualMachine::~VirtualMachine(void) 22 | { 23 | } 24 | 25 | //int VirtualMachine::pc = 0; 26 | 27 | u4 VirtualMachine::run() 28 | { 29 | return (u4)0; 30 | } 31 | 32 | s2 VirtualMachine::jstack[JSTACK_BOTTOM]; 33 | int VirtualMachine::jstacktop = -1; 34 | s2 VirtualMachine::jlocals[MAX_LOCALS]; 35 | 36 | s4 VirtualMachine::jstackint[JSTACK_BOTTOM]; 37 | int VirtualMachine::jstacktopint; 38 | s4 VirtualMachine::jlocalsint[MAX_LOCALS]; 39 | 40 | s1* VirtualMachine::bytearrays[MAX_ARRAY]; 41 | s2* VirtualMachine::shortarrays[MAX_ARRAY]; 42 | s4* VirtualMachine::intarrays[MAX_ARRAY]; 43 | u2* VirtualMachine::objarrays[MAX_ARRAY]; 44 | 45 | int VirtualMachine::baCount=-1; 46 | int VirtualMachine::saCount=-1; 47 | int VirtualMachine::iaCount=-1; 48 | int VirtualMachine::aaCount=-1; 49 | 50 | JavaClass* VirtualMachine::javaClasses[MAX_CLASSES]; 51 | 52 | int VirtualMachine::jcCount = -1; 53 | 54 | bool VirtualMachine::push(s2 value) 55 | { 56 | if(jstacktop+1 == JSTACK_BOTTOM) 57 | { 58 | return false; 59 | } 60 | else 61 | { 62 | jstack[++jstacktop]=value; 63 | return true; 64 | } 65 | } 66 | 67 | s2 VirtualMachine::pop() 68 | { 69 | if(jstacktop == -1) 70 | { 71 | return 0; 72 | } 73 | else 74 | { 75 | return jstack[jstacktop--]; 76 | } 77 | } 78 | 79 | bool VirtualMachine::pushint(s4 value) 80 | { 81 | if(jstacktopint+1 == JSTACK_BOTTOM) 82 | { 83 | return false; 84 | } 85 | else 86 | { 87 | jstackint[++jstacktopint]=value; 88 | return true; 89 | } 90 | } 91 | 92 | s4 VirtualMachine::popint() 93 | { 94 | if(jstacktopint == -1) 95 | { 96 | return 0; 97 | } 98 | else 99 | { 100 | return jstackint[jstacktopint--]; 101 | } 102 | } 103 | 104 | 105 | u4 VirtualMachine::runStatic(u1* pByteCode, int* pPC, AbstractApplet* pCA,int maxstack,int paras,int maxlocals) 106 | { 107 | #define READU1 (int)readU1(pByteCode,pPC) 108 | #define READU2 (int)readU2(pByteCode,pPC) 109 | #define READU4 (int)readU4(pByteCode,pPC) 110 | #define READS1 readS1(pByteCode,pPC) 111 | #define READS2 (int)readS2(pByteCode,pPC) 112 | #define READS4 (int)readS4(pByteCode,pPC) 113 | 114 | #define MAKEINT() makeInt(jlocals[index],jlocals[index+1]) 115 | u1 bytecode = 0xFF; 116 | u1 bIndex = 0;//Used for sstore,... 117 | s1 bConst = 0;//Used for sinc,... 118 | s1 bValue = 0;//Used for bspush,... 119 | s2 sValue = 0;//Used for sspush,... 120 | u2 sIndex = 0;//Used for getstatic_ 121 | s4 iValue = 0;//Used for iipush,... 122 | int nPairs = 0; 123 | int lowValue = 0; 124 | int highValue = 0; 125 | int i = 0; 126 | 127 | for(i=paras;i>0;i--) 128 | { 129 | jlocals[i-1] = pop(); 130 | } 131 | 132 | for(;;) 133 | { 134 | bytecode= readU1(pByteCode, pPC); 135 | //pc=*pPC; 136 | switch(bytecode) 137 | { 138 | case 0x0: 139 | COUT<<"nop"<=value2) 1194 | { 1195 | COUT<<"\t"<<(int)value1<<" >= "<<(int)value2<<" go! "<= "<<(int)value2<<" not go "<pConstPool->pConstantPool[sIndex]; 1297 | //COUT<<"\tThe tag of the static field is "<<(int)pCI->Tag<info[0]; 1299 | u2 sOffset = makeU2(pCI->info[1],pCI->info[2]); 1300 | 1301 | if(value1 == 0) 1302 | { 1303 | //COUT<<"\tIt is an internal static field."<pStaticField->pStaticFieldImage+sOffset; 1306 | } 1307 | else 1308 | { 1309 | COUT<<"\tIt is defined in package "<<(int)value1<pConstPool->pConstantPool[index]; 1317 | 1318 | s1 value1 = pCI->info[0]; 1319 | 1320 | if(value1 == 0) 1321 | { 1322 | u2 sOffset = makeU2(pCI->info[0],pCI->info[1]); 1323 | COUT<<"\tIt is an internal class."<pImport->packages[packageIndex]; 1333 | 1334 | CardLibrary *pCL = CardLibList::findLibrary(pPI); 1335 | 1336 | if(pCL != NULL) 1337 | { 1338 | COUT<<"\tExternal library found!"<info[1]; 1340 | 1341 | u2 coffset = pCL->pExport->pClassExport[classtoken]->classOffset; 1342 | 1343 | createClass(coffset,pCL); 1344 | 1345 | } 1346 | else 1347 | { 1348 | COUT<<"\tIt is defined in package "<<(int)packageIndex<<" the token is "<<(int)pCI->info[1]<pConstPool->pConstantPool[index]; 1367 | 1368 | //ClassRef thisclass(makeU2(pCI->info[0],pCI->info[1])); 1369 | 1370 | u1 token = pCI->info[2]; 1371 | 1372 | s2 value = pop(); 1373 | 1374 | u2 thisclassobject = (u2)pop(); 1375 | 1376 | javaClasses[(thisclassobject-0x100)]->pInstance[index] = value; 1377 | 1378 | COUT<<"\t Field referenc is object "<<(int)thisclassobject<<" field "<<(int)token<pInstance[index]<pConstPool->pConstantPool[index]; 1385 | 1386 | u1 byte1 = pCI->info[0]; 1387 | 1388 | if((byte1 & 0x80) == 0x80) 1389 | { 1390 | u1 packageIndex = byte1 & 0x7F; 1391 | PackageInfo* pPI = pCA->pImport->packages[packageIndex]; 1392 | 1393 | CardLibrary *pCL = CardLibList::findLibrary(pPI); 1394 | 1395 | if(pCL != NULL) 1396 | { 1397 | COUT<<"\tExternal library found!"<info[1]; 1399 | 1400 | u2 sOffset = pCL->pExport->pClassExport[classtoken]->classOffset; 1401 | 1402 | ClassInfo* pClInf = (ClassInfo*)pCL->pClass->pClasses[sOffset]; 1403 | 1404 | u1 token = pCI->info[2]; 1405 | 1406 | pCL->pMethod->executeByteCode(pClInf->publicVirtualMethodTable[token-pClInf->publicMethodTableBase],pCL); 1407 | } 1408 | else 1409 | { 1410 | COUT<<"\tInvoke package "<<(int)pCI->info[0]<<" class "<<(int)pCI->info[1]<<" method "<<(int)pCI->info[2]<info[0],pCI->info[1]); 1417 | u1 token = pCI->info[2]; 1418 | COUT<<"\tInvoke class "<<(int)offset<<" method "<<(int)token<pClass->pClasses[offset]; 1420 | if((token & 0x80)==0x80) 1421 | { 1422 | COUT<<"This is a TODO function here!"<pMethod->executeByteCode(pClInf->publicVirtualMethodTable[token - pClInf->publicMethodTableBase],pCA); 1427 | } 1428 | } 1429 | } 1430 | 1431 | void VirtualMachine::vm_getfield_s_this(u1 index,AbstractApplet *pCA) 1432 | { 1433 | s2 value = javaClasses[((u2)jlocals[0]-0x100)]->pInstance[index]; 1434 | 1435 | push(value); 1436 | 1437 | COUT<<"\t Field referenc is object "<<(int)((u2)jlocals[0]-0x100)<<" Index "<<(int)index<<" is "<<(int)value<pClass->pClasses[sOffset]; 1451 | 1452 | ClassRef* pCR = &pClInf->superClassRef; 1453 | 1454 | int declaredInstanceSize = pClInf->declaredInstanceSize; 1455 | 1456 | javaClasses[++jcCount] = new JavaClass(ClassRef(sOffset),*pCR,declaredInstanceSize); 1457 | push((u2)(jcCount+0x100)); 1458 | 1459 | COUT<<"\tClass "<<(int)(jcCount+0x100)<<" has been created! "<pConstPool->pConstantPool[index]; 1465 | 1466 | s1 value1 = pCI->info[0]; 1467 | 1468 | if(value1 == 0) 1469 | { 1470 | u2 sOffset = makeU2(pCI->info[1],pCI->info[2]); 1471 | COUT<<"\tMethod in offset "<<(int)sOffset<<" has been invoked!"<pMethod->executeByteCode(sOffset,pCA); 1474 | } 1475 | else 1476 | { 1477 | u1 packageIndex = value1 & 0x7F; 1478 | PackageInfo* pPI = pCA->pImport->packages[packageIndex]; 1479 | 1480 | CardLibrary *pCL = CardLibList::findLibrary(pPI); 1481 | 1482 | if(pCL != NULL) 1483 | { 1484 | COUT<<"\tExternal library found!"<info[1]; 1486 | 1487 | u2 sOffset = pCL->pExport->pClassExport[classtoken]->pStaticMethodOffsets[pCI->info[2]]; 1488 | 1489 | pCL->pMethod->executeByteCode(sOffset,pCL); 1490 | } 1491 | else 1492 | { 1493 | COUT<<"\tInvoke package "<<(int)pCI->info[0]<<" class "<<(int)pCI->info[1]<<" method "<<(int)pCI->info[2]<pConstPool->pConstantPool[sIndex]; 1523 | if(pCI->Tag != 1){ 1524 | COUT<<"\t\tError! It is not a classref!"<superClass.classref == makeU2(pCI->info[0],pCI->info[1])){ 1533 | COUT<<"The current object is an instance of class "<<(int)pObj->superClass.classref<info[0]; 1539 | if(value1 == 0) 1540 | { 1541 | u2 sOffset = makeU2(pCI->info[0],pCI->info[1]); 1542 | COUT<<"\tIt is an internal class."<pClass->pClasses[sOffset]; 1546 | 1547 | if(!pACI->isInterface()){ 1548 | COUT<<"\tIt's an class"< 1551 | } 1552 | 1553 | //pObj->superClass; 1554 | } 1555 | else 1556 | { 1557 | u1 packageIndex = value1 & 0x7F; 1558 | PackageInfo* pPI = pCA->pImport->packages[packageIndex]; 1559 | CardLibrary *pCL = CardLibList::findLibrary(pPI); 1560 | 1561 | if(pCL != NULL) 1562 | { 1563 | COUT<<"\tExternal library found!"<info[1]; 1565 | 1566 | u2 coffset = pCL->pExport->pClassExport[classtoken]->classOffset; 1567 | 1568 | COUT<<"\tThe class offset is "<info[1]<