|
| 1 | +/* libinjection.i SWIG interface file */ |
| 2 | +%module libinjection |
| 3 | +%{ |
| 4 | +#include "libinjection.h" |
| 5 | +#include "libinjection_sqli.h" |
| 6 | +#include <stddef.h> |
| 7 | + |
| 8 | +/* This is the callback function that runs a python function |
| 9 | + * |
| 10 | + */ |
| 11 | +static char libinjection_python_check_fingerprint(sfilter* sf, int lookuptype, const char* word, size_t len) |
| 12 | +{ |
| 13 | + PyObject *fp; |
| 14 | + PyObject *arglist; |
| 15 | + PyObject *result; |
| 16 | + const char* strtype; |
| 17 | + char ch; |
| 18 | + |
| 19 | + // get sfilter->pattern |
| 20 | + // convert to python string |
| 21 | + fp = SWIG_InternalNewPointerObj((void*)sf, SWIGTYPE_p_libinjection_sqli_state,0); |
| 22 | + |
| 23 | + arglist = Py_BuildValue("(Nis#)", fp, lookuptype, word, len); |
| 24 | + // call pyfunct with string arg |
| 25 | + result = PyObject_CallObject((PyObject*) sf->userdata, arglist); |
| 26 | + Py_DECREF(arglist); |
| 27 | + if (result == NULL) { |
| 28 | + printf("GOT NULL\n"); |
| 29 | + // python call has an exception |
| 30 | + // pass it back |
| 31 | + ch = '\0'; |
| 32 | + } else { |
| 33 | + // convert value of python call to a char |
| 34 | + strtype = PyString_AsString(result); |
| 35 | + ch = strtype[0]; |
| 36 | + Py_DECREF(result); |
| 37 | + } |
| 38 | + return ch; |
| 39 | +} |
| 40 | + |
| 41 | +%} |
| 42 | +%include "typemaps.i" |
| 43 | + |
| 44 | +// The C functions all start with 'libinjection_' as a namespace |
| 45 | +// We don't need this since it's in the libinjection python package |
| 46 | +// i.e. libinjection.libinjection_is_sqli --> libinjection.is_sqli |
| 47 | + // |
| 48 | +%rename("%(strip:[libinjection_])s") ""; |
| 49 | + |
| 50 | +// SWIG doesn't natively support fixed sized arrays. |
| 51 | +// this typemap converts the fixed size array sfilter.tokevec |
| 52 | +// into a list of pointers to stoken_t types. In otherword this code makes this example work |
| 53 | +// s = sfilter() |
| 54 | +// libinjection_is_sqli(s, "a string",...) |
| 55 | +// for i in len(s.pat): |
| 56 | +// print s.tokevec[i].val |
| 57 | +// |
| 58 | + |
| 59 | +%typemap(out) stoken_t [ANY] { |
| 60 | +int i; |
| 61 | +$result = PyList_New($1_dim0); |
| 62 | +for (i = 0; i < $1_dim0; i++) { |
| 63 | + PyObject *o = SWIG_NewPointerObj((void*)(& $1[i]), SWIGTYPE_p_stoken_t,0); |
| 64 | + PyList_SetItem($result,i,o); |
| 65 | +} |
| 66 | +} |
| 67 | + |
| 68 | +// automatically append string length into arg array |
| 69 | +%apply (char *STRING, size_t LENGTH) { (const char *s, size_t slen) }; |
| 70 | + |
| 71 | +%typemap(in) (ptr_lookup_fn fn, void* userdata) { |
| 72 | + if ($input == Py_None) { |
| 73 | + $1 = NULL; |
| 74 | + $2 = NULL; |
| 75 | + } else { |
| 76 | + $1 = libinjection_python_check_fingerprint; |
| 77 | + $2 = $input; |
| 78 | + } |
| 79 | +} |
| 80 | +%include "libinjection.h" |
| 81 | +%include "libinjection_sqli.h" |
0 commit comments