patch [#17683] Fix OBJ import line endings
[blender.git] / source / blender / src / pub / license_key.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program 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  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include "license_key.h"
31 #include "keyed_functions.h"
32 #include "BKE_utildefines.h"
33 #include "BIF_screen.h"  // splash
34 #include "BIF_toolbox.h"
35 #include "blenkey.h"
36 #include <stdio.h>
37 #include <string.h>
38
39 #include "BLI_blenlib.h"
40
41 #include "BLO_readfile.h"
42 #include "BLO_keyStore.h"
43
44 #ifdef HAVE_CONFIG_H
45 #include <config.h>
46 #endif
47
48 int LICENSE_KEY_VALID = TRUE;
49 int I_AM_PUBLISHER = TRUE;
50
51 static UserStruct User;
52
53 // Python stuff
54
55 #include "Python.h"
56 #include "marshal.h" 
57 #include "compile.h" /* to give us PyCodeObject */
58 #include "eval.h"               /* prototype for PyEval_EvalCode */
59
60 #include "BPY_extern.h"
61
62 #include "IMB_imbuf.h"
63
64 Fptr g_functab[PYKEY_TABLEN];
65 Fptr g_ptrtab[PYKEY_TABLEN];
66
67 static int g_seed[3] = PYKEY_SEED;
68 static PyObject *g_module_self;
69 static PyObject *g_main;
70
71
72 // end Python stuff
73
74 // **************** PYTHON STUFF **************************
75 /* ----------------------------------------------------- */
76 /* this is the dummy functions to demonstrate */
77
78 int sticky_shoes(void *vp)
79 {
80         return 0;
81 }
82
83 /*
84 int key_func1(void *vp) {
85         printf("function 1 called\n");
86 }
87
88 */
89 int key_return_true(void *vp) {
90         return 1;
91 }
92
93
94 /* ----------------------------------------------------- */
95
96 /* Declarations for objects of type Fplist */
97
98
99 static char prot_getseed__doc__[] = "";
100
101 static PyObject *
102 prot_getseed(self, args)
103         PyObject *self; /* Not used */
104         PyObject *args;
105 {
106         PyObject *p;
107         p = PyTuple_New(3);
108         PyTuple_SetItem(p, 0, PyInt_FromLong(g_seed[0]));
109         PyTuple_SetItem(p, 1, PyInt_FromLong(g_seed[1]));
110         PyTuple_SetItem(p, 2, PyInt_FromLong(g_seed[2]));
111         return p;
112 }
113
114 static char prot_getlen__doc__[] = "";
115 static PyObject *
116 prot_getlen(self, args)
117         PyObject *self; /* Not used */
118         PyObject *args;
119 {
120         return Py_BuildValue("i", PYKEY_TABLEN);
121 }
122
123 static char prot_getptr__doc__[] =
124 ""
125 ;
126
127 static PyObject *
128 prot_getptr(self, args)
129         PyObject *self; /* Not used */
130         PyObject *args;
131 {
132         PyObject *p;
133         Fptr f;
134         int index;
135         /* we don't catch errors here, we're in the key code */
136         if (!g_functab)
137                 return NULL;
138         if (!PyArg_ParseTuple(args, "i", &index))
139                 return NULL;
140         if (index >= PYKEY_TABLEN)
141                 return NULL;
142
143         f = g_functab[index];
144         p = PyCObject_FromVoidPtr(f , NULL);
145         return p;
146 }
147
148 static char prot_setptr__doc__[] =
149 ""
150 ;
151 static PyObject *
152 prot_setptr(self, args)
153         PyObject *self; /* Not used */
154         PyObject *args;
155 {
156         PyObject *p;
157
158         int index;
159
160         if (!g_ptrtab) 
161                 return NULL;
162         if (!PyArg_ParseTuple(args, "iO", &index, &p))
163                 return NULL;
164         if (index >= PYKEY_TABLEN)
165                 return NULL;
166         if (!PyCObject_Check(p)) {
167                 return NULL;
168         }
169
170         g_ptrtab[index] = PyCObject_AsVoidPtr(p);
171         return Py_BuildValue("i", 1);
172 }
173
174 static PyObject *callkeycode(
175         unsigned char *keycode,
176         int keycodelen)
177 {
178         PyCodeObject *code;
179         PyObject *maindict = PyModule_GetDict(g_main);
180
181         code = (PyCodeObject *) PyMarshal_ReadObjectFromString(keycode, keycodelen);
182         if (!PyEval_EvalCode(code, maindict, maindict))
183                 return NULL;
184         return Py_BuildValue("i", 1);
185 }
186
187
188
189 /* List of methods defined in the module */
190
191 static struct PyMethodDef prot_methods[] = {
192         {"getlen",      (PyCFunction)prot_getlen,       METH_VARARGS,   prot_getlen__doc__},
193         {"getseed",     (PyCFunction)prot_getseed,      METH_VARARGS,   prot_getseed__doc__},
194         {"getptr",      (PyCFunction)prot_getptr,       METH_VARARGS,   prot_getptr__doc__},
195         {"setptr",      (PyCFunction)prot_setptr,       METH_VARARGS,   prot_setptr__doc__},
196  
197         {NULL,   (PyCFunction)NULL, 0, NULL}            /* sentinel */
198 };
199
200
201 /* Initialization function for the module (*must* be called initprot) */
202
203 static char prot_module_documentation[] = "No Documentation";
204
205 static void init_ftable(void)  // initializes functiontable
206 {
207         int i;
208
209         g_functab[0] = &key_func1;
210 /*  add more key_funcs here */
211
212         for (i = 1; i < PYKEY_TABLEN; i++)
213         {
214                 g_functab[i] = &sticky_shoes;
215         }
216 }
217
218
219 static void init_ptable(void)  // initializes functiontable
220 {
221         int i;
222
223         for (i = 0; i < PYKEY_TABLEN; i++)
224         {
225                 g_ptrtab[i] = &sticky_shoes;
226         }
227 }
228
229
230 static void insertname(PyObject *m,PyObject *p, char *name)
231 {
232         PyObject *d = PyModule_GetDict(m);
233
234         EXPP_dict_set_item_str(d, name, p);
235 }
236
237 /* initialisation */
238 static void initprot()
239 {
240         PyObject *m, *d;
241         PyObject *capi1;
242         init_ftable(); 
243
244         g_main = PyImport_AddModule("__main__");
245
246         m = Py_InitModule4("prot", prot_methods,
247                 prot_module_documentation,
248                 (PyObject*)NULL,PYTHON_API_VERSION);
249         g_module_self = m;      
250         d = PyModule_GetDict(m);
251         EXPP_dict_set_item_str(d, "error", PyString_FromString("prot.error");
252
253         /* add global object */
254
255         capi1 = PyCObject_FromVoidPtr((void *)g_functab , NULL);
256         if (capi1) {
257                 insertname(m, capi1, "APIfunctab");
258         }       
259         
260         /* Check for errors */
261         if (PyErr_Occurred())
262                 Py_FatalError("can't initialize module prot");
263
264         init_ptable(); 
265 }
266
267 // ******************************* KEY STUFF *********************
268
269 static void create_key_name(char * keyname)
270 {
271         sprintf(keyname, "%s/.BPkey", BLI_gethome());
272 }
273
274 void checkhome()
275 {
276         int keyresult;
277         char *HexPriv, *HexPub, *HexPython;
278         byte *Byte;
279         char keyname[FILE_MAXDIR + FILE_MAXFILE];
280         int wasInitialized;
281         unsigned char *keycode = NULL;
282         int keycodelen = 0;
283
284         create_key_name(keyname);
285         keyresult = ReadKeyFile(keyname, &User, &HexPriv, &HexPub,
286                                 &Byte, &HexPython);
287         if (keyresult != 0) {
288             // printf("\nReadKeyFile error %d\n", keyresult);
289         } else {
290             // printf("\nReadKeyFile OK\n");
291                 LICENSE_KEY_VALID = TRUE;
292
293                 wasInitialized = Py_IsInitialized();
294                 
295                 // make it failsafe if python interpreter was already initialized
296                 if (wasInitialized) 
297                         Py_Initialize();
298                         
299                 initprot();                   // initialize module and function tables
300                 // get python byte code
301                 keycode = DeHexify(HexPython);
302                 keycodelen = strlen(HexPython) / 2;
303
304                 callkeycode(keycode, keycodelen);
305
306                 Py_Finalize(); 
307
308                 if (wasInitialized)     // if we were initialized,
309                         BPY_start_python(); // restart creator python
310
311                 //some debugging stuff
312                 // print_ptable();
313
314                 // Store key stuff for use by stream
315                 keyStoreConstructor(
316                         &User,
317                         HexPriv,
318                         HexPub,
319                         Byte,
320                         HexPython);
321
322                 // other initialization code
323
324         }
325 }
326
327 void SHOW_LICENSE_KEY(void)
328 {
329         extern int datatoc_tonize;
330         extern char datatoc_ton[];
331         char string[1024];
332         int maxtype, type;
333         char *typestrings[] = {
334                 "",
335                 "Individual",
336                 "Company",
337                 "Unlimited",
338                 "Educational"};
339
340         maxtype = (sizeof(typestrings) / sizeof(char *)) - 1;
341         type = User.keytype;
342         if (type > maxtype) {
343                 type = 0;
344         }
345
346         if (LICENSE_KEY_VALID) {
347                 sprintf(string, "%s License registered to: %s (%s)", typestrings[type], User.name, User.email);
348                 splash((void *)datatoc_ton, datatoc_tonize, string);
349         }
350 }
351
352 void loadKeyboard(char * name)
353 {
354         char keyname[FILE_MAXDIR + FILE_MAXFILE];
355         FILE *in, *out;
356         char string[1024], *match = 0;
357         int i, c;
358         int found = 0;
359
360         // make sure we don't overwrite a valid key...
361         
362         if (!LICENSE_KEY_VALID) {
363                 in = fopen(name, "rb");
364                 if (in) {
365                         // scan for blender key magic, read strings
366                         // with anything but a newline
367                         while (fscanf(in, "%1000[^\n\r]", string) != EOF) {
368                                 match = strstr(string, BLENKEYMAGIC);
369                                 if (match) {
370                                         break;
371                                 }
372                                 fscanf(in, "\n");
373                         }
374                         
375                         if (match) {
376                                 // found blender key magic, open output file
377                                 // to copy key information
378                                 
379                                 create_key_name(keyname);
380                                 out = fopen(keyname, "wb");
381                                 if (out) {
382                                         // printout first line
383                                         fprintf(out, "%s", match);
384                                         for (i = 0; i < 350; i++) {
385                                                 // handle control characters (\n\r)
386                                                 while (1) {
387                                                         c = getc(in);
388                                                         if (c == '\n') {
389                                                                 // output a \n for each \n in the input
390                                                                 fprintf(out, "\n");
391                                                         } else if (c == EOF) {
392                                                                 break;
393                                                         } else if (c < ' ') {
394                                                                 // skip control characters
395                                                         } else {
396                                                                 ungetc(c, in);
397                                                                 break;
398                                                         }
399                                                 }
400                                                 
401                                                 if (fscanf(in, "%1000[^\n\r]", string) != EOF) {
402                                                         if (strcmp(string, BLENKEYSEPERATOR) == 0) {
403                                                                 found++;
404                                                         }
405                                                         fprintf(out, "%s", string);
406                                                 } else {
407                                                         break;
408                                                 }
409
410                                                 if (found >= 2) {
411                                                         break;
412                                                 }
413                                         }
414                                         
415                                         fclose(out);
416                                         
417                                         checkhome();
418                                         if (LICENSE_KEY_VALID) {
419                                                 SHOW_LICENSE_KEY();
420                                         } else {
421                                                 error("Not a valid license key ! Removing installed key.");
422                                                 BLI_delete(keyname, 0, 0);
423                                         }
424
425                                 } else {
426                                         error("Can't install key");
427                                 }
428                         } else {
429                                 error("File doesn't contain a valid key: %s", name);
430                         }
431
432                         fclose(in);
433
434                         if (LICENSE_KEY_VALID) {
435                                 if (okee("Remove input file: '%s'?", name)) {
436                                         BLI_delete(name, 0, 0);
437                                 }
438                         }
439
440                 } else {
441                         error("File doesn't exist: %s", name);
442                 }
443         }
444 }