Rene Rebe | 3 Sep 22:03 2006

%cstring_output_allocate_size and perl

Hi all,

while using SWIG to bridge a C++ (imaging) library with e.g. Perl I run
into a problem with &cstring_output_allocate_size.

Among the functions I export is one to store the encoded image in
the system memory.

According the SWIG documentation %cstring_output_allocate_size
should be what I need "This macro is used to return strings that are
allocated within the program and ... the returned string may contain
binary data.".

However in my case the Perl scalar I get is terminated at the first
embedded binary 0.

I use Perl-5.8.8 and SWIG-1.3.29.

Fragments of my code so far (simplified for the list):

#ifdef SWIG
%cstring_output_allocate_size( char ** s, int *slen, free(*$1))
void encodeImage (char **s, int *slen,
		  Image* image, const char* codec, int quality,
		  const char* compression);


void encodeImage (char **s, int *slen,
		  Image* image, const char* codec, int quality,
		  const char* compression)
  // just for testing ...
  std::ostringstream stream (""); // empty string to start with

  stream << '1'; <<  '2' << '\0' << '3' << '4';

  std::cerr << "c++> size: " << stream.str().size() << std::endl;

  char* payload = (char*) malloc (stream.str().size());
  memcpy (payload, stream.str().c_str(), stream.str().size());

  *s = payload;
  *slen = stream.str().size();

and when I call it from Perl:

$image_bits = ExactImage::encodeImage ($image, "jpeg", 80, "");
print "perl> size: " . length($image_bits) . "\n";

I get:

c++> size: 5
perl> size: 2

When I look at the code generated from SWIG I noticed:

      if (argvi >= items) EXTEND(sp,1);  ST(argvi) = SWIG_FromCharPtrAndSize(*arg1,*arg2); argvi++  ;

and wonder if that function is the right thing to call there as
it only sets the pointer and no size in the target object.

Can someone confirm I hit a SWIG bug to be fixed or is there
some other function that I should use instead?

Thanks in advance,


René userRebe - ExactCODE - Berlin (Europe / Germany)
   | |
            +49 (0)30 / 255 897 45

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo