1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
/* libjclass - Library for reading java class files
* Copyright (C) 2003 Nicos Panayides
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*
* $Id: java_file.c,v 1.19 2003/11/28 10:34:53 anarxia Exp $
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <jclass/class.h>
static uint16_t fread_uint16(FILE*);
static uint32_t fread_uint32(FILE*);
static ConstantPool* fread_constant_pool(FILE*);
static void get_next_entry(FILE*, ConstantPoolEntry*);
static uint16_t* fread_interfaces(FILE*, uint16_t);
static Field* fread_fields(FILE*, uint16_t);
static AttributeContainer* fread_attributes(FILE*, uint16_t);
/* Reads 2 bytes from a file and converts them to native byte order.
*/
static uint16_t fread_uint16(FILE* classfile)
{
uint16_t bytes;
fread(&(bytes), sizeof(uint16_t), 1, classfile);
bytes = UINT16_NATIVE(bytes);
return bytes;
}
/* Reads 4 bytes from a file and converts them to native byte order.
*/
static uint32_t fread_uint32(FILE* classfile)
{
uint32_t bytes;
fread(&(bytes), sizeof(uint32_t), 1, classfile);
bytes = UINT32_NATIVE(bytes);
return bytes;
}
/**
* jclass_class_new_from_file
* @classfile: The file containing the class.
*
* Creates a JavaClass struct from the given class file.
* The file must be opened with "rb" permissions.
* The file will always be closed when the function returns.
*
* Returns: A JavaClass struct allocated with malloc.
*/
JavaClass* jclass_class_new_from_file(FILE* classfile)
{
JavaClass* class_struct;
if (classfile == NULL)
return NULL;
if (fread_uint32(classfile) != JAVA_CLASS_MAGIC)
{
fclose(classfile);
return NULL;
}
class_struct = (JavaClass*) malloc(sizeof(JavaClass));
class_struct->minor_version = fread_uint16(classfile);
class_struct->major_version = fread_uint16(classfile);
class_struct->constant_pool = fread_constant_pool(classfile);
class_struct->access_flags = fread_uint16(classfile);
class_struct->constant_pool->this_class = fread_uint16(classfile);
class_struct->constant_pool->super_class = fread_uint16(classfile);
class_struct->interfaces_count = fread_uint16(classfile);
class_struct->interfaces = fread_interfaces(classfile, class_struct->interfaces_count);
class_struct->fields_count = fread_uint16(classfile);
class_struct->fields = fread_fields(classfile, class_struct->fields_count);
class_struct->methods_count = fread_uint16(classfile);
class_struct->methods = fread_fields(classfile, class_struct->methods_count);
class_struct->attributes_count = fread_uint16(classfile);
class_struct->attributes = fread_attributes(classfile, class_struct->attributes_count);
fclose(classfile);
return class_struct;
}
/**
* jclass_cp_new_from_file
* @classfile: The file containg the class.
*
* Reads the constant pool of the class from the given class file.
* The file must be opened with "rb" permissions.
* The file will always be closed when the function returns.
*
* Returns: A ConstantPool struct.
*/
ConstantPool* jclass_cp_new_from_file(FILE* classfile)
{
ConstantPool* constant_pool;
int bytes;
if (classfile == NULL)
return NULL;
if (fread_uint32(classfile) != JAVA_CLASS_MAGIC)
{
fclose(classfile);
return NULL;
}
fread(&bytes, 4, 1, classfile);
constant_pool = fread_constant_pool(classfile);
fread(&bytes, 2, 1, classfile);
constant_pool->this_class = fread_uint16(classfile);
constant_pool->super_class = fread_uint16(classfile);
fclose(classfile);
return constant_pool;
}
static ConstantPool* fread_constant_pool(FILE* classfile)
{
ConstantPool* constant_pool;
uint16_t count = 0;
constant_pool = (ConstantPool*) malloc(sizeof(ConstantPool));
constant_pool->count = fread_uint16(classfile);
constant_pool->entries = (ConstantPoolEntry*) malloc(sizeof(ConstantPoolEntry) * constant_pool->count);
constant_pool->entries[0].tag = CONSTANT_Empty;
do
{
count++;
get_next_entry(classfile, &constant_pool->entries[count]);
if (constant_pool->entries[count].tag != CONSTANT_Empty)
{
/* For every double or long the next entry is reserved for the VM */
if (constant_pool->entries[count].tag == CONSTANT_Long || constant_pool->entries[count].tag == CONSTANT_Double)
{
count++;
constant_pool->entries[count].tag = CONSTANT_Empty;
}
}
else
{
fputs("Unrecognised entry in the constant pool\n", stderr);
}
}
while(count < constant_pool->count - 1);
return constant_pool;
}
/**
* get_next_entry
* @classfile: The file to read from.
* @info: A pointer to the ConstantPoolEntry struct to store the entry.
*
* Reads the next constant pool entry from the class file.
*/
static void get_next_entry(FILE* classfile, ConstantPoolEntry* info)
{
int tags_read;
tags_read = fread(&(info->tag), sizeof(uint8_t), 1, classfile);
if (tags_read < 1)
{
info->tag = 0;
return;
}
switch(info->tag)
{
case CONSTANT_Class:
info->info.classinfo.name_index = fread_uint16(classfile);
break;
case CONSTANT_Fieldref:
case CONSTANT_Methodref:
case CONSTANT_InterfaceMethodref:
info->info.ref.class_index = fread_uint16(classfile);
info->info.ref.name_and_type_index = fread_uint16(classfile);
break;
case CONSTANT_String:
info->info.stringinfo.string_index = fread_uint16(classfile);
break;
case CONSTANT_Integer:
case CONSTANT_Float:
info->info.integer.bytes = fread_uint32(classfile);
break;
case CONSTANT_Long:
case CONSTANT_Double:
info->info.longinfo = (LongEntry*) malloc(sizeof(LongEntry));
info->info.longinfo->long_bytes = ((uint64_t) fread_uint32(classfile)) << 32;
info->info.longinfo->long_bytes += fread_uint32(classfile);
break;
case CONSTANT_NameAndType:
info->info.nameandtype.name_index = fread_uint16(classfile);
info->info.nameandtype.descriptor_index = fread_uint16(classfile);
break;
case CONSTANT_Utf8:
info->info.utf8 = (UTF8Entry*) malloc(sizeof(UTF8Entry));
info->info.utf8->length = fread_uint16(classfile);
if(info->info.utf8->length)
{
info->info.utf8->contents = (uint8_t*) malloc(sizeof(uint8_t) * info->info.utf8->length);
fread(info->info.utf8->contents, info->info.utf8->length, 1, classfile);
}
else
info->info.utf8->contents = NULL;
break;
default:
fprintf(stderr, "Unknown tag number: %d\n", info->tag);
info->tag = CONSTANT_Empty;
}
}
static uint16_t* fread_interfaces(FILE* classfile, uint16_t count)
{
uint16_t* interfaces;
uint16_t i;
if(count)
interfaces = (uint16_t*) malloc(sizeof(uint16_t) * count);
else
interfaces = NULL;
for(i = 0; i < count; i++)
interfaces[i] = fread_uint16(classfile);
return interfaces;
}
/**
* fread_fields
* @classfile: The file to read the fields from.
* @count: The number of fields to read.
*
* Reads count number of fields from a class file.
*
* Returns: An array of Fields read from a file.
*/
static Field* fread_fields(FILE* classfile, uint16_t count)
{
Field* field_array;
uint16_t i;
if(count)
{
field_array = (Field*) malloc(sizeof(Field) * count);
for(i=0; i < count; i++)
{
field_array[i].access_flags = fread_uint16(classfile);
field_array[i].name_index = fread_uint16(classfile);
field_array[i].descriptor_index = fread_uint16(classfile);
field_array[i].attributes_count = fread_uint16(classfile);
field_array[i].attributes = fread_attributes(classfile, field_array[i].attributes_count);
}
}
else
field_array = NULL;
return field_array;
}
/**
* fread_attributes
* @classfile: The file to read the attributes from.
* @count: Number of attributes to read.
*
* Helper function to read attributes from the file.
* The file pointer must be where the attributes start.
*
* Returns: An array of AttributeContainer structs with the attributes.
*/
static AttributeContainer* fread_attributes(FILE* classfile, uint16_t count)
{
AttributeContainer* attributes;
int j;
if(!count)
return NULL;
attributes = (AttributeContainer*) malloc(sizeof(AttributeContainer) * count);
for(j=0; j < count; j++)
{
attributes[j].name_index = fread_uint16(classfile);
attributes[j].length = fread_uint32(classfile);
if(attributes[j].length)
{
attributes[j].contents = (uint8_t*) malloc(sizeof(uint8_t) * attributes[j].length);
fread(attributes[j].contents, attributes[j].length, 1, classfile);
}
else
attributes[j].contents = NULL;
}
return attributes;
}
|