Skip to content

Commit

Permalink
Merge pull request #17594 from hrydgard/backport-fixes
Browse files Browse the repository at this point in the history
Port some fixes/improvements from the retroachievements branch
  • Loading branch information
unknownbrackets committed Jun 17, 2023
2 parents 93fbb5c + 0983927 commit 0fb7d6f
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 62 deletions.
58 changes: 29 additions & 29 deletions Common/Crypto/md5.cpp
Expand Up @@ -62,7 +62,7 @@
/*
* MD5 context setup
*/
void md5_starts( md5_context *ctx )
void ppsspp_md5_starts( md5_context *ctx )
{
ctx->total[0] = 0;
ctx->total[1] = 0;
Expand All @@ -73,7 +73,7 @@ void md5_starts( md5_context *ctx )
ctx->state[3] = 0x10325476;
}

static void md5_process( md5_context *ctx, unsigned char data[64] )
static void ppsspp_md5_process( md5_context *ctx, unsigned char data[64] )
{
unsigned long X[16], A, B, C, D;

Expand Down Expand Up @@ -199,7 +199,7 @@ static void md5_process( md5_context *ctx, unsigned char data[64] )
/*
* MD5 process buffer
*/
void md5_update( md5_context *ctx, unsigned char *input, int ilen )
void ppsspp_md5_update( md5_context *ctx, unsigned char *input, int ilen )
{
int fill;
unsigned long left;
Expand All @@ -220,15 +220,15 @@ void md5_update( md5_context *ctx, unsigned char *input, int ilen )
{
memcpy( (void *) (ctx->buffer + left),
(void *) input, fill );
md5_process( ctx, ctx->buffer );
ppsspp_md5_process( ctx, ctx->buffer );
input += fill;
ilen -= fill;
left = 0;
}

while( ilen >= 64 )
{
md5_process( ctx, input );
ppsspp_md5_process( ctx, input );
input += 64;
ilen -= 64;
}
Expand All @@ -251,7 +251,7 @@ static const unsigned char md5_padding[64] =
/*
* MD5 final digest
*/
void md5_finish( md5_context *ctx, unsigned char output[16] )
void ppsspp_md5_finish( md5_context *ctx, unsigned char output[16] )
{
unsigned long last, padn;
unsigned long high, low;
Expand All @@ -267,8 +267,8 @@ void md5_finish( md5_context *ctx, unsigned char output[16] )
last = ctx->total[0] & 0x3F;
padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );

md5_update( ctx, (unsigned char *) md5_padding, padn );
md5_update( ctx, msglen, 8 );
ppsspp_md5_update( ctx, (unsigned char *) md5_padding, padn );
ppsspp_md5_update( ctx, msglen, 8 );

PUT_ULONG_LE( ctx->state[0], output, 0 );
PUT_ULONG_LE( ctx->state[1], output, 4 );
Expand All @@ -279,28 +279,28 @@ void md5_finish( md5_context *ctx, unsigned char output[16] )
/*
* output = MD5( input buffer )
*/
void md5( unsigned char *input, int ilen, unsigned char output[16] )
void ppsspp_md5( unsigned char *input, int ilen, unsigned char output[16] )
{
md5_context ctx;

md5_starts( &ctx );
md5_update( &ctx, input, ilen );
md5_finish( &ctx, output );
ppsspp_md5_starts( &ctx );
ppsspp_md5_update( &ctx, input, ilen );
ppsspp_md5_finish( &ctx, output );

memset( &ctx, 0, sizeof( md5_context ) );
}

/*
* MD5 HMAC context setup
*/
void md5_hmac_starts( md5_context *ctx, unsigned char *key, int keylen )
void ppsspp_md5_hmac_starts( md5_context *ctx, unsigned char *key, int keylen )
{
int i;
unsigned char sum[16];

if( keylen > 64 )
{
md5( key, keylen, sum );
ppsspp_md5( key, keylen, sum );
keylen = 16;
key = sum;
}
Expand All @@ -314,47 +314,47 @@ void md5_hmac_starts( md5_context *ctx, unsigned char *key, int keylen )
ctx->opad[i] = (unsigned char)( ctx->opad[i] ^ key[i] );
}

md5_starts( ctx );
md5_update( ctx, ctx->ipad, 64 );
ppsspp_md5_starts( ctx );
ppsspp_md5_update( ctx, ctx->ipad, 64 );

memset( sum, 0, sizeof( sum ) );
}

/*
* MD5 HMAC process buffer
*/
void md5_hmac_update( md5_context *ctx, unsigned char *input, int ilen )
void ppsspp_md5_hmac_update( md5_context *ctx, unsigned char *input, int ilen )
{
md5_update( ctx, input, ilen );
ppsspp_md5_update( ctx, input, ilen );
}

/*
* MD5 HMAC final digest
*/
void md5_hmac_finish( md5_context *ctx, unsigned char output[16] )
void ppsspp_md5_hmac_finish( md5_context *ctx, unsigned char output[16] )
{
unsigned char tmpbuf[16];

md5_finish( ctx, tmpbuf );
md5_starts( ctx );
md5_update( ctx, ctx->opad, 64 );
md5_update( ctx, tmpbuf, 16 );
md5_finish( ctx, output );
ppsspp_md5_finish( ctx, tmpbuf );
ppsspp_md5_starts( ctx );
ppsspp_md5_update( ctx, ctx->opad, 64 );
ppsspp_md5_update( ctx, tmpbuf, 16 );
ppsspp_md5_finish( ctx, output );

memset( tmpbuf, 0, sizeof( tmpbuf ) );
}

/*
* output = HMAC-MD5( hmac key, input buffer )
*/
void md5_hmac( unsigned char *key, int keylen, unsigned char *input, int ilen,
void ppsspp_md5_hmac( unsigned char *key, int keylen, unsigned char *input, int ilen,
unsigned char output[16] )
{
md5_context ctx;

md5_hmac_starts( &ctx, key, keylen );
md5_hmac_update( &ctx, input, ilen );
md5_hmac_finish( &ctx, output );
ppsspp_md5_hmac_starts( &ctx, key, keylen );
ppsspp_md5_hmac_update( &ctx, input, ilen );
ppsspp_md5_hmac_finish( &ctx, output );

memset( &ctx, 0, sizeof( md5_context ) );
}
Expand Down Expand Up @@ -464,7 +464,7 @@ static const unsigned char md5_hmac_test_sum[7][16] =
/*
* Checkup routine
*/
int md5_self_test( int verbose )
int ppsspp_md5_self_test( int verbose )
{
int i, buflen;
unsigned char buf[1024];
Expand Down
20 changes: 10 additions & 10 deletions Common/Crypto/md5.h
Expand Up @@ -46,7 +46,7 @@ extern "C" {
*
* \param ctx context to be initialized
*/
void md5_starts( md5_context *ctx );
void ppsspp_md5_starts( md5_context *ctx );

/**
* \brief MD5 process buffer
Expand All @@ -55,15 +55,15 @@ void md5_starts( md5_context *ctx );
* \param input buffer holding the data
* \param ilen length of the input data
*/
void md5_update( md5_context *ctx, unsigned char *input, int ilen );
void ppsspp_md5_update( md5_context *ctx, unsigned char *input, int ilen );

/**
* \brief MD5 final digest
*
* \param ctx MD5 context
* \param output MD5 checksum result
*/
void md5_finish( md5_context *ctx, unsigned char output[16] );
void ppsspp_md5_finish( md5_context *ctx, unsigned char output[16] );

/**
* \brief Output = MD5( input buffer )
Expand All @@ -72,7 +72,7 @@ void md5_finish( md5_context *ctx, unsigned char output[16] );
* \param ilen length of the input data
* \param output MD5 checksum result
*/
void md5( unsigned char *input, int ilen, unsigned char output[16] );
void ppsspp_md5( unsigned char *input, int ilen, unsigned char output[16] );

/**
* \brief Output = MD5( file contents )
Expand All @@ -83,7 +83,7 @@ void md5( unsigned char *input, int ilen, unsigned char output[16] );
* \return 0 if successful, 1 if fopen failed,
* or 2 if fread failed
*/
int md5_file( char *path, unsigned char output[16] );
int ppsspp_md5_file( char *path, unsigned char output[16] );

/**
* \brief MD5 HMAC context setup
Expand All @@ -92,7 +92,7 @@ int md5_file( char *path, unsigned char output[16] );
* \param key HMAC secret key
* \param keylen length of the HMAC key
*/
void md5_hmac_starts( md5_context *ctx, unsigned char *key, int keylen );
void ppsspp_md5_hmac_starts( md5_context *ctx, unsigned char *key, int keylen );

/**
* \brief MD5 HMAC process buffer
Expand All @@ -101,15 +101,15 @@ void md5_hmac_starts( md5_context *ctx, unsigned char *key, int keylen );
* \param input buffer holding the data
* \param ilen length of the input data
*/
void md5_hmac_update( md5_context *ctx, unsigned char *input, int ilen );
void ppsspp_md5_hmac_update( md5_context *ctx, unsigned char *input, int ilen );

/**
* \brief MD5 HMAC final digest
*
* \param ctx HMAC context
* \param output MD5 HMAC checksum result
*/
void md5_hmac_finish( md5_context *ctx, unsigned char output[16] );
void ppsspp_md5_hmac_finish( md5_context *ctx, unsigned char output[16] );

/**
* \brief Output = HMAC-MD5( hmac key, input buffer )
Expand All @@ -120,7 +120,7 @@ void md5_hmac_finish( md5_context *ctx, unsigned char output[16] );
* \param ilen length of the input data
* \param output HMAC-MD5 result
*/
void md5_hmac( unsigned char *key, int keylen,
void ppsspp_md5_hmac( unsigned char *key, int keylen,
unsigned char *input, int ilen,
unsigned char output[16] );

Expand All @@ -129,7 +129,7 @@ void md5_hmac( unsigned char *key, int keylen,
*
* \return 0 if successful, or 1 if the test failed
*/
int md5_self_test( int verbose );
int ppsspp_md5_self_test( int verbose );

#ifdef __cplusplus
}
Expand Down
53 changes: 41 additions & 12 deletions Common/Net/HTTPClient.cpp
Expand Up @@ -458,8 +458,8 @@ int Client::ReadResponseEntity(net::Buffer *readbuf, const std::vector<std::stri
return 0;
}

Download::Download(const std::string &url, const Path &outfile)
: progress_(&cancelled_), url_(url), outfile_(outfile) {
Download::Download(RequestMethod method, const std::string &url, const std::string &postData, const std::string &postMime, const Path &outfile)
: method_(method), progress_(&cancelled_), url_(url), postData_(postData), postMime_(postMime), outfile_(outfile) {
}

Download::~Download() {
Expand All @@ -484,7 +484,7 @@ void Download::SetFailed(int code) {
completed_ = true;
}

int Download::PerformGET(const std::string &url) {
int Download::Perform(const std::string &url) {
Url fileUrl(url);
if (!fileUrl.Valid()) {
return -1;
Expand All @@ -510,7 +510,11 @@ int Download::PerformGET(const std::string &url) {
}

RequestParams req(fileUrl.Resource(), acceptMime_);
return client.GET(req, &buffer_, responseHeaders_, &progress_);
if (method_ == RequestMethod::GET) {
return client.GET(req, &buffer_, responseHeaders_, &progress_);
} else {
return client.POST(req, postData_, postMime_, &buffer_, &progress_);
}
}

std::string Download::RedirectLocation(const std::string &baseUrl) {
Expand All @@ -533,7 +537,7 @@ void Download::Do() {

std::string downloadURL = url_;
while (resultCode_ == 0) {
int resultCode = PerformGET(downloadURL);
int resultCode = Perform(downloadURL);
if (resultCode == -1) {
SetFailed(resultCode);
return;
Expand All @@ -557,12 +561,12 @@ void Download::Do() {
}

if (resultCode == 200) {
INFO_LOG(IO, "Completed downloading %s to %s", url_.c_str(), outfile_.empty() ? "memory" : outfile_.c_str());
INFO_LOG(IO, "Completed requesting %s (storing result to %s)", url_.c_str(), outfile_.empty() ? "memory" : outfile_.c_str());
if (!outfile_.empty() && !buffer_.FlushToFile(outfile_)) {
ERROR_LOG(IO, "Failed writing download to '%s'", outfile_.c_str());
}
} else {
ERROR_LOG(IO, "Error downloading '%s' to '%s': %i", url_.c_str(), outfile_.c_str(), resultCode);
ERROR_LOG(IO, "Error requesting '%s' (storing result to '%s'): %i", url_.c_str(), outfile_.empty() ? "memory" : outfile_.c_str(), resultCode);
}
resultCode_ = resultCode;
}
Expand All @@ -575,10 +579,10 @@ void Download::Do() {
}

std::shared_ptr<Download> Downloader::StartDownload(const std::string &url, const Path &outfile, const char *acceptMime) {
std::shared_ptr<Download> dl(new Download(url, outfile));
std::shared_ptr<Download> dl(new Download(RequestMethod::GET, url, "", "", outfile));
if (acceptMime)
dl->SetAccept(acceptMime);
downloads_.push_back(dl);
newDownloads_.push_back(dl);
dl->Start();
return dl;
}
Expand All @@ -588,19 +592,36 @@ std::shared_ptr<Download> Downloader::StartDownloadWithCallback(
const Path &outfile,
std::function<void(Download &)> callback,
const char *acceptMime) {
std::shared_ptr<Download> dl(new Download(url, outfile));
std::shared_ptr<Download> dl(new Download(RequestMethod::GET, url, "", "", outfile));
if (acceptMime)
dl->SetAccept(acceptMime);
dl->SetCallback(callback);
downloads_.push_back(dl);
newDownloads_.push_back(dl);
dl->Start();
return dl;
}

std::shared_ptr<Download> Downloader::AsyncPostWithCallback(
const std::string &url,
const std::string &postData,
const std::string &postMime,
std::function<void(Download &)> callback) {
std::shared_ptr<Download> dl(new Download(RequestMethod::POST, url, postData, postMime, Path()));
dl->SetCallback(callback);
newDownloads_.push_back(dl);
dl->Start();
return dl;
}

void Downloader::Update() {
for (auto iter : newDownloads_) {
downloads_.push_back(iter);
}
newDownloads_.clear();

restart:
for (size_t i = 0; i < downloads_.size(); i++) {
auto &dl = downloads_[i];
auto dl = downloads_[i];
if (dl->Done()) {
dl->RunCallback();
dl->Join();
Expand All @@ -610,6 +631,14 @@ void Downloader::Update() {
}
}

void Downloader::WaitForAll() {
// TODO: Should lock? Though, OK if called from main thread, where Update() is called from.
while (!downloads_.empty()) {
Update();
sleep_ms(10);
}
}

std::vector<float> Downloader::GetCurrentProgress() {
std::vector<float> progress;
for (size_t i = 0; i < downloads_.size(); i++) {
Expand Down

0 comments on commit 0fb7d6f

Please sign in to comment.