LCOV - code coverage report
Current view: top level - lib/mpi - mpiutil.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 34 48 70.8 %
Date: 2020-09-30 20:25:40 Functions: 5 6 83.3 %
Branches: 14 28 50.0 %

           Branch data     Line data    Source code
       1                 :            : /* mpiutil.ac  -  Utility functions for MPI
       2                 :            :  * Copyright (C) 1998, 1999 Free Software Foundation, Inc.
       3                 :            :  *
       4                 :            :  * This file is part of GnuPG.
       5                 :            :  *
       6                 :            :  * GnuPG is free software; you can redistribute it and/or modify
       7                 :            :  * it under the terms of the GNU General Public License as published by
       8                 :            :  * the Free Software Foundation; either version 2 of the License, or
       9                 :            :  * (at your option) any later version.
      10                 :            :  *
      11                 :            :  * GnuPG is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
      19                 :            :  */
      20                 :            : 
      21                 :            : #include "mpi-internal.h"
      22                 :            : 
      23                 :            : /****************
      24                 :            :  * Note:  It was a bad idea to use the number of limbs to allocate
      25                 :            :  *        because on a alpha the limbs are large but we normally need
      26                 :            :  *        integers of n bits - So we should chnage this to bits (or bytes).
      27                 :            :  *
      28                 :            :  *        But mpi_alloc is used in a lot of places :-)
      29                 :            :  */
      30                 :       3312 : MPI mpi_alloc(unsigned nlimbs)
      31                 :            : {
      32                 :            :         MPI a;
      33                 :            : 
      34                 :            :         a = kmalloc(sizeof *a, GFP_KERNEL);
      35         [ +  - ]:       3312 :         if (!a)
      36                 :            :                 return a;
      37                 :            : 
      38         [ +  + ]:       3312 :         if (nlimbs) {
      39                 :       2484 :                 a->d = mpi_alloc_limb_space(nlimbs);
      40         [ -  + ]:       2484 :                 if (!a->d) {
      41                 :          0 :                         kfree(a);
      42                 :          0 :                         return NULL;
      43                 :            :                 }
      44                 :            :         } else {
      45                 :        828 :                 a->d = NULL;
      46                 :            :         }
      47                 :            : 
      48                 :       3312 :         a->alloced = nlimbs;
      49                 :       3312 :         a->nlimbs = 0;
      50                 :       3312 :         a->sign = 0;
      51                 :       3312 :         a->flags = 0;
      52                 :       3312 :         a->nbits = 0;
      53                 :       3312 :         return a;
      54                 :            : }
      55                 :            : EXPORT_SYMBOL_GPL(mpi_alloc);
      56                 :            : 
      57                 :       5796 : mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs)
      58                 :            : {
      59                 :       5796 :         size_t len = nlimbs * sizeof(mpi_limb_t);
      60                 :            : 
      61         [ +  - ]:       5796 :         if (!len)
      62                 :            :                 return NULL;
      63                 :            : 
      64                 :       5796 :         return kmalloc(len, GFP_KERNEL);
      65                 :            : }
      66                 :            : 
      67                 :       3312 : void mpi_free_limb_space(mpi_ptr_t a)
      68                 :            : {
      69   [ +  -  #  #  :       6624 :         if (!a)
                   +  - ]
      70                 :       3312 :                 return;
      71                 :            : 
      72                 :       6624 :         kzfree(a);
      73                 :            : }
      74                 :            : 
      75                 :          0 : void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
      76                 :            : {
      77                 :          0 :         mpi_free_limb_space(a->d);
      78                 :          0 :         a->d = ap;
      79                 :          0 :         a->alloced = nlimbs;
      80                 :          0 : }
      81                 :            : 
      82                 :            : /****************
      83                 :            :  * Resize the array of A to NLIMBS. the additional space is cleared
      84                 :            :  * (set to 0) [done by m_realloc()]
      85                 :            :  */
      86                 :        828 : int mpi_resize(MPI a, unsigned nlimbs)
      87                 :            : {
      88                 :            :         void *p;
      89                 :            : 
      90         [ +  - ]:        828 :         if (nlimbs <= a->alloced)
      91                 :            :                 return 0;       /* no need to do it */
      92                 :            : 
      93         [ -  + ]:        828 :         if (a->d) {
      94                 :          0 :                 p = kmalloc_array(nlimbs, sizeof(mpi_limb_t), GFP_KERNEL);
      95         [ #  # ]:          0 :                 if (!p)
      96                 :            :                         return -ENOMEM;
      97                 :          0 :                 memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
      98                 :          0 :                 kzfree(a->d);
      99                 :          0 :                 a->d = p;
     100                 :            :         } else {
     101                 :        828 :                 a->d = kcalloc(nlimbs, sizeof(mpi_limb_t), GFP_KERNEL);
     102         [ +  - ]:        828 :                 if (!a->d)
     103                 :            :                         return -ENOMEM;
     104                 :            :         }
     105                 :        828 :         a->alloced = nlimbs;
     106                 :        828 :         return 0;
     107                 :            : }
     108                 :            : 
     109                 :       6624 : void mpi_free(MPI a)
     110                 :            : {
     111         [ +  + ]:       6624 :         if (!a)
     112                 :       6624 :                 return;
     113                 :            : 
     114         [ -  + ]:       3312 :         if (a->flags & 4)
     115                 :          0 :                 kzfree(a->d);
     116                 :            :         else
     117                 :       3312 :                 mpi_free_limb_space(a->d);
     118                 :            : 
     119         [ -  + ]:       3312 :         if (a->flags & ~7)
     120                 :          0 :                 pr_info("invalid flag value in mpi\n");
     121                 :       3312 :         kfree(a);
     122                 :            : }
     123                 :            : EXPORT_SYMBOL_GPL(mpi_free);
     124                 :            : 
     125                 :            : MODULE_DESCRIPTION("Multiprecision maths library");
     126                 :            : MODULE_LICENSE("GPL");

Generated by: LCOV version 1.14