py_magic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. Python wrappers for magic functions.
  3. Copyright (C) Brett Funderburg, Deepfile Corp. Austin, TX, US 2003
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions
  6. are met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice immediately at the beginning of the file, without modification,
  9. this list of conditions, and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. The name of the author may not be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  19. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. SUCH DAMAGE.
  26. */
  27. #include <Python.h>
  28. #include <magic.h>
  29. #include "py_magic.h"
  30. /* Exceptions raised by this module */
  31. PyObject* magic_error_obj;
  32. /* Create a new magic_cookie_hnd object */
  33. PyObject* new_magic_cookie_handle(magic_t cookie)
  34. {
  35. magic_cookie_hnd* mch;
  36. mch = PyObject_New(magic_cookie_hnd, &magic_cookie_type);
  37. mch->cookie = cookie;
  38. return (PyObject*)mch;
  39. }
  40. static char _magic_open__doc__[] =
  41. "Returns a magic cookie on success and None on failure.\n";
  42. static PyObject* py_magic_open(PyObject* self, PyObject* args)
  43. {
  44. int flags = 0;
  45. magic_t cookie;
  46. if(!PyArg_ParseTuple(args, "i", &flags))
  47. return NULL;
  48. if(!(cookie = magic_open(flags))) {
  49. PyErr_SetString(magic_error_obj, "failure initializing magic cookie");
  50. return NULL;
  51. }
  52. return new_magic_cookie_handle(cookie);
  53. }
  54. static char _magic_close__doc__[] =
  55. "Closes the magic database and deallocates any resources used.\n";
  56. static PyObject* py_magic_close(PyObject* self, PyObject* args)
  57. {
  58. magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
  59. magic_close(hnd->cookie);
  60. Py_INCREF(Py_None);
  61. return Py_None;
  62. }
  63. static char _magic_error__doc__[] =
  64. "Returns a textual explanation of the last error or None \
  65. if there was no error.\n";
  66. static PyObject* py_magic_error(PyObject* self, PyObject* args)
  67. {
  68. magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
  69. const char* message = NULL;
  70. PyObject* result = Py_None;
  71. message = magic_error(hnd->cookie);
  72. if(message != NULL)
  73. result = PyString_FromString(message);
  74. else
  75. Py_INCREF(Py_None);
  76. return result;
  77. }
  78. static char _magic_errno__doc__[] =
  79. "Returns a numeric error code. If return value is 0, an internal \
  80. magic error occurred. If return value is non-zero, the value is \
  81. an OS error code. Use the errno module or os.strerror() can be used \
  82. to provide detailed error information.\n";
  83. static PyObject* py_magic_errno(PyObject* self, PyObject* args)
  84. {
  85. magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
  86. return PyInt_FromLong(magic_errno(hnd->cookie));
  87. }
  88. static char _magic_file__doc__[] =
  89. "Returns a textual description of the contents of the argument passed \
  90. as a filename or None if an error occurred and the MAGIC_ERROR flag \
  91. is set. A call to errno() will return the numeric error code.\n";
  92. static PyObject* py_magic_file(PyObject* self, PyObject* args)
  93. {
  94. magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
  95. char* filename = NULL;
  96. const char* message = NULL;
  97. PyObject* result = Py_None;
  98. if(!(PyArg_ParseTuple(args, "s", &filename)))
  99. return NULL;
  100. message = magic_file(hnd->cookie, filename);
  101. if(message != NULL)
  102. result = PyString_FromString(message);
  103. else
  104. Py_INCREF(Py_None);
  105. return result;
  106. }
  107. static char _magic_buffer__doc__[] =
  108. "Returns a textual description of the contents of the argument passed \
  109. as a buffer or None if an error occurred and the MAGIC_ERROR flag \
  110. is set. A call to errno() will return the numeric error code.\n";
  111. static PyObject* py_magic_buffer(PyObject* self, PyObject* args)
  112. {
  113. magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
  114. void* buffer = NULL;
  115. int buffer_length = 0;
  116. const char* message = NULL;
  117. PyObject* result = Py_None;
  118. if(!(PyArg_ParseTuple(args, "s#", (char**)&buffer, &buffer_length)))
  119. return NULL;
  120. message = magic_buffer(hnd->cookie, buffer, buffer_length);
  121. if(message != NULL)
  122. result = PyString_FromString(message);
  123. else
  124. Py_INCREF(Py_None);
  125. return result;
  126. }
  127. static char _magic_setflags__doc__[] =
  128. "Set flags on the cookie object.\n \
  129. Returns -1 on systems that don't support utime(2) or utimes(2) \
  130. when MAGIC_PRESERVE_ATIME is set.\n";
  131. static PyObject* py_magic_setflags(PyObject* self, PyObject* args)
  132. {
  133. magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
  134. int flags;
  135. int result;
  136. if(!(PyArg_ParseTuple(args, "i", &flags)))
  137. return NULL;
  138. result = magic_setflags(hnd->cookie, flags);
  139. return PyInt_FromLong(result);
  140. }
  141. static char _magic_check__doc__[] =
  142. "Check the validity of entries in the colon separated list of \
  143. database files passed as argument or the default database file \
  144. if no argument.\n Returns 0 on success and -1 on failure.\n";
  145. static PyObject* py_magic_check(PyObject* self, PyObject* args)
  146. {
  147. magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
  148. char* filename = NULL;
  149. int result;
  150. if(!(PyArg_ParseTuple(args, "|s", &filename)))
  151. return NULL;
  152. result = magic_check(hnd->cookie, filename);
  153. return PyInt_FromLong(result);
  154. }
  155. static char _magic_compile__doc__[] =
  156. "Compile entries in the colon separated list of database files \
  157. passed as argument or the default database file if no argument.\n \
  158. Returns 0 on success and -1 on failure.\n \
  159. The compiled files created are named from the basename(1) of each file \
  160. argument with \".mgc\" appended to it.\n";
  161. static PyObject* py_magic_compile(PyObject* self, PyObject* args)
  162. {
  163. magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
  164. char* filename = NULL;
  165. int result;
  166. if(!(PyArg_ParseTuple(args, "|s", &filename)))
  167. return NULL;
  168. result = magic_compile(hnd->cookie, filename);
  169. return PyInt_FromLong(result);
  170. }
  171. static char _magic_load__doc__[] =
  172. "Must be called to load entries in the colon separated list of database files \
  173. passed as argument or the default database file if no argument before \
  174. any magic queries can be performed.\n \
  175. Returns 0 on success and -1 on failure.\n";
  176. static PyObject* py_magic_load(PyObject* self, PyObject* args)
  177. {
  178. magic_cookie_hnd* hnd = (magic_cookie_hnd*)self;
  179. char* filename = NULL;
  180. int result;
  181. if(!(PyArg_ParseTuple(args, "|s", &filename)))
  182. return NULL;
  183. result = magic_load(hnd->cookie, filename);
  184. return PyInt_FromLong(result);
  185. }
  186. /* object methods */
  187. static PyMethodDef magic_cookie_hnd_methods[] = {
  188. { "close", (PyCFunction)py_magic_close,
  189. METH_NOARGS, _magic_close__doc__ },
  190. { "error", (PyCFunction)py_magic_error,
  191. METH_NOARGS, _magic_error__doc__ },
  192. { "file", (PyCFunction)py_magic_file,
  193. METH_VARARGS, _magic_file__doc__ },
  194. { "buffer", (PyCFunction)py_magic_buffer,
  195. METH_VARARGS, _magic_buffer__doc__ },
  196. { "setflags", (PyCFunction)py_magic_setflags,
  197. METH_VARARGS, _magic_setflags__doc__ },
  198. { "check", (PyCFunction)py_magic_check,
  199. METH_VARARGS, _magic_check__doc__ },
  200. { "compile", (PyCFunction)py_magic_compile,
  201. METH_VARARGS, _magic_compile__doc__ },
  202. { "load", (PyCFunction)py_magic_load,
  203. METH_VARARGS, _magic_load__doc__ },
  204. { "errno", (PyCFunction)py_magic_errno,
  205. METH_NOARGS, _magic_errno__doc__ },
  206. { NULL, NULL }
  207. };
  208. /* module level methods */
  209. static PyMethodDef magic_methods[] = {
  210. { "open", (PyCFunction)py_magic_open,
  211. METH_VARARGS, _magic_open__doc__ },
  212. { NULL, NULL }
  213. };
  214. static void py_magic_dealloc(PyObject* self)
  215. {
  216. PyObject_Del(self);
  217. }
  218. static PyObject* py_magic_getattr(PyObject* self, char* attrname)
  219. {
  220. return Py_FindMethod(magic_cookie_hnd_methods, self, attrname);
  221. }
  222. PyTypeObject magic_cookie_type = {
  223. PyObject_HEAD_INIT(NULL)
  224. 0,
  225. "Magic cookie",
  226. sizeof(magic_cookie_hnd),
  227. 0,
  228. py_magic_dealloc, /* tp_dealloc */
  229. 0, /* tp_print */
  230. py_magic_getattr, /* tp_getattr */
  231. 0, /* tp_setattr */
  232. 0, /* tp_compare */
  233. 0, /* tp_repr */
  234. 0, /* tp_as_number */
  235. 0, /* tp_as_sequence */
  236. 0, /* tp_as_mapping */
  237. 0, /* tp_hash */
  238. };
  239. /* Initialize constants */
  240. static struct const_vals {
  241. const char* const name;
  242. unsigned int value;
  243. } module_const_vals[] = {
  244. { "MAGIC_NONE", MAGIC_NONE },
  245. { "MAGIC_DEBUG", MAGIC_DEBUG },
  246. { "MAGIC_SYMLINK", MAGIC_SYMLINK },
  247. { "MAGIC_COMPRESS", MAGIC_COMPRESS },
  248. { "MAGIC_DEVICES", MAGIC_DEVICES },
  249. { "MAGIC_MIME", MAGIC_MIME },
  250. { "MAGIC_CONTINUE", MAGIC_CONTINUE },
  251. { "MAGIC_CHECK", MAGIC_CHECK },
  252. { "MAGIC_PRESERVE_ATIME", MAGIC_PRESERVE_ATIME },
  253. { "MAGIC_ERROR", MAGIC_ERROR},
  254. { NULL }
  255. };
  256. static void const_init(PyObject* dict)
  257. {
  258. struct const_vals* tmp;
  259. PyObject *obj;
  260. for(tmp = module_const_vals; tmp->name; ++tmp) {
  261. obj = PyInt_FromLong(tmp->value);
  262. PyDict_SetItemString(dict, tmp->name, obj);
  263. Py_DECREF(obj);
  264. }
  265. }
  266. /*
  267. * Module initialization
  268. */
  269. void initmagic(void)
  270. {
  271. PyObject* module;
  272. PyObject* dict;
  273. /* Initialize module */
  274. module = Py_InitModule("magic", magic_methods);
  275. dict = PyModule_GetDict(module);
  276. magic_error_obj = PyErr_NewException("magic.error", NULL, NULL);
  277. PyDict_SetItemString(dict, "error", magic_error_obj);
  278. magic_cookie_type.ob_type = &PyType_Type;
  279. /* Initialize constants */
  280. const_init(dict);
  281. if(PyErr_Occurred())
  282. Py_FatalError("can't initialize module magic");
  283. }