From aa31c0517333688b611ba453b08138960b53244e Mon Sep 17 00:00:00 2001 From: Janusz Kozerski Date: Tue, 23 Jun 2015 17:22:19 +0200 Subject: Add pulling out certificates from apps' signature. * Move DPL include files from "dpl/*" into "cchecker/dpl/* because of cert-svc-vcore also uses DPL (from wrt-commons), and that was causing build break. * Certificates are stored in buffer and database. * OCSP URLs are pulled out from certificates and stored in database. Change-Id: Ic0601da38619f7c7b784d952abe56888db036673 --- packaging/cert-checker.spec | 1 + src/CMakeLists.txt | 2 + src/certs.cpp | 156 +++++++ src/db/sql_query.cpp | 2 +- src/dpl/core/include/cchecker/dpl/assert.h | 43 ++ src/dpl/core/include/cchecker/dpl/availability.h | 30 ++ src/dpl/core/include/cchecker/dpl/char_traits.h | 39 ++ src/dpl/core/include/cchecker/dpl/colors.h | 74 +++ src/dpl/core/include/cchecker/dpl/errno_string.h | 36 ++ src/dpl/core/include/cchecker/dpl/exception.h | 385 ++++++++++++++++ src/dpl/core/include/cchecker/dpl/noncopyable.h | 38 ++ src/dpl/core/include/cchecker/dpl/noreturn.h | 27 ++ src/dpl/core/include/cchecker/dpl/optional.h | 175 ++++++++ src/dpl/core/include/cchecker/dpl/scoped_array.h | 65 +++ src/dpl/core/include/cchecker/dpl/scoped_free.h | 57 +++ .../core/include/cchecker/dpl/scoped_resource.h | 80 ++++ src/dpl/core/include/cchecker/dpl/string.h | 141 ++++++ src/dpl/core/include/dpl/assert.h | 43 -- src/dpl/core/include/dpl/availability.h | 30 -- src/dpl/core/include/dpl/char_traits.h | 38 -- src/dpl/core/include/dpl/colors.h | 74 --- src/dpl/core/include/dpl/errno_string.h | 35 -- src/dpl/core/include/dpl/exception.h | 385 ---------------- src/dpl/core/include/dpl/noncopyable.h | 38 -- src/dpl/core/include/dpl/noreturn.h | 27 -- src/dpl/core/include/dpl/optional.h | 175 -------- src/dpl/core/include/dpl/scoped_array.h | 65 --- src/dpl/core/include/dpl/scoped_free.h | 57 --- src/dpl/core/include/dpl/scoped_resource.h | 80 ---- src/dpl/core/include/dpl/string.h | 140 ------ src/dpl/core/src/assert.cpp | 4 +- src/dpl/core/src/char_traits.cpp | 3 +- src/dpl/core/src/colors.cpp | 3 +- src/dpl/core/src/errno_string.cpp | 11 +- src/dpl/core/src/exception.cpp | 2 +- src/dpl/core/src/noncopyable.cpp | 3 +- src/dpl/core/src/string.cpp | 10 +- .../cchecker/dpl/db/naive_synchronization_object.h | 45 ++ .../db/include/cchecker/dpl/db/sql_connection.h | 499 +++++++++++++++++++++ .../include/dpl/db/naive_synchronization_object.h | 45 -- src/dpl/db/include/dpl/db/sql_connection.h | 499 --------------------- src/dpl/db/src/naive_synchronization_object.cpp | 3 +- src/dpl/db/src/sql_connection.cpp | 11 +- src/include/cchecker/app.h | 21 + src/include/cchecker/certs.h | 46 ++ src/include/cchecker/logic.h | 4 +- src/include/cchecker/sql_query.h | 2 +- src/logic.cpp | 26 +- tests/colour_log_formatter.cpp | 2 +- 49 files changed, 2013 insertions(+), 1764 deletions(-) create mode 100644 src/certs.cpp create mode 100644 src/dpl/core/include/cchecker/dpl/assert.h create mode 100644 src/dpl/core/include/cchecker/dpl/availability.h create mode 100644 src/dpl/core/include/cchecker/dpl/char_traits.h create mode 100644 src/dpl/core/include/cchecker/dpl/colors.h create mode 100644 src/dpl/core/include/cchecker/dpl/errno_string.h create mode 100644 src/dpl/core/include/cchecker/dpl/exception.h create mode 100644 src/dpl/core/include/cchecker/dpl/noncopyable.h create mode 100644 src/dpl/core/include/cchecker/dpl/noreturn.h create mode 100644 src/dpl/core/include/cchecker/dpl/optional.h create mode 100644 src/dpl/core/include/cchecker/dpl/scoped_array.h create mode 100644 src/dpl/core/include/cchecker/dpl/scoped_free.h create mode 100644 src/dpl/core/include/cchecker/dpl/scoped_resource.h create mode 100644 src/dpl/core/include/cchecker/dpl/string.h delete mode 100644 src/dpl/core/include/dpl/assert.h delete mode 100644 src/dpl/core/include/dpl/availability.h delete mode 100644 src/dpl/core/include/dpl/char_traits.h delete mode 100644 src/dpl/core/include/dpl/colors.h delete mode 100644 src/dpl/core/include/dpl/errno_string.h delete mode 100644 src/dpl/core/include/dpl/exception.h delete mode 100644 src/dpl/core/include/dpl/noncopyable.h delete mode 100644 src/dpl/core/include/dpl/noreturn.h delete mode 100644 src/dpl/core/include/dpl/optional.h delete mode 100644 src/dpl/core/include/dpl/scoped_array.h delete mode 100644 src/dpl/core/include/dpl/scoped_free.h delete mode 100644 src/dpl/core/include/dpl/scoped_resource.h delete mode 100644 src/dpl/core/include/dpl/string.h create mode 100644 src/dpl/db/include/cchecker/dpl/db/naive_synchronization_object.h create mode 100644 src/dpl/db/include/cchecker/dpl/db/sql_connection.h delete mode 100644 src/dpl/db/include/dpl/db/naive_synchronization_object.h delete mode 100644 src/dpl/db/include/dpl/db/sql_connection.h create mode 100644 src/include/cchecker/certs.h diff --git a/packaging/cert-checker.spec b/packaging/cert-checker.spec index bccc58d..9f3ce30 100644 --- a/packaging/cert-checker.spec +++ b/packaging/cert-checker.spec @@ -14,6 +14,7 @@ BuildRequires: pkgconfig(glib-2.0) BuildRequires: pkgconfig(notification) BuildRequires: pkgconfig(dbus-1) BuildRequires: pkgconfig(dbus-glib-1) +BuildRequires: pkgconfig(cert-svc-vcore) BuildRequires: pkgconfig(libsystemd-journal) BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(sqlite3) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index da303b4..bd75eb5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,6 @@ PKG_CHECK_MODULES(CERT_CHECKER_DEP REQUIRED + cert-svc-vcore dbus-1 dbus-glib-1 db-util @@ -19,6 +20,7 @@ SET(CERT_CHECKER_SOURCES ${CERT_CHECKER_SRC_PATH}/app.cpp ${CERT_CHECKER_SRC_PATH}/logic.cpp ${CERT_CHECKER_SRC_PATH}/queue.cpp + ${CERT_CHECKER_SRC_PATH}/certs.cpp # logs ${CERT_CHECKER_SRC_PATH}/log/log.cpp # dpl diff --git a/src/certs.cpp b/src/certs.cpp new file mode 100644 index 0000000..4a69fde --- /dev/null +++ b/src/certs.cpp @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file certs.cpp + * @author Janusz Kozerski (j.kozerski@samsung.com) + * @version 1.0 + * @brief This file is the implementation of certificates logic + * Getting out findinf app signature, getting certificates out of + * signature. Checking OCSP + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace { +const std::string signatureXmlSchemaPath = std::string(tzplatform_getenv(TZ_SYS_SHARE)) + + std::string("/app-installers/signature_schema.xsd"); +} + +namespace CCHECKER { + +Certs::Certs() +{ + ValidationCore::VCoreInit(); +} + +Certs::~Certs() +{ + ValidationCore::VCoreDeinit(); +} + +void Certs::get_certificates (app_t &app, ocsp_urls_t &ocsp_urls) +{ + std::vector signatures; + (void) signatures; + + if (0 != tzplatform_set_user(app.uid)) { + LogError("Cannot set user: tzplatform_set_user has failed"); + return; + } + + if (app.app_id == TEMP_APP_ID) { + LogDebug("Temporary app_id. Searching for apps in package."); + search_app(app, ocsp_urls); + } + else { + const char *pkg_path = tzplatform_mkpath(TZ_USER_APP, app.pkg_id.c_str()); + std::string app_path = std::string(pkg_path) + std::string("/") + app.app_id; + find_app_signatures (app, app_path, ocsp_urls); + } + +} + +/* Since there's no information about application in signal, + * and we've got information only about package, we have to check + * all applications that belongs to that package + */ +void Certs::search_app (app_t &app, ocsp_urls_t &ocsp_urls) +{ + DIR *dp; + struct dirent *entry; + const char *pkg_path = tzplatform_mkpath(TZ_USER_APP, app.pkg_id.c_str()); + if (!pkg_path) { + LogError("tzplatform_mkpath has returned NULL for TZ_USER_APP"); + return; + } + + dp = opendir(pkg_path); + if (dp != NULL) { + while ((entry = readdir(dp))) { + if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 && entry->d_type == DT_DIR) { + LogDebug("Found app: " << entry->d_name); + std::string app_path = std::string(pkg_path) + std::string("/") + std::string(entry->d_name); + find_app_signatures(app, app_path, ocsp_urls); + } + } + closedir(dp); //close directory + } + else + LogError("Couldn't open the package directory."); +} + +// Together with certificates we can pull out OCSP URLs +void Certs::find_app_signatures (app_t &app, const std::string &app_path, ocsp_urls_t &ocsp_urls) +{ + ValidationCore::SignatureFinder signature_finder(app_path); + ValidationCore::SignatureFileInfoSet signature_files; + + if (signature_finder.find(signature_files) != + ValidationCore::SignatureFinder::NO_ERROR) { + LogError("Error while searching for signatures in " << app_path.c_str()); + return; + } + LogDebug("Number of signature files: " << signature_files.size()); + + LogDebug("Searching for certificates"); + for (auto iter = signature_files.begin(); iter != signature_files.end(); iter++){ + LogDebug("Checking signature"); + ValidationCore::SignatureData data(app_path + std::string("/") + (*iter).getFileName(), + (*iter).getFileNumber()); + LogDebug("signatureXmlSchemaPath: " << signatureXmlSchemaPath); + try { + ValidationCore::SignatureReader reader; + reader.initialize(data, signatureXmlSchemaPath); + reader.read(data); + ValidationCore::CertificateList certs = data.getCertList(); + for (auto cert_iter = certs.begin(); cert_iter != certs.end(); cert_iter++ ){ + std::string app_cert = (*cert_iter)->getBase64(); + app.certificates.push_back(app_cert); + LogDebug("Certificate: " << app_cert << " has been added"); + + // check OCSP URL + std::string ocsp_url = DPL::ToUTF8String((*cert_iter)->getOCSPURL()); + if (ocsp_url != std::string("")) { + std::string issuer = DPL::ToUTF8String( + (*cert_iter)->getCommonName(ValidationCore::Certificate::FIELD_ISSUER)); + int64_t time = (*cert_iter)->getNotBefore(); + url_t url(issuer, ocsp_url, time); + ocsp_urls.push_back(url); + LogDebug("Found OCSP URL: " << ocsp_url << " for issuer: " << issuer << ", time: " << time); + + } + } + } catch (const ValidationCore::ParserSchemaException::Base& exception) { + // Needs to catch parser exceptions + LogError("Error occured in ParserSchema: " << exception.DumpToString()); + } + } +} + +} // CCHECKER diff --git a/src/db/sql_query.cpp b/src/db/sql_query.cpp index c39be95..0968717 100644 --- a/src/db/sql_query.cpp +++ b/src/db/sql_query.cpp @@ -20,7 +20,7 @@ * @brief This file is the implementation of SQL queries */ -#include +#include #include #include diff --git a/src/dpl/core/include/cchecker/dpl/assert.h b/src/dpl/core/include/cchecker/dpl/assert.h new file mode 100644 index 0000000..03dd7d0 --- /dev/null +++ b/src/dpl/core/include/cchecker/dpl/assert.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file assert.h + * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) + * @version 1.0 + * @brief This file is the implementation file of assert + */ +#ifndef CCHECKER_ASSERT_H +#define CCHECKER_ASSERT_H + +#include + +namespace CCHECKER { +// Assertion handler procedure +// Do not call directly +// Always use Assert macro +CCHECKER_NORETURN void AssertProc(const char *condition, + const char *file, + int line, + const char *function); +} // namespace CCHECKER + +#define Assert(Condition) do { if (!(Condition)) { CCHECKER::AssertProc(#Condition, \ + __FILE__, \ + __LINE__, \ + __FUNCTION__); \ + } } while (0) + +#endif // CCHECKER_ASSERT_H diff --git a/src/dpl/core/include/cchecker/dpl/availability.h b/src/dpl/core/include/cchecker/dpl/availability.h new file mode 100644 index 0000000..b1cb894 --- /dev/null +++ b/src/dpl/core/include/cchecker/dpl/availability.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file availability.h + * @author Jihoon Chung (jihoon.chung@samsung.com) + * @version 1.0 + */ +#ifndef CCHECKER_AVAILABILITY_H +#define CCHECKER_AVAILABILITY_H + +#define CCHECKER_DEPRECATED __attribute__((deprecated)) +#define CCHECKER_DEPRECATED_WITH_MESSAGE(msg) __attribute__((deprecated(msg))) + +#define CCHECKER_UNUSED __attribute__((unused)) +#define CCHECKER_UNUSED_PARAM(variable) (void)variable + +#endif // CCHECKER_AVAILABILITY_H diff --git a/src/dpl/core/include/cchecker/dpl/char_traits.h b/src/dpl/core/include/cchecker/dpl/char_traits.h new file mode 100644 index 0000000..06e2840 --- /dev/null +++ b/src/dpl/core/include/cchecker/dpl/char_traits.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file char_traits.h + * @author Piotr Marcinkiewicz (p.marcinkiew@samsung.com) + * @version 1.0 + * @brief Char traits are used to create basic_string extended with + * additional features + * Current char traits could be extended in feature to boost + * performance + */ +#ifndef CCHECKER_CHAR_TRAITS +#define CCHECKER_CHAR_TRAITS + +#include +#include +#include +#include + +#include + +namespace CCHECKER { +typedef std::char_traits CharTraits; +} // namespace CCHECKER + +#endif // CCHECKER_CHAR_TRAITS diff --git a/src/dpl/core/include/cchecker/dpl/colors.h b/src/dpl/core/include/cchecker/dpl/colors.h new file mode 100644 index 0000000..d652f5b --- /dev/null +++ b/src/dpl/core/include/cchecker/dpl/colors.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file colors.h + * @author Lukasz Wrzosek (l.wrzosek@samsung.com) + * @version 1.0 + * @brief Some constants with definition of colors for Console + * and html output + */ + +#ifndef CCHECKER_COLORS_H +#define CCHECKER_COLORS_H + +namespace CCHECKER { +namespace Colors { +namespace Text { +extern const char* BOLD_GREEN_BEGIN; +extern const char* BOLD_GREEN_END; +extern const char* PURPLE_BEGIN; +extern const char* PURPLE_END; +extern const char* RED_BEGIN; +extern const char* RED_END; +extern const char* GREEN_BEGIN; +extern const char* GREEN_END; +extern const char* CYAN_BEGIN; +extern const char* CYAN_END; +extern const char* BOLD_RED_BEGIN; +extern const char* BOLD_RED_END; +extern const char* BOLD_YELLOW_BEGIN; +extern const char* BOLD_YELLOW_END; +extern const char* BOLD_GOLD_BEGIN; +extern const char* BOLD_GOLD_END; +extern const char* BOLD_WHITE_BEGIN; +extern const char* BOLD_WHITE_END; +extern const char* COLOR_END; +} //namespace Text + +namespace Html { +extern const char* BOLD_GREEN_BEGIN; +extern const char* BOLD_GREEN_END; +extern const char* PURPLE_BEGIN; +extern const char* PURPLE_END; +extern const char* RED_BEGIN; +extern const char* RED_END; +extern const char* GREEN_BEGIN; +extern const char* GREEN_END; +extern const char* CYAN_BEGIN; +extern const char* CYAN_END; +extern const char* BOLD_RED_BEGIN; +extern const char* BOLD_RED_END; +extern const char* BOLD_YELLOW_BEGIN; +extern const char* BOLD_YELLOW_END; +extern const char* BOLD_GOLD_BEGIN; +extern const char* BOLD_GOLD_END; +extern const char* BOLD_WHITE_BEGIN; +extern const char* BOLD_WHITE_END; +} //namespace Html +} //namespace Colors +} //namespace CCHECKER + +#endif /* CCHECKER_COLORS_H */ diff --git a/src/dpl/core/include/cchecker/dpl/errno_string.h b/src/dpl/core/include/cchecker/dpl/errno_string.h new file mode 100644 index 0000000..c98eba3 --- /dev/null +++ b/src/dpl/core/include/cchecker/dpl/errno_string.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file errno_string.h + * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) + * @version 1.0 + * @brief This file is the implementation file of errno string + */ +#ifndef CCHECKER_ERRNO_STRING_H +#define CCHECKER_ERRNO_STRING_H + +#include +#include + +#include + +namespace CCHECKER { +DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, InvalidErrnoValue) + +std::string GetErrnoString(int error = errno); +} // namespace CCHECKER + +#endif // CCHECKER_ERRNO_STRING_H diff --git a/src/dpl/core/include/cchecker/dpl/exception.h b/src/dpl/core/include/cchecker/dpl/exception.h new file mode 100644 index 0000000..f7b9afc --- /dev/null +++ b/src/dpl/core/include/cchecker/dpl/exception.h @@ -0,0 +1,385 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file exception.h + * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) + * @version 1.0 + * @brief Header file for base exception + */ +#ifndef CCHECKER_EXCEPTION_H +#define CCHECKER_EXCEPTION_H + +#include +#include +#include +#include +#include +#include + +namespace CCHECKER { +void LogUnhandledException(const std::string &str); +void LogUnhandledException(const std::string &str, + const char *filename, + int line, + const char *function); +} + +namespace CCHECKER { +class Exception +{ + private: + static unsigned int m_exceptionCount; + static Exception* m_lastException; + static void (*m_terminateHandler)(); + + static void AddRef(Exception* exception) + { + if (!m_exceptionCount) { + m_terminateHandler = std::set_terminate(&TerminateHandler); + } + + ++m_exceptionCount; + m_lastException = exception; + } + + static void UnRef(Exception* e) + { + if (m_lastException == e) { + m_lastException = NULL; + } + + --m_exceptionCount; + + if (!m_exceptionCount) { + std::set_terminate(m_terminateHandler); + m_terminateHandler = NULL; + } + } + + static void TerminateHandler() + { + if (m_lastException != NULL) { + DisplayKnownException(*m_lastException); + abort(); + } else { + DisplayUnknownException(); + abort(); + } + } + + Exception *m_reason; + std::string m_path; + std::string m_function; + int m_line; + + protected: + std::string m_message; + std::string m_className; + + public: + static std::string KnownExceptionToString(const Exception &e) + { + std::ostringstream message; + message << + "\033[1;5;31m\n=== Unhandled CCHECKER exception occurred ===\033[m\n\n"; + message << "\033[1;33mException trace:\033[m\n\n"; + message << e.DumpToString(); + message << "\033[1;31m\n=== Will now abort ===\033[m\n"; + + return message.str(); + } + + static std::string UnknownExceptionToString() + { + std::ostringstream message; + message << + "\033[1;5;31m\n=== Unhandled non-CCHECKER exception occurred ===\033[m\n\n"; + message << "\033[1;31m\n=== Will now abort ===\033[m\n"; + + return message.str(); + } + + static void DisplayKnownException(const Exception& e) + { + LogUnhandledException(KnownExceptionToString(e).c_str()); + } + + static void DisplayUnknownException() + { + LogUnhandledException(UnknownExceptionToString().c_str()); + } + + Exception(const Exception &other) + { + // Deep copy + if (other.m_reason != NULL) { + m_reason = new Exception(*other.m_reason); + } else { + m_reason = NULL; + } + + m_message = other.m_message; + m_path = other.m_path; + m_function = other.m_function; + m_line = other.m_line; + + m_className = other.m_className; + + AddRef(this); + } + + const Exception &operator =(const Exception &other) + { + if (this == &other) { + return *this; + } + + // Deep copy + if (other.m_reason != NULL) { + m_reason = new Exception(*other.m_reason); + } else { + m_reason = NULL; + } + + m_message = other.m_message; + m_path = other.m_path; + m_function = other.m_function; + m_line = other.m_line; + + m_className = other.m_className; + + AddRef(this); + + return *this; + } + + Exception(const char *path, + const char *function, + int line, + const std::string &message) : + m_reason(NULL), + m_path(path), + m_function(function), + m_line(line), + m_message(message) + { + AddRef(this); + } + + Exception(const char *path, + const char *function, + int line, + const Exception &reason, + const std::string &message) : + m_reason(new Exception(reason)), + m_path(path), + m_function(function), + m_line(line), + m_message(message) + { + AddRef(this); + } + + virtual ~Exception() throw() + { + if (m_reason != NULL) { + delete m_reason; + m_reason = NULL; + } + + UnRef(this); + } + + void Dump() const + { + // Show reason first + if (m_reason != NULL) { + m_reason->Dump(); + } + + // Afterward, dump exception + const char *file = strchr(m_path.c_str(), '/'); + + if (file == NULL) { + file = m_path.c_str(); + } else { + ++file; + } + + printf("\033[0;36m[%s:%i]\033[m %s() \033[4;35m%s\033[m: %s\033[m\n", + file, m_line, + m_function.c_str(), + m_className.c_str(), + m_message.empty() ? "" : m_message.c_str()); + } + + std::string DumpToString() const + { + std::string ret; + if (m_reason != NULL) { + ret = m_reason->DumpToString(); + } + + const char *file = strchr(m_path.c_str(), '/'); + + if (file == NULL) { + file = m_path.c_str(); + } else { + ++file; + } + + char buf[1024]; + snprintf(buf, + sizeof(buf), + "\033[0;36m[%s:%i]\033[m %s() \033[4;35m%s\033[m: %s\033[m\n", + file, + m_line, + m_function.c_str(), + m_className.c_str(), + m_message.empty() ? "" : m_message.c_str()); + + buf[sizeof(buf) - 1] = '\n'; + ret += buf; + + return ret; + } + + Exception *GetReason() const + { + return m_reason; + } + + std::string GetPath() const + { + return m_path; + } + + std::string GetFunction() const + { + return m_function; + } + + int GetLine() const + { + return m_line; + } + + std::string GetMessage() const + { + return m_message; + } + + std::string GetClassName() const + { + return m_className; + } +}; +} // namespace CCHECKER + +#define Try try + +#define Throw(ClassName) \ + throw ClassName(__FILE__, __FUNCTION__, __LINE__) + +#define ThrowMsg(ClassName, Message) \ + do \ + { \ + std::ostringstream dplLoggingStream; \ + dplLoggingStream << Message; \ + throw ClassName(__FILE__, __FUNCTION__, __LINE__, dplLoggingStream.str()); \ + } while (0) + +#define ReThrow(ClassName) \ + throw ClassName(__FILE__, __FUNCTION__, __LINE__, _rethrown_exception) + +#define ReThrowMsg(ClassName, Message) \ + throw ClassName(__FILE__, \ + __FUNCTION__, \ + __LINE__, \ + _rethrown_exception, \ + Message) + +#define Catch(ClassName) \ + catch (const ClassName &_rethrown_exception) + +#define DECLARE_EXCEPTION_TYPE(BaseClass, Class) \ + class Class : \ + public BaseClass \ + { \ + public: \ + Class(const char *path, \ + const char *function, \ + int line, \ + const std::string & message = std::string()) : \ + BaseClass(path, function, line, message) \ + { \ + BaseClass::m_className = #Class; \ + } \ + \ + Class(const char *path, \ + const char *function, \ + int line, \ + const CCHECKER::Exception & reason, \ + const std::string & message = std::string()) : \ + BaseClass(path, function, line, reason, message) \ + { \ + BaseClass::m_className = #Class; \ + } \ + }; + +#define UNHANDLED_EXCEPTION_HANDLER_BEGIN try + +#define UNHANDLED_EXCEPTION_HANDLER_END \ + catch (const CCHECKER::Exception &exception) \ + { \ + std::ostringstream msg; \ + msg << CCHECKER::Exception::KnownExceptionToString(exception); \ + CCHECKER::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \ + abort(); \ + } \ + catch (std::exception& e) \ + { \ + std::ostringstream msg; \ + msg << e.what(); \ + msg << "\n"; \ + msg << CCHECKER::Exception::UnknownExceptionToString(); \ + CCHECKER::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \ + abort(); \ + } \ + catch (...) \ + { \ + std::ostringstream msg; \ + msg << CCHECKER::Exception::UnknownExceptionToString(); \ + CCHECKER::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \ + abort(); \ + } + +namespace CCHECKER { +namespace CommonException { +/** + * Internal exception definitions + * + * These should normally not happen. + * Usually, exception trace with internal error includes + * important messages. + */ +DECLARE_EXCEPTION_TYPE(Exception, InternalError) ///< Unexpected error from + // underlying libraries or + // kernel +} +} + +#endif // CCHECKER_EXCEPTION_H diff --git a/src/dpl/core/include/cchecker/dpl/noncopyable.h b/src/dpl/core/include/cchecker/dpl/noncopyable.h new file mode 100644 index 0000000..747299c --- /dev/null +++ b/src/dpl/core/include/cchecker/dpl/noncopyable.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file noncopyable + * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) + * @version 1.0 + * @brief This file is the implementation file of noncopyable + */ +#ifndef CCHECKER_NONCOPYABLE_H +#define CCHECKER_NONCOPYABLE_H + +namespace CCHECKER { +class Noncopyable +{ + private: + Noncopyable(const Noncopyable &); + const Noncopyable &operator=(const Noncopyable &); + + public: + Noncopyable(); + virtual ~Noncopyable(); +}; +} // namespace CCHECKER + +#endif // CCHECKER_NONCOPYABLE_H diff --git a/src/dpl/core/include/cchecker/dpl/noreturn.h b/src/dpl/core/include/cchecker/dpl/noreturn.h new file mode 100644 index 0000000..6eaaa64 --- /dev/null +++ b/src/dpl/core/include/cchecker/dpl/noreturn.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file noreturn.h + * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) + * @version 1.0 + * @brief This file is the implementation file of noreturn + */ +#ifndef CCHECKER_NORETURN_H +#define CCHECKER_NORETURN_H + +#define CCHECKER_NORETURN __attribute__((__noreturn__)) + +#endif // CCHECKER_NORETURN_H diff --git a/src/dpl/core/include/cchecker/dpl/optional.h b/src/dpl/core/include/cchecker/dpl/optional.h new file mode 100644 index 0000000..42d1bd4 --- /dev/null +++ b/src/dpl/core/include/cchecker/dpl/optional.h @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file optional_value.h + * @author Lukasz Wrzosek (l.wrzosek@samsung.com) + * @version 1.0 + */ + +#ifndef CCHECKER_OPTIONAL_H +#define CCHECKER_OPTIONAL_H + +#include + +namespace CCHECKER { +template +class Optional +{ + class Exception + { + public: + DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, Base) + DECLARE_EXCEPTION_TYPE(Base, NullReference) + }; + + public: + Optional() : + m_null(true), + m_value() + {} + + Optional(const Type& t) : + m_null(false), + m_value(t) + {} + + bool IsNull() const + { + return m_null; + } + + Type& operator*() + { + if (m_null) { + Throw(typename Exception::NullReference); + } + return m_value; + } + + const Type& operator*() const + { + if (m_null) { + Throw(typename Exception::NullReference); + } + return m_value; + } + + const Type* operator->() const + { + if (m_null) { + Throw(typename Exception::NullReference); + } + return &m_value; + } + + Type* operator->() + { + if (m_null) { + Throw(typename Exception::NullReference); + } + return &m_value; + } + + bool operator!() const + { + return m_null; + } + + Optional& operator=(const Type& other) + { + m_null = false; + m_value = other; + return *this; + } + + bool operator==(const Optional& aSecond) const + { + return LogicalOperator(*this, aSecond, + std::equal_to(), std::equal_to()); + } + + bool operator==(const Type& aSecond) const + { + return Optional(aSecond) == *this; + } + + bool operator!=(const Optional& aSecond) const + { + return !(*this == aSecond); + } + + bool operator<(const Optional& aSecond) const + { + return LogicalOperator(*this, aSecond, + std::less(), std::less()); + } + + bool operator>(const Optional& aSecond) const + { + return LogicalOperator(*this, aSecond, + std::greater(), std::greater()); + } + + bool operator<=(const Optional& aSecond) const + { + return *this == aSecond || *this < aSecond; + } + + bool operator>=(const Optional& aSecond) const + { + return *this == aSecond || *this > aSecond; + } + + static Optional Null; + + private: + bool m_null; + Type m_value; + + template + static bool LogicalOperator(const Optional& aFirst, + const Optional& aSecond, + taComparator aComparator, + taNullComparator aNullComparator) + { + if (aFirst.m_null == aSecond.m_null) { + if (aFirst.m_null) { + return taEquality; + } else { + return aComparator(aFirst.m_value, aSecond.m_value); + } + } else { + return aNullComparator(aFirst.m_null, aSecond.m_null); + } + } +}; + +template +Optional Optional::Null = Optional(); +} //namespace CCHECKER + +template +std::ostream& operator<<(std::ostream& aStream, + const CCHECKER::Optional& aOptional) +{ + if (aOptional.IsNull()) { + return aStream << "null optional"; + } else { + return aStream << *aOptional; + } +} + +#endif // CCHECKER_OPTIONAL_VALUE_H diff --git a/src/dpl/core/include/cchecker/dpl/scoped_array.h b/src/dpl/core/include/cchecker/dpl/scoped_array.h new file mode 100644 index 0000000..54c0c80 --- /dev/null +++ b/src/dpl/core/include/cchecker/dpl/scoped_array.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/*! + * @file scoped_ptr.h + * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) + * @version 1.0 + * @brief This file is the implementation file of scoped array RAII + */ +#ifndef CCHECKER_SCOPED_ARRAY_H +#define CCHECKER_SCOPED_ARRAY_H + +#include + +#include +#include + +namespace CCHECKER { +template +struct ScopedArrayPolicy +{ + typedef Class* Type; + static Type NullValue() + { + return NULL; + } + static void Destroy(Type ptr) + { + delete[] ptr; + } +}; + +template +class ScopedArray : public ScopedResource > +{ + typedef ScopedArrayPolicy Policy; + typedef ScopedResource BaseType; + + public: + explicit ScopedArray(Class *ptr = Policy::NullValue()) : BaseType(ptr) { } + + Class &operator [](std::ptrdiff_t k) const + { + Assert(this->m_value != Policy::NullValue() && + "Dereference of scoped NULL array!"); + Assert(k >= 0 && "Negative array index"); + + return this->m_value[k]; + } +}; +} // namespace CCHECKER + +#endif // CCHECKER_SCOPED_PTR_H diff --git a/src/dpl/core/include/cchecker/dpl/scoped_free.h b/src/dpl/core/include/cchecker/dpl/scoped_free.h new file mode 100644 index 0000000..d8fee50 --- /dev/null +++ b/src/dpl/core/include/cchecker/dpl/scoped_free.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/*! + * @file scoped_free.h + * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) + * @version 1.0 + * @brief This file is the implementation file of scoped free RAII + */ + +#ifndef CCHECKER_SCOPED_FREE_H +#define CCHECKER_SCOPED_FREE_H + +#include +#include + +#include + +namespace CCHECKER { +template +struct ScopedFreePolicy +{ + typedef Class* Type; + static Type NullValue() + { + return NULL; + } + static void Destroy(Type ptr) + { + free(ptr); + } +}; + +template +class ScopedFree : public ScopedResource > +{ + typedef ScopedFreePolicy Policy; + typedef ScopedResource BaseType; + + public: + explicit ScopedFree(Memory *ptr = Policy::NullValue()) : BaseType(ptr) { } +}; +} // namespace CCHECKER + +#endif // CCHECKER_SCOPED_FREE_H diff --git a/src/dpl/core/include/cchecker/dpl/scoped_resource.h b/src/dpl/core/include/cchecker/dpl/scoped_resource.h new file mode 100644 index 0000000..2b3c72d --- /dev/null +++ b/src/dpl/core/include/cchecker/dpl/scoped_resource.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file scoped_resource.h + * @author Piotr Marcinkiewicz (p.marcinkiew@samsung.com) + * @version 1.0 + * @brief This file is the implementation file of scoped resource pattern + */ +#ifndef CCHECKER_SCOPED_RESOURCE_H +#define CCHECKER_SCOPED_RESOURCE_H + +#include + +namespace CCHECKER { +template +class ScopedResource : + private Noncopyable +{ + public: + typedef typename ClassPolicy::Type ValueType; + typedef ScopedResource ThisType; + + protected: + ValueType m_value; + + public: + explicit ScopedResource(ValueType value) : m_value(value) { } + + ~ScopedResource() + { + ClassPolicy::Destroy(m_value); + } + + ValueType Get() const + { + return m_value; + } + + void Reset(ValueType value = ClassPolicy::NullValue()) + { + ClassPolicy::Destroy(m_value); + m_value = value; + } + + ValueType Release() + { + ValueType value = m_value; + m_value = ClassPolicy::NullValue(); + return value; + } + typedef ValueType ThisType::*UnknownBoolType; + + operator UnknownBoolType() const + { + return m_value == ClassPolicy::NullValue() ? + 0 : //0 is valid here because it converts to false + &ThisType::m_value; //it converts to true + } + + bool operator !() const + { + return m_value == ClassPolicy::NullValue(); + } +}; +} // namespace CCHECKER + +#endif // CCHECKER_SCOPED_RESOURCE_H diff --git a/src/dpl/core/include/cchecker/dpl/string.h b/src/dpl/core/include/cchecker/dpl/string.h new file mode 100644 index 0000000..d9c3bbd --- /dev/null +++ b/src/dpl/core/include/cchecker/dpl/string.h @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file string.h + * @author Piotr Marcinkiewicz (p.marcinkiew@samsung.com) + * @version 1.0 + */ +#ifndef CCHECKER_STRING +#define CCHECKER_STRING + +#include +#include +#include + +#include +#include + +namespace CCHECKER { +// @brief CCHECKER string +typedef std::basic_string String; + +// @brief String exception class +class StringException +{ + public: + DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, Base) + + // @brief Invalid init for UTF8 to UTF32 converter + DECLARE_EXCEPTION_TYPE(Base, IconvInitErrorUTF8ToUTF32) + + // @brief Invalid taStdContainerinit for UTF32 to UTF32 converter + DECLARE_EXCEPTION_TYPE(Base, IconvInitErrorUTF32ToUTF8) + + // @brief Invalid conversion for UTF8 to UTF32 converter + DECLARE_EXCEPTION_TYPE(Base, IconvConvertErrorUTF8ToUTF32) + + // @brief Invalid conversion for UTF8 to UTF32 converter + DECLARE_EXCEPTION_TYPE(Base, IconvConvertErrorUTF32ToUTF8) + + // @brief Invalid ASCII character detected in FromASCII + DECLARE_EXCEPTION_TYPE(Base, InvalidASCIICharacter) + + // @brief Invalid ASCII character detected in FromASCII + DECLARE_EXCEPTION_TYPE(Base, ICUInvalidCharacterFound) +}; + +//!\brief convert ASCII string to CCHECKER::String +String FromASCIIString(const std::string& aString); + +//!\brief convert UTF32 string to CCHECKER::String +String FromUTF32String(const std::wstring& aString); + +//@brief Returns String object created from UTF8 string +//@param[in] aString input UTF-8 string +String FromUTF8String(const std::string& aString); + +//@brief Returns String content as std::string +std::string ToUTF8String(const String& aString); + +//@brief Compare two unicode strings +int StringCompare(const String &left, + const String &right, + bool caseInsensitive = false); + +//@brief Splits the string into substrings. +//@param[in] str Input string +//@param[in] delimiters array or string containing a sequence of substring +// delimiters. Can be also a single delimiter character. +//@param[in] it InserterIterator that is used to save the generated substrings. +template +void Tokenize(const StringType& str, + const Delimiters& delimiters, + InserterIterator it, + bool ignoreEmpty = false) +{ + typename StringType::size_type nextSearchStart = 0; + typename StringType::size_type pos; + typename StringType::size_type length; + + while (true) { + pos = str.find_first_of(delimiters, nextSearchStart); + length = + ((pos == StringType::npos) ? str.length() : pos) - nextSearchStart; + + if (!ignoreEmpty || length > 0) { + *it = str.substr(nextSearchStart, length); + it++; + } + + if (pos == StringType::npos) { + return; + } + + nextSearchStart = pos + 1; + } +} + +namespace Utils { + +template class ConcatFunc : public std::binary_function +{ +public: + explicit ConcatFunc(const T & val) : m_delim(val) {} + T operator()(const T & arg1, const T & arg2) const + { + return arg1 + m_delim + arg2; + } +private: + T m_delim; +}; + +} + +template +typename ForwardIterator::value_type Join(ForwardIterator begin, ForwardIterator end, typename ForwardIterator::value_type delim) +{ + typedef typename ForwardIterator::value_type value; + if(begin == end) return value(); + Utils::ConcatFunc func(delim); + ForwardIterator init = begin; + return std::accumulate(++begin, end, *init, func); +} + +} //namespace CCHECKER + +std::ostream& operator<<(std::ostream& aStream, const CCHECKER::String& aString); + +#endif // CCHECKER_STRING diff --git a/src/dpl/core/include/dpl/assert.h b/src/dpl/core/include/dpl/assert.h deleted file mode 100644 index 936c71e..0000000 --- a/src/dpl/core/include/dpl/assert.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * @file assert.h - * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) - * @version 1.0 - * @brief This file is the implementation file of assert - */ -#ifndef CCHECKER_ASSERT_H -#define CCHECKER_ASSERT_H - -#include - -namespace CCHECKER { -// Assertion handler procedure -// Do not call directly -// Always use Assert macro -CCHECKER_NORETURN void AssertProc(const char *condition, - const char *file, - int line, - const char *function); -} // namespace CCHECKER - -#define Assert(Condition) do { if (!(Condition)) { CCHECKER::AssertProc(#Condition, \ - __FILE__, \ - __LINE__, \ - __FUNCTION__); \ - } } while (0) - -#endif // CCHECKER_ASSERT_H diff --git a/src/dpl/core/include/dpl/availability.h b/src/dpl/core/include/dpl/availability.h deleted file mode 100644 index b1cb894..0000000 --- a/src/dpl/core/include/dpl/availability.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * @file availability.h - * @author Jihoon Chung (jihoon.chung@samsung.com) - * @version 1.0 - */ -#ifndef CCHECKER_AVAILABILITY_H -#define CCHECKER_AVAILABILITY_H - -#define CCHECKER_DEPRECATED __attribute__((deprecated)) -#define CCHECKER_DEPRECATED_WITH_MESSAGE(msg) __attribute__((deprecated(msg))) - -#define CCHECKER_UNUSED __attribute__((unused)) -#define CCHECKER_UNUSED_PARAM(variable) (void)variable - -#endif // CCHECKER_AVAILABILITY_H diff --git a/src/dpl/core/include/dpl/char_traits.h b/src/dpl/core/include/dpl/char_traits.h deleted file mode 100644 index e76f48b..0000000 --- a/src/dpl/core/include/dpl/char_traits.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * @file char_traits.h - * @author Piotr Marcinkiewicz (p.marcinkiew@samsung.com) - * @version 1.0 - * @brief Char traits are used to create basic_string extended with - * additional features - * Current char traits could be extended in feature to boost - * performance - */ -#ifndef CCHECKER_CHAR_TRAITS -#define CCHECKER_CHAR_TRAITS - -#include -#include -#include -#include -#include - -namespace CCHECKER { -typedef std::char_traits CharTraits; -} // namespace CCHECKER - -#endif // CCHECKER_CHAR_TRAITS diff --git a/src/dpl/core/include/dpl/colors.h b/src/dpl/core/include/dpl/colors.h deleted file mode 100644 index d652f5b..0000000 --- a/src/dpl/core/include/dpl/colors.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * @file colors.h - * @author Lukasz Wrzosek (l.wrzosek@samsung.com) - * @version 1.0 - * @brief Some constants with definition of colors for Console - * and html output - */ - -#ifndef CCHECKER_COLORS_H -#define CCHECKER_COLORS_H - -namespace CCHECKER { -namespace Colors { -namespace Text { -extern const char* BOLD_GREEN_BEGIN; -extern const char* BOLD_GREEN_END; -extern const char* PURPLE_BEGIN; -extern const char* PURPLE_END; -extern const char* RED_BEGIN; -extern const char* RED_END; -extern const char* GREEN_BEGIN; -extern const char* GREEN_END; -extern const char* CYAN_BEGIN; -extern const char* CYAN_END; -extern const char* BOLD_RED_BEGIN; -extern const char* BOLD_RED_END; -extern const char* BOLD_YELLOW_BEGIN; -extern const char* BOLD_YELLOW_END; -extern const char* BOLD_GOLD_BEGIN; -extern const char* BOLD_GOLD_END; -extern const char* BOLD_WHITE_BEGIN; -extern const char* BOLD_WHITE_END; -extern const char* COLOR_END; -} //namespace Text - -namespace Html { -extern const char* BOLD_GREEN_BEGIN; -extern const char* BOLD_GREEN_END; -extern const char* PURPLE_BEGIN; -extern const char* PURPLE_END; -extern const char* RED_BEGIN; -extern const char* RED_END; -extern const char* GREEN_BEGIN; -extern const char* GREEN_END; -extern const char* CYAN_BEGIN; -extern const char* CYAN_END; -extern const char* BOLD_RED_BEGIN; -extern const char* BOLD_RED_END; -extern const char* BOLD_YELLOW_BEGIN; -extern const char* BOLD_YELLOW_END; -extern const char* BOLD_GOLD_BEGIN; -extern const char* BOLD_GOLD_END; -extern const char* BOLD_WHITE_BEGIN; -extern const char* BOLD_WHITE_END; -} //namespace Html -} //namespace Colors -} //namespace CCHECKER - -#endif /* CCHECKER_COLORS_H */ diff --git a/src/dpl/core/include/dpl/errno_string.h b/src/dpl/core/include/dpl/errno_string.h deleted file mode 100644 index 498b2af..0000000 --- a/src/dpl/core/include/dpl/errno_string.h +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * @file errno_string.h - * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) - * @version 1.0 - * @brief This file is the implementation file of errno string - */ -#ifndef CCHECKER_ERRNO_STRING_H -#define CCHECKER_ERRNO_STRING_H - -#include -#include -#include - -namespace CCHECKER { -DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, InvalidErrnoValue) - -std::string GetErrnoString(int error = errno); -} // namespace CCHECKER - -#endif // CCHECKER_ERRNO_STRING_H diff --git a/src/dpl/core/include/dpl/exception.h b/src/dpl/core/include/dpl/exception.h deleted file mode 100644 index f7b9afc..0000000 --- a/src/dpl/core/include/dpl/exception.h +++ /dev/null @@ -1,385 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * @file exception.h - * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) - * @version 1.0 - * @brief Header file for base exception - */ -#ifndef CCHECKER_EXCEPTION_H -#define CCHECKER_EXCEPTION_H - -#include -#include -#include -#include -#include -#include - -namespace CCHECKER { -void LogUnhandledException(const std::string &str); -void LogUnhandledException(const std::string &str, - const char *filename, - int line, - const char *function); -} - -namespace CCHECKER { -class Exception -{ - private: - static unsigned int m_exceptionCount; - static Exception* m_lastException; - static void (*m_terminateHandler)(); - - static void AddRef(Exception* exception) - { - if (!m_exceptionCount) { - m_terminateHandler = std::set_terminate(&TerminateHandler); - } - - ++m_exceptionCount; - m_lastException = exception; - } - - static void UnRef(Exception* e) - { - if (m_lastException == e) { - m_lastException = NULL; - } - - --m_exceptionCount; - - if (!m_exceptionCount) { - std::set_terminate(m_terminateHandler); - m_terminateHandler = NULL; - } - } - - static void TerminateHandler() - { - if (m_lastException != NULL) { - DisplayKnownException(*m_lastException); - abort(); - } else { - DisplayUnknownException(); - abort(); - } - } - - Exception *m_reason; - std::string m_path; - std::string m_function; - int m_line; - - protected: - std::string m_message; - std::string m_className; - - public: - static std::string KnownExceptionToString(const Exception &e) - { - std::ostringstream message; - message << - "\033[1;5;31m\n=== Unhandled CCHECKER exception occurred ===\033[m\n\n"; - message << "\033[1;33mException trace:\033[m\n\n"; - message << e.DumpToString(); - message << "\033[1;31m\n=== Will now abort ===\033[m\n"; - - return message.str(); - } - - static std::string UnknownExceptionToString() - { - std::ostringstream message; - message << - "\033[1;5;31m\n=== Unhandled non-CCHECKER exception occurred ===\033[m\n\n"; - message << "\033[1;31m\n=== Will now abort ===\033[m\n"; - - return message.str(); - } - - static void DisplayKnownException(const Exception& e) - { - LogUnhandledException(KnownExceptionToString(e).c_str()); - } - - static void DisplayUnknownException() - { - LogUnhandledException(UnknownExceptionToString().c_str()); - } - - Exception(const Exception &other) - { - // Deep copy - if (other.m_reason != NULL) { - m_reason = new Exception(*other.m_reason); - } else { - m_reason = NULL; - } - - m_message = other.m_message; - m_path = other.m_path; - m_function = other.m_function; - m_line = other.m_line; - - m_className = other.m_className; - - AddRef(this); - } - - const Exception &operator =(const Exception &other) - { - if (this == &other) { - return *this; - } - - // Deep copy - if (other.m_reason != NULL) { - m_reason = new Exception(*other.m_reason); - } else { - m_reason = NULL; - } - - m_message = other.m_message; - m_path = other.m_path; - m_function = other.m_function; - m_line = other.m_line; - - m_className = other.m_className; - - AddRef(this); - - return *this; - } - - Exception(const char *path, - const char *function, - int line, - const std::string &message) : - m_reason(NULL), - m_path(path), - m_function(function), - m_line(line), - m_message(message) - { - AddRef(this); - } - - Exception(const char *path, - const char *function, - int line, - const Exception &reason, - const std::string &message) : - m_reason(new Exception(reason)), - m_path(path), - m_function(function), - m_line(line), - m_message(message) - { - AddRef(this); - } - - virtual ~Exception() throw() - { - if (m_reason != NULL) { - delete m_reason; - m_reason = NULL; - } - - UnRef(this); - } - - void Dump() const - { - // Show reason first - if (m_reason != NULL) { - m_reason->Dump(); - } - - // Afterward, dump exception - const char *file = strchr(m_path.c_str(), '/'); - - if (file == NULL) { - file = m_path.c_str(); - } else { - ++file; - } - - printf("\033[0;36m[%s:%i]\033[m %s() \033[4;35m%s\033[m: %s\033[m\n", - file, m_line, - m_function.c_str(), - m_className.c_str(), - m_message.empty() ? "" : m_message.c_str()); - } - - std::string DumpToString() const - { - std::string ret; - if (m_reason != NULL) { - ret = m_reason->DumpToString(); - } - - const char *file = strchr(m_path.c_str(), '/'); - - if (file == NULL) { - file = m_path.c_str(); - } else { - ++file; - } - - char buf[1024]; - snprintf(buf, - sizeof(buf), - "\033[0;36m[%s:%i]\033[m %s() \033[4;35m%s\033[m: %s\033[m\n", - file, - m_line, - m_function.c_str(), - m_className.c_str(), - m_message.empty() ? "" : m_message.c_str()); - - buf[sizeof(buf) - 1] = '\n'; - ret += buf; - - return ret; - } - - Exception *GetReason() const - { - return m_reason; - } - - std::string GetPath() const - { - return m_path; - } - - std::string GetFunction() const - { - return m_function; - } - - int GetLine() const - { - return m_line; - } - - std::string GetMessage() const - { - return m_message; - } - - std::string GetClassName() const - { - return m_className; - } -}; -} // namespace CCHECKER - -#define Try try - -#define Throw(ClassName) \ - throw ClassName(__FILE__, __FUNCTION__, __LINE__) - -#define ThrowMsg(ClassName, Message) \ - do \ - { \ - std::ostringstream dplLoggingStream; \ - dplLoggingStream << Message; \ - throw ClassName(__FILE__, __FUNCTION__, __LINE__, dplLoggingStream.str()); \ - } while (0) - -#define ReThrow(ClassName) \ - throw ClassName(__FILE__, __FUNCTION__, __LINE__, _rethrown_exception) - -#define ReThrowMsg(ClassName, Message) \ - throw ClassName(__FILE__, \ - __FUNCTION__, \ - __LINE__, \ - _rethrown_exception, \ - Message) - -#define Catch(ClassName) \ - catch (const ClassName &_rethrown_exception) - -#define DECLARE_EXCEPTION_TYPE(BaseClass, Class) \ - class Class : \ - public BaseClass \ - { \ - public: \ - Class(const char *path, \ - const char *function, \ - int line, \ - const std::string & message = std::string()) : \ - BaseClass(path, function, line, message) \ - { \ - BaseClass::m_className = #Class; \ - } \ - \ - Class(const char *path, \ - const char *function, \ - int line, \ - const CCHECKER::Exception & reason, \ - const std::string & message = std::string()) : \ - BaseClass(path, function, line, reason, message) \ - { \ - BaseClass::m_className = #Class; \ - } \ - }; - -#define UNHANDLED_EXCEPTION_HANDLER_BEGIN try - -#define UNHANDLED_EXCEPTION_HANDLER_END \ - catch (const CCHECKER::Exception &exception) \ - { \ - std::ostringstream msg; \ - msg << CCHECKER::Exception::KnownExceptionToString(exception); \ - CCHECKER::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \ - abort(); \ - } \ - catch (std::exception& e) \ - { \ - std::ostringstream msg; \ - msg << e.what(); \ - msg << "\n"; \ - msg << CCHECKER::Exception::UnknownExceptionToString(); \ - CCHECKER::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \ - abort(); \ - } \ - catch (...) \ - { \ - std::ostringstream msg; \ - msg << CCHECKER::Exception::UnknownExceptionToString(); \ - CCHECKER::LogUnhandledException(msg.str(), __FILE__, __LINE__, __FUNCTION__); \ - abort(); \ - } - -namespace CCHECKER { -namespace CommonException { -/** - * Internal exception definitions - * - * These should normally not happen. - * Usually, exception trace with internal error includes - * important messages. - */ -DECLARE_EXCEPTION_TYPE(Exception, InternalError) ///< Unexpected error from - // underlying libraries or - // kernel -} -} - -#endif // CCHECKER_EXCEPTION_H diff --git a/src/dpl/core/include/dpl/noncopyable.h b/src/dpl/core/include/dpl/noncopyable.h deleted file mode 100644 index 747299c..0000000 --- a/src/dpl/core/include/dpl/noncopyable.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * @file noncopyable - * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) - * @version 1.0 - * @brief This file is the implementation file of noncopyable - */ -#ifndef CCHECKER_NONCOPYABLE_H -#define CCHECKER_NONCOPYABLE_H - -namespace CCHECKER { -class Noncopyable -{ - private: - Noncopyable(const Noncopyable &); - const Noncopyable &operator=(const Noncopyable &); - - public: - Noncopyable(); - virtual ~Noncopyable(); -}; -} // namespace CCHECKER - -#endif // CCHECKER_NONCOPYABLE_H diff --git a/src/dpl/core/include/dpl/noreturn.h b/src/dpl/core/include/dpl/noreturn.h deleted file mode 100644 index 6eaaa64..0000000 --- a/src/dpl/core/include/dpl/noreturn.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * @file noreturn.h - * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) - * @version 1.0 - * @brief This file is the implementation file of noreturn - */ -#ifndef CCHECKER_NORETURN_H -#define CCHECKER_NORETURN_H - -#define CCHECKER_NORETURN __attribute__((__noreturn__)) - -#endif // CCHECKER_NORETURN_H diff --git a/src/dpl/core/include/dpl/optional.h b/src/dpl/core/include/dpl/optional.h deleted file mode 100644 index 6aca7bd..0000000 --- a/src/dpl/core/include/dpl/optional.h +++ /dev/null @@ -1,175 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * @file optional_value.h - * @author Lukasz Wrzosek (l.wrzosek@samsung.com) - * @version 1.0 - */ - -#ifndef CCHECKER_OPTIONAL_H -#define CCHECKER_OPTIONAL_H - -#include - -namespace CCHECKER { -template -class Optional -{ - class Exception - { - public: - DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, Base) - DECLARE_EXCEPTION_TYPE(Base, NullReference) - }; - - public: - Optional() : - m_null(true), - m_value() - {} - - Optional(const Type& t) : - m_null(false), - m_value(t) - {} - - bool IsNull() const - { - return m_null; - } - - Type& operator*() - { - if (m_null) { - Throw(typename Exception::NullReference); - } - return m_value; - } - - const Type& operator*() const - { - if (m_null) { - Throw(typename Exception::NullReference); - } - return m_value; - } - - const Type* operator->() const - { - if (m_null) { - Throw(typename Exception::NullReference); - } - return &m_value; - } - - Type* operator->() - { - if (m_null) { - Throw(typename Exception::NullReference); - } - return &m_value; - } - - bool operator!() const - { - return m_null; - } - - Optional& operator=(const Type& other) - { - m_null = false; - m_value = other; - return *this; - } - - bool operator==(const Optional& aSecond) const - { - return LogicalOperator(*this, aSecond, - std::equal_to(), std::equal_to()); - } - - bool operator==(const Type& aSecond) const - { - return Optional(aSecond) == *this; - } - - bool operator!=(const Optional& aSecond) const - { - return !(*this == aSecond); - } - - bool operator<(const Optional& aSecond) const - { - return LogicalOperator(*this, aSecond, - std::less(), std::less()); - } - - bool operator>(const Optional& aSecond) const - { - return LogicalOperator(*this, aSecond, - std::greater(), std::greater()); - } - - bool operator<=(const Optional& aSecond) const - { - return *this == aSecond || *this < aSecond; - } - - bool operator>=(const Optional& aSecond) const - { - return *this == aSecond || *this > aSecond; - } - - static Optional Null; - - private: - bool m_null; - Type m_value; - - template - static bool LogicalOperator(const Optional& aFirst, - const Optional& aSecond, - taComparator aComparator, - taNullComparator aNullComparator) - { - if (aFirst.m_null == aSecond.m_null) { - if (aFirst.m_null) { - return taEquality; - } else { - return aComparator(aFirst.m_value, aSecond.m_value); - } - } else { - return aNullComparator(aFirst.m_null, aSecond.m_null); - } - } -}; - -template -Optional Optional::Null = Optional(); -} //namespace CCHECKER - -template -std::ostream& operator<<(std::ostream& aStream, - const CCHECKER::Optional& aOptional) -{ - if (aOptional.IsNull()) { - return aStream << "null optional"; - } else { - return aStream << *aOptional; - } -} - -#endif // CCHECKER_OPTIONAL_VALUE_H diff --git a/src/dpl/core/include/dpl/scoped_array.h b/src/dpl/core/include/dpl/scoped_array.h deleted file mode 100644 index ca6a02b..0000000 --- a/src/dpl/core/include/dpl/scoped_array.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/*! - * @file scoped_ptr.h - * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) - * @version 1.0 - * @brief This file is the implementation file of scoped array RAII - */ -#ifndef CCHECKER_SCOPED_ARRAY_H -#define CCHECKER_SCOPED_ARRAY_H - -#include - -#include -#include - -namespace CCHECKER { -template -struct ScopedArrayPolicy -{ - typedef Class* Type; - static Type NullValue() - { - return NULL; - } - static void Destroy(Type ptr) - { - delete[] ptr; - } -}; - -template -class ScopedArray : public ScopedResource > -{ - typedef ScopedArrayPolicy Policy; - typedef ScopedResource BaseType; - - public: - explicit ScopedArray(Class *ptr = Policy::NullValue()) : BaseType(ptr) { } - - Class &operator [](std::ptrdiff_t k) const - { - Assert(this->m_value != Policy::NullValue() && - "Dereference of scoped NULL array!"); - Assert(k >= 0 && "Negative array index"); - - return this->m_value[k]; - } -}; -} // namespace CCHECKER - -#endif // CCHECKER_SCOPED_PTR_H diff --git a/src/dpl/core/include/dpl/scoped_free.h b/src/dpl/core/include/dpl/scoped_free.h deleted file mode 100644 index 9be17b7..0000000 --- a/src/dpl/core/include/dpl/scoped_free.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/*! - * @file scoped_free.h - * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) - * @version 1.0 - * @brief This file is the implementation file of scoped free RAII - */ - -#ifndef CCHECKER_SCOPED_FREE_H -#define CCHECKER_SCOPED_FREE_H - -#include -#include - -#include - -namespace CCHECKER { -template -struct ScopedFreePolicy -{ - typedef Class* Type; - static Type NullValue() - { - return NULL; - } - static void Destroy(Type ptr) - { - free(ptr); - } -}; - -template -class ScopedFree : public ScopedResource > -{ - typedef ScopedFreePolicy Policy; - typedef ScopedResource BaseType; - - public: - explicit ScopedFree(Memory *ptr = Policy::NullValue()) : BaseType(ptr) { } -}; -} // namespace CCHECKER - -#endif // CCHECKER_SCOPED_FREE_H diff --git a/src/dpl/core/include/dpl/scoped_resource.h b/src/dpl/core/include/dpl/scoped_resource.h deleted file mode 100644 index c024684..0000000 --- a/src/dpl/core/include/dpl/scoped_resource.h +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * @file scoped_resource.h - * @author Piotr Marcinkiewicz (p.marcinkiew@samsung.com) - * @version 1.0 - * @brief This file is the implementation file of scoped resource pattern - */ -#ifndef CCHECKER_SCOPED_RESOURCE_H -#define CCHECKER_SCOPED_RESOURCE_H - -#include - -namespace CCHECKER { -template -class ScopedResource : - private Noncopyable -{ - public: - typedef typename ClassPolicy::Type ValueType; - typedef ScopedResource ThisType; - - protected: - ValueType m_value; - - public: - explicit ScopedResource(ValueType value) : m_value(value) { } - - ~ScopedResource() - { - ClassPolicy::Destroy(m_value); - } - - ValueType Get() const - { - return m_value; - } - - void Reset(ValueType value = ClassPolicy::NullValue()) - { - ClassPolicy::Destroy(m_value); - m_value = value; - } - - ValueType Release() - { - ValueType value = m_value; - m_value = ClassPolicy::NullValue(); - return value; - } - typedef ValueType ThisType::*UnknownBoolType; - - operator UnknownBoolType() const - { - return m_value == ClassPolicy::NullValue() ? - 0 : //0 is valid here because it converts to false - &ThisType::m_value; //it converts to true - } - - bool operator !() const - { - return m_value == ClassPolicy::NullValue(); - } -}; -} // namespace CCHECKER - -#endif // CCHECKER_SCOPED_RESOURCE_H diff --git a/src/dpl/core/include/dpl/string.h b/src/dpl/core/include/dpl/string.h deleted file mode 100644 index a271e5a..0000000 --- a/src/dpl/core/include/dpl/string.h +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * @file string.h - * @author Piotr Marcinkiewicz (p.marcinkiew@samsung.com) - * @version 1.0 - */ -#ifndef CCHECKER_STRING -#define CCHECKER_STRING - -#include -#include -#include -#include -#include - -namespace CCHECKER { -// @brief CCHECKER string -typedef std::basic_string String; - -// @brief String exception class -class StringException -{ - public: - DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, Base) - - // @brief Invalid init for UTF8 to UTF32 converter - DECLARE_EXCEPTION_TYPE(Base, IconvInitErrorUTF8ToUTF32) - - // @brief Invalid taStdContainerinit for UTF32 to UTF32 converter - DECLARE_EXCEPTION_TYPE(Base, IconvInitErrorUTF32ToUTF8) - - // @brief Invalid conversion for UTF8 to UTF32 converter - DECLARE_EXCEPTION_TYPE(Base, IconvConvertErrorUTF8ToUTF32) - - // @brief Invalid conversion for UTF8 to UTF32 converter - DECLARE_EXCEPTION_TYPE(Base, IconvConvertErrorUTF32ToUTF8) - - // @brief Invalid ASCII character detected in FromASCII - DECLARE_EXCEPTION_TYPE(Base, InvalidASCIICharacter) - - // @brief Invalid ASCII character detected in FromASCII - DECLARE_EXCEPTION_TYPE(Base, ICUInvalidCharacterFound) -}; - -//!\brief convert ASCII string to CCHECKER::String -String FromASCIIString(const std::string& aString); - -//!\brief convert UTF32 string to CCHECKER::String -String FromUTF32String(const std::wstring& aString); - -//@brief Returns String object created from UTF8 string -//@param[in] aString input UTF-8 string -String FromUTF8String(const std::string& aString); - -//@brief Returns String content as std::string -std::string ToUTF8String(const String& aString); - -//@brief Compare two unicode strings -int StringCompare(const String &left, - const String &right, - bool caseInsensitive = false); - -//@brief Splits the string into substrings. -//@param[in] str Input string -//@param[in] delimiters array or string containing a sequence of substring -// delimiters. Can be also a single delimiter character. -//@param[in] it InserterIterator that is used to save the generated substrings. -template -void Tokenize(const StringType& str, - const Delimiters& delimiters, - InserterIterator it, - bool ignoreEmpty = false) -{ - typename StringType::size_type nextSearchStart = 0; - typename StringType::size_type pos; - typename StringType::size_type length; - - while (true) { - pos = str.find_first_of(delimiters, nextSearchStart); - length = - ((pos == StringType::npos) ? str.length() : pos) - nextSearchStart; - - if (!ignoreEmpty || length > 0) { - *it = str.substr(nextSearchStart, length); - it++; - } - - if (pos == StringType::npos) { - return; - } - - nextSearchStart = pos + 1; - } -} - -namespace Utils { - -template class ConcatFunc : public std::binary_function -{ -public: - explicit ConcatFunc(const T & val) : m_delim(val) {} - T operator()(const T & arg1, const T & arg2) const - { - return arg1 + m_delim + arg2; - } -private: - T m_delim; -}; - -} - -template -typename ForwardIterator::value_type Join(ForwardIterator begin, ForwardIterator end, typename ForwardIterator::value_type delim) -{ - typedef typename ForwardIterator::value_type value; - if(begin == end) return value(); - Utils::ConcatFunc func(delim); - ForwardIterator init = begin; - return std::accumulate(++begin, end, *init, func); -} - -} //namespace CCHECKER - -std::ostream& operator<<(std::ostream& aStream, const CCHECKER::String& aString); - -#endif // CCHECKER_STRING diff --git a/src/dpl/core/src/assert.cpp b/src/dpl/core/src/assert.cpp index c8aed8f..2050617 100644 --- a/src/dpl/core/src/assert.cpp +++ b/src/dpl/core/src/assert.cpp @@ -20,10 +20,10 @@ * @brief This file is the implementation file of assert */ #include -#include #include -#include +#include +#include #include namespace CCHECKER { diff --git a/src/dpl/core/src/char_traits.cpp b/src/dpl/core/src/char_traits.cpp index 32b9197..d142cb2 100644 --- a/src/dpl/core/src/char_traits.cpp +++ b/src/dpl/core/src/char_traits.cpp @@ -23,7 +23,8 @@ * performance */ #include -#include + +#include // // Note: diff --git a/src/dpl/core/src/colors.cpp b/src/dpl/core/src/colors.cpp index 25feadf..272c06b 100644 --- a/src/dpl/core/src/colors.cpp +++ b/src/dpl/core/src/colors.cpp @@ -21,7 +21,8 @@ * and html output */ #include -#include + +#include namespace CCHECKER { namespace Colors { diff --git a/src/dpl/core/src/errno_string.cpp b/src/dpl/core/src/errno_string.cpp index e481a02..5b1de37 100644 --- a/src/dpl/core/src/errno_string.cpp +++ b/src/dpl/core/src/errno_string.cpp @@ -20,11 +20,6 @@ * @brief This file is the implementation file of errno string */ #include -#include -#include -#include -#include -#include #include #include #include @@ -32,6 +27,12 @@ #include #include +#include +#include +#include +#include +#include + namespace CCHECKER { namespace // anonymous { diff --git a/src/dpl/core/src/exception.cpp b/src/dpl/core/src/exception.cpp index 5f2e8e0..d26e13c 100644 --- a/src/dpl/core/src/exception.cpp +++ b/src/dpl/core/src/exception.cpp @@ -20,9 +20,9 @@ * @brief This file is the implementation of exception system */ #include -#include #include +#include #include namespace CCHECKER { diff --git a/src/dpl/core/src/noncopyable.cpp b/src/dpl/core/src/noncopyable.cpp index 1ae80e9..6e6f50c 100644 --- a/src/dpl/core/src/noncopyable.cpp +++ b/src/dpl/core/src/noncopyable.cpp @@ -20,7 +20,8 @@ * @brief This file is the implementation file of noncopyable */ #include -#include + +#include namespace CCHECKER { Noncopyable::Noncopyable() diff --git a/src/dpl/core/src/string.cpp b/src/dpl/core/src/string.cpp index cdc83ec..d22aaba 100644 --- a/src/dpl/core/src/string.cpp +++ b/src/dpl/core/src/string.cpp @@ -20,11 +20,6 @@ * @version 1.0 */ #include -#include -#include -#include -#include -#include #include #include #include @@ -33,6 +28,11 @@ #include #include +#include +#include +#include +#include +#include #include // TODO: Completely move to ICU diff --git a/src/dpl/db/include/cchecker/dpl/db/naive_synchronization_object.h b/src/dpl/db/include/cchecker/dpl/db/naive_synchronization_object.h new file mode 100644 index 0000000..8836635 --- /dev/null +++ b/src/dpl/db/include/cchecker/dpl/db/naive_synchronization_object.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file naive_synchronization_object.h + * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) + * @version 1.0 + * @brief This file is the implementation file of SQL naive + * synchronization object + */ +#ifndef CCHECKER_NAIVE_SYNCHRONIZATION_OBJECT_H +#define CCHECKER_NAIVE_SYNCHRONIZATION_OBJECT_H + +#include + +namespace CCHECKER { +namespace DB { +/** + * Naive synchronization object used to synchronize SQL connection + * to the same database across different threads and processes + */ +class NaiveSynchronizationObject : + public SqlConnection::SynchronizationObject +{ + public: + // [SqlConnection::SynchronizationObject] + virtual void Synchronize(); + virtual void NotifyAll(); +}; +} // namespace DB +} // namespace CCHECKER + +#endif // CCHECKER_NAIVE_SYNCHRONIZATION_OBJECT_H diff --git a/src/dpl/db/include/cchecker/dpl/db/sql_connection.h b/src/dpl/db/include/cchecker/dpl/db/sql_connection.h new file mode 100644 index 0000000..8af5a58 --- /dev/null +++ b/src/dpl/db/include/cchecker/dpl/db/sql_connection.h @@ -0,0 +1,499 @@ +/* + * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file sql_connection.h + * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) + * @version 1.0 + * @brief This file is the implementation file of SQL connection + */ +#ifndef CCHECKER_SQL_CONNECTION_H +#define CCHECKER_SQL_CONNECTION_H + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +namespace CCHECKER { +namespace DB { +/** + * SQL connection class + */ +class SqlConnection +{ + public: + /** + * SQL Exception classes + */ + class Exception + { + public: + DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, Base) + DECLARE_EXCEPTION_TYPE(Base, SyntaxError) + DECLARE_EXCEPTION_TYPE(Base, ConnectionBroken) + DECLARE_EXCEPTION_TYPE(Base, InternalError) + DECLARE_EXCEPTION_TYPE(Base, InvalidColumn) + }; + + typedef int ColumnIndex; + typedef int ArgumentIndex; + + /* + * SQL processed data command + */ + class DataCommand : + private Noncopyable + { + private: + SqlConnection *m_masterConnection; + sqlite3_stmt *m_stmt; + + void CheckBindResult(int result); + void CheckColumnIndex(SqlConnection::ColumnIndex column); + + DataCommand(SqlConnection *connection, const char *buffer); + + friend class SqlConnection; + + public: + virtual ~DataCommand(); + + /** + * Bind null to the prepared statement argument + * + * @param position Index of argument to bind value to + */ + void BindNull(ArgumentIndex position); + + /** + * Bind int to the prepared statement argument + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindInteger(ArgumentIndex position, int value); + + /** + * Bind int8_t to the prepared statement argument + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindInt8(ArgumentIndex position, int8_t value); + + /** + * Bind int16 to the prepared statement argument + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindInt16(ArgumentIndex position, int16_t value); + + /** + * Bind int32 to the prepared statement argument + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindInt32(ArgumentIndex position, int32_t value); + + /** + * Bind int64 to the prepared statement argument + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindInt64(ArgumentIndex position, int64_t value); + + /** + * Bind float to the prepared statement argument + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindFloat(ArgumentIndex position, float value); + + /** + * Bind double to the prepared statement argument + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindDouble(ArgumentIndex position, double value); + + /** + * Bind string to the prepared statement argument + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindString(ArgumentIndex position, const char *value); + + /** + * Bind string to the prepared statement argument + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindString(ArgumentIndex position, const String& value); + + /** + * Bind optional int to the prepared statement argument. + * If optional is not set null will be bound + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindInteger(ArgumentIndex position, const Optional &value); + + /** + * Bind optional int8 to the prepared statement argument. + * If optional is not set null will be bound + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindInt8(ArgumentIndex position, const Optional &value); + + /** + * Bind optional int16 to the prepared statement argument. + * If optional is not set null will be bound + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindInt16(ArgumentIndex position, const Optional &value); + + /** + * Bind optional int32 to the prepared statement argument. + * If optional is not set null will be bound + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindInt32(ArgumentIndex position, const Optional &value); + + /** + * Bind optional int64 to the prepared statement argument. + * If optional is not set null will be bound + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindInt64(ArgumentIndex position, const Optional &value); + + /** + * Bind optional float to the prepared statement argument. + * If optional is not set null will be bound + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindFloat(ArgumentIndex position, const Optional &value); + + /** + * Bind optional double to the prepared statement argument. + * If optional is not set null will be bound + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindDouble(ArgumentIndex position, const Optional &value); + + /** + * Bind optional string to the prepared statement argument. + * If optional is not set null will be bound + * + * @param position Index of argument to bind value to + * @param value Value to bind + */ + void BindString(ArgumentIndex position, const Optional &value); + + /** + * Execute the prepared statement and/or move + * to the next row of the result + * + * @return True when there was a row returned + */ + bool Step(); + + /** + * Reset prepared statement's arguments + * All parameters will become null + */ + void Reset(); + + /** + * Checks whether column value is null + * + * @throw Exception::InvalidColumn + */ + bool IsColumnNull(ColumnIndex column); + + /** + * Get integer value from column in current row. + * + * @throw Exception::InvalidColumn + */ + int GetColumnInteger(ColumnIndex column); + + /** + * Get int8 value from column in current row. + * + * @throw Exception::InvalidColumn + */ + int8_t GetColumnInt8(ColumnIndex column); + + /** + * Get int16 value from column in current row. + * + * @throw Exception::InvalidColumn + */ + int16_t GetColumnInt16(ColumnIndex column); + /** + * Get int32 value from column in current row. + * + * @throw Exception::InvalidColumn + */ + int32_t GetColumnInt32(ColumnIndex column); + + /** + * Get int64 value from column in current row. + * + * @throw Exception::InvalidColumn + */ + int64_t GetColumnInt64(ColumnIndex column); + + /** + * Get float value from column in current row. + * + * @throw Exception::InvalidColumn + */ + float GetColumnFloat(ColumnIndex column); + + /** + * Get double value from column in current row. + * + * @throw Exception::InvalidColumn + */ + double GetColumnDouble(ColumnIndex column); + + /** + * Get string value from column in current row. + * + * @throw Exception::InvalidColumn + */ + std::string GetColumnString(ColumnIndex column); + + /** + * Get optional integer value from column in current row. + * + * @throw Exception::InvalidColumn + */ + Optional GetColumnOptionalInteger(ColumnIndex column); + + /** + * Get optional int8 value from column in current row. + * + * @throw Exception::InvalidColumn + */ + Optional GetColumnOptionalInt8(ColumnIndex column); + + /** + * Get optional int16value from column in current row. + * + * @throw Exception::InvalidColumn + */ + Optional GetColumnOptionalInt16(ColumnIndex column); + + /** + * Get optional int32 value from column in current row. + * + * @throw Exception::InvalidColumn + */ + Optional GetColumnOptionalInt32(ColumnIndex column); + + /** + * Get optional int64 value from column in current row. + * + * @throw Exception::InvalidColumn + */ + Optional GetColumnOptionalInt64(ColumnIndex column); + + /** + * Get optional float value from column in current row. + * + * @throw Exception::InvalidColumn + */ + Optional GetColumnOptionalFloat(ColumnIndex column); + + /** + * Get optional double value from column in current row. + * + * @throw Exception::InvalidColumn + */ + Optional GetColumnOptionalDouble(ColumnIndex column); + + /** + * Get optional string value from column in current row. + * + * @throw Exception::InvalidColumn + */ + Optional GetColumnOptionalString(ColumnIndex column); + }; + + // Move on copy semantics + typedef std::auto_ptr DataCommandAutoPtr; + + // Open flags + class Flag + { + public: + enum Type + { + None = 1 << 0, + UseLucene = 1 << 1 + }; + + enum Option + { + RO = SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READONLY, + /** + * *TODO: please remove CREATE option from RW flag when all places + * that need that switched do CRW + */ + RW = SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READWRITE | + SQLITE_OPEN_CREATE, + CRW = RW | SQLITE_OPEN_CREATE + }; + }; + + // RowID + typedef sqlite3_int64 RowID; + + /** + * Synchronization object used to synchronize SQL connection + * to the same database across different threads and processes + */ + class SynchronizationObject + { + public: + virtual ~SynchronizationObject() {} + + /** + * Synchronizes SQL connection for multiple clients. + */ + virtual void Synchronize() = 0; + + /** + * Notify all waiting clients that the connection is no longer locked. + */ + virtual void NotifyAll() = 0; + }; + + protected: + sqlite3 *m_connection; + + // Options + bool m_usingLucene; + + // Stored data procedures + int m_dataCommandsCount; + + // Synchronization object + std::unique_ptr m_synchronizationObject; + + virtual void Connect(const std::string &address, + Flag::Type = Flag::None, Flag::Option = Flag::RO); + virtual void Disconnect(); + + void TurnOnForeignKeys(); + + static SynchronizationObject *AllocDefaultSynchronizationObject(); + + public: + /** + * Open SQL connection + * + * Synchronization is archieved by using provided asynchronization object. + * If synchronizationObject is set to NULL, so synchronization is performed. + * Ownership of the synchronization object is transfered to sql connection + * object. + * + * @param address Database file name + * @param flags Open flags + * @param synchronizationObject A synchronization object to use. + */ + explicit SqlConnection(const std::string &address = std::string(), + Flag::Type flags = Flag::None, + Flag::Option options = Flag::RO, + SynchronizationObject *synchronizationObject = + AllocDefaultSynchronizationObject()); + + /** + * Destructor + */ + virtual ~SqlConnection(); + + /** + * Execute SQL command without result + * + * @param format + * @param ... + */ + void ExecCommand(const char *format, ...); + + /** + * Prepare stored procedure + * + * @param format SQL statement + * @return Data command representing stored procedure + */ + DataCommandAutoPtr PrepareDataCommand(const char *format, ...); + + /** + * Check whether given table exists + * + * @param tableName Name of the table to check + * @return True if given table name exists + */ + bool CheckTableExist(const char *tableName); + + /** + * Get last insert operation new row id + * + * @return Row ID + */ + RowID GetLastInsertRowID() const; + + void BeginTransaction(); + + void RollbackTransaction(); + + void CommitTransaction(); +}; +} // namespace DB +} // namespace CCHECKER + +#endif // CCHECKER_SQL_CONNECTION_H diff --git a/src/dpl/db/include/dpl/db/naive_synchronization_object.h b/src/dpl/db/include/dpl/db/naive_synchronization_object.h deleted file mode 100644 index 687d666..0000000 --- a/src/dpl/db/include/dpl/db/naive_synchronization_object.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * @file naive_synchronization_object.h - * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) - * @version 1.0 - * @brief This file is the implementation file of SQL naive - * synchronization object - */ -#ifndef CCHECKER_NAIVE_SYNCHRONIZATION_OBJECT_H -#define CCHECKER_NAIVE_SYNCHRONIZATION_OBJECT_H - -#include - -namespace CCHECKER { -namespace DB { -/** - * Naive synchronization object used to synchronize SQL connection - * to the same database across different threads and processes - */ -class NaiveSynchronizationObject : - public SqlConnection::SynchronizationObject -{ - public: - // [SqlConnection::SynchronizationObject] - virtual void Synchronize(); - virtual void NotifyAll(); -}; -} // namespace DB -} // namespace CCHECKER - -#endif // CCHECKER_NAIVE_SYNCHRONIZATION_OBJECT_H diff --git a/src/dpl/db/include/dpl/db/sql_connection.h b/src/dpl/db/include/dpl/db/sql_connection.h deleted file mode 100644 index 321372b..0000000 --- a/src/dpl/db/include/dpl/db/sql_connection.h +++ /dev/null @@ -1,499 +0,0 @@ -/* - * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * @file sql_connection.h - * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com) - * @version 1.0 - * @brief This file is the implementation file of SQL connection - */ -#ifndef CCHECKER_SQL_CONNECTION_H -#define CCHECKER_SQL_CONNECTION_H - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -namespace CCHECKER { -namespace DB { -/** - * SQL connection class - */ -class SqlConnection -{ - public: - /** - * SQL Exception classes - */ - class Exception - { - public: - DECLARE_EXCEPTION_TYPE(CCHECKER::Exception, Base) - DECLARE_EXCEPTION_TYPE(Base, SyntaxError) - DECLARE_EXCEPTION_TYPE(Base, ConnectionBroken) - DECLARE_EXCEPTION_TYPE(Base, InternalError) - DECLARE_EXCEPTION_TYPE(Base, InvalidColumn) - }; - - typedef int ColumnIndex; - typedef int ArgumentIndex; - - /* - * SQL processed data command - */ - class DataCommand : - private Noncopyable - { - private: - SqlConnection *m_masterConnection; - sqlite3_stmt *m_stmt; - - void CheckBindResult(int result); - void CheckColumnIndex(SqlConnection::ColumnIndex column); - - DataCommand(SqlConnection *connection, const char *buffer); - - friend class SqlConnection; - - public: - virtual ~DataCommand(); - - /** - * Bind null to the prepared statement argument - * - * @param position Index of argument to bind value to - */ - void BindNull(ArgumentIndex position); - - /** - * Bind int to the prepared statement argument - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindInteger(ArgumentIndex position, int value); - - /** - * Bind int8_t to the prepared statement argument - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindInt8(ArgumentIndex position, int8_t value); - - /** - * Bind int16 to the prepared statement argument - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindInt16(ArgumentIndex position, int16_t value); - - /** - * Bind int32 to the prepared statement argument - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindInt32(ArgumentIndex position, int32_t value); - - /** - * Bind int64 to the prepared statement argument - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindInt64(ArgumentIndex position, int64_t value); - - /** - * Bind float to the prepared statement argument - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindFloat(ArgumentIndex position, float value); - - /** - * Bind double to the prepared statement argument - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindDouble(ArgumentIndex position, double value); - - /** - * Bind string to the prepared statement argument - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindString(ArgumentIndex position, const char *value); - - /** - * Bind string to the prepared statement argument - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindString(ArgumentIndex position, const String& value); - - /** - * Bind optional int to the prepared statement argument. - * If optional is not set null will be bound - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindInteger(ArgumentIndex position, const Optional &value); - - /** - * Bind optional int8 to the prepared statement argument. - * If optional is not set null will be bound - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindInt8(ArgumentIndex position, const Optional &value); - - /** - * Bind optional int16 to the prepared statement argument. - * If optional is not set null will be bound - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindInt16(ArgumentIndex position, const Optional &value); - - /** - * Bind optional int32 to the prepared statement argument. - * If optional is not set null will be bound - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindInt32(ArgumentIndex position, const Optional &value); - - /** - * Bind optional int64 to the prepared statement argument. - * If optional is not set null will be bound - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindInt64(ArgumentIndex position, const Optional &value); - - /** - * Bind optional float to the prepared statement argument. - * If optional is not set null will be bound - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindFloat(ArgumentIndex position, const Optional &value); - - /** - * Bind optional double to the prepared statement argument. - * If optional is not set null will be bound - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindDouble(ArgumentIndex position, const Optional &value); - - /** - * Bind optional string to the prepared statement argument. - * If optional is not set null will be bound - * - * @param position Index of argument to bind value to - * @param value Value to bind - */ - void BindString(ArgumentIndex position, const Optional &value); - - /** - * Execute the prepared statement and/or move - * to the next row of the result - * - * @return True when there was a row returned - */ - bool Step(); - - /** - * Reset prepared statement's arguments - * All parameters will become null - */ - void Reset(); - - /** - * Checks whether column value is null - * - * @throw Exception::InvalidColumn - */ - bool IsColumnNull(ColumnIndex column); - - /** - * Get integer value from column in current row. - * - * @throw Exception::InvalidColumn - */ - int GetColumnInteger(ColumnIndex column); - - /** - * Get int8 value from column in current row. - * - * @throw Exception::InvalidColumn - */ - int8_t GetColumnInt8(ColumnIndex column); - - /** - * Get int16 value from column in current row. - * - * @throw Exception::InvalidColumn - */ - int16_t GetColumnInt16(ColumnIndex column); - /** - * Get int32 value from column in current row. - * - * @throw Exception::InvalidColumn - */ - int32_t GetColumnInt32(ColumnIndex column); - - /** - * Get int64 value from column in current row. - * - * @throw Exception::InvalidColumn - */ - int64_t GetColumnInt64(ColumnIndex column); - - /** - * Get float value from column in current row. - * - * @throw Exception::InvalidColumn - */ - float GetColumnFloat(ColumnIndex column); - - /** - * Get double value from column in current row. - * - * @throw Exception::InvalidColumn - */ - double GetColumnDouble(ColumnIndex column); - - /** - * Get string value from column in current row. - * - * @throw Exception::InvalidColumn - */ - std::string GetColumnString(ColumnIndex column); - - /** - * Get optional integer value from column in current row. - * - * @throw Exception::InvalidColumn - */ - Optional GetColumnOptionalInteger(ColumnIndex column); - - /** - * Get optional int8 value from column in current row. - * - * @throw Exception::InvalidColumn - */ - Optional GetColumnOptionalInt8(ColumnIndex column); - - /** - * Get optional int16value from column in current row. - * - * @throw Exception::InvalidColumn - */ - Optional GetColumnOptionalInt16(ColumnIndex column); - - /** - * Get optional int32 value from column in current row. - * - * @throw Exception::InvalidColumn - */ - Optional GetColumnOptionalInt32(ColumnIndex column); - - /** - * Get optional int64 value from column in current row. - * - * @throw Exception::InvalidColumn - */ - Optional GetColumnOptionalInt64(ColumnIndex column); - - /** - * Get optional float value from column in current row. - * - * @throw Exception::InvalidColumn - */ - Optional GetColumnOptionalFloat(ColumnIndex column); - - /** - * Get optional double value from column in current row. - * - * @throw Exception::InvalidColumn - */ - Optional GetColumnOptionalDouble(ColumnIndex column); - - /** - * Get optional string value from column in current row. - * - * @throw Exception::InvalidColumn - */ - Optional GetColumnOptionalString(ColumnIndex column); - }; - - // Move on copy semantics - typedef std::auto_ptr DataCommandAutoPtr; - - // Open flags - class Flag - { - public: - enum Type - { - None = 1 << 0, - UseLucene = 1 << 1 - }; - - enum Option - { - RO = SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READONLY, - /** - * *TODO: please remove CREATE option from RW flag when all places - * that need that switched do CRW - */ - RW = SQLITE_OPEN_NOMUTEX | SQLITE_OPEN_READWRITE | - SQLITE_OPEN_CREATE, - CRW = RW | SQLITE_OPEN_CREATE - }; - }; - - // RowID - typedef sqlite3_int64 RowID; - - /** - * Synchronization object used to synchronize SQL connection - * to the same database across different threads and processes - */ - class SynchronizationObject - { - public: - virtual ~SynchronizationObject() {} - - /** - * Synchronizes SQL connection for multiple clients. - */ - virtual void Synchronize() = 0; - - /** - * Notify all waiting clients that the connection is no longer locked. - */ - virtual void NotifyAll() = 0; - }; - - protected: - sqlite3 *m_connection; - - // Options - bool m_usingLucene; - - // Stored data procedures - int m_dataCommandsCount; - - // Synchronization object - std::unique_ptr m_synchronizationObject; - - virtual void Connect(const std::string &address, - Flag::Type = Flag::None, Flag::Option = Flag::RO); - virtual void Disconnect(); - - void TurnOnForeignKeys(); - - static SynchronizationObject *AllocDefaultSynchronizationObject(); - - public: - /** - * Open SQL connection - * - * Synchronization is archieved by using provided asynchronization object. - * If synchronizationObject is set to NULL, so synchronization is performed. - * Ownership of the synchronization object is transfered to sql connection - * object. - * - * @param address Database file name - * @param flags Open flags - * @param synchronizationObject A synchronization object to use. - */ - explicit SqlConnection(const std::string &address = std::string(), - Flag::Type flags = Flag::None, - Flag::Option options = Flag::RO, - SynchronizationObject *synchronizationObject = - AllocDefaultSynchronizationObject()); - - /** - * Destructor - */ - virtual ~SqlConnection(); - - /** - * Execute SQL command without result - * - * @param format - * @param ... - */ - void ExecCommand(const char *format, ...); - - /** - * Prepare stored procedure - * - * @param format SQL statement - * @return Data command representing stored procedure - */ - DataCommandAutoPtr PrepareDataCommand(const char *format, ...); - - /** - * Check whether given table exists - * - * @param tableName Name of the table to check - * @return True if given table name exists - */ - bool CheckTableExist(const char *tableName); - - /** - * Get last insert operation new row id - * - * @return Row ID - */ - RowID GetLastInsertRowID() const; - - void BeginTransaction(); - - void RollbackTransaction(); - - void CommitTransaction(); -}; -} // namespace DB -} // namespace CCHECKER - -#endif // CCHECKER_SQL_CONNECTION_H diff --git a/src/dpl/db/src/naive_synchronization_object.cpp b/src/dpl/db/src/naive_synchronization_object.cpp index 2592c3c..18eeefb 100644 --- a/src/dpl/db/src/naive_synchronization_object.cpp +++ b/src/dpl/db/src/naive_synchronization_object.cpp @@ -21,10 +21,11 @@ * synchronization object */ #include -#include #include #include +#include + namespace { unsigned int seed = time(NULL); } diff --git a/src/dpl/db/src/sql_connection.cpp b/src/dpl/db/src/sql_connection.cpp index 27ea4fd..65c04ec 100644 --- a/src/dpl/db/src/sql_connection.cpp +++ b/src/dpl/db/src/sql_connection.cpp @@ -20,16 +20,17 @@ * @brief This file is the implementation file of SQL connection */ #include -#include -#include -#include -#include -#include #include #include #include #include +#include +#include +#include +#include +#include + namespace CCHECKER { namespace DB { namespace // anonymous diff --git a/src/include/cchecker/app.h b/src/include/cchecker/app.h index c7dff35..3217bdb 100644 --- a/src/include/cchecker/app.h +++ b/src/include/cchecker/app.h @@ -24,10 +24,15 @@ #include #include +#include #include namespace CCHECKER { +// Used as app_id when no information about app id in signal +// Currently in signals from pkgmgr only information about pkg_id is included +const char *const TEMP_APP_ID = "temp#app_id"; + struct app_t { enum class verified_t : int32_t { NO = 0, @@ -50,6 +55,22 @@ struct app_t { std::string str(void) const; }; +struct url_t { + std::string issuer; + std::string url; + int64_t date; + + url_t(const std::string &_issuer, + const std::string &_url, + int64_t _date): + issuer(_issuer), + url(_url), + date(_date) + {}; +}; + +typedef std::list ocsp_urls_t; + } //CCHECKER #endif //CCHECKER_APP_H diff --git a/src/include/cchecker/certs.h b/src/include/cchecker/certs.h new file mode 100644 index 0000000..ed8efe0 --- /dev/null +++ b/src/include/cchecker/certs.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * @file certs.h + * @author Janusz Kozerski (j.kozerski@samsung.com) + * @version 1.0 + * @brief This file is the implementation of certificates logic + * Getting out findinf app signature, getting certificates out of + * signature. Checking OCSP + */ + +#ifndef CCHECKER_CERTS_H +#define CCHECKER_CERTS_H + +#include + +namespace CCHECKER { + +class Certs { + public: + Certs(); + virtual ~Certs(); + void get_certificates (app_t &app, ocsp_urls_t &ocsp_urls); + private: + void find_app_signatures (app_t &app, const std::string &app_path, ocsp_urls_t &ocsp_urls); + void search_app (app_t &app, ocsp_urls_t &ocsp_urls); + +}; + +} // CCHECKER + + +#endif // CCHECKER_CERTS_H diff --git a/src/include/cchecker/logic.h b/src/include/cchecker/logic.h index 1f42ab3..d698437 100644 --- a/src/include/cchecker/logic.h +++ b/src/include/cchecker/logic.h @@ -31,6 +31,7 @@ #include #include +#include #include namespace CCHECKER { @@ -84,7 +85,7 @@ class Logic { void pkgmanager_uninstall(const app_t &app); void get_certs_from_signature(const std::string &signature, std::vector &cert); - void add_app_to_buffer(const app_t &app); + void add_app_to_buffer_and_database(const app_t &app); void remove_app_from_buffer(const app_t &app); void pkgmgr_callback_internal(GVariant *parameters, pkgmgr_event_t event); @@ -111,6 +112,7 @@ class Logic { void set_should_exit(void); Queue m_queue; + Certs m_certs; std::list m_buffer; DB::SqlQuery *m_sqlquery; bool m_was_setup_called; diff --git a/src/include/cchecker/sql_query.h b/src/include/cchecker/sql_query.h index 3996b75..5a22740 100644 --- a/src/include/cchecker/sql_query.h +++ b/src/include/cchecker/sql_query.h @@ -23,7 +23,7 @@ #include #include -#include +#include #include namespace CCHECKER { diff --git a/src/logic.cpp b/src/logic.cpp index 82059ac..445c266 100644 --- a/src/logic.cpp +++ b/src/logic.cpp @@ -32,7 +32,6 @@ namespace CCHECKER { namespace { const char *const DB_PATH = tzplatform_mkpath(TZ_SYS_DB, ".cert-checker.db"); -const char *const TEPM_APP_ID = "temp#app_id"; } Logic::~Logic(void) @@ -264,7 +263,7 @@ void Logic::pkgmgr_callback_internal(GVariant *parameters, status = g_variant_dup_string(g_variant_get_child_value(parameters, 5), NULL); // FIXME: No information about app_id in the signal. Use stub. - app_t app(TEPM_APP_ID, pkgid, uid, {}); + app_t app(TEMP_APP_ID, pkgid, uid, {}); if (std::string(state) == "end" && std::string(status) == "ok") { if (event == EVENT_INSTALL) { @@ -358,7 +357,9 @@ void Logic::process_queue(void) error_t Logic::process_buffer(void) { - // TODO: Implement + for(auto iter = m_buffer.begin(); iter != m_buffer.end(); iter++) { + // TODO: Implement checking OCSP + } return NO_ERROR; } @@ -393,18 +394,29 @@ void Logic::process_all() void Logic::process_event(const event_t &event) { if (event.event_type == event_t::event_type_t::APP_INSTALL) { - // TODO: implement geting app signature, then getting certificates from app signature. - // TODO: implement add app to buffer and database - add_app_to_buffer(event.app); + // pulling out certificates from signatures + app_t app = event.app; + ocsp_urls_t ocsp_urls; + m_certs.get_certificates(app, ocsp_urls); + add_app_to_buffer_and_database(app); + + // Adding OCSP URLs - if found any + if (!ocsp_urls.empty()){ + LogDebug("Some OCSP url has been found. Adding to database"); + for (auto iter = ocsp_urls.begin(); iter != ocsp_urls.end(); iter++){ + m_sqlquery->set_url(iter->issuer, iter->url, iter->date); + } + } } else if (event.event_type == event_t::event_type_t::APP_UNINSTALL) { remove_app_from_buffer(event.app); + m_sqlquery->remove_app_from_check_list(event.app); } else LogError("Unknown event type"); } -void Logic::add_app_to_buffer(const app_t &app) +void Logic::add_app_to_buffer_and_database(const app_t &app) { // First add app to DB if(!m_sqlquery->add_app_to_check_list(app)) { diff --git a/tests/colour_log_formatter.cpp b/tests/colour_log_formatter.cpp index 8b71d99..4587237 100644 --- a/tests/colour_log_formatter.cpp +++ b/tests/colour_log_formatter.cpp @@ -12,7 +12,7 @@ #include #include -#include +#include // ************************************************************************** // // ************** colour_log_formatter ************** // -- cgit v0.11.2