* Initial import of OpenSSL r_big API implementation

- Split each implementation in different files
  - Add --without-openssl flag in configure
This commit is contained in:
pancake 2010-07-16 23:13:34 +02:00
parent 9e5f355e07
commit 6102da137d
9 changed files with 278 additions and 149 deletions

View File

@ -25,4 +25,5 @@ DL_LIBS=@DL_LIBS@
WITHPIC=@WITHPIC@
HAVE_LIB_TCC=@HAVE_LIB_TCC@
HAVE_VALASWIG=@HAVE_VALASWIG@
HAVE_LIB_GMP=@HAVE_LIB_GMP@
HAVE_LIB_OPENSSL=@HAVE_LIB_OPENSSL@

10
configure vendored
View File

@ -20,6 +20,7 @@ trap control_c 2
DEBUGGER=1
HAVE_EWF=1
HAVE_GMP=1
HAVE_OPENSSL=1
[ -z "${USERCC}" ] && USERCC="gcc"
[ -z "${USEROSTYPE}" ] && USEROSTYPE="auto"
WITHPIC=1
@ -145,6 +146,7 @@ printf "\nOptional Features:
--without-debugger disable native debugger features
--without-ewf disable EWF dependency
--without-gmp disable GMP dependency
--without-openssl disable openssl dependency
--with-compiler Define compiler to use (see mk/) (USERCC=gcc)
--with-ostype Choose OS type ( gnulinux windows darwin ) (USEROSTYPE=auto)
--without-pic do not build libr as a program independent location \n"
@ -191,7 +193,7 @@ echo "VERSION: 0.4.5"
echo "LANGS: c"
echo "REQUIRED: libdl"
echo "OPTIONAL: libewf"
echo "FLAGS: --without-debugger --without-ewf --without-gmp --with-compiler=gcc --with-ostype=auto --without-pic"
echo "FLAGS: --without-debugger --without-ewf --without-gmp --without-openssl --with-compiler=gcc --with-ostype=auto --without-pic"
exit 0
;;
"--cache-file")
@ -236,6 +238,7 @@ echo "FLAGS: --without-debugger --without-ewf --without-gmp --with-compiler=
"--without-debugger") DEBUGGER="0"; ;;
"--without-ewf") HAVE_EWF="0"; ;;
"--without-gmp") HAVE_GMP="0"; ;;
"--without-openssl") HAVE_OPENSSL="0"; ;;
--with-compiler) if [ -z "${value}" ]; then USERCC="gcc"; else USERCC="${value}" ; fi ;;
--with-ostype) if [ -z "${value}" ]; then USEROSTYPE="auto"; else USEROSTYPE="${value}" ; fi ;;
"--without-pic") WITHPIC="0"; ;;
@ -256,7 +259,7 @@ parse_options $1
shift
done
ENVWORDS="MANDIR INFODIR LIBDIR INCLUDEDIR LOCALSTATEDIR SYSCONFDIR DATADIR LIBEXECDIR SBINDIR BINDIR EPREFIX PREFIX SPREFIX TARGET HOST BUILD INSTALL INSTALL_LIB INSTALL_MAN INSTALL_PROGRAM INSTALL_DIR INSTALL_SCRIPT INSTALL_DATA HOST_OS HOST_CPU BUILD_OS BUILD_CPU TARGET_OS TARGET_CPU PKGNAME VPATH VERSION CONTACT CONTACT_NAME CONTACT_MAIL CC CFLAGS LDFLAGS HAVE_LANG_C DEBUGGER HAVE_LIB_DL DL_LIBS LIL_ENDIAN BIG_ENDIAN BYTEORDER HAVE_LIB_EWF HAVE_EWF HAVE_LIB_GMP HAVE_GMP HAVE_LIB_LUA5_1 HAVE_LIB_TCC USERCC USEROSTYPE WITHPIC"
ENVWORDS="MANDIR INFODIR LIBDIR INCLUDEDIR LOCALSTATEDIR SYSCONFDIR DATADIR LIBEXECDIR SBINDIR BINDIR EPREFIX PREFIX SPREFIX TARGET HOST BUILD INSTALL INSTALL_LIB INSTALL_MAN INSTALL_PROGRAM INSTALL_DIR INSTALL_SCRIPT INSTALL_DATA HOST_OS HOST_CPU BUILD_OS BUILD_CPU TARGET_OS TARGET_CPU PKGNAME VPATH VERSION CONTACT CONTACT_NAME CONTACT_MAIL CC CFLAGS LDFLAGS HAVE_LANG_C DEBUGGER HAVE_LIB_DL DL_LIBS LIL_ENDIAN BIG_ENDIAN BYTEORDER HAVE_LIB_EWF HAVE_EWF HAVE_LIB_GMP HAVE_GMP HAVE_LIB_OPENSSL HAVE_OPENSSL HAVE_LIB_LUA5_1 HAVE_LIB_TCC USERCC USEROSTYPE WITHPIC"
create_environ
@ -343,6 +346,9 @@ HAVE_LIB_EWF="0"; fi
check_library HAVE_LIB_GMP gmp 0
if [ "$HAVE_GMP" = "0" ]; then
HAVE_LIB_GMP="0"; fi
check_library HAVE_LIB_OPENSSL openssl 0
if [ "$HAVE_OPENSSL" = "0" ]; then
HAVE_LIB_OPENSSL="0"; fi
check_library HAVE_LIB_LUA5_1 lua5.1 0
check_library HAVE_LIB_TCC tcc 0
if [ "$USEROSTYPE" = "auto" ]; then

