Merge pull request #1259 from heinrich5991/pr_ddnet_mapbugs_sha256

Also use sha256 for the mapbugs stuff
This commit is contained in:
Dennis Felsing 2018-08-21 09:07:57 +02:00 committed by GitHub
commit 6f75cff582
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 87 additions and 23 deletions

View file

@ -35,6 +35,11 @@ void sha256_str(SHA256_DIGEST digest, char *str, size_t max_len)
}
}
int sha256_from_str(SHA256_DIGEST *out, const char *str)
{
return str_hex_decode(out->data, sizeof(out->data), str);
}
int sha256_comp(SHA256_DIGEST digest1, SHA256_DIGEST digest2)
{
return mem_comp(digest1.data, digest2.data, sizeof(digest1.data));

View file

@ -2561,16 +2561,18 @@ static int byteval(const char *byte, unsigned char *dst)
return 0;
}
int str_hex_decode(unsigned char *dst, int dst_size, const char *src)
int str_hex_decode(void *dst, int dst_size, const char *src)
{
int len = str_length(src)/2;
unsigned char *cdst = dst;
int slen = str_length(src);
int len = slen / 2;
int i;
if(len != dst_size)
if(slen != dst_size * 2)
return 2;
for(i = 0; i < len && dst_size; i++, dst_size--)
{
if(byteval(src + i * 2, dst++))
if(byteval(src + i * 2, cdst++))
return 1;
}
return 0;

View file

@ -1291,7 +1291,8 @@ const char *str_find(const char *haystack, const char *needle);
/*
Function: str_hex
Takes a datablock and generates a hexstring of it.
Takes a datablock and generates a hex string of it, with spaces
between bytes.
Parameters:
dst - Buffer to fill with hex data
@ -1306,7 +1307,8 @@ void str_hex(char *dst, int dst_size, const void *data, int data_size);
/*
Function: str_hex_decode
Takes a hex string and returns a byte array.
Takes a hex string *without spaces between bytes* and returns a
byte array.
Parameters:
dst - Buffer for the byte array
@ -1321,7 +1323,7 @@ void str_hex(char *dst, int dst_size, const void *data, int data_size);
Remarks:
- The contents of the buffer is only valid on success
*/
int str_hex_decode(unsigned char *dst, int dst_size, const char *src);
int str_hex_decode(void *dst, int dst_size, const char *src);
/*
Function: str_timestamp
Copies a time stamp in the format year-month-day_hour-minute-second to the string.

View file

@ -6,6 +6,7 @@ struct CMapDescription
{
const char *m_pName;
int m_Size;
SHA256_DIGEST m_Sha256;
int m_Crc;
bool operator==(const CMapDescription &Other) const
@ -34,14 +35,21 @@ static unsigned int IsBugFlagSet(int Bug, unsigned int BugFlags)
return (BugFlags & BugToFlag(Bug)) != 0;
}
static SHA256_DIGEST s(const char *pSha256)
{
SHA256_DIGEST Result;
dbg_assert(sha256_from_str(&Result, pSha256) == 0, "invalid sha256 in mapbugs");
return Result;
}
static CMapBugsInternal MAP_BUGS[] =
{
{{"Binary", 2022597, 0x0ae3a3d5}, BugToFlag(BUG_GRENADE_DOUBLEEXPLOSION)}
{{"Binary", 2022597, s("65b410e197fd2298ec270e89a84b762f6739d1d18089529f8ef6cf2104d3d600"), 0x0ae3a3d5}, BugToFlag(BUG_GRENADE_DOUBLEEXPLOSION)}
};
CMapBugs GetMapBugs(const char *pName, int Size, int Crc)
CMapBugs GetMapBugs(const char *pName, int Size, SHA256_DIGEST Sha256, int Crc)
{
CMapDescription Map = {pName, Size, Crc};
CMapDescription Map = {pName, Size, Sha256, Crc};
CMapBugs Result;
Result.m_Extra = 0;
for(unsigned int i = 0; i < sizeof(MAP_BUGS) / sizeof(MAP_BUGS[0]); i++)
@ -111,9 +119,12 @@ void CMapBugs::Dump() const
dbg_msg("mapbugs", "enabling map compatibility mode %s", aBugs);
if(pInternal)
{
dbg_msg("mapbugs", "map='%s' map_size=%d map_crc=%08x",
char aSha256[SHA256_MAXSTRSIZE];
sha256_str(pInternal->m_Map.m_Sha256, aSha256, sizeof(aSha256));
dbg_msg("mapbugs", "map='%s' map_size=%d map_sha256=%s map_crc=%08x",
pInternal->m_Map.m_pName,
pInternal->m_Map.m_Size,
aSha256,
pInternal->m_Map.m_Crc);
}
}

View file

@ -1,6 +1,8 @@
#ifndef GAME_MAPBUGS_H
#define GAME_MAPBUGS_H
#include <base/hash.h>
enum
{
#define MAPBUG(constname, string) constname,
@ -18,7 +20,7 @@ enum
class CMapBugs
{
friend CMapBugs GetMapBugs(const char *pName, int Size, int Crc);
friend CMapBugs GetMapBugs(const char *pName, int Size, SHA256_DIGEST Sha256, int Crc);
void *m_pData;
unsigned int m_Extra;
@ -28,5 +30,5 @@ public:
void Dump() const;
};
CMapBugs GetMapBugs(const char *pName, int Size, int Crc);
CMapBugs GetMapBugs(const char *pName, int Size, SHA256_DIGEST Sha256, int Crc);
#endif // GAME_MAPBUGS_H

View file

@ -2641,7 +2641,7 @@ void CGameContext::OnInit(/*class IKernel *pKernel*/)
SHA256_DIGEST MapSha256;
int MapCrc;
Server()->GetMapInfo(aMapName, sizeof(aMapName), &MapSize, &MapSha256, &MapCrc);
m_MapBugs = GetMapBugs(aMapName, MapSize, MapCrc);
m_MapBugs = GetMapBugs(aMapName, MapSize, MapSha256, MapCrc);
// reset everything here
//world = new GAMEWORLD;

