summaryrefslogtreecommitdiffstats
path: root/mpi/mpi.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-02-26 22:34:14 -0800
committerKaz Kylheku <kaz@kylheku.com>2016-02-26 22:34:14 -0800
commit875c0c2d16fbcaa61bfc46da58533ab0890bf513 (patch)
treedcc2c644127dc38c3b2a6b4cfda12491c8368399 /mpi/mpi.c
parenta9e1bec854f6c0343dfb2cb32879cdb31d84f209 (diff)
downloadtxr-875c0c2d16fbcaa61bfc46da58533ab0890bf513.tar.gz
txr-875c0c2d16fbcaa61bfc46da58533ab0890bf513.tar.bz2
txr-875c0c2d16fbcaa61bfc46da58533ab0890bf513.zip
Functions for converting between buffers and integers.
These functions convert between positive integers, and fixed-size memory buffers representing pure binary numbers in big endian byte order. This functionality will be used in some upcoming networking code. * arith.c (num_from_buffer, num_to_buffer): New functions. * arith.h (num_from_buffer, num_to_buffer): Declared. * mpi/mpi.c (mp_to_unsigned_buf): New function. * mpi/mpi.h (mp_to_unsigned_buf): Declared.
Diffstat (limited to 'mpi/mpi.c')
-rw-r--r--mpi/mpi.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/mpi/mpi.c b/mpi/mpi.c
index c52e47bd..64cfb25a 100644
--- a/mpi/mpi.c
+++ b/mpi/mpi.c
@@ -3010,6 +3010,32 @@ mp_err mp_to_unsigned_bin(mp_int *mp, unsigned char *str)
/* }}} */
+mp_err mp_to_unsigned_buf(mp_int *mp, unsigned char *str, int size)
+{
+ mp_digit *dp, *end;
+ unsigned char *spos;
+
+ ARGCHK(mp != NULL && str != NULL && size >= 0, MP_BADARG);
+
+ for (spos = str + size, dp = DIGITS(mp), end = dp + USED(mp); dp < end; dp++) {
+ int ix;
+ mp_digit d = *dp;
+
+ for (ix = 0; ix < (int) sizeof(mp_digit); ++ix) {
+ if (dp + 1 == end && d == 0)
+ break;
+ ARGCHK(spos >= str, MP_RANGE);
+ *--spos = d & 0xFF;
+ d >>= 8;
+ }
+ }
+
+ while (spos > str)
+ *--spos = 0;
+
+ return MP_OKAY;
+}
+
/* {{{ mp_count_bits(mp) */
int mp_count_bits(mp_int *mp)