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

The function embed checksum can be decomposed in the following sequence of steps

ID: 3561493 • Letter: T

Question

The function embed checksum can be decomposed in the following sequence of steps: Load some image data Convert the sequence of bytes of the image data array into a null terminated string of '0' and '1'. This string data string will become the rst input parameter of the function checksum Remove all characters from data string that correspond to bits 0 of the image data. These bits were coloured in red in Section 1.2 Compute the checksum of the (reduced) data string using the function checksum from Section 3. This checksum corresponds to the string checksum string

Overwrite the image le with the checksum string embedded as a message.

Note that The data string will have initially 8 times more characters than the number of bytes of the image data array.

The image data of the image le gets overwritten.

/**

* This function is responsible for computing the checksum

* of all bits 1 to 7 of an image, and embedding the

* checksum as a null terminated string of '0' and '1'

* in the image file.

*

* @param char* file_name - The name of the image file to be processed

* @param char* key_string - The null terminated key string of '0' and '1'

*

* @return int - the numeric value 0 on success,

* -1 if an error occurs (file does not exists for example)

*/

int embed_checksum(char* file_name, char* key_string);

int checksum(char* data_string, char* key_string, char** checksum_string){

                int i;

                int j;

                int k;

   int data_size = strlen(data_string);

   int key_size = strlen(key_string);

   //run till right end of data_string align with key_string

   for(i=0;i<data_size-key_size+1;i++){

       //xor would only be done when left most of key_sting align with 1 on data_string

       if(data_string[i]=='1'){

           for(j=0,k=i;j<key_size;j++,k++)

               //xoring

               data_string[k] = ((data_string[k]-'0') ^ (key_string[j]-'0')) + '0';

       }

   }

   char * buffer;

   //checksum string is of size key_size-1 plus 1 for null character

   buffer = (char*) malloc (key_size);

   if(buffer==NULL) return -1;

   //copy last key_size - 1 character from data_string into checksum_string

   strcpy(buffer,data_string+data_size-key_size+1);

   *checksum_string = buffer;

   return 0;

}

Explanation / Answer

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int checksum(char* data_string, char* key_string, char** checksum_string);
//main functiion to check the function
int main(){
   char data_string[100],key_string[100];
    cin>>data_string>>key_string;
    char* checksum_string;
   cout<<checksum(data_string,key_string,&checksum_string)<<endl;
   cout<<checksum_string<<endl;

   return 0;
}

int checksum(char* data_string, char* key_string, char** checksum_string){
   int data_size = strlen(data_string);
   int key_size = strlen(key_string);
  
   //run till right end of data_string align with key_string
   for(int i=0;i<data_size-key_size+1;i++){
       //xor would only be done when left most of key_sting align with 1 on data_string
       if(data_string[i]=='1'){
           for(int j=0,k=i;j<key_size;j++,k++)
               //xoring
               data_string[k] = ((data_string[k]-'0') ^ (key_string[j]-'0')) + '0';
       }
   }
  
   char * buffer;
   //checksum string is of size key_size-1 plus 1 for null character
   buffer = (char*) malloc (key_size);
   if(buffer==NULL) return -1;
   //copy last key_size - 1 character from data_string into checksum_string
   strcpy(buffer,data_string+data_size-key_size+1);
  
   *checksum_string = buffer;
   return 0;
}

Run:

Copy the code in file named of your choice i.e checksum.cpp
    type in terminal
        c++ checksum.cpp
        ./a.out

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote