Add mp_set_long and mp_set_ulong to the public API.

This commit is contained in:
nelsonb%netscape.com 2000-09-28 22:53:45 +00:00
Родитель cb37a54b7a
Коммит 58b6dcc398
2 изменённых файлов: 44 добавлений и 12 удалений

Просмотреть файл

@ -35,7 +35,7 @@
* the GPL. If you do not delete the provisions above, a recipient
* may use your version of this file under either the MPL or the GPL.
*
* $Id: mpi.c,v 1.24 2000/09/14 00:30:49 nelsonb%netscape.com Exp $
* $Id: mpi.c,v 1.25 2000/09/28 22:53:45 nelsonb%netscape.com Exp $
*/
#include "mpi-priv.h"
@ -346,18 +346,18 @@ mp_err mp_set_int(mp_int *mp, long z)
if(z == 0)
return MP_OKAY; /* shortcut for zero */
for(ix = sizeof(long) - 1; ix >= 0; ix--) {
if((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY)
return res;
res = s_mp_add_d(mp,
(mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX));
if(res != MP_OKAY)
return res;
if (sizeof v <= sizeof(mp_digit)) {
DIGIT(mp,0) = v;
} else {
for (ix = sizeof(long) - 1; ix >= 0; ix--) {
if ((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY)
return res;
res = s_mp_add_d(mp, (mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX));
if (res != MP_OKAY)
return res;
}
}
if(z < 0)
SIGN(mp) = NEG;
@ -367,6 +367,36 @@ mp_err mp_set_int(mp_int *mp, long z)
/* }}} */
/* {{{ mp_set_ulong(mp, z) */
mp_err mp_set_ulong(mp_int *mp, ulong z)
{
int ix;
mp_err res;
ARGCHK(mp != NULL, MP_BADARG);
mp_zero(mp);
if(z == 0)
return MP_OKAY; /* shortcut for zero */
if (sizeof z <= sizeof(mp_digit)) {
DIGIT(mp,0) = z;
} else {
for (ix = sizeof(long) - 1; ix >= 0; ix--) {
if ((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY)
return res;
res = s_mp_add_d(mp, (mp_digit)((z >> (ix * CHAR_BIT)) & UCHAR_MAX));
if (res != MP_OKAY)
return res;
}
}
return MP_OKAY;
} /* end mp_set_ulong() */
/* }}} */
/*------------------------------------------------------------------------*/
/* {{{ Digit arithmetic */

Просмотреть файл

@ -36,7 +36,7 @@
* may use your version of this file under either the MPL or the
* GPL.
*
* $Id: mpi.h,v 1.11 2000/09/14 00:30:50 nelsonb%netscape.com Exp $
* $Id: mpi.h,v 1.12 2000/09/28 22:53:45 nelsonb%netscape.com Exp $
*/
#ifndef _H_MPI_
@ -183,6 +183,8 @@ void mp_clear(mp_int *mp);
void mp_zero(mp_int *mp);
void mp_set(mp_int *mp, mp_digit d);
mp_err mp_set_int(mp_int *mp, long z);
#define mp_set_long(mp,z) mp_set_int(mp,z)
mp_err mp_set_ulong(mp_int *mp, ulong z);
/* Single digit arithmetic */
mp_err mp_add_d(const mp_int *a, mp_digit d, mp_int *b);