View File

@ -18,6 +18,10 @@ CHKLIB gmp
ARG_WITHOUT HAVE_GMP gmp disable GMP dependency ;
IFNOT HAVE_GMP { HAVE_LIB_GMP = 0 ; }
CHKLIB openssl
ARG_WITHOUT HAVE_OPENSSL openssl disable openssl dependency ;
IFNOT HAVE_OPENSSL { HAVE_LIB_OPENSSL = 0 ; }
CHKLIB lua5.1
CHKLIB tcc

View File

@ -11,6 +11,9 @@
#ifdef HAVE_LIB_GMP
#include <gmp.h>
#endif
#ifdef HAVE_LIB_OPENSSL
#include <openssl/bn.h>
#endif
/* empty classes */
typedef struct { } RSystem;
@ -305,12 +308,16 @@ R_API int r_range_overlap(ut64 a0, ut64 a1, ut64 b0, ut64 b1, int *d);
#ifdef HAVE_LIB_GMP
#define RNumBig mpz_t
#else
#ifdef HAVE_LIB_OPENSSL
#define RNumBig BIGNUM
#else
#define R_BIG_SIZE 10000
typedef struct r_num_big_t {
char dgts[R_BIG_SIZE];
int sign, last;
} RNumBig;
#endif
#endif
R_API RNumBig *r_big_new(RNumBig *b);
R_API void r_big_free(RNumBig *b);

View File

@ -1,7 +1,18 @@
NAME=r_util
include ../config.mk
OBJ=mem.o pool.o num.o str.o re.o hex.o file.o alloca.o range.o big.o log.o
NAME=r_util
OBJ=mem.o pool.o num.o str.o re.o hex.o file.o alloca.o range.o log.o
OBJ+=float.o prof.o cache.o sys.o btree.o buf.o list.o flist.o w32-sys.o
ifeq (${HAVE_LIB_GMP},1)
OBJ+=big-gmp.o
else
ifeq (${HAVE_LIB_OPENSSL},1)
OBJ+=big-ssl.o
else
OBJ+=big.o
endif
endif
LDFLAGS+=${BN_LIBS}
include ../rules.mk

87
libr/util/big-gmp.c Normal file
View File

