Initial thread support.

This commit is contained in:
LoRd_MuldeR 2022-03-21 21:11:46 +01:00
parent c607046831
commit ddefc8c142
Signed by: mulder
GPG Key ID: 2B5913365F57E03F
11 changed files with 2132 additions and 30 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,183 @@
/*
* Module: sched.h
*
* Purpose:
* Provides an implementation of POSIX realtime extensions
* as defined in
*
* POSIX 1003.1b-1993 (POSIX.1b)
*
* --------------------------------------------------------------------------
*
* Pthreads-win32 - POSIX Threads Library for Win32
* Copyright(C) 1998 John E. Bossom
* Copyright(C) 1999,2005 Pthreads-win32 contributors
*
* Contact Email: rpj@callisto.canberra.edu.au
*
* The current list of contributors is contained
* in the file CONTRIBUTORS included with the source
* code distribution. The list can also be seen at the
* following World Wide Web location:
* http://sources.redhat.com/pthreads-win32/contributors.html
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library in the file COPYING.LIB;
* if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#if !defined(_SCHED_H)
#define _SCHED_H
#undef PTW32_SCHED_LEVEL
#if defined(_POSIX_SOURCE)
#define PTW32_SCHED_LEVEL 0
/* Early POSIX */
#endif
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309
#undef PTW32_SCHED_LEVEL
#define PTW32_SCHED_LEVEL 1
/* Include 1b, 1c and 1d */
#endif
#if defined(INCLUDE_NP)
#undef PTW32_SCHED_LEVEL
#define PTW32_SCHED_LEVEL 2
/* Include Non-Portable extensions */
#endif
#define PTW32_SCHED_LEVEL_MAX 3
#if ( defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112 ) || !defined(PTW32_SCHED_LEVEL)
#define PTW32_SCHED_LEVEL PTW32_SCHED_LEVEL_MAX
/* Include everything */
#endif
#if defined(__GNUC__) && !defined(__declspec)
# error Please upgrade your GNU compiler to one that supports __declspec.
#endif
/*
* When building the library, you should define PTW32_BUILD so that
* the variables/functions are exported correctly. When using the library,
* do NOT define PTW32_BUILD, and then the variables/functions will
* be imported correctly.
*/
#if !defined(PTW32_STATIC_LIB)
# if defined(PTW32_BUILD)
# define PTW32_DLLPORT __declspec (dllexport)
# else
# define PTW32_DLLPORT __declspec (dllimport)
# endif
#else
# define PTW32_DLLPORT
#endif
/*
* This is a duplicate of what is in the autoconf config.h,
* which is only used when building the pthread-win32 libraries.
*/
#if !defined(PTW32_CONFIG_H)
# if defined(WINCE)
# define NEED_ERRNO
# define NEED_SEM
# endif
# if defined(__MINGW64__)
# define HAVE_STRUCT_TIMESPEC
# define HAVE_MODE_T
# elif defined(_UWIN) || defined(__MINGW32__)
# define HAVE_MODE_T
# endif
#endif
/*
*
*/
#if PTW32_SCHED_LEVEL >= PTW32_SCHED_LEVEL_MAX
#if defined(NEED_ERRNO)
#include "need_errno.h"
#else
#include <errno.h>
#endif
#endif /* PTW32_SCHED_LEVEL >= PTW32_SCHED_LEVEL_MAX */
#if (defined(__MINGW64__) || defined(__MINGW32__)) || defined(_UWIN)
# if PTW32_SCHED_LEVEL >= PTW32_SCHED_LEVEL_MAX
/* For pid_t */
# include <sys/types.h>
/* Required by Unix 98 */
# include <time.h>
# else
typedef int pid_t;
# endif
#else
typedef int pid_t;
#endif
/* Thread scheduling policies */
enum {
SCHED_OTHER = 0,
SCHED_FIFO,
SCHED_RR,
SCHED_MIN = SCHED_OTHER,
SCHED_MAX = SCHED_RR
};
struct sched_param {
int sched_priority;
};
#if defined(__cplusplus)
extern "C"
{
#endif /* __cplusplus */
PTW32_DLLPORT int __cdecl sched_yield (void);
PTW32_DLLPORT int __cdecl sched_get_priority_min (int policy);
PTW32_DLLPORT int __cdecl sched_get_priority_max (int policy);
PTW32_DLLPORT int __cdecl sched_setscheduler (pid_t pid, int policy);
PTW32_DLLPORT int __cdecl sched_getscheduler (pid_t pid);
/*
* Note that this macro returns ENOTSUP rather than
* ENOSYS as might be expected. However, returning ENOSYS
* should mean that sched_get_priority_{min,max} are
* not implemented as well as sched_rr_get_interval.
* This is not the case, since we just don't support
* round-robin scheduling. Therefore I have chosen to
* return the same value as sched_setscheduler when
* SCHED_RR is passed to it.
*/
#define sched_rr_get_interval(_pid, _interval) \
( errno = ENOTSUP, (int) -1 )
#if defined(__cplusplus)
} /* End of extern "C" */
#endif /* __cplusplus */
#undef PTW32_SCHED_LEVEL
#undef PTW32_SCHED_LEVEL_MAX
#endif /* !_SCHED_H */

View File

@ -0,0 +1,169 @@
/*
* Module: semaphore.h
*
* Purpose:
* Semaphores aren't actually part of the PThreads standard.
* They are defined by the POSIX Standard:
*
* POSIX 1003.1b-1993 (POSIX.1b)
*
* --------------------------------------------------------------------------
*
* Pthreads-win32 - POSIX Threads Library for Win32
* Copyright(C) 1998 John E. Bossom
* Copyright(C) 1999,2005 Pthreads-win32 contributors
*
* Contact Email: rpj@callisto.canberra.edu.au
*
* The current list of contributors is contained
* in the file CONTRIBUTORS included with the source
* code distribution. The list can also be seen at the
* following World Wide Web location:
* http://sources.redhat.com/pthreads-win32/contributors.html
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library in the file COPYING.LIB;
* if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#if !defined( SEMAPHORE_H )
#define SEMAPHORE_H
#undef PTW32_SEMAPHORE_LEVEL
#if defined(_POSIX_SOURCE)
#define PTW32_SEMAPHORE_LEVEL 0
/* Early POSIX */
#endif
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309
#undef PTW32_SEMAPHORE_LEVEL
#define PTW32_SEMAPHORE_LEVEL 1
/* Include 1b, 1c and 1d */
#endif
#if defined(INCLUDE_NP)
#undef PTW32_SEMAPHORE_LEVEL
#define PTW32_SEMAPHORE_LEVEL 2
/* Include Non-Portable extensions */
#endif
#define PTW32_SEMAPHORE_LEVEL_MAX 3
#if !defined(PTW32_SEMAPHORE_LEVEL)
#define PTW32_SEMAPHORE_LEVEL PTW32_SEMAPHORE_LEVEL_MAX
/* Include everything */
#endif
#if defined(__GNUC__) && ! defined (__declspec)
# error Please upgrade your GNU compiler to one that supports __declspec.
#endif
/*
* When building the library, you should define PTW32_BUILD so that
* the variables/functions are exported correctly. When using the library,
* do NOT define PTW32_BUILD, and then the variables/functions will
* be imported correctly.
*/
#if !defined(PTW32_STATIC_LIB)
# if defined(PTW32_BUILD)
# define PTW32_DLLPORT __declspec (dllexport)
# else
# define PTW32_DLLPORT __declspec (dllimport)
# endif
#else
# define PTW32_DLLPORT
#endif
/*
* This is a duplicate of what is in the autoconf config.h,
* which is only used when building the pthread-win32 libraries.
*/
#if !defined(PTW32_CONFIG_H)
# if defined(WINCE)
# define NEED_ERRNO
# define NEED_SEM
# endif
# if defined(__MINGW64__)
# define HAVE_STRUCT_TIMESPEC
# define HAVE_MODE_T
# elif defined(_UWIN) || defined(__MINGW32__)
# define HAVE_MODE_T
# endif
#endif
/*
*
*/
#if PTW32_SEMAPHORE_LEVEL >= PTW32_SEMAPHORE_LEVEL_MAX
#if defined(NEED_ERRNO)
#include "need_errno.h"
#else
#include <errno.h>
#endif
#endif /* PTW32_SEMAPHORE_LEVEL >= PTW32_SEMAPHORE_LEVEL_MAX */
#define _POSIX_SEMAPHORES
#if defined(__cplusplus)
extern "C"
{
#endif /* __cplusplus */
#if !defined(HAVE_MODE_T)
typedef unsigned int mode_t;
#endif
typedef struct sem_t_ * sem_t;
PTW32_DLLPORT int __cdecl sem_init (sem_t * sem,
int pshared,
unsigned int value);
PTW32_DLLPORT int __cdecl sem_destroy (sem_t * sem);
PTW32_DLLPORT int __cdecl sem_trywait (sem_t * sem);
PTW32_DLLPORT int __cdecl sem_wait (sem_t * sem);
PTW32_DLLPORT int __cdecl sem_timedwait (sem_t * sem,
const struct timespec * abstime);
PTW32_DLLPORT int __cdecl sem_post (sem_t * sem);
PTW32_DLLPORT int __cdecl sem_post_multiple (sem_t * sem,
int count);
PTW32_DLLPORT int __cdecl sem_open (const char * name,
int oflag,
mode_t mode,
unsigned int value);
PTW32_DLLPORT int __cdecl sem_close (sem_t * sem);
PTW32_DLLPORT int __cdecl sem_unlink (const char * name);
PTW32_DLLPORT int __cdecl sem_getvalue (sem_t * sem,
int * sval);
#if defined(__cplusplus)
} /* End of extern "C" */
#endif /* __cplusplus */
#undef PTW32_SEMAPHORE_LEVEL
#undef PTW32_SEMAPHORE_LEVEL_MAX
#endif /* !SEMAPHORE_H */

