~ubuntu-branches/ubuntu/utopic/clamav/utopic-security

« back to all changes in this revision

Viewing changes to libclamav/tomsfastmath/bin/fp_toradix_n.c

  • Committer: Package Import Robot
  • Author(s): Scott Kitterman
  • Date: 2014-02-01 11:06:17 UTC
  • mfrom: (0.35.37 sid)
  • Revision ID: package-import@ubuntu.com-20140201110617-33h2xxk09dep0ui4
Tags: 0.98.1+dfsg-1ubuntu1
* Merge from Debian unstable.  Remaining changes:
  - Drop build-dep on electric-fence (in Universe)
  - Add apparmor profiles for clamd and freshclam along with maintainer
    script changes
  - Add autopkgtest

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* TomsFastMath, a fast ISO C bignum library.
 
2
 * That project is public domain and free for all purposes.
 
3
 * fp_toradix_n included in TomsFastMath specification (tfm.h), but unimplemented until now.
 
4
 * It is based on conversion of mp_toradix_n from libtommath
 
5
 * Will send the body of this function back to the LibTom projects, if they want it
 
6
 */
 
7
#include "bignum_fast.h"
 
8
 
 
9
int fp_toradix_n(fp_int *a, char *str, int radix, int maxlen)
 
10
{
 
11
  int     digs;
 
12
  fp_int  t;
 
13
  fp_digit d;
 
14
  char   *_s = str;
 
15
 
 
16
  /* check range of the maxlen, radix */
 
17
  if (maxlen < 2 || radix < 2 || radix > 64) {
 
18
    return FP_VAL;
 
19
  }
 
20
 
 
21
  /* quick out if its zero */
 
22
  if (fp_iszero(a) == 1) {
 
23
     *str++ = '0';
 
24
     *str = '\0';
 
25
     return FP_OKAY;
 
26
  }
 
27
 
 
28
  fp_init_copy(&t, a);
 
29
 
 
30
  /* if it is negative output a - */
 
31
  if (t.sign == FP_NEG) {
 
32
    ++_s;
 
33
    *str++ = '-';
 
34
    t.sign = FP_ZPOS;
 
35
    --maxlen;
 
36
  }
 
37
 
 
38
  digs = 0;
 
39
  while (fp_iszero (&t) == FP_NO) {
 
40
    if (--maxlen < 1) {
 
41
       /* no more room */
 
42
       break;
 
43
    }
 
44
    fp_div_d (&t, (fp_digit) radix, &t, &d);
 
45
    *str++ = fp_s_rmap[d];
 
46
    ++digs;
 
47
  }
 
48
 
 
49
  /* reverse the digits of the string.  In this case _s points
 
50
   * to the first digit [exluding the sign] of the number]
 
51
   */
 
52
  fp_reverse ((unsigned char *)_s, digs);
 
53
 
 
54
  /* append a NULL so the string is properly terminated */
 
55
  *str = '\0';
 
56
  return FP_OKAY;
 
57
}
 
58