Created. MP integer binary encoding/decoding functions.
authorPekka Riikonen <priikone@silcnet.org>
Mon, 10 Jul 2000 05:37:24 +0000 (05:37 +0000)
committerPekka Riikonen <priikone@silcnet.org>
Mon, 10 Jul 2000 05:37:24 +0000 (05:37 +0000)
lib/silcmath/Makefile.am
lib/silcmath/mpbin.c [new file with mode: 0644]
lib/silcmath/mpbin.h [new file with mode: 0644]

index 436ad72d221e5788591da4ddca6888965b9c63e3..2aa9ec48bef8b117bf5730ed66be5f07be3cedd3 100644 (file)
@@ -24,7 +24,8 @@ noinst_LIBRARIES = libsilcmath.a
 
 libsilcmath_a_SOURCES = \
        silcprimegen.c \
-       modinv.c
+       modinv.c \
+       mpbin.c
 
 EXTRA_DIST = *.h
 
diff --git a/lib/silcmath/mpbin.c b/lib/silcmath/mpbin.c
new file mode 100644 (file)
index 0000000..3e8cc61
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+
+  mpbin.c
+
+  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+
+  Copyright (C) 2000 Pekka Riikonen
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+  
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+*/
+
+#include "silcincludes.h"
+
+/* Encodes MP integer into binary data. Returns allocated data that
+   must be free'd by the caller. */
+
+unsigned char *silc_mp_mp2bin(SilcInt *val, unsigned int *ret_len)
+{
+  int i;
+  unsigned int size;
+  unsigned char *ret;
+  SilcInt tmp;
+
+  size = (silc_mp_sizeinbase(val, 2) + 7) / 8;
+  ret = silc_calloc(size, sizeof(*ret));
+
+  silc_mp_init_set(&tmp, val);
+
+  for (i = size; i > 0; i--) {
+    ret[i - 1] = (unsigned char)(silc_mp_get_ui(&tmp) & 0xff);
+    silc_mp_fdiv_q_2exp(&tmp, &tmp, 8);
+  }
+
+  silc_mp_clear(&tmp);
+
+  if (*ret_len)
+    *ret_len = size;
+
+  return ret;
+}
+
+/* Decodes binary data into MP integer. The integer sent as argument
+   must be initialized. */
+
+void silc_mp_bin2mp(unsigned char *data, unsigned int len, SilcInt *ret)
+{
+  int i;
+
+  silc_mp_set_ui(ret, 0);
+
+  for (i = 0; i < len; i++) {
+    silc_mp_mul_2exp(ret, ret, 8);
+    silc_mp_add_ui(ret, ret, data[i]);
+  }
+}
diff --git a/lib/silcmath/mpbin.h b/lib/silcmath/mpbin.h
new file mode 100644 (file)
index 0000000..41ebf12
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+
+  mpbin.h
+  
+  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+
+  Copyright (C) 2000 Pekka Riikonen
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+  
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+*/
+
+#ifndef MPBIN_H
+#define MPBIN_H
+
+unsigned char *silc_mp_mp2bin(SilcInt *val, unsigned int *ret_len);
+void silc_mp_bin2mp(unsigned char *data, unsigned int len, SilcInt *ret);
+
+#endif