Paweł Bylica
8 years ago
21 changed files with 22 additions and 1435 deletions
@ -1,119 +0,0 @@ |
|||
/*
|
|||
This file is part of ethash. |
|||
|
|||
ethash is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethash is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with ethash. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file io.c
|
|||
* @author Lefteris Karapetsas <lefteris@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
#include "io.h" |
|||
#include <string.h> |
|||
#include <stdio.h> |
|||
#include <errno.h> |
|||
|
|||
enum ethash_io_rc ethash_io_prepare( |
|||
char const* dirname, |
|||
ethash_h256_t const seedhash, |
|||
FILE** output_file, |
|||
uint64_t file_size, |
|||
bool force_create |
|||
) |
|||
{ |
|||
char mutable_name[DAG_MUTABLE_NAME_MAX_SIZE]; |
|||
enum ethash_io_rc ret = ETHASH_IO_FAIL; |
|||
// reset errno before io calls
|
|||
errno = 0; |
|||
|
|||
// assert directory exists
|
|||
if (!ethash_mkdir(dirname)) { |
|||
ETHASH_CRITICAL("Could not create the ethash directory"); |
|||
goto end; |
|||
} |
|||
|
|||
ethash_io_mutable_name(ETHASH_REVISION, &seedhash, mutable_name); |
|||
char* tmpfile = ethash_io_create_filename(dirname, mutable_name, strlen(mutable_name)); |
|||
if (!tmpfile) { |
|||
ETHASH_CRITICAL("Could not create the full DAG pathname"); |
|||
goto end; |
|||
} |
|||
|
|||
FILE *f; |
|||
if (!force_create) { |
|||
// try to open the file
|
|||
f = ethash_fopen(tmpfile, "rb+"); |
|||
if (f) { |
|||
size_t found_size; |
|||
if (!ethash_file_size(f, &found_size)) { |
|||
fclose(f); |
|||
ETHASH_CRITICAL("Could not query size of DAG file: \"%s\"", tmpfile); |
|||
goto free_memo; |
|||
} |
|||
if (file_size != found_size - ETHASH_DAG_MAGIC_NUM_SIZE) { |
|||
fclose(f); |
|||
ret = ETHASH_IO_MEMO_SIZE_MISMATCH; |
|||
goto free_memo; |
|||
} |
|||
// compare the magic number, no need to care about endianess since it's local
|
|||
uint64_t magic_num; |
|||
if (fread(&magic_num, ETHASH_DAG_MAGIC_NUM_SIZE, 1, f) != 1) { |
|||
// I/O error
|
|||
fclose(f); |
|||
ETHASH_CRITICAL("Could not read from DAG file: \"%s\"", tmpfile); |
|||
ret = ETHASH_IO_MEMO_SIZE_MISMATCH; |
|||
goto free_memo; |
|||
} |
|||
if (magic_num != ETHASH_DAG_MAGIC_NUM) { |
|||
fclose(f); |
|||
ret = ETHASH_IO_MEMO_SIZE_MISMATCH; |
|||
goto free_memo; |
|||
} |
|||
ret = ETHASH_IO_MEMO_MATCH; |
|||
goto set_file; |
|||
} |
|||
} |
|||
|
|||
// file does not exist, will need to be created
|
|||
f = ethash_fopen(tmpfile, "wb+"); |
|||
if (!f) { |
|||
ETHASH_CRITICAL("Could not create DAG file: \"%s\"", tmpfile); |
|||
goto free_memo; |
|||
} |
|||
// make sure it's of the proper size
|
|||
if (fseek(f, (long int)(file_size + ETHASH_DAG_MAGIC_NUM_SIZE - 1), SEEK_SET) != 0) { |
|||
fclose(f); |
|||
ETHASH_CRITICAL("Could not seek to the end of DAG file: \"%s\". Insufficient space?", tmpfile); |
|||
goto free_memo; |
|||
} |
|||
if (fputc('\n', f) == EOF) { |
|||
fclose(f); |
|||
ETHASH_CRITICAL("Could not write in the end of DAG file: \"%s\". Insufficient space?", tmpfile); |
|||
goto free_memo; |
|||
} |
|||
if (fflush(f) != 0) { |
|||
fclose(f); |
|||
ETHASH_CRITICAL("Could not flush at end of DAG file: \"%s\". Insufficient space?", tmpfile); |
|||
goto free_memo; |
|||
} |
|||
ret = ETHASH_IO_MEMO_MISMATCH; |
|||
goto set_file; |
|||
|
|||
ret = ETHASH_IO_MEMO_MATCH; |
|||
set_file: |
|||
*output_file = f; |
|||
free_memo: |
|||
free(tmpfile); |
|||
end: |
|||
return ret; |
|||
} |
@ -1,202 +0,0 @@ |
|||
/*
|
|||
This file is part of ethash. |
|||
|
|||
ethash is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethash is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with ethash. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file io.h
|
|||
* @author Lefteris Karapetsas <lefteris@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
#pragma once |
|||
#include <stdlib.h> |
|||
#include <stdint.h> |
|||
#include <stdbool.h> |
|||
#include <stdio.h> |
|||
#ifdef __cplusplus |
|||
#define __STDC_FORMAT_MACROS 1 |
|||
#endif |
|||
#include <inttypes.h> |
|||
#include "endian.h" |
|||
#include "ethash.h" |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
// Maximum size for mutable part of DAG file name
|
|||
// 6 is for "full-R", the suffix of the filename
|
|||
// 10 is for maximum number of digits of a uint32_t (for REVISION)
|
|||
// 1 is for - and 16 is for the first 16 hex digits for first 8 bytes of
|
|||
// the seedhash and last 1 is for the null terminating character
|
|||
// Reference: https://github.com/ethereum/wiki/wiki/Ethash-DAG
|
|||
#define DAG_MUTABLE_NAME_MAX_SIZE (6 + 10 + 1 + 16 + 1) |
|||
/// Possible return values of @see ethash_io_prepare
|
|||
enum ethash_io_rc { |
|||
ETHASH_IO_FAIL = 0, ///< There has been an IO failure
|
|||
ETHASH_IO_MEMO_SIZE_MISMATCH, ///< DAG with revision/hash match, but file size was wrong.
|
|||
ETHASH_IO_MEMO_MISMATCH, ///< The DAG file did not exist or there was revision/hash mismatch
|
|||
ETHASH_IO_MEMO_MATCH, ///< DAG file existed and revision/hash matched. No need to do anything
|
|||
}; |
|||
|
|||
// small hack for windows. I don't feel I should use va_args and forward just
|
|||
// to have this one function properly cross-platform abstracted
|
|||
#if defined(_WIN32) && !defined(__GNUC__) |
|||
#define snprintf(...) sprintf_s(__VA_ARGS__) |
|||
#endif |
|||
|
|||
/**
|
|||
* Logs a critical error in important parts of ethash. Should mostly help |
|||
* figure out what kind of problem (I/O, memory e.t.c.) causes a NULL |
|||
* ethash_full_t |
|||
*/ |
|||
#ifdef ETHASH_PRINT_CRITICAL_OUTPUT |
|||
#define ETHASH_CRITICAL(...) \ |
|||
do \ |
|||
{ \ |
|||
printf("ETHASH CRITICAL ERROR: "__VA_ARGS__); \ |
|||
printf("\n"); \ |
|||
fflush(stdout); \ |
|||
} while (0) |
|||
#else |
|||
#define ETHASH_CRITICAL(...) |
|||
#endif |
|||
|
|||
/**
|
|||
* Prepares io for ethash |
|||
* |
|||
* Create the DAG directory and the DAG file if they don't exist. |
|||
* |
|||
* @param[in] dirname A null terminated c-string of the path of the ethash |
|||
* data directory. If it does not exist it's created. |
|||
* @param[in] seedhash The seedhash of the current block number, used in the |
|||
* naming of the file as can be seen from the spec at: |
|||
* https://github.com/ethereum/wiki/wiki/Ethash-DAG
|
|||
* @param[out] output_file If there was no failure then this will point to an open |
|||
* file descriptor. User is responsible for closing it. |
|||
* In the case of memo match then the file is open on read |
|||
* mode, while on the case of mismatch a new file is created |
|||
* on write mode |
|||
* @param[in] file_size The size that the DAG file should have on disk |
|||
* @param[out] force_create If true then there is no check to see if the file |
|||
* already exists |
|||
* @return For possible return values @see enum ethash_io_rc |
|||
*/ |
|||
enum ethash_io_rc ethash_io_prepare( |
|||
char const* dirname, |
|||
ethash_h256_t const seedhash, |
|||
FILE** output_file, |
|||
uint64_t file_size, |
|||
bool force_create |
|||
); |
|||
|
|||
/**
|
|||
* An fopen wrapper for no-warnings crossplatform fopen. |
|||
* |
|||
* Msvc compiler considers fopen to be insecure and suggests to use their |
|||
* alternative. This is a wrapper for this alternative. Another way is to |
|||
* #define _CRT_SECURE_NO_WARNINGS, but disabling all security warnings does |
|||
* not sound like a good idea. |
|||
* |
|||
* @param file_name The path to the file to open |
|||
* @param mode Opening mode. Check fopen() |
|||
* @return The FILE* or NULL in failure |
|||
*/ |
|||
FILE* ethash_fopen(char const* file_name, char const* mode); |
|||
|
|||
/**
|
|||
* An strncat wrapper for no-warnings crossplatform strncat. |
|||
* |
|||
* Msvc compiler considers strncat to be insecure and suggests to use their |
|||
* alternative. This is a wrapper for this alternative. Another way is to |
|||
* #define _CRT_SECURE_NO_WARNINGS, but disabling all security warnings does |
|||
* not sound like a good idea. |
|||
* |
|||
* @param des Destination buffer |
|||
* @param dest_size Maximum size of the destination buffer. This is the |
|||
* extra argument for the MSVC secure strncat |
|||
* @param src Souce buffer |
|||
* @param count Number of bytes to copy from source |
|||
* @return If all is well returns the dest buffer. If there is an |
|||
* error returns NULL |
|||
*/ |
|||
char* ethash_strncat(char* dest, size_t dest_size, char const* src, size_t count); |
|||
|
|||
/**
|
|||
* A cross-platform mkdir wrapper to create a directory or assert it's there |
|||
* |
|||
* @param dirname The full path of the directory to create |
|||
* @return true if the directory was created or if it already |
|||
* existed |
|||
*/ |
|||
bool ethash_mkdir(char const* dirname); |
|||
|
|||
/**
|
|||
* Get a file's size |
|||
* |
|||
* @param[in] f The open file stream whose size to get |
|||
* @param[out] size Pass a size_t by reference to contain the file size |
|||
* @return true in success and false if there was a failure |
|||
*/ |
|||
bool ethash_file_size(FILE* f, size_t* ret_size); |
|||
|
|||
/**
|
|||
* Get a file descriptor number from a FILE stream |
|||
* |
|||
* @param f The file stream whose fd to get |
|||
* @return Platform specific fd handler |
|||
*/ |
|||
int ethash_fileno(FILE* f); |
|||
|
|||
/**
|
|||
* Create the filename for the DAG. |
|||
* |
|||
* @param dirname The directory name in which the DAG file should reside |
|||
* If it does not end with a directory separator it is appended. |
|||
* @param filename The actual name of the file |
|||
* @param filename_length The length of the filename in bytes |
|||
* @return A char* containing the full name. User must deallocate. |
|||
*/ |
|||
char* ethash_io_create_filename( |
|||
char const* dirname, |
|||
char const* filename, |
|||
size_t filename_length |
|||
); |
|||
|
|||
/**
|
|||
* Gets the default directory name for the DAG depending on the system |
|||
* |
|||
* The spec defining this directory is here: https://github.com/ethereum/wiki/wiki/Ethash-DAG
|
|||
* |
|||
* @param[out] strbuf A string buffer of sufficient size to keep the |
|||
* null termninated string of the directory name |
|||
* @param[in] buffsize Size of @a strbuf in bytes |
|||
* @return true for success and false otherwise |
|||
*/ |
|||
bool ethash_get_default_dirname(char* strbuf, size_t buffsize); |
|||
|
|||
static inline bool ethash_io_mutable_name( |
|||
uint32_t revision, |
|||
ethash_h256_t const* seed_hash, |
|||
char* output |
|||
) |
|||
{ |
|||
uint64_t hash = *((uint64_t*)seed_hash); |
|||
#if LITTLE_ENDIAN == BYTE_ORDER |
|||
hash = ethash_swap_u64(hash); |
|||
#endif |
|||
return snprintf(output, DAG_MUTABLE_NAME_MAX_SIZE, "full-R%u-%016" PRIx64, revision, hash) >= 0; |
|||
} |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
@ -1,111 +0,0 @@ |
|||
/*
|
|||
This file is part of ethash. |
|||
|
|||
ethash is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethash is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with ethash. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file io_posix.c
|
|||
* @author Lefteris Karapetsas <lefteris@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
|
|||
#include "io.h" |
|||
#include <sys/types.h> |
|||
#include <sys/stat.h> |
|||
#include <errno.h> |
|||
#include <libgen.h> |
|||
#include <stdio.h> |
|||
#include <unistd.h> |
|||
#include <stdlib.h> |
|||
#include <pwd.h> |
|||
|
|||
FILE* ethash_fopen(char const* file_name, char const* mode) |
|||
{ |
|||
return fopen(file_name, mode); |
|||
} |
|||
|
|||
char* ethash_strncat(char* dest, size_t dest_size, char const* src, size_t count) |
|||
{ |
|||
return strlen(dest) + count + 1 <= dest_size ? strncat(dest, src, count) : NULL; |
|||
} |
|||
|
|||
bool ethash_mkdir(char const* dirname) |
|||
{ |
|||
int rc = mkdir(dirname, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); |
|||
return rc != -1 || errno == EEXIST; |
|||
} |
|||
|
|||
int ethash_fileno(FILE *f) |
|||
{ |
|||
return fileno(f); |
|||
} |
|||
|
|||
char* ethash_io_create_filename( |
|||
char const* dirname, |
|||
char const* filename, |
|||
size_t filename_length |
|||
) |
|||
{ |
|||
size_t dirlen = strlen(dirname); |
|||
size_t dest_size = dirlen + filename_length + 1; |
|||
if (dirname[dirlen] != '/') { |
|||
dest_size += 1; |
|||
} |
|||
char* name = malloc(dest_size); |
|||
if (!name) { |
|||
return NULL; |
|||
} |
|||
|
|||
name[0] = '\0'; |
|||
ethash_strncat(name, dest_size, dirname, dirlen); |
|||
if (dirname[dirlen] != '/') { |
|||
ethash_strncat(name, dest_size, "/", 1); |
|||
} |
|||
ethash_strncat(name, dest_size, filename, filename_length); |
|||
return name; |
|||
} |
|||
|
|||
bool ethash_file_size(FILE* f, size_t* ret_size) |
|||
{ |
|||
struct stat st; |
|||
int fd; |
|||
if ((fd = fileno(f)) == -1 || fstat(fd, &st) != 0) { |
|||
return false; |
|||
} |
|||
*ret_size = st.st_size; |
|||
return true; |
|||
} |
|||
|
|||
bool ethash_get_default_dirname(char* strbuf, size_t buffsize) |
|||
{ |
|||
static const char dir_suffix[] = ".ethash/"; |
|||
strbuf[0] = '\0'; |
|||
char* home_dir = getenv("HOME"); |
|||
if (!home_dir || strlen(home_dir) == 0) |
|||
{ |
|||
struct passwd* pwd = getpwuid(getuid()); |
|||
if (pwd) |
|||
home_dir = pwd->pw_dir; |
|||
} |
|||
|
|||
size_t len = strlen(home_dir); |
|||
if (!ethash_strncat(strbuf, buffsize, home_dir, len)) { |
|||
return false; |
|||
} |
|||
if (home_dir[len] != '/') { |
|||
if (!ethash_strncat(strbuf, buffsize, "/", 1)) { |
|||
return false; |
|||
} |
|||
} |
|||
return ethash_strncat(strbuf, buffsize, dir_suffix, sizeof(dir_suffix)); |
|||
} |
@ -1,100 +0,0 @@ |
|||
/*
|
|||
This file is part of ethash. |
|||
|
|||
ethash is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethash is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with ethash. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file io_win32.c
|
|||
* @author Lefteris Karapetsas <lefteris@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
|
|||
#include "io.h" |
|||
#include <direct.h> |
|||
#include <errno.h> |
|||
#include <stdio.h> |
|||
#include <sys/stat.h> |
|||
#include <sys/types.h> |
|||
#include <shlobj.h> |
|||
|
|||
FILE* ethash_fopen(char const* file_name, char const* mode) |
|||
{ |
|||
FILE* f; |
|||
return fopen_s(&f, file_name, mode) == 0 ? f : NULL; |
|||
} |
|||
|
|||
char* ethash_strncat(char* dest, size_t dest_size, char const* src, size_t count) |
|||
{ |
|||
return strncat_s(dest, dest_size, src, count) == 0 ? dest : NULL; |
|||
} |
|||
|
|||
bool ethash_mkdir(char const* dirname) |
|||
{ |
|||
int rc = _mkdir(dirname); |
|||
return rc != -1 || errno == EEXIST; |
|||
} |
|||
|
|||
int ethash_fileno(FILE* f) |
|||
{ |
|||
return _fileno(f); |
|||
} |
|||
|
|||
char* ethash_io_create_filename( |
|||
char const* dirname, |
|||
char const* filename, |
|||
size_t filename_length |
|||
) |
|||
{ |
|||
size_t dirlen = strlen(dirname); |
|||
size_t dest_size = dirlen + filename_length + 1; |
|||
if (dirname[dirlen] != '\\' || dirname[dirlen] != '/') { |
|||
dest_size += 1; |
|||
} |
|||
char* name = malloc(dest_size); |
|||
if (!name) { |
|||
return NULL; |
|||
} |
|||
|
|||
name[0] = '\0'; |
|||
ethash_strncat(name, dest_size, dirname, dirlen); |
|||
if (dirname[dirlen] != '\\' || dirname[dirlen] != '/') { |
|||
ethash_strncat(name, dest_size, "\\", 1); |
|||
} |
|||
ethash_strncat(name, dest_size, filename, filename_length); |
|||
return name; |
|||
} |
|||
|
|||
bool ethash_file_size(FILE* f, size_t* ret_size) |
|||
{ |
|||
struct _stat st; |
|||
int fd; |
|||
if ((fd = _fileno(f)) == -1 || _fstat(fd, &st) != 0) { |
|||
return false; |
|||
} |
|||
*ret_size = st.st_size; |
|||
return true; |
|||
} |
|||
|
|||
bool ethash_get_default_dirname(char* strbuf, size_t buffsize) |
|||
{ |
|||
static const char dir_suffix[] = "Ethash\\"; |
|||
strbuf[0] = '\0'; |
|||
if (!SUCCEEDED(SHGetFolderPathA(NULL, CSIDL_LOCAL_APPDATA, NULL, 0, (CHAR*)strbuf))) { |
|||
return false; |
|||
} |
|||
if (!ethash_strncat(strbuf, buffsize, "\\", 1)) { |
|||
return false; |
|||
} |
|||
|
|||
return ethash_strncat(strbuf, buffsize, dir_suffix, sizeof(dir_suffix)); |
|||
} |
@ -1,47 +0,0 @@ |
|||
/*
|
|||
This file is part of ethash. |
|||
|
|||
ethash is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethash is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with ethash. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file mmap.h
|
|||
* @author Lefteris Karapetsas <lefteris@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
#pragma once |
|||
#if defined(__MINGW32__) || defined(_WIN32) |
|||
#include <sys/types.h> |
|||
|
|||
#define PROT_READ 0x1 |
|||
#define PROT_WRITE 0x2 |
|||
/* This flag is only available in WinXP+ */ |
|||
#ifdef FILE_MAP_EXECUTE |
|||
#define PROT_EXEC 0x4 |
|||
#else |
|||
#define PROT_EXEC 0x0 |
|||
#define FILE_MAP_EXECUTE 0 |
|||
#endif |
|||
|
|||
#define MAP_SHARED 0x01 |
|||
#define MAP_PRIVATE 0x02 |
|||
#define MAP_ANONYMOUS 0x20 |
|||
#define MAP_ANON MAP_ANONYMOUS |
|||
#define MAP_FAILED ((void *) -1) |
|||
|
|||
void* mmap(void* start, size_t length, int prot, int flags, int fd, off_t offset); |
|||
void munmap(void* addr, size_t length); |
|||
#else // posix, yay! ^_^
|
|||
#include <sys/mman.h> |
|||
#endif |
|||
|
|||
|
@ -1,84 +0,0 @@ |
|||
/* mmap() replacement for Windows
|
|||
* |
|||
* Author: Mike Frysinger <vapier@gentoo.org> |
|||
* Placed into the public domain |
|||
*/ |
|||
|
|||
/* References:
|
|||
* CreateFileMapping: http://msdn.microsoft.com/en-us/library/aa366537(VS.85).aspx
|
|||
* CloseHandle: http://msdn.microsoft.com/en-us/library/ms724211(VS.85).aspx
|
|||
* MapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366761(VS.85).aspx
|
|||
* UnmapViewOfFile: http://msdn.microsoft.com/en-us/library/aa366882(VS.85).aspx
|
|||
*/ |
|||
|
|||
#include <io.h> |
|||
#include <windows.h> |
|||
#include "mmap.h" |
|||
|
|||
#ifdef __USE_FILE_OFFSET64 |
|||
# define DWORD_HI(x) (x >> 32) |
|||
# define DWORD_LO(x) ((x) & 0xffffffff) |
|||
#else |
|||
# define DWORD_HI(x) (0) |
|||
# define DWORD_LO(x) (x) |
|||
#endif |
|||
|
|||
void* mmap(void* start, size_t length, int prot, int flags, int fd, off_t offset) |
|||
{ |
|||
if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) |
|||
return MAP_FAILED; |
|||
if (fd == -1) { |
|||
if (!(flags & MAP_ANON) || offset) |
|||
return MAP_FAILED; |
|||
} else if (flags & MAP_ANON) |
|||
return MAP_FAILED; |
|||
|
|||
DWORD flProtect; |
|||
if (prot & PROT_WRITE) { |
|||
if (prot & PROT_EXEC) |
|||
flProtect = PAGE_EXECUTE_READWRITE; |
|||
else |
|||
flProtect = PAGE_READWRITE; |
|||
} else if (prot & PROT_EXEC) { |
|||
if (prot & PROT_READ) |
|||
flProtect = PAGE_EXECUTE_READ; |
|||
else if (prot & PROT_EXEC) |
|||
flProtect = PAGE_EXECUTE; |
|||
} else |
|||
flProtect = PAGE_READONLY; |
|||
|
|||
off_t end = length + offset; |
|||
HANDLE mmap_fd, h; |
|||
if (fd == -1) |
|||
mmap_fd = INVALID_HANDLE_VALUE; |
|||
else |
|||
mmap_fd = (HANDLE)_get_osfhandle(fd); |
|||
h = CreateFileMapping(mmap_fd, NULL, flProtect, DWORD_HI(end), DWORD_LO(end), NULL); |
|||
if (h == NULL) |
|||
return MAP_FAILED; |
|||
|
|||
DWORD dwDesiredAccess; |
|||
if (prot & PROT_WRITE) |
|||
dwDesiredAccess = FILE_MAP_WRITE; |
|||
else |
|||
dwDesiredAccess = FILE_MAP_READ; |
|||
if (prot & PROT_EXEC) |
|||
dwDesiredAccess |= FILE_MAP_EXECUTE; |
|||
if (flags & MAP_PRIVATE) |
|||
dwDesiredAccess |= FILE_MAP_COPY; |
|||
void *ret = MapViewOfFile(h, dwDesiredAccess, DWORD_HI(offset), DWORD_LO(offset), length); |
|||
if (ret == NULL) { |
|||
ret = MAP_FAILED; |
|||
} |
|||
// since we are handling the file ourselves with fd, close the Windows Handle here
|
|||
CloseHandle(h); |
|||
return ret; |
|||
} |
|||
|
|||
void munmap(void* addr, size_t length) |
|||
{ |
|||
UnmapViewOfFile(addr); |
|||
} |
|||
|
|||
#undef DWORD_HI |
|||
#undef DWORD_LO |
@ -1,37 +0,0 @@ |
|||
/*
|
|||
This file is part of ethash. |
|||
|
|||
ethash is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethash is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with ethash. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
|
|||
/** @file sha3.cpp
|
|||
* @author Tim Hughes <tim@twistedfury.com> |
|||
* @date 2015 |
|||
*/ |
|||
#include <stdint.h> |
|||
#include <cryptopp/sha3.h> |
|||
|
|||
extern "C" { |
|||
struct ethash_h256; |
|||
typedef struct ethash_h256 ethash_h256_t; |
|||
void SHA3_256(ethash_h256_t const* ret, uint8_t const* data, size_t size) |
|||
{ |
|||
CryptoPP::SHA3_256().CalculateDigest((uint8_t*)ret, data, size); |
|||
} |
|||
|
|||
void SHA3_512(uint8_t* const ret, uint8_t const* data, size_t size) |
|||
{ |
|||
CryptoPP::SHA3_512().CalculateDigest(ret, data, size); |
|||
} |
|||
} |
@ -1,18 +0,0 @@ |
|||
#pragma once |
|||
|
|||
#include "compiler.h" |
|||
#include <stdint.h> |
|||
#include <stdlib.h> |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
struct ethash_h256; |
|||
|
|||
void SHA3_256(struct ethash_h256 const* ret, uint8_t const* data, size_t size); |
|||
void SHA3_512(uint8_t* const ret, uint8_t const* data, size_t size); |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
@ -1,41 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file util.c
|
|||
* @author Tim Hughes <tim@twistedfury.com> |
|||
* @date 2015 |
|||
*/ |
|||
#include <stdarg.h> |
|||
#include <stdio.h> |
|||
#include "util.h" |
|||
|
|||
#ifdef _MSC_VER |
|||
|
|||
// foward declare without all of Windows.h
|
|||
__declspec(dllimport) void __stdcall OutputDebugStringA(const char* lpOutputString); |
|||
|
|||
void debugf(const char *str, ...) |
|||
{ |
|||
va_list args; |
|||
va_start(args, str); |
|||
|
|||
char buf[1<<16]; |
|||
_vsnprintf_s(buf, sizeof(buf), sizeof(buf), str, args); |
|||
buf[sizeof(buf)-1] = '\0'; |
|||
OutputDebugStringA(buf); |
|||
} |
|||
|
|||
#endif |
@ -1,47 +0,0 @@ |
|||
/*
|
|||
This file is part of ethash. |
|||
|
|||
ethash is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethash is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with ethash. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file util.h
|
|||
* @author Tim Hughes <tim@twistedfury.com> |
|||
* @date 2015 |
|||
*/ |
|||
#pragma once |
|||
#include <stdint.h> |
|||
#include "compiler.h" |
|||
|
|||
#ifdef __cplusplus |
|||
extern "C" { |
|||
#endif |
|||
|
|||
#ifdef _MSC_VER |
|||
void debugf(char const* str, ...); |
|||
#else |
|||
#define debugf printf |
|||
#endif |
|||
|
|||
static inline uint32_t min_u32(uint32_t a, uint32_t b) |
|||
{ |
|||
return a < b ? a : b; |
|||
} |
|||
|
|||
static inline uint32_t clamp_u32(uint32_t x, uint32_t min_, uint32_t max_) |
|||
{ |
|||
return x < min_ ? min_ : (x > max_ ? max_ : x); |
|||
} |
|||
|
|||
#ifdef __cplusplus |
|||
} |
|||
#endif |
@ -1,38 +0,0 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file util.c
|
|||
* @author Tim Hughes <tim@twistedfury.com> |
|||
* @date 2015 |
|||
*/ |
|||
#include <stdarg.h> |
|||
#include <stdio.h> |
|||
#include "util.h" |
|||
|
|||
|
|||
// foward declare without all of Windows.h
|
|||
__declspec(dllimport) void __stdcall OutputDebugStringA(char const* lpOutputString); |
|||
|
|||
void debugf(char const* str, ...) |
|||
{ |
|||
va_list args; |
|||
va_start(args, str); |
|||
|
|||
char buf[1<<16]; |
|||
_vsnprintf_s(buf, sizeof(buf), sizeof(buf), str, args); |
|||
buf[sizeof(buf)-1] = '\0'; |
|||
OutputDebugStringA(buf); |
|||
} |
Loading…
Reference in new issue