PageRenderTime 82ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src/pycrt/frozen_main.C

https://github.com/alexeysudachen/python-tool
C | 228 lines | 198 code | 28 blank | 2 comment | 27 complexity | 8ca5f33822dd3f385aebd0b30b16fd69 MD5 | raw file
  1. #include <stdlib.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4. #if defined _WIN32 && !defined _PY2CC_STATIC
  5. #define DLL_IMPORT __declspec(dllimport)
  6. #else
  7. #define DLL_IMPORT
  8. #endif
  9. /*extern struct _frozen _py2cc_Modules[];*/
  10. struct _frozen { char *name; unsigned char *code; int size; };
  11. DLL_IMPORT extern struct _frozen *PyImport_FrozenModules;
  12. DLL_IMPORT extern int Py_SetProgramName(char *);
  13. DLL_IMPORT extern int PyImport_ImportFrozenModule(char *);
  14. DLL_IMPORT extern int PySys_SetArgv(int argc,char **argv);
  15. DLL_IMPORT extern int Py_FatalError(char *);
  16. DLL_IMPORT extern int PyErr_Print();
  17. DLL_IMPORT extern int Py_Initialize();
  18. DLL_IMPORT extern int Py_Finalize();
  19. DLL_IMPORT extern int Py_FrozenFlag;
  20. DLL_IMPORT extern int Py_IgnoreEnvironmentFlag;
  21. DLL_IMPORT extern int Py_OptimizeFlag;
  22. static int j_frozen_main(struct _frozen *);
  23. static void *frozen_table_mem = 0;
  24. static int frozen_table_count = 0;
  25. static j_init_frozen_table()
  26. {
  27. if ( !frozen_table_count )
  28. {
  29. struct _frozen *i = (struct _frozen*)_py2cc_Modules;
  30. PyImport_FrozenModules = i;
  31. while ( i->name ) ++frozen_table_count, ++i;
  32. }
  33. }
  34. static void j_extend_frozen_table(struct _frozen *ftable, int before)
  35. {
  36. int ftable_count = 0;
  37. struct _frozen *i = ftable;
  38. struct _frozen *mem = 0;
  39. while ( i->name ) ++ftable_count, ++i;
  40. mem = (struct _frozen *)malloc( sizeof(struct _frozen)*(ftable_count+frozen_table_count+1) );
  41. memcpy( mem + (before?0:frozen_table_count), ftable, ftable_count*sizeof(struct _frozen));
  42. memcpy( mem + (!before?0:ftable_count), PyImport_FrozenModules, frozen_table_count*sizeof(struct _frozen));
  43. if ( frozen_table_mem ) free(frozen_table_mem);
  44. frozen_table_mem = mem;
  45. PyImport_FrozenModules = mem;
  46. frozen_table_count += ftable_count;
  47. memset( PyImport_FrozenModules + frozen_table_count, 0, sizeof(struct _frozen) );
  48. }
  49. typedef struct PyObject PyObject;
  50. typedef struct PyMethodDef {
  51. char *ml_name; /* The name of the built-in function/method */
  52. void *ml_meth; /* The C function that implements it */
  53. int ml_flags; /* Combination of METH_xxx flags, which mostly
  54. describe the args expected by the C func */
  55. char *ml_doc; /* The __doc__ attribute, or NULL */
  56. } PyMethodDef;
  57. enum {METH_VARARGS =0x0001};
  58. DLL_IMPORT extern PyObject _Py_NoneStruct;
  59. #define Py_None (&_Py_NoneStruct)
  60. DLL_IMPORT extern void Py_IncRef(PyObject *o);
  61. DLL_IMPORT extern void Py_DecRef(PyObject *o);
  62. DLL_IMPORT extern PyObject *PyImport_ExecCodeModuleEx(char *name, PyObject *co, char *pathname);
  63. DLL_IMPORT extern PyObject *PyImport_GetModuleDict();
  64. DLL_IMPORT extern PyObject *PyDict_GetItemString(PyObject *,char *);
  65. DLL_IMPORT extern PyObject *PyCFunction_NewEx(PyMethodDef *,void *,PyObject *);
  66. DLL_IMPORT extern void PyModule_AddObject(PyObject *,char *,PyObject *);
  67. DLL_IMPORT extern PyObject *PyMarshal_ReadObjectFromString(char *, int);
  68. DLL_IMPORT extern PyObject *PyImport_AddModule(char *);
  69. DLL_IMPORT extern PyObject *PyImport_ImportModule(char *);
  70. DLL_IMPORT extern void PyErr_Clear();
  71. DLL_IMPORT extern PyObject *PyExc_ImportError;
  72. static void j_builtin_function(PyMethodDef *ml)
  73. {
  74. PyObject *builtin, *pfunc;
  75. builtin = PyDict_GetItemString(PyImport_GetModuleDict(),"__builtin__");
  76. pfunc = PyCFunction_NewEx(ml,0,0);
  77. PyModule_AddObject(builtin,ml->ml_name,pfunc);
  78. PyErr_Clear();
  79. }
  80. static PyObject *j_import_frozen_module(PyObject *,PyObject *args);
  81. static PyMethodDef j_import_frozen_module_ml = { "ed62969f98484bca902e8af82f039880",&j_import_frozen_module,METH_VARARGS,0};
  82. int j_frozen_main_1(int argc, char **argv)
  83. {
  84. char *p;
  85. int n;
  86. struct _frozen *Z_frozen = 0;
  87. j_init_frozen_table();
  88. #if defined _PY2CC_USE_RUNTIME
  89. Z_frozen = j_access_runtime(_PY2CC_USE_RUNTIME);
  90. if ( Z_frozen )
  91. {
  92. j_extend_frozen_table(Z_frozen,0);
  93. }
  94. #endif
  95. Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
  96. Py_IgnoreEnvironmentFlag = 1;
  97. Py_OptimizeFlag = 1;
  98. Py_SetProgramName(argv[0]);
  99. Py_Initialize();
  100. PySys_SetArgv(argc,argv);
  101. j_builtin_function(&j_import_frozen_module_ml);
  102. n = PyImport_ImportFrozenModule("__main__");
  103. if (n == 0)
  104. Py_FatalError("__main__ not frozen");
  105. if (n < 0)
  106. PyErr_Print();
  107. Py_Finalize();
  108. return n<0?-1:0;
  109. }
  110. static char **j_buildargv(char *input,int *count);
  111. #if defined _PY2CC_ON_POSIX || defined _PY2CC_CONSOLE_SUBSYSTEM
  112. int main(int argc,char **argv)
  113. #elif defined _PY2CC_ON_WINDOWS && defined _PY2CC_WINDOWS_SUBSYSTEM
  114. char *__stdcall GetCommandLineA();
  115. int __stdcall WinMain(int _0,int _1,int _2,int _3)
  116. #else
  117. #error "unknown platform"
  118. #endif
  119. {
  120. int sts = 0;
  121. #if defined _PY2CC_ON_WINDOWS && defined _PY2CC_WINDOWS_SUBSYSTEM
  122. int argc = 0;
  123. char **argv = j_buildargv( GetCommandLineA(), &argc );
  124. #endif
  125. return j_frozen_main_1(argc,argv);
  126. }
  127. static char *j_jjlz_decompressor(unsigned char *buffer, int *len);
  128. static PyObject *j_import_frozen_module(PyObject *_0,PyObject *args)
  129. {
  130. char *mod_name = 0, *mod_code = 0, *mod_file = "<frozen>";
  131. int mod_code_len = 0;
  132. if ( PyArg_ParseTuple(args,"ss#|s",&mod_name,&mod_code,&mod_code_len,&mod_file) )
  133. {
  134. PyObject *code = 0;
  135. PyObject *o = 0;
  136. char *temp_buff = 0;
  137. if ( mod_code_len >= 8 && 0 == memcmp(mod_code,"JJLZ",4) )
  138. {
  139. temp_buff = j_jjlz_decompressor(mod_code,&mod_code_len);
  140. if ( !temp_buff )
  141. {
  142. PyErr_SetString(PyExc_ImportError,"failed to init module");
  143. return 0;
  144. }
  145. mod_code = temp_buff;
  146. }
  147. code = PyMarshal_ReadObjectFromString(mod_code,mod_code_len);
  148. if ( temp_buff ) free(temp_buff);
  149. if ( code )
  150. {
  151. o = PyImport_ExecCodeModuleEx(mod_name, code, mod_file);
  152. Py_DecRef(code);
  153. }
  154. if ( o )
  155. {
  156. Py_IncRef(Py_None);
  157. return Py_None;
  158. }
  159. }
  160. return 0;
  161. }
  162. static char *j_jjlz_decompressor(unsigned char *in_b, int *inout_len)
  163. {
  164. enum { JJLZ_MAX_LEN = 15 };
  165. int out_i=0, in_i = 0, out_b_len, in_b_len = *inout_len-8;
  166. char *out_b;
  167. in_b += 4; /*skip 'JJLZ'*/
  168. out_b_len = (unsigned int)in_b[0]|((unsigned int)in_b[1]<<8)|
  169. ((unsigned int)in_b[2]<<16)|((unsigned int)in_b[3]<<24);
  170. out_b = malloc(out_b_len);
  171. in_b += 4; /*skip out_b_len*/
  172. while ( in_i < in_b_len && out_i < out_b_len )
  173. {
  174. if ( in_b[in_i] == 0x80 )
  175. {/* one char */
  176. out_b[out_i++] = in_b[++in_i];
  177. ++in_i;
  178. }
  179. else if ( !in_b[in_i] )
  180. {/* several chars */
  181. int l = (int)in_b[++in_i]+1;
  182. ++in_i;
  183. while ( l-- )
  184. {
  185. out_b[out_i++] = in_b[in_i++];
  186. }
  187. }
  188. else
  189. {/* code */
  190. unsigned short code = (short)in_b[in_i]|((short)in_b[in_i+1] << 8);
  191. int l = code & 0x0f;
  192. int off = code >> 4;
  193. memcpy(out_b+out_i,out_b+out_i-off-JJLZ_MAX_LEN,l);
  194. out_i += l;
  195. in_i += 2;
  196. }
  197. }
  198. *inout_len = out_i;
  199. return out_b;
  200. }