Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The following program takes a PPM image and will either encrypt or decrypt it wh

ID: 3751855 • Letter: T

Question

The following program takes a PPM image and will either encrypt or decrypt it when supplied with a key.

These 3 errors continue popping up no matter what I do. Please help fix them and state what the ppm image decrypts into.

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

key: 22695477

secret.ppm

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

//lab4.c //

#include "stdio.h"
#include "stdlib.h"
#include "crypto.h"

int main(int argc, char const *argv[]) {

FILE* image_file = fopen(argv[1], "r");
FILE* key_file = fopen(argv[2], "r");
FILE* out_image = fopen(argv[3], "w");

// Read image
header_t header;
img_t img;
  
read_header(image_file, header);
  
void read_header(FILE* in_img, header_t* header);
  
read_header(image_file, &header);
read_image(image_file, &img, &header);

// Read secret key
int seed;

read_seed(key_file, *seed);
  
void read_seed(FILE* seed_file, int* seed);
  
read_seed(key_file, &seed);
  
sp_rand(seed);
  
printf("Encrypt[1] or Decrypt[2]? ");
  
int choice;
while(true) {
scanf("%d",&choice);
if(choice == 1 || choice == 2) {
sym_crypt(out_image, &header, &img, choice);
break;
} else {
printf("Please enter 1 or 2 ");
}
}

/********************/
fclose(image_file);
fclose(key_file);
fclose(out_image);
return 0;
}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


// crypto.c //

#include "crypto.h"

int P_RAND_SEED = 1;

void read_header(FILE* in_img, header_t* header) {

header->type = malloc(sizeof(char)*3);
fscanf(in_img, "%s ", header->type);
fscanf(in_img, "%d %d ", &header->w, &header->h);
fscanf(in_img, "%d ", &header->cs);
printf("Header: %s %d %d %d ", header->type, header->w, header->h, header->cs);
}


Fix errors...
void read_image(FILE* input, img_t* image, header_t* header) {
int row, col;

Error 1:
image->data = malloc(sizeof(pixel_t*) * header.h);

image->data = malloc(sizeof(pixel_t*) * header->h);
for(row = 0; row < header->h; row++) {
image->data[row] = malloc(sizeof(pixel_t) * header->w * 3);
for(; col < header->w; col++) {

pixel_t pix;
fscanf(input, "%c%c%c", &pix.r, &pix.g, &pix.b);

image.data[row][col] = pix;

image->data[row][col] = pix;
}
}
}

void read_seed(FILE* seed_file, int* seed) {
fscanf(seed_file, "%d", seed);
}

void sym_crypt(FILE* output, header_t* header, img_t* image, int mode) {
// Write header of PPM to files
fprintf(output, "%s %d %d %d ", header->type, header->w, header->h, header->cs);


// Encrypt or Decrypt!
// fix errors //
int row, col;
switch (mode) {
case 1:

printf("Encrypt ", );

printf("Encrypt ");
for(row = 0; row < header->h; row++) {
for(col = 0; col < header->w; col++) {
int swap_row = p_rand(row*row) % header->h;
int swap_col = p_rand(col*col) % header->w;
pixel_t swap = image->data[row][col];
image->data[row][col] = image->data[swap_row][swap_col];
image->data[swap_row][swap_col] = swap;
}
} break;
case 2:
printf("Decrypt ");
for(row = header->h-1; row >= 0; row--) {
for(; col >= 0; col--) {

int swap_row = p_rand(row*row) % header.h;

int swap_row = p_rand(row*row) % header->h;
int swap_col = p_rand(col*col) % header->w;
pixel_t swap = image->data[row][col];
image->data[row][col] = image->data[swap_row][swap_col];
image->data[swap_row][swap_col] = swap;
}
} break;
default: break;
}
for(row = 0; row < header->h; row++) {
for(col = 0; col < header->w; col++) {
pixel_t pix = image->data[row][col];
fprintf(output, "%c%c%c", pix.r, pix.g, pix.b);
}
}

}
void sp_rand(unsigned int seed) {
P_RAND_SEED = seed;
}

unsigned int p_rand(unsigned int init) {
return (P_RAND_SEED*init) % INT_MAX;
}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

// crypto.h //


#ifndef CRYPTO_H
#define CRYPTO_H value
#include "stdio.h"
#include "stdlib.h"
#include "limits.h"
#include "stdbool.h"

extern int P_RAND_SEED;

typedef struct header_t {
char* type;
int w, h, cs;
} header_t;

typedef struct pixel_t {
unsigned char r,g,b;
} pixel_t;

typedef struct img_t {
pixel_t** data;
} img_t;

void read_header(FILE* in_img, header_t* header);
void read_image(FILE* in_img, img_t* image, header_t* header);
void read_seed(FILE* seed_file, int* seed);
void sym_crypt(FILE* out_img, header_t* header, img_t* image, int mode);

// random number generator //
void sp_rand(unsigned int seed);
unsigned int p_rand(unsigned int init);

#endif

Running /home/ec2-user/environment/lab4.c /home/ec2-user/environment/lab4.c: In function main /home/ec2-user/environment/1ab4. c : 17:4: error: ncompatible type for argument 2 of cread-header' read header (image file, header); A. In file included from /home/ec2-user/environment/lab4.c:5:0: /home/ec2-user/environment/crypto.h:26:6: note: expected 'struct header t but argument is of type 'header t' void read header(FILE in img, header_t* header); /home/ec2-user/environment/lab4.c:27:24: error: invalid type argument of unary(have 'int) read seed (key file, *seed); A.

Explanation / Answer

1st error:

in crypto.h , you have defined a function :

void read_header(FILE* in_img, header_t* header); // this is taking a pointer variable as argument

But in lab4.c:

header_t header; // you should take like -> header_t *header;
img_t img;
read_header(image_file, header); // sothat when you pass header through this , the read_header() of crypto.h will get a pointer of header_t;

Do this , and run program , i think first two errors will diappear.

2nd error:

in lab4.c:

int seed; // the seed is an integer type , not a pointer value
read_seed(key_file, *seed); //here , you are passing seed as a pointer

i think here is the problem : you can try like ->  int *seed;

Run the code by doing this things , and if not working then focus on types of arguments you are passing and in the defined function , what type of arguments you are receiving . if not getting , please comment.

Thanks