LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/hcrypto/libtommath - bn_mp_radix_size.c (source / functions) Hit Total Coverage
Test: coverage report for master 2f515e9b Lines: 0 29 0.0 %
Date: 2024-04-21 15:09:00 Functions: 0 1 0.0 %

          Line data    Source code
       1             : #include "tommath_private.h"
       2             : #ifdef BN_MP_RADIX_SIZE_C
       3             : /* LibTomMath, multiple-precision integer library -- Tom St Denis */
       4             : /* SPDX-License-Identifier: Unlicense */
       5             : 
       6             : /* returns size of ASCII representation */
       7           0 : mp_err mp_radix_size(const mp_int *a, int radix, int *size)
       8             : {
       9           0 :    mp_err  err;
      10           0 :    int digs;
      11           0 :    mp_int   t;
      12           0 :    mp_digit d;
      13             : 
      14           0 :    *size = 0;
      15             : 
      16             :    /* make sure the radix is in range */
      17           0 :    if ((radix < 2) || (radix > 64)) {
      18           0 :       return MP_VAL;
      19             :    }
      20             : 
      21           0 :    if (MP_IS_ZERO(a)) {
      22           0 :       *size = 2;
      23           0 :       return MP_OKAY;
      24             :    }
      25             : 
      26             :    /* special case for binary */
      27           0 :    if (radix == 2) {
      28           0 :       *size = (mp_count_bits(a) + ((a->sign == MP_NEG) ? 1 : 0) + 1);
      29           0 :       return MP_OKAY;
      30             :    }
      31             : 
      32             :    /* digs is the digit count */
      33           0 :    digs = 0;
      34             : 
      35             :    /* if it's negative add one for the sign */
      36           0 :    if (a->sign == MP_NEG) {
      37           0 :       ++digs;
      38             :    }
      39             : 
      40             :    /* init a copy of the input */
      41           0 :    if ((err = mp_init_copy(&t, a)) != MP_OKAY) {
      42           0 :       return err;
      43             :    }
      44             : 
      45             :    /* force temp to positive */
      46           0 :    t.sign = MP_ZPOS;
      47             : 
      48             :    /* fetch out all of the digits */
      49           0 :    while (!MP_IS_ZERO(&t)) {
      50           0 :       if ((err = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
      51           0 :          goto LBL_ERR;
      52             :       }
      53           0 :       ++digs;
      54             :    }
      55             : 
      56             :    /* return digs + 1, the 1 is for the NULL byte that would be required. */
      57           0 :    *size = digs + 1;
      58           0 :    err = MP_OKAY;
      59             : 
      60           0 : LBL_ERR:
      61           0 :    mp_clear(&t);
      62           0 :    return err;
      63             : }
      64             : 
      65             : #endif

Generated by: LCOV version 1.14