View file

@ -40,3 +40,21 @@ TEST(Hash, Sha256Eq)
{
EXPECT_EQ(sha256("", 0), sha256("", 0));
}
TEST(Hash, Sha256FromStr)
{
SHA256_DIGEST Expected = {{
0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x23, 0x45, 0x67, 0x89,
0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x23, 0x45, 0x67, 0x89,
0x01, 0x23, 0x45, 0x67, 0x89, 0x01, 0x23, 0x45, 0x67, 0x89,
0x01, 0x23,
}};
SHA256_DIGEST Sha256;
EXPECT_FALSE(sha256_from_str(&Sha256, "0123456789012345678901234567890123456789012345678901234567890123"));
EXPECT_EQ(Sha256, Expected);
EXPECT_TRUE(sha256_from_str(&Sha256, "012345678901234567890123456789012345678901234567890123456789012"));
EXPECT_TRUE(sha256_from_str(&Sha256, "01234567890123456789012345678901234567890123456789012345678901234"));
EXPECT_TRUE(sha256_from_str(&Sha256, ""));
EXPECT_TRUE(sha256_from_str(&Sha256, "012345678901234567890123456789012345678901234567890123456789012x"));
EXPECT_TRUE(sha256_from_str(&Sha256, "x123456789012345678901234567890123456789012345678901234567890123"));
}

View file

