diff --git a/README.md b/README.md index 1f3a6dd..8b83d53 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Copyright © 2005-2022 Silicondust USA Inc. . +Copyright © 2005-2023 Silicondust USA Inc. . This library implements the libhdhomerun protocol for use with Silicondust HDHomeRun TV tuners. diff --git a/hdhomerun_discover.c b/hdhomerun_discover.c index 6a375d6..8dae716 100644 --- a/hdhomerun_discover.c +++ b/hdhomerun_discover.c @@ -863,7 +863,7 @@ static void hdhomerun_discover_recv_internal_auth_bin(char **poutput, struct hdh int i; 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; raw24 = (uint32_t)hdhomerun_pkt_read_u8(rx_pkt) << 16; diff --git a/hdhomerun_os_posix.c b/hdhomerun_os_posix.c index 3191edf..90b38a0 100644 --- a/hdhomerun_os_posix.c +++ b/hdhomerun_os_posix.c @@ -65,27 +65,31 @@ static inline void clock_realtime_timespec(struct timespec *ts) #endif -static pthread_once_t random_get32_once = PTHREAD_ONCE_INIT; -static FILE *random_get32_fp = NULL; +static pthread_once_t random_getbytes_once = PTHREAD_ONCE_INIT; +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) { - pthread_once(&random_get32_once, random_get32_init); - - if (!random_get32_fp) { - return (uint32_t)getcurrenttime(); - } - uint32_t Result; - if (fread(&Result, 4, 1, random_get32_fp) != 1) { - return (uint32_t)getcurrenttime(); - } - + random_getbytes((uint8_t *)&Result, 4); return Result; } @@ -96,6 +100,18 @@ uint64_t getcurrenttime(void) 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) { uint64_t delay_s = ms / 1000; diff --git a/hdhomerun_os_posix.h b/hdhomerun_os_posix.h index 8643b64..319efec 100644 --- a/hdhomerun_os_posix.h +++ b/hdhomerun_os_posix.h @@ -63,8 +63,11 @@ typedef struct { extern "C" { #endif +extern LIBHDHOMERUN_API void random_getbytes(uint8_t *out, size_t length); extern LIBHDHOMERUN_API uint32_t random_get32(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_minimum(uint64_t ms); diff --git a/hdhomerun_os_windows.c b/hdhomerun_os_windows.c index 42fc741..70d814c 100644 --- a/hdhomerun_os_windows.c +++ b/hdhomerun_os_windows.c @@ -20,10 +20,15 @@ #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 Result = 0; - BCryptGenRandom(NULL, (uint8_t *)&Result, 4, BCRYPT_USE_SYSTEM_PREFERRED_RNG); + uint32_t Result; + random_getbytes((uint8_t *)&Result, 4); return Result; } @@ -32,6 +37,24 @@ uint64_t getcurrenttime(void) 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) { Sleep((DWORD)ms); diff --git a/hdhomerun_os_windows.h b/hdhomerun_os_windows.h index fecc60f..d1fab53 100644 --- a/hdhomerun_os_windows.h +++ b/hdhomerun_os_windows.h @@ -87,8 +87,11 @@ typedef HANDLE thread_cond_t; extern "C" { #endif +extern LIBHDHOMERUN_API void random_getbytes(uint8_t *out, size_t length); extern LIBHDHOMERUN_API uint32_t random_get32(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_minimum(uint64_t ms);