|
| 1 | +program steganography |
| 2 | + use iso_c_binding, only: c_int, c_size_t, c_char, c_char_ptr |
| 3 | + use iso_fortran_env, only: output_unit |
| 4 | + use, intrinsic :: iso_fortran_env, only: int32, int64 |
| 5 | + |
| 6 | + ! Declare variables to hold the input image and the data to be injected |
| 7 | + type(c_ptr) :: image, data |
| 8 | + integer(c_size_t) :: imageSize, dataSize |
| 9 | + |
| 10 | + ! Read the input image into memory |
| 11 | + call c_f_pointer(image, read_file("in.png")) |
| 12 | + imageSize = size(image) |
| 13 | + |
| 14 | + ! Read the data to be injected into memory |
| 15 | + call c_f_pointer(data, read_file("data.txt")) |
| 16 | + dataSize = size(data) |
| 17 | + |
| 18 | + ! Encode the data size as a 4-byte big-endian integer |
| 19 | + integer(c_int) :: dataSizeBE |
| 20 | + dataSizeBE = dataSize |
| 21 | + call swap_endian(dataSizeBE) |
| 22 | + |
| 23 | + ! Inject the data size and the data into the input image |
| 24 | + call inject_data(image, imageSize, dataSizeBE, 4) |
| 25 | + call inject_data(image, imageSize, data, dataSize) |
| 26 | + |
| 27 | + ! Write the modified image to a file |
| 28 | + call write_file("out.png", image, imageSize) |
| 29 | + |
| 30 | +contains |
| 31 | + |
| 32 | + ! Define a subroutine to read a file into memory |
| 33 | + function read_file(filename) result(ptr) |
| 34 | + ! Arguments |
| 35 | + character(len=*), intent(in) :: filename |
| 36 | + |
| 37 | + ! Result |
| 38 | + type(c_ptr) :: ptr |
| 39 | + |
| 40 | + ! Local variables |
| 41 | + integer :: fd |
| 42 | + integer(c_size_t) :: size |
| 43 | + |
| 44 | + ! Open the file |
| 45 | + fd = open(filename, O_RDONLY) |
| 46 | + if (fd < 0) stop "Error opening file" |
| 47 | + |
| 48 | + ! Get the size of the file |
| 49 | + size = lseek(fd, 0, SEEK_END) |
| 50 | + |
| 51 | + ! Allocate memory to hold the contents of the file |
| 52 | + call c_f_pointer(ptr, malloc(size)) |
| 53 | + |
| 54 | + ! Rewind the file to the beginning |
| 55 | + lseek(fd, 0, SEEK_SET) |
| 56 | + |
| 57 | + ! Read the contents of the file into memory |
| 58 | + read(fd, ptr, size) |
| 59 | + |
| 60 | + ! Close the file |
| 61 | + close(fd) |
| 62 | + end function read_file |
| 63 | + |
| 64 | + ! Define a subroutine to write data to a file |
| 65 | + subroutine write_file(filename, data, size) |
| 66 | + ! Arguments |
| 67 | + character(len=*), intent(in) :: filename |
| 68 | + type(c_ptr), intent(in) :: data |
| 69 | + integer(c_size_t), intent(in) :: size |
| 70 | + |
| 71 | + ! Local variables |
| 72 | + integer :: fd |
| 73 | + |
| 74 | + ! Open the file |
| 75 | + fd = open(filename, O_WRONLY | O_CREAT) |
| 76 | + if (fd < 0) stop "Error opening file" |
| 77 | + |
| 78 | + ! Write the data to the file |
| 79 | + write(fd, data, size) |
| 80 | + |
| 81 | + ! Close the file |
| 82 | + close( |
0 commit comments