Advertisement
Guest User

Untitled

a guest
Oct 4th, 2023
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #include <cstdint>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <sys/mman.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9.  
  10. static void print_usage(const char *name) {
  11. printf("Usage: %s -f <file_path> -w <width> -h <height>\n", name);
  12. }
  13.  
  14. int main(int argc, char **argv) {
  15. const char *file_path = NULL;
  16. uint32_t resolution_width = 0;
  17. uint32_t resolution_height = 0;
  18.  
  19. int opt;
  20. while ((opt = getopt(argc, argv, "f:w:h:")) != -1) {
  21. switch (opt) {
  22. case 'f':
  23. file_path = optarg;
  24. break;
  25. case 'w':
  26. sscanf(optarg, "%u", &resolution_width);
  27. break;
  28. case 'h':
  29. sscanf(optarg, "%u", &resolution_height);
  30. break;
  31. default:
  32. print_usage(argv[0]);
  33. return (EXIT_FAILURE);
  34. }
  35. }
  36.  
  37. if (file_path == NULL || resolution_width <= 0 || resolution_height <= 0) {
  38. print_usage(argv[0]);
  39. return (EXIT_FAILURE);
  40. }
  41.  
  42. // Open the file for read and write
  43. int fd = open(file_path, O_RDWR);
  44. if (fd == -1) {
  45. perror("Error opening file");
  46. return (EXIT_FAILURE);
  47. }
  48.  
  49. // Get the size of the file
  50. struct stat file_stat;
  51. if (fstat(fd, &file_stat) == -1) {
  52. perror("Error getting file size");
  53. close(fd);
  54. return (EXIT_FAILURE);
  55. }
  56.  
  57. off_t file_size = file_stat.st_size;
  58.  
  59. // Map the file into memory
  60. void *mapped_data =
  61. mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  62. if (mapped_data == MAP_FAILED) {
  63. perror("Error mapping file to memory");
  64. close(fd);
  65. return (EXIT_FAILURE);
  66. }
  67.  
  68. // Now you can treat mapped_data as an array and edit its contents
  69. uint32_t *data = (uint32_t *)((u_int8_t *)mapped_data + 0x3C);
  70.  
  71. printf("Old value: %u x %u\n", data[0], data[1]);
  72.  
  73. if (resolution_width != 0) {
  74. data[0] = resolution_width;
  75. }
  76. if (resolution_height != 0) {
  77. data[1] = resolution_height;
  78. }
  79.  
  80. printf("New value: %u x %u\n", data[0], data[1]);
  81.  
  82. // Unmap the file from memory
  83. if (munmap(mapped_data, file_size) == -1) {
  84. perror("Error unmapping file from memory");
  85. close(fd);
  86. return (EXIT_FAILURE);
  87. }
  88.  
  89. // Close the file descriptor
  90. close(fd);
  91.  
  92. return 0;
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement