Reformatting code using Kernighan & Ritchie style, tabs, tab width=4.
Command line: astyle --style=kr --indent=force-tab=4 --lineend=linux -n -r <file names>
This commit is contained in:
@@ -42,7 +42,7 @@ class AvatarManager
|
||||
public:
|
||||
|
||||
AvatarManager(bool useExternalServer = false, const std::string &externalServerAddress = "",
|
||||
const std::string &externalServerUser = "", const std::string &externalServerPassword = "");
|
||||
const std::string &externalServerUser = "", const std::string &externalServerPassword = "");
|
||||
~AvatarManager();
|
||||
|
||||
bool Init(const std::string &dataDir, const std::string &cacheDir);
|
||||
|
||||
@@ -48,15 +48,14 @@
|
||||
using namespace std;
|
||||
using namespace boost::filesystem;
|
||||
|
||||
struct AvatarFileState
|
||||
{
|
||||
struct AvatarFileState {
|
||||
ifstream inputStream;
|
||||
};
|
||||
|
||||
AvatarManager::AvatarManager(bool useExternalServer, const std::string &externalServerAddress,
|
||||
const string &externalServerUser, const string &externalServerPassword)
|
||||
: m_useExternalServer(useExternalServer), m_externalServerAddress(externalServerAddress),
|
||||
m_externalServerUser(externalServerUser), m_externalServerPassword(externalServerPassword)
|
||||
const string &externalServerUser, const string &externalServerPassword)
|
||||
: m_useExternalServer(useExternalServer), m_externalServerAddress(externalServerAddress),
|
||||
m_externalServerUser(externalServerUser), m_externalServerPassword(externalServerPassword)
|
||||
{
|
||||
m_uploader.reset(new UploaderThread());
|
||||
}
|
||||
@@ -87,8 +86,7 @@ AvatarManager::Init(const string &dataDir, const string &cacheDir)
|
||||
}
|
||||
if (cacheDir.empty() || tmpCachePath.empty())
|
||||
LOG_ERROR("Cache directory was not set!");
|
||||
else
|
||||
{
|
||||
else {
|
||||
boost::mutex::scoped_lock lock(m_cachedAvatarsMutex);
|
||||
tmpRet = InternalReadDirectory(tmpCachePath.directory_string(), m_cachedAvatars);
|
||||
retVal = retVal && tmpRet;
|
||||
@@ -105,20 +103,17 @@ AvatarManager::AddSingleAvatar(const std::string &fileName)
|
||||
path filePath(fileName);
|
||||
string tmpFileName(filePath.file_string());
|
||||
|
||||
if (!fileName.empty() && !tmpFileName.empty())
|
||||
{
|
||||
if (!fileName.empty() && !tmpFileName.empty()) {
|
||||
unsigned outFileSize = 0;
|
||||
AvatarFileType outFileType;
|
||||
boost::shared_ptr<AvatarFileState> tmpFileState = OpenAvatarFileForChunkRead(tmpFileName, outFileSize, outFileType);
|
||||
|
||||
// Check whether the avatar file is valid.
|
||||
if (tmpFileState.get())
|
||||
{
|
||||
if (tmpFileState.get()) {
|
||||
tmpFileState.reset();
|
||||
|
||||
MD5Buf md5buf;
|
||||
if (CryptHelper::MD5Sum(tmpFileName, md5buf))
|
||||
{
|
||||
if (CryptHelper::MD5Sum(tmpFileName, md5buf)) {
|
||||
boost::mutex::scoped_lock lock(m_avatarsMutex);
|
||||
m_avatars.insert(AvatarMap::value_type(md5buf, tmpFileName));
|
||||
retVal = true;
|
||||
@@ -133,13 +128,11 @@ AvatarManager::OpenAvatarFileForChunkRead(const std::string &fileName, unsigned
|
||||
{
|
||||
outFileSize = 0;
|
||||
boost::shared_ptr<AvatarFileState> retVal;
|
||||
try
|
||||
{
|
||||
try {
|
||||
outFileType = GetAvatarFileType(fileName);
|
||||
boost::shared_ptr<AvatarFileState> fileState(new AvatarFileState);
|
||||
fileState->inputStream.open(fileName.c_str(), ios_base::in | ios_base::binary);
|
||||
if (!fileState->inputStream.fail())
|
||||
{
|
||||
if (!fileState->inputStream.fail()) {
|
||||
// Find out file size.
|
||||
// Not fully portable, but works on win/linux/mac.
|
||||
fileState->inputStream.seekg(0, ios_base::beg);
|
||||
@@ -149,8 +142,7 @@ AvatarManager::OpenAvatarFileForChunkRead(const std::string &fileName, unsigned
|
||||
fileState->inputStream.seekg(0, ios_base::beg);
|
||||
std::streamoff posDiff(endPos - startPos);
|
||||
outFileSize = (unsigned)posDiff;
|
||||
if (outFileSize >= MIN_AVATAR_FILE_SIZE && outFileSize <= MAX_AVATAR_FILE_SIZE)
|
||||
{
|
||||
if (outFileSize >= MIN_AVATAR_FILE_SIZE && outFileSize <= MAX_AVATAR_FILE_SIZE) {
|
||||
// Validate type of file by verifying image header.
|
||||
unsigned char fileHeader[MAX_HEADER_SIZE];
|
||||
fileState->inputStream.read((char *)fileHeader, sizeof(fileHeader));
|
||||
@@ -160,8 +152,7 @@ AvatarManager::OpenAvatarFileForChunkRead(const std::string &fileName, unsigned
|
||||
retVal = fileState;
|
||||
}
|
||||
}
|
||||
} catch (...)
|
||||
{
|
||||
} catch (...) {
|
||||
LOG_ERROR("Exception caught when trying to open avatar.");
|
||||
}
|
||||
return retVal;
|
||||
@@ -171,17 +162,13 @@ unsigned
|
||||
AvatarManager::ChunkReadAvatarFile(boost::shared_ptr<AvatarFileState> fileState, unsigned char *data, unsigned chunkSize)
|
||||
{
|
||||
unsigned retVal = 0;
|
||||
if (fileState.get())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!fileState->inputStream.fail() && !fileState->inputStream.eof())
|
||||
{
|
||||
if (fileState.get()) {
|
||||
try {
|
||||
if (!fileState->inputStream.fail() && !fileState->inputStream.eof()) {
|
||||
fileState->inputStream.read((char *)data, chunkSize);
|
||||
retVal = fileState->inputStream.gcount();
|
||||
}
|
||||
} catch (...)
|
||||
{
|
||||
} catch (...) {
|
||||
LOG_ERROR("Exception caught when trying to read avatar.");
|
||||
}
|
||||
}
|
||||
@@ -195,8 +182,7 @@ AvatarManager::AvatarFileToNetPackets(const string &fileName, unsigned requestId
|
||||
unsigned fileSize = 0;
|
||||
AvatarFileType fileType;
|
||||
boost::shared_ptr<AvatarFileState> tmpState = OpenAvatarFileForChunkRead(fileName, fileSize, fileType);
|
||||
if (tmpState.get() && fileSize && fileType != AVATAR_FILE_TYPE_UNKNOWN)
|
||||
{
|
||||
if (tmpState.get() && fileSize && fileType != AVATAR_FILE_TYPE_UNKNOWN) {
|
||||
boost::shared_ptr<NetPacket> avatarHeader(new NetPacket(NetPacket::Alloc));
|
||||
avatarHeader->GetMsg()->present = PokerTHMessage_PR_avatarReplyMessage;
|
||||
AvatarReplyMessage_t *netHeader = &avatarHeader->GetMsg()->choice.avatarReplyMessage;
|
||||
@@ -209,11 +195,9 @@ AvatarManager::AvatarFileToNetPackets(const string &fileName, unsigned requestId
|
||||
unsigned numBytes = 0;
|
||||
unsigned totalBytesRead = 0;
|
||||
vector<unsigned char> tmpData(MAX_FILE_DATA_SIZE);
|
||||
do
|
||||
{
|
||||
do {
|
||||
numBytes = ChunkReadAvatarFile(tmpState, &tmpData[0], MAX_FILE_DATA_SIZE);
|
||||
if (numBytes)
|
||||
{
|
||||
if (numBytes) {
|
||||
totalBytesRead += numBytes;
|
||||
|
||||
boost::shared_ptr<NetPacket> avatarFile(new NetPacket(NetPacket::Alloc));
|
||||
@@ -231,8 +215,7 @@ AvatarManager::AvatarFileToNetPackets(const string &fileName, unsigned requestId
|
||||
|
||||
if (fileSize != totalBytesRead)
|
||||
retVal = ERR_NET_WRONG_AVATAR_SIZE;
|
||||
else
|
||||
{
|
||||
else {
|
||||
boost::shared_ptr<NetPacket> avatarEnd(new NetPacket(NetPacket::Alloc));
|
||||
avatarEnd->GetMsg()->present = PokerTHMessage_PR_avatarReplyMessage;
|
||||
AvatarReplyMessage_t *netEnd = &avatarEnd->GetMsg()->choice.avatarReplyMessage;
|
||||
@@ -268,19 +251,18 @@ string
|
||||
AvatarManager::GetAvatarFileExtension(AvatarFileType fileType)
|
||||
{
|
||||
string ext;
|
||||
switch (fileType)
|
||||
{
|
||||
case AVATAR_FILE_TYPE_PNG:
|
||||
ext = ".png";
|
||||
break;
|
||||
case AVATAR_FILE_TYPE_JPG:
|
||||
ext = ".jpg";
|
||||
break;
|
||||
case AVATAR_FILE_TYPE_GIF:
|
||||
ext = ".gif";
|
||||
break;
|
||||
case AVATAR_FILE_TYPE_UNKNOWN:
|
||||
break;
|
||||
switch (fileType) {
|
||||
case AVATAR_FILE_TYPE_PNG:
|
||||
ext = ".png";
|
||||
break;
|
||||
case AVATAR_FILE_TYPE_JPG:
|
||||
ext = ".jpg";
|
||||
break;
|
||||
case AVATAR_FILE_TYPE_GIF:
|
||||
ext = ".gif";
|
||||
break;
|
||||
case AVATAR_FILE_TYPE_UNKNOWN:
|
||||
break;
|
||||
}
|
||||
return ext;
|
||||
}
|
||||
@@ -289,17 +271,14 @@ bool
|
||||
AvatarManager::GetHashForAvatar(const std::string &fileName, MD5Buf &md5buf) const
|
||||
{
|
||||
bool found = false;
|
||||
if (exists(fileName))
|
||||
{
|
||||
if (exists(fileName)) {
|
||||
// Scan default avatars first.
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_avatarsMutex);
|
||||
AvatarMap::const_iterator i = m_avatars.begin();
|
||||
AvatarMap::const_iterator end = m_avatars.end();
|
||||
while (i != end)
|
||||
{
|
||||
if (i->second == fileName)
|
||||
{
|
||||
while (i != end) {
|
||||
if (i->second == fileName) {
|
||||
md5buf = i->first;
|
||||
found = true;
|
||||
break;
|
||||
@@ -308,15 +287,12 @@ AvatarManager::GetHashForAvatar(const std::string &fileName, MD5Buf &md5buf) con
|
||||
}
|
||||
}
|
||||
// Check cached avatars next.
|
||||
if (!found)
|
||||
{
|
||||
if (!found) {
|
||||
boost::mutex::scoped_lock lock(m_cachedAvatarsMutex);
|
||||
AvatarMap::const_iterator i = m_cachedAvatars.begin();
|
||||
AvatarMap::const_iterator end = m_cachedAvatars.end();
|
||||
while (i != end)
|
||||
{
|
||||
if (i->second == fileName)
|
||||
{
|
||||
while (i != end) {
|
||||
if (i->second == fileName) {
|
||||
md5buf = i->first;
|
||||
found = true;
|
||||
break;
|
||||
@@ -326,8 +302,7 @@ AvatarManager::GetHashForAvatar(const std::string &fileName, MD5Buf &md5buf) con
|
||||
}
|
||||
|
||||
// Calculate md5 sum if not found.
|
||||
if (!found)
|
||||
{
|
||||
if (!found) {
|
||||
if (CryptHelper::MD5Sum(fileName, md5buf))
|
||||
found = true;
|
||||
}
|
||||
@@ -342,18 +317,15 @@ AvatarManager::GetAvatarFileName(const MD5Buf &md5buf, std::string &fileName) co
|
||||
{
|
||||
boost::mutex::scoped_lock lock(m_avatarsMutex);
|
||||
AvatarMap::const_iterator pos = m_avatars.find(md5buf);
|
||||
if (pos != m_avatars.end())
|
||||
{
|
||||
if (pos != m_avatars.end()) {
|
||||
fileName = pos->second;
|
||||
retVal = true;
|
||||
}
|
||||
}
|
||||
if (!retVal)
|
||||
{
|
||||
if (!retVal) {
|
||||
boost::mutex::scoped_lock lock(m_cachedAvatarsMutex);
|
||||
AvatarMap::const_iterator pos = m_cachedAvatars.find(md5buf);
|
||||
if (pos != m_cachedAvatars.end())
|
||||
{
|
||||
if (pos != m_cachedAvatars.end()) {
|
||||
fileName = pos->second;
|
||||
retVal = true;
|
||||
}
|
||||
@@ -377,24 +349,19 @@ AvatarManager::StoreAvatarInCache(const MD5Buf &md5buf, AvatarFileType avatarFil
|
||||
boost::mutex::scoped_lock lock(m_cacheDirMutex);
|
||||
cacheDir = m_cacheDir;
|
||||
}
|
||||
try
|
||||
{
|
||||
try {
|
||||
string ext(GetAvatarFileExtension(avatarFileType));
|
||||
if (!ext.empty() && !cacheDir.empty())
|
||||
{
|
||||
if (!ext.empty() && !cacheDir.empty()) {
|
||||
// Check header before storing file.
|
||||
if (IsValidAvatarFileType(avatarFileType, data, size))
|
||||
{
|
||||
if (IsValidAvatarFileType(avatarFileType, data, size)) {
|
||||
path tmpPath(cacheDir);
|
||||
tmpPath /= (md5buf.ToString() + ext);
|
||||
string fileName(tmpPath.file_string());
|
||||
ofstream o(fileName.c_str(), ios_base::out | ios_base::binary | ios_base::trunc);
|
||||
if (!o.fail())
|
||||
{
|
||||
if (!o.fail()) {
|
||||
o.write((const char *)data, size);
|
||||
o.close();
|
||||
if (upload && m_useExternalServer)
|
||||
{
|
||||
if (upload && m_useExternalServer) {
|
||||
m_uploader->QueueUpload(m_externalServerAddress, m_externalServerUser, m_externalServerPassword, fileName, size);
|
||||
}
|
||||
|
||||
@@ -406,8 +373,7 @@ AvatarManager::StoreAvatarInCache(const MD5Buf &md5buf, AvatarFileType avatarFil
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (...)
|
||||
{
|
||||
} catch (...) {
|
||||
LOG_ERROR("Exception caught when trying to store avatar.");
|
||||
}
|
||||
return retVal;
|
||||
@@ -418,32 +384,28 @@ AvatarManager::IsValidAvatarFileType(AvatarFileType avatarFileType, const unsign
|
||||
{
|
||||
bool validType = false;
|
||||
|
||||
switch (avatarFileType)
|
||||
{
|
||||
case AVATAR_FILE_TYPE_PNG:
|
||||
if (fileHeaderSize >= PNG_HEADER_SIZE
|
||||
&& memcmp(fileHeader, PNG_HEADER, PNG_HEADER_SIZE) == 0)
|
||||
{
|
||||
validType = true;
|
||||
}
|
||||
break;
|
||||
case AVATAR_FILE_TYPE_JPG:
|
||||
if (fileHeaderSize >= JPG_HEADER_SIZE
|
||||
&& memcmp(fileHeader, JPG_HEADER, JPG_HEADER_SIZE) == 0)
|
||||
{
|
||||
validType = true;
|
||||
}
|
||||
break;
|
||||
case AVATAR_FILE_TYPE_GIF:
|
||||
if (fileHeaderSize >= GIF_HEADER_SIZE
|
||||
switch (avatarFileType) {
|
||||
case AVATAR_FILE_TYPE_PNG:
|
||||
if (fileHeaderSize >= PNG_HEADER_SIZE
|
||||
&& memcmp(fileHeader, PNG_HEADER, PNG_HEADER_SIZE) == 0) {
|
||||
validType = true;
|
||||
}
|
||||
break;
|
||||
case AVATAR_FILE_TYPE_JPG:
|
||||
if (fileHeaderSize >= JPG_HEADER_SIZE
|
||||
&& memcmp(fileHeader, JPG_HEADER, JPG_HEADER_SIZE) == 0) {
|
||||
validType = true;
|
||||
}
|
||||
break;
|
||||
case AVATAR_FILE_TYPE_GIF:
|
||||
if (fileHeaderSize >= GIF_HEADER_SIZE
|
||||
&& (memcmp(fileHeader, GIF_HEADER_1, GIF_HEADER_SIZE) == 0
|
||||
|| memcmp(fileHeader, GIF_HEADER_2, GIF_HEADER_SIZE) == 0))
|
||||
{
|
||||
validType = true;
|
||||
}
|
||||
break;
|
||||
case AVATAR_FILE_TYPE_UNKNOWN:
|
||||
break;
|
||||
|| memcmp(fileHeader, GIF_HEADER_2, GIF_HEADER_SIZE) == 0)) {
|
||||
validType = true;
|
||||
}
|
||||
break;
|
||||
case AVATAR_FILE_TYPE_UNKNOWN:
|
||||
break;
|
||||
}
|
||||
return validType;
|
||||
}
|
||||
@@ -456,13 +418,11 @@ AvatarManager::RemoveOldAvatarCacheEntries()
|
||||
boost::mutex::scoped_lock lock(m_cacheDirMutex);
|
||||
cacheDir = m_cacheDir;
|
||||
}
|
||||
try
|
||||
{
|
||||
try {
|
||||
path cachePath(cacheDir);
|
||||
cacheDir = cachePath.directory_string();
|
||||
// Never delete anything if we do not have a special cache dir set.
|
||||
if (!cacheDir.empty())
|
||||
{
|
||||
if (!cacheDir.empty()) {
|
||||
boost::mutex::scoped_lock lock(m_cachedAvatarsMutex);
|
||||
|
||||
// First pass: Remove files which no longer exist.
|
||||
@@ -473,18 +433,15 @@ AvatarManager::RemoveOldAvatarCacheEntries()
|
||||
{
|
||||
AvatarMap::const_iterator i = m_cachedAvatars.begin();
|
||||
AvatarMap::const_iterator end = m_cachedAvatars.end();
|
||||
while (i != end)
|
||||
{
|
||||
while (i != end) {
|
||||
bool keepFile = false;
|
||||
path filePath(i->second);
|
||||
string fileString(filePath.file_string());
|
||||
// Only consider files which are definitely in the cache dir.
|
||||
if (fileString.size() > cacheDir.size() && fileString.substr(0, cacheDir.size()) == cacheDir)
|
||||
{
|
||||
if (fileString.size() > cacheDir.size() && fileString.substr(0, cacheDir.size()) == cacheDir) {
|
||||
// Only consider files with MD5 as file name.
|
||||
MD5Buf tmpBuf;
|
||||
if (exists(filePath) && tmpBuf.FromString(basename(filePath)))
|
||||
{
|
||||
if (exists(filePath) && tmpBuf.FromString(basename(filePath))) {
|
||||
++fileCount;
|
||||
timeMap.insert(TimeAvatarMap::value_type(last_write_time(filePath), i->first));
|
||||
keepFile = true;
|
||||
@@ -500,8 +457,7 @@ AvatarManager::RemoveOldAvatarCacheEntries()
|
||||
{
|
||||
AvatarList::const_iterator i = removeList.begin();
|
||||
AvatarList::const_iterator end = removeList.end();
|
||||
while (i != end)
|
||||
{
|
||||
while (i != end) {
|
||||
m_cachedAvatars.erase(*i);
|
||||
++i;
|
||||
}
|
||||
@@ -514,14 +470,11 @@ AvatarManager::RemoveOldAvatarCacheEntries()
|
||||
// - delete until only MAX_NUMBER_OF_FILES/2 are left.
|
||||
// 2. Files are older than 30 days.
|
||||
|
||||
if (m_cachedAvatars.size() > MAX_NUMBER_OF_FILES)
|
||||
{
|
||||
while (!timeMap.empty() && m_cachedAvatars.size() > MAX_NUMBER_OF_FILES / 2)
|
||||
{
|
||||
if (m_cachedAvatars.size() > MAX_NUMBER_OF_FILES) {
|
||||
while (!timeMap.empty() && m_cachedAvatars.size() > MAX_NUMBER_OF_FILES / 2) {
|
||||
TimeAvatarMap::iterator i = timeMap.begin();
|
||||
AvatarMap::iterator pos = m_cachedAvatars.find(i->second);
|
||||
if (pos != m_cachedAvatars.end())
|
||||
{
|
||||
if (pos != m_cachedAvatars.end()) {
|
||||
path tmpPath(pos->second);
|
||||
remove(tmpPath);
|
||||
m_cachedAvatars.erase(pos);
|
||||
@@ -532,14 +485,12 @@ AvatarManager::RemoveOldAvatarCacheEntries()
|
||||
|
||||
// Get reference time.
|
||||
time_t curTime = time(NULL);
|
||||
while (!timeMap.empty() && !m_cachedAvatars.empty())
|
||||
{
|
||||
while (!timeMap.empty() && !m_cachedAvatars.empty()) {
|
||||
TimeAvatarMap::iterator i = timeMap.begin();
|
||||
if (curTime - i->first < (int)MAX_AVATAR_CACHE_AGE)
|
||||
break;
|
||||
AvatarMap::iterator pos = m_cachedAvatars.find(i->second);
|
||||
if (pos != m_cachedAvatars.end())
|
||||
{
|
||||
if (pos != m_cachedAvatars.end()) {
|
||||
path tmpPath(pos->second);
|
||||
remove(tmpPath);
|
||||
m_cachedAvatars.erase(pos);
|
||||
@@ -547,8 +498,7 @@ AvatarManager::RemoveOldAvatarCacheEntries()
|
||||
timeMap.erase(i);
|
||||
}
|
||||
}
|
||||
} catch (...)
|
||||
{
|
||||
} catch (...) {
|
||||
LOG_ERROR("Exception caught while cleaning up cache.");
|
||||
}
|
||||
}
|
||||
@@ -559,37 +509,29 @@ AvatarManager::InternalReadDirectory(const std::string &dir, AvatarMap &avatars)
|
||||
bool retVal = true;
|
||||
path tmpPath(dir);
|
||||
|
||||
if (exists(tmpPath) && is_directory(tmpPath))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (exists(tmpPath) && is_directory(tmpPath)) {
|
||||
try {
|
||||
// This method is not thread safe. Only call after locking the map.
|
||||
directory_iterator i(tmpPath);
|
||||
directory_iterator end;
|
||||
|
||||
while (i != end)
|
||||
{
|
||||
if (is_regular(i->status()))
|
||||
{
|
||||
while (i != end) {
|
||||
if (is_regular(i->status())) {
|
||||
string md5sum(basename(i->path()));
|
||||
MD5Buf md5buf;
|
||||
string fileName(i->path().file_string());
|
||||
if (md5buf.FromString(md5sum))
|
||||
{
|
||||
if (md5buf.FromString(md5sum)) {
|
||||
// Only consider files with md5sum as name.
|
||||
avatars.insert(AvatarMap::value_type(md5buf, fileName));
|
||||
}
|
||||
}
|
||||
++i;
|
||||
}
|
||||
} catch (...)
|
||||
{
|
||||
} catch (...) {
|
||||
LOG_ERROR("Exception caught when trying to scan avatar directory.");
|
||||
retVal = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
LOG_ERROR("Avatar directory does not exist.");
|
||||
retVal = false;
|
||||
}
|
||||
|
||||
@@ -53,8 +53,7 @@ HashBuf::ToString() const
|
||||
char tmpBuf[2 + 1];
|
||||
tmpBuf[sizeof(tmpBuf) - 1] = 0;
|
||||
const unsigned char *tmpData = GetData();
|
||||
for (int i = 0; i < GetDataSize(); i++)
|
||||
{
|
||||
for (int i = 0; i < GetDataSize(); i++) {
|
||||
sprintf(tmpBuf, "%02x", tmpData[i]);
|
||||
retValue += tmpBuf;
|
||||
}
|
||||
@@ -67,8 +66,7 @@ HashBuf::FromString(const std::string &text)
|
||||
// Convert hex-based string to MD5 data.
|
||||
bool retVal = false;
|
||||
int tmpSize = GetDataSize();
|
||||
if (text.size() == 2 * (unsigned)tmpSize)
|
||||
{
|
||||
if (text.size() == 2 * (unsigned)tmpSize) {
|
||||
unsigned char *tmpData = GetData();
|
||||
const char *t = text.c_str();
|
||||
int i = 0;
|
||||
@@ -92,8 +90,7 @@ HashBuf::IsZero() const
|
||||
int dataSize = GetDataSize();
|
||||
const unsigned char *tmpData = GetData();
|
||||
int i;
|
||||
for (i = 0; i < dataSize; i++)
|
||||
{
|
||||
for (i = 0; i < dataSize; i++) {
|
||||
if (tmpData[i] != 0)
|
||||
break;
|
||||
}
|
||||
@@ -166,8 +163,7 @@ CryptHelper::MD5Sum(const std::string &fileName, MD5Buf &buf)
|
||||
bool retVal = false;
|
||||
FILE *file = fopen(fileName.c_str(), "rb");
|
||||
|
||||
if (file)
|
||||
{
|
||||
if (file) {
|
||||
// Calculate MD5 sum of file.
|
||||
unsigned char readBuf[8192];
|
||||
MD5_CTX context;
|
||||
@@ -211,15 +207,12 @@ CryptHelper::HMACSha1(const unsigned char *keyData, unsigned keySize, const unsi
|
||||
retVal = false;
|
||||
gcry_md_hd_t hd;
|
||||
gcry_error_t err = gcry_md_open(&hd, GCRY_MD_SHA1, GCRY_MD_FLAG_HMAC);
|
||||
if (!err)
|
||||
{
|
||||
if (!err) {
|
||||
err = gcry_md_setkey(hd, keyData, keySize);
|
||||
if (!err)
|
||||
{
|
||||
if (!err) {
|
||||
gcry_md_write(hd, plainData, plainSize);
|
||||
unsigned char *hash = gcry_md_read(hd, 0);
|
||||
if (hash)
|
||||
{
|
||||
if (hash) {
|
||||
memcpy(buf.GetData(), hash, buf.GetDataSize());
|
||||
retVal = true;
|
||||
}
|
||||
@@ -260,8 +253,7 @@ CryptHelper::AES128Encrypt(const unsigned char *keyData, unsigned keySize, const
|
||||
{
|
||||
bool retVal = false;
|
||||
unsigned plainSize = static_cast<unsigned>(plainStr.size());
|
||||
if (keySize && plainSize)
|
||||
{
|
||||
if (keySize && plainSize) {
|
||||
unsigned char key[AES_BLOCK_SIZE];
|
||||
unsigned char iv[AES_BLOCK_SIZE];
|
||||
BytesToKey(keyData, keySize, key, iv);
|
||||
@@ -281,24 +273,20 @@ CryptHelper::AES128Encrypt(const unsigned char *keyData, unsigned keySize, const
|
||||
|
||||
int success = EVP_EncryptInit(&encryptCtx, EVP_aes_128_cbc(), key, iv);
|
||||
EVP_CIPHER_CTX_set_padding(&encryptCtx, 0);
|
||||
if (success)
|
||||
{
|
||||
if (success) {
|
||||
success = EVP_EncryptUpdate(&encryptCtx, &outCipher[0], &outCipherSize, paddedPlainStr, paddedPlainSize);
|
||||
|
||||
if (success && outCipherSize)
|
||||
{
|
||||
if (success && outCipherSize) {
|
||||
// Since padding is off, this will not modify the cipher. However, parameters need to be set.
|
||||
EVP_EncryptFinal(&encryptCtx, &outCipher[0], &outCipherSize);
|
||||
retVal = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
} else
|
||||
outCipher.clear();
|
||||
#else
|
||||
gcry_cipher_hd_t hd;
|
||||
gcry_error_t err = gcry_cipher_open(&hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
|
||||
if (!err)
|
||||
{
|
||||
if (!err) {
|
||||
gcry_cipher_setkey(hd, key, sizeof(key));
|
||||
gcry_cipher_setiv(hd, iv, sizeof(iv));
|
||||
err = gcry_cipher_encrypt(hd, &outCipher[0], cipherSize, paddedPlainStr, paddedPlainSize);
|
||||
@@ -306,8 +294,7 @@ CryptHelper::AES128Encrypt(const unsigned char *keyData, unsigned keySize, const
|
||||
retVal = true;
|
||||
else
|
||||
outCipher.clear();
|
||||
}
|
||||
else
|
||||
} else
|
||||
outCipher.clear();
|
||||
|
||||
gcry_cipher_close(hd);
|
||||
@@ -320,8 +307,7 @@ bool
|
||||
CryptHelper::AES128Decrypt(const unsigned char *keyData, unsigned keySize, const unsigned char *cipher, unsigned cipherSize, string &outPlain)
|
||||
{
|
||||
bool retVal = false;
|
||||
if (keySize && cipherSize)
|
||||
{
|
||||
if (keySize && cipherSize) {
|
||||
unsigned char key[AES_BLOCK_SIZE];
|
||||
unsigned char iv[AES_BLOCK_SIZE];
|
||||
BytesToKey(keyData, keySize, key, iv);
|
||||
@@ -333,24 +319,20 @@ CryptHelper::AES128Decrypt(const unsigned char *keyData, unsigned keySize, const
|
||||
|
||||
int success = EVP_DecryptInit(&decryptCtx, EVP_aes_128_cbc(), key, iv);
|
||||
EVP_CIPHER_CTX_set_padding(&decryptCtx, 0);
|
||||
if (success)
|
||||
{
|
||||
if (success) {
|
||||
success = EVP_DecryptUpdate(&decryptCtx, (unsigned char *)&outPlain[0], &outPlainSize, cipher, cipherSize);
|
||||
|
||||
if (success && outPlainSize)
|
||||
{
|
||||
if (success && outPlainSize) {
|
||||
// Since padding is off, this will not modify the plain text. However, parameters need to be set.
|
||||
EVP_DecryptFinal(&decryptCtx, (unsigned char *)outPlain.c_str(), &outPlainSize);
|
||||
retVal = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
} else
|
||||
outPlain.clear();
|
||||
#else
|
||||
gcry_cipher_hd_t hd;
|
||||
gcry_error_t err = gcry_cipher_open(&hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC, 0);
|
||||
if (!err)
|
||||
{
|
||||
if (!err) {
|
||||
gcry_cipher_setkey(hd, key, sizeof(key));
|
||||
gcry_cipher_setiv(hd, iv, sizeof(iv));
|
||||
err = gcry_cipher_decrypt(hd, &outPlain[0], outPlain.size(), cipher, cipherSize);
|
||||
@@ -358,15 +340,13 @@ CryptHelper::AES128Decrypt(const unsigned char *keyData, unsigned keySize, const
|
||||
retVal = true;
|
||||
else
|
||||
outPlain.clear();
|
||||
}
|
||||
else
|
||||
} else
|
||||
outPlain.clear();
|
||||
|
||||
gcry_cipher_close(hd);
|
||||
#endif
|
||||
// Remove trailing zeroes (padding).
|
||||
if (!outPlain.empty())
|
||||
{
|
||||
if (!outPlain.empty()) {
|
||||
size_t pos = outPlain.find_first_of('\0');
|
||||
if (pos != string::npos)
|
||||
outPlain = outPlain.substr(0, pos);
|
||||
|
||||
@@ -54,11 +54,9 @@ loghelper_init(const string &logDir, int logLevel)
|
||||
void
|
||||
internal_log_err(const string &msg)
|
||||
{
|
||||
if (!g_logFile.empty())
|
||||
{
|
||||
if (!g_logFile.empty()) {
|
||||
ofstream o(g_logFile.c_str(), ios_base::out | ios_base::app);
|
||||
if (!o.fail())
|
||||
{
|
||||
if (!o.fail()) {
|
||||
o << second_clock::local_time() << " ERR: " << msg;
|
||||
o.flush();
|
||||
}
|
||||
@@ -68,10 +66,8 @@ internal_log_err(const string &msg)
|
||||
void
|
||||
internal_log_msg(const std::string &msg)
|
||||
{
|
||||
if (g_logLevel)
|
||||
{
|
||||
if (!g_logFile.empty())
|
||||
{
|
||||
if (g_logLevel) {
|
||||
if (!g_logFile.empty()) {
|
||||
ofstream o(g_logFile.c_str(), ios_base::out | ios_base::app);
|
||||
if (!o.fail())
|
||||
o << second_clock::local_time() << " MSG: " << msg;
|
||||
@@ -82,10 +78,8 @@ internal_log_msg(const std::string &msg)
|
||||
void
|
||||
internal_log_level(const std::string &msg, int logLevel)
|
||||
{
|
||||
if (g_logLevel >= logLevel)
|
||||
{
|
||||
if (!g_logFile.empty())
|
||||
{
|
||||
if (g_logLevel >= logLevel) {
|
||||
if (!g_logFile.empty()) {
|
||||
ofstream o(g_logFile.c_str(), ios_base::out | ios_base::app);
|
||||
if (!o.fail())
|
||||
o << second_clock::local_time() << " OUT: " << msg;
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
using namespace std;
|
||||
|
||||
PokerTHException::PokerTHException(const char *sourcefile, int sourceline, int errorId, int osErrorCode)
|
||||
: m_errorId(errorId), m_osErrorCode(osErrorCode)
|
||||
: m_errorId(errorId), m_osErrorCode(osErrorCode)
|
||||
{
|
||||
ostringstream msgStream;
|
||||
msgStream << sourcefile << " (" << sourceline << "): Error " << errorId;
|
||||
|
||||
@@ -26,8 +26,7 @@ inline void ADD_MSEC_TO_XTIME(boost::xtime &xt, unsigned msec)
|
||||
{
|
||||
xt.sec += msec / 1000;
|
||||
xt.nsec += (msec % 1000) * 1000000;
|
||||
if (xt.nsec > NANOSECONDS_PER_SECOND)
|
||||
{
|
||||
if (xt.nsec > NANOSECONDS_PER_SECOND) {
|
||||
xt.sec++;
|
||||
xt.nsec -= NANOSECONDS_PER_SECOND;
|
||||
}
|
||||
@@ -39,8 +38,7 @@ class ThreadStarter
|
||||
{
|
||||
public:
|
||||
ThreadStarter(Thread &thread) : m_thread(thread) {}
|
||||
void operator()()
|
||||
{
|
||||
void operator()() {
|
||||
m_thread.MainWrapper();
|
||||
}
|
||||
|
||||
@@ -62,8 +60,7 @@ Thread::Run()
|
||||
boost::mutex::scoped_lock threadLock(m_threadObjMutex);
|
||||
|
||||
// Create the boost thread object.
|
||||
if (!m_threadObj.get())
|
||||
{
|
||||
if (!m_threadObj.get()) {
|
||||
// Initialise data structures within the context of the thread
|
||||
// who runs/terminates this thread.
|
||||
m_userReqTerminateLock.reset(new boost::timed_mutex::scoped_try_lock(m_shouldTerminateMutex));
|
||||
@@ -89,14 +86,11 @@ Thread::Join(unsigned msecTimeout)
|
||||
return true;
|
||||
|
||||
bool tmpIsTerminated;
|
||||
if (msecTimeout == THREAD_WAIT_INFINITE)
|
||||
{
|
||||
if (msecTimeout == THREAD_WAIT_INFINITE) {
|
||||
// Wait infinitely.
|
||||
boost::timed_mutex::scoped_lock lock(m_isTerminatedMutex);
|
||||
tmpIsTerminated = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
// Wait for the termination of the application code.
|
||||
#if (BOOST_VERSION) >= 103500
|
||||
boost::defer_lock_t defer;
|
||||
@@ -113,12 +107,10 @@ Thread::Join(unsigned msecTimeout)
|
||||
#endif
|
||||
}
|
||||
|
||||
if (tmpIsTerminated)
|
||||
{
|
||||
if (tmpIsTerminated) {
|
||||
boost::mutex::scoped_lock lock(m_threadObjMutex);
|
||||
// Wait for "real" termination of the thread.
|
||||
if (m_threadObj.get())
|
||||
{
|
||||
if (m_threadObj.get()) {
|
||||
m_threadObj->join();
|
||||
m_threadObj.reset();
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
#endif
|
||||
|
||||
#if defined(__FreeBSD__) || defined(__NetBSD__)
|
||||
#define HAVE_ICONV_CONST
|
||||
#define HAVE_ICONV_CONST
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
@@ -57,8 +57,7 @@ ConvHelper::NativeToUtf8(const std::string &inStr)
|
||||
|
||||
if (conversion == (iconv_t)(-1))
|
||||
LOG_ERROR("iconv_open() failed: " << strerror(errno));
|
||||
else
|
||||
{
|
||||
else {
|
||||
size_t retval = iconv(conversion, &inbuf, &insize, &outbuf, &outsize);
|
||||
|
||||
if (retval == (size_t)-1)
|
||||
@@ -91,8 +90,7 @@ ConvHelper::Utf8ToNative(const std::string &inStr)
|
||||
|
||||
if (conversion == (iconv_t)(-1))
|
||||
LOG_ERROR("iconv_open() failed: " << strerror(errno));
|
||||
else
|
||||
{
|
||||
else {
|
||||
size_t retval = iconv(conversion, &inbuf, &insize, &outbuf, &outsize);
|
||||
|
||||
if (retval == (size_t)-1)
|
||||
|
||||
+28
-28
@@ -25,28 +25,28 @@
|
||||
#define HAVE_SSIZE_T
|
||||
#include <sys/types.h>
|
||||
#ifdef _WIN64 // This is only for Windows.
|
||||
#ifndef _SSIZE_T_
|
||||
typedef long long ssize_t;
|
||||
#define _SSIZE_T_
|
||||
#endif
|
||||
#ifndef _PID_T_
|
||||
typedef __int64 pid_t;
|
||||
#define _PID_T_
|
||||
#endif
|
||||
#ifndef _SSIZE_T_
|
||||
typedef long long ssize_t;
|
||||
#define _SSIZE_T_
|
||||
#endif
|
||||
#ifndef _PID_T_
|
||||
typedef __int64 pid_t;
|
||||
#define _PID_T_
|
||||
#endif
|
||||
#elif _WIN32
|
||||
#ifndef _SSIZE_T_
|
||||
typedef int ssize_t;
|
||||
#define _SSIZE_T_
|
||||
#endif
|
||||
#ifndef _PID_T_
|
||||
typedef int pid_t;
|
||||
#define _PID_T_
|
||||
#endif
|
||||
#ifndef _SSIZE_T_
|
||||
typedef int ssize_t;
|
||||
#define _SSIZE_T_
|
||||
#endif
|
||||
#ifndef _PID_T_
|
||||
typedef int pid_t;
|
||||
#define _PID_T_
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef X509_NAME
|
||||
#undef X509_NAME // Again for Windows - conflict with WinCrypt.h.
|
||||
#undef X509_NAME // Again for Windows - conflict with WinCrypt.h.
|
||||
#endif
|
||||
|
||||
// For BSD-Systems, we assume that OpenSSL is part of the operating system.
|
||||
@@ -56,21 +56,21 @@
|
||||
// http://www.openssl.org/support/faq.html#LEGAL2
|
||||
//
|
||||
#if defined(_WIN64) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
|
||||
#define HAVE_OPENSSL
|
||||
#define HAVE_OPENSSL
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_OPENSSL
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/md5.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/evp.h>
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/md5.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <openssl/hmac.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/evp.h>
|
||||
#else
|
||||
// For all other systems, we use GnuTLS.
|
||||
#include <gnutls/openssl.h>
|
||||
#include <gcrypt.h>
|
||||
#define PKTH_USE_GNUTLS
|
||||
// For all other systems, we use GnuTLS.
|
||||
#include <gnutls/openssl.h>
|
||||
#include <gcrypt.h>
|
||||
#define PKTH_USE_GNUTLS
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -31,8 +31,12 @@ public:
|
||||
PokerTHException(const char *sourcefile, int sourceline, int errorId, int osErrorCode);
|
||||
virtual ~PokerTHException() throw();
|
||||
|
||||
int GetErrorId() const {return m_errorId;}
|
||||
int GetOsErrorCode() const {return m_osErrorCode;}
|
||||
int GetErrorId() const {
|
||||
return m_errorId;
|
||||
}
|
||||
int GetOsErrorCode() const {
|
||||
return m_osErrorCode;
|
||||
}
|
||||
|
||||
virtual const char *what() const throw();
|
||||
|
||||
|
||||
+1
-1
@@ -82,7 +82,7 @@ private:
|
||||
|
||||
mutable boost::shared_ptr<boost::barrier> m_threadStartBarrier;
|
||||
|
||||
friend class ThreadStarter;
|
||||
friend class ThreadStarter;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -33,22 +33,18 @@ Convert(const std::string &inStr, int fromCP, int toCP)
|
||||
// convert str from current Windows source to target charset
|
||||
string retStr(inStr);
|
||||
|
||||
if (!inStr.empty())
|
||||
{
|
||||
if (!inStr.empty()) {
|
||||
int len = (int)inStr.length() + 1;
|
||||
int reqLen = ::MultiByteToWideChar(fromCP, 0, inStr.c_str(), len, NULL, 0);
|
||||
|
||||
if (reqLen)
|
||||
{
|
||||
if (reqLen) {
|
||||
wchar_t *wstr = new wchar_t[reqLen];
|
||||
wstr[0] = L'\0';
|
||||
if (::MultiByteToWideChar(fromCP, 0, inStr.c_str(), len, wstr, reqLen) == (int)reqLen)
|
||||
{
|
||||
if (::MultiByteToWideChar(fromCP, 0, inStr.c_str(), len, wstr, reqLen) == (int)reqLen) {
|
||||
len = reqLen;
|
||||
reqLen = ::WideCharToMultiByte(toCP, 0, wstr, len, NULL, 0, NULL, NULL);
|
||||
|
||||
if (reqLen)
|
||||
{
|
||||
if (reqLen) {
|
||||
char *str = new char[reqLen];
|
||||
if (::WideCharToMultiByte(toCP, 0, wstr, len, str, reqLen, NULL, NULL) == (int)reqLen)
|
||||
retStr = str;
|
||||
|
||||
Reference in New Issue
Block a user