@ -0,0 +1,87 @@
/* TODO: Implement all functions using gmp code */
#include <stdio.h>
#include <r_util.h>
#include <gmp.h>
static inline void r_big_zero(RNumBig *n) {
/* TODO */
}
R_API void r_big_print(RNumBig *n) {
/* TODO */
}
R_API void r_big_set_str(RNumBig *n, const char *str) {
mpz_set_str (*n, str, 10);
}
R_API RNumBig *r_big_new(RNumBig *b) {
RNumBig *n = R_NEW (RNumBig);
if (n) {
if (b) memcpy (n, b, sizeof (RNumBig));
else mpz_init (*n);
}
return n;
}
R_API void r_big_free(RNumBig *b) {
free (b);
}
R_API void r_big_set(RNumBig *a, RNumBig *b) {
mpz_set (*a, *b);
}
R_API void r_big_set_st(RNumBig *n, int v) {
/* TODO */
}
R_API void r_big_set_st64(RNumBig *n, st64 v) {
/* TODO */
}
/* c = a [+*-/] b; */
R_API void r_big_add (RNumBig *c, RNumBig *a, RNumBig *b) {
/* TODO */
}
R_API void r_big_sub(RNumBig *c, RNumBig *a, RNumBig *b) {
/* TODO */
}
R_API int r_big_cmp(RNumBig *a, RNumBig *b) {
return mpz_cmp (*a, *b);
}
R_API int r_big_cmp_st(RNumBig *n, int v) {
return mpz_cmp_si (*n, v);
}
/* multiply n by 10^d */
R_API void r_big_shift(RNumBig *n, int d) {
/* TODO */
}
R_API void r_big_mul (RNumBig *c, RNumBig *a, RNumBig *b) {
mpz_mul (*c, *a, *b);
}
R_API void r_big_mul_ut (RNumBig *c, RNumBig *a, ut32 b) {
mpz_mul_ui (*c, *a, b);
}
R_API void r_big_div(RNumBig *c, RNumBig *a, RNumBig *b) {
/* TODO */
}
R_API void r_big_div_ut(RNumBig *c, RNumBig *a, ut32 b) {
mpz_divexact_ui (*c, *a, b);
}
R_API int r_big_divisible_ut(RNumBig *n, ut32 v) {
return mpz_divisible_ui_p (*n, v);
}
R_API void r_big_mod(RNumBig *c, RNumBig *a, RNumBig *b) {
/* TODO */
}

89
libr/util/big-ssl.c Normal file
View File

@ -0,0 +1,89 @@
/* TODO: Implement all functions using gmp code */
#include <stdio.h>
#include <r_util.h>
static inline void r_big_zero(RNumBig *n) {
BN_zero (n);
}
R_API void r_big_print(RNumBig *n) {
/* TODO */
}
R_API void r_big_set_str(RNumBig *n, const char *str) {
BN_set_word (n, atoi (str));
}
R_API RNumBig *r_big_new(RNumBig *b) {
return BN_new ();
}
R_API void r_big_free(RNumBig *b) {
BN_free (b);
}
R_API void r_big_set(RNumBig *a, RNumBig *b) {
BN_copy (a, b);
}
R_API void r_big_set_st(RNumBig *n, int v) {
BN_set_word (n, v);
}
R_API void r_big_set_st64(RNumBig *n, st64 v) {
BN_set_word (n, v);
}
/* c = a [+*-/] b; */
R_API void r_big_add (RNumBig *c, RNumBig *a, RNumBig *b) {
BN_add (c, a, b);
}
R_API void r_big_sub(RNumBig *c, RNumBig *a, RNumBig *b) {
BN_sub (c, a, b);
}
R_API int r_big_cmp(RNumBig *a, RNumBig *b) {
return BN_ucmp (a, b);
}
R_API int r_big_cmp_st(RNumBig *n, int v) {
return BN_cmp (a, b);
}
/* multiply n by 10^d */
R_API void r_big_shift(RNumBig *n, int d) {
RNumBig a;
BN_copy (&a, n);
BN_lshift, (n, a, d);
}
R_API void r_big_mul (RNumBig *c, RNumBig *a, RNumBig *b) {
/* TODO: last parameter must be BN_CTX */
BN_mul (c, a, b, NULL);
}
R_API void r_big_mul_ut (RNumBig *c, RNumBig *a, ut32 b) {
RNumBig *b = BN_new ();
BN_set_word (b, b);
r_big_mul (c, a, &b);
BN_free (b);
}
R_API void r_big_div(RNumBig *c, RNumBig *a, RNumBig *b) {
BN_div (c, NULL, a, b);
}
R_API void r_big_div_ut(RNumBig *c, RNumBig *a, ut32 b) {
eprintf (__FUNCTION__": TODO\n");
// mpz_divexact_ui (*c, *a, b);
}
R_API int r_big_divisible_ut(RNumBig *n, ut32 v) {
eprintf (__FUNCTION__": TODO\n");
// return mpz_divisible_ui_p (*n, v);
}
R_API void r_big_mod(RNumBig *c, RNumBig *a, RNumBig *b) {
BN_div (NULL, c, a, b);
}

View File

@ -4,29 +4,17 @@
*/
/* XXX : seems broken for big numbers */
/* TODO: Implement gmp code */
#include <stdio.h>
#include <r_util.h>
#ifdef HAVE_LIB_GMP
#include <gmp.h>
#endif
static inline void r_big_zero(RNumBig *n) {
#ifdef HAVE_LIB_GMP
return;
#else
while ((n->last>0) && !n->dgts[n->last])
n->last--;
if (!n->last && !*n->dgts)
n->sign = 1; /* hack to avoid -0 */
#endif
}
R_API void r_big_print(RNumBig *n) {
#ifdef HAVE_LIB_GMP
return;
#else
int i;
if (n->last>=0) {
if (n->sign<0)
@ -35,13 +23,9 @@ R_API void r_big_print(RNumBig *n) {
printf ("%c", '0'+n->dgts[i]);
printf ("\n");
}
#endif
}
R_API void r_big_set_str(RNumBig *n, const char *str) {
#ifdef HAVE_LIB_GMP
mpz_set_str (*n, str, 10);
#else
int i, len;
if (*str=='-') {
n->sign = -1;
@ -50,19 +34,13 @@ R_API void r_big_set_str(RNumBig *n, const char *str) {
for (i=len=strlen (str)-1; *str; i--, str++)
n->dgts[i] = *str-'0';
n->last = len;
#endif
}
R_API RNumBig *r_big_new(RNumBig *b) {
RNumBig *n = R_NEW (RNumBig);
if (n) {
if (b) memcpy (n, b, sizeof (RNumBig));
else
#ifdef HAVE_LIB_GMP
mpz_init (*n);
#else
r_big_set_st (n, 0);
#endif
else r_big_set_st (n, 0);
}
return n;
}
@ -72,17 +50,10 @@ R_API void r_big_free(RNumBig *b) {
}
R_API void r_big_set(RNumBig *a, RNumBig *b) {
#ifdef HAVE_LIB_GMP
mpz_set (*a, *b);
#else
memcpy (a, b, sizeof (RNumBig));
#endif
}
R_API void r_big_set_st(RNumBig *n, int v) {
#ifdef HAVE_LIB_GMP
return;
#else
int t;
n->last = 0;
n->sign = (v>=0)?1:-1;
@ -90,13 +61,9 @@ R_API void r_big_set_st(RNumBig *n, int v) {
for (n->last=0, t=R_ABS (v); t>0; t/=10, n->last++)
n->dgts[n->last] = (t % 10);
if (!v) n->last = 0;
#endif
}
R_API void r_big_set_st64(RNumBig *n, st64 v) {
#ifdef HAVE_LIB_GMP
return;
#else
st64 t;
n->sign = (v<0)?-1:1;
memset (n->dgts, 0, R_BIG_SIZE);
@ -106,14 +73,10 @@ R_API void r_big_set_st64(RNumBig *n, st64 v) {
n->dgts[n->last] = t%10;
}
if (!v) n->last = 0;
#endif
}
/* c = a [+*-/] b; */
R_API void r_big_add (RNumBig *c, RNumBig *a, RNumBig *b) {
#ifdef HAVE_LIB_GMP
return;
#else
int i, carry;
RNumBig t;
r_big_set_st (&t, 0);
@ -135,13 +98,9 @@ R_API void r_big_add (RNumBig *c, RNumBig *a, RNumBig *b) {
}
*c = t;
r_big_zero (c);
#endif
}
R_API void r_big_sub(RNumBig *c, RNumBig *a, RNumBig *b) {
#ifdef HAVE_LIB_GMP
return;
#else
RNumBig t;
int i, v, borrow;
@ -175,13 +134,9 @@ R_API void r_big_sub(RNumBig *c, RNumBig *a, RNumBig *b) {
}
*c = t;
r_big_zero (c);
#endif
}
R_API int r_big_cmp(RNumBig *a, RNumBig *b) {
#ifdef HAVE_LIB_GMP
return mpz_cmp (*a, *b);
#else
int i;
if ((a->sign == -1) && (b->sign == 1)) return 1;
if ((a->sign == 1) && (b->sign == -1)) return -1;
@ -192,22 +147,14 @@ R_API int r_big_cmp(RNumBig *a, RNumBig *b) {
if (b->dgts[i] > a->dgts[i]) return a->sign;
}
return 0;
#endif
}
R_API int r_big_cmp_st(RNumBig *n, int v) {
#ifdef HAVE_LIB_GMP
return mpz_cmp_si (*n, v);
#else
return 0;
#endif
}
/* multiply n by 10^d */
R_API void r_big_shift(RNumBig *n, int d) {
#ifdef HAVE_LIB_GMP
return;
#else
int i;
if (!n->last && !*n->dgts)
return;
@ -215,13 +162,9 @@ R_API void r_big_shift(RNumBig *n, int d) {
n->dgts[i+d] = n->dgts[i];
memset (n->dgts, 0, d);
n->last += d;
#endif
}
R_API void r_big_mul (RNumBig *c, RNumBig *a, RNumBig *b) {
#ifdef HAVE_LIB_GMP
mpz_mul (*c, *a, *b);
#else
RNumBig t, tmp, row;
int i,j;
r_big_set_st (&t, 0);
@ -237,21 +180,13 @@ R_API void r_big_mul (RNumBig *c, RNumBig *a, RNumBig *b) {
*c = t;
c->sign = a->sign * b->sign;
r_big_zero (c);
#endif
}
R_API void r_big_mul_ut (RNumBig *c, RNumBig *a, ut32 b) {
#ifdef HAVE_LIB_GMP
mpz_mul_ui (*c, *a, b);
#else
return;
#endif
}
R_API void r_big_div(RNumBig *c, RNumBig *a, RNumBig *b) {
#ifdef HAVE_LIB_GMP
return;
#else
RNumBig t, tmp, row;
int i, asign, bsign;
@ -278,97 +213,19 @@ R_API void r_big_div(RNumBig *c, RNumBig *a, RNumBig *b) {
r_big_zero (c);
a->sign = asign;
b->sign = bsign;
#endif
}
R_API void r_big_div_ut(RNumBig *c, RNumBig *a, ut32 b) {
#ifdef HAVE_LIB_GMP
mpz_divexact_ui (*c, *a, b);
#else
return;
#endif
}
R_API int r_big_divisible_ut(RNumBig *n, ut32 v) {
#ifdef HAVE_LIB_GMP
return mpz_divisible_ui_p (*n, v);
#else
return 0;
#endif
}
R_API void r_big_mod(RNumBig *c, RNumBig *a, RNumBig *b) {
#ifdef HAVE_LIB_GMP
return;
#else
RNumBig t; // a%b = a-((a/b)*b)
r_big_div (c, a, b); // c=a/b
r_big_mul (&t, c, b); // t=c*b
r_big_sub (c, a, &t); // c=a-t
#endif
}
#if TEST
void main() {
int a,b;
RNumBig n1, n2, n3, zero;
r_big_set_st (&n2, -2);
r_big_set_str (&n3, "-3");
//r_big_set_st (&n3, -3);
printf ("n3last = %d\n", n3.last);
printf ("%d %d\n", n3.dgts[0], n3.dgts[1]);
r_big_mul (&n2, &n2, &n3);
r_big_print (&n2);
printf("--\n");
r_big_set_st (&n1, 2);
r_big_set_st (&n2, 3);
r_big_mul(&n1, &n1, &n2);
r_big_print (&n1);
r_big_set_st (&n3, 923459999);
r_big_mul (&n1, &n2, &n3);
r_big_mul (&n2, &n1, &n3);
r_big_mul (&n1, &n2, &n3);
r_big_print (&n1);
r_big_set_st64 (&n2, 9999923459999999);
r_big_set_st64 (&n3, 9999992345999999);
r_big_mul (&n1, &n2, &n3);
r_big_mul (&n2, &n1, &n3);
r_big_mul (&n1, &n2, &n3);
r_big_print (&n1);
while (scanf ("%d %d\n",&a,&b) != EOF) {
printf("a = %d b = %d\n",a,b);
r_big_set_st(&n1, a);
r_big_set_st(&n2, b);
r_big_add (&n3, &n1, &n2);
printf ("addition -- ");
r_big_print (&n3);
printf ("r_big_cmp a ? b = %d\n",r_big_cmp(&n1, &n2));
r_big_sub (&n3,&n1,&n2);
printf("subtraction -- ");
r_big_print (&n3);
r_big_mul (&n3,&n1,&n2);
printf("multiplication -- ");
r_big_print (&n3);
r_big_set_st(&zero, 0);
if (r_big_cmp(&zero, &n2) == 0)
printf("division -- NaN \n");
else {
r_big_div (&n3,&n1,&n2);
printf("division -- ");
r_big_print (&n3);
}
printf("--------------------------\n");
}
}
#endif

67
libr/util/t/big.c Normal file
View File

@ -0,0 +1,67 @@
#include "../big.c"
/*
#include "../big-ssl.c"
#include "../big-gmp.c"
*/
void main() {
int a,b;
RNumBig n1, n2, n3, zero;
r_big_set_st (&n2, -2);
r_big_set_str (&n3, "-3");
//r_big_set_st (&n3, -3);
printf ("n3last = %d\n", n3.last);
printf ("%d %d\n", n3.dgts[0], n3.dgts[1]);
r_big_mul (&n2, &n2, &n3);
r_big_print (&n2);
printf("--\n");
r_big_set_st (&n1, 2);
r_big_set_st (&n2, 3);
r_big_mul(&n1, &n1, &n2);
r_big_print (&n1);
r_big_set_st (&n3, 923459999);
r_big_mul (&n1, &n2, &n3);
r_big_mul (&n2, &n1, &n3);
r_big_mul (&n1, &n2, &n3);
r_big_print (&n1);
r_big_set_st64 (&n2, 9999923459999999);
r_big_set_st64 (&n3, 9999992345999999);
r_big_mul (&n1, &n2, &n3);
r_big_mul (&n2, &n1, &n3);
r_big_mul (&n1, &n2, &n3);
r_big_print (&n1);
while (scanf ("%d %d\n",&a,&b) != EOF) {
printf("a = %d b = %d\n",a,b);
r_big_set_st(&n1, a);
r_big_set_st(&n2, b);
r_big_add (&n3, &n1, &n2);
printf ("addition -- ");
r_big_print (&n3);
printf ("r_big_cmp a ? b = %d\n",r_big_cmp(&n1, &n2));
r_big_sub (&n3,&n1,&n2);
printf("subtraction -- ");
r_big_print (&n3);
r_big_mul (&n3,&n1,&n2);
printf("multiplication -- ");
r_big_print (&n3);
r_big_set_st(&zero, 0);
if (r_big_cmp(&zero, &n2) == 0)
printf("division -- NaN \n");
else {
r_big_div (&n3,&n1,&n2);
printf("division -- ");
r_big_print (&n3);
}
printf("--------------------------\n");
}
}