Skip to content

Commit 14eeaee

Browse files
committed
DownloadManager: rework hash comparison functions for useful errors.
Also lowercases hashes from Lua. The calculated digests are always converted to lowercase strings.
1 parent 6136ea5 commit 14eeaee

File tree

7 files changed

+58
-46
lines changed

7 files changed

+58
-46
lines changed

threaded-libcurl/DownloadManager.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,26 @@ int DownloadManager::busy( void ) {
8989

9090
// These are kind of out of place but they are useful.
9191

92-
int DownloadManager::checkFileSHA1( const std::string filename, const std::string expected ) {
92+
std::string DownloadManager::getFileSHA1( const std::string filename ) {
9393
std::string buffer;
9494
std::fstream inFile( filename, std::ios::in | std::ios::binary | std::ios::ate );
9595
if (inFile.is_open( )) {
9696
buffer.resize( inFile.tellg( ) );
9797
inFile.seekg( 0, std::ios::beg );
9898
inFile.read( &buffer[0], buffer.size( ) );
9999
inFile.close( );
100-
return checkStringSHA1( buffer, expected );
100+
return getStringSHA1( buffer );
101101
}
102-
return -1;
102+
return "";
103103
}
104104

105-
int DownloadManager::checkStringSHA1( const std::string string, const std::string expected ) {
105+
std::string DownloadManager::getStringSHA1( const std::string string ) {
106106
SHA1_CTX ctx;
107107
uint8_t digest[SHA1_DIGEST_SIZE];
108108
SHA1_Init( &ctx );
109109
SHA1_Update( &ctx, reinterpret_cast<const uint8_t*>(string.c_str( )), string.size( ) );
110110
SHA1_Final( &ctx, digest );
111-
return sha1compare( digest, expected );
111+
return digestToHex( digest );
112112
}
113113

114114
/*

threaded-libcurl/DownloadManager.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class DownloadManager {
99
unsigned int finishedCount = 0, addedCount = 0, failedCount = 0;
1010

1111
public:
12-
const static unsigned int version = 0x000101;
12+
const static unsigned int version = 0x000102;
1313
DownloadManager( void );
1414
~DownloadManager( void );
1515
double getProgress( void );
@@ -20,6 +20,6 @@ class DownloadManager {
2020
void terminate( void );
2121
void clear( void );
2222
int busy( void );
23-
static int checkFileSHA1( std::string filename, std::string expected );
24-
static int checkStringSHA1( std::string string, std::string expecteds );
23+
static std::string getFileSHA1( std::string filename );
24+
static std::string getStringSHA1( std::string string );
2525
};

threaded-libcurl/DownloadManager.moon

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[[ ---------- Usage ----------
1+
[[---------- Usage ----------
22
33
1. Create a DownloadManager:
44
@@ -67,11 +67,11 @@ void Sleep(unsigned long);
6767
sleep = ffi.os == "Windows" and (( ms = 100 ) -> ffi.C.Sleep ms) or (( ms = 100 ) -> ffi.C.usleep ms*1000)
6868

6969
class DownloadManager
70-
@version = 0x000103
71-
@version_string = "0.1.3"
70+
@version = 0x000104
71+
@version_string = "0.1.4"
7272

7373
DM = nil
74-
DMVersion = 0x000101
74+
DMVersion = 0x000102
7575
pathExt = "/automation/include/#{@__name}/#{(ffi.os != 'Windows') and 'lib' or ''}#{@__name}.#{(OSX: 'dylib', Windows: 'dll')[ffi.os] or 'so'}"
7676
defaultLibraryPaths = aegisub and {aegisub.decode_path("?user"..pathExt), aegisub.decode_path("?data"..pathExt)} or {@__name}
7777
msgs = {
@@ -137,6 +137,10 @@ class DownloadManager
137137
-- lfs.mkdir returns nil on success and error alike
138138
return nil, err if err
139139

140+
-- make sure sha1 is lowercase for comparison.
141+
if sha1
142+
sha1 = sha1\lower!
143+
140144
DM.addDownload @manager, url, outfile, sha1
141145
@downloadCount += 1
142146
@downloads[@downloadCount] = id:@downloadCount, :url, :outfile, :sha1
@@ -182,17 +186,17 @@ class DownloadManager
182186
-- just make them instance methods than trying to juggle the DM init.
183187
-- Also make them fat arrow functions for calling consistency.
184188
checkFileSHA1: ( filename, expected ) =>
185-
switch DM.checkFileSHA1 filename, expected
186-
when -1
187-
return nil
188-
when 0
189-
return true
190-
when 1
191-
return false
189+
result = DM.getFileSHA1 filename
190+
if nil == result
191+
return nil, "Could not open file #{filename}."
192+
if result == expected\lower!
193+
return true
194+
else
195+
return false, "Hash mismatch. Got #{result}, expected #{expected}."
192196

193197
checkStringSHA1: ( string, expected ) =>
194-
switch DM.checkStringSHA1 filename, expected
195-
when 0
196-
return true
197-
when 1
198-
return false
198+
result = DM.getStringSHA1 string
199+
if result == expected\lower!
200+
return true
201+
else
202+
return false, "Hash mismatch. Got #{result}, expected #{expected}."

threaded-libcurl/DownloadManagerC.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ extern "C" {
4040
reinterpret_cast<DownloadManager*>(mgr)->clear( );
4141
}
4242

43-
EXPORT int checkFileSHA1( const char *filename, const char *expected ) {
44-
return DownloadManager::checkFileSHA1( std::string( filename ), std::string( expected ) );
43+
EXPORT const char* getFileSHA1( const char *filename ) {
44+
auto result = DownloadManager::getFileSHA1( std::string( filename ) );
45+
if ( "" == result )
46+
return NULL;
47+
48+
return result.c_str( );
4549
}
4650

47-
EXPORT int checkStringSHA1( const char *string, const char *expected ) {
48-
return DownloadManager::checkStringSHA1( std::string( string ), std::string( expected ) );
51+
EXPORT const char* getStringSHA1( const char *string ) {
52+
return DownloadManager::getStringSHA1( std::string( string ) ).c_str( );
4953
}
5054

5155
EXPORT uint version( void ) {

threaded-libcurl/DownloadManagerC.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ struct CDlM;
1515
typedef struct CDlM CDlM;
1616
typedef unsigned int uint;
1717

18-
EXPORT CDlM* newDM ( void );
19-
EXPORT uint addDownload ( CDlM *mgr, const char *url,
20-
const char *outfile, const char *sha1 );
21-
EXPORT double progress ( CDlM *mgr );
22-
EXPORT int busy ( CDlM *mgr );
23-
EXPORT int checkDownload ( CDlM *mgr, uint i );
24-
EXPORT const char* getError ( CDlM *mgr, uint i );
25-
EXPORT void terminate ( CDlM *mgr );
26-
EXPORT void clear ( CDlM *mgr );
27-
EXPORT int checkFileSHA1 ( const char *filename, const char *expected );
28-
EXPORT int checkStringSHA1( const char *string, const char *expected );
29-
EXPORT uint version ( void );
30-
EXPORT void freeDM ( CDlM *mgr );
18+
EXPORT CDlM* newDM ( void );
19+
EXPORT uint addDownload ( CDlM *mgr, const char *url,
20+
const char *outfile, const char *sha1 );
21+
EXPORT double progress ( CDlM *mgr );
22+
EXPORT int busy ( CDlM *mgr );
23+
EXPORT int checkDownload( CDlM *mgr, uint i );
24+
EXPORT const char* getError ( CDlM *mgr, uint i );
25+
EXPORT void terminate ( CDlM *mgr );
26+
EXPORT void clear ( CDlM *mgr );
27+
EXPORT const char* getFileSHA1 ( const char *filename );
28+
EXPORT const char* getStringSHA1( const char *string );
29+
EXPORT uint version ( void );
30+
EXPORT void freeDM ( CDlM *mgr );
3131

3232
#ifdef __cplusplus
3333
}

threaded-libcurl/Downloader.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <iostream>
22
#include <fstream>
3+
#include <sstream>
34
#include <cstdint>
45
#include <cstdio>
56

@@ -13,7 +14,7 @@ static int curlProgressCallback( void *userdata, curl_off_t dltotal, curl_off_t
1314
return static_cast<Downloader*>(userdata)->progressCallback( dltotal, dlnow );
1415
}
1516

16-
int sha1compare( uint8_t digest[SHA1_DIGEST_SIZE], const std::string sha1 ) {
17+
std::string digestToHex( uint8_t digest[SHA1_DIGEST_SIZE] ) {
1718
char hash[41];
1819
for( unsigned int offset = 0; offset < SHA1_DIGEST_SIZE; offset++ ) {
1920
#ifdef _WIN32
@@ -22,7 +23,7 @@ int sha1compare( uint8_t digest[SHA1_DIGEST_SIZE], const std::string sha1 ) {
2223
snprintf( hash + 2*offset, 3, "%02x", digest[offset] );
2324
#endif // _WIN32
2425
}
25-
return (std::string(hash) != sha1);
26+
return std::string(hash);
2627
}
2728

2829
Downloader::Downloader( std::string theUrl, std::string theOutfile ) {
@@ -66,8 +67,11 @@ void Downloader::finalize( void ) {
6667
if (hasSHA1) {
6768
uint8_t digest[SHA1_DIGEST_SIZE];
6869
SHA1_Final( &sha1ctx, digest );
69-
if (sha1compare( digest, sha1 )) {
70-
error = "sha1 mismatch.";
70+
auto result = digestToHex( digest );
71+
if ( result != sha1 ) {
72+
std::ostringstream fmtStr("Hash mismatch. Got ");
73+
fmtStr << result << ", expected " << sha1;
74+
error = fmtStr.str( );
7175
failed = true;
7276
return;
7377
}

threaded-libcurl/Downloader.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
#include "sha1.h"
77

8-
int sha1compare( uint8_t digest[SHA1_DIGEST_SIZE], const std::string sha1 );
8+
std::string digestToHex( uint8_t digest[SHA1_DIGEST_SIZE] );
99

1010
class Downloader {
1111
std::thread thread;

0 commit comments

Comments
 (0)