Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Calling a program that is dumped to a C array.
QtWS25 Last Chance

Calling a program that is dumped to a C array.

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
10 Posts 3 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mrbitmap
    wrote on last edited by
    #1

    Hello, it's not quite qt related, however it can be considered a qt resource program too, but for now, let's say that I have a binutil like program that dumps an elf/exe file to a .c file to a char array formint that format

    extern const char _binary_default[]; // dumped hex data
    extern const size_t _binary_default_size; // size of array
    

    so in my program I am trying to do the following:

    #include "binary7.c" // the file wit hex data
    
    extern const char _binary_default[];  // externals to the file`s array
    extern const size_t _binary_default_size; // externals to file`s size
    
    typedef int (*fb)(int argc, char** arv);
    
    int main(int argc, char *argv[]) 
    {
        fb c = (fb)_binary_default; // casting the array to a function
        c(argc, argv);   // try calling it
        return 0;
    }
    

    Let's say I've dumped a program "Hello world". Will that approach work? For me it does not work. My g2bin program formats the array with fprintf(..., "0x%x") . So, Qt embeded world, will you lend me a hand in this?

    Thanks.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Maybe you could write it back as a file and use QProcess to run it ?

      M 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        Maybe you could write it back as a file and use QProcess to run it ?

        M Offline
        M Offline
        mrbitmap
        wrote on last edited by
        #3

        @mrjj
        Hello, yes I can, but that's not what I need, I need to run it that way. I am writing an emulator and this is the firmware I am expecting.

        mrjjM 1 Reply Last reply
        0
        • M mrbitmap

          @mrjj
          Hello, yes I can, but that's not what I need, I need to run it that way. I am writing an emulator and this is the firmware I am expecting.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @mrbitmap
          Ok. what platform are u targeting ?

          M 1 Reply Last reply
          0
          • mrjjM mrjj

            @mrbitmap
            Ok. what platform are u targeting ?

            M Offline
            M Offline
            mrbitmap
            wrote on last edited by
            #5

            @mrjj
            I don't know if it's appropriate to write it here. Let say a CPU from over 20 years ago.

            mrjjM 1 Reply Last reply
            0
            • M mrbitmap

              @mrjj
              I don't know if it's appropriate to write it here. Let say a CPU from over 20 years ago.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @mrbitmap
              I ment the OS.
              All sampes i know of , write it to file
              You would need somehow to create the process without a file and im not sure
              there are any easy way to do that. ( since its exe and not DLL/So)

              This talk of this
              http://stackoverflow.com/questions/305203/createprocess-from-memory-buffer

              M 1 Reply Last reply
              0
              • mrjjM mrjj

                @mrbitmap
                I ment the OS.
                All sampes i know of , write it to file
                You would need somehow to create the process without a file and im not sure
                there are any easy way to do that. ( since its exe and not DLL/So)

                This talk of this
                http://stackoverflow.com/questions/305203/createprocess-from-memory-buffer

                M Offline
                M Offline
                mrbitmap
                wrote on last edited by
                #7

                @mrjj
                I'll consider it. I am executing it under linux. Will report back after few experiments. I am just asking if the approach I've pointed is legitimate.

                mrjjM jsulmJ 2 Replies Last reply
                0
                • M mrbitmap

                  @mrjj
                  I'll consider it. I am executing it under linux. Will report back after few experiments. I am just asking if the approach I've pointed is legitimate.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  @mrbitmap
                  Well on windows , many scanners would trigger on your exe :)
                  Its 100% same way as spyware try to execute its payload.

                  Anyway, before you can directly run it, should not have a "decompile" function?
                  I assume that g2bin change the values so no zero are found and hence can
                  be included as a string.
                  So if u give _binary_default to some process function, the actual memory block will be in correct format or still in this "hex" format?
                  Maybe I should rather ask. If you save it directly back to a file.
                  and run it. It works?

                  On linux it seems to be possible
                  http://stackoverflow.com/questions/10523681/execute-a-process-from-memory-within-another-process

                  All exe files compressors do this
                  https://upx.github.io/
                  So u might be able to be inspired.

                  But if u cant use a file , then be prepared for it to get a bit hairy :)

                  1 Reply Last reply
                  2
                  • M mrbitmap

                    @mrjj
                    I'll consider it. I am executing it under linux. Will report back after few experiments. I am just asking if the approach I've pointed is legitimate.

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @mrbitmap How do you fill _binary_default?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @mrbitmap How do you fill _binary_default?

                      M Offline
                      M Offline
                      mrbitmap
                      wrote on last edited by
                      #10

                      @jsulm
                      Hello, here is the complete code:

                      #include <stdio.h>
                      #include <stdlib.h>
                      #define DEBUG 1
                      
                      static FILE* open_or_exit(const char* fname, char* perms)
                      {
                          FILE* fp = fopen(fname, perms);
                          if ( !fp )
                              exit(EXIT_FAILURE);
                          else
                              return fp;
                      }
                      
                      
                      int main(int argc, char** argv)
                      {
                          if ( argc < 3 )
                          {
                              fprintf(stderr, "ERROR usage!\ng2bin <infile> <outfile>\n");
                              return 1;
                          }
                          char varname[128]={0};
                          if ( argc == 4 )
                          {
                              sprintf(varname, "%s", argv[3]);
                          } else
                          {
                              sprintf(varname, "%s", "default");
                          }
                      
                          FILE *infile = open_or_exit(argv[1], "rb");
                          FILE* outfile = open_or_exit(argv[2], "w");
                      
                          unsigned short buff[256]={0};
                          size_t line=0;
                          size_t nread=0;
                      
                          fprintf(outfile, "#include <stdlib.h>\n");
                          fprintf(outfile, "const char ");
                          fprintf(outfile, "_binary_%s", varname);
                          fprintf(outfile, "[]={\n");
                          do
                          {
                              nread = fread(buff, 1, sizeof(buff), infile);
                      #ifdef DEBUG
                            printf("[%d] bytes read\n", nread);
                      #endif
                              for(int i=0; i < (sizeof(buff)/sizeof(buff[0])); i++)
                              {
                                  fprintf(outfile, "0x%02x", buff[i]);
                                  if ( nread > 0)
                                      fprintf(outfile, ",");
                                  else
                                      break;
                                  if ( line++ >= 10 )
                                  {
                                      fprintf(outfile, "\n");
                                      line = 0;
                                  }
                              }
                      
                          } while ( nread > 0);
                          fprintf(outfile, "};\n");
                          fprintf(outfile, "const size_t _binary_%s_size=sizeof(_binary_%s);\n"
                                  ,varname, varname);
                      
                          fclose(infile);
                          fclose(outfile);
                      
                          return 0;
                      }
                      
                      

                      That dumps a file into a .c compilable file with data and data's size. A feedback would be welcome.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved