Skip to content

Commit

Permalink
Address issues from CR.
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Hansen <zrax0111@gmail.com>
  • Loading branch information
Hoikas and zrax committed Feb 20, 2024
1 parent ac7a10e commit 4e17321
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
18 changes: 11 additions & 7 deletions AuthServ/AuthServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ void cb_fileList(AuthServer_Private& client)
return true;
};
try {
SDL::DescriptorDb::ForDescriptorFiles(DS::Settings::SdlPath(), populateSdl);
SDL::DescriptorDb::ForDescriptorFiles(DS::Settings::SdlPath(), std::move(populateSdl));
result = DS::e_NetSuccess;
} catch (const DS::SystemError& err) {
fputs(err.what(), stderr);
Expand Down Expand Up @@ -644,9 +644,9 @@ void cb_downloadStart(AuthServer_Private& client)
filename = DS::Settings::AuthRoot() + filename;
}

std::unique_ptr<DS::Stream> stream = std::make_unique<DS::FileStream>();
auto fileStream = std::make_unique<DS::FileStream>();
try {
static_cast<DS::FileStream*>(stream.get())->open(filename.c_str(), "rb");
fileStream->open(filename.c_str(), "rb");
} catch (const DS::FileIOException& ex) {
ST::printf(stderr, "[Auth] Could not open file {}: {}\n[Auth] Requested by {}\n",
filename, ex.what(), DS::SockIpAddress(client.m_sock));
Expand All @@ -659,23 +659,27 @@ void cb_downloadStart(AuthServer_Private& client)
}

// All auth downloads must be encrypted.
if (!DS::EncryptedStream::CheckEncryption(stream.get()).has_value()) {
std::unique_ptr<DS::Stream> bufStream = std::make_unique<DS::BufferStream>();
std::unique_ptr<DS::Stream> stream;
if (!DS::EncryptedStream::CheckEncryption(fileStream.get()).has_value()) {
auto bufStream = std::make_unique<DS::BufferStream>();
{
DS::EncryptedStream encStream(bufStream.get(), DS::EncryptedStream::Mode::e_write,
DS::EncryptedStream::Type::e_xxtea,
DS::Settings::DroidKey());
uint8_t buf[CHUNK_SIZE];
while (stream->tell() < stream->size()) {
ssize_t nread = stream->readBytes(buf, sizeof(buf));
while (fileStream->tell() < fileStream->size()) {
ssize_t nread = fileStream->readBytes(buf, sizeof(buf));
DS_ASSERT(nread >= 0);
encStream.writeBytes(buf, nread);
}
}
bufStream->seek(0, SEEK_SET);
stream = std::move(bufStream);
} else {
stream = std::move(fileStream);
}

DS_ASSERT(stream);
client.m_buffer.write<uint32_t>(DS::e_NetSuccess);
client.m_buffer.write<uint32_t>(stream->size());
client.m_buffer.write<uint32_t>(stream->tell());
Expand Down
2 changes: 1 addition & 1 deletion SDL/DescriptorDb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ bool SDL::DescriptorDb::ForLatestDescriptors(descfunc_t functor)
return true;
}

bool SDL::DescriptorDb::ForDescriptorFiles(const char* sdlpath, const filefunc_t& functor)
bool SDL::DescriptorDb::ForDescriptorFiles(const char* sdlpath, filefunc_t functor)
{
dirent** dirls;
int count = scandir(sdlpath, &dirls, &sel_sdl, &alphasort);
Expand Down
2 changes: 1 addition & 1 deletion SDL/DescriptorDb.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ namespace SDL
static StateDescriptor* FindDescriptor(const ST::string& name, int version);
static StateDescriptor* FindLatestDescriptor(const ST::string& name);
static bool ForLatestDescriptors(descfunc_t functor);
static bool ForDescriptorFiles(const char* sdlpath, const filefunc_t& functor);
static bool ForDescriptorFiles(const char* sdlpath, filefunc_t functor);

private:
DescriptorDb() = delete;
Expand Down
1 change: 1 addition & 0 deletions SDL/SdlParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ bool SDL::Parser::open(const char* filename)
m_fileStream->open(filename, "r");
} catch (DS::FileIOException& ex) {
ST::printf(stderr, "[SDL] Error opening file {} for reading: {}\n", filename, ex.what());
close();
return false;
}

Expand Down
18 changes: 12 additions & 6 deletions streams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,7 @@ DS::EncryptedStream::EncryptedStream(
switch (mode) {
case Mode::e_read:
base->readBytes(header, sizeof(header));
if (memcmp(header, "whatdoyousee", 12) == 0)
m_type = DS::EncryptedStream::Type::e_tea;
else if (memcmp(header, "BriceIsSmart", 12) == 0)
if (memcmp(header, "whatdoyousee", 12) == 0 || memcmp(header, "BriceIsSmart", 12) == 0)
m_type = DS::EncryptedStream::Type::e_tea;
else if (memcmp(header, "notthedroids", 12) == 0)
m_type = DS::EncryptedStream::Type::e_xxtea;
Expand All @@ -330,6 +328,14 @@ DS::EncryptedStream::EncryptedStream(

DS::EncryptedStream::~EncryptedStream()
{
close();
}

void DS::EncryptedStream::close()
{
if (m_base == nullptr)
return;

if (m_mode == Mode::e_write) {
if (m_pos % sizeof(m_buffer) != 0)
cryptFlush();
Expand All @@ -344,6 +350,8 @@ DS::EncryptedStream::~EncryptedStream()
}
m_base->write<uint32_t>(m_size);
}

m_base = nullptr;
}

std::optional<DS::EncryptedStream::Type> DS::EncryptedStream::CheckEncryption(const char* filename)
Expand All @@ -362,9 +370,7 @@ std::optional<DS::EncryptedStream::Type> DS::EncryptedStream::CheckEncryption(DS
stream->readBytes(header, sizeof(header));
stream->seek(pos, SEEK_SET);

if (memcmp(header, "whatdoyousee", sizeof(header)) == 0)
return DS::EncryptedStream::Type::e_tea;
if (memcmp(header, "BriceIsSmart", sizeof(header)) == 0)
if (memcmp(header, "whatdoyousee", sizeof(header)) == 0 || memcmp(header, "BriceIsSmart", sizeof(header)) == 0)
return DS::EncryptedStream::Type::e_tea;
if (memcmp(header, "notthedroids", sizeof(header)) == 0)
return DS::EncryptedStream::Type::e_xxtea;
Expand Down
5 changes: 5 additions & 0 deletions streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ namespace DS
static std::optional<Type> CheckEncryption(const char* filename);
static std::optional<Type> CheckEncryption(DS::Stream* stream);

void close();

Type getEncType() const { return m_type; }
void setKeys(const uint32_t* keys);

Expand All @@ -327,6 +329,9 @@ namespace DS
uint32_t size() const override { return m_size; }
bool atEof() override { return m_pos == m_size; }
void flush() override { m_base->flush(); }

EncryptedStream& operator =(const EncryptedStream& copy) = delete;
EncryptedStream& operator =(EncryptedStream&& move) = delete;
};
}

Expand Down

0 comments on commit 4e17321

Please sign in to comment.