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:

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


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,
Who is Participating?

Improve company productivity with a Business Account.Sign Up

NovaDenizenConnect With a Mentor Commented:
If you are in the US, then the DMCA is on your side.

You don't have to do anything heroic.  Just mildly encode your file (add 1 to each character or something similarly simple) and somehow label the data so that it is obvious that it is protected by an access control mechanism, and circumventing the mechanism would be in violation of the DMCA.

Why don't you just put up a website and only serve the file out to people who click through a licensing agreement?
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

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.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
jim_welch_seAuthor Commented:
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,

Hi Jim,

the approach I outlined should work in that case.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.