Skip to content

Commit

Permalink
Split platform code for beatmap.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
Ammon Smith committed Apr 12, 2017
1 parent 8626565 commit ff7532e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 89 deletions.
18 changes: 14 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
oppai
# Binaries
build/
/oppai
obj
*.o
*.[oad]

# Artifacts
*.tar.gz
tags

# Project settings
.ycm_extra_conf.py*

# Misc
*.swp
*~
tags
build/
*.bak

86 changes: 3 additions & 83 deletions beatmap.cc
Original file line number Diff line number Diff line change
@@ -1,89 +1,9 @@
#include <string>

// TODO: separate platform specific code
#ifdef _MSC_VER
#include <Windows.h>
#include <direct.h>
#include <Wincrypt.h>
#define mkdir _mkdir

internalfn
size_t get_exe_path(char* buf, size_t bufsize) {
return GetModuleFileNameA(0, buf, (u32)bufsize);
}

#define MD5_DIGEST_LENGTH 16

internalfn
void MD5(u8 const* buf, size_t buflen, u8* digest)
{
HCRYPTPROV prov;

if (!CryptAcquireContext(&prov, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
fprintf(stderr, "CryptAcquireContext 0x%08X\n", GetLastError());
return;
}

HCRYPTHASH hash;

if (!CryptCreateHash(prov, CALG_MD5, 0, 0, &hash))
{
fprintf(stderr, "CryptCreateHash 0x%08X\n", GetLastError());
goto cleanup;
}

if (!CryptHashData(hash, buf, (DWORD)buflen, 0))
{
fprintf(stderr, "CryptHashData 0x%08X\n", GetLastError());
goto cleanup2;
}

DWORD hashlen = MD5_DIGEST_LENGTH;

if (!CryptGetHashParam(hash, HP_HASHVAL, digest, &hashlen, 0))
{
fprintf(stderr, "CryptGetHashParam 0x%08X\n", GetLastError());
}

cleanup2:
CryptDestroyHash(hash);

cleanup:
CryptReleaseContext(prov, 0);
}
#if defined(_WIN32) | defined(_WIN64)
# include "beatmap_win.cc"
#else
#include <openssl/md5.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#define mkdir(x) mkdir(x, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)

internalfn
size_t get_exe_path(char* buf, size_t bufsize)
{
ssize_t res;

res = readlink("/proc/self/exe", buf, bufsize);
if (res >= 0) {
return (size_t)res;
}

res = readlink("/proc/curproc/file", buf, bufsize);
if (res >= 0) {
return (size_t)res;
}

res = readlink("/proc/self/path/a.out", buf, bufsize);
if (res >= 0) {
return (size_t)res;
}

perror("readlink");
strcpy(buf, ".");

return 1;
}
# include "beatmap_unix.cc"
#endif

// shit code ahead! I am way too lazy to write nice code for parsers, sorry
Expand Down
32 changes: 32 additions & 0 deletions beatmap_unix.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <openssl/md5.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#define mkdir(x) mkdir(x, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)

internalfn
size_t get_exe_path(char* buf, size_t bufsize)
{
ssize_t res;

res = readlink("/proc/self/exe", buf, bufsize);
if (res >= 0) {
return (size_t)res;
}

res = readlink("/proc/curproc/file", buf, bufsize);
if (res >= 0) {
return (size_t)res;
}

res = readlink("/proc/self/path/a.out", buf, bufsize);
if (res >= 0) {
return (size_t)res;
}

perror("readlink");
strcpy(buf, ".");

return 1;
}

51 changes: 51 additions & 0 deletions beatmap_win.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include <Windows.h>
#include <direct.h>
#include <Wincrypt.h>
#define mkdir _mkdir

internalfn
size_t get_exe_path(char* buf, size_t bufsize) {
return GetModuleFileNameA(0, buf, (u32)bufsize);
}

#define MD5_DIGEST_LENGTH 16

internalfn
void MD5(u8 const* buf, size_t buflen, u8* digest)
{
HCRYPTPROV prov;

if (!CryptAcquireContext(&prov, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
{
fprintf(stderr, "CryptAcquireContext 0x%08X\n", GetLastError());
return;
}

HCRYPTHASH hash;

if (!CryptCreateHash(prov, CALG_MD5, 0, 0, &hash))
{
fprintf(stderr, "CryptCreateHash 0x%08X\n", GetLastError());
goto cleanup;
}

if (!CryptHashData(hash, buf, (DWORD)buflen, 0))
{
fprintf(stderr, "CryptHashData 0x%08X\n", GetLastError());
goto cleanup2;
}

DWORD hashlen = MD5_DIGEST_LENGTH;

if (!CryptGetHashParam(hash, HP_HASHVAL, digest, &hashlen, 0))
{
fprintf(stderr, "CryptGetHashParam 0x%08X\n", GetLastError());
}

cleanup2:
CryptDestroyHash(hash);

cleanup:
CryptReleaseContext(prov, 0);
}

2 changes: 1 addition & 1 deletion common.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define internalfn static
#define globvar static

#if _WIN32 || _WIN64
#if defined(_WIN32) || defined(_WIN64)
#define NEEDS_TO_INSTALL_GENTOO 1
#define strtok_r strtok_s
#define __func__ __FUNCTION__
Expand Down
2 changes: 1 addition & 1 deletion types.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ typedef uint_least8_t u8;
#define fu32 "u"
#define fu16 "hu"

#if _WIN32 || _WIN64
#if defined(_WIN32) || defined(_WIN64)
#include <Windows.h>
typedef SSIZE_T ssize_t;
#endif
Expand Down

0 comments on commit ff7532e

Please sign in to comment.