Skip to content

Commit

Permalink
[misc] move hash definitions to rufus.h
Browse files Browse the repository at this point in the history
* Also always add an extra NUL to read_file(), some additional macros in missing.h
  and fix some warnings in process.c.
  • Loading branch information
pbatard committed Mar 27, 2024
1 parent d3f78c4 commit a59389e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 54 deletions.
27 changes: 0 additions & 27 deletions src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,20 +97,6 @@
#define BUFFER_SIZE (64*KB)
#define WAIT_TIME 5000

/* Blocksize for each algorithm - Must be a power of 2 */
#define MD5_BLOCKSIZE 64
#define SHA1_BLOCKSIZE 64
#define SHA256_BLOCKSIZE 64
#define SHA512_BLOCKSIZE 128
#define MAX_BLOCKSIZE SHA512_BLOCKSIZE

/* Hashsize for each algorithm */
#define MD5_HASHSIZE 16
#define SHA1_HASHSIZE 20
#define SHA256_HASHSIZE 32
#define SHA512_HASHSIZE 64
#define MAX_HASHSIZE SHA512_HASHSIZE

/* Number of buffers we work with */
#define NUM_BUFFERS 3 // 2 + 1 as a mere double buffered async I/O
// would modify the buffer being processed.
Expand Down Expand Up @@ -178,16 +164,6 @@ static const uint64_t K512[80] = {
0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL
};

/*
* For convenience, we use a common context for all the hash algorithms,
* which means some elements may be unused...
*/
typedef struct ALIGNED(64) {
uint8_t buf[MAX_BLOCKSIZE];
uint64_t state[8];
uint64_t bytecount;
} HASH_CONTEXT;