Binary file not shown.

View File

@ -226,6 +226,7 @@
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<MinimumRequiredVersion>5.1</MinimumRequiredVersion>
<AdditionalLibraryDirectories>$(ProjectDir)../etc/deps/pthreadw32/lib/$(PlatformToolset)/debug/$(PlatformTarget);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -255,6 +256,7 @@
<GenerateDebugInformation>false</GenerateDebugInformation>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
<MinimumRequiredVersion>5.1</MinimumRequiredVersion>
<AdditionalLibraryDirectories>$(ProjectDir)../etc/deps/pthreadw32/lib/$(PlatformToolset)/static/$(PlatformTarget);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release_SSE2|Win32'">
@ -284,6 +286,7 @@
<GenerateDebugInformation>false</GenerateDebugInformation>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
<MinimumRequiredVersion>5.1</MinimumRequiredVersion>
<AdditionalLibraryDirectories>$(ProjectDir)../etc/deps/pthreadw32/lib/$(PlatformToolset)/static/$(PlatformTarget);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release_DLL|Win32'">
@ -313,6 +316,7 @@
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
<SubSystem>Console</SubSystem>
<MinimumRequiredVersion>5.1</MinimumRequiredVersion>
<AdditionalLibraryDirectories>$(ProjectDir)../etc/deps/pthreadw32/lib/$(PlatformToolset)/shared/$(PlatformTarget);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -330,6 +334,8 @@
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<MinimumRequiredVersion>5.2</MinimumRequiredVersion>
<AdditionalLibraryDirectories>$(ProjectDir)../etc/deps/pthreadw32/lib/$(PlatformToolset)/debug/$(PlatformTarget);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>pthreadVC2.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -359,6 +365,8 @@
<GenerateDebugInformation>false</GenerateDebugInformation>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
<MinimumRequiredVersion>5.2</MinimumRequiredVersion>
<AdditionalLibraryDirectories>$(ProjectDir)../etc/deps/pthreadw32/lib/$(PlatformToolset)/static/$(PlatformTarget);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>pthreadVC2.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release_SSE2|x64'">
@ -388,6 +396,8 @@
<GenerateDebugInformation>false</GenerateDebugInformation>
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
<MinimumRequiredVersion>5.2</MinimumRequiredVersion>
<AdditionalLibraryDirectories>$(ProjectDir)../etc/deps/pthreadw32/lib/$(PlatformToolset)/static/$(PlatformTarget);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>pthreadVC2.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release_DLL|x64'">
@ -417,6 +427,8 @@
<LinkTimeCodeGeneration>UseLinkTimeCodeGeneration</LinkTimeCodeGeneration>
<SubSystem>Console</SubSystem>
<MinimumRequiredVersion>5.2</MinimumRequiredVersion>
<AdditionalLibraryDirectories>$(ProjectDir)../etc/deps/pthreadw32/lib/$(PlatformToolset)/shared/$(PlatformTarget);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>pthreadVC2.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -38,12 +38,14 @@
<ClCompile Include="src\junk.c" />
<ClCompile Include="src\keygen.c" />
<ClCompile Include="src\slunkcrypt.c" />
<ClCompile Include="src\thread.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\slunkcrypt.h" />
<ClInclude Include="include\slunkcrypt.hpp" />
<ClInclude Include="src\compiler.h" />
<ClInclude Include="src\keygen.h" />
<ClInclude Include="src\thread.h" />
<ClInclude Include="src\version.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
@ -201,7 +203,7 @@
<PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<AdditionalIncludeDirectories>$(ProjectDir)include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir)include;$(ProjectDir)..\etc\deps\pthreadw32\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4706;4204</DisableSpecificWarnings>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<EnableEnhancedInstructionSet>NoExtensions</EnableEnhancedInstructionSet>
@ -220,7 +222,7 @@
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<AdditionalIncludeDirectories>$(ProjectDir)include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir)include;$(ProjectDir)..\etc\deps\pthreadw32\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<Optimization>MaxSpeed</Optimization>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
@ -252,7 +254,7 @@
<PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<AdditionalIncludeDirectories>$(ProjectDir)include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir)include;$(ProjectDir)..\etc\deps\pthreadw32\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<Optimization>MaxSpeed</Optimization>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
@ -284,7 +286,7 @@
<PreprocessorDefinitions>WIN32;NDEBUG;_DLL;SLUNKCRYPT_SHARED=1;SLUNKCRYPT_EXPORT=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<AdditionalIncludeDirectories>$(ProjectDir)include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir)include;$(ProjectDir)..\etc\deps\pthreadw32\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<Optimization>MaxSpeed</Optimization>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
@ -312,7 +314,7 @@
<PreprocessorDefinitions>_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<AdditionalIncludeDirectories>$(ProjectDir)include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir)include;$(ProjectDir)..\etc\deps\pthreadw32\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>4706;4204</DisableSpecificWarnings>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<EnableEnhancedInstructionSet>NotSet</EnableEnhancedInstructionSet>
@ -331,7 +333,7 @@
<PreprocessorDefinitions>NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<AdditionalIncludeDirectories>$(ProjectDir)include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir)include;$(ProjectDir)..\etc\deps\pthreadw32\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<Optimization>MaxSpeed</Optimization>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
@ -363,7 +365,7 @@
<PreprocessorDefinitions>NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<AdditionalIncludeDirectories>$(ProjectDir)include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir)include;$(ProjectDir)..\etc\deps\pthreadw32\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<Optimization>MaxSpeed</Optimization>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
@ -395,7 +397,7 @@
<PreprocessorDefinitions>NDEBUG;_DLL;SLUNKCRYPT_SHARED=1;SLUNKCRYPT_EXPORT=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<AdditionalIncludeDirectories>$(ProjectDir)include</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir)include;$(ProjectDir)..\etc\deps\pthreadw32\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<Optimization>MaxSpeed</Optimization>
<InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
<FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>

