How do i convert an integer to an address for a function pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • rodrigo.gloria@gmail.com

    #1

    How do i convert an integer to an address for a function pointer

    I am trying to convert a integer to an address of a function pointer.

    I want to encrypt the pointer and then do some validation, afterwards i
    will decrpyt the pointer back to an address. Can this be done. Below
    is some of my code.


    typedef void (*funcptr)(void );
    void tryme(void){};

    void main()
    {

    char *buffer = new char[30];
    // save address of tryme fuction into a string
    itoa ( (unsigned int)(&tryme),bu ffer,16);
    Encrypt(buffer) ;

    //some code here

    // retreive address tryme function
    decryptbuffer = Decrypt(buffer) ;
    unsigned int x = (unsigned int)atoi(decryp tbuffer );
    funcptr restoreFuncPtr = (funcptr)x; // convert int to funcptr
    address

    }



    }

  • benben

    #2
    Re: How do i convert an integer to an address for a function pointer

    >Can this be done.

    Compile and run your code and you'd find out.

    Ben

    Comment

    • Ramki

      #3
      Re: How do i convert an integer to an address for a function pointer

      Hi,
      Your code should work.
      But, using reinterpret_cas t<> is safe

      Checkout the following code snippet

      typedef int (*FP)(void);
      int add()
      {
      return 10;
      }

      main()
      {
      FP funPtr;
      unsigned int num = reinterpret_cas t<unsigned int>(add);
      funPtr = reinterpret_cas t<FP>(num);
      printf("%d", funPtr());
      }


      Thanx,
      Rama

      Comment

      • Jacek Dziedzic

        #4
        Re: How do i convert an integer to an address for a function pointer

        rodrigo.gloria@ gmail.com wrote:[color=blue]
        > I am trying to convert a integer to an address of a function pointer.
        >
        > I want to encrypt the pointer and then do some validation, afterwards i
        > will decrpyt the pointer back to an address. Can this be done. Below
        > is some of my code.
        >
        >
        > typedef void (*funcptr)(void );
        > void tryme(void){};
        >
        > void main()
        > {
        >
        > char *buffer = new char[30];
        > // save address of tryme fuction into a string
        > itoa ( (unsigned int)(&tryme),bu ffer,16);
        > Encrypt(buffer) ;
        >
        > //some code here
        >
        > // retreive address tryme function
        > decryptbuffer = Decrypt(buffer) ;
        > unsigned int x = (unsigned int)atoi(decryp tbuffer );
        > funcptr restoreFuncPtr = (funcptr)x; // convert int to funcptr
        > address[/color]

        What makes you think the value of a pointer will FIT
        inside an unsigned integer?

        You might have 64-bit pointers and 32-bit unsigned ints,
        what then?

        HTH,
        - J.

        Comment

        • Tomás

          #5
          Re: How do i convert an integer to an address for a function pointer

          posted:
          [color=blue]
          > I am trying to convert a integer to an address of a function pointer.[/color]


          typedef unsigned long PointerNumericV alue;


          void* Encrypt( void* p )
          {
          PointerNumericV alue value = reinterpret_cas t<unsigned long>(p);

          //Now encrypt it:

          p << 2;

          p += 3;

          //Now return it:

          return reinterpret_cas t<void*>(p);
          }

          Comment

          • Ramki

            #6
            Re: How do i convert an integer to an address for a function pointer

            Then u can take ,

            unsigned _int64 (or)
            long long unsigned int
            ........ here 64-bit pointers may fit.

            :-)
            Thanx,
            Rama

            Comment

            • Jacek Dziedzic

              #7
              Re: How do i convert an integer to an address for a function pointer

              Ramki wrote:[color=blue]
              > Then u can take ,
              >
              > unsigned _int64 (or)
              > long long unsigned int
              > ........ here 64-bit pointers may fit.
              >
              > :-)[/color]

              Yes, if you have stuff like "_int64". That, however,
              raises the question of what happens if pointers are
              bigger still, or ints are 16-bit?

              What I meant to say was, you can't assume a 1:1
              relationship between the size of a pointer and
              sizeof(unsigned int).

              - J.

              Comment

              • Jacek Dziedzic

                #8
                Re: How do i convert an integer to an address for a function pointer

                Tomás wrote:[color=blue]
                > posted:
                >
                > [color=green]
                >>I am trying to convert a integer to an address of a function pointer.[/color]
                >
                >
                >
                > typedef unsigned long PointerNumericV alue;
                >
                >
                > void* Encrypt( void* p )
                > {
                > PointerNumericV alue value = reinterpret_cas t<unsigned long>(p);
                >
                > //Now encrypt it:
                >
                > p << 2;
                >
                > p += 3;
                >
                > //Now return it:
                >
                > return reinterpret_cas t<void*>(p);
                > }[/color]

                Still, what if it doesn't fit inside an unsigned long?,
                although it's the OP's problem.

                - J.

                Comment

                • Jack Klein

                  #9
                  Re: How do i convert an integer to an address for a function pointer

                  On 16 Mar 2006 01:57:57 -0800, "Ramki" <writerama@gmai l.com> wrote in
                  comp.lang.c++:
                  [color=blue]
                  > Hi,
                  > Your code should work.
                  > But, using reinterpret_cas t<> is safe
                  >
                  > Checkout the following code snippet
                  >
                  > typedef int (*FP)(void);
                  > int add()
                  > {
                  > return 10;
                  > }
                  >
                  > main()[/color]

                  Your program is ill-formed, implicit int is no valid in C++, all
                  function definitions and declarations must specify a return type.
                  [color=blue]
                  > {
                  > FP funPtr;
                  > unsigned int num = reinterpret_cas t<unsigned int>(add);
                  > funPtr = reinterpret_cas t<FP>(num);
                  > printf("%d", funPtr());
                  > }
                  >
                  >
                  > Thanx,
                  > Rama[/color]

                  Your compiler is non conforming, or you are not invoking it in
                  conforming mode, if it accepts this program without issuing a
                  diagnostic for the implicit int in the definition of main().

                  It is also non-conforming if it accepts the application of
                  reinterpret_cas t on a function pointer type to any scalar type. This
                  is just plain not valid C++.

                  --
                  Jack Klein
                  Home: http://JK-Technology.Com
                  FAQs for
                  comp.lang.c http://c-faq.com/
                  comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                  alt.comp.lang.l earn.c-c++

                  Comment

                  • Me

                    #10
                    Re: How do i convert an integer to an address for a function pointer

                    rodrigo.gloria@ gmail.com wrote:[color=blue]
                    > I am trying to convert a integer to an address of a function pointer.
                    >
                    > I want to encrypt the pointer and then do some validation, afterwards i
                    > will decrpyt the pointer back to an address. Can this be done. Below
                    > is some of my code.[/color]

                    Not guaranteed to work at all (see J.5.7 in the C standard)
                    [color=blue]
                    > typedef void (*funcptr)(void );
                    > void tryme(void){}[/color]
                    <snip>

                    funcptr fn = tryme;
                    unsigned char buf[sizeof(fn)];
                    memcpy(buf, &fn, sizeof(fn));
                    encrypt_inplace (buf, sizeof(fn));

                    ....

                    decrypt_inplace (buf, sizeof(fn));
                    memcpy(&fn, buf, sizeof(fn));
                    fn();

                    Comment

                    • rodrigo.gloria@gmail.com

                      #11
                      Re: How do i convert an integer to an address for a function pointer

                      Thanks alot the method below worked.

                      Hi,
                      Your code should work.
                      But, using reinterpret_cas t<> is safe

                      Checkout the following code snippet


                      typedef int (*FP)(void);
                      int add()
                      {
                      return 10;



                      }


                      main()
                      {
                      FP funPtr;
                      unsigned int num = reinterpret_cas t<unsigned int>(add);
                      funPtr = reinterpret_cas t<FP>(num);
                      printf("%d", funPtr());


                      }

                      Thank you,

                      Rodrigo

                      Comment

                      Working...