Cleaning r_util.h

This commit is contained in:
Maijin 2016-07-27 16:55:20 +02:00
parent fd76792386
commit 80171b0dc5
6 changed files with 151 additions and 182 deletions

View File

@ -5,6 +5,7 @@
#include "../blob/version.c"
#define STDIN_BUFFER_SIZE 354096
#define R_STATIC_ASSERT(x) switch (0) {case 0: case (x):;}
static RNum *num;
static int help();

View File

@ -240,4 +240,143 @@ static inline void r_write_ble(void *dst, ut64 val, bool big_endian, int size) {
}
}
// TODO: find better names and write vapis
#define ut8p_b(x) ((x)[0])
#define ut8p_bw(x) ((x)[0]|((x)[1]<<8))
#define ut8p_bd(x) ((x)[0]|((x)[1]<<8)|((x)[2]<<16)|((x)[3]<<24))
#define ut8p_bq(x) ((x)[0]|((x)[1]<<8)|((x)[2]<<16)|((x)[3]<<24)|((x)[4]<<32)|((x)[5]<<40)|((x)[6]<<48)|((x)[7]<<56))
#define ut8p_lw(x) ((x)[1]|((x)[0]<<8))
#define ut8p_ld(x) ((x)[3]|((x)[2]<<8)|((x)[1]<<16)|((x)[0]<<24))
#define ut8p_lq(x) ((x)[7]|((x)[6]<<8)|((x)[5]<<16)|((x)[4]<<24)|((x)[3]<<32)|((x)[2]<<40)|((x)[1]<<48)|((x)[0]<<56))
/*swap*/
static inline ut16 r_swap_ut16(ut16 val) {
return (val << 8) | (val >> 8 );
}
static inline st16 r_swap_st16(st16 val) {
val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );
return (val << 16) | (val >> 16);
}
static inline ut32 r_swap_ut32(ut32 val) {
val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );
return (val << 16) | (val >> 16);
}
static inline st32 r_swap_st32(st32 val) {
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF );
return (val << 16) | ((val >> 16) & 0xFFFF);
}
static inline ut64 r_swap_ut64(ut64 val) {
val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );
val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );
return (val << 32) | (val >> 32);
}
static inline st64 r_swap_st64(st64 val) {
val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );
val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );
return (val << 32) | ((val >> 32) & 0xFFFFFFFFULL);
}
/* Some "secured" functions, to do basic operation (mul, sub, add...) on integers */
static inline int UT64_ADD(ut64 *r, ut64 a, ut64 b) {
if(UT64_MAX - a < b)
return 0;
if(r != NULL)
*r = a + b;
return 1;
}
static inline int UT64_MUL(ut64 *r, ut64 a, ut64 b) {
if(a && UT64_MAX / a < b)
return 0;
if(r != NULL)
*r = a * b;
return 1;
}
static inline int UT64_SUB(ut64 *r, ut64 a, ut64 b) {
if(b > a)
return 0;
if(r != NULL)
*r = a - b;
return 1;
}
static inline int UT32_ADD(ut32 *r, ut32 a, ut32 b) {
if(UT32_MAX - a < b)
return 0;
if(r != NULL)
*r = a + b;
return 1;
}
static inline int UT32_MUL(ut32 *r, ut32 a, ut32 b) {
if(a && UT32_MAX / a < b)
return 0;
if(r != NULL)
*r = a * b;
return 1;
}
static inline int UT32_SUB(ut32 *r, ut32 a, ut32 b) {
if(b > a)
return 0;
if(r != NULL)
*r = a - b;
return 1;
}
static inline int UT16_ADD(ut16 *r, ut16 a, ut16 b) {
if(UT16_MAX - a < b)
return 0;
if(r != NULL)
*r = a + b;
return 1;
}
static inline int UT16_MUL(ut16 *r, ut16 a, ut16 b) {
if(a && UT16_MAX / a < b)
return 0;
if(r != NULL)
*r = a * b;
return 1;
}
static inline int UT16_SUB(ut16 *r, ut16 a, ut16 b) {
if(b > a)
return 0;
if(r != NULL)
*r = a - b;
return 1;
}
static inline int UT8_ADD(ut8 *r, ut8 a, ut8 b) {
if(UT8_MAX - a < b)
return 0;
if(r != NULL)
*r = a + b;
return 1;
}
static inline int UT8_MUL(ut8 *r, ut8 a, ut8 b) {
if(a && UT8_MAX / a < b)
return 0;
if(r != NULL)
*r = a * b;
return 1;
}
static inline int UT8_SUB(ut8 *r, ut8 a, ut8 b) {
if(b > a)
return 0;
if(r != NULL)
*r = a - b;
return 1;
}
#endif

View File

@ -3,6 +3,7 @@
#include <r_types.h>
#include <r_flist.h>
#include <ht.h>
#ifdef __cplusplus
extern "C" {
@ -25,6 +26,12 @@ typedef struct r_list_t {
RListFree free;
} RList;
typedef struct r_list_range_t {
RHashTable64 *h;
RList *l;
//RListComparator c;
} RListRange;
typedef int (*RListComparator)(const void *a, const void *b);
#define ROFList_Parent RList

View File

@ -160,6 +160,8 @@ R_API char *r_print_stereogram_bytes(const ut8 *buf, int len);
R_API char *r_print_stereogram(const char *bump, int w, int h);
R_API void r_print_stereogram_print(RPrint *p, const char *buf);
R_API void r_print_set_screenbounds(RPrint *p, ut64 addr);
R_API int r_util_lines_getline(ut64 *lines_cache, int lines_cache_sz, ut64 off);
#endif
#ifdef __cplusplus

View File

@ -60,188 +60,6 @@ extern "C" {
R_LIB_VERSION_HEADER(r_util);
// TODO: use lowercase here?
#define R_REFCTR_CLASS int refctr;void (*ref_free)(x)
#define R_REFCTR_INIT(x,y) x->refctr=0;x->ref_free=y
#define R_REFCTR_REF(x) x->refctr++
#define R_REFCTR_UNREF(x) if (--x->refctr<=0) x->ref_free(x)
#define R_STATIC_ASSERT(x) switch (0) {case 0: case (x):;}
#if 0
typedef struct {
R_REFCTR_CLASS;
} Foo;
Foo *r_foo_new () {
Foo *f = R_NEW(Foo)
R_REFCTR_INIT (f, r_foo_free);
...
return f;
}
Foo *foo = r_foo_new (Foo)
R_REFCTR_REF (foo)
R_REFCTR_UNREF (foo)
#endif
/* empty classes */
typedef struct { } RSystem;
//typedef struct { } RStr;
typedef struct { } RLog;
#define RStr char*
typedef int (*RStrRangeCallback) (void *, int);
/* TODO : THIS IS FROM See libr/anal/fcnstore.c for refactoring info */
typedef struct r_list_range_t {
RHashTable64 *h;
RList *l;
//RListComparator c;
} RListRange;
#ifdef R_API
// TODO: find better names and write vapis
#define ut8p_b(x) ((x)[0])
#define ut8p_bw(x) ((x)[0]|((x)[1]<<8))
#define ut8p_bd(x) ((x)[0]|((x)[1]<<8)|((x)[2]<<16)|((x)[3]<<24))
#define ut8p_bq(x) ((x)[0]|((x)[1]<<8)|((x)[2]<<16)|((x)[3]<<24)|((x)[4]<<32)|((x)[5]<<40)|((x)[6]<<48)|((x)[7]<<56))
#define ut8p_lw(x) ((x)[1]|((x)[0]<<8))
#define ut8p_ld(x) ((x)[3]|((x)[2]<<8)|((x)[1]<<16)|((x)[0]<<24))
#define ut8p_lq(x) ((x)[7]|((x)[6]<<8)|((x)[5]<<16)|((x)[4]<<24)|((x)[3]<<32)|((x)[2]<<40)|((x)[1]<<48)|((x)[0]<<56))
/*swap*/
static inline ut16 r_swap_ut16(ut16 val) {
return (val << 8) | (val >> 8 );
}
static inline st16 r_swap_st16(st16 val) {
val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );
return (val << 16) | (val >> 16);
}
static inline ut32 r_swap_ut32(ut32 val) {
val = ((val << 8) & 0xFF00FF00 ) | ((val >> 8) & 0xFF00FF );
return (val << 16) | (val >> 16);
}
static inline st32 r_swap_st32(st32 val) {
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF );
return (val << 16) | ((val >> 16) & 0xFFFF);
}
static inline ut64 r_swap_ut64(ut64 val) {
val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );
val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );
return (val << 32) | (val >> 32);
}
static inline st64 r_swap_st64(st64 val) {
val = ((val << 8) & 0xFF00FF00FF00FF00ULL ) | ((val >> 8) & 0x00FF00FF00FF00FFULL );
val = ((val << 16) & 0xFFFF0000FFFF0000ULL ) | ((val >> 16) & 0x0000FFFF0000FFFFULL );
return (val << 32) | ((val >> 32) & 0xFFFFFFFFULL);
}
#endif
R_API int r_util_lines_getline(ut64 *lines_cache, int lines_cache_sz, ut64 off);
/* Some "secured" functions, to do basic operation (mul, sub, add...) on integers */
static inline int UT64_ADD(ut64 *r, ut64 a, ut64 b) {
if(UT64_MAX - a < b)
return 0;
if(r != NULL)
*r = a + b;
return 1;
}
static inline int UT64_MUL(ut64 *r, ut64 a, ut64 b) {
if(a && UT64_MAX / a < b)
return 0;
if(r != NULL)
*r = a * b;
return 1;
}
static inline int UT64_SUB(ut64 *r, ut64 a, ut64 b) {
if(b > a)
return 0;
if(r != NULL)
*r = a - b;
return 1;
}
static inline int UT32_ADD(ut32 *r, ut32 a, ut32 b) {
if(UT32_MAX - a < b)
return 0;
if(r != NULL)
*r = a + b;
return 1;
}
static inline int UT32_MUL(ut32 *r, ut32 a, ut32 b) {
if(a && UT32_MAX / a < b)
return 0;
if(r != NULL)
*r = a * b;
return 1;
}
static inline int UT32_SUB(ut32 *r, ut32 a, ut32 b) {
if(b > a)
return 0;
if(r != NULL)
*r = a - b;
return 1;
}
static inline int UT16_ADD(ut16 *r, ut16 a, ut16 b) {
if(UT16_MAX - a < b)
return 0;
if(r != NULL)
*r = a + b;
return 1;
}
static inline int UT16_MUL(ut16 *r, ut16 a, ut16 b) {
if(a && UT16_MAX / a < b)
return 0;
if(r != NULL)
*r = a * b;
return 1;
}
static inline int UT16_SUB(ut16 *r, ut16 a, ut16 b) {
if(b > a)
return 0;
if(r != NULL)
*r = a - b;
return 1;
}
static inline int UT8_ADD(ut8 *r, ut8 a, ut8 b) {
if(UT8_MAX - a < b)
return 0;
if(r != NULL)
*r = a + b;
return 1;
}
static inline int UT8_MUL(ut8 *r, ut8 a, ut8 b) {
if(a && UT8_MAX / a < b)
return 0;
if(r != NULL)
*r = a * b;
return 1;
}
static inline int UT8_SUB(ut8 *r, ut8 a, ut8 b) {
if(b > a)
return 0;
if(r != NULL)
*r = a - b;
return 1;
}
#ifdef __cplusplus
}
#endif

View File

@ -1,6 +1,8 @@
#ifndef R_STR_H
#define R_STR_H
typedef int (*RStrRangeCallback) (void *, int);
/* TODO ..use as uppercase maybe? they are macros! */
#define strnull(x) (!x||!*x)
#define iswhitechar(x) ((x)==' '||(x)=='\t'||(x)=='\n'||(x)=='\r')