]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add base32hex implementation by Peter Conrad
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 21 Sep 2020 19:42:47 +0000 (21:42 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 23 Sep 2020 07:41:38 +0000 (09:41 +0200)
Implementation copied verbatim from (a patch in)
<https://github.com/pmconrad/tinydnssec>.

LICENSE.adoc
src/third_party/CMakeLists.txt
src/third_party/base32hex.c [new file with mode: 0644]
src/third_party/base32hex.h [new file with mode: 0644]

index 20ca0a44982990c9d9e50a9cedf98a447cad79fd..b6a18140be3159e1fa44a385d6ba3f775d59f53f 100644 (file)
@@ -52,6 +52,29 @@ the GPL: that is, if separated from the ccache sources, they may be usable
 under less restrictive terms.
 
 
+src/third_party/base32hex.[hc]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+This base32hex implementation comes from
+<https://github.com/pmconrad/tinydnssec>.
+
+-------------------------------------------------------------------------------
+(C) 2012 Peter Conrad <conrad@quisquis.de>
+
+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 <http://www.gnu.org/licenses/>.
+-------------------------------------------------------------------------------
+
+
 src/third_party/blake3/*.[hcS]
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
index d4876cdaa9ef3caa56a04720d689039d62e0f7b7..7c62046a129e0e18f3ac1d73e09d2795f6b56f01 100644 (file)
@@ -1,4 +1,4 @@
-set(third_party_source_files format.cpp xxhash.c)
+set(third_party_source_files base32hex.c format.cpp xxhash.c)
 
 if(NOT MSVC)
   list(APPEND third_party_source_files getopt_long.c)
diff --git a/src/third_party/base32hex.c b/src/third_party/base32hex.c
new file mode 100644 (file)
index 0000000..354c043
--- /dev/null
@@ -0,0 +1,79 @@
+/* (C) 2012 Peter Conrad <conrad@quisquis.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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
diff --git a/src/third_party/base32hex.h b/src/third_party/base32hex.h
new file mode 100644 (file)
index 0000000..0ea92ab
--- /dev/null
@@ -0,0 +1,23 @@
+/* (C) 2012 Peter Conrad <conrad@quisquis.de>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _BASE32_HEX_H
+#define _BASE32_HEX_H
+
+#include <stdint.h>
+
+extern unsigned int base32hex(char *out, uint8_t *in, unsigned int len);
+
+#endif