@ -1,26 +1,37 @@
#include <gtest/gtest.h>
#include <base/system.h>
#include <game/mapbugs.h>
static const char *BINARY_SHA256 = "65b410e197fd2298ec270e89a84b762f6739d1d18089529f8ef6cf2104d3d600";
static const char *DM1_SHA256 = "0b0c481d77519c32fbe85624ef16ec0fa9991aec7367ad538bd280f28d8c26cf";
static CMapBugs GetMapBugsImpl(const char *pName, int Size, const char *pSha256, int Crc)
{
SHA256_DIGEST Sha256 = {{0}};
dbg_assert(sha256_from_str(&Sha256, pSha256) == 0, "invalid sha256 in tests");
return GetMapBugs(pName, Size, Sha256, Crc);
}
TEST(MapBugs, Contains)
{
EXPECT_TRUE(GetMapBugs("Binary", 2022597, 0x0ae3a3d5).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugs("Binarx", 2022597, 0x0ae3a3d5).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugs("Binary", 2022597, 0x0ae3a3d6).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugs("Binary", 2022598, 0x0ae3a3d5).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugs("dm1", 5805, 0xf2159e6e).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_TRUE(GetMapBugsImpl("Binary", 2022597, BINARY_SHA256, 0x0ae3a3d5).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugsImpl("Binarx", 2022597, BINARY_SHA256, 0x0ae3a3d5).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugsImpl("Binary", 2022597, BINARY_SHA256, 0x0ae3a3d6).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugsImpl("Binary", 2022598, BINARY_SHA256, 0x0ae3a3d5).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_FALSE(GetMapBugsImpl("dm1", 5805, DM1_SHA256, 0xf2159e6e).Contains(BUG_GRENADE_DOUBLEEXPLOSION));
}
TEST(MapBugs, Update)
{
{
CMapBugs Binary = GetMapBugs("Binary", 2022597, 0x0ae3a3d5);
CMapBugs Binary = GetMapBugsImpl("Binary", 2022597, BINARY_SHA256, 0x0ae3a3d5);
EXPECT_EQ(Binary.Update("grenade-doubleexplosion@ddnet.tw"), MAPBUGUPDATE_OVERRIDDEN);
EXPECT_EQ(Binary.Update("doesntexist@invalid"), MAPBUGUPDATE_NOTFOUND);
EXPECT_TRUE(Binary.Contains(BUG_GRENADE_DOUBLEEXPLOSION));
}
{
CMapBugs Dm1 = GetMapBugs("dm1", 5805, 0xf2159e6e);
CMapBugs Dm1 = GetMapBugsImpl("dm1", 5805, DM1_SHA256, 0xf2159e6e);
EXPECT_FALSE(Dm1.Contains(BUG_GRENADE_DOUBLEEXPLOSION));
EXPECT_EQ(Dm1.Update("doesntexist@invalid"), MAPBUGUPDATE_NOTFOUND);
EXPECT_FALSE(Dm1.Contains(BUG_GRENADE_DOUBLEEXPLOSION));
@ -31,6 +42,6 @@ TEST(MapBugs, Update)
TEST(MapBugs, Dump)
{
GetMapBugs("Binary", 2022597, 0x0ae3a3d5).Dump();
GetMapBugs("dm1", 5805, 0xf2159e6e).Dump();
GetMapBugsImpl("Binary", 2022597, BINARY_SHA256, 0x0ae3a3d5).Dump();
GetMapBugsImpl("dm1", 5805, DM1_SHA256, 0xf2159e6e).Dump();
}

View file

@ -93,3 +93,16 @@ TEST(Str, Endswith)
EXPECT_EQ(str_endswith(ABCDEFG, DEFG) - ABCDEFG,
str_length(ABCDEFG) - str_length(DEFG));
}
TEST(Str, HexDecode)
{
char aOut[5] = {'a', 'b', 'c', 'd', 0};
EXPECT_EQ(str_hex_decode(aOut, 0, ""), 0); EXPECT_STREQ(aOut, "abcd");
EXPECT_EQ(str_hex_decode(aOut, 0, " "), 2); EXPECT_STREQ(aOut, "abcd");
EXPECT_EQ(str_hex_decode(aOut, 1, "1"), 2); EXPECT_STREQ(aOut + 1, "bcd");
EXPECT_EQ(str_hex_decode(aOut, 1, "41"), 0); EXPECT_STREQ(aOut, "Abcd");
EXPECT_EQ(str_hex_decode(aOut, 1, "4x"), 1); EXPECT_STREQ(aOut + 1, "bcd");
EXPECT_EQ(str_hex_decode(aOut, 1, "x1"), 1); EXPECT_STREQ(aOut + 1, "bcd");
EXPECT_EQ(str_hex_decode(aOut, 1, "411"), 2); EXPECT_STREQ(aOut + 1, "bcd");
EXPECT_EQ(str_hex_decode(aOut, 4, "41424344"), 0); EXPECT_STREQ(aOut, "ABCD");
}