this repo has no description
1/*
2 ACTVCODE.h
3
4 Copyright (C) 2009 Paul C. Pratt
5
6 You can redistribute this file and/or modify it under the terms
7 of version 2 of the GNU General Public License as published by
8 the Free Software Foundation. You should have received a copy
9 of the license along with this file; see the file COPYING.
10
11 This file is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 license for more details.
15*/
16
17/*
18 ACTiVation CODE
19*/
20
21LOCALFUNC uimr KeyFun0(uimr x, uimr y, uimr m)
22{
23 uimr r = x + y;
24
25 if ((r >= m) || (r < x)) {
26 r -= m;
27 }
28
29 return r;
30}
31
32LOCALFUNC uimr KeyFun1(uimr x, uimr y, uimr m)
33{
34 uimr r = 0;
35 uimr t = x;
36 uimr s = y;
37
38 while (s > 0) {
39 if (0 != (s & 1)) {
40 r = KeyFun0(r, t, m);
41 }
42 t = KeyFun0(t, t, m);
43 s >>= 1;
44 }
45
46 return r;
47}
48
49LOCALFUNC uimr KeyFun2(uimr x, uimr y, uimr m)
50{
51 uimr r = 1;
52 uimr t = x;
53 uimr s = y;
54
55 while (s > 0) {
56 if (0 != (s & 1)) {
57 r = KeyFun1(r, t, m);
58 }
59 t = KeyFun1(t, t, m);
60 s >>= 1;
61 }
62
63 return r;
64}
65
66LOCALFUNC blnr CheckActvCode(ui3p p, blnr *Trial)
67{
68 blnr IsOk = falseblnr;
69 uimr v0 = do_get_mem_long(p);
70 uimr v1 = do_get_mem_long(p + 4);
71
72 if (v0 > KeyCon2) {
73 /* v0 too big */
74 } else if (v1 > KeyCon4) {
75 /* v1 too big */
76 } else {
77 uimr t0 = KeyFun0(v0, KeyCon0, KeyCon2);
78 uimr t1 = KeyFun2(KeyCon1, t0, KeyCon2);
79 uimr t2 = KeyFun2(v1, KeyCon3, KeyCon4);
80 uimr t3 = KeyFun0(t2, KeyCon4 - t1, KeyCon4);
81 uimr t4 = KeyFun0(t3, KeyCon4 - KeyCon5, KeyCon4);
82 if ((0 == (t4 >> 8)) && (t4 >= KeyCon6)) {
83 *Trial = falseblnr;
84 IsOk = trueblnr;
85 } else if (0 == t4) {
86 *Trial = trueblnr;
87 IsOk = trueblnr;
88 }
89 }
90
91 return IsOk;
92}
93
94/* user interface */
95
96LOCALFUNC blnr Key2Digit(ui3r key, ui3r *r)
97{
98 ui3r v;
99
100 switch (key) {
101 case MKC_0:
102 case MKC_KP0:
103 v = 0;
104 break;
105 case MKC_1:
106 case MKC_KP1:
107 v = 1;
108 break;
109 case MKC_2:
110 case MKC_KP2:
111 v = 2;
112 break;
113 case MKC_3:
114 case MKC_KP3:
115 v = 3;
116 break;
117 case MKC_4:
118 case MKC_KP4:
119 v = 4;
120 break;
121 case MKC_5:
122 case MKC_KP5:
123 v = 5;
124 break;
125 case MKC_6:
126 case MKC_KP6:
127 v = 6;
128 break;
129 case MKC_7:
130 case MKC_KP7:
131 v = 7;
132 break;
133 case MKC_8:
134 case MKC_KP8:
135 v = 8;
136 break;
137 case MKC_9:
138 case MKC_KP9:
139 v = 9;
140 break;
141 default:
142 return falseblnr;
143 break;
144 }
145
146 *r = v;
147 return trueblnr;
148}
149
150#define ActvCodeMaxLen 20
151LOCALVAR ui4r ActvCodeLen = 0;
152LOCALVAR ui3b ActvCodeDigits[ActvCodeMaxLen];
153
154#define ActvCodeFileLen 8
155
156#if UseActvFile
157FORWARDFUNC tMacErr ActvCodeFileSave(ui3p p);
158FORWARDFUNC tMacErr ActvCodeFileLoad(ui3p p);
159#endif
160
161LOCALVAR ui3b CurActvCode[ActvCodeFileLen];
162
163LOCALPROC DoActvCodeModeKey(ui3r key)
164{
165 ui3r digit;
166 ui3r L;
167 int i;
168 blnr Trial;
169
170 if (MKC_BackSpace == key) {
171 if (ActvCodeLen > 0) {
172 --ActvCodeLen;
173 NeedWholeScreenDraw = trueblnr;
174 }
175 } else if (Key2Digit(key, &digit)) {
176 if (ActvCodeLen < (ActvCodeMaxLen - 1)) {
177 ActvCodeDigits[ActvCodeLen] = digit;
178 ++ActvCodeLen;
179 NeedWholeScreenDraw = trueblnr;
180 L = ActvCodeDigits[0] + (1 + 9);
181 if (ActvCodeLen == L) {
182 uimr v0 = 0;
183 uimr v1 = 0;
184
185 for (i = 1; i < (ActvCodeDigits[0] + 1); ++i) {
186 v0 = v0 * 10 + ActvCodeDigits[i];
187 }
188 for (; i < ActvCodeLen; ++i) {
189 v1 = v1 * 10 + ActvCodeDigits[i];
190 }
191
192 do_put_mem_long(&CurActvCode[0], v0);
193 do_put_mem_long(&CurActvCode[4], v1);
194
195 if (CheckActvCode(CurActvCode, &Trial)) {
196 SpecialModeClr(SpclModeActvCode);
197 NeedWholeScreenDraw = trueblnr;
198#if UseActvFile
199 if (Trial) {
200 MacMsg(
201 "Using temporary code.",
202 "Thank you for trying Mini vMac!",
203 falseblnr);
204 } else {
205 if (mnvm_noErr != ActvCodeFileSave(CurActvCode))
206 {
207 MacMsg("Oops",
208 "I could not save the activation code"
209 " to disk.",
210 falseblnr);
211 } else {
212 MacMsg("Activation succeeded.",
213 "Thank you!", falseblnr);
214 }
215 }
216#else
217 MacMsg(
218 "Thank you for trying Mini vMac!",
219 "sample variation : ^v",
220 falseblnr);
221#endif
222 }
223 } else if (ActvCodeLen > L) {
224 --ActvCodeLen;
225 }
226 }
227 }
228}
229
230LOCALPROC DrawCellsActvCodeModeBody(void)
231{
232#if UseActvFile
233 DrawCellsOneLineStr("Please enter your activation code:");
234 DrawCellsBlankLine();
235#else
236 DrawCellsOneLineStr(
237 "To try this variation of ^p, please type these numbers:");
238 DrawCellsBlankLine();
239 DrawCellsOneLineStr("281 953 822 340");
240 DrawCellsBlankLine();
241#endif
242
243 if (0 == ActvCodeLen) {
244 DrawCellsOneLineStr("?");
245 } else {
246 int i;
247 ui3r L = ActvCodeDigits[0] + (1 + 9);
248
249 DrawCellsBeginLine();
250 for (i = 0; i < L; ++i) {
251 if (0 == ((L - i) % 3)) {
252 if (0 != i) {
253 DrawCellAdvance(kCellSpace);
254 }
255 }
256 if (i < ActvCodeLen) {
257 DrawCellAdvance(kCellDigit0 + ActvCodeDigits[i]);
258 } else if (i == ActvCodeLen) {
259 DrawCellAdvance(kCellQuestion);
260 } else {
261 DrawCellAdvance(kCellHyphen);
262 }
263 }
264 DrawCellsEndLine();
265 if (L == ActvCodeLen) {
266 DrawCellsBlankLine();
267 DrawCellsOneLineStr(
268 "Sorry, this is not a valid activation code.");
269 }
270 }
271
272#if UseActvFile
273 DrawCellsBlankLine();
274 DrawCellsOneLineStr(
275 "If you haven;}t obtained an activation code yet,"
276 " here is a temporary one:");
277 DrawCellsBlankLine();
278 DrawCellsOneLineStr("281 953 822 340");
279#else
280 DrawCellsBlankLine();
281 DrawCellsOneLineStr(kStrForMoreInfo);
282 DrawCellsOneLineStr("http://www.gryphel.com/c/var/");
283#endif
284}
285
286LOCALPROC DrawActvCodeMode(void)
287{
288 DrawSpclMode0(
289#if UseActvFile
290 "Activation Code",
291#else
292 "sample variation : ^v",
293#endif
294 DrawCellsActvCodeModeBody);
295}
296
297#if UseActvFile
298LOCALPROC ClStrAppendHexLong(int *L0, ui3b *r, ui5r v)
299{
300 ClStrAppendHexWord(L0, r, (v >> 16) & 0xFFFF);
301 ClStrAppendHexWord(L0, r, v & 0xFFFF);
302}
303#endif
304
305LOCALPROC CopyRegistrationStr(void)
306{
307 ui3b ps[ClStrMaxLength];
308 int i;
309 int L;
310 tPbuf j;
311#if UseActvFile
312 int L0;
313 ui5r sum;
314
315 ClStrFromSubstCStr(&L0, ps, "^v ");
316
317 for (i = 0; i < L0; ++i) {
318 ps[i] = Cell2MacAsciiMap[ps[i]];
319 }
320 L = L0;
321
322 sum = 0;
323 for (i = 0; i < L; ++i) {
324 sum += ps[i];
325 sum = (sum << 5) | ((sum >> (32 - 5)) & 0x1F);
326 sum += (sum << 8);
327 }
328
329 sum &= 0x1FFFFFFF;
330
331 sum = KeyFun0(sum, do_get_mem_long(&CurActvCode[0]), KeyCon4);
332
333 ClStrAppendHexLong(&L, ps, sum);
334
335 sum = KeyFun0(sum, do_get_mem_long(&CurActvCode[4]), KeyCon4);
336 sum = KeyFun2(sum, KeyCon3, KeyCon4);
337
338 ClStrAppendHexLong(&L, ps, sum);
339
340 for (i = L0; i < L; ++i) {
341 ps[i] = Cell2MacAsciiMap[ps[i]];
342 }
343#else
344 ClStrFromSubstCStr(&L, ps, "^v");
345
346 for (i = 0; i < L; ++i) {
347 ps[i] = Cell2MacAsciiMap[ps[i]];
348 }
349#endif
350
351 if (mnvm_noErr == PbufNew(L, &j)) {
352 PbufTransfer(ps, j, 0, L, trueblnr);
353 HTCEexport(j);
354 }
355}
356
357LOCALFUNC blnr ActvCodeInit(void)
358{
359#if UseActvFile
360 blnr Trial;
361
362 if ((mnvm_noErr != ActvCodeFileLoad(CurActvCode))
363 || (! CheckActvCode(CurActvCode, &Trial))
364 || Trial
365 )
366#endif
367 {
368 SpecialModeSet(SpclModeActvCode);
369 NeedWholeScreenDraw = trueblnr;
370 }
371
372 return trueblnr;
373}