add chunks dump

This commit is contained in:
geoffrey 2025-07-24 17:44:22 +02:00
parent c851620cd4
commit 4ececfc6d5
2 changed files with 70 additions and 11 deletions

BIN
main

Binary file not shown.

81
main.c

@ -43,27 +43,38 @@ struct iHDR{
} __attribute__((packed));
/* PROTOTYPES */
static void list_chunks(int, const char *, int);
static int hide_message(int, char **);
static int unhide_message(int, char **);
static int getHeader(int, char *);
static size_t insertChunk(int, const char *, size_t, int);
static void convert_from_uint(unsigned char *, const unsigned int);
static void convert_from_bytes(const unsigned char *, unsigned int *);
static void usage(const char *);
int main(int argc, char *argv[], char **envp){
int fd_r;
int fd_r, dump = -1;
// The two statements below check if the arguments passed are correct
if (argc < 4){
printf("Usage: %s <hide|unhide> <picture> <text file>\n", argv[0]);
if (argc < 3){
usage(argv[0]);
return -1;
}
if (strcmp(argv[1], "hide") != 0 && strcmp(argv[1], "unhide") != 0){
printf("ok\n");
printf("usage: %s <hide|unhide> <picture> <text file>\n", argv[0]);
if (strcmp(argv[1], "hide") != 0 && strcmp(argv[1], "unhide") != 0 && strcmp(argv[1], "chunks") != 0){
usage(argv[0]);
return -1;
}
if (strncmp(argv[1], "hide", 4) == 0 && argc < 4){
usage(argv[0]);
return -1;
}
else if (strncmp(argv[1], "unhide", 6) == 0 && argc < 4){
usage(argv[0]);
return -1;
}
else if (strncmp(argv[1], "chunks", 6) == 0 && argc == 4){
dump = atoi(argv[3]);
}
// https://man7.org/linux/man-pages/man2/open.2.html
@ -73,14 +84,62 @@ int main(int argc, char *argv[], char **envp){
return -1;
}
if (strcmp(argv[1], "hide") == 0){
if (strcmp(argv[1], "hide") == 0)
hide_message(fd_r, argv);
}
if (strcmp(argv[1], "unhide") == 0){
if (strcmp(argv[1], "unhide") == 0)
unhide_message(fd_r, argv);
}
if (strcmp(argv[1], "chunks") == 0)
list_chunks(fd_r, argv[2], dump);
close(fd_r);
}
/*
* This function print the usage of the program
*/
static void usage(const char *program){
printf("Usage: %s <hide|unhide|chunks> <picture> <text file|id>\n", program);
}
/*
* In this function, we get all chunks and dump a chunk if specified
*/
static void list_chunks(int fd, const char *filename, int dump){
struct chunk s_chunk;
int c = 1;
int nbChunks = 0;
// We bypass the signature
lseek(fd, 8, SEEK_SET);
while(c){
read(fd, &s_chunk, sizeof(struct chunk));
off_t off = lseek(fd, 0, SEEK_CUR);
unsigned int s = 0;
convert_from_bytes(s_chunk.length, &s);
if (dump == -1)
printf("chunk %d at 0x%0x type %c%c%c%c, length %x\n",
nbChunks, off, s_chunk.type[0], s_chunk.type[1], s_chunk.type[2], s_chunk.type[3],
s);
if (nbChunks == dump){
char *tmp = malloc(s);
read(fd, tmp, s);
printf("%s\n", tmp);
//write(0, tmp, s); // Write to stdout
lseek(fd, off, SEEK_SET); // Go back to the chunk header
free(tmp);
}
if (s_chunk.type[0] == 'I' && s_chunk.type[1] == 'E' &&
s_chunk.type[2] == 'N' && s_chunk.type[3] == 'D')
c = 0;
/* +4 for the CRC */
lseek(fd, s + 4, SEEK_CUR);
nbChunks += 1;
memset(&s_chunk, 0, sizeof(struct chunk));
}
}
/*
* In this function, we will hide the text provide in argument of the program to
* the destination image