Thread: Helping understand this code..

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    1

    Helping understand this code..

    Could somebodt please tell me:

    1) the quality of this code, as in efficiency

    This advice could really help me change things.

    Code:
    // intrinsics.h
    
    #ifndef __INTRINSICS_H__
    #define __INTRINSICS_H__
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    static __inline__ __attribute__((always_inline)) void __asm_push(const void *d) {
        #ifdef _M_AMD64
            __asm__ __volatile__("pushq %0\n" : : "rim"(d));
        #else
            __asm__ __volatile__("push %0\n" : : "rim"(d));
        #endif
    }
    
    static __inline__ __attribute__((always_inline)) unsigned long long __asm_call(const unsigned long long f) {
        unsigned long long rv;
       __asm__ __volatile__("call *%0\n" : "=r"(rv) : "0"(f));
        return rv;
    }
    
    #if defined(_M_AMD64)
    
    static __inline__ __attribute__((always_inline)) void __asm_writetorcx(const void *r) {
       __asm__ __volatile__("movq %q[r], %%rcx\t\n": : [r]"g"(r):"memory","%rcx");
    }
    
    static __inline__ __attribute__((always_inline)) void __asm_writetordx(const void *r) {
       __asm__ __volatile__("movq %q[r], %%rdx\n": : [r]"g"(r):"memory","%rdx");
    }
    
    static __inline__ __attribute__((always_inline)) void __asm_writetor8(const void *r) {
       __asm__ __volatile__("movq %q[r], %%r8\n": : [r]"g"(r):"memory","%r8");
    }
    
    static __inline__ __attribute__((always_inline)) void __asm_writetor9(const void *r) {
       __asm__ __volatile__("movq %q[r], %%r9\n": : [r]"g"(r):"memory","%r9");
    }
    
    /*** Taken from intrin.h ***/
    static __inline__ __attribute__((always_inline)) unsigned __int64 __readgsqword(const unsigned long Offset)
    {
       unsigned __int64 value;
       __asm__ __volatile__("movq %%gs:%a[Offset], %q[value]" : [value] "=q" (value) : [Offset] "irm" (Offset));
       return value;
    }
    
    #else
    static __inline__ __attribute__((always_inline)) unsigned long __readfsdword(const unsigned long Offset)
    {
       unsigned long value;
       __asm__ __volatile__("movl %%fs:%a[Offset], %k[value]" : [value] "=q" (value) : [Offset] "irm" (Offset));
       return value;
    }
    
    #endif //_M_AMD64
    
    #ifdef __cplusplus
    }
    #endif
    
    #endif // __INTRINSICS_H__test.c
    PTEB getCurrentTeb() {
        #if defined(_WIN64)
            return (PPEB)__readgsqword(0x30);
        #else
            return (PTEB)__readfsdword(0x18);
        #endif // defined
    }
    
    PPEB getCurrentPeb() {
        PTEB teb;
    
        if((teb=(PTEB)getCurrentTeb())==0)
            return 0;
        return (PPEB)teb->ProcessEnvironmentBlock;
    }
    
    static unsigned long long xcall(unsigned long long f, int c, ...) {
        va_list xstack;
        va_start(xstack, c);
        int i=0;
        unsigned long long fnc={0};
    
        #if defined(_WIN64)
       if(c<=0);
            else {
          //printf("Arguments count: %i working\n",c);
          void *w = { (c > 0) ? c--, va_arg(xstack, void *) : 0 };
          void *x = { (c > 0) ? c--, va_arg(xstack, void *) : 0 };
          void *y = { (c > 0) ? c--, va_arg(xstack, void *) : 0 };
          void *z = { (c > 0) ? c--, va_arg(xstack, void *) : 0 };
          __asm_writetorcx(w);
          __asm_writetordx(x);
          __asm_writetor8(y);
          __asm_writetor9(z);
          if(c>0)
             for(i=0;i<c;i++)
                __asm_push(va_arg(xstack, void *));
       }
        #else
            for(i=0;i<c;i++)
                __asm_push(va_arg(xstack, void *));
        #endif
        fnc = __asm_call(f);
        va_end(xstack);
        return fnc;
    }
    
    // main.c
    
    int main(int argc, char *argv[])
    {
       void *lpMsgBuff;
    
       LPCSTR lib="user32.dll";
       LPCSTR fn="MessageBoxA";
       char l2[]="ws2_32.dll";
    
       printf("Current TEB: 0x%lX\n", (unsigned long)getCurrentTeb());
       printf("Current PEB: 0x%lX\n", (unsigned long)getCurrentPeb());
       unsigned long long h=(unsigned long)GetProcAddress(
                (HANDLE)LoadLibrary(lib),fn);
       printf("Current %s Address: 0x%lX\n",fn , (unsigned long)h);
       unsigned long long r=xcall(h,4, NULL, (void *)&l2,(void *)&l2, MB_OK);
       if(r<=0) {
          DWORD err=GetLastError();
          printf("xcall returned 0, checking GetLastError()\n");
          printf("Error code is: 0x%lX\n",GetLastError());
          FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                0,
                err,
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                (LPTSTR)&lpMsgBuff,
                0,0);
          printf("Returned error Message: %s\n",(char *)lpMsgBuff);
          system("pause");
          return 0;
       }
       printf("%s address : 0x%lX\n", l2,(unsigned long)r);
       printf("\n\n");
       system("pause");
       return 0;
    }
    Last edited by n2dam; 12-20-2014 at 11:59 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is not a standard C program. It is compiler specific, windows specific, code. That means you have asked for help in the wrong forum - the right forum (i.e. increase likelihood of finding someone with enough specialised knowledge to advise you) would be a windows programming forum, not a C programming forum. You have not disclosed where the code came from (e.g. my guess would be that the headers are from a library shipped with a particular compiler or vendor-specific library) but that information would be useful to anyone who might be able to help.


    Some basic observations about the code from "intrinsics.h" and "intrin.h"
    1) it makes use of compiler-specific extensions (reserved identifiers with double underscores, and inline assembler).
    2) It has presumably been hand-crafted for use with specific compiler/library/system combinations, and it is not obvious which.
    3) It has not been written for readability so, without specialist knowledge of the targeted compilers/libraries/systems it is not possible to really describe what it is intended to do, let alone whether it meets that intent.

    The quality is, at best, variable.
    1) Quality is unknown in some aspects (e.g. performance on a targeted system) but is definitely quite low on others (difficult to maintain without specialist knowledge, the way to obtain the specialist knowledge is not obvious).
    2) The code is incomplete, because is uses types (like PTEB) which are not declared (so either there is more code - for example in another header file - that it relies or it relies on more compiler-specific extensions). It would be necessary to find specific documentation (which may or may not even exist - which I suggest is another indicator of low quality)



    On the main() function, some basic observations include;
    1) It is windows specific. Evidence of that include the strings which are passed to functions, the use of win32 API functions.
    2) It uses functions defined in the preceding header files, without any explanation of what it is achieving by doing do. That implies the preceding header files are also windows specific.
    3) The output is cryptic, and presumably intended for use by specialised developers rather than being targeted for non-expert users.
    4) The use of system("pause") statements also suggest it is written for use by developers, rather than other users.

    In terms of quality, it is special-purpose code that gives no useful information to anyone except a specalised developer. It has probably been written as a one-off, so there is probably not documentation around either.

    Without knowing what the code achieves, or what you modify it to achieve, it is not possible to give advice about any possible changes.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    An unnecessarily complicated mess to achieve something simple that could be achieved in a few lines of 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. helping with code in FLEX
    By Moran Abilea in forum Tech Board
    Replies: 9
    Last Post: 11-18-2014, 03:11 PM
  2. helping with code in FLEX
    By Moran Abilea in forum C Programming
    Replies: 1
    Last Post: 11-17-2014, 02:19 PM
  3. Replies: 1
    Last Post: 11-06-2014, 01:33 PM
  4. Help me understand this code...
    By shaselai in forum C Programming
    Replies: 1
    Last Post: 02-04-2009, 10:50 AM
  5. I want to understand this code
    By BadProgrammer in forum C++ Programming
    Replies: 9
    Last Post: 12-18-2003, 02:39 PM