SWIG and binary strings?

Ng Pheng Siong ngps at madcap.dyn.ml.org
Fri Jun 25 12:06:48 EDT 1999


In article <al8pv2nwnny.fsf at myntti.helsinki.fi>,
Markus Stenberg  <mstenber at cc.Helsinki.FI> wrote:
>I've swig'ed OpenSSL library for my Python needs. 

Me too! 

Well, I've only done RSA and block ciphers, since that's
what my application needs for now. (Ok, one block cipher. 
Adding more block ciphers looks like cut-&-paste to me.)


>Is there any elegant way to make SWIG generate s# automatically (as opposed
>to s, and requiring unneccessary length parameter and much whining if
>string has 0's?)
>
>My (ugly) solution was following:

Dunno if my solution qualifies as ugly, but here's what I do:

typedef struct _blob {
        unsigned char *data;
        int len;
} Blob;

%typemap(python, in) Blob * {
        if (!PyString_Check($source)) {
                PyErr_SetString(PyExc_TypeError, "expected Blob*");
                return NULL;
        }
        $target=(Blob *)malloc(sizeof(Blob));
        if (!$target) {
                PyErr_SetString(PyExc_MemoryError, "malloc Blob*");
                return NULL;
        }
        $target->data=PyString_AsString($source);
        $target->len=PyString_Size($source);
}

Blob *rsa_private_encrypt(RSA *rsa, Blob *from, int padding) { 
	Blob *to;
	...
	to->len=RSA_private_encrypt(from->len, from->data, to->data, ...)
	...
}


Cheers.
-- 
Ng Pheng Siong <ngps at post1.com>





More information about the Python-list mailing list