[plt-scheme] FFI and byte-strings

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Sat Feb 3 05:31:51 EST 2007

Hi all,

Is there a way to go from a byte-string to a pointer
to the beginning of the c-array, where the actual bytes
are stored?

What I'd like to do is the following, where the
sought after function is called bytes->pointer:

(define (final-context->bytes context)
   (let ([bs (make-bytes
               (md-name->output-size
                 (context-md-name context))])
     (EVP_DigestFinal_ex (context-c context)
                         (bytes->pointer bs)
                         #f)))

The function EVP_DigestFinal_ex takes 3 arguments,
a pointer to a context strucuture, a pointer to
an array in which to store the bytes of the message
digest and a pointer to an int.

That is EVP_DigestFinal_ex stores the final
message digest to the array pointed to by
the second argument. Since the length of the
message digest is known, it should be possible
to write directly into the byte string to avoid
copying.

As a work-around I could use malloc allocate
space for the message digest and then use
make-sized-byte-string -- but then I'm suddenly
responsible for deallocating the piece of memory
in question.

-- 
Jens Axel Søgaard




Posted on the users mailing list.