X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcmath%2Fmpbin.c;h=c8ed53488992608503eeee8dd2a9025089599672;hb=838fe0781769e5734459fdba84b1b1794574da7e;hp=ea3070e58485500b58b34db8f2a833b98469e58b;hpb=8fd8212bcd16f2b53fbedff2a9b9a4e8c15b9695;p=crypto.git diff --git a/lib/silcmath/mpbin.c b/lib/silcmath/mpbin.c index ea3070e5..c8ed5348 100644 --- a/lib/silcmath/mpbin.c +++ b/lib/silcmath/mpbin.c @@ -16,9 +16,8 @@ GNU General Public License for more details. */ -/* $Id$ */ -#include "silc.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 @@ -78,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; +}