Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

C Tips

The reminder operator (%) in C

I’ve found the reminder operator in C very annoying. For example, if you have a ≡ 1 (mod n), then there are three kinds of result in c:

a%n == n+1 // if a < 0 && n < 0
a%n == 1-n // if a < 0 && n > 0
a%n == 1 // if a > 0 

The color in c

ANSC escape code

bash:

txtblk='\e[0;30m' # Black - Regular
txtred='\e[0;31m' # Red
txtgrn='\e[0;32m' # Green
txtylw='\e[0;33m' # Yellow
txtblu='\e[0;34m' # Blue
txtpur='\e[0;35m' # Purple
txtcyn='\e[0;36m' # Cyan
txtwht='\e[0;37m' # White
bldblk='\e[1;30m' # Black - Bold
bldred='\e[1;31m' # Red
bldgrn='\e[1;32m' # Green
bldylw='\e[1;33m' # Yellow
bldblu='\e[1;34m' # Blue
bldpur='\e[1;35m' # Purple
bldcyn='\e[1;36m' # Cyan
bldwht='\e[1;37m' # White
unkblk='\e[4;30m' # Black - Underline
undred='\e[4;31m' # Red
undgrn='\e[4;32m' # Green
undylw='\e[4;33m' # Yellow
undblu='\e[4;34m' # Blue
undpur='\e[4;35m' # Purple
undcyn='\e[4;36m' # Cyan
undwht='\e[4;37m' # White
bakblk='\e[40m'   # Black - Background
bakred='\e[41m'   # Red
bakgrn='\e[42m'   # Green
bakylw='\e[43m'   # Yellow
bakblu='\e[44m'   # Blue
bakpur='\e[45m'   # Purple
bakcyn='\e[46m'   # Cyan
bakwht='\e[47m'   # White
txtrst='\e[0m'    # Text Reset

in C:

#include <stdio.h>

#define ANSI_COLOR_RED     "\x1b[31m"
#define ANSI_COLOR_GREEN   "\x1b[32m"
#define ANSI_COLOR_YELLOW  "\x1b[33m"
#define ANSI_COLOR_BLUE    "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN    "\x1b[36m"
#define ANSI_COLOR_RESET   "\x1b[0m"

int main (int argc, char const *argv[]) {

  printf(ANSI_COLOR_RED     "This text is RED!"     ANSI_COLOR_RESET "\n");
  printf(ANSI_COLOR_GREEN   "This text is GREEN!"   ANSI_COLOR_RESET "\n");
  printf(ANSI_COLOR_YELLOW  "This text is YELLOW!"  ANSI_COLOR_RESET "\n");
  printf(ANSI_COLOR_BLUE    "This text is BLUE!"    ANSI_COLOR_RESET "\n");
  printf(ANSI_COLOR_MAGENTA "This text is MAGENTA!" ANSI_COLOR_RESET "\n");
  printf(ANSI_COLOR_CYAN    "This text is CYAN!"    ANSI_COLOR_RESET "\n");

  return 0;
}

Determine System Type Using Compiler Predefined Macros

How to list compiler predefined macros

Clang/LLVM:
clang -dM -E -x c /dev/null     
clang++ -dM -E -x c++ /dev/null
GNU GCC/G++:
gcc -dM -E -x c /dev/null   
g++ -dM -E -x c++ /dev/null 

How to detect the operating system type using compiler predefined macros

Linux

#if defined(__linux__)
    /* Linux. --------------------------------------------------- */

#endif

BSD

#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <sys/param.h>
#if defined(BSD)
    /* BSD (DragonFly BSD, FreeBSD, OpenBSD, NetBSD). ----------- */

#endif
#endif

Reference

http://nadeausoftware.com/articles/2012/01/c_c_tip_how_use_compiler_predefined_macros_detect_operating_system

Linux ramdom syscall not found

According linux man page, I wrote codes as below:

#include <linux/random.h>
......
#ifdef __linux__
    unsigned char buf[2 * LOOP_MAX * sizeof(int)];
    int *p = (int *)buf;
    result = getrandom(buf, LOOP_MAX * sizeof(int), 0);
#else
    srand((unsigned) time(0));
#endif

When compiling with gcc, I got this error message:

......c: In function ‘main’:
......c:43:5: warning: implicit declaration of function ‘getrandom’ [-Wimplicit-function-declaration]
     getrandom( buf, LOOP_MAX * sizeof(int), 0);
     ^
/tmp/ccng3Ckd.o: In function `main':
/......c:43: undefined reference to `getrandom'

I’m using the newest kernel and I’m sure that the syscall exists. I haven’t find the reason yet. But a workaroud:

#ifdef __linux__
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/random.h>
#else
......
#endif
......
#ifdef __linux__
    unsigned char buf[2 * LOOP_MAX * sizeof(int)];
    int *p = (int *)buf;
    result = syscall(SYS_getrandom, buf, LOOP_MAX * sizeof(int), 0);
#else
......

References:

http://man7.org/linux/man-pages/man2/getrandom.2.html

https://stackoverflow.com/questions/30800331/getrandom-syscall-in-c-not-found

How to debug stripped binaries with GDB

references: https://reverseengineering.stackexchange.com/questions/1935/how-to-handle-stripped-binaries-with-gdb-no-source-no-symbols-and-gdb-only-sho

Tricks you should know

Press return/enter will run last command

Commands can be abbreviated as long as they are unambiguous.

eg:

b for break (despite bt and backtrace)
c or cont for continue (despite catch, call and so on)
n for next (despite ni and nexti)

You can call a function with “call”

You can toggle sources, registers and asm view using layout `src/regs/asm’

GDB debug processing

run the command

$ gdb commands_to_run

Look for entry point

> info file
        `.......', file type elf64-x86-64.h
        Entry point: 0x402350
        0x0000000000400200 - 0x000000000040021c is .interp) group(s) only
        0x000000000040021c - 0x000000000040023c is .note.ABI-tag
        0x000000000040023c - 0x0000000000400260 is .note.gnu.build-id
        0x0000000000400260 - 0x0000000000400294 is .gnu.hash
        0x0000000000400298 - 0x0000000000400a78 is .dynsym
        0x0000000000400a78 - 0x0000000000400d54 is .dynstr

set breakpoints and run

> b *0x402350
        Breakpoint 1 at 0x402350
> run
        Starting program: /home/lfs/sources/john-1.7.8/run/john
        
        Breakpoint 1, 0x0000000000402350 in ?? ()

assembly

> set disassembly-flavor intel/att
> layout asm
> layout regs

btw, if you prefer to save screen estate, starting with GDB 7.0 you can use:

> set disassemble-next-line on

debug

# Execute one machine instruction, but if it is a function call, proceed until the function returns. 
> nexti 
> ni
# Execute one machine instruction, then stop and return to the debugger. 
> stepi 
>si