Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Help with finishing a project

Options
  • 22-05-2012 6:25pm
    #1
    Registered Users Posts: 300 ✭✭


    Hi, if someone could help finishing 2 functions in this code would be greatfull. Program to embed a bitmap picture into another picture.
    Few beers from me :D


    // ### Entities #########################################################################
    // Extraction
    // FILE* image_in; This is the bitmap which contains the embedded file.
    //
    // Embedding
    // FILE* originalBMP ; The original file which is to be altered
    // FILE* embeddedFile ; The file which is to be embedded
    // FILE* alteredBMP ; The altered version of originalBMP
    //
    // The bmp contains 3 parts, header ( 14 bytes)
    // info ( 40 bytes)
    // image ( Remainder if file)
    //
    // The hidden file is contained in the image section as 2 parts:
    // leader ( 86 bytes contained in the first 8 * 86 bytes of image)
    // embeddedFile ( embeddedLength bytes long, contained in next (8 * embeddedLength) bytes of image.)
    //########################################################################################

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    //### This function is complete #############################
    //===== main =============================================================================
    // Asks user whether they wish to extract or embed a file in a bitmap container file
    //========================================================================================
    int main()
    {
    // prototypes
    void embed (void);
    void extract(void);

    // variables
    char choice;

    // Code
    do
    {
    printf("\nWhat would you like to do\n\n");
    printf(" 1) Extract a file from an image\n");
    printf(" 2) embed a file into an image\n\n");
    printf(" 0) Exit\n\n");
    printf(" > ");

    choice = getchar();
    getchar();
    }
    while ((choice !='0' ) && (choice != '1') && (choice != '2') );

    if (choice == '1') extract();
    if (choice == '2') embed();

    return(0);
    }

    //########################################################################################
    //# Extraction
    //########################################################################################

    //### This function is complete. #############################
    //===== extract ==========================================================================
    //# Extacts a file from a container bmp file
    //========================================================================================
    void extract(void)
    {
    // prototypes
    FILE* askAndOpen ( char message[], char mode );
    int BMPheaders ( FILE* image_in, unsigned char header[], unsigned char info[] );
    int searchForFile( unsigned char image[], char filename[] );
    void saveFile ( unsigned char image[], char filename[], int im_size );

    // variables
    FILE* image_in;

    unsigned char header[14]; // Array containing bmp header information
    unsigned char info [40]; // Array containing bmp info section
    unsigned char* image; // pointer to image data

    int imageSize; // size of data part of image.
    int embeddedLength; // Length of embedded file
    char embeddedName[80]; // Name of embedded file

    // code
    image_in = askAndOpen("Please enter name of image file: ",'r'); // open container (bmp) file

    imageSize = BMPheaders( image_in, header, info ); // read and analyse headers of container (bmp) file
    if ( NULL == (image = (unsigned char*)malloc(imageSize)) ) exit(5); // assign memory to store image section of container (bmp)
    if ( imageSize != fread(image, sizeof(char), imageSize, image_in) ) exit(6); // read image section of container (bmp) file.

    // At this point we have read in the header, info and image section of the
    // bmp file. Now we need to extract the embedded file from the image section

    embeddedLength = searchForFile(image, embeddedName); // check if there is a file embedded in image.
    // If there is, embedded Length > 0
    if (embeddedLength) saveFile (image, embeddedName, embeddedLength); // extract and save the file which is embedded in the image section

    free (image);
    fclose (image_in);

    }

    //### This function is complete. #############################
    //===== searchForFile ====================================================================
    //# Analyses the container bmp image section to see if there is a file embedded there.
    //# If a file is embedded it returns the length of the file, and the filename is stored in filename[]
    //========================================================================================
    int searchForFile(unsigned char image[], char filename[])
    {
    // prototypes
    unsigned char groupToByte( unsigned char group[8] );

    // variables
    unsigned char leader[86];
    unsigned char group [8];
    int i;
    int j;

    // Code

    for (i=0; i<86; i++) // The leader contains 86 bytes
    {
    for ( j=0; j<8; j++ ) // Copy 8 bytes from image section to group
    group[j] = image[ i*8 + j ];

    leader = groupToByte(group); // Construct byte in leader from 8 bytes in group
    }

    if ( (leader[0] == 'K') && (leader[1] == 'S') ) // if the first two chars are KS
    { // A file is embedded
    for (i=0; i<79; i++)
    filename = (leader[i+2] == '*') ? 0 : leader[i+2] ; // extract filename
    printf ("Filelength = %d", leader[82] + 256*( leader[83] + 256*(leader[84] + 256 * leader[85]))); // return length
    return (leader[82] + 256*( leader[83] + 256*(leader[84] + 256 * leader[85]))); // return length
    }
    else // No file embedded
    return (0); // return length = 0

    }


    //## This function is incomplete. You need to extract the embedded file from the image section INCOMPLETE
    // of the bmp file, and save the file as filename[]
    //===== saveFile =========================================================================
    //# extracts embedded data from image and saves it to a file.
    //========================================================================================
    void saveFile(unsigned char image[], char filename[], int filelength)
    {
    // prototypes
    unsigned char groupToByte( unsigned char group[8] );

    // The hidden file is embedded in the array 'image' It is necessary to extract it and
    // save it as a file. The length of the hidden file is 'filename' and it is 'filelength'
    // bytes long.
    // The extraction process is almost a cut and paste of the code in searchForFile()
    // but you must remember that 86 bytes have already been extracted as the leader.



    }

    //### This function is complete. #############################
    //===== groupToByte ======================================================================
    //# extracts the least significant bit from 8 bytes and combines them into a single byte.
    //========================================================================================
    unsigned char groupToByte( unsigned char group[8] )
    {
    // variables
    unsigned char Byte = 0;
    int i;

    for (i=0; i<8; i++)
    Byte = Byte * 2 + (group % 2) ;
    }


    //########################################################################################
    //# Embedding
    //########################################################################################

    //### This function is complete. #############################
    //===== embed ============================================================================
    //# Asks user for names of container bmp file, file to be embedded, and output file.
    //# Opens these files and outputs file containing embedded file.
    //========================================================================================
    void embed (void)
    {
    // prototypes
    FILE* askAndOpen ( char message[], char mode );
    int BMPheaders ( FILE* image_in, unsigned char header[], unsigned char info[] );
    void EmbedFile ( unsigned char image[], char filename[], unsigned char data[], int filelength );

    // variables
    FILE* originalBMP ;
    FILE* embeddedFile ;
    FILE* alteredBMP ;
    unsigned char header[14]; // Array containing bmp header information
    unsigned char info [40]; // Array containing bmp info section
    unsigned char* image; // pointer to image data
    int imageSize; // size of data part of image.

    unsigned char* embeddedData; // pointer to storage for embedded file
    int embeddedLength; // length of embedded file
    char embeddedName[80]; // name of embedded file

    // code

    // Open and read contents of original bmp file to contain embedded file.
    originalBMP = askAndOpen("Please enter name of container file : ",'r'); // open container file for reading
    imageSize = BMPheaders( originalBMP, header, info ); // read and analyse headers of container file
    if ( NULL == (image = (unsigned char*)malloc(imageSize)) ) exit(5); // assign memory to store image section of container
    if ( imageSize != fread(image, sizeof(char), imageSize, originalBMP) ) exit(6); // read image section of container file.
    fclose( originalBMP ); // close bmp file

    // Open and read contents of embedded file.
    do // open embedded file
    {
    printf("=================================================================\n");
    printf("Please enter name of file to be embedded: ");
    fgets(embeddedName,80,stdin); // read name of file from keyboard
    if (embeddedName[0]=='*') exit (0); // check that its not a '*'
    embeddedName[strlen(embeddedName)-1] = 0; // remove newline character
    }
    while (NULL == (embeddedFile = fopen(embeddedName,"rb")));

    if ( NULL == (embeddedData = (unsigned char*)malloc(imageSize/8)) ) exit(7); // assign memory to store embedded file
    embeddedLength = fread(embeddedData, sizeof(unsigned char), (imageSize/8), embeddedFile); // read contents of embedded file
    fclose(embeddedFile); // close embedded file

    if (embeddedLength >(imageSize/8 - 56) )
    {
    printf("File too large to embed ");
    exit (8);
    }

    // Embed embeddedData in image
    EmbedFile (image, embeddedName, embeddedData, embeddedLength);
    free (embeddedData);

    // open and write data to altered bmp.
    alteredBMP = askAndOpen("Please enter name of output bmp : ",'w'); // open altered bmp for writing
    fwrite(header, sizeof(unsigned char), 14, alteredBMP); // write header section of output file
    fwrite(info, sizeof(unsigned char), 40, alteredBMP); // write info section of output file
    fwrite(image, sizeof(unsigned char), imageSize, alteredBMP); // write image section of output file
    fclose( alteredBMP );

    free (image);

    }
    //### This function is not complete. INCOMPLETE
    //# You will have to create a leader with the characters KS, filename padded to 80 chars and filelength
    //# and embed it in image.
    //# You also have to embed the data in image.
    //===== embedFile ========================================================================
    //# Generates the leader and embeds it in the image section of the bitmap.
    //# Embeds the rest of the hidden file in the image.
    //========================================================================================
    void EmbedFile (unsigned char image[], char filename[], unsigned char data[], int filelength )
    {
    // prototypes
    void byteToGroup (unsigned char group[8], unsigned char byte);

    // variables
    unsigned char leader[86];
    unsigned char group [8];
    int i;
    int j;
    int Filelength;
    // code

    // Construct leader
    leader[0] = 'K'; // Insert signature
    leader[1] = 'S';

    for (i=2; i<82; i++) // Fill the name part with *
    leader = '*';

    for( i=0; i< strlen(filename); i++)
    leader[i+2] = filename; // Insert Filename;

    Filelength = filelength; // Insert filelength
    leader[82] = Filelength % 256; Filelength = filelength / 256;
    leader[83] = Filelength % 256; Filelength = filelength / 256;
    leader[84] = Filelength % 256; Filelength = filelength / 256;
    leader[85] = Filelength % 256;

    for (i=0; i< 86; i++ )
    {
    for (j=0; j<8; j++)
    group[j] = image[ 8 * i + j]; // Copy groups of 8 bytes from image to 'group'

    byteToGroup( group, leader); // embed 1 byte of leader for hidden file in group

    for (j=0; j<8; j++)
    image[ 8 * i + j] = group[j]; // and then copy them back.
    }
    // At this stage the leader has been embedded in the image.
    // You now have to do the same with the actual file.

    }


    //===== byteToGroup ======================================================================
    //# Takes as input an 8 byte array of unsigned char (group) , and an unsigned char (byte).
    //# Replaces the least significant bit of each element of group with a bit from char.
    //========================================================================================
    void byteToGroup (unsigned char group[8], unsigned char byte)
    {
    // variables
    int i;
    unsigned char Byte;

    // code

    Byte = byte;

    for (i=7; i>=0; i--)
    {
    group = 2 * ( group/2 ); // Make sure that group is even, i.e. the least significant bit=0
    group = group + Byte % 2; // Add the last bit from Byte
    Byte = Byte / 2; // Divide by 2 so that the next time round we look at the next bit
    }

    }

    //########################################################################################
    //# Common
    //########################################################################################

    //### This function is complete. #############################
    //===== askAndOpen =======================================================================
    //# prints the message message[] to the screen and expects the user to input a filename.
    //# Continues to ask for filename until file is successfully opened.
    //# Returns file handle of file.
    //# User may quit program by entering * instead of filename.
    //========================================================================================
    FILE* askAndOpen(char message[], char mode)
    {
    // variables
    char filename[80];
    char modestring[3] = {'x','b',0};
    FILE* filef;

    // code
    modestring[0] = mode;

    do
    {
    printf("=================================================================\n");
    printf("%s",message);
    fgets(filename,80,stdin); // read name of file from keyboard
    if (filename[0]=='*') exit (0); // check that its not a '*'
    filename[strlen(filename)-1] = 0; // remove newline character
    }
    while (NULL == (filef = fopen(filename,modestring)));

    return (filef);
    }

    //### This function is complete. #############################
    //===== BMPheaders =======================================================================
    //# Reads in info and header sections of file, and returns image Size
    //# header[] and info[] are filled with data from header and info section of file.
    //========================================================================================
    int BMPheaders( FILE* image_in, unsigned char header[], unsigned char info[] )
    {
    if ( 14 != fread( header, sizeof(char), 14, image_in)) exit (1); // Header just contains file size and bmp identifier
    if ( 40 != fread( info, sizeof(char), 40, image_in)) exit (2); // Info section contains data about image size and bpp
    if ( (header[0] != 'B') || (header[1] != 'M') ) exit (3); // First two chars in header should be BM
    if ( 24 != info[14] ) exit (4); // Can only handle 24 bit bmps

    return ( info[20] + (info[21] + (info[22] + info[23] * 256 ) * 256 ) * 256); // Size of image section of file in bytes
    }


Comments

  • Registered Users Posts: 650 ✭✭✭Freddio


    I remember getting a bollocking from the boards people for helping people with their homework.

    At the time they deleted my post but not the person asking the question.

    I wonder how they'll view this


  • Registered Users Posts: 300 ✭✭Tomas_S


    Freddio wrote: »
    I remember getting a bollocking from the boards people for helping people with their homework.

    At the time they deleted my post but not the person asking the question.

    I wonder how they'll view this

    I hope there will be no trouble


  • Registered Users Posts: 40,038 ✭✭✭✭Sparks


    Tomas_S wrote: »
    Hi, if someone could help finishing 2 functions in this code would be greatfull.
    Hi, if you could read the charter, I'd be grateful too.


This discussion has been closed.
Advertisement