summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util')
-rw-r--r--src/lib/util/chd.cpp14
-rw-r--r--src/lib/util/hashing.h54
-rw-r--r--src/lib/util/path_to_regex.cpp4
-rw-r--r--src/lib/util/path_to_regex.hpp4
-rw-r--r--src/lib/util/server_ws.hpp4
-rw-r--r--src/lib/util/sha1.hpp232
-rw-r--r--src/lib/util/unicode.cpp10
7 files changed, 181 insertions, 141 deletions
diff --git a/src/lib/util/chd.cpp b/src/lib/util/chd.cpp
index 0db64344861..dc7c334b1bf 100644
--- a/src/lib/util/chd.cpp
+++ b/src/lib/util/chd.cpp
@@ -885,7 +885,7 @@ chd_error chd_file::read_hunk(uint32_t hunknum, void *buffer)
// get a pointer to the map entry
uint64_t blockoffs;
uint32_t blocklen;
- uint32_t blockcrc;
+ util::crc32_t blockcrc;
uint8_t *rawmap;
uint8_t *dest = reinterpret_cast<uint8_t *>(buffer);
switch (m_version)
@@ -2147,12 +2147,12 @@ void chd_file::decompress_v5_map()
// read the reader
uint8_t rawbuf[16];
file_read(m_mapoffset, rawbuf, sizeof(rawbuf));
- uint32_t mapbytes = be_read(&rawbuf[0], 4);
- uint64_t firstoffs = be_read(&rawbuf[4], 6);
- uint16_t mapcrc = be_read(&rawbuf[10], 2);
- uint8_t lengthbits = rawbuf[12];
- uint8_t selfbits = rawbuf[13];
- uint8_t parentbits = rawbuf[14];
+ uint32_t const mapbytes = be_read(&rawbuf[0], 4);
+ uint64_t const firstoffs = be_read(&rawbuf[4], 6);
+ util::crc16_t const mapcrc = be_read(&rawbuf[10], 2);
+ uint8_t const lengthbits = rawbuf[12];
+ uint8_t const selfbits = rawbuf[13];
+ uint8_t const parentbits = rawbuf[14];
// now read the map
std::vector<uint8_t> compressed(mapbytes);
diff --git a/src/lib/util/hashing.h b/src/lib/util/hashing.h
index 906bad047b4..5e9c9134c97 100644
--- a/src/lib/util/hashing.h
+++ b/src/lib/util/hashing.h
@@ -15,10 +15,12 @@
#include "osdcore.h"
#include "corestr.h"
-#include <string>
#include "md5.h"
#include "sha1.h"
+#include <functional>
+#include <string>
+
namespace util {
//**************************************************************************
@@ -132,13 +134,23 @@ protected:
// final digest
struct crc32_t
{
- bool operator==(const crc32_t &rhs) const { return m_raw == rhs.m_raw; }
- bool operator!=(const crc32_t &rhs) const { return m_raw != rhs.m_raw; }
+ crc32_t() { }
+ constexpr crc32_t(const crc32_t &rhs) = default;
+ constexpr crc32_t(const uint32_t crc) : m_raw(crc) { }
+
+ constexpr bool operator==(const crc32_t &rhs) const { return m_raw == rhs.m_raw; }
+ constexpr bool operator!=(const crc32_t &rhs) const { return m_raw != rhs.m_raw; }
+
+ crc32_t &operator=(const crc32_t &rhs) = default;
crc32_t &operator=(const uint32_t crc) { m_raw = crc; return *this; }
- operator uint32_t() const { return m_raw; }
+
+ constexpr operator uint32_t() const { return m_raw; }
+
bool from_string(const char *string, int length = -1);
std::string as_string() const;
+
uint32_t m_raw;
+
static const crc32_t null;
};
@@ -178,13 +190,23 @@ protected:
// final digest
struct crc16_t
{
- bool operator==(const crc16_t &rhs) const { return m_raw == rhs.m_raw; }
- bool operator!=(const crc16_t &rhs) const { return m_raw != rhs.m_raw; }
+ crc16_t() { }
+ constexpr crc16_t(const crc16_t &rhs) = default;
+ constexpr crc16_t(const uint16_t crc) : m_raw(crc) { }
+
+ constexpr bool operator==(const crc16_t &rhs) const { return m_raw == rhs.m_raw; }
+ constexpr bool operator!=(const crc16_t &rhs) const { return m_raw != rhs.m_raw; }
+
+ crc16_t &operator=(const crc16_t &rhs) = default;
crc16_t &operator=(const uint16_t crc) { m_raw = crc; return *this; }
- operator uint16_t() const { return m_raw; }
+
+ constexpr operator uint16_t() const { return m_raw; }
+
bool from_string(const char *string, int length = -1);
std::string as_string() const;
+
uint16_t m_raw;
+
static const crc16_t null;
};
@@ -220,4 +242,22 @@ protected:
} // namespace util
+namespace std {
+
+template <> struct hash<::util::crc32_t>
+{
+ typedef ::util::crc32_t argument_type;
+ typedef std::size_t result_type;
+ result_type operator()(argument_type const & s) const { return std::hash<std::uint32_t>()(s); }
+};
+
+template <> struct hash<::util::crc16_t>
+{
+ typedef ::util::crc16_t argument_type;
+ typedef std::size_t result_type;
+ result_type operator()(argument_type const & s) const { return std::hash<std::uint16_t>()(s); }
+};
+
+} // namespace std
+
#endif // __HASHING_H__
diff --git a/src/lib/util/path_to_regex.cpp b/src/lib/util/path_to_regex.cpp
index 927ababbb8a..89a21a5e7f6 100644
--- a/src/lib/util/path_to_regex.cpp
+++ b/src/lib/util/path_to_regex.cpp
@@ -1,7 +1,7 @@
// license:MIT
// copyright-holders:Alfred Bratterud
-// NOTE: Author allowed MAME project to distribute this file under MIT
-// license. Other projects need to do it under Apache 2 license
+// NOTE: Author allowed MAME project to distribute this file under MIT
+// license. Other projects need to do it under Apache 2 license
//
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
diff --git a/src/lib/util/path_to_regex.hpp b/src/lib/util/path_to_regex.hpp
index 7608d77bb5d..a6b11a22031 100644
--- a/src/lib/util/path_to_regex.hpp
+++ b/src/lib/util/path_to_regex.hpp
@@ -1,7 +1,7 @@
// license:MIT
// copyright-holders:Alfred Bratterud
-// NOTE: Author allowed MAME project to distribute this file under MIT
-// license. Other projects need to do it under Apache 2 license
+// NOTE: Author allowed MAME project to distribute this file under MIT
+// license. Other projects need to do it under Apache 2 license
//
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
diff --git a/src/lib/util/server_ws.hpp b/src/lib/util/server_ws.hpp
index 170dc31ae63..b9d4721e96b 100644
--- a/src/lib/util/server_ws.hpp
+++ b/src/lib/util/server_ws.hpp
@@ -64,7 +64,7 @@ namespace webpp {
}
};
-
+
class Connection {
friend class SocketServerBase<socket_type>;
friend class SocketServer<socket_type>;
@@ -142,7 +142,7 @@ namespace webpp {
catch (...) {}
}
};
-
+
class Message : public std::istream {
friend class SocketServerBase<socket_type>;
diff --git a/src/lib/util/sha1.hpp b/src/lib/util/sha1.hpp
index 7befbd326f0..a293bebb7cc 100644
--- a/src/lib/util/sha1.hpp
+++ b/src/lib/util/sha1.hpp
@@ -36,76 +36,76 @@ namespace { // local
// Rotate an integer value to left.
inline unsigned int rol(unsigned int value, unsigned int steps) {
- return ((value << steps) | (value >> (32 - steps)));
+ return ((value << steps) | (value >> (32 - steps)));
}
// Sets the first 16 integers in the buffert to zero.
// Used for clearing the W buffert.
inline void clearWBuffert(unsigned int * buffert)
{
- for (int pos = 16; --pos >= 0;)
- {
- buffert[pos] = 0;
- }
+ for (int pos = 16; --pos >= 0;)
+ {
+ buffert[pos] = 0;
+ }
}
inline void innerHash(unsigned int * result, unsigned int * w)
{
- unsigned int a = result[0];
- unsigned int b = result[1];
- unsigned int c = result[2];
- unsigned int d = result[3];
- unsigned int e = result[4];
-
- int round = 0;
-
- #define sha1macro(func,val) \
- { \
- const unsigned int t = rol(a, 5) + (func) + e + val + w[round]; \
- e = d; \
- d = c; \
- c = rol(b, 30); \
- b = a; \
- a = t; \
- }
-
- while (round < 16)
- {
- sha1macro((b & c) | (~b & d), 0x5a827999)
- ++round;
- }
- while (round < 20)
- {
- w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
- sha1macro((b & c) | (~b & d), 0x5a827999)
- ++round;
- }
- while (round < 40)
- {
- w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
- sha1macro(b ^ c ^ d, 0x6ed9eba1)
- ++round;
- }
- while (round < 60)
- {
- w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
- sha1macro((b & c) | (b & d) | (c & d), 0x8f1bbcdc)
- ++round;
- }
- while (round < 80)
- {
- w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
- sha1macro(b ^ c ^ d, 0xca62c1d6)
- ++round;
- }
-
- #undef sha1macro
-
- result[0] += a;
- result[1] += b;
- result[2] += c;
- result[3] += d;
- result[4] += e;
+ unsigned int a = result[0];
+ unsigned int b = result[1];
+ unsigned int c = result[2];
+ unsigned int d = result[3];
+ unsigned int e = result[4];
+
+ int round = 0;
+
+ #define sha1macro(func,val) \
+ { \
+ const unsigned int t = rol(a, 5) + (func) + e + val + w[round]; \
+ e = d; \
+ d = c; \
+ c = rol(b, 30); \
+ b = a; \
+ a = t; \
+ }
+
+ while (round < 16)
+ {
+ sha1macro((b & c) | (~b & d), 0x5a827999)
+ ++round;
+ }
+ while (round < 20)
+ {
+ w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
+ sha1macro((b & c) | (~b & d), 0x5a827999)
+ ++round;
+ }
+ while (round < 40)
+ {
+ w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
+ sha1macro(b ^ c ^ d, 0x6ed9eba1)
+ ++round;
+ }
+ while (round < 60)
+ {
+ w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
+ sha1macro((b & c) | (b & d) | (c & d), 0x8f1bbcdc)
+ ++round;
+ }
+ while (round < 80)
+ {
+ w[round] = rol((w[round - 3] ^ w[round - 8] ^ w[round - 14] ^ w[round - 16]), 1);
+ sha1macro(b ^ c ^ d, 0xca62c1d6)
+ ++round;
+ }
+
+ #undef sha1macro
+
+ result[0] += a;
+ result[1] += b;
+ result[2] += c;
+ result[3] += d;
+ result[4] += e;
}
} // namespace
@@ -118,62 +118,62 @@ inline void innerHash(unsigned int * result, unsigned int * w)
* the sha1 result in.
*/
inline void calc(void const * src, size_t bytelength, unsigned char * hash) {
- // Init the result array.
- unsigned int result[5] = { 0x67452301, 0xefcdab89, 0x98badcfe,
- 0x10325476, 0xc3d2e1f0 };
-
- // Cast the void src pointer to be the byte array we can work with.
- unsigned char const * sarray = static_cast<unsigned char const *>(src);
-
- // The reusable round buffer
- unsigned int w[80];
-
- // Loop through all complete 64byte blocks.
-
- size_t endCurrentBlock;
- size_t currentBlock = 0;
-
- if (bytelength >= 64) {
- size_t const endOfFullBlocks = bytelength - 64;
-
- while (currentBlock <= endOfFullBlocks) {
- endCurrentBlock = currentBlock + 64;
-
- // Init the round buffer with the 64 byte block data.
- for (int roundPos = 0; currentBlock < endCurrentBlock; currentBlock += 4)
- {
- // This line will swap endian on big endian and keep endian on
- // little endian.
- w[roundPos++] = static_cast<unsigned int>(sarray[currentBlock + 3])
- | (static_cast<unsigned int>(sarray[currentBlock + 2]) << 8)
- | (static_cast<unsigned int>(sarray[currentBlock + 1]) << 16)
- | (static_cast<unsigned int>(sarray[currentBlock]) << 24);
- }
- innerHash(result, w);
- }
- }
-
- // Handle the last and not full 64 byte block if existing.
- endCurrentBlock = bytelength - currentBlock;
- clearWBuffert(w);
- size_t lastBlockBytes = 0;
- for (;lastBlockBytes < endCurrentBlock; ++lastBlockBytes) {
- w[lastBlockBytes >> 2] |= static_cast<unsigned int>(sarray[lastBlockBytes + currentBlock]) << ((3 - (lastBlockBytes & 3)) << 3);
- }
-
- w[lastBlockBytes >> 2] |= 0x80 << ((3 - (lastBlockBytes & 3)) << 3);
- if (endCurrentBlock >= 56) {
- innerHash(result, w);
- clearWBuffert(w);
- }
- w[15] = bytelength << 3;
- innerHash(result, w);
-
- // Store hash in result pointer, and make sure we get in in the correct
- // order on both endian models.
- for (int hashByte = 20; --hashByte >= 0;) {
- hash[hashByte] = (result[hashByte >> 2] >> (((3 - hashByte) & 0x3) << 3)) & 0xff;
- }
+ // Init the result array.
+ unsigned int result[5] = { 0x67452301, 0xefcdab89, 0x98badcfe,
+ 0x10325476, 0xc3d2e1f0 };
+
+ // Cast the void src pointer to be the byte array we can work with.
+ unsigned char const * sarray = static_cast<unsigned char const *>(src);
+
+ // The reusable round buffer
+ unsigned int w[80];
+
+ // Loop through all complete 64byte blocks.
+
+ size_t endCurrentBlock;
+ size_t currentBlock = 0;
+
+ if (bytelength >= 64) {
+ size_t const endOfFullBlocks = bytelength - 64;
+
+ while (currentBlock <= endOfFullBlocks) {
+ endCurrentBlock = currentBlock + 64;
+
+ // Init the round buffer with the 64 byte block data.
+ for (int roundPos = 0; currentBlock < endCurrentBlock; currentBlock += 4)
+ {
+ // This line will swap endian on big endian and keep endian on
+ // little endian.
+ w[roundPos++] = static_cast<unsigned int>(sarray[currentBlock + 3])
+ | (static_cast<unsigned int>(sarray[currentBlock + 2]) << 8)
+ | (static_cast<unsigned int>(sarray[currentBlock + 1]) << 16)
+ | (static_cast<unsigned int>(sarray[currentBlock]) << 24);
+ }
+ innerHash(result, w);
+ }
+ }
+
+ // Handle the last and not full 64 byte block if existing.
+ endCurrentBlock = bytelength - currentBlock;
+ clearWBuffert(w);
+ size_t lastBlockBytes = 0;
+ for (;lastBlockBytes < endCurrentBlock; ++lastBlockBytes) {
+ w[lastBlockBytes >> 2] |= static_cast<unsigned int>(sarray[lastBlockBytes + currentBlock]) << ((3 - (lastBlockBytes & 3)) << 3);
+ }
+
+ w[lastBlockBytes >> 2] |= 0x80 << ((3 - (lastBlockBytes & 3)) << 3);
+ if (endCurrentBlock >= 56) {
+ innerHash(result, w);
+ clearWBuffert(w);
+ }
+ w[15] = bytelength << 3;
+ innerHash(result, w);
+
+ // Store hash in result pointer, and make sure we get in in the correct
+ // order on both endian models.
+ for (int hashByte = 20; --hashByte >= 0;) {
+ hash[hashByte] = (result[hashByte >> 2] >> (((3 - hashByte) & 0x3) << 3)) & 0xff;
+ }
}
} // namespace sha1
diff --git a/src/lib/util/unicode.cpp b/src/lib/util/unicode.cpp
index 5849abb8d73..b48cef44c79 100644
--- a/src/lib/util/unicode.cpp
+++ b/src/lib/util/unicode.cpp
@@ -15,7 +15,7 @@
#define UTF8PROC_DLLEXPORT
#endif
-#include "utf8proc/utf8proc.h"
+#include <utf8proc.h>
#include <codecvt>
#include <locale>
@@ -389,7 +389,7 @@ std::string utf8_from_wstring(const std::wstring &string)
//-------------------------------------------------
// internal_normalize_unicode - uses utf8proc to
-// normalize unicode
+// normalize unicode
//-------------------------------------------------
static std::string internal_normalize_unicode(const char *s, size_t length, unicode_normalization_form normalization_form, bool null_terminated)
@@ -437,7 +437,7 @@ static std::string internal_normalize_unicode(const char *s, size_t length, unic
//-------------------------------------------------
// normalize_unicode - uses utf8proc to normalize
-// unicode
+// unicode
//-------------------------------------------------
std::string normalize_unicode(const std::string &s, unicode_normalization_form normalization_form)
@@ -448,7 +448,7 @@ std::string normalize_unicode(const std::string &s, unicode_normalization_form n
//-------------------------------------------------
// normalize_unicode - uses utf8proc to normalize
-// unicode
+// unicode
//-------------------------------------------------
std::string normalize_unicode(const char *s, unicode_normalization_form normalization_form)
@@ -459,7 +459,7 @@ std::string normalize_unicode(const char *s, unicode_normalization_form normaliz
//-------------------------------------------------
// normalize_unicode - uses utf8proc to normalize
-// unicode
+// unicode
//-------------------------------------------------
std::string normalize_unicode(const char *s, size_t length, unicode_normalization_form normalization_form)