Thread: Writing some kind of extractor or installer.

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    Writing some kind of extractor or installer.

    I want my program to create a file. So it would be like the file is inside the program, and the program extracts it. What I did was write another program that would create an array of unsigned chars with all the hex values of the file I want to create, like this:

    Code:
    #define BLA_DATA_SIZE 22
    #define BLA_DATA_NAME bla
    
    const unsigned char bla[22] = {
    	0x62, 0x6C, 0x61, 0x20, 0x62, 0x6C, 0x61, 0x20, 0x62, 0x6C, 0x61, 0x20, 0x62, 0x6C, 0x61, 0x20, 
    	0x62, 0x6C, 0x61, 0x2E, 0x2E, 0x2E
    };
    and then, my program would write that something like this

    Code:
    #include <stdio.h>
    #include "bla.h"
    
    int main(void)
    {
        FILE *outfile = NULL;
        
        outfile = fopen("./bla.txt", "wb");
        fwrite(BLA_DATA_NAME, sizeof(unsigned char), BLA_DATA_SIZE, outfile);
        fclose(outfile);
        
        return 0;
    }
    If the file I want to create is too big (about 20 or 30MB) then the compiler gives me an error that it failed trying to allocated 65536 bytes of memory. If I changed the unsigned char[] to an unsigned char*, then the compiler would give me a whole bunch of errors, saying that I have exceeded the maximum number of elements or something.

    How can I make the program create bigger files (something like 100MB)? Is there a better way to do this?

    Thanks!

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Creating big files is not a problem. The problem is trying to store all those hex numbers in memory at once. You should just keep it in a separate file that is not part of you're executable. Then just copy the file from your original copy to bla.txt.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    That's what I don't want to do. I want to keep it in the same file as the executable.

    I mean, it must be possible, just like self-extracting archives and installers.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    As said before, there is no problem in creating a huge file, the can be of any size. I wonder what error messages do you get when you allocate memory. As far as i can see if you allocate 65536 you should be getting any problem there at all. Its not a that huge chunk of memory that your requesting. There is something else which is causing the error.

    How about posting a few more bits of your code. And whats your application. Why do you want the file contents to be with the binary itself?

    ssharish

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Quote Originally Posted by ssharish2005 View Post
    As said before, there is no problem in creating a huge file, the can be of any size. I wonder what error messages do you get when you allocate memory. As far as i can see if you allocate 65536 you should be getting any problem there at all. Its not a that huge chunk of memory that your requesting. There is something else which is causing the error.

    How about posting a few more bits of your code. And whats your application. Why do you want the file contents to be with the binary itself?

    ssharish
    OK, the compiler was the one that gave me the error message, so it doesn't even compile. I'll make a huge hex like that again and try it asap and give you the error message.

    Also, that's the only code I have. I'm just curious and want to know how it can be done, so that if I need it in the future, I can use it.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    I think you can add anything at the end of the .exe file without any problems. You will need to copy the file data to the end of the .exe along with a size value and when the program starts read this value and use it to seek to the start of the file data.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    here is the error gcc gives me when I compile (using MinGW)

    Code:
    cc1.exe: out of memory allocating 65536 bytes
    The bla.h file is 1.23GB in size and the file from where I got it is 204MB

  8. #8
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Quote Originally Posted by audinue View Post
    Thanks a lot! I'll try this on a really big file and see what happens.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    It doesn't compile. I'm getting the undefine reference stuff...

    First of all, I'm using MinGW in WinXP in a P4 w/ HT processor.

    I used objcopy like this:
    Code:
    objcopy --input-target binary --output-target elf32-i386 --binary-architecture i386 data.avi data.o
    It produced the data.o object file. My main looks like this:

    Code:
    #include <stdio.h>
    
    extern unsigned char _binary_data_avi_start;
    extern unsigned char _binary_data_avi_end;
    
    int main(void)
    {
        FILE *outfile = NULL;
        unsigned char *p = &_binary_data_avi_start;
        
        outfile = fopen("./bla.avi", "wb");
        while(p != &_binary_data_avi_end);
            fwrite(p++, sizeof(unsigned char), 1, outfile);
        
        fclose(outfile);
        
        return 0;
    }
    And the error is:
    Code:
    ccwi4ND9.o:main.c:(.text+0x34): undefined reference to `_binary_data_avi_start'
    ccwi4ND9.o:main.c:(.text+0x79): undefined reference to `_binary_data_avi_end'
    collect2: ld returned 1 exit status
    .... what am I doing wrong?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How did you determine that was the symbol name?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Quote Originally Posted by Salem View Post
    How did you determine that was the symbol name?
    I opened it with HexEdit, looked at the end of the file, and found this:

    Code:
    _binary_data_avi_start _binary_data_avi_end _binary_data_avi_size

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Try without the _ in your source code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Quote Originally Posted by Salem View Post
    Try without the _ in your source code.
    Ok well I took the leading _ off and it compiled, it created bla.avi, but it got stuck and didn't do anything else. So after a couple of minutes I aborted and bla.avi was 0 bytes in size so it definitely didn't work. It may be a problem with that fwrite and while I put... but not sure.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while(p != &_binary_data_avi_end);
    The ; at the end makes this an infinite loop

    Why not write the entire block in one fwrite call?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a UDF in C++
    By kripatel in forum Linux Programming
    Replies: 0
    Last Post: 03-22-2008, 04:14 PM
  2. help in writing a C-Progrm
    By kumaravel_futu in forum C Programming
    Replies: 10
    Last Post: 02-11-2008, 01:02 PM
  3. Experienced coders read. ( Common/efficient writing )
    By knave in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2003, 09:07 PM
  4. Writing "Enter" character to a txt file
    By Hankyaku in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2003, 11:06 AM
  5. The Art of Writing Comments :: Software Engineering
    By kuphryn in forum C++ Programming
    Replies: 15
    Last Post: 11-23-2002, 05:18 PM