Mercurial > code
changeset 493:59e2c93af9ed
Remove dynlib, directory, hash
author | David Demelier <markand@malikania.fr> |
---|---|
date | Mon, 30 Nov 2015 14:24:09 +0100 |
parents | b5b2bb02412a |
children | 94296e6bdb00 |
files | CMakeLists.txt modules/directory/CMakeLists.txt modules/directory/directory.cpp modules/directory/directory.h modules/directory/test/main.cpp modules/dynlib/CMakeLists.txt modules/dynlib/dynlib.cpp modules/dynlib/dynlib.h modules/dynlib/test/Plugin.cpp modules/dynlib/test/main.cpp modules/hash/CMakeLists.txt modules/hash/hash.cpp modules/hash/hash.h modules/hash/test/main.cpp |
diffstat | 14 files changed, 1 insertions(+), 1014 deletions(-) [+] |
line wrap: on
line diff
--- a/CMakeLists.txt Thu Nov 19 16:41:02 2015 +0100 +++ b/CMakeLists.txt Mon Nov 30 14:24:09 2015 +0100 @@ -57,9 +57,6 @@ endif () add_subdirectory(modules/base64) -add_subdirectory(modules/directory) -add_subdirectory(modules/dynlib) -add_subdirectory(modules/hash) add_subdirectory(modules/ini) add_subdirectory(modules/js) add_subdirectory(modules/json) @@ -67,4 +64,4 @@ add_subdirectory(modules/sockets) add_subdirectory(modules/unicode) add_subdirectory(modules/xdg) -add_subdirectory(modules/zip) \ No newline at end of file +add_subdirectory(modules/zip)
--- a/modules/directory/CMakeLists.txt Thu Nov 19 16:41:02 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,26 +0,0 @@ -# -# CMakeLists.txt -- directory module -# -# Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> -# -# Permission to use, copy, modify, and/or distribute this software for any -# purpose with or without fee is hereby granted, provided that the above -# copyright notice and this permission notice appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -# - -project(directory) - -code_define_module( - NAME directory - SOURCES - ${directory_SOURCE_DIR}/directory.cpp - ${directory_SOURCE_DIR}/directory.h -)
--- a/modules/directory/directory.cpp Thu Nov 19 16:41:02 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,159 +0,0 @@ -/* - * directory.cpp -- open and read directories - * - * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <sstream> -#include <stdexcept> - -#include "directory.h" - -#if defined(_WIN32) -# include <Windows.h> -#else -# include <cstring> -# include <cerrno> - -# include <sys/types.h> -# include <dirent.h> -#endif - -#if defined(_WIN32) - -namespace { - -std::string systemError() -{ - LPSTR error = nullptr; - std::string errmsg = "Unknown error"; - - FormatMessageA( - FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPSTR)&error, 0, NULL); - - if (error) { - errmsg = std::string(error); - LocalFree(error); - } - - return errmsg; -} - -} // !namespace - -void Directory::systemLoad(const std::string &path, int flags) -{ - std::ostringstream oss; - HANDLE handle; - WIN32_FIND_DATA fdata; - - oss << path << "\\*"; - handle = FindFirstFile(oss.str().c_str(), &fdata); - - if (handle == nullptr) - throw std::runtime_error(systemError()); - - do { - DirectoryEntry entry; - - entry.name = fdata.cFileName; - if (entry.name == "." && !(flags & Directory::Dot)) - continue; - if (entry.name == ".." && !(flags & Directory::DotDot)) - continue; - - switch (fdata.dwFileAttributes) { - case FILE_ATTRIBUTE_DIRECTORY: - entry.type = DirectoryEntry::Dir; - break; - case FILE_ATTRIBUTE_NORMAL: - entry.type = DirectoryEntry::File; - break; - case FILE_ATTRIBUTE_REPARSE_POINT: - entry.type = DirectoryEntry::Link; - break; - default: - break; - } - - push_back(entry); - } while (FindNextFile(handle, &fdata) != 0); - - FindClose(handle); -} - -#else - -void Directory::systemLoad(const std::string &path, int flags) -{ - DIR *dp; - struct dirent *ent; - - if ((dp = opendir(path.c_str())) == nullptr) - throw std::runtime_error(strerror(errno)); - - while ((ent = readdir(dp)) != nullptr) { - DirectoryEntry entry; - - entry.name = ent->d_name; - if (entry.name == "." && !(flags & Directory::Dot)) - continue; - if (entry.name == ".." && !(flags & Directory::DotDot)) - continue; - - switch (ent->d_type) { - case DT_DIR: - entry.type = DirectoryEntry::Dir; - break; - case DT_REG: - entry.type = DirectoryEntry::File; - break; - case DT_LNK: - entry.type = DirectoryEntry::Link; - break; - default: - break; - } - - push_back(entry); - } - - closedir(dp); -} - -#endif - -bool operator==(const DirectoryEntry &e1, const DirectoryEntry &e2) -{ - return e1.name == e2.name && e1.type == e2.type; -} - -Directory::Directory() -{ -} - -Directory::Directory(const std::string &path, int flags) -{ - systemLoad(path, flags); -} - -int Directory::count() const -{ - return size(); -}
--- a/modules/directory/directory.h Thu Nov 19 16:41:02 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,90 +0,0 @@ -/* - * directory.h -- open and read directories - * - * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef _DIRECTORY_H_ -#define _DIRECTORY_H_ - -#include <cstddef> -#include <string> -#include <vector> - -/** - * @class DirectoryEntry - * @brief Entry in the directory list - */ -class DirectoryEntry { -public: - /** - * @brief Describe the type of an entry - */ - enum { - Unknown = 0, - File, - Dir, - Link - }; - - std::string name; //! name of entry (base name) - int type{Unknown}; //! type of file - - friend bool operator==(const DirectoryEntry &e1, const DirectoryEntry &e2); -}; - -/** - * @class Directory - * @brief class to manipulate directories - * - * This class allow the user to iterate directories in a for range based - * loop using iterators. - */ -class Directory : public std::vector<DirectoryEntry> { -public: - /** - * @enum Flags - * @brief optional flags to read directories - */ - enum Flags { - Dot = (1 << 0), //!< If set, lists "." too - DotDot = (1 << 1) //!< If set, lists ".." too - }; - -private: - void systemLoad(const std::string &path, int flags); - -public: - /** - * Default constructor, does nothing. - */ - Directory(); - - /** - * Open a directory and read all its content. - * @param path the path - * @param flags the optional flags - */ - Directory(const std::string &path, int flags = 0); - - /** - * Get the number of entries in the directory. - * - * @return the number - */ - int count() const; -}; - -#endif // !_DIRECTORY_H_
--- a/modules/directory/test/main.cpp Thu Nov 19 16:41:02 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,115 +0,0 @@ -/* - * main.cpp -- test directory - * - * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <gtest/gtest.h> - -#include <directory.h> - -TEST(Filter, withDot) -{ - try { - int flags = Directory::Dot; - Directory directory(".", flags); - bool dot(false); - bool dotdot(false); - - for (const auto &entry : directory) { - if (entry.name == ".") - dot = true; - if (entry.name == "..") - dotdot = true; - } - - ASSERT_TRUE(dot); - ASSERT_FALSE(dotdot); - } catch (const std::runtime_error &error) { - FAIL() << "skipping test because: " << error.what(); - } -} - -TEST(Filter, withDotDot) -{ - try { - int flags = Directory::DotDot; - Directory directory(".", flags); - bool dot(false); - bool dotdot(false); - - for (const auto &entry : directory) { - if (entry.name == ".") - dot = true; - if (entry.name == "..") - dotdot = true; - } - - ASSERT_FALSE(dot); - ASSERT_TRUE(dotdot); - } catch (const std::runtime_error &error) { - FAIL() << "skipping test because: " << error.what(); - } -} - -TEST(Filter, withBothDots) -{ - try { - int flags = Directory::Dot | Directory::DotDot; - Directory directory(".", flags); - bool dot(false); - bool dotdot(false); - - for (const auto &entry : directory) { - if (entry.name == ".") - dot = true; - if (entry.name == "..") - dotdot = true; - } - - ASSERT_TRUE(dot); - ASSERT_TRUE(dotdot); - } catch (const std::runtime_error &error) { - FAIL() << "skipping test because: " << error.what(); - } -} - -TEST(Filter, withoutDots) -{ - try { - Directory directory("."); - bool dot(false); - bool dotdot(false); - - for (const auto &entry : directory) { - if (entry.name == ".") - dot = true; - if (entry.name == "..") - dotdot = true; - } - - ASSERT_FALSE(dot); - ASSERT_FALSE(dotdot); - } catch (const std::runtime_error &error) { - FAIL() << "skipping test because: " << error.what(); - } -} - -int main(int argc, char **argv) -{ - testing::InitGoogleTest(&argc, argv); - - return RUN_ALL_TESTS(); -}
--- a/modules/dynlib/CMakeLists.txt Thu Nov 19 16:41:02 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,58 +0,0 @@ -# -# CMakeLists.txt -- dynlib module -# -# Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> -# -# Permission to use, copy, modify, and/or distribute this software for any -# purpose with or without fee is hereby granted, provided that the above -# copyright notice and this permission notice appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -# - -project(dynlib) - -if (WIN32) - set(EXTENSION ".dll") -elseif (UNIX) - set(EXTENSION ".so") -elseif (APPLE) - set(EXTENSION ".dylib") -else () - message(FATAL_ERROR "Unsupported platform") -endif () - -if (CMAKE_SYSTEM_NAME MATCHES "Linux") - set(LIBRARIES dl) -endif () - -code_define_module( - NAME dynlib - LIBRARIES ${LIBRARIES} - FLAGS EXTENSION=\"${EXTENSION}\" - SOURCES - ${dynlib_SOURCE_DIR}/dynlib.cpp - ${dynlib_SOURCE_DIR}/dynlib.h -) - -if (WITH_DYNLIB) - # Add a fake plugin - add_library(dynlib-plugin MODULE ${dynlib_SOURCE_DIR}/test/Plugin.cpp) - set_target_properties( - dynlib-plugin - PROPERTIES - PREFIX "" - LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR} - LIBRARY_OUTPUT_DIRECTORY_DEBUG ${CMAKE_BINARY_DIR} - LIBRARY_OUTPUT_DIRECTORY_RELEASE ${CMAKE_BINARY_DIR} - LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO ${CMAKE_BINARY_DIR} - LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL ${CMAKE_BINARY_DIR} - ) - target_include_directories(dynlib-plugin PRIVATE ${dynlib_SOURCE_DIR}) -endif ()
--- a/modules/dynlib/dynlib.cpp Thu Nov 19 16:41:02 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,144 +0,0 @@ -/* - * dynlib.cpp -- portable shared library loader - * - * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <stdexcept> - -#if defined(_WIN32) -# include <Windows.h> -#else -# include <dlfcn.h> -#endif - -#include "dynlib.h" - -#if defined(_WIN32) - -namespace { - -std::string systemError() -{ - LPSTR error = nullptr; - std::string errmsg = "Unknown error"; - - FormatMessageA( - FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, - GetLastError(), - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPSTR)&error, 0, NULL); - - if (error) { - errmsg = std::string(error); - LocalFree(error); - } - - return errmsg; -} - -} - -void Dynlib::systemInit() -{ - m_handle = nullptr; -} - -Dynlib::Handle Dynlib::systemLoad(const std::string &path, Policy) const -{ - Handle handle = LoadLibraryA(path.c_str()); - - if (handle == nullptr) - throw std::runtime_error(systemError()); - - return handle; -} - -Dynlib::Sym Dynlib::systemSym(const std::string &name) -{ - Sym sym; - - if (m_handle == nullptr) - throw std::runtime_error("library not loaded"); - - sym = GetProcAddress(m_handle, name.c_str()); - if (sym == nullptr) - throw std::out_of_range(systemError()); - - return sym; -} - -void Dynlib::systemClose() -{ - if (m_handle != nullptr) - FreeLibrary(m_handle); -} - -#else - -void Dynlib::systemInit() -{ - m_handle = nullptr; -} - -Dynlib::Handle Dynlib::systemLoad(const std::string &path, Policy policy) const -{ - int mode = (policy == Immediately) ? RTLD_NOW : RTLD_LAZY; - Handle handle; - - handle = dlopen(path.c_str(), mode); - if (handle == nullptr) - throw std::runtime_error(dlerror()); - - return handle; -} - -Dynlib::Sym Dynlib::systemSym(const std::string &name) -{ - Sym sym; - - if (m_handle == nullptr) - throw std::runtime_error("library not loaded"); - - sym = dlsym(m_handle, name.c_str()); - if (sym == nullptr) - throw std::out_of_range(dlerror()); - - return sym; -} - -void Dynlib::systemClose() -{ - if (m_handle != nullptr) - dlclose(m_handle); -} - -#endif - -Dynlib::Dynlib() -{ - systemInit(); -} - -Dynlib::Dynlib(const std::string &path, Policy policy) -{ - m_handle = systemLoad(path, policy); -} - -Dynlib::~Dynlib() -{ - systemClose(); -}
--- a/modules/dynlib/dynlib.h Thu Nov 19 16:41:02 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,103 +0,0 @@ -/* - * dynlib.h -- portable shared library loader - * - * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef _DYNLIB_H_ -#define _DYNLIB_H_ - -#include <string> - -#if defined(_WIN32) -# include <Windows.h> -# define DYNLIB_EXPORT __declspec(dllexport) -#else -# define DYNLIB_EXPORT -#endif - -/** - * @class Dynlib - * @brief Load a dynamic module - * - * This class is a portable wrapper to load shared libraries on - * supported systems. - */ -class Dynlib { -public: -#if defined(_WIN32) - using Handle = HMODULE; - using Sym = FARPROC; -#else - using Handle = void *; - using Sym = void *; -#endif - - enum Policy { - Immediately, //! load symbols immediately - Lazy //! load symbols when needed - }; - -private: - Handle m_handle; - - void systemInit(); - Handle systemLoad(const std::string &path, Policy policy) const; - Sym systemSym(const std::string &name); - void systemClose(); - -public: - /** - * Copy is forbidden. - */ - Dynlib(const Dynlib &) = delete; - Dynlib &operator=(const Dynlib &) = delete; - - /** - * Default constructor. - */ - Dynlib(); - - /** - * Constructor to load a shared module. The path must - * be absolute. - * - * @param path the absolute path - * @param policy the policy to load - * @throw std::runtime_error on error - */ - Dynlib(const std::string &path, Policy policy = Immediately); - - /** - * Close the library automatically. - */ - ~Dynlib(); - - /** - * Get a symbol from the library. - * - * @param name the symbol - * @return the symbol - * @throw std::runtime_error on error - * @throw std::out_of_range if not found - */ - template <typename T> - T sym(const std::string &name) - { - return reinterpret_cast<T>(systemSym(name)); - } -}; - -#endif // !_DYNLIB_H_
--- a/modules/dynlib/test/Plugin.cpp Thu Nov 19 16:41:02 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,12 +0,0 @@ -#include <string> - -#include <dynlib.h> - -extern "C" { - -void DYNLIB_EXPORT initialize(std::string &result) -{ - result = "Hello World"; -} - -}
--- a/modules/dynlib/test/main.cpp Thu Nov 19 16:41:02 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -/* - * TestDynLib.cpp -- test the dynamic library loader - * - * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <iostream> - -#include <gtest/gtest.h> - -#include <dynlib.h> - -/* - * NOTE: the EXTENSION is defined by CMake in the form of a string - * literal containing the appropriate extension for the given system. - */ - -using Initialize = void (*)(std::string &s); - -TEST(Basic, initialize) -{ - try { - Dynlib library("./dynlib-plugin" EXTENSION); - Initialize init = library.sym<Initialize>("initialize"); - - std::string expected("Hello World"); - std::string result; - - init(result); - - ASSERT_EQ(expected, result); - } catch (const std::runtime_error &error) { - FAIL() << error.what(); - } catch (const std::out_of_range &error) { - FAIL() << error.what(); - } -} - -TEST(Basic, absent) -{ - try { - Dynlib library("./dynlib-plugin" EXTENSION); - library.sym<Initialize>("initialize_typo"); - } catch (const std::out_of_range &error) { - return; - } - - FAIL() << "Expected a failure but succeed"; -} - -int main(int argc, char **argv) -{ - testing::InitGoogleTest(&argc, argv); - - return RUN_ALL_TESTS(); -}
--- a/modules/hash/CMakeLists.txt Thu Nov 19 16:41:02 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,30 +0,0 @@ -# -# CMakeLists.txt -- hash module -# -# Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> -# -# Permission to use, copy, modify, and/or distribute this software for any -# purpose with or without fee is hereby granted, provided that the above -# copyright notice and this permission notice appear in all copies. -# -# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -# - -project(hash) - -if (OPENSSL_FOUND) - code_define_module( - NAME hash - LIBRARIES ${OPENSSL_LIBRARIES} - INCLUDES ${OPENSSL_INCLUDE_DIR} - SOURCES - ${hash_SOURCE_DIR}/hash.cpp - ${hash_SOURCE_DIR}/hash.h - ) -endif ()
--- a/modules/hash/hash.cpp Thu Nov 19 16:41:02 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -/* - * hash.cpp -- hash functions - * - * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include "hash.h" - -#include <openssl/sha.h> -#include <openssl/md5.h> - -namespace hash { - -namespace { - -template <typename Context> -using Init = int (*)(Context *); - -template <typename Context> -using Update = int (*)(Context *, const void *, size_t); - -template <typename Context> -using Final = int (*)(unsigned char *, Context *); - -template <typename Context, size_t Length> -std::string convert(const std::string &input, Init<Context> init, Update<Context> update, Final<Context> finalize) -{ - unsigned char digest[Length]; - char hash[Length * 2 + 1]; - - Context ctx; - init(&ctx); - update(&ctx, input.c_str(), input.length()); - finalize(digest, &ctx); - - for (unsigned long i = 0; i < Length; i++) - sprintf(&hash[i * 2], "%02x", (unsigned int)digest[i]); - - return std::string(hash); -} - -} // !namespace - -std::string md5(const std::string &input) -{ - return convert<MD5_CTX, MD5_DIGEST_LENGTH>(input, MD5_Init, MD5_Update, MD5_Final); -} - -std::string sha1(const std::string &input) -{ - return convert<SHA_CTX, SHA_DIGEST_LENGTH>(input, SHA1_Init, SHA1_Update, SHA1_Final); -} - -std::string sha256(const std::string &input) -{ - return convert<SHA256_CTX, SHA256_DIGEST_LENGTH>(input, SHA256_Init, SHA256_Update, SHA256_Final); -} - -std::string sha512(const std::string &input) -{ - return convert<SHA512_CTX, SHA512_DIGEST_LENGTH>(input, SHA512_Init, SHA512_Update, SHA512_Final); -} - -} // !hash
--- a/modules/hash/hash.h Thu Nov 19 16:41:02 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -/* - * hash.h -- hash functions - * - * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#ifndef _HASH_H_ -#define _HASH_H_ - -/** - * @file hash.h - * @brief Hash functions - */ - -#include <string> - -namespace hash { - -/** - * Hash using MD5. - * - * @param input the input string - * @return the hashed string - */ -std::string md5(const std::string &input); - -/** - * Hash using SHA1. - * - * @param input the input string - * @return the hashed string - */ -std::string sha1(const std::string &input); - -/** - * Hash using SHA256. - * - * @param input the input string - * @return the hashed string - */ -std::string sha256(const std::string &input); - -/** - * Hash using SHA512. - * - * @param input the input string - * @return the hashed string - */ -std::string sha512(const std::string &input); - -} // !hash - -#endif // !_HASH_H_
--- a/modules/hash/test/main.cpp Thu Nov 19 16:41:02 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -/* - * main.cpp -- test the hash cryptographic functions - * - * Copyright (c) 2013-2015 David Demelier <markand@malikania.fr> - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include <gtest/gtest.h> - -#include <hash.h> - -/* - * We test the "Hello World" message in all cryptographic functions. - */ - -TEST(Hash, md5) -{ - std::string expected = "b10a8db164e0754105b7a99be72e3fe5"; - std::string output = hash::md5("Hello World"); - - ASSERT_EQ(expected, output); -} - -TEST(Hash, sha1) -{ - std::string expected = "0a4d55a8d778e5022fab701977c5d840bbc486d0"; - std::string output = hash::sha1("Hello World"); - - ASSERT_EQ(expected, output); -} - -TEST(Hash, sha256) -{ - std::string expected = "a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e"; - std::string output = hash::sha256("Hello World"); - - ASSERT_EQ(expected, output); -} - -TEST(Hash, sha512) -{ - std::string expected = "2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0daa8f51671064615d645ae27acb15bfb1447f459b"; - std::string output = hash::sha512("Hello World"); - - ASSERT_EQ(expected, output); -} - -int main(int argc, char **argv) -{ - testing::InitGoogleTest(&argc, argv); - - return RUN_ALL_TESTS(); -}