-
Notifications
You must be signed in to change notification settings - Fork 8
/
misc.cpp
510 lines (445 loc) · 12.4 KB
/
misc.cpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#include "Misc.h"
#include "RegistryFunctions.h"
// ----
float GetTimeDifference(LPSYSTEMTIME _Now, LPSYSTEMTIME _Then)
{
float ret = 0.0f;
if ((_Now) && (_Then))
{
ret += (float)((_Now->wYear - _Then->wYear) * 60 * 60 * 24 * 32 * 14);
ret += (float)((_Now->wMonth - _Then->wMonth) * 60 * 60 * 24 * 32);
ret += (float)((_Now->wDay - _Then->wDay) * 60 * 60 * 24);
ret += (float)((_Now->wHour - _Then->wHour) * 60 * 60);
ret += (float)((_Now->wMinute - _Then->wMinute) * 60);
ret += (float)((_Now->wSecond - _Then->wSecond));
ret += (float)((_Now->wMilliseconds - _Then->wMilliseconds)) / 1000.0f;
}
return ret;
}
// ----
// Tells you the length of a file - "_filelength" is not provided for CE.
long GetFileSize(CString* _FileName)
{
FILE *FH;
long ret = -1;
if (_FileName && _FileName->GetString())
{
FH = FOPEN(_FileName->GetString(), "rb");
if (FH)
{
fseek(FH, 0, SEEK_END); // Seek end
ret = ftell(FH);
fclose(FH);
}
}
return ret;
}
// ----
// Returns true if the point is within the bounds of the
// rect - the test is inclusive on the top left edge, and
// exclusive on the bottom right.
bool IsPointInRect(POINT* _Pos, RECT* _Rect)
{
return ( (_Pos)
&& (_Rect)
&& (_Pos->y >= _Rect->top)
&& (_Pos->y < _Rect->bottom)
&& (_Pos->x >= _Rect->left)
&& (_Pos->x < _Rect->right)
);
}
// ----
// Rounds _Value to the nearest _Span.
// Modes
// -1 => round down
// 0 => round at mid point
// 1 => round up
int Round(int _Value, int _Snap, int _Mode)
{
switch (_Mode)
{
case -1:
return ((_Value / _Snap) * _Snap);
case 0:
return (((_Value + (_Snap >> 1)) / _Snap) * _Snap);
case 1:
return (((_Value + (_Snap >> 1)) / _Snap) * _Snap);
default:
NODEFAULT;
return 0;
}
}
// ----
// Returns whether the file could be opened.
bool TestReadFile(CString* _FileName)
{
FILE *FH;
if (_FileName && _FileName->GetString())
{
FH = FOPEN(_FileName->GetString(), "rb");
if (FH)
{
fclose(FH);
return true;
}
}
return false;
}
// ----
// Really complicated encryption algorithm
void ChangeEncryptionState(char* _Buffer, int _Count)
{
if ((_Count < 1) || (!_Buffer))
return;
for (int i = _Count; i--;)
{
*(_Buffer + i) = (*(_Buffer + i)) ^ 0xFF;
}
}
// Convert from portrait mode co-ordiantes to the
// co-ordinates for the specified orientation.
void ConvertCoordSpace(POINT _A, POINT *_B, EOrientation _C)
{
switch (_C)
{
case EO_Portrait_Normal:
_B->x = _A.x;
_B->y = _A.y;
break;
case EO_Landscape_Normal:
_B->x = _A.y;
_B->y = 239 - _A.x;
break;
case EO_Landscape_Flipped:
_B->x = 319 - _A.y;
_B->y = _A.x;
break;
}
}
// ----
bool GetRegistrationString(CString* RegString)
{
DWORD DataType = 0;
char* Value = NULL;
char Buffer[512], *BufferPtr;
unsigned short BuffItem;
DWORD DataLength = 0;
LONG ret;
bool Ret;
// ----
// Load Volume from registry if we can.
ret = LoadFromRegistry( HKEY_CURRENT_USER, // Existing key or default root.
&CString("ControlPanel\\Owner"), // Sub key, relative to _ExistingKey. (Must not start with a '/' char!)
&CString("Owner"), // Name of value to retrieve.
&DataType, // A pointer to a DWORD filled-in by the function.
&Value, // A pointer to buffer created within the function - you are responsible for the freeing of this resource.
&DataLength);
if ( (SUCCEEDED(ret))
&& (DataType == REG_BINARY)
&& (DataLength)
&& (Value))
{
WCHAR *ValuePtr;
ValuePtr = (unsigned short*)Value;
BufferPtr = Buffer;
while ( (*ValuePtr)
&& (DataLength > 0)
)
{
// Need to read it as a unicode variable
BuffItem = *(unsigned short*)ValuePtr;
if ( ( (BuffItem >= 32)
&& (BuffItem < 127))
|| ( (BuffItem >= 160)
&& (BuffItem <= 255))
)
{
*BufferPtr = BuffItem & 0xFF;
BufferPtr++;
}
ValuePtr++; // Move to next widechar
DataLength -= 2;
}
*BufferPtr = NULL; // Null terminate
// Now check that the name is okay - need to remove spaces, etc
Ret = true; // start by assuming it is okay
(*RegString) = Buffer;
if (!RegString->GetStringLength())
Ret = false; // Null string
}
else
{
Ret = false;
}
if (Value)
delete []Value;
if (!Ret)
(*RegString) = "Unable to obtain username";
return Ret;
}
// ----
void RPNStackPush( unsigned long *Stack,
unsigned short *StackPtr,
unsigned long Value)
{
Stack[*StackPtr] = Value;
*StackPtr = *StackPtr + 1;
}
// ----
unsigned long RPNStackPop(unsigned long *Stack,
unsigned short *StackPtr)
{
*StackPtr = *StackPtr - 1;
return Stack[*StackPtr];
}
#define NumRPNOps 18
char RPNList[NumRPNOps][4] =
{
"i",
"c",
"key",
"+",
"-",
"*",
"/",
"%",
"==",
">=",
"&&",
"||",
"!",
"<<",
">>",
"~",
"&",
"|"
};
// A logical match up for the array above.
#define RPN_I 0
#define RPN_C 1
#define RPN_KEY 2
#define RPN_ADD 3
#define RPN_SUB 4
#define RPN_MULU 5
#define RPN_DIVU 6
#define RPN_MOD 7
#define RPN_EQU 8
#define RPN_GTE 9
#define RPN_LOGAND 10
#define RPN_LOGOR 11
#define RPN_LOGNOT 12
#define RPN_LSL 13
#define RPN_LSR 14
#define RPN_INVERT 15 // Isn't this just RPN_LOGNOT by a different name?
#define RPN_BITAND 16
#define RPN_BITOR 17
// ----
// Returns the OP code index - returns -1 if it is an
// unknown value (would probably indicate a ture value).
signed long IsRPNOp(char *Str)
{
for (int i = NumRPNOps; i--;)
{
if (!strcmp(RPNList[i], Str))
{
return i;
}
}
// OutputDebugStringA(Str);
return -1; // Not found in list, assuming that it is a true number
}
// ----
// Validate that a user's name and the key they provide match!
bool ValidateRegistrationDetails(CString *UserName,
unsigned short UserKey,
char *RPN)
{
unsigned long RPNStack[32]; // Stack
unsigned short RPNStackIndex; // Stack index
WCHAR *UserNamePtr;
char RPNString[512]; // A working copy of our RPN string
char *RPNStringPtr; // Pointer to a RPN token
unsigned short i; // PUN index
unsigned short c; // PUN char - could be a char, but will use a u16 for ease of understanding
unsigned short Key; // Resultant key
unsigned short IntermediateA; // Used during a calculation.
unsigned short IntermediateB; // Used during a calculation.
RPNStackIndex = 0;
Key = 0;
if ( (!UserName)
|| (!UserName->GetString())
)
return false;
// Need to convert to Handango string - if it is longer
// then 10 chars we take the first and last 5 chars
WCHAR UserNameBuffer[512];
wcscpy(UserNameBuffer, UserName->GetString());
if (wcslen(UserNameBuffer) > 10)
{
wcscpy(UserNameBuffer + 5, UserNameBuffer + wcslen(UserNameBuffer) - 5);
}
UserNamePtr = UserNameBuffer;
// Loop through each of the letters in the PUN in-turn
for (i = 0; i < wcslen(UserNameBuffer); i++)
{
c = (*UserNamePtr) & 0xFF; // Convert the WCHAR back to a char.
// Okay, we have our values for Key, i and c.
// Now put them through the RPN process.
strcpy(RPNString, RPN); // Make a working copy of our RPN string
RPNStringPtr = strtok(RPNString, ", ");
while (RPNStringPtr)
{
switch (IsRPNOp(RPNStringPtr))
{
case RPN_I: // Push I onto the stack
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)i);
break;
case RPN_C: // Push C onto the stack
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)c);
break;
case RPN_KEY: // Push KEY onto the stack
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)Key);
break;
case RPN_ADD:
// Don't forget to pull the values in reverse order - intermediate B then A.
IntermediateB = RPNStackPop(RPNStack, &RPNStackIndex);
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(IntermediateA + IntermediateB));
break;
case RPN_SUB:
// Don't forget to pull the values in reverse order - intermediate B then A.
IntermediateB = RPNStackPop(RPNStack, &RPNStackIndex);
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(IntermediateA - IntermediateB));
break;
case RPN_MULU:
// Don't forget to pull the values in reverse order - intermediate B then A.
IntermediateB = RPNStackPop(RPNStack, &RPNStackIndex);
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(IntermediateA * IntermediateB));
break;
case RPN_DIVU:
// Don't forget to pull the values in reverse order - intermediate B then A.
IntermediateB = RPNStackPop(RPNStack, &RPNStackIndex);
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(IntermediateA / IntermediateB));
break;
case RPN_MOD:
// Don't forget to pull the values in reverse order - intermediate B then A.
IntermediateB = RPNStackPop(RPNStack, &RPNStackIndex);
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(IntermediateA % IntermediateB));
break;
case RPN_EQU:
// Don't forget to pull the values in reverse order - intermediate B then A.
IntermediateB = RPNStackPop(RPNStack, &RPNStackIndex);
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(IntermediateA == IntermediateB)?(1):(0));
break;
case RPN_GTE:
// Don't forget to pull the values in reverse order - intermediate B then A.
IntermediateB = RPNStackPop(RPNStack, &RPNStackIndex);
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(IntermediateA >= IntermediateB)?(1):(0));
break;
case RPN_LOGAND:
// Don't forget to pull the values in reverse order - intermediate B then A.
IntermediateB = RPNStackPop(RPNStack, &RPNStackIndex);
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(IntermediateA && IntermediateB));
break;
case RPN_LOGOR:
IntermediateB = RPNStackPop(RPNStack, &RPNStackIndex);
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(IntermediateA || IntermediateB));
break;
case RPN_LOGNOT:
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(!IntermediateA));
break;
case RPN_LSL:
IntermediateB = RPNStackPop(RPNStack, &RPNStackIndex);
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(IntermediateA << IntermediateB));
break;
case RPN_LSR:
IntermediateB = RPNStackPop(RPNStack, &RPNStackIndex);
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(IntermediateA >> IntermediateB));
break;
case RPN_INVERT:
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(!IntermediateA));
break;
case RPN_BITAND:
IntermediateB = RPNStackPop(RPNStack, &RPNStackIndex);
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(IntermediateA & IntermediateB));
break;
case RPN_BITOR:
IntermediateB = RPNStackPop(RPNStack, &RPNStackIndex);
IntermediateA = RPNStackPop(RPNStack, &RPNStackIndex);
RPNStackPush( RPNStack,
&RPNStackIndex,
(unsigned short)(IntermediateA | IntermediateB));
break;
default: // Should indicate a number.
RPNStackPush( RPNStack,
&RPNStackIndex,
atoi(RPNStringPtr));
break;
}
// Prepare for next loop.
RPNStringPtr = strtok(NULL, ", "); // Point to next RPN token
}
Key = RPNStackPop(RPNStack, &RPNStackIndex);
UserNamePtr++;
}
// All done - Key should now be the correct Key
return Key == UserKey;
}
#define ACTIVATIONMASK 0x370ADB39
// ----
// Encrypt and decrypt the activation code;
unsigned long EncryptCode(unsigned long aaaa)
{
return aaaa ^ ACTIVATIONMASK;
}
unsigned long DecryptCode(unsigned long aaaa)
{
return aaaa ^ ACTIVATIONMASK;
}