aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/utils/bit_ops.h89
-rw-r--r--src/utils/bswap.h39
-rw-r--r--src/utils/charset.h33
-rw-r--r--src/utils/util.cpp (renamed from src/util.cpp)0
-rw-r--r--src/utils/util.h38
-rw-r--r--src/utils/version.cpp (renamed from src/version.cpp)0
-rw-r--r--src/utils/version.h33
-rw-r--r--src/utils/xor_buf.h65
8 files changed, 297 insertions, 0 deletions
diff --git a/src/utils/bit_ops.h b/src/utils/bit_ops.h
new file mode 100644
index 000000000..594d130a4
--- /dev/null
+++ b/src/utils/bit_ops.h
@@ -0,0 +1,89 @@
+/*************************************************
+* Bit/Word Operations Header File *
+* (C) 1999-2008 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_BIT_OPS_H__
+#define BOTAN_BIT_OPS_H__
+
+#include <botan/types.h>
+
+namespace Botan {
+
+/*************************************************
+* Return true iff arg is 2**n for some n > 0 *
+* T should be an unsigned integer type *
+*************************************************/
+template<typename T>
+inline bool power_of_2(T arg)
+ {
+ return ((arg != 0 && arg != 1) && ((arg & (arg-1)) == 0));
+ }
+
+/*************************************************
+* Return the index of the highest set bit
+* T is an unsigned integer type
+*************************************************/
+template<typename T>
+inline u32bit high_bit(T n)
+ {
+ for(u32bit i = 8*sizeof(T); i > 0; --i)
+ if((n >> (i - 1)) & 0x01)
+ return i;
+ return 0;
+ }
+
+/*************************************************
+* Return the index of the lowest set bit *
+*************************************************/
+template<typename T>
+inline u32bit low_bit(T n)
+ {
+ for(u32bit i = 0; i != 8*sizeof(T); ++i)
+ if((n >> i) & 0x01)
+ return (i + 1);
+ return 0;
+ }
+
+/*************************************************
+* Return the number of significant bytes in n *
+*************************************************/
+template<typename T>
+inline u32bit significant_bytes(T n)
+ {
+ for(u32bit j = 0; j != sizeof(T); ++j)
+ if(get_byte(j, n))
+ return sizeof(T)-j;
+ return 0;
+ }
+
+/*************************************************
+* Return the Hamming weight of n *
+*************************************************/
+template<typename T>
+inline u32bit hamming_weight(T n)
+ {
+ const byte NIBBLE_WEIGHTS[] = {
+ 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
+
+ u32bit weight = 0;
+ for(u32bit i = 0; i != 2*sizeof(T); ++i)
+ weight += NIBBLE_WEIGHTS[(n >> (4*i)) & 0x0F];
+ return weight;
+ }
+
+/*************************************************
+* Count the trailing zero bits in n *
+*************************************************/
+template<typename T>
+inline u32bit ctz(T n)
+ {
+ for(int i = 0; i != 8*sizeof(T); ++i)
+ if((n >> i) & 0x01)
+ return i;
+ return 8*sizeof(T);
+ }
+
+}
+
+#endif
diff --git a/src/utils/bswap.h b/src/utils/bswap.h
new file mode 100644
index 000000000..e38d3c6fa
--- /dev/null
+++ b/src/utils/bswap.h
@@ -0,0 +1,39 @@
+/*************************************************
+* Byte Swapping Operations Header File *
+* (C) 1999-2008 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_BSWAP_H__
+#define BOTAN_BSWAP_H__
+
+#include <botan/types.h>
+#include <botan/rotate.h>
+
+namespace Botan {
+
+/*************************************************
+* Byte Swapping Functions *
+*************************************************/
+inline u16bit reverse_bytes(u16bit input)
+ {
+ return rotate_left(input, 8);
+ }
+
+inline u32bit reverse_bytes(u32bit input)
+ {
+ input = ((input & 0xFF00FF00) >> 8) | ((input & 0x00FF00FF) << 8);
+ return rotate_left(input, 16);
+ }
+
+inline u64bit reverse_bytes(u64bit input)
+ {
+ u32bit hi = ((input >> 40) & 0x00FF00FF) | ((input >> 24) & 0xFF00FF00);
+ u32bit lo = ((input & 0xFF00FF00) >> 8) | ((input & 0x00FF00FF) << 8);
+ hi = (hi << 16) | (hi >> 16);
+ lo = (lo << 16) | (lo >> 16);
+ return (static_cast<u64bit>(lo) << 32) | hi;
+ }
+
+}
+
+#endif
diff --git a/src/utils/charset.h b/src/utils/charset.h
new file mode 100644
index 000000000..ac9f501a0
--- /dev/null
+++ b/src/utils/charset.h
@@ -0,0 +1,33 @@
+/*************************************************
+* Character Set Handling Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_CHARSET_H__
+#define BOTAN_CHARSET_H__
+
+#include <botan/types.h>
+#include <botan/enums.h>
+#include <string>
+
+namespace Botan {
+
+namespace Charset {
+
+/*************************************************
+* Character Set Handling *
+*************************************************/
+std::string transcode(const std::string&, Character_Set, Character_Set);
+
+bool is_digit(char);
+bool is_space(char);
+bool caseless_cmp(char, char);
+
+byte char2digit(char);
+char digit2char(byte);
+
+}
+
+}
+
+#endif
diff --git a/src/util.cpp b/src/utils/util.cpp
index af1c62ebd..af1c62ebd 100644
--- a/src/util.cpp
+++ b/src/utils/util.cpp
diff --git a/src/utils/util.h b/src/utils/util.h
new file mode 100644
index 000000000..879acd1db
--- /dev/null
+++ b/src/utils/util.h
@@ -0,0 +1,38 @@
+/*************************************************
+* Utility Functions Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_UTIL_H__
+#define BOTAN_UTIL_H__
+
+#include <botan/types.h>
+
+namespace Botan {
+
+/*************************************************
+* Time Access Functions *
+*************************************************/
+BOTAN_DLL u64bit system_time();
+
+/*************************************************
+* Memory Locking Functions *
+*************************************************/
+BOTAN_DLL void lock_mem(void*, u32bit);
+BOTAN_DLL void unlock_mem(void*, u32bit);
+
+/*************************************************
+* Misc Utility Functions *
+*************************************************/
+BOTAN_DLL u32bit round_up(u32bit, u32bit);
+BOTAN_DLL u32bit round_down(u32bit, u32bit);
+
+/*************************************************
+* Work Factor Estimates *
+*************************************************/
+BOTAN_DLL u32bit entropy_estimate(const byte[], u32bit);
+BOTAN_DLL u32bit dl_work_factor(u32bit);
+
+}
+
+#endif
diff --git a/src/version.cpp b/src/utils/version.cpp
index 09deb53d6..09deb53d6 100644
--- a/src/version.cpp
+++ b/src/utils/version.cpp
diff --git a/src/utils/version.h b/src/utils/version.h
new file mode 100644
index 000000000..6604b1885
--- /dev/null
+++ b/src/utils/version.h
@@ -0,0 +1,33 @@
+/*************************************************
+* Version Information Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_VERSION_H__
+#define BOTAN_VERSION_H__
+
+#include <botan/types.h>
+#include <string>
+
+namespace Botan {
+
+/*************************************************
+* Get information describing the version *
+*************************************************/
+BOTAN_DLL std::string version_string();
+BOTAN_DLL u32bit version_major();
+BOTAN_DLL u32bit version_minor();
+BOTAN_DLL u32bit version_patch();
+
+/*************************************************
+* Macros for compile-time version checks *
+*************************************************/
+#define BOTAN_VERSION_CODE_FOR(a,b,c) ((a << 16) | (b << 8) | (c))
+
+#define BOTAN_VERSION_CODE BOTAN_VERSION_CODE_FOR(BOTAN_VERSION_MAJOR, \
+ BOTAN_VERSION_MINOR, \
+ BOTAN_VERSION_PATCH)
+
+}
+
+#endif
diff --git a/src/utils/xor_buf.h b/src/utils/xor_buf.h
new file mode 100644
index 000000000..0a71aef3e
--- /dev/null
+++ b/src/utils/xor_buf.h
@@ -0,0 +1,65 @@
+/*************************************************
+* Xor Operations Header File *
+* (C) 1999-2008 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_XOR_BUF_H__
+#define BOTAN_XOR_BUF_H__
+
+#include <botan/types.h>
+
+namespace Botan {
+
+/*************************************************
+* XOR Arrays *
+*************************************************/
+inline void xor_buf(byte out[], const byte in[], u32bit length)
+ {
+ while(length >= 8)
+ {
+#if BOTAN_UNALIGNED_LOADSTOR_OK
+ *reinterpret_cast<u64bit*>(out) ^= *reinterpret_cast<const u64bit*>(in);
+#else
+ out[0] ^= in[0]; out[1] ^= in[1];
+ out[2] ^= in[2]; out[3] ^= in[3];
+ out[4] ^= in[4]; out[5] ^= in[5];
+ out[6] ^= in[6]; out[7] ^= in[7];
+#endif
+
+ out += 8; in += 8; length -= 8;
+ }
+ for(u32bit j = 0; j != length; ++j)
+ out[j] ^= in[j];
+ }
+
+/*************************************************
+* XOR Arrays *
+*************************************************/
+inline void xor_buf(byte out[],
+ const byte in[],
+ const byte in2[],
+ u32bit length)
+ {
+ while(length >= 8)
+ {
+#if BOTAN_UNALIGNED_LOADSTOR_OK
+ *reinterpret_cast<u64bit*>(out) =
+ *reinterpret_cast<const u64bit*>(in) ^
+ *reinterpret_cast<const u64bit*>(in2);
+#else
+ out[0] = in[0] ^ in2[0]; out[1] = in[1] ^ in2[1];
+ out[2] = in[2] ^ in2[2]; out[3] = in[3] ^ in2[3];
+ out[4] = in[4] ^ in2[4]; out[5] = in[5] ^ in2[5];
+ out[6] = in[6] ^ in2[6]; out[7] = in[7] ^ in2[7];
+#endif
+
+ in += 8; in2 += 8; out += 8; length -= 8;
+ }
+
+ for(u32bit j = 0; j != length; ++j)
+ out[j] = in[j] ^ in2[j];
+ }
+
+}
+
+#endif