Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdint>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/mman.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <unistd.h>
- static void print_usage(const char *name) {
- printf("Usage: %s -f <file_path> -w <width> -h <height>\n", name);
- }
- int main(int argc, char **argv) {
- const char *file_path = NULL;
- uint32_t resolution_width = 0;
- uint32_t resolution_height = 0;
- int opt;
- while ((opt = getopt(argc, argv, "f:w:h:")) != -1) {
- switch (opt) {
- case 'f':
- file_path = optarg;
- break;
- case 'w':
- sscanf(optarg, "%u", &resolution_width);
- break;
- case 'h':
- sscanf(optarg, "%u", &resolution_height);
- break;
- default:
- print_usage(argv[0]);
- return (EXIT_FAILURE);
- }
- }
- if (file_path == NULL || resolution_width <= 0 || resolution_height <= 0) {
- print_usage(argv[0]);
- return (EXIT_FAILURE);
- }
- // Open the file for read and write
- int fd = open(file_path, O_RDWR);
- if (fd == -1) {
- perror("Error opening file");
- return (EXIT_FAILURE);
- }
- // Get the size of the file
- struct stat file_stat;
- if (fstat(fd, &file_stat) == -1) {
- perror("Error getting file size");
- close(fd);
- return (EXIT_FAILURE);
- }
- off_t file_size = file_stat.st_size;
- // Map the file into memory
- void *mapped_data =
- mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
- if (mapped_data == MAP_FAILED) {
- perror("Error mapping file to memory");
- close(fd);
- return (EXIT_FAILURE);
- }
- // Now you can treat mapped_data as an array and edit its contents
- uint32_t *data = (uint32_t *)((u_int8_t *)mapped_data + 0x3C);
- printf("Old value: %u x %u\n", data[0], data[1]);
- if (resolution_width != 0) {
- data[0] = resolution_width;
- }
- if (resolution_height != 0) {
- data[1] = resolution_height;
- }
- printf("New value: %u x %u\n", data[0], data[1]);
- // Unmap the file from memory
- if (munmap(mapped_data, file_size) == -1) {
- perror("Error unmapping file from memory");
- close(fd);
- return (EXIT_FAILURE);
- }
- // Close the file descriptor
- close(fd);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement