I am trying to program a simple bit JVM with C. After reading the .class file with hex, I am trying to parse this file.
char *get_bytecode(const char *filename) {
FILE *fileptr = fopen(filename, "rb");
if (!fileptr) {
fprintf(stderr, "Error: could not open file %s\n", filename);
return NULL;
}
char *buffer = malloc(1);
buffer[0] = '\0';
unsigned char byte;
while(fread(&byte, sizeof(byte), 1, fileptr) == 1) {
char *temp = malloc(3);
sprintf(temp, "%02x", byte);
buffer = realloc(buffer, strlen(buffer) + strlen(temp) + 1);
strcat(buffer, temp);
free(temp);
}
fclose(fileptr);
return buffer;
}
There is no problem with the above function that I occured. After that, I wrote a function to parse the bytecode I received:
classfile parse_class(const char *bytecode_hex) {
classfile classfile;
memset(&classfile, 0, sizeof(classfile));
char *endptr;
classfile.magic = strtoul(bytecode_hex, &endptr, 16);
printf("Magic: %08X\n", classfile.magic);
classfile.minor = (uint16_t)strtoul(endptr, &endptr, 16);
printf("Minor: %04X\n", classfile.minor);
classfile.major = (uint16_t)strtoul(endptr, NULL, 16);
printf("Major: %04X\n", classfile.major);
return classfile;
}
I guess the problem is here because I am getting an output like this:
Magic: FFFFFFFF
Minor: 0000
Major: 0000
but the expected output should be like this:
Magic: CAFEBABE
Minor: 0000
Major: 0056
I couldn't understand exactly what caused the problem. Thank you in advance for any constructive comments.