-
+ AFFA0E93E6625CAD75CD10C454B493CBEBF061AECA3185E45B6571A26AE10F03947A1EF363C0898609F95CC737053E45A73926B22F14F4EC6A800410915D3FF9mpi/mpi-scan.c(0 . 0)(1 . 138)
7955 /* mpi-scan.c - MPI functions
7956 * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
7957 *
7958 * This file is part of GnuPG.
7959 *
7960 * GnuPG is free software; you can redistribute it and/or modify
7961 * it under the terms of the GNU General Public License as published by
7962 * the Free Software Foundation; either version 3 of the License, or
7963 * (at your option) any later version.
7964 *
7965 * GnuPG is distributed in the hope that it will be useful,
7966 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7967 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7968 * GNU General Public License for more details.
7969 *
7970 * You should have received a copy of the GNU General Public License
7971 * along with this program; if not, see <http://www.gnu.org/licenses/>.
7972 */
7973
7974 #include <config.h>
7975 #include <stdio.h>
7976 #include <stdlib.h>
7977 #include "mpi-internal.h"
7978 #include "longlong.h"
7979
7980 /****************
7981 * Scan through an mpi and return byte for byte. a -1 is returned to indicate
7982 * the end of the mpi. Scanning is done from the lsb to the msb, returned
7983 * values are in the range of 0 .. 255.
7984 *
7985 * FIXME: This code is VERY ugly!
7986 */
7987 #if 0 /* Code is not used */
7988 int
7989 mpi_getbyte( MPI a, unsigned idx )
7990 {
7991 int i, j;
7992 unsigned n;
7993 mpi_ptr_t ap;
7994 mpi_limb_t limb;
7995
7996 ap = a->d;
7997 for(n=0,i=0; i < a->nlimbs; i++ ) {
7998 limb = ap[i];
7999 for( j=0; j < BYTES_PER_MPI_LIMB; j++, n++ )
8000 if( n == idx )
8001 return (limb >> j*8) & 0xff;
8002 }
8003 return -1;
8004 }
8005 #endif /* Code is not used */
8006
8007
8008 /****************
8009 * Put a value at position IDX into A. idx counts from lsb to msb
8010 */
8011 /* FIXME: There is a problem with the long constants which should have
8012 a LL prefix or better the macros we use at other places. */
8013 #if 0 /* Code is not used */
8014 void
8015 mpi_putbyte( MPI a, unsigned idx, int xc )
8016 {
8017
8018 int i, j;
8019 unsigned n;
8020 mpi_ptr_t ap;
8021 mpi_limb_t limb, c;
8022
8023 c = xc & 0xff;
8024 ap = a->d;
8025 for(n=0,i=0; i < a->alloced; i++ ) {
8026 limb = ap[i];
8027 for( j=0; j < BYTES_PER_MPI_LIMB; j++, n++ )
8028 if( n == idx ) {
8029 #if BYTES_PER_MPI_LIMB == 4
8030 if( j == 0 )
8031 limb = (limb & 0xffffff00) | c;
8032 else if( j == 1 )
8033 limb = (limb & 0xffff00ff) | (c<<8);
8034 else if( j == 2 )
8035 limb = (limb & 0xff00ffff) | (c<<16);
8036 else
8037 limb = (limb & 0x00ffffff) | (c<<24);
8038 #elif BYTES_PER_MPI_LIMB == 8
8039 if( j == 0 )
8040 limb = (limb & 0xffffffffffffff00) | c;
8041 else if( j == 1 )
8042 limb = (limb & 0xffffffffffff00ff) | (c<<8);
8043 else if( j == 2 )
8044 limb = (limb & 0xffffffffff00ffff) | (c<<16);
8045 else if( j == 3 )
8046 limb = (limb & 0xffffffff00ffffff) | (c<<24);
8047 else if( j == 4 )
8048 limb = (limb & 0xffffff00ffffffff) | (c<<32);
8049 else if( j == 5 )
8050 limb = (limb & 0xffff00ffffffffff) | (c<<40);
8051 else if( j == 6 )
8052 limb = (limb & 0xff00ffffffffffff) | (c<<48);
8053 else
8054 limb = (limb & 0x00ffffffffffffff) | (c<<56);
8055 #else
8056 #error please enhance this function, its ugly - i know.
8057 #endif
8058 if( a->nlimbs <= i )
8059 a->nlimbs = i+1;
8060 ap[i] = limb;
8061 return;
8062 }
8063 }
8064 abort(); /* index out of range */
8065 }
8066 #endif /* Code is not used */
8067
8068
8069 /****************
8070 * Count the number of zerobits at the low end of A
8071 */
8072 unsigned int
8073 mpi_trailing_zeros( MPI a )
8074 {
8075 unsigned n, count = 0;
8076
8077 for(n=0; n < a->nlimbs; n++ ) {
8078 if( a->d[n] ) {
8079 unsigned nn;
8080 mpi_limb_t alimb = a->d[n];
8081
8082 count_trailing_zeros( nn, alimb );
8083 count += nn;
8084 break;
8085 }
8086 count += BITS_PER_MPI_LIMB;
8087 }
8088 return count;
8089
8090 }
8091
8092