Skip to content

Commit

Permalink
Fix #1443 - trim teaEncode trailing zeroes (#1534)
Browse files Browse the repository at this point in the history
* Fix #1443 - trim teaEncode block padding(0's) at the end
  • Loading branch information
Pirulax committed Jun 28, 2020
1 parent 5c3e05b commit 767a41c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
3 changes: 0 additions & 3 deletions Shared/sdk/SharedUtil.Hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ namespace SharedUtil
unsigned char m_digest[16];
};

void encodeXtea(unsigned int* v, unsigned int* w, unsigned int* k);
void decodeXTea(unsigned int* v, unsigned int* w, unsigned int* k);

void TeaEncode(const SString& str, const SString& key, SString* out);
void TeaDecode(const SString& str, const SString& key, SString* out);

Expand Down
25 changes: 23 additions & 2 deletions Shared/sdk/SharedUtil.Hash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ namespace SharedUtil
return GenerateHashHexString(hashFunction, NULL, 0);
}

void encodeXtea(unsigned int* v, unsigned int* w, unsigned int* k)
inline void encodeXtea(unsigned int* v, unsigned int* w, unsigned int* k)
{
unsigned int v0 = v[0], v1 = v[1], i, sum = 0;
unsigned int delta = 0x9E3779B9;
Expand All @@ -710,7 +710,7 @@ namespace SharedUtil
w[1] = v1;
}

void decodeXtea(unsigned int* v, unsigned int* w, unsigned int* k)
inline void decodeXtea(unsigned int* v, unsigned int* w, unsigned int* k)
{
unsigned int v0 = v[0], v1 = v[1], i, sum = 0xC6EF3720;
unsigned int delta = 0x9E3779B9;
Expand Down Expand Up @@ -818,5 +818,26 @@ namespace SharedUtil

out->assign((char*)buffer, numPasses * 4);
delete[] buffer;


// Delete all 0's from the end
// As of now, even if the user passed it in
// We'll delete it, because there's no way
// We can know if the user added the padding or not.

// Note: If we get there, there's at least 1 block(4 chars)
// before out->end()
{
// Traverse the string until the beginning, or until the fist non-0 char
auto iter = out->end();
for (; iter != out->begin(); iter--)
{
if (*std::prev(iter) != 0)
break;
}

if (iter != out->end())
out->erase(iter, out->end()); // Erase all 0s
}
}
} // namespace SharedUtil

0 comments on commit 767a41c

Please sign in to comment.