Added silc_mp_format and silc_mp_unformat MP integer encoder/decoder
[crypto.git] / lib / silcmath / mpbin.c
index 29d80f7711e71e007b2ddb8d20dde27fd766cd9d..c8ed53488992608503eeee8dd2a9025089599672 100644 (file)
@@ -2,24 +2,22 @@
 
   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
   GNU General Public License for more details.
 
 */
-/* $Id$ */
 
-#include "silcincludes.h"
+#include "silccrypto.h"
 
 /* Encodes MP integer into binary data. Returns allocated data that
    must be free'd by the caller. If `len' is provided the destination
 unsigned char *silc_mp_mp2bin(SilcMPInt *val, SilcUInt32 len,
                              SilcUInt32 *ret_len)
 {
-  int i;
   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);
-
-  for (i = size; i > 0; i--) {
-    ret[i - 1] = (unsigned char)(silc_mp_get_ui(&tmp) & 0xff);
-    silc_mp_div_2exp(&tmp, &tmp, 8);
-  }
+  if (!ret)
+    return NULL;
 
-  silc_mp_uninit(&tmp);
+  silc_mp_mp2bin_noalloc(val, ret, size);
 
   if (ret_len)
     *ret_len = size;
@@ -87,3 +77,49 @@ void silc_mp_bin2mp(unsigned char *data, SilcUInt32 len, SilcMPInt *ret)
     silc_mp_add_ui(ret, ret, data[i]);
   }
 }
+
+/* MP integer encoding with silc_buffer_format. */
+
+int silc_mp_format(SilcStack stack, SilcBuffer buffer,
+                  void *value, void *context)
+{
+  SilcMPInt *mp = value;
+  unsigned char *m;
+  SilcUInt32 m_len;
+  int ret;
+
+  /* Encode */
+  m = silc_mp_mp2bin(mp, 0, &m_len);
+  if (!m)
+    return -1;
+
+  ret = silc_buffer_sformat(stack, buffer,
+                           SILC_STR_UINT32(m_len),
+                           SILC_STR_DATA(m, m_len),
+                           SILC_STR_END);
+
+  silc_free(m);
+
+  return ret;
+}
+
+/* MP integer decoding with silc_buffer_unformat. */
+
+int silc_mp_unformat(SilcStack stack, SilcBuffer buffer,
+                    void **value, void *context)
+{
+  SilcMPInt *mp = *value;
+  unsigned char *m;
+  SilcUInt32 m_len;
+  int ret;
+
+  ret = silc_buffer_sunformat(stack, buffer,
+                             SILC_STR_UI32_NSTRING(&m, &m_len),
+                             SILC_STR_END);
+  if (ret < 0)
+    return ret;
+
+  silc_mp_bin2mp(m, m_len, mp);
+
+  return ret;
+}