static void md5_init(HASH_CONTEXT *ctx)
{
memset(ctx, 0, sizeof(*ctx));
Expand Down Expand Up @@ -1467,9 +1443,6 @@ static void null_write(HASH_CONTEXT *ctx, const uint8_t *buf, size_t len) { }
static void null_final(HASH_CONTEXT *ctx) { }
#endif

typedef void hash_init_t(HASH_CONTEXT *ctx);
typedef void hash_write_t(HASH_CONTEXT *ctx, const uint8_t *buf, size_t len);
typedef void hash_final_t(HASH_CONTEXT *ctx);
hash_init_t *hash_init[HASH_MAX] = { md5_init, sha1_init , sha256_init, sha512_init };
hash_write_t *hash_write[HASH_MAX] = { md5_write, sha1_write , sha256_write, sha512_write };
hash_final_t *hash_final[HASH_MAX] = { md5_final, sha1_final , sha256_final, sha512_final };
Expand Down
10 changes: 5 additions & 5 deletions src/missing.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif

#ifndef MAX
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#endif

#define LO_ALIGN_X_TO_Y(x, y) (((x) / (y)) * (y))
#define HI_ALIGN_X_TO_Y(x, y) ((((x) + (y) - 1) / (y)) * (y))

#if defined(__GNUC__)
#define ALIGNED(m) __attribute__ ((__aligned__(m)))
#elif defined(_MSC_VER)
#define ALIGNED(m) __declspec(align(m))
#endif
#define IS_HEXASCII(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))

/*
* Prefetch 64 bytes at address m, for read-only operation
Expand Down
16 changes: 8 additions & 8 deletions src/process.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ static DWORD WINAPI SearchProcessThread(LPVOID param)
goto out;
}
if (wHandleName != NULL) {
for (i = 0; i < nHandles; i++)
free(wHandleName[i]);
for (j = 0; j < nHandles; j++)
free(wHandleName[j]);
free(wHandleName);
}
safe_free(wHandleNameLen);
Expand All @@ -515,10 +515,10 @@ static DWORD WINAPI SearchProcessThread(LPVOID param)
ReleaseMutex(hLock);
goto out;
}
for (i = 0; i < nHandles; i++) {
wHandleName[i] = wcsdup(blocking_process.wHandleName[i]);
wHandleNameLen[i] = (USHORT)wcslen(blocking_process.wHandleName[i]);
if (wHandleName[i] == NULL) {
for (j = 0; j < nHandles; j++) {
wHandleName[j] = wcsdup(blocking_process.wHandleName[j]);
wHandleNameLen[j] = (USHORT)wcslen(blocking_process.wHandleName[j]);
if (wHandleName[j] == NULL) {
ReleaseMutex(hLock);
goto out;
}
Expand Down Expand Up @@ -741,8 +741,8 @@ static DWORD WINAPI SearchProcessThread(LPVOID param)
uprintf("Warning: Could not start process handle enumerator!");

if (wHandleName != NULL) {
for (i = 0; i < nHandles; i++)
free(wHandleName[i]);
for (j = 0; j < nHandles; j++)
free(wHandleName[j]);
free(wHandleName);
}
safe_free(wHandleNameLen);
Expand Down
53 changes: 45 additions & 8 deletions src/rufus.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,6 @@ enum image_option_type {
IMOP_MAX
};

enum hash_type {
HASH_MD5 = 0,
HASH_SHA1,
HASH_SHA256,
HASH_SHA512,
HASH_MAX
};

enum file_io_type {
FILE_IO_READ = 0,
FILE_IO_WRITE,
Expand Down Expand Up @@ -484,6 +476,51 @@ typedef struct {
uint32_t* address; // 32-bit will do, as we're not dealing with >4GB DLLs...
} dll_resolver_t;

/* Alignment macro */
#if defined(__GNUC__)
#define ALIGNED(m) __attribute__ ((__aligned__(m)))
#elif defined(_MSC_VER)
#define ALIGNED(m) __declspec(align(m))
#endif

/* Hash definitions */
enum hash_type {
HASH_MD5 = 0,
HASH_SHA1,
HASH_SHA256,
HASH_SHA512,
HASH_MAX
};

/* Blocksize for each hash algorithm - Must be a power of 2 */
#define MD5_BLOCKSIZE 64
#define SHA1_BLOCKSIZE 64
#define SHA256_BLOCKSIZE 64
#define SHA512_BLOCKSIZE 128
#define MAX_BLOCKSIZE SHA512_BLOCKSIZE

/* Hashsize for each hash algorithm */
#define MD5_HASHSIZE 16
#define SHA1_HASHSIZE 20
#define SHA256_HASHSIZE 32
#define SHA512_HASHSIZE 64
#define MAX_HASHSIZE SHA512_HASHSIZE

/* Context for the hash algorithms */
typedef struct ALIGNED(64) {
uint8_t buf[MAX_BLOCKSIZE];
uint64_t state[8];
uint64_t bytecount;
} HASH_CONTEXT;

/* Hash functions */
typedef void hash_init_t(HASH_CONTEXT* ctx);
typedef void hash_write_t(HASH_CONTEXT* ctx, const uint8_t* buf, size_t len);
typedef void hash_final_t(HASH_CONTEXT* ctx);
extern hash_init_t* hash_init[HASH_MAX];
extern hash_write_t* hash_write[HASH_MAX];
extern hash_final_t* hash_final[HASH_MAX];

#ifndef __VA_GROUP__
#define __VA_GROUP__(...) __VA_ARGS__
#endif
Expand Down
10 changes: 5 additions & 5 deletions src/rufus.rc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 232, 326
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_ACCEPTFILES
CAPTION "Rufus 4.5.2117"
CAPTION "Rufus 4.5.2118"
FONT 9, "Segoe UI Symbol", 400, 0, 0x0
BEGIN
LTEXT "Drive Properties",IDS_DRIVE_PROPERTIES_TXT,8,6,53,12,NOT WS_GROUP
Expand Down Expand Up @@ -392,8 +392,8 @@ END
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 4,5,2117,0
PRODUCTVERSION 4,5,2117,0
FILEVERSION 4,5,2118,0
PRODUCTVERSION 4,5,2118,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
Expand All @@ -411,13 +411,13 @@ BEGIN
VALUE "Comments", "https://rufus.ie"
VALUE "CompanyName", "Akeo Consulting"
VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "4.5.2117"
VALUE "FileVersion", "4.5.2118"
VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "� 2011-2024 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "https://www.gnu.org/licenses/gpl-3.0.html"
VALUE "OriginalFilename", "rufus-4.5.exe"
VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "4.5.2117"
VALUE "ProductVersion", "4.5.2118"
END
END
BLOCK "VarFileInfo"
Expand Down
5 changes: 4 additions & 1 deletion src/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ uint32_t read_file(const char* path, uint8_t** buf)
uint32_t size = (uint32_t)ftell(fd);
fseek(fd, 0L, SEEK_SET);

*buf = malloc(size);
// +1 so we can add an extra NUL
*buf = malloc(size + 1);
if (*buf == NULL) {
uprintf("Error: Can't allocate %d bytes buffer for file '%s'", size, path);
size = 0;
Expand All @@ -135,6 +136,8 @@ uint32_t read_file(const char* path, uint8_t** buf)
uprintf("Error: Can't read '%s'", path);
size = 0;
}
// Always NUL terminate the file
(*buf)[size] = 0;

out:
fclose(fd);
Expand Down

0 comments on commit a59389e

Please sign in to comment.