Hint: The file lab5-src/hintdump.c includes the barebones of the memdump function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include lt;stdio.hgt; #include lt;string.hgt; #include lt;stdlib.hgt;
void mymemdump(FILE * fd, char * p , int len) #123; int i; fprintf(fd, "0x%016lX: ", (unsigned long) p);
for (i=0; i lt; len; i++) #123; int c = p[i]amp;0xFF;
fprintf(fd, "%02X ", c);
fprintf(fd, "%c ", (cgt;=32)?c:'.');
if (i % 16 == 0 ) #123; fprintf(fd,"\n"); #125; #125; #125;
int main() #123; char a[30]; int x; x = 5; strcpy(a,"Hello world\n"); mymemdump(stdout,(char*) amp;x, 64); #125;
|
Step 4. Implement String Functions Using Pointers
In the file mystring.c implement the string functions indicated using pointers. Do not use predefined string functions such as strlen, strcpy etc. You have to write your own. Also, do not use the array operator [ ] and use pointers instead. Run the testall script to verify that your implementation is correct.
mystring.c:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include lt;stdlib.hgt; #include "mystring.h"
int mystrlen(char * s) #123; return 0; #125;
char * mystrcpy(char * dest, char * src) #123; return NULL; #125;
char * mystrcat(char * dest, char * src) #123; return NULL; #125;
int mystrcmp(char * s1, char * s2) #123; return -1; #125;
char * mystrstr(char * hay, char * needle) #123; return NULL; #125;
char * mystrdup(char * s) #123; return NULL; #125;
char * mymemcpy(char * dest, char * src, int n) #123; return NULL; #125;
|
Step 5. Implementing Array Functions
Implement the array operations indicated in the file array.c Do not use “[ ]” and instead use pointers. Run the testall script to verify that they are correct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include lt;stdio.hgt; #include "array.h"
double sumArray(int n, double * array) #123; double sum = 0; double * p = array; double * pend = p+n;
while (p lt; pend) #123; sum += *p; p++; #125;
return sum; #125;
double maxArray(int n, double * array) #123; return 0; #125;
double minArray(int n, double * array) #123; return 0; #125;
int findArray(int n, double * array, double min, double max) #123; return -1; #125;
int sortArray(int n, double * array) #123; #125;
void printArray(int n, double * array) #123; #125;
|
Step 6: File Dump
Using your implementation of MemoryDump in Step 3 write a program:
myfiledump file [maxbytes]
That prints a byte dump similar to MemoryDump but using a file insteqad of memory.
Where “file” is the name of the file to print. The parameter “maxbytes” is optional and indicates the number of bytes to print. If “maxbytes” does not exist, then it will print the entire file.
Write your implementation in the file myfiledump.c.
We give you the file ./myfiledump.org that is our solution to this program so you can compare with.
For example:
cs240@data ~/lab5-ptr-memdump/lab5-src $ ./myfiledump.org mem.c
0x0000000000000000: 0A 23 69 6E 63 6C 75 64 65 20 3C 73 74 64 69 6F .#include lt;stdio
0x0000000000000010: 2E 68 3E 0A 23 69 6E 63 6C 75 64 65 20 3C 73 74 .hgt;.#include lt;st
0x0000000000000020: 72 69 6E 67 2E 68 3E 0A 23 69 6E 63 6C 75 64 65 ring.hgt;.#include
0x0000000000000030: 20 3C 73 74 64 6C 69 62 2E 68 3E 0A 0A 76 6F 69 lt;stdlib.hgt;..voi
0x0000000000000040: 64 20 6D 79 6D 65 6D 64 75 6D 70 28 46 49 4C 45 d mymemdump(FILE
0x0000000000000050: 20 2A 66 64 2C 20 63 68 61 72 20 2A 20 70 20 2C *fd, char * p ,
0x0000000000000060: 20 69 6E 74 20 6C 65 6E 29 3B 0A 0A 73 74 72 75 int len);..stru
Or
cs240@data ~/lab5-ptr-memdump/lab5-src $ ./myfiledump.org myfiledump.org 100
0x0000000000000000: 7F 45 4C 46 02 01 01 00 00 00 00 00 00 00 00 00 .ELF............
0x0000000000000010: 02 00 3E 00 01 00 00 00 E0 06 40 00 00 00 00 00 ..gt;.......@.....
0x0000000000000020: 40 00 00 00 00 00 00 00 88 23 00 00 00 00 00 00 @........#......
0x0000000000000030: 00 00 00 00 40 00 38 00 0A 00 40 00 22 00 1F 00 ....@.8...@.quot;...
0x0000000000000040: 06 00 00 00 05 00 00 00 40 00 00 00 00 00 00 00 ........@.......
0x0000000000000050: 40 00 40 00 00 00 00 00 40 00 40 00 00 00 00 00 @.@.....@.@.....
0x0000000000000060: 30 02 00 00 0...
Hints:
Use FILE * fin = fopen(...) to open the file passes as argument. See “man fopen”
You can determine the length of the file by using the following sequence. Fine :
1 2 3 4
|
fseek(fin, 0L, SEEK_END); int fileSize = ftell(fin); fseek(fin, 0L, SEEK_SET);
|
Allocate a large chunk of size fileSize bytes using malloc and read the whole file into it using fread.
Copy your implementation of memoryDump into myfiledump.c and modify to print using offset 0 as the beginning.
Step 7. Determine the Signature of Special Files
The first few bytes of an executable are called “The Magic Number” or “Signature
and are used by different programs to identify the type of a file. Using your myfiledump tool find the signature for the following files as well as the extension used. Sometimes the signature shows later in the file at some known “Offset” but most of the time appears at the beginning (
| File Type |
Extension |
Signature |
Signature Offset |
| Executable file |
|
|
| WAV file |
|
|
| JPEG |
|
|
| GIF |
|
|
| TAR |
|
|
| ZIP |
|
Write this table in a file signature.txt and include it in lab5-src/signature.txt
Step 8. Our Tests
Also, make sure that your program passes our tests. To run the tests, compile your program and type
./testall
or run each individual test as indicated in the output.
Make sure that all tests pass and testall gives the maximum points.
Step 9. Turning In your Project
Follow these instructions to turnin lab5:
cd cs240
turnin -c cs240 -v -p lab5 lab5-src