]> pd.if.org Git - zpackage/blob - libtomcrypt/src/math/radix_to_bin.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / math / radix_to_bin.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9 #include "tomcrypt.h"
10
11 /**
12    @file radix_to_bin.c
13    Convert data from a specific radix to binary.
14    Steffen Jaeckel
15 */
16
17 /**
18    Convert data from a specific radix to binary
19
20       The default MPI descriptors #ltm_desc, #tfm_desc and #gmp_desc
21       have the following restrictions on parameters:
22
23         \p in    - NUL-terminated char buffer
24
25         \p radix - 2..64
26
27    @param in    The input
28    @param radix The radix of the input
29    @param out   The output buffer
30    @param len   [in/out] The length of the output buffer
31
32    @return CRYPT_OK on success.
33 */
34 int radix_to_bin(const void *in, int radix, void *out, unsigned long *len)
35 {
36    unsigned long l;
37    void* mpi;
38    int err;
39
40    LTC_ARGCHK(in  != NULL);
41    LTC_ARGCHK(len != NULL);
42
43    if ((err = mp_init(&mpi)) != CRYPT_OK) return err;
44    if ((err = mp_read_radix(mpi, in, radix)) != CRYPT_OK) goto LBL_ERR;
45
46    if ((l = mp_unsigned_bin_size(mpi)) > *len) {
47       *len = l;
48       err = CRYPT_BUFFER_OVERFLOW;
49       goto LBL_ERR;
50    }
51    *len = l;
52
53    if ((err = mp_to_unsigned_bin(mpi, out)) != CRYPT_OK) goto LBL_ERR;
54
55 LBL_ERR:
56    mp_clear(mpi);
57    return err;
58 }
59
60 /* ref:         $Format:%D$ */
61 /* git commit:  $Format:%H$ */
62 /* commit time: $Format:%ai$ */