summaryrefslogtreecommitdiffstats
path: root/src/lib/hex-dec.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 09:51:24 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 09:51:24 +0000
commitf7548d6d28c313cf80e6f3ef89aed16a19815df1 (patch)
treea3f6f2a3f247293bee59ecd28e8cd8ceb6ca064a /src/lib/hex-dec.c
parentInitial commit. (diff)
downloaddovecot-f7548d6d28c313cf80e6f3ef89aed16a19815df1.tar.xz
dovecot-f7548d6d28c313cf80e6f3ef89aed16a19815df1.zip
Adding upstream version 1:2.3.19.1+dfsg1.upstream/1%2.3.19.1+dfsg1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--src/lib/hex-dec.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/lib/hex-dec.c b/src/lib/hex-dec.c
new file mode 100644
index 0000000..1c68001
--- /dev/null
+++ b/src/lib/hex-dec.c
@@ -0,0 +1,38 @@
+/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "hex-dec.h"
+
+void dec2hex(unsigned char *hexstr, uintmax_t dec, unsigned int hexstr_size)
+{
+ unsigned int i;
+
+ for (i = 0; i < hexstr_size; i++) {
+ unsigned int value = dec & 0x0f;
+ if (value < 10)
+ hexstr[hexstr_size-i-1] = value + '0';
+ else
+ hexstr[hexstr_size-i-1] = value - 10 + 'A';
+ dec >>= 4;
+ }
+}
+
+uintmax_t hex2dec(const unsigned char *data, unsigned int len)
+{
+ unsigned int i;
+ uintmax_t value = 0;
+
+ for (i = 0; i < len; i++) {
+ value = value*0x10;
+ if (data[i] >= '0' && data[i] <= '9')
+ value += data[i]-'0';
+ else if (data[i] >= 'A' && data[i] <= 'F')
+ value += data[i]-'A' + 10;
+ else if (data[i] >= 'a' && data[i] <= 'f')
+ value += data[i]-'a' + 10;
+ else
+ return 0;
+ }
+ return value;
+}
+