Hi,
I have been struggling with this now for a couple weeks. I am writing a program that looks for headers of files. Upon finding one, it looks in the header for how large the program is. The code below is a subset of the program I am writing. I already know where the header is, but when I try to read in the bytes I get some weird output.
The output I get is (Notice the Hex:FFFFFF80):
Starting File Read -- findenddatorg
Hex:00 Hex:FFFFFF80 Hex:00 Hex:00 Filename: (null) Startpos: 26215452 Endpos: 0 Size: 0 -- Filelength: 140736547508496
It should be:
Starting File Read -- findenddatorg
Hex:00 Hex:80 Hex:00 Hex:00 Filename: (null) Startpos: 26215452 Endpos: 0 Size: 0 -- Filelength: 140736547508496
Why in the world do I have a FFFFFF80? If I adjust this line:
startpos = startpos+28;
to
startpos = startpos+29;
I get:
Starting File Read -- findenddatorg
Hex:FFFFFF80 Hex:00 Hex:00 Hex:00 Filename: (null) Startpos: 26215453 Endpos: 0 Size: 0 -- Filelength: 140736975497536
If I change to: startpos = startpos+30;
Starting File Read -- findenddatorg
Hex:00 Hex:00 Hex:00 Hex:40 Filename: (null) Startpos: 26215454 Endpos: 0 Size: 0 -- Filelength: 140734021634096
Any ideas??
Below is my code:
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
// My headers
#include "main.h"
char *filename = NULL;
char *programname = NULL;
int fd; //File
FILE *logfile;
FILE *foundfile;
int row,col; // Curses row and col
char *configfilename = NULL;
char *logfilename = NULL;
off_t findenddatorg(off_t startpos, off_t endpos, unsigned long long size) {
//off_t getsize,getsizefinal = 0;
off_t getsizefinal = 0;
char getsize[] = "AAAA";
char getsize2[] ="AAAA";
char blowme[]="AAAA";
int *getsize_ptr;
char out[12];
int fileend = 0;
long fileend2,testlong = 0;
int out_file = 0;
int read_size = 0;
int dat_length = 0;
off_t currpos;
int hexcnt = 0;
unsigned long long filelength = 0;
unsigned long long fileleft = 0;
char holda,holdb;
startpos = startpos+28;
currpos = lseek(fd, startpos, SEEK_SET);
getsize_ptr = &getsize;
if (currpos == -1)
fprintf(stderr, "Could not seek ending position of -- Startpos: %lld -- Endpos: %lld -- Size: %lld -- Filename: %s\n", startpos,endpos,size,filename);
else {
printf("Starting File Read -- findenddatorg\n");
read_size = read(fd, getsize, sizeof(getsize));
if ( read_size < 0 ) {
exit(8);
}
for (hexcnt = 0; hexcnt < 4; hexcnt++)
printf("Hex:%02X ", getsize[hexcnt]);
printf("Filename: %s \t Startpos: %lld \t Endpos: %lld \t Size: %lld -- Filelength: %lld \n\n",filename, startpos,endpos,size, getsize);
return(getsizefinal);
}
}
int main (int argc, char **argv) {
off_t currpos;
long saveline;
fd = open("test_small.dd", O_RDONLY|O_LARGEFILE);
//fd = open(filename, O_RDONLY|O_BINARY|O_LARGEFILE);
if (fd == -1) {
fprintf(stderr, "Error opening file: %s\n", argv[optind]);
exit(1);
}
currpos = lseek(fd, 0, SEEK_CUR);
saveline = findenddatorg(26215424, 0, 0);
return 0;
}
Thanks,
CM