~ubuntu-branches/ubuntu/karmic/gnupg2/karmic-updates

« back to all changes in this revision

Viewing changes to mpi/mpi-bit.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Urlichs
  • Date: 2006-01-24 04:31:42 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060124043142-pbg192or6qxv3yk2
Tags: 1.9.20-1
* New Upstream version. Closes:#306890,#344530
  * Closes:#320490: gpg-protect-tool fails to decrypt PKCS-12 files 
* Depend on libopensc2-dev, not -1-. Closes:#348106

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mpi-bit.c  -  MPI bit level fucntions
 
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 <config.h>
 
22
#include <stdio.h>
 
23
#include <stdlib.h>
 
24
#include <assert.h>
 
25
#include "mpi-internal.h"
 
26
#include "longlong.h"
 
27
 
 
28
 
 
29
#ifdef MPI_INTERNAL_NEED_CLZ_TAB
 
30
#ifdef __STDC__
 
31
const
 
32
#endif
 
33
unsigned char
 
34
__clz_tab[] =
 
35
{
 
36
  0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
 
37
  6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
 
38
  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
 
39
  7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
 
40
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
 
41
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
 
42
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
 
43
  8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
 
44
};
 
45
#endif
 
46
 
 
47
 
 
48
#define A_LIMB_1 ((mpi_limb_t)1)
 
49
 
 
50
 
 
51
/****************
 
52
 * Sometimes we have MSL (most significant limbs) which are 0;
 
53
 * this is for some reasons not good, so this function removes them.
 
54
 */
 
55
void
 
56
mpi_normalize( MPI a )
 
57
{
 
58
    if( mpi_is_opaque (a) )
 
59
        return;
 
60
 
 
61
    for( ; a->nlimbs && !a->d[a->nlimbs-1]; a->nlimbs-- )
 
62
        ;
 
63
}
 
64
 
 
65
 
 
66
 
 
67
/****************
 
68
 * Return the number of bits in A.
 
69
 */
 
70
unsigned
 
71
mpi_get_nbits( MPI a )
 
72
{
 
73
    unsigned n;
 
74
 
 
75
    mpi_normalize( a );
 
76
    if( a->nlimbs ) {
 
77
        mpi_limb_t alimb = a->d[a->nlimbs-1];
 
78
        if( alimb )
 
79
            count_leading_zeros( n, alimb );
 
80
        else
 
81
            n = BITS_PER_MPI_LIMB;
 
82
        n = BITS_PER_MPI_LIMB - n + (a->nlimbs-1) * BITS_PER_MPI_LIMB;
 
83
    }
 
84
    else
 
85
        n = 0;
 
86
    return n;
 
87
}
 
88
 
 
89
 
 
90
/****************
 
91
 * Test whether bit N is set.
 
92
 */
 
93
int
 
94
mpi_test_bit( MPI a, unsigned n )
 
95
{
 
96
    unsigned limbno, bitno;
 
97
    mpi_limb_t limb;
 
98
 
 
99
    limbno = n / BITS_PER_MPI_LIMB;
 
100
    bitno  = n % BITS_PER_MPI_LIMB;
 
101
 
 
102
    if( limbno >= a->nlimbs )
 
103
        return 0; /* too far left: this is a 0 */
 
104
    limb = a->d[limbno];
 
105
    return (limb & (A_LIMB_1 << bitno))? 1: 0;
 
106
}
 
107
 
 
108
 
 
109
/****************
 
110
 * Set bit N of A.
 
111
 */
 
112
void
 
113
mpi_set_bit( MPI a, unsigned n )
 
114
{
 
115
    unsigned limbno, bitno;
 
116
 
 
117
    limbno = n / BITS_PER_MPI_LIMB;
 
118
    bitno  = n % BITS_PER_MPI_LIMB;
 
119
 
 
120
    if( limbno >= a->nlimbs ) { /* resize */
 
121
        if( a->alloced >= limbno )
 
122
            mpi_resize(a, limbno+1 );
 
123
        a->nlimbs = limbno+1;
 
124
    }
 
125
    a->d[limbno] |= (A_LIMB_1<<bitno);
 
126
}
 
127
 
 
128
/****************
 
129
 * Set bit N of A. and clear all bits above
 
130
 */
 
131
void
 
132
mpi_set_highbit( MPI a, unsigned n )
 
