File Embedder in C

Some times it is necessary for our compiled project to have it’s supporting files embedded within the EXE module itself so that the supporting files may not be put into a seperate folder and carried along with the project. So here I am presenting you with the source code of the FILE EMBEDDER UTILITY project.
This utility can be used to embed one file with in the other. ie: Suppose we need to embed a .bat file(or any other file *.exe,*bmp,.txt…..) into our final project so that the batch file is present with in the compiled module and is hidden from the users avoiding tthe need to carry the .bat file every time with the project.
Both the Embedding and extraction process has been presented in seperate functions for your convenience. Here’s the code…..

#include<stdio.h>
#include<conio.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<stdlib.h>
#include<string.h>
void embed(void);
void extract(void);
char buff[1],sname[128],tname[128],dname[128],choice;
unsigned long int size=0;long int psize=0;int outh,bytes=0;
FILE *source,*target,*data;
void main()
{
while(1)
{
clrscr();
puts(“nntttFILE EMBEDDING UTILITY BY SRIKANTHnnn”);
puts(“1.Embed A File 2. Extract A File 3.Exitn”);
choice=getch();
switch(choice)
{
case ’1?:embed();
getch();
break;
case ’2?:
extract();
getch();
break;
default:
exit(0);
}
}
}
void embed()
{
puts(“nEnter The Source Filenamen”);
scanf(“%s”,sname);
source=fopen(sname,”rb+”);
if(source==NULL)
{
puts(“nCannot Open The Source Filen”);
return;
}
puts(“nEnter The Target Filenamen”);
scanf(“%s”,tname);
outh=open(tname,O_RDONLYO_BINARY);
if(outh==-1)
{
puts(“nCannot Open The Target Filen”);
return;
}
printf(“nReading The Source File Please Wait…n”);
while((bytes=read(outh,buff,1))>0)
size+=bytes;
data=fopen(“Data.cfg”,”w”);
if(data==NULL)
{
puts(“nCannot Create Configuration The Filen”);
return;
}
fprintf(data,”%lu”,size);
close(outh);
fclose(data);
target=fopen(tname,”rb”);
if(target==NULL)
{
puts(“Cannot Open Target Filen”);
return;
}
printf(“nEmbedding Please Wait…n”);
fseek(source,0,SEEK_END);
while(fread(buff,1,1,target)>0)
fwrite(buff,1,1,source);
fcloseall();
printf(“nEmbedding Completed Successfullyn”);
}
void extract()
{
printf(“nEnter The Source Filenamen”);
scanf(“%s”,sname);
source=fopen(sname,”rb”);
if(source==NULL)
{
printf(“nCannot Open The Source Filen”);
return;
}
printf(“nEnter The Target Filename(eg: abc.exe)n”);
scanf(“%s”,tname);
printf(“nEnter The Configuration Filename(eg: DATA.cfg)n”);
scanf(“%s”,dname);
data=fopen(dname,”r”);
if(data==NULL)
{
printf(“nConfiguration File Not Foundn”);
return;
}
fscanf(data,”%ld”,&psize);
target=fopen(tname,”wb”);
if(target==NULL)
{
puts(“nCannot Open The Target Filen”);
return;
}
printf(“nExtracting Please Wait…n”);
fseek(source,-psize,SEEK_END);
while((fread(buff,1,1,source))>0)
fwrite(buff,1,1,target);
printf(“nFile Extraction Completed Successfullyn”);
fcloseall();
}
Reactions:

Post a Comment

Locate IP Address

IP
Want This

Widgets

 
Top