kashmir-dependency-master/0000755000175000017500000000000013222010060015077 5ustar aviauaviaukashmir-dependency-master/LICENSE_1_0.txt0000644000175000017500000000247212560442737017416 0ustar aviauaviauBoost Software License - Version 1.0 - August 17th, 2003 Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following: The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. kashmir-dependency-master/cli.cpp0000644000175000017500000000507712560442737016413 0ustar aviauaviau/********************************************************************\ * cli.cpp -- UNIX command line interface * * * * Copyright (C) 2009 Kenneth Laskoski * * * \********************************************************************/ /** @file cli.cpp @brief UNIX command line interface @author Copyright (C) 2009 Kenneth Laskoski Use, modification, and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or a copy at .) */ #include "kashmir/uuid_gen.h" #include "kashmir/system/ccmd5.h" #include "kashmir/system/ccsha1.h" #include "kashmir/system/devrand.h" #include #include #include namespace { int n = 1; int v = 4; std::ostream* outp = &std::cout; std::ofstream ofile; void parse_cmd_line(int argc, char *argv[]) { int ch; char *p; while ((ch = getopt(argc, argv, "n:o:v:")) != -1) { switch (ch) { case 'n': n = strtoul(optarg, &p, 10); if (*p != '\0' || n < 1) std::cerr << "invalid argument for option 'n'\n"; break; case 'o': ofile.open(optarg); outp = &ofile; break; case 'v': v = strtoul(optarg, &p, 10); if (*p == '\0' && (3 <= v && v <= 5)) break; std::cerr << "invalid argument for option 'v'\n"; // fall through default: exit(1); } argv += optind; argc -= optind; } } } int main(int argc, char *argv[]) { using kashmir::system::ccmd5; using kashmir::system::ccsha1; using kashmir::system::DevRand; parse_cmd_line(argc, argv); std::ostream& out = *outp; kashmir::uuid_t uuid; if (v == 3) { ccmd5 md5engine; out << kashmir::uuid::generate(md5engine, uuid, "") << '\n'; return 0; } if (v == 5) { ccsha1 sha1engine; out << kashmir::uuid::generate(sha1engine, uuid, "") << '\n'; return 0; } DevRand devrand; for (int i = 0; i < n; i++) { devrand >> uuid; out << uuid << '\n'; } return 0; } kashmir-dependency-master/kashmir/0000755000175000017500000000000012560442737016565 5ustar aviauaviaukashmir-dependency-master/kashmir/unique.h0000644000175000017500000000106412560442737020245 0ustar aviauaviau// unique.h -- disable copy construction and assignment // Copyright (C) 2008 Kenneth Laskoski /** @file unique.h @brief disable copy construction and assignment @author Copyright (C) 2008 Kenneth Laskoski */ #ifndef KL_UNIQUE_H #define KL_UNIQUE_H namespace kashmir { namespace unique_ADL_fence { // CRTP allows better empty base optimization template class unique { protected: unique() {} ~unique() {} private: unique(const unique&); unique& operator=(const unique&); }; } using namespace unique_ADL_fence; } #endif kashmir-dependency-master/kashmir/system/0000755000175000017500000000000012560442737020111 5ustar aviauaviaukashmir-dependency-master/kashmir/system/ccsha1.h0000644000175000017500000000164312560442737021430 0ustar aviauaviau// ccsha1.h -- hash algorithm described in FIPS 180-1 // Apple's Common Crypto implementation // Copyright (C) 2013 Kenneth Laskoski /** @file ccsha1.h @brief Apple's Common Crypto implementation of sha1 algorithm @author Copyright (C) 2013 Kenneth Laskoski */ #ifndef KL_CCSHA1_H #define KL_CCSHA1_H #include "../sha1_gen.h" #include "../unique.h" #include #include namespace kashmir { namespace system { class ccsha1 : public sha1::engine, unique { CC_SHA1_CTX ctx; public: ccsha1() { CC_SHA1_Init(&ctx); } void update(const char *const source, std::size_t count) { CC_SHA1_Update(&ctx, reinterpret_cast(source), count); } sha1_t operator()() { sha1_t data; CC_SHA1_Final(reinterpret_cast(&data), &ctx); return data; } }; }} #endif kashmir-dependency-master/kashmir/system/devrandom.h0000644000175000017500000000136312560442737022244 0ustar aviauaviau// devrandom.h -- UNIX random number generator // Copyright (C) 2008 Kenneth Laskoski /** @file devrandom.h @brief UNIX random number generator @author Copyright (C) 2008 Kenneth Laskoski */ #ifndef KL_DEVRANDOM_H #define KL_DEVRANDOM_H #include "../randomstream.h" #include "../unique.h" #include #include namespace kashmir { namespace system { class DevRandom : public randomstream, unique { std::ifstream file; public: DevRandom() : file("/dev/random", std::ios::binary) { if (!file) throw std::runtime_error("failed to open random device."); } void read(char *buffer, std::size_t count) { file.read(buffer, count); } }; }} #endif kashmir-dependency-master/kashmir/system/winrandom.h0000644000175000017500000000255512560442737022267 0ustar aviauaviau// winrandom.h -- Windows random number generator // Copyright (C) 2008 Kenneth Laskoski /** @file winrandom.h @brief Windows random number generator @author Copyright (C) 2008 Kenneth Laskoski with contribution of @author Chet Stuut Use, modification, and distribution are subject to the Boost Software License, Version 1.0. See accompanying file LICENSE_1_0.txt or . */ #ifndef KL_WINRANDOM_H #define KL_WINRANDOM_H #include "../randomstream.h" #include "../unique.h" #include #define WINVER 0x0500 #define _WIN32_WINNT 0x0500 #include #include namespace kashmir { namespace system { class WinRandom : public randomstream, unique { HCRYPTPROV hProv; public: WinRandom() { if (!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) if (GetLastError() != NTE_BAD_KEYSET || !CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) throw std::runtime_error("failed to acquire cryptographic context."); } ~WinRandom() { CryptReleaseContext(hProv, 0); } void read(char *buffer, std::size_t count) { if (!CryptGenRandom(hProv, count, buffer)) throw std::runtime_error("system failed to generate random data."); } }; }} #endif kashmir-dependency-master/kashmir/system/devrand.h0000644000175000017500000000163612560442737021713 0ustar aviauaviau// devrand.h -- UNIX random number generator // Copyright (C) 2008 Kenneth Laskoski /** @file devrand.h @brief UNIX random number generator @author Copyright (C) 2008 Kenneth Laskoski Use, modification, and distribution are subject to the Boost Software License, Version 1.0. See accompanying file LICENSE_1_0.txt or . */ #ifndef KL_DEVRAND_H #define KL_DEVRAND_H #include "../randomstream.h" #include "../unique.h" #include #include namespace kashmir { namespace system { class DevRand : public randomstream, unique { std::ifstream file; public: DevRand() : file("/dev/urandom", std::ios::binary) { if (!file) throw std::runtime_error("failed to open random device."); } void read(char *buffer, std::size_t count) { file.read(buffer, count); } }; }} #endif kashmir-dependency-master/kashmir/system/winrand.h0000644000175000017500000000324612560442737021731 0ustar aviauaviau// winrand.h -- Windows random number generator // Copyright (C) 2008 Kenneth Laskoski /** @file winrand.h @brief Windows random number generator @author Copyright (C) 2008 Kenneth Laskoski WinRand uses ADVAPI32!RtlGenRandom and does not require a CSP context see and Use, modification, and distribution are subject to the Boost Software License, Version 1.0. See accompanying file LICENSE_1_0.txt or . */ #ifndef KL_WINRAND_H #define KL_WINRAND_H #include "../randomstream.h" #include "../unique.h" #include #define WINVER 0x0501 #define _WIN32_WINNT 0x0501 #include namespace kashmir { namespace system { class WinRand : public randomstream, unique { HMODULE advapi32; BOOLEAN (APIENTRY *RtlGenRandom)(void*, ULONG); public: WinRand() : advapi32(LoadLibrary("ADVAPI32.DLL")) { if (!advapi32) throw std::runtime_error("failed to load ADVAPI32.DLL."); RtlGenRandom = reinterpret_cast(GetProcAddress(advapi32, "SystemFunction036")); if (!RtlGenRandom) { FreeLibrary(advapi32); throw std::runtime_error("failed to get ADVAPI32!RtlGenRandom address."); } } ~WinRand() { FreeLibrary(advapi32); } void read(char *buffer, std::size_t count) { if (!RtlGenRandom(buffer, count)) throw std::runtime_error("system failed to generate random data."); } }; }} #endif kashmir-dependency-master/kashmir/system/iosrandom.h0000644000175000017500000000143512560442737022260 0ustar aviauaviau// iosrandom.h -- iOS random number generator // Copyright (C) 2013 Kenneth Laskoski /** @file iosrandom.h @brief iOS random number generator @author Copyright (C) 2013 Kenneth Laskoski */ #ifndef KL_IOSRANDOM_H #define KL_IOSRANDOM_H #include "../randomstream.h" #include "../unique.h" #include #include namespace kashmir { namespace system { class iOSRandom : public randomstream, unique { SecRandomRef ref; public: iOSRandom(SecRandomRef ref=kSecRandomDefault) : ref(ref) {} void read(char *buffer, std::size_t count) { if (SecRandomCopyBytes(ref, count, reinterpret_cast(buffer))) throw std::runtime_error("system failed to generate random data."); } }; }} #endif kashmir-dependency-master/kashmir/system/ccmd5.h0000644000175000017500000000162512560442737021261 0ustar aviauaviau// ccmd5.h -- hash algorithm described in IETF RFC 1321 // Apple's Common Crypto implementation // Copyright (C) 2013 Kenneth Laskoski /** @file ccmd5.h @brief Apple's Common Crypto implementation of md5 algorithm @author Copyright (C) 2013 Kenneth Laskoski */ #ifndef KL_CCMD5_H #define KL_CCMD5_H #include "../md5_gen.h" #include "../unique.h" #include #include namespace kashmir { namespace system { class ccmd5 : public md5::engine, unique { CC_MD5_CTX ctx; public: ccmd5() { CC_MD5_Init(&ctx); } void update(const char *const source, std::size_t count) { CC_MD5_Update(&ctx, reinterpret_cast(source), count); } md5_t operator()() { md5_t data; CC_MD5_Final(reinterpret_cast(&data), &ctx); return data; } }; }} #endif kashmir-dependency-master/kashmir/system/abstractrandom.h0000644000175000017500000000101712560442737023265 0ustar aviauaviau// abstractrandom.h -- abstract random number generator // Copyright (C) 2013 Kenneth Laskoski /** @file abstractrandom.h @brief abstract random number generator @author Copyright (C) 2013 Kenneth Laskoski */ #ifndef KL_ABSTRACTRANDOM_H #define KL_ABSTRACTRANDOM_H #include "../randomstream.h" namespace kashmir { namespace system { class AbstractRandom : public randomstream { public: virtual ~AbstractRandom() {} virtual void read(char *buffer, std::size_t count) = 0; }; }} #endif kashmir-dependency-master/kashmir/system/sslmd5.h0000644000175000017500000000155212560442737021474 0ustar aviauaviau// sslmd5.h -- hash algorithm described in IETF RFC 1321 // Open SSL implementation // Copyright (C) 2013 Kenneth Laskoski /** @file sslmd5.h @brief Open SSL implementation of md5 algorithm @author Copyright (C) 2013 Kenneth Laskoski */ #ifndef KL_SSLMD5_H #define KL_SSLMD5_H #include "../md5_gen.h" #include "../unique.h" #include #include namespace kashmir { namespace system { class sslmd5 : public md5::engine, unique { MD5_CTX ctx; public: sslmd5() { MD5_Init(&ctx); } void update(const char *const source, std::size_t count) { MD5_Update(&ctx, reinterpret_cast(source), count); } md5_t operator()() { md5_t data; MD5_Final(reinterpret_cast(&data), &ctx); return data; } }; }} #endif kashmir-dependency-master/kashmir/sha1_gen.h0000644000175000017500000000125612560442737020427 0ustar aviauaviau// sha1_gen.h -- hash algorithm described in FIPS 180-1 // Copyright (C) 2013 Kenneth Laskoski /** @file sha1_gen.h @brief hash algorithm described in FIPS 180-1 @author Copyright (C) 2013 Kenneth Laskoski */ #ifndef KL_SHA1_GEN_H #define KL_SHA1_GEN_H #include "sha1.h" namespace kashmir { namespace sha1 { template class engine { crtp_impl *const self; public: engine() : self(static_cast(this)) {} void update(const char *const source, std::size_t count) { self->update(source, count); } sha1_t operator()() { return (*self)(); } }; } // namespace kashmir::sha1 } // namespace kashmir #endif kashmir-dependency-master/kashmir/sha1.h0000644000175000017500000001255012560442737017575 0ustar aviauaviau// sha1.h -- hash algorithm data product described in FIPS 180-1 // Copyright (C) 2012 Kenneth Laskoski /** @file sha1.h @brief hash algorithm data product described in FIPS 180-1 @author Copyright (C) 2012 Kenneth Laskoski */ #ifndef KL_SHA1_H #define KL_SHA1_H #include "array.h" #include #include #include #include #include "iostate.h" namespace kashmir { namespace sha1 { /** @class sha1_t @brief This class implements the data product of the hash algorithm described in - FIPS 180-1 - available at http://www.itl.nist.gov/fipspubs/fip180-1.htm This documents the code below. */ // an SHA-1 is a string of 20 octets (160 bits) // we use an unpacked representation, value_type may be larger than 8 bits, // in which case every input operation must assert data[i] < 256 for i < 16 // note even char may be more than 8 bits in some particular platform typedef unsigned char value_type; typedef std::size_t size_type; const size_type size = 20, string_size = 40; class sha1_t { typedef array data_type; data_type data; public: // we keep data uninitialized to stress concreteness sha1_t() {} ~sha1_t() {} // trivial copy and assignment sha1_t(const sha1_t& rhs) : data(rhs.data) {} sha1_t& operator=(const sha1_t& rhs) { data = rhs.data; return *this; } // OK, now we bow to convenience // initialization from C string explicit sha1_t(const char* string) { std::stringstream stream(string); get(stream); } // test for nil value bool is_nil() const { for (size_type i = 0; i < size; ++i) if (data[i]) return false; return true; } // safe bool idiom typedef data_type sha1_t::*bool_type; operator bool_type() const { return is_nil() ? 0 : &sha1_t::data; } // only equality makes sense here bool operator==(const sha1_t& rhs) const { return data == rhs.data; } // stream operators template std::basic_ostream& put(std::basic_ostream& os) const; template std::basic_istream& get(std::basic_istream& is); }; // only equality makes sense here inline bool operator!=(const sha1_t& lhs, const sha1_t& rhs) { return !(lhs == rhs); } template std::basic_ostream& sha1_t::put(std::basic_ostream& os) const { if (!os.good()) return os; const typename std::basic_ostream::sentry ok(os); if (ok) { ios_flags_saver flags(os); basic_ios_fill_saver fill(os); const std::streamsize width = os.width(0); const std::streamsize mysize = string_size; // right padding if (flags.value() & (std::ios_base::right | std::ios_base::internal)) for (std::streamsize i = width; i > mysize; --i) os << fill.value(); os << std::hex; os.fill(os.widen('0')); for (size_t i = 0; i < size; ++i) { os.width(2); os << static_cast(data[i]); } // left padding if (flags.value() & std::ios_base::left) for (std::streamsize i = width; i > mysize; --i) os << fill.value(); } return os; } template std::basic_istream& sha1_t::get(std::basic_istream& is) { if (!is.good()) return is; const typename std::basic_istream::sentry ok(is); if (ok) { char_t hexdigits[16]; char_t* const npos = hexdigits+16; typedef std::ctype facet_t; const facet_t& facet = std::use_facet(is.getloc()); const char* tmp = "0123456789abcdef"; facet.widen(tmp, tmp+16, hexdigits); char_t c; char_t* f; for (size_t i = 0; i < size; ++i) { is >> c; c = facet.tolower(c); f = std::find(hexdigits, npos, c); if (f == npos) { is.setstate(std::ios_base::failbit); break; } data[i] = static_cast(std::distance(hexdigits, f)); is >> c; c = facet.tolower(c); f = std::find(hexdigits, npos, c); if (f == npos) { is.setstate(std::ios_base::failbit); break; } data[i] <<= 4; data[i] |= static_cast(std::distance(hexdigits, f)); } if (!is) throw std::runtime_error("failed to extract valid sha1 from stream."); } return is; } template inline std::basic_ostream& operator<<(std::basic_ostream& os, const sha1_t& sha1) { return sha1.put(os); } template inline std::basic_istream& operator>>(std::basic_istream& is, sha1_t& sha1) { return sha1.get(is); } } // namespace kashmir::sha1 using sha1::sha1_t; } // namespace kashmir #endif kashmir-dependency-master/kashmir/uuid_gen.h0000644000175000017500000000553012560442737020540 0ustar aviauaviau// uuid_gen.h -- universally unique ID - as defined by ISO/IEC 9834-8:2005 // Copyright (C) 2008 Kenneth Laskoski /** @file uuid_gen.h @brief universally unique ID - as defined by ISO/IEC 9834-8:2005 @author Copyright (C) 2008 Kenneth Laskoski */ #ifndef KL_UUID_GEN_H #define KL_UUID_GEN_H #include "uuid.h" #include "md5_gen.h" #include "sha1_gen.h" #include "randomstream.h" namespace kashmir { namespace uuid { template randomstream& operator>>(randomstream& is, uuid_t& uuid) { // get random bytes // we take advantage of our representation char *data = reinterpret_cast(&uuid); is.read(data, size); // a more general solution would be // input_iterator it; // std::copy(it, it+size, data.begin()); // if uuid_t::value_type is larger than 8 bits, we need // to maintain the invariant data[i] < 256 for i < 16 // Example (which may impact randomness): // for (size_t i = 0; i < size; ++i) // data[i] &= 0xff; // set variant // should be 0b10xxxxxx data[8] &= 0xbf; // 0b10111111 data[8] |= 0x80; // 0b10000000 // set version // should be 0b0100xxxx data[6] &= 0x4f; // 0b01001111 data[6] |= 0x40; // 0b01000000 return is; } template uuid_t generate(randomstream& is) { uuid_t uuid; is >> uuid; return uuid; } template uuid_t generate(md5::engine& md5engine, const uuid_t& nameSpace, const std::string& name) { md5engine.update(reinterpret_cast(&nameSpace), 16); md5engine.update(name.c_str(), name.size()); kashmir::md5_t md5 = md5engine(); kashmir::uuid_t& uuid = *(reinterpret_cast(&md5)); unsigned char *const data = reinterpret_cast(&uuid); // set variant // should be 0b10xxxxxx data[8] &= 0xbf; // 0b10111111 data[8] |= 0x80; // 0b10000000 // set version // should be 0b0011xxxx data[6] &= 0x3f; // 0b00111111 data[6] |= 0x30; // 0b00110000 return uuid; } template uuid_t generate(sha1::engine& sha1engine, const uuid_t& nameSpace, const std::string& name) { sha1engine.update(reinterpret_cast(&nameSpace), 16); sha1engine.update(name.c_str(), name.size()); kashmir::sha1_t sha1 = sha1engine(); kashmir::uuid_t& uuid = *(reinterpret_cast(&sha1)); unsigned char *const data = reinterpret_cast(&uuid); // set variant // should be 0b10xxxxxx data[8] &= 0xbf; // 0b10111111 data[8] |= 0x80; // 0b10000000 // set version // should be 0b0101xxxx data[6] &= 0x5f; // 0b01011111 data[6] |= 0x50; // 0b01010000 return uuid; } } // namespace kashmir::uuid } // namespace kashmir #endif kashmir-dependency-master/kashmir/md5.h0000644000175000017500000001251212560442737017424 0ustar aviauaviau// md5.h -- hash algorithm data product described in IETF RFC 1321 // Copyright (C) 2010 Kenneth Laskoski /** @file md5.h @brief hash algorithm data product described in IETF RFC 1321 @author Copyright (C) 2010 Kenneth Laskoski */ #ifndef KL_MD5_H #define KL_MD5_H #include "array.h" #include #include #include #include #include "iostate.h" namespace kashmir { namespace md5 { /** @class md5_t @brief This class implements the data product of the hash algorithm described in - IETF RFC 1321 - available at http://tools.ietf.org/html/rfc1321 This documents the code below. */ // an MD5 is a string of 16 octets (128 bits) // we use an unpacked representation, value_type may be larger than 8 bits, // in which case every input operation must assert data[i] < 256 for i < 16 // note that even char may be more than 8 bits in some particular platform typedef unsigned char value_type; typedef std::size_t size_type; const size_type size = 16, string_size = 32; class md5_t { typedef array data_type; data_type data; public: // we keep data uninitialized to stress concreteness md5_t() {} ~md5_t() {} // trivial copy and assignment md5_t(const md5_t& rhs) : data(rhs.data) {} md5_t& operator=(const md5_t& rhs) { data = rhs.data; return *this; } // OK, now we bow to convenience // initialization from C string explicit md5_t(const char* string) { std::stringstream stream(string); get(stream); } // test for nil value bool is_nil() const { for (size_type i = 0; i < size; ++i) if (data[i]) return false; return true; } // safe bool idiom typedef data_type md5_t::*bool_type; operator bool_type() const { return is_nil() ? 0 : &md5_t::data; } // only equality makes sense here bool operator==(const md5_t& rhs) const { return data == rhs.data; } // stream operators template std::basic_ostream& put(std::basic_ostream& os) const; template std::basic_istream& get(std::basic_istream& is); }; // only equality makes sense here inline bool operator!=(const md5_t& lhs, const md5_t& rhs) { return !(lhs == rhs); } template std::basic_ostream& md5_t::put(std::basic_ostream& os) const { if (!os.good()) return os; const typename std::basic_ostream::sentry ok(os); if (ok) { ios_flags_saver flags(os); basic_ios_fill_saver fill(os); const std::streamsize width = os.width(0); const std::streamsize mysize = string_size; // right padding if (flags.value() & (std::ios_base::right | std::ios_base::internal)) for (std::streamsize i = width; i > mysize; --i) os << fill.value(); os << std::hex; os.fill(os.widen('0')); for (size_t i = 0; i < size; ++i) { os.width(2); os << static_cast(data[i]); } // left padding if (flags.value() & std::ios_base::left) for (std::streamsize i = width; i > mysize; --i) os << fill.value(); } return os; } template std::basic_istream& md5_t::get(std::basic_istream& is) { if (!is.good()) return is; const typename std::basic_istream::sentry ok(is); if (ok) { char_t hexdigits[16]; char_t* const npos = hexdigits+16; typedef std::ctype facet_t; const facet_t& facet = std::use_facet(is.getloc()); const char* tmp = "0123456789abcdef"; facet.widen(tmp, tmp+16, hexdigits); char_t c; char_t* f; for (size_t i = 0; i < size; ++i) { is >> c; c = facet.tolower(c); f = std::find(hexdigits, npos, c); if (f == npos) { is.setstate(std::ios_base::failbit); break; } data[i] = static_cast(std::distance(hexdigits, f)); is >> c; c = facet.tolower(c); f = std::find(hexdigits, npos, c); if (f == npos) { is.setstate(std::ios_base::failbit); break; } data[i] <<= 4; data[i] |= static_cast(std::distance(hexdigits, f)); } if (!is) throw std::runtime_error("failed to extract valid md5 from stream."); } return is; } template inline std::basic_ostream& operator<<(std::basic_ostream& os, const md5_t& md5) { return md5.put(os); } template inline std::basic_istream& operator>>(std::basic_istream& is, md5_t& md5) { return md5.get(is); } } // namespace kashmir::md5 using md5::md5_t; } // namespace kashmir #endif kashmir-dependency-master/kashmir/randomstream.h0000644000175000017500000000437112560442737021437 0ustar aviauaviau// randomstream.h -- random number stream // Copyright (C) 2008 Kenneth Laskoski /** @file randomstream.h @brief random number stream @author Copyright (C) 2008 Kenneth Laskoski */ #ifndef KL_RANDOMSTREAM_H #define KL_RANDOMSTREAM_H #include namespace kashmir { // CRTP stands for curiously recurring template pattern template class randomstream { crtp_impl *const self; public: randomstream() : self(static_cast(this)) {} void read(char *buffer, std::size_t count) { self->read(buffer, count); } randomstream& operator>>(char& c) { read(&c, 1); return *this; } randomstream& operator>>(signed char& c) { read(reinterpret_cast(&c), 1); return *this; } randomstream& operator>>(unsigned char& c) { read(reinterpret_cast(&c), 1); return *this; } randomstream& operator>>(int& n) { read(reinterpret_cast(&n), sizeof(int)); return *this; } randomstream& operator>>(long& n) { read(reinterpret_cast(&n), sizeof(long)); return *this; } randomstream& operator>>(short& n) { read(reinterpret_cast(&n), sizeof(short)); return *this; } randomstream& operator>>(unsigned int& u) { read(reinterpret_cast(&u), sizeof(unsigned int)); return *this; } randomstream& operator>>(unsigned long& u) { read(reinterpret_cast(&u), sizeof(unsigned long)); return *this; } randomstream& operator>>(unsigned short& u) { read(reinterpret_cast(&u), sizeof(unsigned short)); return *this; } randomstream& operator>>(float& f) { read(reinterpret_cast(&f), sizeof(float)); return *this; } randomstream& operator>>(double& f) { read(reinterpret_cast(&f), sizeof(double)); return *this; } randomstream& operator>>(long double& f) { read(reinterpret_cast(&f), sizeof(long double)); return *this; } randomstream& operator>>(bool& b) { read(reinterpret_cast(&b), sizeof(bool)); b &= 1; return *this; } randomstream& operator>>(void*& p) { read(reinterpret_cast(&p), sizeof(void*)); return *this; } }; } // namespace kashmir #endif kashmir-dependency-master/kashmir/iofwd.h0000644000175000017500000000526712560442737020060 0ustar aviauaviau/********************************************************************\ * iofwd.h -- io forward decls * * * * Copyright (C) 2008 Kenneth Laskoski * * * \********************************************************************/ /** @file iofwd.h @brief io forward decls @author Copyright (C) 2008 Kenneth Laskoski shameless copy of work by @author Copyright (C) 2002 Daryle Walker Use, modification, and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or a copy at .) See for the library's home page. */ #ifndef KL_IOFWD_H #define KL_IOFWD_H #include namespace kashmir { class ios_flags_saver; class ios_precision_saver; class ios_width_saver; class ios_base_all_saver; template < typename Ch, class Tr = ::std::char_traits > class basic_ios_iostate_saver; template < typename Ch, class Tr = ::std::char_traits > class basic_ios_exception_saver; template < typename Ch, class Tr = ::std::char_traits > class basic_ios_tie_saver; template < typename Ch, class Tr = ::std::char_traits > class basic_ios_rdbuf_saver; template < typename Ch, class Tr = ::std::char_traits > class basic_ios_fill_saver; template < typename Ch, class Tr = ::std::char_traits > class basic_ios_locale_saver; template < typename Ch, class Tr = ::std::char_traits > class basic_ios_all_saver; typedef basic_ios_iostate_saver ios_iostate_saver; typedef basic_ios_iostate_saver wios_iostate_saver; typedef basic_ios_exception_saver ios_exception_saver; typedef basic_ios_exception_saver wios_exception_saver; typedef basic_ios_tie_saver ios_tie_saver; typedef basic_ios_tie_saver wios_tie_saver; typedef basic_ios_rdbuf_saver ios_rdbuf_saver; typedef basic_ios_rdbuf_saver wios_rdbuf_saver; typedef basic_ios_fill_saver ios_fill_saver; typedef basic_ios_fill_saver wios_fill_saver; typedef basic_ios_locale_saver ios_locale_saver; typedef basic_ios_locale_saver wios_locale_saver; typedef basic_ios_all_saver ios_all_saver; typedef basic_ios_all_saver wios_all_saver; class ios_iword_saver; class ios_pword_saver; class ios_all_word_saver; } // namespace kashmir #endif kashmir-dependency-master/kashmir/iostate.h0000644000175000017500000003050512560442737020411 0ustar aviauaviau/********************************************************************\ * iostate.h -- io state saver classes * * * * Copyright (C) 2008 Kenneth Laskoski * * * \********************************************************************/ /** @file iostate.h @brief io state saver classes this classes save io states on construction and restore them on destruction, RAII style @author Copyright (C) 2008 Kenneth Laskoski slight modification of work by @author Copyright (C) 2002, 2005 Daryle Walker Use, modification, and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or a copy at .) See for the library's home page. */ #ifndef KL_IOSTATE_H #define KL_IOSTATE_H #include "iofwd.h" // self include #include // for std::ios_base, std::basic_ios, etc. #include // for std::locale #include // for std::basic_ostream #include // for std::basic_streambuf #include // for std::char_traits namespace kashmir { // Basic stream state saver class declarations -----------------------------// class ios_flags_saver { public: typedef ::std::ios_base state_type; typedef ::std::ios_base::fmtflags aspect_type; explicit ios_flags_saver( state_type &s ) : s_save_( s ), a_save_( s.flags() ) {} ios_flags_saver( state_type &s, aspect_type const &a ) : s_save_( s ), a_save_( s.flags(a) ) {} ~ios_flags_saver() { this->restore(); } const aspect_type& value() const { return a_save_; } void restore() { s_save_.flags( a_save_ ); } private: state_type & s_save_; aspect_type const a_save_; ios_flags_saver& operator=(const ios_flags_saver&); }; class ios_precision_saver { public: typedef ::std::ios_base state_type; typedef ::std::streamsize aspect_type; explicit ios_precision_saver( state_type &s ) : s_save_( s ), a_save_( s.precision() ) {} ios_precision_saver( state_type &s, aspect_type const &a ) : s_save_( s ), a_save_( s.precision(a) ) {} ~ios_precision_saver() { this->restore(); } const aspect_type& value() const { return a_save_; } void restore() { s_save_.precision( a_save_ ); } private: state_type & s_save_; aspect_type const a_save_; ios_precision_saver& operator=(const ios_precision_saver&); }; class ios_width_saver { public: typedef ::std::ios_base state_type; typedef ::std::streamsize aspect_type; explicit ios_width_saver( state_type &s ) : s_save_( s ), a_save_( s.width() ) {} ios_width_saver( state_type &s, aspect_type const &a ) : s_save_( s ), a_save_( s.width(a) ) {} ~ios_width_saver() { this->restore(); } const aspect_type& value() const { return a_save_; } void restore() { s_save_.width( a_save_ ); } private: state_type & s_save_; aspect_type const a_save_; ios_width_saver& operator=(const ios_width_saver&); }; // Advanced stream state saver class template declarations -----------------// template < typename Ch, class Tr > class basic_ios_iostate_saver { public: typedef ::std::basic_ios state_type; typedef ::std::ios_base::iostate aspect_type; explicit basic_ios_iostate_saver( state_type &s ) : s_save_( s ), a_save_( s.rdstate() ) {} basic_ios_iostate_saver( state_type &s, aspect_type const &a ) : s_save_( s ), a_save_( s.rdstate() ) { s.clear(a); } ~basic_ios_iostate_saver() { this->restore(); } const aspect_type& value() const { return a_save_; } void restore() { s_save_.clear( a_save_ ); } private: state_type & s_save_; aspect_type const a_save_; }; template < typename Ch, class Tr > class basic_ios_exception_saver { public: typedef ::std::basic_ios state_type; typedef ::std::ios_base::iostate aspect_type; explicit basic_ios_exception_saver( state_type &s ) : s_save_( s ), a_save_( s.exceptions() ) {} basic_ios_exception_saver( state_type &s, aspect_type const &a ) : s_save_( s ), a_save_( s.exceptions() ) { s.exceptions(a); } ~basic_ios_exception_saver() { this->restore(); } const aspect_type& value() const { return a_save_; } void restore() { s_save_.exceptions( a_save_ ); } private: state_type & s_save_; aspect_type const a_save_; }; template < typename Ch, class Tr > class basic_ios_tie_saver { public: typedef ::std::basic_ios state_type; typedef ::std::basic_ostream * aspect_type; explicit basic_ios_tie_saver( state_type &s ) : s_save_( s ), a_save_( s.tie() ) {} basic_ios_tie_saver( state_type &s, aspect_type const &a ) : s_save_( s ), a_save_( s.tie(a) ) {} ~basic_ios_tie_saver() { this->restore(); } const aspect_type& value() const { return a_save_; } void restore() { s_save_.tie( a_save_ ); } private: state_type & s_save_; aspect_type const a_save_; }; template < typename Ch, class Tr > class basic_ios_rdbuf_saver { public: typedef ::std::basic_ios state_type; typedef ::std::basic_streambuf * aspect_type; explicit basic_ios_rdbuf_saver( state_type &s ) : s_save_( s ), a_save_( s.rdbuf() ) {} basic_ios_rdbuf_saver( state_type &s, aspect_type const &a ) : s_save_( s ), a_save_( s.rdbuf(a) ) {} ~basic_ios_rdbuf_saver() { this->restore(); } const aspect_type& value() const { return a_save_; } void restore() { s_save_.rdbuf( a_save_ ); } private: state_type & s_save_; aspect_type const a_save_; }; template < typename Ch, class Tr > class basic_ios_fill_saver { public: typedef ::std::basic_ios state_type; typedef typename state_type::char_type aspect_type; explicit basic_ios_fill_saver( state_type &s ) : s_save_( s ), a_save_( s.fill() ) {} basic_ios_fill_saver( state_type &s, aspect_type const &a ) : s_save_( s ), a_save_( s.fill(a) ) {} ~basic_ios_fill_saver() { this->restore(); } const aspect_type& value() const { return a_save_; } void restore() { s_save_.fill( a_save_ ); } private: state_type & s_save_; aspect_type const a_save_; }; template < typename Ch, class Tr > class basic_ios_locale_saver { public: typedef ::std::basic_ios state_type; typedef ::std::locale aspect_type; explicit basic_ios_locale_saver( state_type &s ) : s_save_( s ), a_save_( s.getloc() ) {} basic_ios_locale_saver( state_type &s, aspect_type const &a ) : s_save_( s ), a_save_( s.imbue(a) ) {} ~basic_ios_locale_saver() { this->restore(); } const aspect_type& value() const { return a_save_; } void restore() { s_save_.imbue( a_save_ ); } private: state_type & s_save_; aspect_type const a_save_; }; // User-defined stream state saver class declarations ----------------------// class ios_iword_saver { public: typedef ::std::ios_base state_type; typedef int index_type; typedef long aspect_type; explicit ios_iword_saver( state_type &s, index_type i ) : s_save_( s ), a_save_( s.iword(i) ), i_save_( i ) {} ios_iword_saver( state_type &s, index_type i, aspect_type const &a ) : s_save_( s ), a_save_( s.iword(i) ), i_save_( i ) { s.iword(i) = a; } ~ios_iword_saver() { this->restore(); } const aspect_type& value() const { return a_save_; } void restore() { s_save_.iword( i_save_ ) = a_save_; } private: state_type & s_save_; aspect_type const a_save_; index_type const i_save_; ios_iword_saver& operator=(const ios_iword_saver&); }; class ios_pword_saver { public: typedef ::std::ios_base state_type; typedef int index_type; typedef void * aspect_type; explicit ios_pword_saver( state_type &s, index_type i ) : s_save_( s ), a_save_( s.pword(i) ), i_save_( i ) {} ios_pword_saver( state_type &s, index_type i, aspect_type const &a ) : s_save_( s ), a_save_( s.pword(i) ), i_save_( i ) { s.pword(i) = a; } ~ios_pword_saver() { this->restore(); } const aspect_type& value() const { return a_save_; } void restore() { s_save_.pword( i_save_ ) = a_save_; } private: state_type & s_save_; aspect_type const a_save_; index_type const i_save_; ios_pword_saver operator=(const ios_pword_saver&); }; // Combined stream state saver class (template) declarations ---------------// class ios_base_all_saver { public: typedef ::std::ios_base state_type; explicit ios_base_all_saver( state_type &s ) : s_save_( s ), a1_save_( s.flags() ), a2_save_( s.precision() ) , a3_save_( s.width() ) {} ~ios_base_all_saver() { this->restore(); } void restore() { s_save_.width( a3_save_ ); s_save_.precision( a2_save_ ); s_save_.flags( a1_save_ ); } private: state_type & s_save_; state_type::fmtflags const a1_save_; ::std::streamsize const a2_save_; ::std::streamsize const a3_save_; ios_base_all_saver& operator=(const ios_base_all_saver&); }; template < typename Ch, class Tr > class basic_ios_all_saver { public: typedef ::std::basic_ios state_type; explicit basic_ios_all_saver( state_type &s ) : s_save_( s ), a1_save_( s.flags() ), a2_save_( s.precision() ) , a3_save_( s.width() ), a4_save_( s.rdstate() ) , a5_save_( s.exceptions() ), a6_save_( s.tie() ) , a7_save_( s.rdbuf() ), a8_save_( s.fill() ) , a9_save_( s.getloc() ) {} ~basic_ios_all_saver() { this->restore(); } void restore() { s_save_.imbue( a9_save_ ); s_save_.fill( a8_save_ ); s_save_.rdbuf( a7_save_ ); s_save_.tie( a6_save_ ); s_save_.exceptions( a5_save_ ); s_save_.clear( a4_save_ ); s_save_.width( a3_save_ ); s_save_.precision( a2_save_ ); s_save_.flags( a1_save_ ); } private: state_type & s_save_; typename state_type::fmtflags const a1_save_; ::std::streamsize const a2_save_; ::std::streamsize const a3_save_; typename state_type::iostate const a4_save_; typename state_type::iostate const a5_save_; ::std::basic_ostream * const a6_save_; ::std::basic_streambuf * const a7_save_; typename state_type::char_type const a8_save_; ::std::locale const a9_save_; }; class ios_all_word_saver { public: typedef ::std::ios_base state_type; typedef int index_type; ios_all_word_saver( state_type &s, index_type i ) : s_save_( s ), i_save_( i ), a1_save_( s.iword(i) ) , a2_save_( s.pword(i) ) {} ~ios_all_word_saver() { this->restore(); } void restore() { s_save_.pword( i_save_ ) = a2_save_; s_save_.iword( i_save_ ) = a1_save_; } private: state_type & s_save_; index_type const i_save_; long const a1_save_; void * const a2_save_; ios_all_word_saver& operator=(const ios_all_word_saver&); }; } // namespace kashmir #endif kashmir-dependency-master/kashmir/uuid.h0000644000175000017500000001431112560442737017704 0ustar aviauaviau// uuid.h -- universally unique ID - as defined by ISO/IEC 9834-8:2005 // Copyright (C) 2008 Kenneth Laskoski /** @file uuid.h @brief universally unique ID - as defined by ISO/IEC 9834-8:2005 @author Copyright (C) 2008 Kenneth Laskoski */ #ifndef KL_UUID_H #define KL_UUID_H #include "array.h" #include #include #include #include #include "iostate.h" namespace kashmir { namespace uuid { /** @class uuid_t @brief This class is a C++ concrete type representing the UUID defined in - ISO/IEC 9834-8:2005 | ITU-T Rec. X.667 - available at http://www.itu.int/ITU-T/studygroups/com17/oid.html - IETF RFC 4122 - available at http://tools.ietf.org/html/rfc4122 These technically equivalent standards document the code below. */ // an UUID is a string of 16 octets (128 bits) // we use an unpacked representation, value_type may be larger than 8 bits, // in which case every input operation must assert data[i] < 256 for i < 16 // note that even char may be more than 8 bits in some particular platform typedef unsigned char value_type; typedef std::size_t size_type; const size_type size = 16, string_size = 36; class uuid_t { typedef array data_type; data_type data; public: // we keep data uninitialized to stress concreteness uuid_t() {} ~uuid_t() {} // trivial copy and assignment uuid_t(const uuid_t& rhs) : data(rhs.data) {} uuid_t& operator=(const uuid_t& rhs) { data = rhs.data; return *this; } // OK, now we bow to convenience // initialization from C string explicit uuid_t(const char* string) { std::stringstream stream(string); get(stream); } // test for nil value bool is_nil() const { for (size_type i = 0; i < size; ++i) if (data[i]) return false; return true; } // safe bool idiom typedef data_type uuid_t::*bool_type; operator bool_type() const { return is_nil() ? 0 : &uuid_t::data; } // comparison operators define a total order bool operator==(const uuid_t& rhs) const { return data == rhs.data; } bool operator<(const uuid_t& rhs) const { return data < rhs.data; } // stream operators template std::basic_ostream& put(std::basic_ostream& os) const; template std::basic_istream& get(std::basic_istream& is); }; // comparison operators define a total order inline bool operator>(const uuid_t& lhs, const uuid_t& rhs) { return (rhs < lhs); } inline bool operator<=(const uuid_t& lhs, const uuid_t& rhs) { return !(rhs < lhs); } inline bool operator>=(const uuid_t& lhs, const uuid_t& rhs) { return !(lhs < rhs); } inline bool operator!=(const uuid_t& lhs, const uuid_t& rhs) { return !(lhs == rhs); } template std::basic_ostream& uuid_t::put(std::basic_ostream& os) const { if (!os.good()) return os; const typename std::basic_ostream::sentry ok(os); if (ok) { ios_flags_saver flags(os); basic_ios_fill_saver fill(os); const std::streamsize width = os.width(0); const std::streamsize mysize = string_size; // right padding if (flags.value() & (std::ios_base::right | std::ios_base::internal)) for (std::streamsize i = width; i > mysize; --i) os << fill.value(); os << std::hex; os.fill(os.widen('0')); for (size_t i = 0; i < size; ++i) { os.width(2); os << static_cast(data[i]); if (i == 3 || i == 5 || i == 7 || i == 9) os << os.widen('-'); } // left padding if (flags.value() & std::ios_base::left) for (std::streamsize i = width; i > mysize; --i) os << fill.value(); } return os; } template std::basic_istream& uuid_t::get(std::basic_istream& is) { if (!is.good()) return is; const typename std::basic_istream::sentry ok(is); if (ok) { char_t hexdigits[16]; char_t* const npos = hexdigits+16; typedef std::ctype facet_t; const facet_t& facet = std::use_facet(is.getloc()); const char* tmp = "0123456789abcdef"; facet.widen(tmp, tmp+16, hexdigits); char_t c; char_t* f; for (size_t i = 0; i < size; ++i) { is >> c; c = facet.tolower(c); f = std::find(hexdigits, npos, c); if (f == npos) { is.setstate(std::ios_base::failbit); break; } data[i] = static_cast(std::distance(hexdigits, f)); is >> c; c = facet.tolower(c); f = std::find(hexdigits, npos, c); if (f == npos) { is.setstate(std::ios_base::failbit); break; } data[i] <<= 4; data[i] |= static_cast(std::distance(hexdigits, f)); if (i == 3 || i == 5 || i == 7 || i == 9) { is >> c; if (c != is.widen('-')) { is.setstate(std::ios_base::failbit); break; } } } if (!is) throw std::runtime_error("failed to extract valid uuid from stream."); } return is; } template inline std::basic_ostream& operator<<(std::basic_ostream& os, const uuid_t& uuid) { return uuid.put(os); } template inline std::basic_istream& operator>>(std::basic_istream& is, uuid_t& uuid) { return uuid.get(is); } } // namespace kashmir::uuid using uuid::uuid_t; } // namespace kashmir #endif kashmir-dependency-master/kashmir/md5_gen.h0000644000175000017500000000125412560442737020256 0ustar aviauaviau// md5_gen.h -- hash algorithm described in IETF RFC 1321 // Copyright (C) 2013 Kenneth Laskoski /** @file md5_gen.h @brief hash algorithm described in IETF RFC 1321 @author Copyright (C) 2013 Kenneth Laskoski */ #ifndef KL_MD5_GEN_H #define KL_MD5_GEN_H #include "md5.h" namespace kashmir { namespace md5 { template class engine { crtp_impl *const self; public: engine() : self(static_cast(this)) {} void update(const char *const source, std::size_t count) { self->update(source, count); } md5_t operator()() { return (*self)(); } }; } // namespace kashmir::md5 } // namespace kashmir #endif kashmir-dependency-master/kashmir/array.h0000644000175000017500000000471312560442737020061 0ustar aviauaviau// array.h -- STL container interface to arrays // Copyright (C) 2009 Kenneth Laskoski /** @file array.h @brief STL container interface to arrays @author Copyright (C) 2009 Kenneth Laskoski */ #ifndef KL_ARRAY_H #define KL_ARRAY_H #include namespace kashmir { template class array { T data[N]; public: array() {} ~array() {} typedef T value_type; typedef std::size_t size_type; enum { size = N }; // copy and assignment array(const array& rhs) { std::copy(rhs.data, rhs.data+size, data); } array& operator=(const array& rhs) { std::copy(rhs.data, rhs.data+size, data); return *this; } value_type& operator[](size_type i) { return data[i]; } const value_type& operator[](size_type i) const { return data[i]; } typedef value_type* iterator; iterator begin() { return data; } iterator end() { return data+N; } typedef const value_type* const_iterator; const_iterator begin() const { return data; } const_iterator end() const { return data+N; } typedef std::reverse_iterator reverse_iterator; reverse_iterator rbegin() { return reverse_iterator(end()); } reverse_iterator rend() { return reverse_iterator(begin()); } typedef std::reverse_iterator const_reverse_iterator; const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); } const_reverse_iterator rend() const { return const_reverse_iterator(begin()); } }; // comparison operators define a total order template inline bool operator==(const array& lhs, const array& rhs) { return std::equal(lhs.begin(), lhs.end(), rhs.begin()); } template inline bool operator<(const array& lhs, const array& rhs) { return std::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); } template inline bool operator>(const array& lhs, const array& rhs) { return (rhs < lhs); } template inline bool operator<=(const array& lhs, const array& rhs) { return !(rhs < lhs); } template inline bool operator>=(const array& lhs, const array& rhs) { return !(lhs < rhs); } template inline bool operator!=(const array& lhs, const array& rhs) { return !(lhs == rhs); } } // namespace kashmir #endif