Link to home
Start Free TrialLog in
Avatar of jim_welch_se
jim_welch_se

asked on

C or C++ File Extaction Program

Hello All,

Let me start off by saying I am very new to the Linux/Unix platform and I'm trying to come up to speed.  I am currently researching how to create a simple binary program that you can concatinate a file onto another file and then extract the file when run.  Basically I figured out how to implement this using a script but it does not statisfy what I need because as a script a user can modify my script to get the file and also see how I embedded the file.  This is not something i can allow.  So unless there is some way of making my script file unreadable or uneditable it looks like I am going to have to go the binary route.   Below I have included some of my script code that i need to duplicate using C/C++


With my script I basically just concatinate a file(which happens to be a tar file) by doing this:

cat myscript.sh  file.tar.gz > mypackage.sh

The myscript.sh script myscript.sh i wrote looks like this:

#!/bin/sh
echo ""
echo "Package - extracting archive... please wait"
echo ""

SKIP=`awk '/^__ARCHIVE_FOLLOWS__/ { print NR + 1; exit 0; }' $0`

# take the archive portion of this file and pipe it to tar
tail +$SKIP $0 | tar xz


exit 0

__ARCHIVE_FOLLOWS__


Like I previously mentioned I basically need to reproduce this functionality by making a C/C++ implentation of this script.

Since I have little or no experience with programming for this platform I was hoping someone could help me.  Any information you can provide would be greatly appreciated.

Best Regards,
-Jim
Avatar of sunnycoder
sunnycoder
Flag of India image

Hi Jim,

If you plan to use this script for your own machine, change it permissions to be owned by root and allow only execution to all users. Something like passwd.

If you plan to distribute it, then binary approach makes sense. But realize that :
- Linux is primarily thriving on open source. Users, not knowing what you are doing in your program and  easy availablity of several good options is likely to prevent it from being a big hit :)
- If you do make a binary, it is not a big task to reverse engineer a program of that size.

That been said I cannot think of any pretty way to incorporate a binary within a C program binary.What we can do is to generate the C source code on the fly and include the binary within the source code as a char array and then compile this source code to give us a self extracting archive.

Somethin on the lines of:

fp = fopen ( "new_source.c", "w");
fprintf (fp,"#include<stdio.h>\n");
...
fprintf (fp,"char [] file = \"{");
arch_file = fopen (argv[1], "r");
while ( fread, (buffer, 1, BUF_LEN, archfile) != 0 )
      fwrite (buffer, ,1 BUF_LEN, fp);
fprintf ( fp , "};\n");
...
Next comes printing the code in the new_source for extracting the tar file in array file[]
Last comes code to close and compile the newly generated source file.

Messy, but looks like the only way out

cheers
sunnycoder
Avatar of sneeuw_chan
sneeuw_chan

If you'll always be adding tar files, or gzip files, you could of course search for the tar or gzip header, then all the program needs to do is find itself as file.
Which should normally be argv[0], although I don't think you can always depend on that.
So you have a program that has some kind of proprietary file embedded within it, and you are going to distribute this program to hostile users in some kind of way that they can successfully run the program, yet have no way to see the proprietary file?

That is provably impossible, unless you keep the program on a system you control that prevents the user from ever reading the program file.  Once the hostile user can read the program file it will always always always be possible for the user to disassemble, simulate, or debug the program and gain access to your secret file.  You can only make it a little more difficult for them, or only keep the secret from your less able users.
Avatar of jim_welch_se

ASKER

Hello All,

Thank you all for your feed back I really appreciate it.

Maybe I should give some more details about the major issue I am trying to take care of by writing this app.  Basically my program has to prompt a user with a License agreement and if they accept this then I supposed to allow them to get the file.  The script I wrote accomplishes this task but its a bit to modifiable to users.  If a user is wants to get at my file I want to make it somewhat difficult to do so.    I hope that clarify what I am trying to accomplish.

Best Regards,
-Jim

Hi Jim,

the approach I outlined should work in that case.
ASKER CERTIFIED SOLUTION
Avatar of NovaDenizen
NovaDenizen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial