Reputation: 73
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.
Upvotes: 0
Views: 44
Reputation: 112412
Did you look at your bytecode_hex
string? You are printing a long string of hexadecimal digits. The first strtoul()
processes it in its entirely, overflowing, and so returning 0xffffffff
. (Or perhaps 0xffffffffffffffff
, since you are only printing the low eight digits.) The next two strtoul()
calls see no hex digits, and so return 0.
You need to put in spaces for where you want the strtoul()
to stop. Otherwise it has no clue how to parse a string of nothing but hex digits.
Also, it makes no sense to convert the byte codes to hex, and then back to binary. Just process the byte codes.
Upvotes: 0