mpi-scan.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* mpi-scan.c - MPI functions
  2. * Copyright (C) 1998, 1999, 2000, 2001 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. #include "longlong.h"
  22. /****************
  23. * Scan through an mpi and return byte for byte. a -1 is returned to indicate
  24. * the end of the mpi. Scanning is done from the lsb to the msb, returned
  25. * values are in the range of 0 .. 255.
  26. *
  27. * FIXME: This code is VERY ugly!
  28. */
  29. int mpi_getbyte(const MPI a, unsigned idx)
  30. {
  31. int i, j;
  32. unsigned n;
  33. mpi_ptr_t ap;
  34. mpi_limb_t limb;
  35. ap = a->d;
  36. for (n = 0, i = 0; i < a->nlimbs; i++) {
  37. limb = ap[i];
  38. for (j = 0; j < BYTES_PER_MPI_LIMB; j++, n++)
  39. if (n == idx)
  40. return (limb >> j * 8) & 0xff;
  41. }
  42. return -1;
  43. }
  44. /****************
  45. * Put a value at position IDX into A. idx counts from lsb to msb
  46. */
  47. void mpi_putbyte(MPI a, unsigned idx, int xc)
  48. {
  49. int i, j;
  50. unsigned n;
  51. mpi_ptr_t ap;
  52. mpi_limb_t limb, c;
  53. c = xc & 0xff;
  54. ap = a->d;
  55. for (n = 0, i = 0; i < a->alloced; i++) {
  56. limb = ap[i];
  57. for (j = 0; j < BYTES_PER_MPI_LIMB; j++, n++)
  58. if (n == idx) {
  59. #if BYTES_PER_MPI_LIMB == 4
  60. if (j == 0)
  61. limb = (limb & 0xffffff00) | c;
  62. else if (j == 1)
  63. limb = (limb & 0xffff00ff) | (c << 8);
  64. else if (j == 2)
  65. limb = (limb & 0xff00ffff) | (c << 16);
  66. else
  67. limb = (limb & 0x00ffffff) | (c << 24);
  68. #elif BYTES_PER_MPI_LIMB == 8
  69. if (j == 0)
  70. limb = (limb & 0xffffffffffffff00) | c;
  71. else if (j == 1)
  72. limb =
  73. (limb & 0xffffffffffff00ff) | (c <<
  74. 8);
  75. else if (j == 2)
  76. limb =
  77. (limb & 0xffffffffff00ffff) | (c <<
  78. 16);
  79. else if (j == 3)
  80. limb =
  81. (limb & 0xffffffff00ffffff) | (c <<
  82. 24);
  83. else if (j == 4)
  84. limb =
  85. (limb & 0xffffff00ffffffff) | (c <<
  86. 32);
  87. else if (j == 5)
  88. limb =
  89. (limb & 0xffff00ffffffffff) | (c <<
  90. 40);
  91. else if (j == 6)
  92. limb =
  93. (limb & 0xff00ffffffffffff) | (c <<
  94. 48);
  95. else
  96. limb =
  97. (limb & 0x00ffffffffffffff) | (c <<
  98. 56);
  99. #else
  100. #error please enhance this function, its ugly - i know.
  101. #endif
  102. if (a->nlimbs <= i)
  103. a->nlimbs = i + 1;
  104. ap[i] = limb;
  105. return;
  106. }
  107. }
  108. log_bug("index out of range\n");
  109. }
  110. /****************
  111. * Count the number of zerobits at the low end of A
  112. */
  113. unsigned mpi_trailing_zeros(const MPI a)
  114. {
  115. unsigned n, count = 0;
  116. for (n = 0; n < a->nlimbs; n++) {
  117. if (a->d[n]) {
  118. unsigned nn;
  119. mpi_limb_t alimb = a->d[n];
  120. count_trailing_zeros(nn, alimb);
  121. count += nn;
  122. break;
  123. }
  124. count += BITS_PER_MPI_LIMB;
  125. }
  126. return count;
  127. }