From 7f632c0640f174bbbc1deb532e3a3977d595d28a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henryk=20Pl=C3=B6tz?= Date: Fri, 3 Oct 2014 20:22:01 +0200 Subject: Apply djbdns-1.05-dnssec.patch SHA1 62e2ce1d31f1fe908fac84fc8bd049a12621810f, contained in tinydnssec-1.05-1.3.tar.bz2 Source was http://www.tinydnssec.org/download/tinydnssec-1.05-1.3.tar.bz2, SHA1 b33d5c3e0de67f6427aad8c00a99580b59804075 --- base32hex.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 base32hex.c (limited to 'base32hex.c') diff --git a/base32hex.c b/base32hex.c new file mode 100644 index 0000000..95826fd --- /dev/null +++ b/base32hex.c @@ -0,0 +1,79 @@ +/* (C) 2012 Peter Conrad + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "base32hex.h" + +#define to_32hex(c) ((c) < 10 ? (c) + '0' : (c) + 'a' - 10) + +/* out must point to a buffer of at least (len * 8 / 5) + 1 bytes. + * Encoded string is *not* padded. + * See RFC-4648. This implementation produces lowercase hex characters. + * Returns length of encoded string. + */ +unsigned int base32hex(char *out, uint8_t *in, unsigned int len) { +int buf = 0, bits = 0; +char *x = out; + + while (len-- > 0) { + buf <<= 8; + buf |= *in++; + bits += 8; + while (bits >= 5) { + char c = (buf >> (bits - 5)) & 0x1f; + *x++ = to_32hex(c); + bits -= 5; + } + } + if (bits > 0) { + char c = (buf << (5 - bits)) & 0x1f; + *x++ = to_32hex(c); + } + return x - out; +} + +#ifdef TEST +#include +#include +#include + +static void test(char *in, char *expected, int explen) { +char buf[255]; +int r; + + if ((r = base32hex(buf, in, strlen(in))) != explen) { + printf("Failed: b32h('%s') yields %d chars (expected %d)\n", + in, r, explen); + exit(1); + } + if (strncmp(buf, expected, r)) { + buf[r] = 0; + printf("Failed: b32h('%s') = '%s' (expected %s)\n", + in, buf, expected); + exit(1); + } +} + +int main(int argc, char **argv) { + test("", "", 0); + test("f", "co", 2); + test("fo", "cpng", 4); + test("foo", "cpnmu", 5); + test("foob", "cpnmuog", 7); + test("fooba", "cpnmuoj1", 8); + test("foobar", "cpnmuoj1e8", 10); + printf("Success!\n"); +} + +#endif -- cgit v1.2.3