From 67d36b695d6b2be03683d45ca7b2d91a6098c9fe Mon Sep 17 00:00:00 2001 From: heinrich5991 Date: Sun, 19 Aug 2018 12:12:11 +0200 Subject: [PATCH] Also use sha256 for the mapbugs stuff --- src/base/hash.c | 5 +++++ src/base/system.c | 10 ++++++---- src/base/system.h | 8 +++++--- src/game/mapbugs.cpp | 19 +++++++++++++++---- src/game/mapbugs.h | 6 ++++-- src/game/server/gamecontext.cpp | 2 +- src/test/hash.cpp | 18 ++++++++++++++++++ src/test/mapbugs.cpp | 29 ++++++++++++++++++++--------- src/test/str.cpp | 13 +++++++++++++ 9 files changed, 87 insertions(+), 23 deletions(-) diff --git a/src/base/hash.c b/src/base/hash.c index eb2f20587..7a08d3708 100644 --- a/src/base/hash.c +++ b/src/base/hash.c @@ -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)); diff --git a/src/base/system.c b/src/base/system.c index dcada2604..7120fe318 100644 --- a/src/base/system.c +++ b/src/base/system.c @@ -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; diff --git a/src/base/system.h b/src/base/system.h index eefc2463d..3279fadc0 100644 --- a/src/base/system.h +++ b/src/base/system.h @@ -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. diff --git a/src/game/mapbugs.cpp b/src/game/mapbugs.cpp index 2b25f29dc..e33b8fa72 100644 --- a/src/game/mapbugs.cpp +++ b/src/game/mapbugs.cpp @@ -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); } } diff --git a/src/game/mapbugs.h b/src/game/mapbugs.h index 95ea25989..3b1b684ef 100644 --- a/src/game/mapbugs.h +++ b/src/game/mapbugs.h @@ -1,6 +1,8 @@ #ifndef GAME_MAPBUGS_H #define GAME_MAPBUGS_H +#include + 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 diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 647ae88b5..cafdcf23b 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -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; diff --git a/src/test/hash.cpp b/src/test/hash.cpp index 7a018e215..a61c7bb85 100644 --- a/src/test/hash.cpp +++ b/src/test/hash.cpp @@ -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")); +} diff --git a/src/test/mapbugs.cpp b/src/test/mapbugs.cpp index b47b03e1f..0d8032e31 100644 --- a/src/test/mapbugs.cpp +++ b/src/test/mapbugs.cpp @@ -1,26 +1,37 @@ #include +#include #include +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(); } diff --git a/src/test/str.cpp b/src/test/str.cpp index b50be4e17..a096e2f41 100644 --- a/src/test/str.cpp +++ b/src/test/str.cpp @@ -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"); +}