Assignment #6 – (100 points) Solution




• Answer the questions individually. Group effort is not allowed.

• Solutions must be committed to your respective repositories on github.

• For this assignment, you are not allowed to use any library functions other than printf.

• Ensure that your code runs on

• Prototypes must be provided for all functions within the header file define.h.

• Code must be appropriately commented.

• Useful resources:

– Common linux commands:



1.(100points)Programsoperateonfileswithspecificformat.Forexample,Linux executesELFbinaries.Microsoft Windows executesPEbinaries.ELFisafile formatthattellstheloaderwhereandwhattofindinsidethefile. Similarly, awordprocessorcanopen.docfiles,,.gz,etc. Inordertoaidindetectingifafile isofaparticulartype,asignatureisem- beddedwithinthefile toindicatethetypeofafile.Often,thesignatureisthe firstfewbyteswithinafile.Thiswillenableanapplicationtoquicklyidentify ifitcanoperateonthefile. Inthisproblem,youwillimplementaprogram filerecognizerthatacceptsafileascommandlineinput,andprintsthe


Table1:Signaturetotypemapping.Allsignaturesstartfromthebeginningofthefile. This isonly asamplefortheassignment.For curiousminds,morecanbefoundhere:

Signature (first few bytes in Hex) File type Expected output

47 49 46 38 39 61 or 47 49 46 38 37 61 GIF Image This is a GIF file.

7f 45 4C 46 ELF file This is an ELF file.

25 50 44 46 PDF file This is a PDF file.

50 4B ZIP file This is a ZIP file.

CA FE BA BE Java class file This is a Java class file.

89 50 4E 47 0D 0A 1A 0A PNG file This is a PNG file.

Anything else Unknown file File type unknown.

format of the file and the program that can open it. NOTE: Although file exten- sions can sometimes tell what format a file is, it is an unreliable indicator. The file signature is the true indicator of a file type.

2.Youwillimplementfilerecognizer.candfilerecognizer.hthat containsallyourimplementation. YouwillwriteaMakefilethatgenerates filerecognizerexecutable.Yourprogrammustacceptexactlyonecom- mandlineargument(i.e.,file name). Ifmoreorlessthanexpectednumberof argumentsarepassed,youaretoprint”Invalidinput.”and exit.

Useful resources

1. Examine fopen(), fscanf() and fclose() functions to perform file IO.

2. Run the find command (e.g., find / -name “*.png”) on to find files of specific types to test.

Testing your code

In order to test your code, we will run the make command to generate the program. We will then call the program in a loop on a set of known file types, and in each case, we will check if the output is as expected in Table 1. We will also provide invalid number of arguments and non-existent files as arguments. NOTE: The testing process may be automated. It is your responsibility to ensure that the files are named correctly. You are advised to test the programs before submitting. Each valid input file is guaranteed to be at least 10 bytes in size.