I wanted to try a different approach to PSET4, but not sure if it's stupid or not. It seems to be 'kind of' working so hoping to get someone with more knowledge to help me understand whether this is a path worth pursuing or not.
I'm attempting to read through the entire file and get the memory addresses of the 'JPGHeaders' and store them in an array. My plan after that will be to do some memory address math to find the number of bytes to write and then write the correct number of bytes from each address until the end of the file, looping over that array.
For some reason, matches are getting stored as 'nil' in my array; I thought at first it may be because random bytes within the JPG had that same combo, but when I changed it to jump 512 bytes it does the same thing.
Is this approached incorrect from the beginning, or is there something I don't understand about FILE I/O or matching that's making it incorrect?
Code snippet in question:
FILE* ptr = fopen("card.raw", "r"); // open the file; point to the head to be used to find addresses from the beginning of the file
FILE* matched_address; // create a pointer in the file when an address matches
char *infile = "card.raw";
FILE* readptr = fopen(infile, "r"); // create a pointer to read from the file;
if (readptr == NULL){
printf("Could not open %s.\n", infile);
return 2;
}
FILE* locarray[50]; // initialize an array of memory addresses
int read_index=0, i=0; // will store the addresses based how many 'jpgHead' sized chunks they are away from the beginning of the file
JPGHead jpgMatch;
char s[sizeof(jpgMatch)]; // create an array to store 4 byte sized elements to test if they match a jpg setting; this will be overwritten
// Now for the action: we read through the file 4 bytes at a time, finding the addresses of those that match the JPG signature, and storing those addresses to an array...
while(fgets(s, sizeof(jpgMatch), readptr) != NULL){
if (s[0] == (char)0xff && s[1] == (char)0xd8 && s[2] == (char)0xff && (s[3] <= (char)0xef || s[3] >= (char)0xe0) )
{
matched_address = ptr + (read_index * sizeof(jpgMatch)); // the address will just be the index * the size of chunks we're moving forward away from the head of the file
locarray[i] = matched_address; // store the address in an array; only increase the arrays index when you've matched;
i++;
}
read_index++; //
}
for (int j = 0; j < 50; j++)
{
printf("array element %i is %p \n", j, locarray[j]);
}
return 0;
}
EDIT: Here's the results of stored arrays. Many look like memory addresses but not sure why there are 'nils': array element 0 is 0x7f220959ff48
array element 1 is 0x7f2209388816
array element 2 is (nil)
array element 3 is 0x7ffdb3646540
array element 4 is 0x7f2208cb6d28
array element 5 is 0x7ffdb3646650
array element 6 is 0x7f2208cc31a8
array element 7 is 0x3d8f538
array element 8 is 0x7ffdb3646640
array element 9 is 0x7f22095a2ae8
array element 10 is (nil)
array element 11 is 0x7f220959ff98
array element 12 is 0x7f220959fa10
array element 13 is 0x400411
array element 14 is 0x7f2208cc3d78
array element 15 is 0x400300
array element 16 is 0x100000000
array element 17 is 0x100000830
array element 18 is (nil)
array element 19 is 0x7ffdb3646708
array element 20 is 0x7ffdb36466e0
array element 21 is 0x7f220959ff98
array element 22 is 0x1
array element 23 is 0x7f22095a2520
array element 24 is 0x7f22095a21c8
array element 25 is 0x7f2209388991
array element 26 is (nil)
array element 27 is 0x7f220959ff98
array element 28 is 0x1
array element 29 is (nil)
array element 30 is 0x7ffd00000001
array element 31 is 0x7f22095a21c8
array element 32 is (nil)
array element 33 is (nil)
array element 34 is (nil)
array element 35 is (nil)
array element 36 is (nil)
array element 37 is 0x7f22095a2520
array element 38 is 0x7ffdb3646650
array element 39 is 0x7ffdb3646640
array element 40 is 0xf63d4e2e
array element 41 is 0x400411
array element 42 is 0xffffffff
array element 43 is 0x7ffdb36467a8
array element 44 is 0x7f2208cc31a8
array element 45 is 0x7f220959fa10
array element 46 is 0x7f22095a21c8
array element 47 is (nil)
array element 48 is 0x1
array element 49 is 0x4008ad