This commit is contained in:
nickkelsey 2023-04-28 10:50:23 -07:00
commit bc207f273c
6 changed files with 63 additions and 18 deletions

View file

@ -1,4 +1,4 @@
Copyright © 2005-2022 Silicondust USA Inc. <www.silicondust.com>. Copyright © 2005-2023 Silicondust USA Inc. <www.silicondust.com>.
This library implements the libhdhomerun protocol for use with Silicondust HDHomeRun TV tuners. This library implements the libhdhomerun protocol for use with Silicondust HDHomeRun TV tuners.

View file

@ -863,7 +863,7 @@ static void hdhomerun_discover_recv_internal_auth_bin(char **poutput, struct hdh
int i; int i;
for (i = 0; i < 24; i += 4) { for (i = 0; i < 24; i += 4) {
static char hdhomerun_discover_recv_base64_encode_table[64 + 1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; static const char hdhomerun_discover_recv_base64_encode_table[64 + 1] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
uint32_t raw24; uint32_t raw24;
raw24 = (uint32_t)hdhomerun_pkt_read_u8(rx_pkt) << 16; raw24 = (uint32_t)hdhomerun_pkt_read_u8(rx_pkt) << 16;

View file

@ -65,27 +65,31 @@ static inline void clock_realtime_timespec(struct timespec *ts)
#endif #endif
static pthread_once_t random_get32_once = PTHREAD_ONCE_INIT; static pthread_once_t random_getbytes_once = PTHREAD_ONCE_INIT;
static FILE *random_get32_fp = NULL; static FILE *random_getbytes_fp = NULL;
static void random_get32_init(void) static void random_getbytes_init(void)
{ {
random_get32_fp = fopen("/dev/urandom", "rb"); random_getbytes_fp = fopen("/dev/urandom", "rb");
}
void random_getbytes(uint8_t *out, size_t length)
{
pthread_once(&random_getbytes_once, random_getbytes_init);
if (!random_getbytes_fp) {
exit(1);
}
if (fread(out, 1, length, random_getbytes_fp) != length) {
exit(1);
}
} }
uint32_t random_get32(void) uint32_t random_get32(void)
{ {
pthread_once(&random_get32_once, random_get32_init);
if (!random_get32_fp) {
return (uint32_t)getcurrenttime();
}
uint32_t Result; uint32_t Result;
if (fread(&Result, 4, 1, random_get32_fp) != 1) { random_getbytes((uint8_t *)&Result, 4);
return (uint32_t)getcurrenttime();
}
return Result; return Result;
} }
@ -96,6 +100,18 @@ uint64_t getcurrenttime(void)
return ((uint64_t)ts.tv_sec * 1000) + (ts.tv_nsec / 1000000); return ((uint64_t)ts.tv_sec * 1000) + (ts.tv_nsec / 1000000);
} }
uint64_t timer_get_hires_ticks(void)
{
struct timespec ts;
clock_monotonic_timespec(&ts);
return ((uint64_t)ts.tv_sec * 1000000) + (ts.tv_nsec / 1000);
}
uint64_t timer_get_hires_frequency(void)
{
return 1000000;
}
void msleep_approx(uint64_t ms) void msleep_approx(uint64_t ms)
{ {
uint64_t delay_s = ms / 1000; uint64_t delay_s = ms / 1000;

View file

@ -63,8 +63,11 @@ typedef struct {
extern "C" { extern "C" {
#endif #endif
extern LIBHDHOMERUN_API void random_getbytes(uint8_t *out, size_t length);
extern LIBHDHOMERUN_API uint32_t random_get32(void); extern LIBHDHOMERUN_API uint32_t random_get32(void);
extern LIBHDHOMERUN_API uint64_t getcurrenttime(void); extern LIBHDHOMERUN_API uint64_t getcurrenttime(void);
extern LIBHDHOMERUN_API uint64_t timer_get_hires_ticks(void);
extern LIBHDHOMERUN_API uint64_t timer_get_hires_frequency(void);
extern LIBHDHOMERUN_API void msleep_approx(uint64_t ms); extern LIBHDHOMERUN_API void msleep_approx(uint64_t ms);
extern LIBHDHOMERUN_API void msleep_minimum(uint64_t ms); extern LIBHDHOMERUN_API void msleep_minimum(uint64_t ms);

View file

@ -20,10 +20,15 @@
#include "hdhomerun.h" #include "hdhomerun.h"
void random_getbytes(uint8_t *out, size_t length)
{
BCryptGenRandom(NULL, out, (ULONG)length, BCRYPT_USE_SYSTEM_PREFERRED_RNG);
}
uint32_t random_get32(void) uint32_t random_get32(void)
{ {
uint32_t Result = 0; uint32_t Result;
BCryptGenRandom(NULL, (uint8_t *)&Result, 4, BCRYPT_USE_SYSTEM_PREFERRED_RNG); random_getbytes((uint8_t *)&Result, 4);
return Result; return Result;
} }
@ -32,6 +37,24 @@ uint64_t getcurrenttime(void)
return GetTickCount64(); return GetTickCount64();
} }
uint64_t timer_get_hires_ticks(void)
{
LARGE_INTEGER count;
if (!QueryPerformanceCounter(&count)) {
return 0;
}
return count.QuadPart;
}
uint64_t timer_get_hires_frequency(void)
{
LARGE_INTEGER count;
if (!QueryPerformanceFrequency(&count)) {
return 0;
}
return count.QuadPart;
}
void msleep_approx(uint64_t ms) void msleep_approx(uint64_t ms)
{ {
Sleep((DWORD)ms); Sleep((DWORD)ms);

View file

@ -87,8 +87,11 @@ typedef HANDLE thread_cond_t;
extern "C" { extern "C" {
#endif #endif
extern LIBHDHOMERUN_API void random_getbytes(uint8_t *out, size_t length);
extern LIBHDHOMERUN_API uint32_t random_get32(void); extern LIBHDHOMERUN_API uint32_t random_get32(void);
extern LIBHDHOMERUN_API uint64_t getcurrenttime(void); extern LIBHDHOMERUN_API uint64_t getcurrenttime(void);
extern LIBHDHOMERUN_API uint64_t timer_get_hires_ticks(void);
extern LIBHDHOMERUN_API uint64_t timer_get_hires_frequency(void);
extern LIBHDHOMERUN_API void msleep_approx(uint64_t ms); extern LIBHDHOMERUN_API void msleep_approx(uint64_t ms);
extern LIBHDHOMERUN_API void msleep_minimum(uint64_t ms); extern LIBHDHOMERUN_API void msleep_minimum(uint64_t ms);