2 * This file is part of DisOrder.
3 * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file lib/basen.c @brief Arbitrary base conversion
20 * The functions in this file handle arbitrary-size non-negative integers,
21 * represented as a bigendian (MSW first) sequence of @c unsigned @c long
22 * words. The words themselves use the native byte order.
29 /** @brief Test whether v is 0
30 * @param v Pointer to bigendian bignum
31 * @param nwords Length of bignum
34 static int zero(const uint32_t *v, int nwords) {
37 for(n = 0; n < nwords && !v[n]; ++n)
42 /** @brief Divide v by m returning the remainder.
43 * @param v Pointer to bigendian bignum
44 * @param nwords Length of bignum
45 * @param m Divisor (must not be 0)
48 * The quotient is stored in @p v.
50 static unsigned divide(uint32_t *v, int nwords, unsigned long m) {
51 unsigned long r = 0, a, b;
54 /* we do the divide 16 bits at a time */
55 for(n = 0; n < nwords; ++n) {
69 /** @brief Multiple v by m and add a
70 * @param v Pointer to bigendian bignum
71 * @param nwords Length of bignum
72 * @param m Value to multiply by
73 * @param a Value to add
74 * @return 0 on success, non-0 on overflow
78 static int mla(uint32_t *v, int nwords, uint32_t m, uint32_t a) {
83 const uint64_t p = (uint64_t)v[n] * m + carry;
84 carry = (uint32_t)(p >> 32);
88 /* If there is still a carry then we overflowed */
92 static const char basen_chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
94 /** @brief Convert v to a chosen base
95 * @param v Pointer to bigendian bignum (modified!)
96 * @param nwords Length of bignum
97 * @param buffer Output buffer
98 * @param bufsize Size of output buffer
99 * @param base Number base (2..62)
100 * @return 0 on success, -1 if the buffer is too small
102 * Converts @p v to a string in the given base using decimal digits, lower case
103 * letters and upper case letters as digits.
105 * The inverse of nesab().
107 int basen(uint32_t *v,
116 return -1; /* overflow */
117 buffer[--i] = basen_chars[divide(v, nwords, base)];
118 } while(!zero(v, nwords));
119 memmove(buffer, buffer + i, bufsize - i);
120 buffer[bufsize - i] = 0;
124 /** @brief Convert a string back to a large integer in an arbitrary base
125 * @param v Where to store result as a bigendian bignum
126 * @param nwords Space available in @p v
127 * @param s Input string
128 * @param base Number base (2..62)
129 * @return 0 on success, non-0 on overflow or invalid input
131 * The inverse of basen(). If the number is much smaller than the buffer then
132 * the first words will be 0.
134 int nesab(uint32_t *v,
138 /* Initialize to 0 */
139 memset(v, 0, nwords * sizeof *v);
141 const char *dp = strchr(basen_chars, *s++);
144 if(mla(v, nwords, base, dp - basen_chars))