Mailing List Archive

SWIG and binary strings?
I've swig'ed OpenSSL library for my Python needs. That was a cakewalk,
until I ran at the 'char *, int'-defined strings with nulls embedded in
them used in, among other things, SSL_write().

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:

- Define each function in wrong fashion in the .i
(SSL_write as (SSL*, char *) while it should be SSL_write(SSL *, char *,
int), etc)

- Run SWIG

- Run patch after SWIG, to do following fixes:
@@ -1108,10 +1110,11 @@
int _result;
SSL * _arg0;
char * _arg1;
+ int _arg2;
char * _argc0 = 0;
self = self;
- if(!PyArg_ParseTuple(args,ßs:SSL_write",&_argc0,&_arg1))
+ if(!PyArg_ParseTuple(args,ßs#:SSL_write",&_argc0,&_arg1,&_arg2))
return NULL;
if (_argc0) {
if (SWIG_GetPtr(_argc0,(void **) &_arg0,"_SSL_p")) {
@@ -1119,7 +1122,7 @@
return NULL;
}
}
- _result = (int )SSL_write(_arg0,_arg1);
+ _result = (int )SSL_write(_arg0,_arg1,_arg2);
_resultobj = Py_BuildValue(ï",_result);
return _resultobj;
}

.. and obviously same for other functions as well.
I do not find this elegant, however, is there any other way? (Beyond
writing ugly wrappers in pure Python-C, and I am too lazy to maintain those
;->)

- Academically-curious-ly, Markus

--
Markus Stenberg
Finger fingon@mpoli.fi for PGP key -- PGP key id: 1024/5DAC7D21
SWIG and binary strings? [ In reply to ]
In article <al8pv2nwnny.fsf@myntti.helsinki.fi>,
Markus Stenberg <mstenber@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@post1.com>
SWIG and binary strings? [ In reply to ]
ngps@madcap.dyn.ml.org (Ng Pheng Siong) writes:
> In article <al8pv2nwnny.fsf@myntti.helsinki.fi>,
> Markus Stenberg <mstenber@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.)

Ah, I've concentrated on SSL itself (TLS actually, but still; full TLS/SSL
functionality, no ciphers whatsoever wrapped). I wish someone wrapped whole
OpenSSL and maintained it.. ;-) (I'd do it, but I'm too lazy by half)

> >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:

It does; I'd prefer solution that didn't sacrifice any speed (although,
admittedly, I guess your approach's overhead isn't very high) because
esp. SSL_write is called fairly often ;-) Also, being transparent to the
user wouldn't hurt (.. count length of strings? isn't this Century of the
Fruitbat?)

I thought of that as well.. Oh well. Hoped there was some really elegant
solution I was just missing ;-)

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

-Markus Stenberg

--
"Schizophrenia, like the Unicorn, is described in various ways by
various people. And its elimination, like the capture of the Unicorn,
appears to require some special conditions, not all of which have yet
been specified. Finally, whether or not those conditions exist, belief
in their existence has had significant consequences."
- Schizophrenia, Behavioural Aspects (Salzinger, 1972)
SWIG and binary strings? [ In reply to ]
On 28 Jun 1999 07:57:04 +0300, Markus Stenberg <mstenber@cc.Helsinki.FI> wrote:
>Ah, I've concentrated on SSL itself (TLS actually, but still; full TLS/SSL
>functionality, no ciphers whatsoever wrapped). I wish someone wrapped whole
>OpenSSL and maintained it.. ;-) (I'd do it, but I'm too lazy by half)

mxCrypto (http://www.cs.uni-duesseldorf.de/~lemburg/mxCrypto.html)
wraps the cipher implementations in OpenSSL; talk to M.A. Lemburg, the
author of mxCrypto, about incorporating your SWIG interfaces into
mxCrypto. I believe he's been wanting to wrap the SSL interfaces for
a while; you may be able to save him some time.

(mxCrypto + SSL socket support would be a wonderful thing to
have, particularly if it's developed outside of the US.)

--
A.M. Kuchling http://starship.python.net/crew/amk/
It would be nice to be unfailingly, perpetually, remorselessly funny, day in
and day out, year in and year out until somebody murdered you, now wouldn't it?
-- Robertson Davies, _The Diary of Samuel Marchbanks_