diff options
author | Moritz Schulte <mo@g10code.com> | 2005-02-16 19:48:35 +0000 |
---|---|---|
committer | Moritz Schulte <mo@g10code.com> | 2005-02-16 19:48:35 +0000 |
commit | 7892671f7e9fbe7dbd02a4a7cef0a9c784de9c76 (patch) | |
tree | 5b755662049616c73593e1d9cf2b7064740b1fb8 /mpi/mpiutil.c | |
parent | 3c4632b4cccc600244879881cbb2292b1b68ca93 (diff) | |
download | libgcrypt-7892671f7e9fbe7dbd02a4a7cef0a9c784de9c76.tar.gz |
2005-02-16 Moritz Schulte <moritz@g10code.com>
* mpiutil.c (_gcry_mpi_alloc_limb_space): Rewritten, fixed memory
corruption.
Diffstat (limited to 'mpi/mpiutil.c')
-rw-r--r-- | mpi/mpiutil.c | 45 |
1 files changed, 35 insertions, 10 deletions
diff --git a/mpi/mpiutil.c b/mpi/mpiutil.c index 2a0bafab..8c99bf77 100644 --- a/mpi/mpiutil.c +++ b/mpi/mpiutil.c @@ -75,16 +75,13 @@ _gcry_mpi_alloc_secure( unsigned nlimbs ) mpi_ptr_t _gcry_mpi_alloc_limb_space( unsigned int nlimbs, int secure ) { - size_t len = nlimbs * sizeof(mpi_limb_t); - mpi_ptr_t p = NULL; - - if (!nlimbs) - { - p = secure? gcry_xmalloc_secure( 1 ) : gcry_xmalloc( 1 ); - *p = 0; - } - else - p = secure? gcry_xmalloc_secure( len ) : gcry_xmalloc( len ); + mpi_ptr_t p; + size_t len; + + len = (nlimbs ? nlimbs : 1) * sizeof (mpi_limb_t); + p = secure ? gcry_xmalloc_secure (len) : gcry_xmalloc (len); + if (! nlimbs) + *p = 0; return p; } @@ -305,6 +302,34 @@ _gcry_mpi_set_ui( gcry_mpi_t w, unsigned long u) w->flags = 0; } +gcry_err_code_t +_gcry_mpi_get_ui (gcry_mpi_t w, unsigned long *u) +{ + gcry_err_code_t err = GPG_ERR_NO_ERROR; + unsigned long x = 0; + + if (w->nlimbs > 1) + err = GPG_ERR_TOO_LARGE; + else if (w->nlimbs == 1) + x = w->d[0]; + else + x = 0; + + if (! err) + *u = x; + + return err; +} + +gcry_error_t +gcry_mpi_get_ui (gcry_mpi_t w, unsigned long *u) +{ + gcry_err_code_t err = GPG_ERR_NO_ERROR; + + err = _gcry_mpi_get_ui (w, u); + + return gcry_error (err); +} gcry_mpi_t _gcry_mpi_alloc_set_ui( unsigned long u) |