Simple test for asyncio.library.
[AROS-Contrib.git] / gfx / mysticview / src / Mystic_Keyfile.c
blobc13f0124aa30f9585990c2c6af13fa0933b1ec63
1 /*********************************************************************
2 ----------------------------------------------------------------------
4 MysticView
5 keyfiles
7 ----------------------------------------------------------------------
8 *********************************************************************/
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include <time.h>
15 #include <utility/tagitem.h>
16 #include <workbench/startup.h>
17 #include <workbench/workbench.h>
18 #include <libraries/cybergraphics.h>
19 #include <intuition/intuition.h>
20 #include <exec/memory.h>
22 #include <clib/macros.h>
24 #include <proto/asl.h>
25 #include <proto/exec.h>
26 #include <proto/intuition.h>
27 #include <proto/graphics.h>
28 #include <proto/utility.h>
29 #include <proto/guigfx.h>
30 #include <proto/cybergraphics.h>
31 #include <proto/wb.h>
32 #include <proto/dos.h>
33 #include <proto/intuition.h>
34 #include <proto/gadtools.h>
35 #include <proto/icon.h>
36 #include <proto/render.h>
38 #include "Mystic_FileHandling.h"
39 #include "Mystic_Subtask.h"
40 #include "Mystic_Keyfile.h"
42 #include <assert.h>
46 void crcgen(unsigned long *table)
48 unsigned long crc, poly;
49 int i, j;
51 poly = 0xEDB88320L;
52 for (i=0; i<256; i++) {
53 crc = i;
54 for (j=8; j>0; j--) {
55 if (crc&1) {
56 crc = (crc >> 1) ^ poly;
57 } else {
58 crc >>= 1;
61 table[i] = crc;
66 unsigned long getcrc(unsigned long *crctable, char *buffer, int buffersize)
68 register unsigned long crc;
69 int i;
71 crc = 0xFFFFFFFF;
73 for (i = 0; i < buffersize; ++i)
75 crc = ((crc>>8) & 0x00FFFFFF) ^ crctable[ (crc^buffer[i]) & 0xFF ];
78 return( crc^0xFFFFFFFF );
84 BOOL getbit(char *buffer, int bitnr)
86 unsigned char mask = 1 << (7 - (bitnr & 7));
88 return (BOOL) !!(buffer[bitnr>>3] & mask);
93 void setbit(char *buffer, int bitnr, BOOL bit)
95 unsigned char mask = 1 << (7 - (bitnr & 7));
97 buffer[bitnr>>3] &= ~mask;
98 if (bit)
100 buffer[bitnr>>3] |= mask;
105 void decode(char *buffer, int buflen, unsigned long key)
107 int i;
108 int bitlen;
109 BOOL bit1, bit2;
110 int pos1, pos2;
112 bitlen = buflen << 3;
114 Forbid();
116 srand(key^0x7f532c9a);
118 for (i = 0; i < buflen; ++i)
120 buffer[i] ^= rand();
123 Permit();
125 for (i = 0; i < bitlen / 2; ++i)
127 pos1 = i;
128 pos2 = bitlen-i-1;
129 bit1 = !getbit(buffer, pos1);
130 bit2 = getbit(buffer, pos2);
131 setbit(buffer, pos1, bit2);
132 setbit(buffer, pos2, bit1);
138 BOOL CheckKey(void)
140 #ifndef REGISTERED
142 struct keyfile *keyfile;
143 BOOL keyokay = FALSE;
145 if (keyfile = (struct keyfile *) FindSemaphore(mypersonalID))
147 char *buffer;
149 if (buffer = Malloc(keyfile->len))
151 ULONG crc;
153 memcpy(buffer, keyfile->key, keyfile->len);
154 decode(buffer, keyfile->len, keyfile->len);
155 crc = getcrc(keyfile->crctable, buffer + 4, keyfile->len - 4);
157 if (crc == *((ULONG *) buffer))
159 keyokay = TRUE;
162 srand(time(NULL));
164 Free(buffer);
168 return keyokay;
170 #else
172 return TRUE;
174 #endif
178 char *GetKeyLicensee(void)
180 #ifndef REGISTERED
182 struct keyfile *keyfile;
183 char *name = NULL;
185 if (keyfile = (struct keyfile *) FindSemaphore(mypersonalID))
187 char *buffer;
189 if (buffer = Malloc(keyfile->len))
191 ULONG crc;
193 memcpy(buffer, keyfile->key, keyfile->len);
194 decode(buffer, keyfile->len, keyfile->len);
195 crc = getcrc(keyfile->crctable, buffer + 4, keyfile->len - 4);
197 if (crc == *((ULONG *) buffer))
199 name = _StrDup(&buffer[13]);
202 srand(time(NULL));
204 Free(buffer);
208 return name;
210 #else
212 return NULL;
214 #endif
223 /*********************************************************************
224 ----------------------------------------------------------------------
226 RemoveKey(keyfile)
228 remove keyfile from memory.
230 ----------------------------------------------------------------------
231 *********************************************************************/
233 void RemoveKey(struct keyfile *keyfile)
235 #ifndef REGISTERED
237 if (keyfile)
239 RemSemaphore(&keyfile->semaphore);
240 Free(keyfile->key);
241 Free(keyfile);
244 #endif
248 /*********************************************************************
249 ----------------------------------------------------------------------
251 keyfile = InstallKey()
253 install keyfile in memory.
255 ----------------------------------------------------------------------
256 *********************************************************************/
258 struct keyfile *InstallKey(void)
260 #ifndef REGISTERED
262 struct keyfile *keyfile;
264 BOOL success = FALSE;
265 BPTR fp;
266 struct FileInfoBlock ALIGNED fib;
269 if (keyfile = Malloclear(sizeof(struct keyfile)))
271 if (fp = Open("L:MysticView.key", MODE_OLDFILE))
273 ExamineFH(fp, &fib);
275 keyfile->len = fib.fib_Size;
277 if (keyfile->key = Malloc(keyfile->len))
279 if (Read(fp, keyfile->key, keyfile->len) == keyfile->len)
281 keyfile->semaphore.ss_Link.ln_Name = mypersonalID;
282 keyfile->semaphore.ss_Link.ln_Pri = 1;
283 crcgen(keyfile->crctable);
284 AddSemaphore(&keyfile->semaphore);
285 success = TRUE;
288 Close(fp);
292 if (!success && keyfile)
294 Free(keyfile->key);
295 Free(keyfile);
296 keyfile = NULL;
299 return keyfile;
301 #else
303 return NULL;
305 #endif