1
0
forked from 0ad/0ad

Unify duplicate u8* to hex string functions in a new Hexify function variant, similar to bb1f86f515, used by Tests, CacheLoader, Terms and Conditions, Lobby and mod.io.

Removes the call to the sprintf_s function from test_MD5.h in 17718981cf
and JSInterface_Main.cpp in 468d963e78.
As reported by Vladislav that function might not null-terminate strings
on untested/newer platforms, but the caller requires it here.
The sprintf_s calls in other places have the same problem.

Differential Revision: https://code.wildfiregames.com/D1591
Accepted By: Vladislav
This was SVN commit r21863.
This commit is contained in:
elexis 2018-08-08 12:59:05 +00:00
parent 1e59db453a
commit ecce63628c
7 changed files with 34 additions and 36 deletions

View File

@ -23,6 +23,7 @@
#include "lib/utf8.h"
#include "lobby/IXmppClient.h"
#include "ps/Profile.h"
#include "ps/Util.h"
#include "scriptinterface/ScriptInterface.h"
#include "third_party/encryption/pkcs5_pbkdf2.h"
@ -358,14 +359,7 @@ std::string JSI_Lobby::EncryptPassword(const std::string& password, const std::s
unsigned char encrypted[DIGESTSIZE];
pbkdf2(encrypted, (unsigned char*)password.c_str(), password.length(), salt_buffer, DIGESTSIZE, ITERATIONS);
static const char base16[] = "0123456789ABCDEF";
char hex[2 * DIGESTSIZE];
for (int i = 0; i < DIGESTSIZE; ++i)
{
hex[i*2] = base16[encrypted[i] >> 4]; // 4 high bits
hex[i*2 + 1] = base16[encrypted[i] & 0x0F]; // 4 low bits
}
return std::string(hex, sizeof(hex));
return Hexify(encrypted, DIGESTSIZE);
}
std::wstring JSI_Lobby::EncryptPassword(ScriptInterface::CxPrivate* UNUSED(pCxPrivate), const std::wstring& pass, const std::wstring& user)

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2010 Wildfire Games.
/* Copyright (C) 2018 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -18,16 +18,14 @@
#include "lib/self_test.h"
#include "maths/MD5.h"
#include "ps/Util.h"
class TestMD5 : public CxxTest::TestSuite
{
public:
std::string decode(u8* digest)
{
char digeststr[MD5::DIGESTSIZE*2+1];
for (size_t i = 0; i < MD5::DIGESTSIZE; ++i)
sprintf_s(digeststr+2*i, 3, "%02x", (unsigned int)digest[i]);
return digeststr;
return Hexify(digest, MD5::DIGESTSIZE);
}
void compare(const char* input, const char* expected)

View File

@ -19,8 +19,9 @@
#include "CacheLoader.h"
#include "ps/CLogger.h"
#include "maths/MD5.h"
#include "ps/CLogger.h"
#include "ps/Util.h"
#include <iomanip>
@ -134,19 +135,18 @@ VfsPath CCacheLoader::LooseCachePath(const VfsPath& sourcePath, const MD5& initi
hash.Update((const u8*)&version, sizeof(version));
// these are local cached files, so we don't care about endianness etc
// Use a short prefix of the full hash (we don't need high collision-resistance),
// converted to hex
u8 digest[MD5::DIGESTSIZE];
hash.Final(digest);
std::wstringstream digestPrefix;
digestPrefix << std::hex;
for (size_t i = 0; i < 8; ++i)
digestPrefix << std::setfill(L'0') << std::setw(2) << (int)digest[i];
// Get the mod path
OsPath path;
m_VFS->GetRealPath(sourcePath, path);
// Construct the final path
return VfsPath("cache") / path_name_only(path.BeforeCommon(sourcePath).Parent().string().c_str()) / sourcePath.ChangeExtension(sourcePath.Extension().string() + L"." + digestPrefix.str() + m_FileExtension);
return VfsPath("cache") /
path_name_only(path.BeforeCommon(sourcePath).Parent().string().c_str()) /
sourcePath.ChangeExtension(sourcePath.Extension().string() +
L"." +
// Use a short prefix of the full hash (we don't need high collision-resistance)
wstring_from_utf8(Hexify(digest, 8)) +
m_FileExtension);
}

View File

@ -34,6 +34,7 @@
#include "ps/GameSetup/Paths.h"
#include "ps/Mod.h"
#include "ps/ModInstaller.h"
#include "ps/Util.h"
#include "scriptinterface/ScriptConversions.h"
#include "scriptinterface/ScriptInterface.h"
@ -520,17 +521,15 @@ bool ModIo::VerifyDownloadedFile(std::string& err)
{
u8 digest[MD5::DIGESTSIZE];
m_CallbackData->md5.Final(digest);
std::stringstream md5digest;
md5digest << std::hex << std::setfill('0');
for (size_t i = 0; i < MD5::DIGESTSIZE; ++i)
md5digest << std::setw(2) << (int)digest[i];
std::string md5digest = Hexify(digest, MD5::DIGESTSIZE);
if (m_ModData[m_DownloadModID].properties.at("filehash_md5") != md5digest.str())
if (m_ModData[m_DownloadModID].properties.at("filehash_md5") != md5digest)
{
err = fmt::sprintf(
g_L10n.Translate("Invalid file. Expected md5 %s, got %s."),
m_ModData[m_DownloadModID].properties.at("filehash_md5").c_str(),
md5digest.str());
md5digest);
return false;
}
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2018 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -431,6 +431,15 @@ std::string Hexify(const std::string& s)
std::stringstream str;
str << std::hex;
for (const char& c : s)
str << std::setfill('0') << std::setw(2) << (int)(unsigned char)c;
str << std::setfill('0') << std::setw(2) << static_cast<int>(static_cast<unsigned char>(c));
return str.str();
}
std::string Hexify(const u8* s, size_t length)
{
std::stringstream str;
str << std::hex;
for (size_t i = 0; i < length; ++i)
str << std::setfill('0') << std::setw(2) << static_cast<int>(s[i]);
return str.str();
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2017 Wildfire Games.
/* Copyright (C) 2018 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -35,5 +35,6 @@ void WriteBigScreenshot(const VfsPath& extension, int tiles);
Status tex_write(Tex* t, const VfsPath& filename);
std::string Hexify(const std::string& s);
std::string Hexify(const u8* s, size_t length);
#endif // PS_UTIL_H

View File

@ -29,6 +29,7 @@
#include "ps/GameSetup/Atlas.h"
#include "ps/Globals.h"
#include "ps/Hotkey.h"
#include "ps/Util.h"
#include "scriptinterface/ScriptInterface.h"
#include "tools/atlas/GameInterface/GameLoop.h"
@ -118,11 +119,7 @@ std::string JSI_Main::CalculateMD5(ScriptInterface::CxPrivate* UNUSED(pCxPrivate
m.Update((const u8*)input.c_str(), input.length());
m.Final(digest);
char digeststr[MD5::DIGESTSIZE*2+1];
for (size_t i = 0; i < MD5::DIGESTSIZE; ++i)
sprintf_s(digeststr+2*i, 3, "%02x", (unsigned int)digest[i]);
return digeststr;
return Hexify(digest, MD5::DIGESTSIZE);
}
void JSI_Main::RegisterScriptFunctions(const ScriptInterface& scriptInterface)