mpiutil.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include "mpi-internal.h"
  21. /****************
  22. * Note: It was a bad idea to use the number of limbs to allocate
  23. * because on a alpha the limbs are large but we normally need
  24. * integers of n bits - So we should chnage this to bits (or bytes).
  25. *
  26. * But mpi_alloc is used in a lot of places :-)
  27. */
  28. MPI mpi_alloc(unsigned nlimbs)
  29. {
  30. MPI a;
  31. a = kmalloc(sizeof *a, GFP_KERNEL);
  32. if (!a)
  33. return a;
  34. if (nlimbs) {
  35. a->d = mpi_alloc_limb_space(nlimbs);
  36. if (!a->d) {
  37. kfree(a);
  38. return NULL;
  39. }
  40. } else {
  41. a->d = NULL;
  42. }
  43. a->alloced = nlimbs;
  44. a->nlimbs = 0;
  45. a->sign = 0;
  46. a->flags = 0;
  47. a->nbits = 0;
  48. return a;
  49. }
  50. EXPORT_SYMBOL_GPL(mpi_alloc);
  51. mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs)
  52. {
  53. size_t len = nlimbs * sizeof(mpi_limb_t);
  54. if (!len)
  55. return NULL;
  56. return kmalloc(len, GFP_KERNEL);
  57. }
  58. void mpi_free_limb_space(mpi_ptr_t a)
  59. {
  60. if (!a)
  61. return;
  62. kzfree(a);
  63. }
  64. void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs)
  65. {
  66. mpi_free_limb_space(a->d);
  67. a->d = ap;
  68. a->alloced = nlimbs;
  69. }
  70. /****************
  71. * Resize the array of A to NLIMBS. the additional space is cleared
  72. * (set to 0) [done by m_realloc()]
  73. */
  74. int mpi_resize(MPI a, unsigned nlimbs)
  75. {
  76. void *p;
  77. if (nlimbs <= a->alloced)
  78. return 0; /* no need to do it */
  79. if (a->d) {
  80. p = kmalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
  81. if (!p)
  82. return -ENOMEM;
  83. memcpy(p, a->d, a->alloced * sizeof(mpi_limb_t));
  84. kzfree(a->d);
  85. a->d = p;
  86. } else {
  87. a->d = kzalloc(nlimbs * sizeof(mpi_limb_t), GFP_KERNEL);
  88. if (!a->d)
  89. return -ENOMEM;
  90. }
  91. a->alloced = nlimbs;
  92. return 0;
  93. }
  94. void mpi_free(MPI a)
  95. {
  96. if (!a)
  97. return;
  98. if (a->flags & 4)
  99. kzfree(a->d);
  100. else
  101. mpi_free_limb_space(a->d);
  102. if (a->flags & ~7)
  103. pr_info("invalid flag value in mpi\n");
  104. kfree(a);
  105. }
  106. EXPORT_SYMBOL_GPL(mpi_free);
  107. MODULE_DESCRIPTION("Multiprecision maths library");
  108. MODULE_LICENSE("GPL");