Added silc_mp_format and silc_mp_unformat MP integer encoder/decoder master
authorPekka Riikonen <priikone@silcnet.org>
Wed, 4 Jun 2008 15:24:30 +0000 (18:24 +0300)
committerPekka Riikonen <priikone@silcnet.org>
Wed, 4 Jun 2008 15:24:30 +0000 (18:24 +0300)
These functions can be used with silc_buffer_format and
silc_buffer_unformat and the SILC_STR_FUNC formatter.

lib/silcmath/mpbin.c
lib/silcmath/silcmp.h

index d9aff6b16b021b5762a31d3885c90c44b843028c..c8ed53488992608503eeee8dd2a9025089599672 100644 (file)
@@ -77,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;
+}
index 2d3c4019932c2ef8b9c4edd832945fbf532f2ff5..5ff3d34772b70e15b409a0a4b0b400f2e9fc817e 100644 (file)
@@ -713,4 +713,60 @@ SilcBool silc_mp_or(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
  ***/
 SilcBool silc_mp_xor(SilcMPInt *dst, SilcMPInt *mp1, SilcMPInt *mp2);
 
+/* Utility functions */
+
+/****f* silcmath/silc_mp_format
+ *
+ * SYNOPSIS
+ *
+ *    int silc_mp_format(SilcStack stack, SilcBuffer buffer,
+ *                       void *value, void *context)
+ *
+ * DESCRIPTION
+ *
+ *    MP integer encoding function to be used with silc_buffer_format and
+ *    SILC_STR_FUNC formatter.  The encoded data is of following format:
+ *
+ *      SILC_STR_UINT32, integer_len,
+ *      SILC_STR_DATA    integer_data
+ *
+ * EXAMPLE
+ *
+ *    silc_buffer_format(buf,
+ *                       SILC_STR_FUNC(silc_mp_format, mpint, NULL),
+ *                       SILC_STR_END);
+ *
+ ***/
+int silc_mp_format(SilcStack stack, SilcBuffer buffer,
+                  void *value, void *context);
+
+/****f* silcmath/silc_mp_unformat
+ *
+ * SYNOPSIS
+ *
+ *    int silc_mp_unformat(SilcStack stack, SilcBuffer buffer,
+ *                         void **value, void *context)
+ *
+ * DESCRIPTION
+ *
+ *    MP integer decoding function to be used with silc_buffer_unformat and
+ *    SILC_STR_FUNC unformatter.  This function expects that the length of
+ *    the integer is encoded as 32-bit integer and precedes the integer
+ *    data.
+ *
+ * EXAMPLE
+ *
+ *    SilcMPint mp_ptr;
+ *
+ *    silc_mp_init(&mpint);
+ *    mp_ptr = &mpint;
+ *
+ *    silc_buffer_unformat(buf,
+ *                         SILC_STR_FUNC(silc_mp_unformat, &mp_ptr, NULL),
+ *                         SILC_STR_END);
+ *
+ ***/
+int silc_mp_unformat(SilcStack stack, SilcBuffer buffer,
+                    void **value, void *context);
+
 #endif