utils.c
1 /* 2 * Bean Java VM 3 * Copyright (C) 2005-2015 Christian Lins <christian@lins.me> 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #include <vm.h> 19 20 #define __BYTE_ORDER_LITTLE_ENDIAN__ 21 22 #ifdef __BYTE_ORDER_LITTLE_ENDIAN__ 23 /* Works only for reading bigendian and saving in littleendian */ 24 unsigned int BufferToInt(unsigned char buffer[4]) 25 { 26 register unsigned int var = 0; 27 28 var = buffer[0]; 29 var = var << (sizeof(unsigned char) * 8); 30 var = var | buffer[1]; 31 var = var << (sizeof(unsigned char) * 8); 32 var = var | buffer[2]; 33 var = var << (sizeof(unsigned char) * 8); 34 var = var | buffer[3]; 35 36 return var; 37 } 38 #endif 39 40 #ifdef __BYTE_ORDER_BIG_ENDIAN__ 41 /* Works only for reading bigendian and saving in bigendian */ 42 unsigned int BufferToInt(unsigned char buffer[4]) 43 { 44 register unsigned int var = 0; 45 46 var = buffer[3]; 47 var = var << (sizeof(unsigned char) * 8); 48 var = var | buffer[2]; 49 var = var << (sizeof(unsigned char) * 8); 50 var = var | buffer[1]; 51 var = var << (sizeof(unsigned char) * 8); 52 var = var | buffer[0]; 53 54 return var; 55 } 56 #endif 57 58 #ifdef __BYTE_ORDER_LITTLE_ENDIAN__ 59 /* Works only for reading bigendian (Java) and saving in littleendian */ 60 unsigned short BufferToShort(unsigned char buffer[2]) 61 { 62 register unsigned short var = 0; 63 64 var = buffer[0]; 65 var = var << (sizeof(unsigned char) * 8); 66 var = var | buffer[1]; 67 68 return var; 69 } 70 #endif 71 72 #ifdef __BYTE_ORDER_ORDER_BIG_ENDIAN__ 73 /* Works only for reading bigendian (Java) and saving in bigendian */ 74 unsigned short BufferToShort(unsigned char buffer[2]) 75 { 76 register unsigned short var = 0; 77 78 var = buffer[1]; 79 var = var << (sizeof(unsigned char) * 8); 80 var = var | buffer[0]; 81 82 return var; 83 } 84 #endif 85 86 /* 87 * Creates a complete system path using a classname (without extension) and the 88 * classlib path. 89 */ 90 char *compose_path(char *className, char *basePath) 91 { 92 int n, pathLength; 93 char *completePath; 94 95 assert(className != NULL); 96 assert(basePath != NULL); 97 98 /* Don't forget the \0 and '/' */ 99 pathLength = 100 strlen(basePath) + strlen(className) + strlen(".class") + 2; 101 completePath = (char *) malloc(sizeof(char) * pathLength); 102 103 /* Clear the buffer, otherwise strcat does not work */ 104 for (n = 0; n < pathLength; n++) { 105 completePath[n] = 0; 106 } 107 108 strcat(completePath, basePath); 109 strcat(completePath, "/"); 110 strcat(completePath, className); 111 strcat(completePath, ".class"); 112 113 completePath[pathLength - 1] = 0; 114 115 #ifdef WIN32 116 for (n = 0; n < pathLength; n++) 117 if (completePath[n] == '/') { 118 completePath[n] = '\\'; 119 } 120 #endif 121 122 #ifdef DEBUG 123 printf("Combined Path: %s\n", completePath); 124 #endif 125 126 return completePath; 127 }