summaryrefslogtreecommitdiff
path: root/src/util.h
blob: c46ff724b65b1fe6ca0bc64b46ced521a23b4683 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//|_   _   _.     _  ._  |_   _.  _ |
//| | (/_ (_| \/ (/_ | | | | (_| (_ |<

#pragma once
#include <chrono>
#include <windows.h>
#include <stdio.h>
#include <thread>

#include "typedef.h"

constexpr U64 T_MS = 10000;
constexpr U64 T_SEC = 1000 * T_MS;
constexpr U64 T_MIN = 60 * T_SEC;
constexpr U64 T_HOUR = 60 * T_MIN;
constexpr U64 T_DAY = 24 * T_HOUR;

extern ULONG u_thread_create( LPTHREAD_START_ROUTINE routine, void* param = 0 );
extern ULONG u_thread_create( HANDLE proc, LPTHREAD_START_ROUTINE routine, void* param = 0 );
extern void  u_sleep( U64 ns );

template < U32 size >
struct STR {
  STR() = default;
  STR( const char* other ) { strcpy_s< size >( data, other ); }
  template < U32 size2 >
  STR( STR< size2 >&& other ) { strcpy_s< size >( data, other ); }

  operator char*() { return data; }
  
  char data[size]{};
};

template < U32 size >
struct WSTR {
  WSTR() = default;
  WSTR( const wchar_t* other ) { wstrcpy_s< size >( data, other ); }
  template < U32 size2 >
  WSTR( WSTR< size2 >&& other ) { wstrcpy_s< size >( data, other ); }

  operator wchar_t*() { return data; }
  
  wchar_t data[size]{};
};

template < typename t >
STR< 32 > u_num_to_string_hex( t num ) {
  STR< 32 > ret;

  sprintf( ret.data, "%08X", num );
  return ret;
}

template < typename t >
STR< 32 > u_num_to_string_dec( t num ) {
  STR< 32 > ret;

  sprintf( ret.data, "%lld", (I64)num );
  return ret;
}

template < U32 size = 128 >
STR< size > u_widebyte_to_ansi( const wchar_t* str ) {
  STR< size > ret;

  for( U32 i = 0; !!str[i]; ++i ) {
    ret.data[i] = str[i] & 0xff;
  }

  return ret;
}

template < U32 size = 128 >
WSTR< size > u_ansi_to_widebyte( const char* str ) {
  WSTR< size > ret;

  for( U32 i = 0; !!str[i]; ++i ) {
    ret.data[i] = str[i];
  }

  return ret;
}

inline U8 u_set_debug_privilege() {
  HANDLE           token;
  TOKEN_PRIVILEGES tkp{};

  if( !OpenProcessToken(
    GetCurrentProcess(),
    TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
    &token
  ) ) {
    return 0;
  }

  if( !LookupPrivilegeValueA( 0, SE_DEBUG_NAME, &tkp.Privileges->Luid ) ) {
    CloseHandle( token );
    return 0;
  }

  tkp.PrivilegeCount = 1;
  tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

  if( !AdjustTokenPrivileges( token, false, &tkp, 0, nullptr, 0 ) ) {
    CloseHandle( token );
    return 0;
  }

  CloseHandle( token );

  return 1;
}

inline U8 u_binary_match( U8* code, U8* pattern, U32 size ) {
  for( U32 i = 0; i < size; ++i ) {
    if( pattern[i] && (code[i] ^ pattern[i]) != 0 )
      return 0;
  }

  return 1;
}

inline U8* u_parse_signature( const char* sig, U32* out_len ) {
  U32 i, byte, len = strlen( sig );
  U8* sig_bytes = (U8*)malloc( len );

  for( i = 0, byte = 0; i < len; ++byte ) {
    if( sig[i] == ' ' )
      return 0;

    if( sig[i] == '?' ) {
      sig_bytes[byte] = 0;
      for( U32 i2 = i; i2 < len; ++i2 ) {
        if( sig[i2 + 1] == ' ' ) {
          i = i2 + 2;
          break;
        }
      }
        
      continue;
    }

    unsigned long temp;
    sscanf( &sig[i], "%02x", &temp );

    sig_bytes[byte] = (U8)( temp & 0xff );
    i += 3;
  }

  if( out_len )
    *out_len = byte;
  return sig_bytes;
}

template <typename t>
inline t u_vector_search( VECTOR<t> v, bool( *func)( t* t1 ) ) {
  for( auto& it : v ) {
    if( func( &it ) )
      return it;
  }

  return {};
}

inline U64 u_tick() {
  static LARGE_INTEGER freq;
  if( !freq.QuadPart )
    QueryPerformanceFrequency( &freq );

  LARGE_INTEGER ticks;
  QueryPerformanceCounter( &ticks );

  constexpr U64 wanted_precision = 10000000;
  if( wanted_precision == freq.QuadPart )
    return ticks.QuadPart;
  return (U64)(ticks.QuadPart / ((double)freq.QuadPart / wanted_precision) );
}

inline F64 u_time() {
  constexpr F64 NSEC_TO_SEC = 1.f / T_SEC;
  return u_tick() * NSEC_TO_SEC;
}