1
1
Fork 0
mirror of https://github.com/pbatard/rufus.git synced 2024-08-14 23:57:05 +00:00

[checksum] additional cleanup

This commit is contained in:
Pete Batard 2016-03-03 14:58:49 +00:00
parent 00ffbae61f
commit 0313e5ca54
3 changed files with 60 additions and 59 deletions

View file

@ -64,9 +64,9 @@
/* Globals */ /* Globals */
char sum_str[NUM_CHECKSUMS][65]; char sum_str[NUM_CHECKSUMS][65];
int bufnum, sum_count[NUM_CHECKSUMS] = { 16, 20, 32 }; uint32_t bufnum, sum_count[NUM_CHECKSUMS] = { 16, 20, 32 };
HANDLE data_ready[NUM_CHECKSUMS], thread_ready[NUM_CHECKSUMS]; HANDLE data_ready[NUM_CHECKSUMS], thread_ready[NUM_CHECKSUMS];
DWORD rSize[2]; DWORD read_size[2];
char ALIGNED(64) buffer[2][BUFFER_SIZE]; char ALIGNED(64) buffer[2][BUFFER_SIZE];
/* /*
@ -77,7 +77,7 @@ char ALIGNED(64) buffer[2][BUFFER_SIZE];
#define ROL(a,b) (((a) << (b)) | ((a) >> (32-(b)))) #define ROL(a,b) (((a) << (b)) | ((a) >> (32-(b))))
#define ROR(a,b) (((a) >> (b)) | ((a) << (32-(b)))) #define ROR(a,b) (((a) >> (b)) | ((a) << (32-(b))))
// For SHA-256 /* SHA-256 constants */
static const uint32_t K[64] = { static const uint32_t K[64] = {
0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
@ -135,8 +135,7 @@ static void sha256_init(SUM_CONTEXT *ctx)
/* Transform the message X which consists of 16 32-bit-words (SHA-1) */ /* Transform the message X which consists of 16 32-bit-words (SHA-1) */
static void sha1_transform(SUM_CONTEXT *ctx, const unsigned char *data) static void sha1_transform(SUM_CONTEXT *ctx, const unsigned char *data)
{ {
uint32_t a, b, c, d, e, tm; uint32_t a, b, c, d, e, tm, x[16];
uint32_t x[16];
/* get values from the chaining vars */ /* get values from the chaining vars */
a = ctx->state[0]; a = ctx->state[0];
@ -152,10 +151,10 @@ static void sha1_transform(SUM_CONTEXT *ctx, const unsigned char *data)
unsigned k; unsigned k;
for (k = 0; k < 16; k += 4) { for (k = 0; k < 16; k += 4) {
const unsigned char *p2 = data + k * 4; const unsigned char *p2 = data + k * 4;
x[k] = get_be32(p2); x[k] = read_swap32(p2);
x[k + 1] = get_be32(p2 + 4); x[k + 1] = read_swap32(p2 + 4);
x[k + 2] = get_be32(p2 + 8); x[k + 2] = read_swap32(p2 + 8);
x[k + 3] = get_be32(p2 + 12); x[k + 3] = read_swap32(p2 + 12);
} }
} }
#endif #endif
@ -267,6 +266,7 @@ static void sha1_transform(SUM_CONTEXT *ctx, const unsigned char *data)
ctx->state[4] += e; ctx->state[4] += e;
} }
/* Transform the message X which consists of 16 32-bit-words (SHA-256) */
static void sha256_transform(SUM_CONTEXT *ctx, const unsigned char *data) static void sha256_transform(SUM_CONTEXT *ctx, const unsigned char *data)
{ {
uint32_t a, b, c, d, e, f, g, h, j, x[16]; uint32_t a, b, c, d, e, f, g, h, j, x[16];
@ -309,10 +309,10 @@ static void sha256_transform(SUM_CONTEXT *ctx, const unsigned char *data)
unsigned k; unsigned k;
for (k = 0; k < 16; k += 4) { for (k = 0; k < 16; k += 4) {
const unsigned char *p2 = data + k * 4; const unsigned char *p2 = data + k * 4;
x[k] = get_be32(p2); x[k] = read_swap32(p2);
x[k + 1] = get_be32(p2 + 4); x[k + 1] = read_swap32(p2 + 4);
x[k + 2] = get_be32(p2 + 8); x[k + 2] = read_swap32(p2 + 8);
x[k + 3] = get_be32(p2 + 12); x[k + 3] = read_swap32(p2 + 12);
} }
} }
#endif #endif
@ -340,27 +340,26 @@ static void sha256_transform(SUM_CONTEXT *ctx, const unsigned char *data)
/* Transform the message X which consists of 16 32-bit-words (MD5) */ /* Transform the message X which consists of 16 32-bit-words (MD5) */
static void md5_transform(SUM_CONTEXT *ctx, const unsigned char *data) static void md5_transform(SUM_CONTEXT *ctx, const unsigned char *data)
{ {
uint32_t a, b, c, d; uint32_t a, b, c, d, x[16];
uint32_t x[16];
a = ctx->state[0]; a = ctx->state[0];
b = ctx->state[1]; b = ctx->state[1];
c = ctx->state[2]; c = ctx->state[2];
d = ctx->state[3]; d = ctx->state[3];
#ifndef BIG_ENDIAN_HOST #ifdef BIG_ENDIAN_HOST
memcpy(x, data, sizeof(x));
#else
{ {
unsigned k; unsigned k;
for (k = 0; k < 16; k += 4) { for (k = 0; k < 16; k += 4) {
const unsigned char *p2 = data + k * 4; const unsigned char *p2 = data + k * 4;
x[k] = get_be32(p2); x[k] = read_swap32(p2);
x[k + 1] = get_be32(p2 + 4); x[k + 1] = read_swap32(p2 + 4);
x[k + 2] = get_be32(p2 + 8); x[k + 2] = read_swap32(p2 + 8);
x[k + 3] = get_be32(p2 + 12); x[k + 3] = read_swap32(p2 + 12);
} }
} }
#else
memcpy(x, data, sizeof(x));
#endif #endif
#define F1(x, y, z) (z ^ (x & (y ^ z))) #define F1(x, y, z) (z ^ (x & (y ^ z)))
@ -470,7 +469,7 @@ static void sha1_write(SUM_CONTEXT *ctx, const unsigned char *buf, size_t len)
} }
while (len >= 64) { while (len >= 64) {
PREFETCH64(&buf[64]); PREFETCH64(buf + 64);
sha1_transform(ctx, buf); sha1_transform(ctx, buf);
ctx->bytecount = 0; ctx->bytecount = 0;
ctx->bitcount += 64 * 8; ctx->bitcount += 64 * 8;
@ -481,10 +480,10 @@ static void sha1_write(SUM_CONTEXT *ctx, const unsigned char *buf, size_t len)
ctx->buf[ctx->bytecount++] = *buf++; ctx->buf[ctx->bytecount++] = *buf++;
} }
/* Update the message digest with the contents of the buffer (SHA-256) */
static void sha256_write(SUM_CONTEXT *ctx, const unsigned char *buf, size_t len) static void sha256_write(SUM_CONTEXT *ctx, const unsigned char *buf, size_t len)
{ {
uint64_t pos = ctx->bytecount & 0x3F; size_t num, pos = ctx->bytecount & 0x3F;
uint64_t num;
ctx->bytecount += len; ctx->bytecount += len;
@ -514,7 +513,7 @@ static void sha256_write(SUM_CONTEXT *ctx, const unsigned char *buf, size_t len)
/* Update the message digest with the contents of the buffer (MD5) */ /* Update the message digest with the contents of the buffer (MD5) */
static void md5_write(SUM_CONTEXT *ctx, const unsigned char *buf, size_t len) static void md5_write(SUM_CONTEXT *ctx, const unsigned char *buf, size_t len)
{ {
uint32_t t; size_t t;
/* Update bitcount */ /* Update bitcount */
ctx->bitcount += (len << 3); ctx->bitcount += (len << 3);
@ -538,7 +537,7 @@ static void md5_write(SUM_CONTEXT *ctx, const unsigned char *buf, size_t len)
/* Process data in 64-byte chunks */ /* Process data in 64-byte chunks */
while (len >= 64) { while (len >= 64) {
PREFETCH64(&buf[64]); PREFETCH64(buf + 64);
memcpy(ctx->buf, buf, 64); memcpy(ctx->buf, buf, 64);
md5_transform(ctx, ctx->buf); md5_transform(ctx, ctx->buf);
buf += 64; buf += 64;
@ -549,7 +548,7 @@ static void md5_write(SUM_CONTEXT *ctx, const unsigned char *buf, size_t len)
memcpy(ctx->buf, buf, len); memcpy(ctx->buf, buf, len);
} }
/* The routine final terminates the computation and returns the digest (SHA-1) */ /* Finalize the computation and write the digest in ctx->state[] (SHA-1) */
static void sha1_final(SUM_CONTEXT *ctx) static void sha1_final(SUM_CONTEXT *ctx)
{ {
unsigned char *p; unsigned char *p;
@ -583,9 +582,8 @@ static void sha1_final(SUM_CONTEXT *ctx)
p = ctx->buf; p = ctx->buf;
#ifdef BIG_ENDIAN_HOST #ifdef BIG_ENDIAN_HOST
#define X(a) do { *(uint32_t*)p = ctx->state[a]; p += 4; } while(0) #define X(a) do { *(uint32_t*)p = ctx->state[a]; p += 4; } while(0)
#else /* little endian */ #else
#define X(a) do { *p++ = (unsigned char) (ctx->state[a] >> 24); *p++ = (unsigned char) (ctx->state[a] >> 16); \ #define X(a) do { write_swap32(p, ctx->state[a]); p += 4; } while(0);
*p++ = (unsigned char) (ctx->state[a] >> 8); *p++ = (unsigned char) ctx->state[a]; } while(0)
#endif #endif
X(0); X(0);
X(1); X(1);
@ -595,14 +593,15 @@ static void sha1_final(SUM_CONTEXT *ctx)
#undef X #undef X
} }
/* Finalize the computation and write the digest in ctx->state[] (SHA-256) */
static void sha256_final(SUM_CONTEXT *ctx) static void sha256_final(SUM_CONTEXT *ctx)
{ {
uint64_t pos = ctx->bytecount & 0x3F; size_t pos = ctx->bytecount & 0x3F;
unsigned char *p; unsigned char *p;
ctx->buf[pos++] = 0x80; ctx->buf[pos++] = 0x80;
// Pad whatever data is left in the buffer. /* Pad whatever data is left in the buffer */
while (pos != (64 - 8)) { while (pos != (64 - 8)) {
pos &= 0x3F; pos &= 0x3F;
if (pos == 0) if (pos == 0)
@ -610,7 +609,7 @@ static void sha256_final(SUM_CONTEXT *ctx)
ctx->buf[pos++] = 0; ctx->buf[pos++] = 0;
} }
// Append to the padding the total message's length in bits and transform. /* Append to the padding the total message's length in bits and transform */
ctx->bitcount = ctx->bytecount << 3; ctx->bitcount = ctx->bytecount << 3;
ctx->buf[63] = (unsigned char) (ctx->bitcount); ctx->buf[63] = (unsigned char) (ctx->bitcount);
ctx->buf[62] = (unsigned char) (ctx->bitcount >> 8); ctx->buf[62] = (unsigned char) (ctx->bitcount >> 8);
@ -626,9 +625,8 @@ static void sha256_final(SUM_CONTEXT *ctx)
p = ctx->buf; p = ctx->buf;
#ifdef BIG_ENDIAN_HOST #ifdef BIG_ENDIAN_HOST
#define X(a) do { *(uint32_t*)p = ctx->state[a]; p += 4; } while(0) #define X(a) do { *(uint32_t*)p = ctx->state[a]; p += 4; } while(0)
#else /* little endian */ #else
#define X(a) do { *p++ = (unsigned char) (ctx->state[a] >> 24); *p++ = (unsigned char) (ctx->state[a] >> 16); \ #define X(a) do { write_swap32(p, ctx->state[a]); p += 4; } while(0);
*p++ = (unsigned char) (ctx->state[a] >> 8); *p++ = (unsigned char) ctx->state[a]; } while(0)
#endif #endif
X(0); X(0);
X(1); X(1);
@ -641,10 +639,10 @@ static void sha256_final(SUM_CONTEXT *ctx)
#undef X #undef X
} }
/* The routine final terminates the computation and returns the digest (MD5) */ /* Finalize the computation and write the digest in ctx->state[] (MD5) */
static void md5_final(SUM_CONTEXT *ctx) static void md5_final(SUM_CONTEXT *ctx)
{ {
uint32_t count; size_t count;
unsigned char *p; unsigned char *p;
/* Compute number of bytes mod 64 */ /* Compute number of bytes mod 64 */
@ -686,9 +684,8 @@ static void md5_final(SUM_CONTEXT *ctx)
p = ctx->buf; p = ctx->buf;
#ifdef BIG_ENDIAN_HOST #ifdef BIG_ENDIAN_HOST
#define X(a) do { *p++ = (unsigned char) (ctx->state[a] >> 24); *p++ = (unsigned char) (ctx->state[a] >> 16); \ #define X(a) do { write_swap32(p, ctx->state[a]); p += 4; } while(0);
*p++ = (unsigned char) (ctx->state[a] >> 8); *p++ = (unsigned char) ctx->state[a]; } while(0) #else
#else /* little endian */
#define X(a) do { *(uint32_t*)p = ctx->state[a]; p += 4; } while(0) #define X(a) do { *(uint32_t*)p = ctx->state[a]; p += 4; } while(0)
#endif #endif
X(0); X(0);
@ -698,6 +695,7 @@ static void md5_final(SUM_CONTEXT *ctx)
#undef X #undef X
} }
//#define NULL_TEST //#define NULL_TEST
#ifdef NULL_TEST #ifdef NULL_TEST
// These 'null' calls are useful for testing load balancing and individual algorithm speed // These 'null' calls are useful for testing load balancing and individual algorithm speed
@ -821,7 +819,7 @@ BOOL SetChecksumAffinity(DWORD_PTR* thread_affinity)
DWORD WINAPI IndividualSumThread(void* param) DWORD WINAPI IndividualSumThread(void* param)
{ {
SUM_CONTEXT sum_ctx = { 0 }; // There's a memset in sum_init, but static analyzers still bug us SUM_CONTEXT sum_ctx = { 0 }; // There's a memset in sum_init, but static analyzers still bug us
int i = (int)(uintptr_t)param, j; uint32_t i = (uint32_t)(uintptr_t)param, j;
sum_init[i](&sum_ctx); sum_init[i](&sum_ctx);
// Signal that we're ready to service requests // Signal that we're ready to service requests
@ -834,8 +832,8 @@ DWORD WINAPI IndividualSumThread(void* param)
uprintf("Failed to wait for event for checksum thread #%d: %s", i, WindowsErrorString()); uprintf("Failed to wait for event for checksum thread #%d: %s", i, WindowsErrorString());
return 1; return 1;
} }
if (rSize[bufnum] != 0) { if (read_size[bufnum] != 0) {
sum_write[i](&sum_ctx, buffer[bufnum], (size_t)rSize[bufnum]); sum_write[i](&sum_ctx, buffer[bufnum], (size_t)read_size[bufnum]);
if (!SetEvent(thread_ready[i])) if (!SetEvent(thread_ready[i]))
goto error; goto error;
} else { } else {
@ -901,8 +899,8 @@ DWORD WINAPI SumThread(void* param)
bufnum = 0; bufnum = 0;
_bufnum = 0; _bufnum = 0;
rSize[0] = 1; // Don't trigger the first loop break read_size[0] = 1; // Don't trigger the first loop break
for (rb = 0; ;rb += rSize[_bufnum]) { for (rb = 0; ;rb += read_size[_bufnum]) {
// Update the progress and check for cancel // Update the progress and check for cancel
if (_GetTickCount64() > LastRefresh + 25) { if (_GetTickCount64() > LastRefresh + 25) {
LastRefresh = _GetTickCount64(); LastRefresh = _GetTickCount64();
@ -928,11 +926,11 @@ DWORD WINAPI SumThread(void* param)
} }
// Break the loop when data has been exhausted // Break the loop when data has been exhausted
if (rSize[bufnum] == 0) if (read_size[bufnum] == 0)
break; break;
// Read data (double buffered) // Read data (double buffered)
if (!ReadFile(h, buffer[_bufnum], BUFFER_SIZE, &rSize[_bufnum], NULL)) { if (!ReadFile(h, buffer[_bufnum], BUFFER_SIZE, &read_size[_bufnum], NULL)) {
FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_READ_FAULT; FormatStatus = ERROR_SEVERITY_ERROR | FAC(FACILITY_STORAGE) | ERROR_READ_FAULT;
uprintf("Read error: %s", WindowsErrorString()); uprintf("Read error: %s", WindowsErrorString());
goto out; goto out;
@ -945,7 +943,7 @@ DWORD WINAPI SumThread(void* param)
} }
} }
// Our last event with rSize=0 signaled the threads to exit - wait for that to happen // Our last event with read_size=0 signaled the threads to exit - wait for that to happen
if (WaitForMultipleObjects(NUM_CHECKSUMS, sum_thread, TRUE, WAIT_TIME) != WAIT_OBJECT_0) { if (WaitForMultipleObjects(NUM_CHECKSUMS, sum_thread, TRUE, WAIT_TIME) != WAIT_OBJECT_0) {
uprintf("Checksum threads did not finalize: %s", WindowsErrorString()); uprintf("Checksum threads did not finalize: %s", WindowsErrorString());
goto out; goto out;

View file

@ -52,6 +52,7 @@
#define PREFETCH64(m) do { _m_prefetch(m); _m_prefetch(m+32); } while(0) #define PREFETCH64(m) do { _m_prefetch(m); _m_prefetch(m+32); } while(0)
#endif #endif
/* Read/write with endianness swap */
#if defined (_MSC_VER) && (_MSC_VER >= 1300) #if defined (_MSC_VER) && (_MSC_VER >= 1300)
#include <stdlib.h> #include <stdlib.h>
#pragma intrinsic(_byteswap_ushort) #pragma intrinsic(_byteswap_ushort)
@ -60,15 +61,17 @@
#define bswap_uint64 _byteswap_uint64 #define bswap_uint64 _byteswap_uint64
#define bswap_uint32 _byteswap_ulong #define bswap_uint32 _byteswap_ulong
#define bswap_uint16 _byteswap_ushort #define bswap_uint16 _byteswap_ushort
#define get_be32(p) bswap_uint32(*(const uint32_t *)(const uint8_t *)(p))
#define get_be64(p) bswap_uint64(*(const uint64_t *)(const uint8_t *)(p))
#elif defined (__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3))) #elif defined (__GNUC__) && ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ >= 3)))
#define bswap_uint64 __builtin_bswap64 #define bswap_uint64 __builtin_bswap64
#define bswap_uint32 __builtin_bswap32 #define bswap_uint32 __builtin_bswap32
#define bswap_uint16 __builtin_bswap16 #define bswap_uint16 __builtin_bswap16
#define get_be32(p) bswap_uint32(*(const uint32_t *)(const uint8_t *)(p))
#define get_be64(p) bswap_uint64(*(const uint32_t *)(const uint8_t *)(p))
#endif #endif
#define read_swap16(p) bswap_uint16(*(const uint16_t*)(const uint8_t*)(p))
#define read_swap32(p) bswap_uint32(*(const uint32_t*)(const uint8_t*)(p))
#define read_swap64(p) bswap_uint64(*(const uint64_t*)(const uint8_t*)(p))
#define write_swap16(p,v) (*(uint16_t*)(void*)(p)) = bswap_uint16(v)
#define write_swap32(p,v) (*(uint32_t*)(void*)(p)) = bswap_uint32(v)
#define write_swap64(p,v) (*(uint32_t*)(void*)(p)) = bswap_uint64(v)
/* /*
* Nibbled from https://github.com/hanji/popcnt/blob/master/populationcount.cpp * Nibbled from https://github.com/hanji/popcnt/blob/master/populationcount.cpp

View file

@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 242, 376 IDD_DIALOG DIALOGEX 12, 12, 242, 376
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_ACCEPTFILES EXSTYLE WS_EX_ACCEPTFILES
CAPTION "Rufus 2.8.875" CAPTION "Rufus 2.8.876"
FONT 8, "Segoe UI Symbol", 400, 0, 0x0 FONT 8, "Segoe UI Symbol", 400, 0, 0x0
BEGIN BEGIN
LTEXT "Device",IDS_DEVICE_TXT,9,6,200,8 LTEXT "Device",IDS_DEVICE_TXT,9,6,200,8
@ -320,8 +320,8 @@ END
// //
VS_VERSION_INFO VERSIONINFO VS_VERSION_INFO VERSIONINFO
FILEVERSION 2,8,875,0 FILEVERSION 2,8,876,0
PRODUCTVERSION 2,8,875,0 PRODUCTVERSION 2,8,876,0
FILEFLAGSMASK 0x3fL FILEFLAGSMASK 0x3fL
#ifdef _DEBUG #ifdef _DEBUG
FILEFLAGS 0x1L FILEFLAGS 0x1L
@ -338,13 +338,13 @@ BEGIN
BEGIN BEGIN
VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)" VALUE "CompanyName", "Akeo Consulting (http://akeo.ie)"
VALUE "FileDescription", "Rufus" VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "2.8.875" VALUE "FileVersion", "2.8.876"
VALUE "InternalName", "Rufus" VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "© 2011-2016 Pete Batard (GPL v3)" VALUE "LegalCopyright", "© 2011-2016 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html" VALUE "LegalTrademarks", "http://www.gnu.org/copyleft/gpl.html"
VALUE "OriginalFilename", "rufus.exe" VALUE "OriginalFilename", "rufus.exe"
VALUE "ProductName", "Rufus" VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "2.8.875" VALUE "ProductVersion", "2.8.876"
END END
END END
BLOCK "VarFileInfo" BLOCK "VarFileInfo"