Browse Source
Define and implement Coding Style using clang-format. top level .clang-format, supported by nearly all Editors/IDE's and if not, one has to manually run clang-format. Its up to the dev. But everybody can use it, and its enables to use a shared definition of coding style. I want to avoid the project to end up in an underspecified/inconsistent state after the code base has grown over the years. So, NOW is the time to do it. Flat project structure becomes unmanageable. Create standard project layout, of at least * src * test for now ALSO: The max. C++ Standard to be used (pEp-wide) has now been raised to C++14 (!!!)pull/8/head

35 changed files with 652 additions and 644 deletions
@ -0,0 +1,39 @@ |
|||
# Generated from CLion C/C++ Code Style settings |
|||
BasedOnStyle: LLVM |
|||
Language: Cpp |
|||
DerivePointerAlignment: true |
|||
SortIncludes: false |
|||
ReflowComments: false |
|||
PointerAlignment: Left |
|||
AlignAfterOpenBracket: AlwaysBreak |
|||
AlignOperands: AlignAfterOperator |
|||
AlignTrailingComments: true |
|||
AllowAllArgumentsOnNextLine: false |
|||
AllowAllParametersOfDeclarationOnNextLine: false |
|||
AllowShortEnumsOnASingleLine: false |
|||
AllowShortFunctionsOnASingleLine: Empty |
|||
AllowShortIfStatementsOnASingleLine: Never |
|||
AllowShortLoopsOnASingleLine: false |
|||
AlwaysBreakTemplateDeclarations: Yes |
|||
BinPackArguments: false |
|||
BinPackParameters: false |
|||
ExperimentalAutoDetectBinPacking: true |
|||
BreakBeforeBraces: Custom |
|||
BraceWrapping: |
|||
AfterFunction: true |
|||
ColumnLimit: 100 |
|||
AllowAllConstructorInitializersOnNextLine: false |
|||
AlwaysBreakAfterDefinitionReturnType: None |
|||
AlwaysBreakAfterReturnType: None |
|||
PenaltyBreakBeforeFirstCallParameter: 0 |
|||
PenaltyReturnTypeOnItsOwnLine: 1000000 |
|||
PenaltyBreakAssignment: 1000000 |
|||
PenaltyExcessCharacter: 10 |
|||
IndentCaseLabels: true |
|||
IndentWidth: 4 |
|||
MaxEmptyLinesToKeep: 2 |
|||
NamespaceIndentation: All |
|||
SpaceAfterTemplateKeyword: false |
|||
AccessModifierOffset: -4 |
|||
AllowShortBlocksOnASingleLine: Always |
|||
IndentPPDirectives: BeforeHash |
@ -1,26 +0,0 @@ |
|||
// This file is under GNU General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#ifndef LIBPEPADAPTER_CALL_WITH_LOCK_HH |
|||
#define LIBPEPADAPTER_CALL_WITH_LOCK_HH |
|||
|
|||
#include <mutex> |
|||
|
|||
namespace pEp |
|||
{ |
|||
extern std::mutex call_with_lock_mutex; |
|||
|
|||
// TODO: use && and std::forward<> to avoid copying of the arguments.
|
|||
// It is not relevant, yet, because at the moment we use this function template only
|
|||
// for init() and release() which have cheap-to-copy pointer parameters only
|
|||
template<class R, class... Args> |
|||
R call_with_lock( R(*fn)(Args...), Args... args) |
|||
{ |
|||
std::lock_guard<std::mutex> L(call_with_lock_mutex); |
|||
return fn(args...); |
|||
} |
|||
|
|||
|
|||
} |
|||
|
|||
#endif // LIBPEPADAPTER_CALL_WITH_LOCK_HH
|
@ -1,23 +0,0 @@ |
|||
// This file is under GNU General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#include "constant_time_algo.hh" |
|||
|
|||
namespace pEp |
|||
{ |
|||
bool constant_time_equal(const std::string& a, const std::string& b) |
|||
{ |
|||
if(a.size() != b.size()) |
|||
return false; |
|||
|
|||
unsigned d = 0; |
|||
for(std::size_t idx = 0; idx<a.size(); ++idx) |
|||
{ |
|||
d |= ( static_cast<unsigned>(a[idx]) ^ static_cast<unsigned>(b[idx]) ); |
|||
} |
|||
|
|||
// if d is still 0, the strings are equal.
|
|||
return d == 0; |
|||
} |
|||
|
|||
} // end of namespace pEp
|
@ -1,135 +0,0 @@ |
|||
// This file is under GNU General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#ifndef LIBPEPADAPTER_MESSAGE_CACHE_HH |
|||
#define LIBPEPADAPTER_MESSAGE_CACHE_HH |
|||
|
|||
#include <string> |
|||
#include <unordered_map> |
|||
#include <mutex> |
|||
#include <pEp/message_api.h> |
|||
#include <pEp/mime.h> |
|||
|
|||
namespace pEp { |
|||
class MessageCache { |
|||
struct cache_entry { |
|||
cache_entry(::message *s, ::message *d) |
|||
: src(s), dst(d) { } |
|||
|
|||
::message *src; |
|||
::message *dst; |
|||
}; |
|||
|
|||
using cache = std::unordered_map<std::string, cache_entry>; |
|||
|
|||
cache _cache; |
|||
std::mutex _mtx; |
|||
long long id_range = 42; |
|||
long long next_id = 23; |
|||
|
|||
public: |
|||
MessageCache(); |
|||
|
|||
enum which { msg_src = 0, msg_dst = 1 }; |
|||
|
|||
static PEP_STATUS cache_mime_decode_message( |
|||
const char *mimetext, |
|||
size_t size, |
|||
message **msg, |
|||
bool* has_possible_pEp_msg |
|||
); |
|||
|
|||
static PEP_STATUS cache_mime_encode_message( |
|||
int one, |
|||
const message * msg, |
|||
bool omit_fields, |
|||
char **mimetext, |
|||
bool has_pEp_msg_attachment |
|||
); |
|||
|
|||
static PEP_STATUS cache_decrypt_message( |
|||
PEP_SESSION session, |
|||
message *src, |
|||
message **dst, |
|||
stringlist_t **keylist, |
|||
PEP_rating *rating, |
|||
PEP_decrypt_flags_t *flags |
|||
); |
|||
|
|||
static PEP_STATUS cache_encrypt_message( |
|||
PEP_SESSION session, |
|||
message *src, |
|||
stringlist_t *extra, |
|||
message **dst, |
|||
PEP_enc_format enc_format, |
|||
PEP_encrypt_flags_t flags |
|||
); |
|||
|
|||
static PEP_STATUS cache_encrypt_message_for_self( |
|||
PEP_SESSION session, |
|||
pEp_identity* target_id, |
|||
message *src, |
|||
stringlist_t* extra, |
|||
message **dst, |
|||
PEP_enc_format enc_format, |
|||
PEP_encrypt_flags_t flags |
|||
); |
|||
|
|||
|
|||
static PEP_STATUS cache_release(std::string id); |
|||
static void removeCacheID(::message* msg); |
|||
|
|||
protected: |
|||
void release(std::string id); |
|||
|
|||
PEP_STATUS mime_decode_message( |
|||
const char *mimetext, |
|||
size_t size, |
|||
message **msg, |
|||
bool* has_possible_pEp_msg |
|||
); |
|||
|
|||
PEP_STATUS mime_encode_message( |
|||
which one, |
|||
const message * src, |
|||
bool omit_fields, |
|||
char **mimetext, |
|||
bool has_pEp_msg_attachment |
|||
); |
|||
|
|||
PEP_STATUS decrypt_message( |
|||
PEP_SESSION session, |
|||
message *src, |
|||
message **dst, |
|||
stringlist_t **keylist, |
|||
PEP_rating *rating, |
|||
PEP_decrypt_flags_t *flags |
|||
); |
|||
|
|||
PEP_STATUS encrypt_message( |
|||
PEP_SESSION session, |
|||
message *src, |
|||
stringlist_t *extra, |
|||
message **dst, |
|||
PEP_enc_format enc_format, |
|||
PEP_encrypt_flags_t flags |
|||
); |
|||
|
|||
PEP_STATUS encrypt_message_for_self( |
|||
PEP_SESSION session, |
|||
pEp_identity* target_id, |
|||
message *src, |
|||
stringlist_t* extra, |
|||
message **dst, |
|||
PEP_enc_format enc_format, |
|||
PEP_encrypt_flags_t flags |
|||
); |
|||
|
|||
void generateCacheID(::message* msg); |
|||
static std::string cacheID(const ::message* msg); |
|||
}; |
|||
|
|||
extern MessageCache message_cache; |
|||
}; |
|||
|
|||
#endif // LIBPEPADAPTER_MESSAGE_CACHE_HH
|
@ -1,40 +0,0 @@ |
|||
// This file is under GNU General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#include "pEpLog.hh" |
|||
#include <iostream> |
|||
#include <sstream> |
|||
#include <mutex> |
|||
#include <atomic> |
|||
|
|||
|
|||
namespace pEp { |
|||
namespace Adapter { |
|||
namespace pEpLog { |
|||
|
|||
std::mutex mtx; |
|||
|
|||
std::atomic_bool is_enabled{false}; |
|||
|
|||
void set_enabled(bool enabled) { |
|||
is_enabled.store(enabled); |
|||
} |
|||
|
|||
bool get_enabled() { |
|||
return is_enabled.load(); |
|||
} |
|||
|
|||
void log(std::string msg) { |
|||
if (is_enabled.load()) { |
|||
std::lock_guard<std::mutex> l(mtx); |
|||
#ifdef ANDROID |
|||
__android_log_print(ANDROID_LOG_DEBUG, "pEpDebugLog", "%s", msg.c_str()); |
|||
#else |
|||
std::cout << msg << std::endl; //std::endl also flushes
|
|||
#endif |
|||
} |
|||
} |
|||
|
|||
} // pEpLog
|
|||
} // Adapter
|
|||
} // pEp
|
@ -1,25 +0,0 @@ |
|||
// This file is under GNU General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#include "slurp.hh" |
|||
#include <fstream> |
|||
#include <sstream> |
|||
#include <stdexcept> |
|||
|
|||
namespace pEp |
|||
{ |
|||
|
|||
std::string slurp(const std::string& filename) |
|||
{ |
|||
std::ifstream input(filename.c_str(), std::ios_base::binary); |
|||
if(!input) |
|||
{ |
|||
throw std::runtime_error("Cannot read file \"" + filename + "\"! "); |
|||
} |
|||
|
|||
std::stringstream sstr; |
|||
sstr << input.rdbuf(); |
|||
return sstr.str(); |
|||
} |
|||
|
|||
} // end of namespace pEp
|
@ -0,0 +1,37 @@ |
|||
# Copyright 2018, pEp Foundation
|
|||
# This file is part of lib pEp Adapter
|
|||
# This file may be used under the terms of the GNU General Public License version 3
|
|||
# see LICENSE.txt
|
|||
|
|||
include ../Makefile.conf |
|||
|
|||
SOURCE=$(wildcard *.cc) |
|||
HEADERS=$(wildcard *.hh *.hxx) |
|||
OBJECTS=$(subst .cc,.o,$(SOURCE)) |
|||
DEPENDS=$(subst .cc,.d,$(SOURCE)) |
|||
CXXFLAGS+= -MMD -MP |
|||
|
|||
ifneq ($(MAKECMDGOALS),clean) |
|||
-include $(DEPENDS) |
|||
endif |
|||
|
|||
.PHONY: install uninstall clean |
|||
|
|||
all: $(TARGET) |
|||
|
|||
$(TARGET): $(OBJECTS) |
|||
$(AR) -rc $@ $^ |
|||
|
|||
clean: |
|||
rm -vf $(TARGET) $(OBJECTS) $(DEPENDS) |
|||
rm -f *.d.* |
|||
|
|||
install: $(TARGET) |
|||
mkdir -p $(PREFIX)/include/pEp |
|||
mkdir -p $(PREFIX)/lib |
|||
cp -v $(HEADERS) $(PREFIX)/include/pEp/ |
|||
cp -v $(TARGET) $(PREFIX)/lib/ |
|||
|
|||
uninstall: |
|||
cd $(PREFIX)/include/pEp && rm -vf $(HEADERS) |
|||
cd $(PREFIX)/lib && rm -vf $(TARGET) |
@ -0,0 +1,26 @@ |
|||
// This file is under GNU General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#ifndef LIBPEPADAPTER_CALL_WITH_LOCK_HH |
|||
#define LIBPEPADAPTER_CALL_WITH_LOCK_HH |
|||
|
|||
#include <mutex> |
|||
|
|||
namespace pEp { |
|||
extern std::mutex call_with_lock_mutex; |
|||
|
|||
// TODO: use && and std::forward<> to avoid copying of the arguments.
|
|||
// It is not relevant, yet, because at the moment we use this function
|
|||
// template only for init() and release() which have cheap-to-copy pointer
|
|||
// parameters only
|
|||
template<class R, class... Args> |
|||
R call_with_lock(R (*fn)(Args...), Args... args) |
|||
{ |
|||
std::lock_guard<std::mutex> L(call_with_lock_mutex); |
|||
return fn(args...); |
|||
} |
|||
|
|||
|
|||
} // namespace pEp
|
|||
|
|||
#endif // LIBPEPADAPTER_CALL_WITH_LOCK_HH
|
@ -0,0 +1,21 @@ |
|||
// This file is under GNU General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#include "constant_time_algo.hh" |
|||
|
|||
namespace pEp { |
|||
bool constant_time_equal(const std::string &a, const std::string &b) |
|||
{ |
|||
if (a.size() != b.size()) |
|||
return false; |
|||
|
|||
unsigned d = 0; |
|||
for (std::size_t idx = 0; idx < a.size(); ++idx) { |
|||
d |= (static_cast<unsigned>(a[idx]) ^ static_cast<unsigned>(b[idx])); |
|||
} |
|||
|
|||
// if d is still 0, the strings are equal.
|
|||
return d == 0; |
|||
} |
|||
|
|||
} // end of namespace pEp
|
@ -0,0 +1,128 @@ |
|||
// This file is under GNU General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#ifndef LIBPEPADAPTER_MESSAGE_CACHE_HH |
|||
#define LIBPEPADAPTER_MESSAGE_CACHE_HH |
|||
|
|||
#include <string> |
|||
#include <unordered_map> |
|||
#include <mutex> |
|||
#include <pEp/message_api.h> |
|||
#include <pEp/mime.h> |
|||
|
|||
namespace pEp { |
|||
class MessageCache { |
|||
struct cache_entry { |
|||
cache_entry(::message *s, ::message *d) : src(s), dst(d) {} |
|||
|
|||
::message *src; |
|||
::message *dst; |
|||
}; |
|||
|
|||
using cache = std::unordered_map<std::string, cache_entry>; |
|||
|
|||
cache _cache; |
|||
std::mutex _mtx; |
|||
long long id_range = 42; |
|||
long long next_id = 23; |
|||
|
|||
public: |
|||
MessageCache(); |
|||
|
|||
enum which |
|||
{ |
|||
msg_src = 0, |
|||
msg_dst = 1 |
|||
}; |
|||
|
|||
static PEP_STATUS cache_mime_decode_message( |
|||
const char *mimetext, |
|||
size_t size, |
|||
message **msg, |
|||
bool *has_possible_pEp_msg); |
|||
|
|||
static PEP_STATUS cache_mime_encode_message( |
|||
int one, |
|||
const message *msg, |
|||
bool omit_fields, |
|||
char **mimetext, |
|||
bool has_pEp_msg_attachment); |
|||
|
|||
static PEP_STATUS cache_decrypt_message( |
|||
PEP_SESSION session, |
|||
message *src, |
|||
message **dst, |
|||
stringlist_t **keylist, |
|||
PEP_rating *rating, |
|||
PEP_decrypt_flags_t *flags); |
|||
|
|||
static PEP_STATUS cache_encrypt_message( |
|||
PEP_SESSION session, |
|||
message *src, |
|||
stringlist_t *extra, |
|||
message **dst, |
|||
PEP_enc_format enc_format, |
|||
PEP_encrypt_flags_t flags); |
|||
|
|||
static PEP_STATUS cache_encrypt_message_for_self( |
|||
PEP_SESSION session, |
|||
pEp_identity *target_id, |
|||
message *src, |
|||
stringlist_t *extra, |
|||
message **dst, |
|||
PEP_enc_format enc_format, |
|||
PEP_encrypt_flags_t flags); |
|||
|
|||
|
|||
static PEP_STATUS cache_release(std::string id); |
|||
static void removeCacheID(::message *msg); |
|||
|
|||
protected: |
|||
void release(std::string id); |
|||
|
|||
PEP_STATUS mime_decode_message( |
|||
const char *mimetext, |
|||
size_t size, |
|||
message **msg, |
|||
bool *has_possible_pEp_msg); |
|||
|
|||
PEP_STATUS mime_encode_message( |
|||
which one, |
|||
const message *src, |
|||
bool omit_fields, |
|||
char **mimetext, |
|||
bool has_pEp_msg_attachment); |
|||
|
|||
PEP_STATUS decrypt_message( |
|||
PEP_SESSION session, |
|||
message *src, |
|||
message **dst, |
|||
stringlist_t **keylist, |
|||
PEP_rating *rating, |
|||
PEP_decrypt_flags_t *flags); |
|||
|
|||
PEP_STATUS encrypt_message( |
|||
PEP_SESSION session, |
|||
message *src, |
|||
stringlist_t *extra, |
|||
message **dst, |
|||
PEP_enc_format enc_format, |
|||
PEP_encrypt_flags_t flags); |
|||
|
|||
PEP_STATUS encrypt_message_for_self( |
|||
PEP_SESSION session, |
|||
pEp_identity *target_id, |
|||
message *src, |
|||
stringlist_t *extra, |
|||
message **dst, |
|||
PEP_enc_format enc_format, |
|||
PEP_encrypt_flags_t flags); |
|||
|
|||
void generateCacheID(::message *msg); |
|||
static std::string cacheID(const ::message *msg); |
|||
}; |
|||
|
|||
extern MessageCache message_cache; |
|||
}; // namespace pEp
|
|||
|
|||
#endif // LIBPEPADAPTER_MESSAGE_CACHE_HH
|
@ -0,0 +1,43 @@ |
|||
// This file is under GNU General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#include "pEpLog.hh" |
|||
#include <iostream> |
|||
#include <sstream> |
|||
#include <mutex> |
|||
#include <atomic> |
|||
|
|||
|
|||
namespace pEp { |
|||
namespace Adapter { |
|||
namespace pEpLog { |
|||
|
|||
std::mutex mtx; |
|||
|
|||
std::atomic_bool is_enabled{false}; |
|||
|
|||
void set_enabled(bool enabled) |
|||
{ |
|||
is_enabled.store(enabled); |
|||
} |
|||
|
|||
bool get_enabled() |
|||
{ |
|||
return is_enabled.load(); |
|||
} |
|||
|
|||
void log(std::string msg) |
|||
{ |
|||
if (is_enabled.load()) { |
|||
std::lock_guard<std::mutex> l(mtx); |
|||
#ifdef ANDROID |
|||
__android_log_print(ANDROID_LOG_DEBUG, "pEpDebugLog", "%s", msg.c_str()); |
|||
#else |
|||
std::cout << msg << std::endl; //std::endl also flushes
|
|||
#endif |
|||
} |
|||
} |
|||
|
|||
} // namespace pEpLog
|
|||
} // namespace Adapter
|
|||
} // namespace pEp
|
@ -0,0 +1,21 @@ |
|||
// This file is under GNU General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#include "slurp.hh" |
|||
#include <fstream> |
|||
#include <sstream> |
|||
#include <stdexcept> |
|||
|
|||
namespace pEp { |
|||
std::string slurp(const std::string& filename) |
|||
{ |
|||
std::ifstream input(filename.c_str(), std::ios_base::binary); |
|||
if (!input) { |
|||
throw std::runtime_error("Cannot read file \"" + filename + "\"! "); |
|||
} |
|||
|
|||
std::stringstream sstr; |
|||
sstr << input.rdbuf(); |
|||
return sstr.str(); |
|||
} |
|||
} // end of namespace pEp
|
@ -0,0 +1,23 @@ |
|||
// This file is under GNU General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#include "status_to_string.hh" |
|||
#include <sstream> |
|||
|
|||
|
|||
namespace pEp { |
|||
|
|||
// in pEpEngine.h positive values are hex, negative are decimal. :-o
|
|||
// TODO: the code should be generated!
|
|||
std::string status_to_string(PEP_STATUS status) |
|||
{ |
|||
std::stringstream ss; |
|||
if (status > 0) { |
|||
ss << "0x" << std::hex << status; |
|||
} else { |
|||
ss << status; |
|||
} |
|||
return ss.str() + " \"" + pEp_status_to_string(status) + '"'; |
|||
} |
|||
|
|||
} // end of namespace pEp
|
@ -1,25 +0,0 @@ |
|||
// This file is under GNU General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#include "status_to_string.hh" |
|||
#include <sstream> |
|||
|
|||
|
|||
namespace pEp |
|||
{ |
|||
|
|||
// in pEpEngine.h positive values are hex, negative are decimal. :-o
|
|||
// TODO: the code should be generated!
|
|||
std::string status_to_string(PEP_STATUS status) |
|||
{ |
|||
std::stringstream ss; |
|||
if(status>0) |
|||
{ |
|||
ss << "0x" << std::hex << status; |
|||
}else{ |
|||
ss << status; |
|||
} |
|||
return ss.str() + " \"" + pEp_status_to_string(status) + '"'; |
|||
} |
|||
|
|||
} // end of namespace pEp
|
Loading…
Reference in new issue