chiark / gitweb /
Document disorder-playrtp --command in CHANGES.html
[disorder] / lib / basen.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
5aff007d 3 * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
460b9539 4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
460b9539 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
460b9539 8 * (at your option) any later version.
e7eb3a27
RK
9 *
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.
14 *
460b9539 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
460b9539 17 */
14ad73b9
RK
18/** @file lib/basen.c @brief Arbitrary base conversion
19 *
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.
23 */
460b9539 24
05b75f8d 25#include "common.h"
460b9539 26
27#include "basen.h"
28
d7b6f0d1 29/** @brief Test whether v is 0
30 * @param v Pointer to bigendian bignum
31 * @param nwords Length of bignum
32 * @return !v
33 */
460b9539 34static int zero(const unsigned long *v, int nwords) {
35 int n;
36
37 for(n = 0; n < nwords && !v[n]; ++n)
38 ;
39 return n == nwords;
40}
41
d7b6f0d1 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)
46 * @return Remainder
47 *
48 * The quotient is stored in @p v.
49 */
460b9539 50static unsigned divide(unsigned long *v, int nwords, unsigned long m) {
51 unsigned long r = 0, a, b;
52 int n;
53
54 /* we do the divide 16 bits at a time */
55 for(n = 0; n < nwords; ++n) {
56 a = v[n] >> 16;
57 b = v[n] & 0xFFFF;
58 a += r << 16;
59 r = a % m;
60 a /= m;
61 b += r << 16;
62 r = b % m;
63 b /= m;
64 v[n] = (a << 16) + b;
65 }
66 return r;
67}
68
d7b6f0d1 69/** @brief Convert v to a chosen base
abe28bfe 70 * @param v Pointer to bigendian bignum (modified!)
d7b6f0d1 71 * @param nwords Length of bignum
72 * @param buffer Output buffer
73 * @param bufsize Size of output buffer
74 * @param base Number base (2..62)
75 * @return 0 on success, -1 if the buffer is too small
76 *
77 * Converts @p v to a string in the given base using decimal digits, lower case
78 * letter sand upper case letters as digits.
79 */
460b9539 80int basen(unsigned long *v,
81 int nwords,
82 char buffer[],
83 size_t bufsize,
84 unsigned base) {
85 size_t i = bufsize;
86 static const char chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
87
88 do {
abe28bfe
RK
89 if(i <= 1)
90 return -1; /* overflow */
460b9539 91 buffer[--i] = chars[divide(v, nwords, base)];
92 } while(!zero(v, nwords));
93 memmove(buffer, buffer + i, bufsize - i);
94 buffer[bufsize - i] = 0;
95 return 0;
96}
97
98/*
99Local Variables:
100c-basic-offset:2
101comment-column:40
102fill-column:79
103End:
104*/