-
+ AFFA0E93E6625CAD75CD10C454B493CBEBF061AECA3185E45B6571A26AE10F03947A1EF363C0898609F95CC737053E45A73926B22F14F4EC6A800410915D3FF9
mpi/mpi-scan.c
(0 . 0)(1 . 138)
9972 /* mpi-scan.c - MPI functions
9973 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
9974 *
9975 * This file is part of GnuPG.
9976 *
9977 * GnuPG is free software; you can redistribute it and/or modify
9978 * it under the terms of the GNU General Public License as published by
9979 * the Free Software Foundation; either version 3 of the License, or
9980 * (at your option) any later version.
9981 *
9982 * GnuPG is distributed in the hope that it will be useful,
9983 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9984 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9985 * GNU General Public License for more details.
9986 *
9987 * You should have received a copy of the GNU General Public License
9988 * along with this program; if not, see <http://www.gnu.org/licenses/>.
9989 */
9990
9991 #include <config.h>
9992 #include <stdio.h>
9993 #include <stdlib.h>
9994 #include "mpi-internal.h"
9995 #include "longlong.h"
9996
9997 /****************
9998 * Scan through an mpi and return byte for byte. a -1 is returned to indicate
9999 * the end of the mpi. Scanning is done from the lsb to the msb, returned
10000 * values are in the range of 0 .. 255.
10001 *
10002 * FIXME: This code is VERY ugly!
10003 */
10004 #if 0 /* Code is not used */
10005 int
10006 mpi_getbyte( MPI a, unsigned idx )
10007 {
10008 int i, j;
10009 unsigned n;
10010 mpi_ptr_t ap;
10011 mpi_limb_t limb;
10012
10013 ap = a->d;
10014 for(n=0,i=0; i < a->nlimbs; i++ ) {
10015 limb = ap[i];
10016 for( j=0; j < BYTES_PER_MPI_LIMB; j++, n++ )
10017 if( n == idx )
10018 return (limb >> j*8) & 0xff;
10019 }
10020 return -1;
10021 }
10022 #endif /* Code is not used */
10023
10024
10025 /****************
10026 * Put a value at position IDX into A. idx counts from lsb to msb
10027 */
10028 /* FIXME: There is a problem with the long constants which should have
10029 a LL prefix or better the macros we use at other places. */
10030 #if 0 /* Code is not used */
10031 void
10032 mpi_putbyte( MPI a, unsigned idx, int xc )
10033 {
10034
10035 int i, j;
10036 unsigned n;
10037 mpi_ptr_t ap;
10038 mpi_limb_t limb, c;
10039
10040 c = xc & 0xff;
10041 ap = a->d;
10042 for(n=0,i=0; i < a->alloced; i++ ) {
10043 limb = ap[i];
10044 for( j=0; j < BYTES_PER_MPI_LIMB; j++, n++ )
10045 if( n == idx ) {
10046 #if BYTES_PER_MPI_LIMB == 4
10047 if( j == 0 )
10048 limb = (limb & 0xffffff00) | c;
10049 else if( j == 1 )
10050 limb = (limb & 0xffff00ff) | (c<<8);
10051 else if( j == 2 )
10052 limb = (limb & 0xff00ffff) | (c<<16);
10053 else
10054 limb = (limb & 0x00ffffff) | (c<<24);
10055 #elif BYTES_PER_MPI_LIMB == 8
10056 if( j == 0 )
10057 limb = (limb & 0xffffffffffffff00) | c;
10058 else if( j == 1 )
10059 limb = (limb & 0xffffffffffff00ff) | (c<<8);
10060 else if( j == 2 )
10061 limb = (limb & 0xffffffffff00ffff) | (c<<16);
10062 else if( j == 3 )
10063 limb = (limb & 0xffffffff00ffffff) | (c<<24);
10064 else if( j == 4 )
10065 limb = (limb & 0xffffff00ffffffff) | (c<<32);
10066 else if( j == 5 )
10067 limb = (limb & 0xffff00ffffffffff) | (c<<40);
10068 else if( j == 6 )
10069 limb = (limb & 0xff00ffffffffffff) | (c<<48);
10070 else
10071 limb = (limb & 0x00ffffffffffffff) | (c<<56);
10072 #else
10073 #error please enhance this function, its ugly - i know.
10074 #endif
10075 if( a->nlimbs <= i )
10076 a->nlimbs = i+1;
10077 ap[i] = limb;
10078 return;
10079 }
10080 }
10081 abort(); /* index out of range */
10082 }
10083 #endif /* Code is not used */
10084
10085
10086 /****************
10087 * Count the number of zerobits at the low end of A
10088 */
10089 unsigned int
10090 mpi_trailing_zeros( MPI a )
10091 {
10092 unsigned n, count = 0;
10093
10094 for(n=0; n < a->nlimbs; n++ ) {
10095 if( a->d[n] ) {
10096 unsigned nn;
10097 mpi_limb_t alimb = a->d[n];
10098
10099 count_trailing_zeros( nn, alimb );
10100 count += nn;
10101 break;
10102 }
10103 count += BITS_PER_MPI_LIMB;
10104 }
10105 return count;
10106
10107 }
10108
10109