mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-04-18 03:28:07 -05:00
Utility function: bignum_from_decimal.
This commit is contained in:
parent
6179c5cc7c
commit
9971da40c3
1
ssh.h
1
ssh.h
@ -630,6 +630,7 @@ Bignum bignum_rshift(Bignum number, int shift);
|
|||||||
Bignum bignum_lshift(Bignum number, int shift);
|
Bignum bignum_lshift(Bignum number, int shift);
|
||||||
int bignum_cmp(Bignum a, Bignum b);
|
int bignum_cmp(Bignum a, Bignum b);
|
||||||
char *bignum_decimal(Bignum x);
|
char *bignum_decimal(Bignum x);
|
||||||
|
Bignum bignum_from_decimal(const char *decimal);
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
void diagbn(char *prefix, Bignum md);
|
void diagbn(char *prefix, Bignum md);
|
||||||
|
28
sshbn.c
28
sshbn.c
@ -7,6 +7,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <ctype.h>
|
||||||
|
|
||||||
#include "misc.h"
|
#include "misc.h"
|
||||||
|
|
||||||
@ -102,6 +103,7 @@ typedef BignumInt *Bignum;
|
|||||||
|
|
||||||
BignumInt bnZero[1] = { 0 };
|
BignumInt bnZero[1] = { 0 };
|
||||||
BignumInt bnOne[2] = { 1, 1 };
|
BignumInt bnOne[2] = { 1, 1 };
|
||||||
|
BignumInt bnTen[2] = { 1, 10 };
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The Bignum format is an array of `BignumInt'. The first
|
* The Bignum format is an array of `BignumInt'. The first
|
||||||
@ -117,7 +119,7 @@ BignumInt bnOne[2] = { 1, 1 };
|
|||||||
* nonzero.
|
* nonzero.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Bignum Zero = bnZero, One = bnOne;
|
Bignum Zero = bnZero, One = bnOne, Ten = bnTen;
|
||||||
|
|
||||||
static Bignum newbn(int length)
|
static Bignum newbn(int length)
|
||||||
{
|
{
|
||||||
@ -1255,6 +1257,30 @@ Bignum bignum_from_bytes_le(const unsigned char *data, int nbytes)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Bignum bignum_from_decimal(const char *decimal)
|
||||||
|
{
|
||||||
|
Bignum result = copybn(Zero);
|
||||||
|
|
||||||
|
while (*decimal) {
|
||||||
|
Bignum tmp, tmp2;
|
||||||
|
|
||||||
|
if (!isdigit((unsigned char)*decimal)) {
|
||||||
|
freebn(result);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = bigmul(result, Ten);
|
||||||
|
tmp2 = bignum_from_long(*decimal - '0');
|
||||||
|
result = bigadd(tmp, tmp2);
|
||||||
|
freebn(tmp);
|
||||||
|
freebn(tmp2);
|
||||||
|
|
||||||
|
decimal++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
Bignum bignum_random_in_range(const Bignum lower, const Bignum upper)
|
Bignum bignum_random_in_range(const Bignum lower, const Bignum upper)
|
||||||
{
|
{
|
||||||
Bignum ret = NULL;
|
Bignum ret = NULL;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user