133
{
 
134
    unsigned limbno, bitno;
 
135
 
 
136
    limbno = n / BITS_PER_MPI_LIMB;
 
137
    bitno  = n % BITS_PER_MPI_LIMB;
 
138
 
 
139
    if( limbno >= a->nlimbs ) { /* resize */
 
140
        if( a->alloced >= limbno )
 
141
            mpi_resize(a, limbno+1 );
 
142
        a->nlimbs = limbno+1;
 
143
    }
 
144
    a->d[limbno] |= (A_LIMB_1<<bitno);
 
145
    for( bitno++; bitno < BITS_PER_MPI_LIMB; bitno++ )
 
146
        a->d[limbno] &= ~(A_LIMB_1 << bitno);
 
147
    a->nlimbs = limbno+1;
 
148
}
 
149
 
 
150
/****************
 
151
 * clear bit N of A and all bits above
 
152
 */
 
153
void
 
154
mpi_clear_highbit( MPI a, unsigned n )
 
155
{
 
156
    unsigned limbno, bitno;
 
157
 
 
158
    limbno = n / BITS_PER_MPI_LIMB;
 
159
    bitno  = n % BITS_PER_MPI_LIMB;
 
160
 
 
161
    if( limbno >= a->nlimbs )
 
162
        return; /* not allocated, so need to clear bits :-) */
 
163
 
 
164
    for( ; bitno < BITS_PER_MPI_LIMB; bitno++ )
 
165
        a->d[limbno] &= ~(A_LIMB_1 << bitno);
 
166
    a->nlimbs = limbno+1;
 
167
}
 
168
 
 
169
/****************
 
170
 * Clear bit N of A.
 
171
 */
 
172
void
 
173
mpi_clear_bit( MPI a, unsigned n )
 
174
{
 
175
    unsigned limbno, bitno;
 
176
 
 
177
    limbno = n / BITS_PER_MPI_LIMB;
 
178
    bitno  = n % BITS_PER_MPI_LIMB;
 
179
 
 
180
    if( limbno >= a->nlimbs )
 
181
        return; /* don't need to clear this bit, it's to far to left */
 
182
    a->d[limbno] &= ~(A_LIMB_1 << bitno);
 
183
}
 
184
 
 
185
 
 
186
/****************
 
187
 * Shift A by N bits to the right
 
188
 * FIXME: should use alloc_limb if X and A are same.
 
189
 */
 
190
void
 
191
mpi_rshift( MPI x, MPI a, unsigned n )
 
192
{
 
193
    mpi_ptr_t xp;
 
194
    mpi_size_t xsize;
 
195
 
 
196
    xsize = a->nlimbs;
 
197
    x->sign = a->sign;
 
198
    RESIZE_IF_NEEDED(x, xsize);
 
199
    xp = x->d;
 
200
 
 
201
    if( xsize ) {
 
202
        mpihelp_rshift( xp, a->d, xsize, n);
 
203
        MPN_NORMALIZE( xp, xsize);
 
204
    }
 
205
    x->nlimbs = xsize;
 
206
}
 
207
 
 
208
 
 
209
/****************
 
210
 * Shift A by COUNT limbs to the left
 
211
 * This is used only within the MPI library
 
212
 */
 
213
void
 
214
mpi_lshift_limbs( MPI a, unsigned int count )
 
215
{
 
216
    mpi_ptr_t ap = a->d;
 
217
    int n = a->nlimbs;
 
218
    int i;
 
219
 
 
220
    if( !count || !n )
 
221
        return;
 
222
 
 
223
    RESIZE_IF_NEEDED( a, n+count );
 
224
 
 
225
    for( i = n-1; i >= 0; i-- )
 
226
        ap[i+count] = ap[i];
 
227
    for(i=0; i < count; i++ )
 
228
        ap[i] = 0;
 
229
    a->nlimbs += count;
 
230
}
 
231
 
 
232
 
 
233
/****************
 
234
 * Shift A by COUNT limbs to the right
 
235
 * This is used only within the MPI library
 
236
 */
 
237
void
 
238
mpi_rshift_limbs( MPI a, unsigned int count )
 
239
{
 
240
    mpi_ptr_t ap = a->d;
 
241
    mpi_size_t n = a->nlimbs;
 
242
    unsigned int i;
 
243
 
 
244
    if( count >= n ) {
 
245
        a->nlimbs = 0;
 
246
        return;
 
247
    }
 
248
 
 
249
    for( i = 0; i < n - count; i++ )
 
250
        ap[i] = ap[i+count];
 
251
    ap[i] = 0;
 
252
    a->nlimbs -= count;
 
253
}
 
254
 
 
255