Added SILC Thread Queue API
[silc.git] / lib / silcmath / mpbin.c
index 8074f2bc706429416fd087563a8ac187bdfb0d35..ea3070e58485500b58b34db8f2a833b98469e58b 100644 (file)
@@ -2,15 +2,14 @@
 
   mpbin.c
 
-  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+  Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2000 - 2001 Pekka Riikonen
+  Copyright (C) 2000 - 2005 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.
-  
+  the Free Software Foundation; version 2 of the License.
+
   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
 */
 /* $Id$ */
 
-#include "silcincludes.h"
+#include "silc.h"
 
 /* Encodes MP integer into binary data. Returns allocated data that
    must be free'd by the caller. If `len' is provided the destination
    buffer is allocated that large. If zero then the size is approximated. */
 
-unsigned char *silc_mp_mp2bin(SilcMPInt *val, uint32 len,
-                             uint32 *ret_len)
+unsigned char *silc_mp_mp2bin(SilcMPInt *val, SilcUInt32 len,
+                             SilcUInt32 *ret_len)
 {
-  int i;
-  uint32 size;
+  SilcUInt32 size;
   unsigned char *ret;
-  SilcMPInt tmp;
 
   size = (len ? len : ((silc_mp_sizeinbase(val, 2) + 7) / 8));
   ret = silc_calloc(size, sizeof(*ret));
-  
-  silc_mp_init(&tmp);
-  silc_mp_set(&tmp, val);
+  if (!ret)
+    return NULL;
 
-  for (i = size; i > 0; i--) {
-    ret[i - 1] = (unsigned char)(silc_mp_get_ui(&tmp) & 0xff);
-    silc_mp_div_2exp(&tmp, &tmp, 8);
-  }
-
-  silc_mp_uninit(&tmp);
+  silc_mp_mp2bin_noalloc(val, ret, size);
 
   if (ret_len)
     *ret_len = size;
@@ -56,10 +47,10 @@ unsigned char *silc_mp_mp2bin(SilcMPInt *val, uint32 len,
    returned into `dst' and it's length to the `ret_len'. */
 
 void silc_mp_mp2bin_noalloc(SilcMPInt *val, unsigned char *dst,
-                           uint32 dst_len)
+                           SilcUInt32 dst_len)
 {
   int i;
-  uint32 size = dst_len;
+  SilcUInt32 size = dst_len;
   SilcMPInt tmp;
 
   silc_mp_init(&tmp);
@@ -76,7 +67,7 @@ void silc_mp_mp2bin_noalloc(SilcMPInt *val, unsigned char *dst,
 /* Decodes binary data into MP integer. The integer sent as argument
    must be initialized. */
 
-void silc_mp_bin2mp(unsigned char *data, uint32 len, SilcMPInt *ret)
+void silc_mp_bin2mp(unsigned char *data, SilcUInt32 len, SilcMPInt *ret)
 {
   int i;