Browse code

bb#5700: implement and use fp_toradix_n()

David Raynor authored on 2012/08/29 02:37:48
Showing 5 changed files
... ...
@@ -406,6 +406,7 @@ libclamav_la_SOURCES += bignum.h\
406 406
         tomsfastmath/bin/fp_to_signed_bin.c\
407 407
         tomsfastmath/bin/fp_to_unsigned_bin.c\
408 408
         tomsfastmath/bin/fp_toradix.c\
409
+        tomsfastmath/bin/fp_toradix_n.c\
409 410
         tomsfastmath/bin/fp_unsigned_bin_size.c\
410 411
         tomsfastmath/bit/fp_cnt_lsb.c\
411 412
         tomsfastmath/bit/fp_count_bits.c\
... ...
@@ -183,6 +183,7 @@ am_libclamav_la_OBJECTS = libclamav_la-matcher-ac.lo \
183 183
 	libclamav_la-fp_signed_bin_size.lo \
184 184
 	libclamav_la-fp_to_signed_bin.lo \
185 185
 	libclamav_la-fp_to_unsigned_bin.lo libclamav_la-fp_toradix.lo \
186
+	libclamav_la-fp_toradix_n.lo \
186 187
 	libclamav_la-fp_unsigned_bin_size.lo \
187 188
 	libclamav_la-fp_cnt_lsb.lo libclamav_la-fp_count_bits.lo \
188 189
 	libclamav_la-fp_div_2.lo libclamav_la-fp_div_2d.lo \
... ...
@@ -695,6 +696,7 @@ libclamav_la_SOURCES = clamav.h matcher-ac.c matcher-ac.h matcher-bm.c \
695 695
 	tomsfastmath/bin/fp_to_signed_bin.c \
696 696
 	tomsfastmath/bin/fp_to_unsigned_bin.c \
697 697
 	tomsfastmath/bin/fp_toradix.c \
698
+	tomsfastmath/bin/fp_toradix_n.c \
698 699
 	tomsfastmath/bin/fp_unsigned_bin_size.c \
699 700
 	tomsfastmath/bit/fp_cnt_lsb.c tomsfastmath/bit/fp_count_bits.c \
700 701
 	tomsfastmath/bit/fp_div_2.c tomsfastmath/bit/fp_div_2d.c \
... ...
@@ -984,6 +986,7 @@ distclean-compile:
984 984
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-fp_to_signed_bin.Plo@am__quote@
985 985
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-fp_to_unsigned_bin.Plo@am__quote@
986 986
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-fp_toradix.Plo@am__quote@
987
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-fp_toradix_n.Plo@am__quote@
987 988
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-fp_unsigned_bin_size.Plo@am__quote@
988 989
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-fsg.Plo@am__quote@
989 990
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libclamav_la-hashtab.Plo@am__quote@
... ...
@@ -2008,6 +2011,13 @@ libclamav_la-fp_toradix.lo: tomsfastmath/bin/fp_toradix.c
2008 2008
 @AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
2009 2009
 @am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-fp_toradix.lo `test -f 'tomsfastmath/bin/fp_toradix.c' || echo '$(srcdir)/'`tomsfastmath/bin/fp_toradix.c
2010 2010
 
2011
+libclamav_la-fp_toradix_n.lo: tomsfastmath/bin/fp_toradix_n.c
2012
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-fp_toradix_n.lo -MD -MP -MF $(DEPDIR)/libclamav_la-fp_toradix_n.Tpo -c -o libclamav_la-fp_toradix_n.lo `test -f 'tomsfastmath/bin/fp_toradix_n.c' || echo '$(srcdir)/'`tomsfastmath/bin/fp_toradix_n.c
2013
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-fp_toradix_n.Tpo $(DEPDIR)/libclamav_la-fp_toradix_n.Plo
2014
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='tomsfastmath/bin/fp_toradix_n.c' object='libclamav_la-fp_toradix_n.lo' libtool=yes @AMDEPBACKSLASH@
2015
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
2016
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -c -o libclamav_la-fp_toradix_n.lo `test -f 'tomsfastmath/bin/fp_toradix_n.c' || echo '$(srcdir)/'`tomsfastmath/bin/fp_toradix_n.c
2017
+
2011 2018
 libclamav_la-fp_unsigned_bin_size.lo: tomsfastmath/bin/fp_unsigned_bin_size.c
2012 2019
 @am__fastdepCC_TRUE@	$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libclamav_la_CFLAGS) $(CFLAGS) -MT libclamav_la-fp_unsigned_bin_size.lo -MD -MP -MF $(DEPDIR)/libclamav_la-fp_unsigned_bin_size.Tpo -c -o libclamav_la-fp_unsigned_bin_size.lo `test -f 'tomsfastmath/bin/fp_unsigned_bin_size.c' || echo '$(srcdir)/'`tomsfastmath/bin/fp_unsigned_bin_size.c
2013 2020
 @am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/libclamav_la-fp_unsigned_bin_size.Tpo $(DEPDIR)/libclamav_la-fp_unsigned_bin_size.Plo
... ...
@@ -6,7 +6,7 @@
6 6
 #include "bignum_fast.h"
