Last active
April 12, 2022 03:37
-
-
Save imkiva/6525703b8cb3a7c146f71beb9205ece6 to your computer and use it in GitHub Desktop.
Strip away preview feature minor version from `.class` file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <fcntl.h> | |
#include <stdint.h> | |
#include <sys/mman.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
struct Head { | |
uint32_t magic; | |
uint16_t minor; | |
uint16_t major; | |
}; | |
uint32_t toBE32(uint32_t le) { | |
unsigned char *p = (unsigned char *) ≤ | |
return ((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | (p[3])); | |
} | |
int main(int argc, const char **argv) { | |
--argc; ++argv; | |
if (argc != 1) { | |
printf("Usage: %s: <class-file>\n", argv[-1]); | |
return 1; | |
} | |
int f = open(argv[0], O_RDWR); | |
if (f < 0) { | |
perror("open"); | |
return 1; | |
} | |
char *cf = mmap( | |
NULL, | |
sizeof(struct Head), | |
PROT_READ | PROT_WRITE, | |
MAP_SHARED, | |
f, | |
0 | |
); | |
if (cf == MAP_FAILED) { | |
perror("mmap"); | |
return 1; | |
} | |
struct Head *h = (struct Head *) cf; | |
if (h->magic == toBE32(0xcafebabe) && h->minor == 0xffff) { | |
printf("%s has preview feature bit set, clearing\n", argv[0]); | |
h->minor = 0; | |
} | |
// printf("magic: %x\n", h->magic); | |
// printf("minor: %u\n", h->minor); | |
// printf("major: %u\n", h->major); | |
munmap(cf, sizeof(struct Head)); | |
close(f); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment