Segmentation fault in reading a .dat file - c

Right now, I'm working on an example code that I wish to integrate into my program later. What essentially I'm trying to do is read a .dat file byte by byte and interpret the data (ie. interpret boot sector to output the sector size, reserved sectors etc.)
To do this, I am reading the data byte by byte and, using the descriptions in fat12 of https://www.win.tue.nl/~aeb/linux/fs/fat/fat-1.html#ss1.3 , I translate the data into the information that I want. Right now, I can pull individual bytes from the file (Is it right to assume that the data pulled is in hex?). However, I need two bytes to have something meaningful. So, I need to combine two bytes into one, convert the hex data into decimal and output the information. Unfortunately, right now, I'm getting a seg fault and for the life of me, I can't figure out what's wrong. Thanks in advance!
int main (int argc, char **argv){
FILE *fp ,*fptest;
long lSize;
char *buffer;
//Open file
fptest= open("fat_volume.dat", "rb");
//Read file into buffer
fread(buffer,1,512,fptest);
//Parse the boot sector
char tmpA, tmpB;
tmpA = buffer[10]; //First byte
tmpB = buffer[11]; //Second byte
//Combine the two bytes into one
char combinedBytes[3];
strcpy (combinedBytes, tmpA);
strcat (combinedBytes, tmpB);
//Hex to decimal converter
long int li1;
li1 = strtol (combinedBytes,NULL,16);
printf ("The sector size is: %ld.\n", li1);
return 0;
}

You must allocate buffer; e.g.
char buffer[512];
or
char *buffer = malloc(512);
EDIT:
The string operations
strcpy (combinedBytes, tmpA);
strcat (combinedBytes, tmpB);
do not make sense either and access/copy too much data (the compiler will warn you about this!).
I suggest do read values as
unsigned char tmpA = buffer[10];
unsigned char tmpB = buffer[11];
unsigned int tmp = (tmpA << 8) | (tmpB << 0); /* or revert in in case of
little-endian */
To make things more efficient, I would write it as
struct fat_header {
uint8_t pad0[10];
uint16_t my_val;
uint8_t pad1[500];
} __attribute__((__packed__)); /* this is not portable and for gcc! */
...
struct fat_header hdr;
fread(&hdr, 1, sizeof hdr, f);
uint16_t val = be16toh(hdr.my_val); /* or le16toh() in case of le */

You are reading into a buffer you never allocated memory for.
What you're trying now is to read from some junk value in memory, who knows, which almost always leads to a segmentation fault.
Use:
char *buffer = malloc(512 * sizeof(char)); // this allocates 512 times the size of a single char of memory
If you don't specify the number inside malloc to be of a specific size (e.g. malloc(512) the number is in bytes, though I think it's better to always include it.
This specific error is called dereferencing a null pointer
EDIT:
I've managed to run this code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char **argv)
{
FILE *fp ,*fptest;
long lSize;
char *buffer;
//Open file
fptest = fopen("input.txt", "rb");
if (fptest == NULL)
{
printf("Error occurred when opening file");
return 1;
}
buffer = malloc(sizeof(char) * 512);
//Read file into buffer
fread(buffer,1,512,fptest);
//Parse the boot sector
char tmpA, tmpB;
tmpA = buffer[10]; //First byte
tmpB = buffer[11]; //Second byte
//Combine the two bytes into one
char combinedBytes[3];
strcpy (combinedBytes, &tmpA);
strcat (combinedBytes, &tmpB);
//Hex to decimal converter
long int li1;
li1 = strtol (combinedBytes,NULL,16);
printf ("The sector size is: %ld.\n", li1);
return 0;
}
You also used a function open() which must be fopen(), and you need to pass the address of tmpA and tmpB to strcpy and strcat.
This is why I don't understand why your compiler doesn't give any errors or warnings..

Related

How to import information from PE_file as binary in C

I'm making PE file format analyzer in C language.
When I used Visual Studio, I could make it much easier.
BUT, I have tired to make it without Visual Studio to improve my C programing.
It was made by GCC(My laptop is macbook).
To analyze PE file format, as you know I have to use file pointer and read file as 'rb' type.
I did it and it looks like worked well.... I wanted to print first and second words("MZ") but It printed unkown(to me) numbers.
P.S I write little different code refer to previous one to smaller than before. But It print NULL.
It means that I wrote code really wrong... PLZ tell me which parts are wrong
#include <stdio.h>
#include <stdlib.h>
// struct to save info of PE_File format
typedef struct _IMAGE_DOS_HEADER
{
unsigned short e_magic;
unsigned short e_cblp;
unsigned short e_cp;
unsigned short e_crlc;
unsigned short e_cparhdr;
unsigned short e_minalloc;
unsigned short e_maxalloc;
unsigned short e_ss;
unsigned short e_sp;
unsigned short e_csum;
unsigned short e_ip;
unsigned short e_cs;
unsigned short e_lfarlc;
unsigned short e_ovno;
unsigned short e_res[4];
unsigned short e_oemid;
unsigned short e_oeminfo;
unsigned short e_res2[10];
unsigned long e_lfanew;
} DOS_HEADER;
// I use two different functions.
// First one was made to import PE File format info from file that I used.
// Another was made to print some imformation. I use some comment because I didn't realize those.
DOS_HEADER get_dos_header(FILE *fp, char* fp_buffer);
void print_data_of_structures(DOS_HEADER/*, NT_HEADER, FILE_HEADER, OPTIONAL_HEADER, DATA_DIRECTORY*/);
int main(void)
{
DOS_HEADER dos_header;
char file_path[1000];
printf("Please input your file path : ");
scanf("%[^\n]s", file_path);
FILE* fp = NULL;
fp = fopen(file_path, "rb");
if(fp == NULL)
printf("Unalbe to open file :/\n");
// To calculate file size.
long fp_size = 0;
fseek(fp, 0, SEEK_END);
fp_size = ftell(fp);
rewind(fp);
// Declare to use as buffer
char* fp_buffer = malloc(sizeof(char) * (fp_size + 1));
// Use function to save analyzed information.
DOS_HEADER info_dos_header = get_dos_header(fp, fp_buffer);
// Print information.
print_data_of_structures(info_dos_header);
fclose(fp);
free(fp_buffer);
return 0;
}
DOS_HEADER get_dos_header(FILE* fp, char* fp_buffer)
{
DOS_HEADER raw_info_dos_header = {0, };
// Read hex info from fp 2 byte 1 time and save at fp_buffer.
fseek(fp, 0, SEEK_SET);
fread(fp_buffer, 2, 1, fp);
raw_info_dos_header.e_magic = fp_buffer;
return raw_info_dos_header;
}
void print_data_of_structures(DOS_HEADER info_dos_header/*, NT_HEADER info_nt_header, FILE_HEADER info_file_header, OPTIONAL_HEADER info_optional_header, DATA_DIRECTORY info_data_directory*/)
{
// print info_dos_header
printf("Print DOS_HEADER\n");
// I tried several print format(for example %hu, %x, %d) But I coudn't "MZ" or any numbers has realation with "MZ".
printf("%s", info_dos_header.e_magic);
}
You have 2 big problems with your code: you are mixing types.
In DOS_HEADER all members (except the last one) are of type unsigned short.
That's important.
In get_dos_header you are doing
fread(fp_buffer, 2, 1, fp);
raw_info_dos_header.e_magic = fp_buffer;
which is wrong.
Don't use magic numbers, use sizeof to get the correct sizes
Check the return value of fread, specially if you are analyzing a binary
format, you must be sure that you've read exactly what you're expecting.
It's much easier to deal with fread when the size argument is 1, because
only when the size argument is 1 the return value of fread matches the
number of bytes read.
e_magic is a unsigned short, fp_buffer is a char*. This assignment is
not copying the contents pointed to by fp_buffer, you are storing an address
as if it were a unsigned short value. You need to copy the memory pointed to
by fp_buffer.
Also I don't see the point of you allocating a buffer of the size of the file
when you are reading chunk by chunk. It would be easier if you don't allocate a
buffer at all and use an array of a size larger than what you are going to read.
int get_dos_header(FILE* fp, DOS_HEADER *dos)
{
unsigned char buffer[8];
size_t ret = fread(buffer, 1, sizeof(dos->e_magic), fp);
if(ret != sizeof(dos->e_magic))
{
fprintf(stderr, "Invalid file size\n");
return 0;
}
memcpy(&dos->e_magic, buffer, sizeof(dos->e_magic));
// DO the others fread operation
...
return 1;
}
Then in main you can do this:
DOS_HEADER raw_info_dos_header = {0, };
if(get_dos_header(fp, &raw_info_dos_header) == 0)
{
fprintf(stderr, "failed to get the DOS header\n");
return 1;
}
...
The second problem is in print_data_of_structures:
printf("%s", info_dos_header.e_magic);
e_magic is a unsigned short, %s expects a pointer to char, it expects a
string. e_magic is definitively not a string. This yield undefined behaviour.
The printf should look like this:
printf("e_magic: %hu\n", info_dos_header.e_magic);
or
printf("e_magic: 0x%04hx\n", info_dos_header.e_magic);
to print it in hexadecimal format.
I also advice you to use pointers instead. Your struct is big and passing a
pointer to a function is much cheaper than passing a copy of a large struct to a
functions. So instead of
void print_data_of_structures(DOS_HEADER info_dos_header);
declare it as
void print_data_of_structures(DOS_HEADER *info_dos_header);
And also don't declare function that can fail (like get_dos_header) as
void, otherwise how do you tell the calling function that it failed? It's
better for such functions to return an int (1 for success, 0 for failure) and
expect a pointer to the struct where the information is stored (see how I
changed get_dos_header above.

How to make a void pointer to read a given part of a binary file

I have a binary file which contains 3 differents structs and a christmas text. On the first line of the binaryfile have they provided me with a int which represents the size of a package inside the file. A package contains 3 structs ,the chistmastext and the size.
The structs lies in a file called framehdr.h and the binary file I'm reading is called TCPdump.
Now am I trying to create a program att will read each package at a time and then withdraw the text.
I have started with something like this:
#pragma warning(disable: 4996)
#include <stdio.h>
#include <stdlib.h>
#include "framehdr.h"
#include <crtdbg.h>
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
FILE *fileOpen;
char *buffer;
size_t dataInFile;
long filesize;
// The three structs
struct ethernet_hdr ethHdr;
struct ip_hdr ipHdr;
struct tcp_hdr tcpHDr;
fileOpen = fopen("C:\\Users\\Viktor\\source\\repos\\Laboration_3\\Laboration_3\\TCPdump", "rb");
if (fileOpen == NULL)
{
printf("Error\n");
}
else
{
printf("Success\n");
}
char lenOf[10];
size_t nr;
// Reads until \n comes
fgets(lenOf, sizeof(lenOf), fileOpen);
sscanf(lenOf, "%d", &nr);
// Withdraw the size of a package and check if it's correct
printf("Value: %d\n", nr);
printf("Adress: %d\n", &nr);
void *ptr;
fread(&ptr, nr, 1, fileOpen);
int resEth = 14;
printf("resEth: %d\n", resEth);
int resIP = IP_HL((struct ip_hdr*)ptr);
printf("ResIP: %d\n", resIP);
int resTcp = TH_OFF((struct tcp_hdr*)ptr);
printf("tcpIP: %d\n", resTcp);
int res = resEth + resIP + resTcp;
printf("Total: %d", res);
fclose(fileOpen);
//free(buffer);
system("pause");
return 0;
}
I know that the first struct ethernet will always have the size of 14 but I need to get the size of the other 2 and I'm suppose to use IP_HL and TH_OFF for that.
But my problems lies in that I can't seem to read the entire package to one
void * with the fread. I get noting in my *ptr.
Which in turn makes the code break when I try to convert the void * to one of the structs ones.
What I'm doing wrong with the void *?
Two problems:
First you should not really use text functions when reading binary files. Binary files doesn't really have "lines" in the sense that text file have it.
Secondly, with
void *ptr;
fread(&ptr, nr, 1, fileOpen);
you are passing a pointer to the pointer variable, you don't actually read anything into memory and then make ptr point to that memory. What happens now is that the fread function will read nr bytes from the file, and then write it to the memory pointed to by &ptr, which will lead to undefined behavior if nr > sizeof ptr (as then the data will be written out of bounds).
You have to allocate nr bytes of memory, and then pass a pointer to the first element of that:
char data[nr];
fread(data, nr, 1, fileOpen);
You should also get into the habit of checking for errors. What if the fread function fails? Or the file is truncated and there isn't nr bytes left to read?
You can check for these conditions by checking what fread returns.
And not only check for fread, there are more functions than fopen that can fail.

How does one print a struct member after reading from binary file (using ab+) in C

Hello I am trying to write code to save a struct to a binary file then print it after reading from file. When I run my code I get a segmentation fault (11).
this is my code (sorry for all the printfs, I was trying to find where the code went wrong)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
struct contact {
int unqID;
char *name;
char *relationship;
char *phone_number;
char *email;
};
FILE *file;
file = fopen("test.bin", "ab+");
struct contact conwrite;
conwrite.unqID = 0;
conwrite.name = "president";
conwrite.relationship = "";
conwrite.phone_number = "";
conwrite.email = "";
struct contact conwrite2;
conwrite2.unqID = 1;
conwrite2.name = "vice president";
conwrite2.relationship = "";
conwrite2.phone_number = "";
conwrite2.email = "";
printf("%s\n", conwrite.name);
printf("%s\n", conwrite2.name);
fwrite(&conwrite, sizeof(conwrite), 1, file);
fwrite(&conwrite2, sizeof(conwrite2), 1, file);
fclose(file);
struct contact phonebook[100];
file = fopen("test.bin", "rb");
printf("size of file: %lu\n", sizeof(file));
printf("size of contact: %lu\n", sizeof(struct contact));
int size = (sizeof(file)) / (sizeof(struct contact));
printf("size of file: %d\n", size);
int read = fread(phonebook, sizeof(conwrite), 2, file);
printf("read: %d\n", read);
if (phonebook[0].name == NULL)
printf("phonebook is empty\n");
else
printf("phonebook is filled\n");
printf("%s\n", phonebook[0].name);
fclose(file);
return 0;
}
You cannot compute the length of the file with the sizeof operator:
printf("size of file: %lu\n", sizeof(file));
Will print the size of a FILE *, the number of byte in the pointer itself. Note that sizeof evaluates to a value of type size_t for which the proper printf format is %zu. If for some reason you cannot use the z option, cast the sizeof as (unsigned long) to use %lu but be aware that size_t is actually larger than unsigned long on some platforms such a Windows 64 bits.
To compute the length of the file on disk, you can use a system call such as stat or possibly ftell() after seeking to the end of the stream with fseek(file, 0L, SEEK_END);. Note that this method only works for tiles open in binary mode. In your program, you do open it in binary mode, but also in update (the +), which seems unnecessary.
Finally, you cannot store the information pointed to by these structures with your approach: the strings pointed to by the struct members are in memory, you are just writing the values of the pointers, which will not be meaningful when read back by a different program or even a different instance of the same program. You need to make these strings arrays of char, as for example:
struct contact {
int unqID;
char name[64];
char relationship[32];
char phone_number[16];
char email[64];
};
You will need to update your code to account for the difference in types. Note that this approach introduces a limit on the size of all textual member. Storing the information in disk files is usually not done this way. Either use specialized libraries that implement databases, or use an interchange format such as csv, json, xml... by increasing order of complexity and versatility, or a custom text based format.

What to use instead of sizeof(void)?

I'm trying to copy a file. I'm using a borrowed code snippet, and there's a line of it which errors which confuses me.
int fileread = open("original.txt", O_RDONLY);
void *buffer;
buffer = malloc(sizeof(void) * size); /*This line gives "Incomplete type not allowed."*/
int nread = read(fileread,buffer,size);
int filewrite = open("original.txt.backup",O_CREAT | O_RDWR, 0644);
write(filewrite,buffer,size);
close(filewrite);
close(fileread);
What should I be using instead? I was thinking char*, but I want to make sure I'm understanding the process going on here.
If you want to allocate a buffer of size bytes, and have buffer point to the beginning of it:
void *buffer = malloc(size);
if (buffer == NULL) {
/* allocation failed */
}
/* ... */
But if you want to do something with the data in the buffer, it will need to have a valid type. An array of unsigned char is a common way to manage buffers of arbitrary contents:
unsigned char *buffer = malloc(size);
/* as above */
sizeof() returns the size of your type. Honestly I think you should just change it to
char *buffer;
buffer = malloc(sizeof(char) * size);
Sizeof(void) makes zero sense

unhandled exception while running the program in VC++ 2008

I was trying to open a binary file in the directory and perform the operations accordingly.I doubt this line..is not the correct way of representation to work the below logic.
int main(int argc, char* argv[])
{
int k=0;
FILE *fp;
unsigned char cd[255];
unsigned long cd1[500];
char *buffer;
unsigned long sa=80044;
int j=0,i=0,n=0;
DIR *dir;
struct dirent *direntry; //could be a file, or a directory
dir = opendir("C:/Documents and Settings/Administrator/Desktop/vicky");
if(!dir) {
printf("Error: directory did not open!\n");
return 1;
}
while((direntry=readdir(dir))!=NULL) {
if(++k < 100)
{
printf("%s\n",direntry->d_name);
sprintf(buffer,"%s",direntry->d_name);//here i got the unhanded exception.
fp=fopen("buffer","rb");
sa=sa-44;
sa=sa/8;
if(fp==NULL)
{
printf("file not found!");
}
else
{
for(j=0;j<(sa);j++)
{
for(i=0;i<8;i++)
{
cd[i]=fgetc(fp);//get each character from file
// printf("%c",cd[i]);
}
if(i==8)//if the i is 8 the character then do the following,just read 8 bytes and then calculate the cd1.
{
sa=sa-8;
cd1[j]=(cd[6] * 65536 + cd[5] * 256 + cd[4]);//multiply the positional weightage and calculate the cd1,which contains the 7 digits decimal value.
//if((cd1[j]> 0x7FFFFF)&&(cd1[j]<=0xFFFFFF))
//cd1[j]=cd1[j]- 0xFFFFFF;
//cd1[j]=cd1[i] * -1;
}
printf("completes first 8 bytes read:%d - %d",j,cd1[j]);//print j and cd1[j] value in console window
}
fclose(fp);//close the file
}
}
if((strcmp(direntry->d_name, "text.txt"))==0) {
printf("\nThe %s file has been found\n",direntry->d_name);
k=-99; //just a flag value to show the file was found
break;
}
}
if(k!=-99)
printf("\nThe test.txt file was not found\n");
closedir(dir);
printf("\n");
getchar();
return 0;
}
This is error i got:Unhandled exception at 0x1029a189 (msvcr90d.dll) in READ_TEXT.exe: 0xC0000005: Access violation writing location 0xcccccccc.Kindly let me any suggestion to read the "direntry->d_name" file name to process the above logic.
Make the character buffer an ordinary array, for simplicity's sake:
char buffer[1024];
That way you're sure to have the memory put aside and ready to use. You should use snprintf() if your platform provides it.
buffer is not allocated. sprintf() is basically attempting to write "nowhere".
The destination variable needs to be either allocated on the heap using malloc or on the stack restricting its use to the current scope.
You have only declared char* buffer, not allocate memory for it using malloc().
Your problem is an uninitialized pointer.
Initialize the pointer with malloc or try an array
char buffer[1024];
change
char * buffer;
to
char buffer[MAX_PATH];
or something similar.
U must allocate the memory for the buffer variable.
char *buffer; statement is not allocating any memory so that i could have
any data.
so better u try
chat buffer[//buffer length];
or char *buffer = new char(//buffer length);
or char buffer = (char)malloc(//buffer length);
these above statement will allocate memory and fix your issue.

Resources