7 7
 typedef fp_int mp_int;
8 8
 #define mp_cmp fp_cmp
9
-#define mp_toradix_n(a,b,c,d) fp_toradix(a,b,c)
9
+#define mp_toradix_n(a,b,c,d) fp_toradix_n(a,b,c,d)
10 10
 #define mp_init(a) (fp_init(a), 0)
11 11
 #define mp_add fp_add
12 12
 
... ...
@@ -123,17 +123,22 @@ int crtmgr_add(crtmgr *m, cli_crt *x509) {
123 123
     m->crts = i;
124 124
 
125 125
     if(cli_debug_flag) {
126
-	char issuer[SHA1_HASH_SIZE*2+1], subject[SHA1_HASH_SIZE*2+1], mod[1024], exp[1024];
127
-	int j;
128
-	//mp_toradix_n(&i->n, mod, 16, sizeof(mod));
129
-	mod[0]='N'; mod[1]='/'; mod[2]='A'; mod[3]='\0';
130
-	//mp_toradix_n(&i->e, exp, 16, sizeof(exp));
131
-	exp[0]='N'; exp[1]='/'; exp[2]='A'; exp[3]='\0';
132
-	for(j=0; j<SHA1_HASH_SIZE; j++) {
133
-	    sprintf(&issuer[j*2], "%02x", i->issuer[j]);
134
-	    sprintf(&subject[j*2], "%02x", i->subject[j]);
135
-	}
136
-	cli_dbgmsg("crtmgr_add: added cert s:%s i:%s n:%s e:%s %lu->%lu %s%s%s\n", subject, issuer, mod, exp, (unsigned long)i->not_before, (unsigned long)i->not_after, i->certSign ? "cert ":"", i->codeSign ? "code ":"", i->timeSign ? "time":"");
126
+        char issuer[SHA1_HASH_SIZE*2+1], subject[SHA1_HASH_SIZE*2+1];
127
+        char mod[1024], exp[1024];
128
+        int j=1024;
129
+        // mod first
130
+        fp_toradix_n(&i->n, mod, 16, j);
131
+        // exp next
132
+        fp_toradix_n(&i->e, exp, 16, j);
133
+        // subject and issuer hashes
134
+        for(j=0; j<SHA1_HASH_SIZE; j++) {
135
+            sprintf(&issuer[j*2], "%02x", i->issuer[j]);
136
+            sprintf(&subject[j*2], "%02x", i->subject[j]);
137
+        }
138
+        // printing lines, broken up to minimize truncation
139
+        cli_dbgmsg("crtmgr_add: added cert s:%s i:%s %lu->%lu %s%s%s\n", subject, issuer, (unsigned long)i->not_before, (unsigned long)i->not_after, i->certSign ? "cert ":"", i->codeSign ? "code ":"", i->timeSign ? "time":"");
140
+        cli_dbgmsg("crtmgr_add: n:%s \n", mod);
141
+        cli_dbgmsg("crtmgr_add: e:%s \n", exp);
137 142
     }
138 143
     m->items++;
139 144
     return 0;
140 145
new file mode 100644
... ...
@@ -0,0 +1,58 @@
0
+/* TomsFastMath, a fast ISO C bignum library.
1
+ * That project is public domain and free for all purposes.
2
+ * fp_toradix_n included in TomsFastMath specification (tfm.h), but unimplemented until now.
3
+ * It is based on conversion of mp_toradix_n from libtommath
4
+ * Will send the body of this function back to the LibTom projects, if they want it
5
+ */
6
+#include "bignum_fast.h"
7
+
8
+int fp_toradix_n(fp_int *a, char *str, int radix, int maxlen)
9
+{
10
+  int     digs;
11
+  fp_int  t;
12
+  fp_digit d;
13
+  char   *_s = str;
14
+
15
+  /* check range of the maxlen, radix */
16
+  if (maxlen < 2 || radix < 2 || radix > 64) {
17
+    return FP_VAL;
18
+  }
19
+
20
+  /* quick out if its zero */
21
+  if (fp_iszero(a) == 1) {
22
+     *str++ = '0';
23
+     *str = '\0';
24
+     return FP_OKAY;
25
+  }
26
+
27
+  fp_init_copy(&t, a);
28
+
29
+  /* if it is negative output a - */
30
+  if (t.sign == FP_NEG) {
31
+    ++_s;
32
+    *str++ = '-';
33
+    t.sign = FP_ZPOS;
34
+    --maxlen;
35
+  }
36
+
37
+  digs = 0;
38
+  while (fp_iszero (&t) == FP_NO) {
39
+    if (--maxlen < 1) {
40
+       /* no more room */
41
+       break;
42
+    }
43
+    fp_div_d (&t, (fp_digit) radix, &t, &d);
44
+    *str++ = fp_s_rmap[d];
45
+    ++digs;
46
+  }
47
+
48
+  /* reverse the digits of the string.  In this case _s points
49
+   * to the first digit [exluding the sign] of the number]
50
+   */
51
+  fp_reverse ((unsigned char *)_s, digs);
52
+
53
+  /* append a NULL so the string is properly terminated */
54
+  *str = '\0';
55
+  return FP_OKAY;
56
+}
57
+