aboutsummaryrefslogtreecommitdiff
path: root/libssh/gcrypt_missing.c
diff options
context:
space:
mode:
authorAndreas Schneider <mail@cynapses.org>2009-05-04 13:52:27 +0000
committerAndreas Schneider <mail@cynapses.org>2009-05-04 13:52:27 +0000
commit61ebfcfa5cbd3de2d402fb6cc14448db79f0f685 (patch)
treed1c682608419a535cd80a61eb119f182689c612d /libssh/gcrypt_missing.c
parentff60d8ce229e320338ce9760ae12004c2755014c (diff)
downloadlibssh-61ebfcfa5cbd3de2d402fb6cc14448db79f0f685.tar.gz
libssh-61ebfcfa5cbd3de2d402fb6cc14448db79f0f685.tar.xz
libssh-61ebfcfa5cbd3de2d402fb6cc14448db79f0f685.zip
Improve own gcrypt function.
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@711 7dcaeef0-15fb-0310-b436-a5af3365683c
Diffstat (limited to 'libssh/gcrypt_missing.c')
-rw-r--r--libssh/gcrypt_missing.c63
1 files changed, 42 insertions, 21 deletions
diff --git a/libssh/gcrypt_missing.c b/libssh/gcrypt_missing.c
index 44f31b45..a9a129b7 100644
--- a/libssh/gcrypt_missing.c
+++ b/libssh/gcrypt_missing.c
@@ -27,53 +27,74 @@
#include "libssh/priv.h"
#ifdef HAVE_LIBGCRYPT
-int my_gcry_dec2bn(bignum *bn, const char *data)
-{
+int my_gcry_dec2bn(bignum *bn, const char *data) {
int count;
-
+
*bn = bignum_new();
+ if (*bn == NULL) {
+ return 0;
+ }
gcry_mpi_set_ui(*bn, 0);
- for (count = 0; data[count]; ++count)
- {
+ for (count = 0; data[count]; count++) {
gcry_mpi_mul_ui(*bn, *bn, 10);
gcry_mpi_add_ui(*bn, *bn, data[count] - '0');
}
+
return count;
}
-char *my_gcry_bn2dec(bignum bn)
-{
+char *my_gcry_bn2dec(bignum bn) {
+ bignum bndup, num, ten;
+ char *ret;
int count, count2;
int size, rsize;
- char *ret;
- bignum bndup, num, ten;
char decnum;
-
+
size = gcry_mpi_get_nbits(bn) * 3;
rsize = size / 10 + size / 1000 + 2;
+
ret = malloc(rsize + 1);
- if (!gcry_mpi_cmp_ui(bn, 0))
+ if (ret == NULL) {
+ return NULL;
+ }
+
+ if (!gcry_mpi_cmp_ui(bn, 0)) {
strcpy(ret, "0");
- else
- {
- for (bndup = gcry_mpi_copy(bn), ten = bignum_new(), num = bignum_new(),
- bignum_set_word(ten, 10), count = rsize; count; --count)
- {
+ } else {
+ ten = bignum_new();
+ if (ten == NULL) {
+ SAFE_FREE(ret);
+ return NULL;
+ }
+
+ num = bignum_new();
+ if (num == NULL) {
+ SAFE_FREE(ret);
+ bignum_free(ten);
+ return NULL;
+ }
+
+ for (bndup = gcry_mpi_copy(bn), bignum_set_word(ten, 10), count = rsize;
+ count; count--) {
gcry_mpi_div(bndup, num, bndup, ten, 0);
- for (decnum = 0, count2 = gcry_mpi_get_nbits(num); count2; decnum *= 2,
- decnum += (gcry_mpi_test_bit(num, count2 - 1) ? 1 : 0), --count2)
- ;
+ for (decnum = 0, count2 = gcry_mpi_get_nbits(num); count2;
+ decnum *= 2, decnum += (gcry_mpi_test_bit(num, count2 - 1) ? 1 : 0),
+ count2--)
+ ;
ret[count - 1] = decnum + '0';
}
- for (count = 0; count < rsize && ret[count] == '0'; ++count)
+ for (count = 0; count < rsize && ret[count] == '0'; count++)
;
- for (count2 = 0; count2 < rsize - count; ++count2)
+ for (count2 = 0; count2 < rsize - count; ++count2) {
ret[count2] = ret[count2 + count];
+ }
ret[count2] = 0;
bignum_free(num);
bignum_free(bndup);
bignum_free(ten);
}
+
return ret;
}
+
#endif