View File

@ -24,6 +24,9 @@
<ClCompile Include="src\keygen.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\thread.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="include\slunkcrypt.h">
@ -41,5 +44,8 @@
<ClInclude Include="src\compiler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\thread.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -7,6 +7,7 @@
#include "slunkcrypt.h"
#include "compiler.h"
#include "keygen.h"
#include "thread.h"
#include "version.h"
/* CRT */
@ -35,12 +36,24 @@ rand_state_t;
typedef struct
{
int reverse_mode;
uint8_t wheel[256U][256U];
const uint8_t (*wheel)[256U];
uint32_t counter;
rand_state_t random;
uint8_t *data;
}
thread_state_t;
typedef struct
{
uint8_t wheel[256U][256U];
thrdpl_t thread_pool;
size_t thread_idx;
thread_state_t thread_data[MAX_THREADS];
}
crypt_state_t;
#define THREAD_COUNT 1U
// ==========================================================================
// Abort flag
// ==========================================================================
@ -117,27 +130,37 @@ static INLINE void random_seed(rand_state_t *const state, uint64_t salt, const u
// Initialization
// ==========================================================================
static int initialize_state(crypt_state_t *const state, const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int mode)
static int initialize_state(crypt_state_t *const state, const uint64_t nonce, const uint8_t *const passwd, const size_t passwd_len, const int mode, const int reset)
{
uint8_t temp[256U][256U];
size_t r, i;
const int reverse = BOOLIFY(mode);
rand_state_t random;
uint32_t counter;
const int reverse_mode = BOOLIFY(mode);
/* backup previous value */
const thrdpl_t thread_pool = reset ? state->thread_pool : THRDPL_NULL;
/* initialize state */
slunkcrypt_bzero(state, sizeof(crypt_state_t));
state->reverse_mode = reverse;
/* create thread-pool */
if ((state->thread_pool = reset ? thread_pool : thrdpl_create(THREAD_COUNT)) == THRDPL_NULL)
{
return SLUNKCRYPT_FAILURE;
}
/* initialize counter */
random_seed(&state->random, nonce, (uint16_t)(-1), passwd, passwd_len);
state->counter = random_next(&state->random);
random_seed(&random, nonce, (uint16_t)(-1), passwd, passwd_len);
counter = random_next(&random);
/* set up the wheel permutations */
for (r = 0U; r < 256U; ++r)
{
random_seed(&state->random, nonce, (uint16_t)r, passwd, passwd_len);
random_seed(&random, nonce, (uint16_t)r, passwd, passwd_len);
for (i = 0U; i < 256U; ++i)
{
const size_t j = random_next(&state->random) % (i + 1U);
const size_t j = random_next(&random) % (i + 1U);
if (j != i)
{
state->wheel[r][i] = state->wheel[r][j];
@ -148,7 +171,7 @@ static int initialize_state(crypt_state_t *const state, const uint64_t nonce, co
}
/* reverse the wheels, if requested */
if (reverse)
if (reverse_mode)
{
for (r = 0U; r < 256U; ++r)
{
@ -165,12 +188,32 @@ static int initialize_state(crypt_state_t *const state, const uint64_t nonce, co
CHECK_ABORTED();
}
random_seed(&state->random, nonce, 256U, passwd, passwd_len);
/* set up thread state */
random_seed(&random, nonce, 256U, passwd, passwd_len);
for (i = 0U; i < THREAD_COUNT; ++i)
{
state->thread_data[i].reverse_mode = reverse_mode;
state->thread_data[i].wheel = state->wheel;
state->thread_data[i].counter = counter + ((uint32_t)i);
memcpy(&state->thread_data[i].random, &random, sizeof(rand_state_t));
for (r = 0U; r < i * 63U; ++r)
{
random_next(&state->thread_data[i].random);
}
CHECK_ABORTED();
}
slunkcrypt_bzero(&counter, sizeof(uint32_t));
slunkcrypt_bzero(&random, sizeof(rand_state_t));
return SLUNKCRYPT_SUCCESS;
/* aborted */
aborted:
thrdpl_destroy(state->thread_pool);
slunkcrypt_bzero(state, sizeof(crypt_state_t));
slunkcrypt_bzero(&counter, sizeof(uint32_t));
slunkcrypt_bzero(&random, sizeof(rand_state_t));
return SLUNKCRYPT_ABORTED;
}
@ -189,18 +232,22 @@ static INLINE void update_offset(uint8_t *const offset, uint32_t seed, rand_stat
}
offset[reverse ? (255U - i) : i] = (uint8_t)seed;
}
for (i = 0U; i < 63U * (THREAD_COUNT - 1U); ++i)
{
random_next(state);
}
}
static INLINE uint8_t process_next_symbol(crypt_state_t *const state, uint8_t value)
static INLINE void process_next_symbol(thread_state_t *const state)
{
uint8_t offset[256U];
size_t i;
update_offset(offset, state->counter++, &state->random, state->reverse_mode);
update_offset(offset, state->counter, &state->random, state->reverse_mode);
for (i = 0U; i < 256U; ++i)
{
value = (state->wheel[i][(value + offset[i]) & 0xFF] - offset[i]) & 0xFF;
*state->data = (state->wheel[i][(*state->data + offset[i]) & 0xFF] - offset[i]) & 0xFF;
}
return value;
state->counter += THREAD_COUNT;
}
// ==========================================================================
@ -235,7 +282,7 @@ slunkcrypt_t slunkcrypt_alloc(const uint64_t nonce, const uint8_t *const passwd,
{
return SLUNKCRYPT_NULL;
}
if (initialize_state(state, nonce, passwd, passwd_len, mode) == SLUNKCRYPT_SUCCESS)
if (initialize_state(state, nonce, passwd, passwd_len, mode, 0) == SLUNKCRYPT_SUCCESS)
{
return ((slunkcrypt_t)state);
}
@ -254,7 +301,7 @@ int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t nonce, const uin
{
return SLUNKCRYPT_FAILURE;
}
if ((result = initialize_state(state, nonce, passwd, passwd_len, mode)) != SLUNKCRYPT_SUCCESS)
if ((result = initialize_state(state, nonce, passwd, passwd_len, mode, 1)) != SLUNKCRYPT_SUCCESS)
{
slunkcrypt_bzero(state, sizeof(crypt_state_t));
}
@ -263,6 +310,7 @@ int slunkcrypt_reset(const slunkcrypt_t context, const uint64_t nonce, const uin
int slunkcrypt_process(const slunkcrypt_t context, const uint8_t *const input, uint8_t *const output, size_t length)
{
size_t i;
crypt_state_t *const state = (crypt_state_t*)context;
if (!state)
{
@ -271,23 +319,26 @@ int slunkcrypt_process(const slunkcrypt_t context, const uint8_t *const input, u
if (length > 0U)
{
size_t i;
memcpy(output, input, length * sizeof(uint8_t));
for (i = 0; i < length; ++i)
{
output[i] = process_next_symbol(state, input[i]);
abort(); //process_next_symbol(state, output + i);
CHECK_ABORTED();
}
}
thrdpl_await(state->thread_pool);
return SLUNKCRYPT_SUCCESS;
aborted:
thrdpl_await(state->thread_pool);
slunkcrypt_bzero(state, sizeof(crypt_state_t));
return SLUNKCRYPT_ABORTED;
}
int slunkcrypt_inplace(const slunkcrypt_t context, uint8_t *const buffer, size_t length)
{
size_t i;
crypt_state_t *const state = (crypt_state_t*)context;
if (!state)
{
@ -296,17 +347,24 @@ int slunkcrypt_inplace(const slunkcrypt_t context, uint8_t *const buffer, size_t
if (length > 0U)
{
size_t i;
for (i = 0; i < length; ++i)
{
buffer[i] = process_next_symbol(state, buffer[i]);
state->thread_data[state->thread_idx].data = buffer + i;
//process_next_symbol(&state->thread_data[state->thread_idx]);
thrdpl_submit(state->thread_pool, process_next_symbol, &state->thread_data[state->thread_idx]);
if (++state->thread_idx >= THREAD_COUNT)
{
state->thread_idx = 0U;
}
CHECK_ABORTED();
}
}
thrdpl_await(state->thread_pool);
return SLUNKCRYPT_SUCCESS;
aborted:
thrdpl_await(state->thread_pool);
slunkcrypt_bzero(state, sizeof(crypt_state_t));
return SLUNKCRYPT_ABORTED;
}
@ -316,6 +374,7 @@ void slunkcrypt_free(const slunkcrypt_t context)
crypt_state_t *const state = (crypt_state_t*)context;
if (state)
{
thrdpl_destroy(state->thread_pool);
slunkcrypt_bzero(state, sizeof(crypt_state_t));
free(state);
}

273
libslunkcrypt/src/thread.c Normal file
View File

@ -0,0 +1,273 @@
/******************************************************************************/
/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
/* This work has been released under the CC0 1.0 Universal license! */
/******************************************************************************/
#ifdef _MSC_VER
#define PTW32_STATIC_LIB 1
#endif
/* Internal */
#include "slunkcrypt.h"
#include "thread.h"
/* CRT */
#include <stdlib.h>
/* PThread */
#include <pthread.h>
#include <semaphore.h>
typedef struct
{
thrdpl_worker_t worker;
void *args;
}
thrdpl_task_t;
typedef struct
{
size_t index;
pthread_mutex_t *mutex;
pthread_cond_t *ready;
size_t *queue;
sem_t sem_free, sem_used;
pthread_t thread;
thrdpl_task_t task;
}
thrdpl_thread_t;
typedef struct
{
size_t thread_count, index, queue;
pthread_mutex_t mutex;
pthread_cond_t ready;
thrdpl_thread_t threads[MAX_THREADS];
}
thrdpl_data_t;
// ==========================================================================
// Thread main
// ==========================================================================
static void *thread_main(void *const arg)
{
thrdpl_thread_t *const data = (thrdpl_thread_t*)arg;
for (;;)
{
if (sem_wait(&data->sem_used) != 0)
{
abort();
}
if (pthread_mutex_lock(data->mutex) != 0)
{
abort();
}
const thrdpl_worker_t worker = data->task.worker;
void *const args = data->task.args;
if (pthread_mutex_unlock(data->mutex) != 0)
{
abort();
}
worker(args);
if (pthread_mutex_lock(data->mutex) != 0)
{
abort();
}
if (!(*data->queue -= 1U))
{
if (pthread_cond_broadcast(data->ready) != 0)
{
abort();
}
}
if (pthread_mutex_unlock(data->mutex) != 0)
{
abort();
}
if (sem_post(&data->sem_free) != 0)
{
abort();
}
}
}
// ==========================================================================
// Manage threads
// ==========================================================================
static int create_thread(const size_t index, thrdpl_thread_t *const thread_data, pthread_mutex_t *const mutex, pthread_cond_t *const ready, size_t *const queue)
{
thread_data->index = index;
thread_data->mutex = mutex;
thread_data->ready = ready;
thread_data->queue = queue;
if (sem_init(&thread_data->sem_free, 0, 1U) != 0)
{
return -1;
}
if (sem_init(&thread_data->sem_used, 0, 0U) != 0)
{
sem_destroy(&thread_data->sem_free);
return -1;
}
if (pthread_create(&thread_data->thread, NULL, thread_main, thread_data) != 0)
{
sem_destroy(&thread_data->sem_used);
sem_destroy(&thread_data->sem_free);
return -1;
}
return 0;
}
static int destroy_thread(thrdpl_thread_t *const thread_data)
{
pthread_cancel(thread_data->thread);
pthread_join(thread_data->thread, NULL);
sem_destroy(&thread_data->sem_used);
sem_destroy(&thread_data->sem_free);
return 0;
}
// ==========================================================================
// Thread pool API
// ==========================================================================
thrdpl_t thrdpl_create(const size_t count)
{
size_t i, j;
thrdpl_data_t *pool = NULL;
if ((count < 1U) || (count > MAX_THREADS))
{
return THRDPL_NULL;
}
if (!(pool = (thrdpl_data_t*) malloc(sizeof(thrdpl_data_t))))
{
return THRDPL_NULL;
}
slunkcrypt_bzero(pool, sizeof(thrdpl_data_t));
pool->thread_count = count;
if (pthread_mutex_init(&pool->mutex, NULL) != 0)
{
goto failure;
}
if (pthread_cond_init(&pool->ready, NULL) != 0)
{
pthread_mutex_destroy(&pool->mutex);
goto failure;
}
for (i = 0U; i < count; ++i)
{
if (create_thread(i, &pool->threads[i], &pool->mutex, &pool->ready, &pool->queue) != 0)
{
for (j = 0U; j < i; ++j)
{
destroy_thread(&pool->threads[j]);
}
pthread_cond_destroy(&pool->ready);
pthread_mutex_destroy(&pool->mutex);
goto failure;
}
}
return (thrdpl_t)pool;
failure:
free(pool);
return (thrdpl_t)NULL;
}
void thrdpl_submit(const thrdpl_t thrdpl, const thrdpl_worker_t worker, void *const args)
{
thrdpl_data_t *const pool = (thrdpl_data_t*)thrdpl;
if (pthread_mutex_lock(&pool->mutex) != 0)
{
abort();
}
thrdpl_thread_t *const thread = &pool->threads[pool->index];
if (++pool->index >= pool->thread_count)
{
pool->index = 0U;
}
++pool->queue;
if (pthread_mutex_unlock(&pool->mutex) != 0)
{
abort();
}
if (sem_wait(&thread->sem_free) != 0)
{
abort();
}
thread->task.worker = worker;
thread->task.args = args;
if (sem_post(&thread->sem_used) != 0)
{
abort();
}
}
void thrdpl_await(const thrdpl_t thrdpl)
{
thrdpl_data_t *const pool = (thrdpl_data_t*)thrdpl;
if (pthread_mutex_lock(&pool->mutex) != 0)
{
abort();
}
while (pool->queue)
{
if (pthread_cond_wait(&pool->ready, &pool->mutex) != 0)
{
abort();
}
}
if (pthread_mutex_unlock(&pool->mutex) != 0)
{
abort();
}
}
void thrdpl_destroy(const thrdpl_t thrdpl)
{
size_t i;
thrdpl_data_t *const pool = (thrdpl_data_t*)thrdpl;
if (pool)
{
for (i = 0U; i < pool->thread_count; ++i)
{
destroy_thread(&pool->threads[i]);
}
pthread_cond_destroy(&pool->ready);
pthread_mutex_destroy(&pool->mutex);
free(pool);
}
}

View File

@ -0,0 +1,23 @@
/******************************************************************************/
/* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
/* This work has been released under the CC0 1.0 Universal license! */
/******************************************************************************/
#ifndef INC_SLUNKCRYPT_THREAD_H
#define INC_SLUNKCRYPT_THREAD_H
#include <stdlib.h>
#include <stdint.h>
#define MAX_THREADS 8U
#define THRDPL_NULL ((thrdpl_t)NULL)
typedef void (*thrdpl_worker_t)(void *arguments);
typedef uintptr_t thrdpl_t;
thrdpl_t thrdpl_create(const size_t count);
void thrdpl_submit(const thrdpl_t thrdpl, const thrdpl_worker_t worker, void *const arguments);
void thrdpl_await(const thrdpl_t thrdpl);
void thrdpl_destroy(const thrdpl_t thrdpl);
#endif

View File

@ -7,7 +7,7 @@
#define INC_SLUNKCRYPT_VERSION_H
#define LIB_VERSION_MAJOR 1
#define LIB_VERSION_MINOR 1
#define LIB_VERSION_PATCH 2
#define LIB_VERSION_MINOR 2
#define LIB_VERSION_PATCH 0
#endif