Re: [Flashcoders] how to uncompress a compressed bytearray with PHP

2008-10-05 Thread Juan Pablo Califano
No worries!

2008/10/3, Benny <[EMAIL PROTECTED]>:
>
> > There's one problem with using URLVariables: it works on NUL-terminated
> Strings
> You're right, so URLVariables is now out of the picture ;-)
>
> I think your class would indeed do the job but because I want to keep the
> client code as light as possible I think I'll just send one big compressed
> ByteArray to the server inflate it and decode the AMF data using the AMF
> capabilities the Zend Framework will bring in the near future.
>
> Thank you for your time and great input!!
> Cheers, Benny
>
> ___
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


RE: [Flashcoders] how to uncompress a compressed bytearray with PHP

2008-10-03 Thread Benny
> There's one problem with using URLVariables: it works on NUL-terminated
Strings
You're right, so URLVariables is now out of the picture ;-)

I think your class would indeed do the job but because I want to keep the
client code as light as possible I think I'll just send one big compressed
ByteArray to the server inflate it and decode the AMF data using the AMF
capabilities the Zend Framework will bring in the near future. 

Thank you for your time and great input!!
Cheers, Benny

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] how to uncompress a compressed bytearray with PHP

2008-10-03 Thread Juan Pablo Califano
There's one problem with using URLVariables: it works on NUL-terminated
strings, so if you happen to have a 0 in the compressed data, it will be
truncated

Some time ago I wrote a class to post data in multipart format (basically,
it does the same the browser does when you have a form with a file input). I
used it to send image files, but since the data is binary, it shouldn't
matter what kind of file you're sending.

So, from the php side you handle it much like an oridnary html form. Text
variables will be put in $_POST, and the "files", i.e. the binary data,
would be in $_FILES.


I had uploaded that class to pastebin and it's still there:
http://pastebin.com/f11a897cf


I'm re-pasting a use example from another email:


=>

 An example of use (it's a copy & paste from some worting code, just to give
you an idea):


import ar.com.califa010.utils.FormData;

   // getEncodedImg() is a method that returns a JPG as a byteArray

   var rawJpg:ByteArray = getEncodedImg();
   var formData:FormData = new FormData();

   var imageMimeType:String = FormData.JPG_FILE;
   var fileName:String = "imageFile.jpg";


   // from the php side, you'll get this data with $_FILES['imageFile']
   // rawJpg is the byteArray you want to send
   // fileName and imageMimeType don't really matter that much
   // it's supposed to send the name of the original file and the type to
hint the server, but
   // I think you can leave it as is and will have no problems

   formData.addFile("imageFile", rawJpg, fileName, imageMimeType);


   formData.addField("sFormat",imgFormat);
   formData.addField("idImagen",_idImagen);

   var req:URLRequest  = new URLRequest(url);

   req.method   = "POST";
   req.contentType  = formData.contentType;
   req.data   = formData.getPostData();

   var loader:URLLoader = new URLLoader();
   configureListeners(loader);

loader.load(req);
 =>

Cheers
Juan Pablo Califano

2008/10/3, Benny <[EMAIL PROTECTED]>:
>
> We are getting closer ;-)
>
> The code you provided worked for me too.
> When I compare my code to yours I notice that I am using the $_POST array
> while you are using the PHP input stream. The other major difference is the
> fact that I am POSTing using via an URLVariables instance.
>
> When I change both in my code then everything indeed works fine.
>
> The only problem now is that in the production code I have to send several
> ByteArray objects to the server in one go. And I have to process them
> separately in the PHP script. Hence I thought the obvious thing would be to
> assign the various vars to an URLVariables object and send that as the
> URLRequest data. On the PHP side I wanted then to access those vars again
> as
> members of the $_POST array and gzinflate the compressed vars there for
> further processing.
>
> Can you think of any solution that would make this possible?
>
> The only thing which comes to my mind at the moment is parsing the php
> input
> string by hand but I still would expect that the $_POST array should just
> work too, after all it seems it contains the correct compressed string. But
> when I feed that string to the gzinflate function I get an empty string in
> return and when I feed it the exact same string hardcoded then it returns
> the correct uncompressed string... I must be missing something obvious ;-)
>
> ___
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] how to uncompress a compressed bytearray with PHP

2008-10-03 Thread Ian Thomas
On Fri, Oct 3, 2008 at 6:36 PM, Benny <[EMAIL PROTECTED]> wrote:

> The only problem now is that in the production code I have to send several
> ByteArray objects to the server in one go. And I have to process them
> separately in the PHP script. Hence I thought the obvious thing would be to
> assign the various vars to an URLVariables object and send that as the
> URLRequest data. On the PHP side I wanted then to access those vars again as
> members of the $_POST array and gzinflate the compressed vars there for
> further processing.
>

Can't you just mash the ByteArrays into one long ByteArray; and at the
beginning of that ByteArray store the number of arrays and the length
of each one?

Then send it as one array and break it apart again once the decoding is done?

Ian
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


RE: [Flashcoders] how to uncompress a compressed bytearray with PHP

2008-10-03 Thread Benny
We are getting closer ;-)

The code you provided worked for me too. 
When I compare my code to yours I notice that I am using the $_POST array
while you are using the PHP input stream. The other major difference is the
fact that I am POSTing using via an URLVariables instance.

When I change both in my code then everything indeed works fine.

The only problem now is that in the production code I have to send several
ByteArray objects to the server in one go. And I have to process them
separately in the PHP script. Hence I thought the obvious thing would be to
assign the various vars to an URLVariables object and send that as the
URLRequest data. On the PHP side I wanted then to access those vars again as
members of the $_POST array and gzinflate the compressed vars there for
further processing.

Can you think of any solution that would make this possible?

The only thing which comes to my mind at the moment is parsing the php input
string by hand but I still would expect that the $_POST array should just
work too, after all it seems it contains the correct compressed string. But
when I feed that string to the gzinflate function I get an empty string in
return and when I feed it the exact same string hardcoded then it returns
the correct uncompressed string... I must be missing something obvious ;-)

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] how to uncompress a compressed bytearray with PHP

2008-10-03 Thread Juan Pablo Califano
Hi,

I think you shouldn't use UTF-8 and mb_ functions. In this scenario, one
useful property of the php strings API is that doesn't really "understand"
anything about encodings. A byte is a char and that's all, so you can use
that API as if it were acting on raw buffers. In this case, that's what you
need, because if you parse the data as UTF-8 you are most likely going
to get illegal sequences at some point.

I've written a little test and it seems to be working (at least I receive
the expected results back in the flash side).

Cheers
Juan Pablo Califano


AS CODE (in the timeline, mind you):


import flash.utils.ByteArray;

var buffer:ByteArray = new ByteArray();
buffer[0] = 0x43;
buffer[1] = 0x61;
buffer[2] = 0x6c;
buffer[3] = 0x69;
buffer[4] = 0x66;
buffer[5] = 0x61;
buffer[6] = 0x00;

buffer.compress();
buffer.position = 0;

var dump:String = "$data = ";
while(buffer.bytesAvailable > 0) {
 dump += "chr(0x" + buffer.readUnsignedByte().toString(16) + ")";
 if(buffer.bytesAvailable > 0) {
  dump += " . ";
 }
}
dump += ";";
trace("dump from flash: " + dump);


import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.events.*;


function completeHandler(event:Event):void {
 var loader:URLLoader = URLLoader(event.target);
 trace("data received from PHP: \n" + loader.data);
}

var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, completeHandler);

var req:URLRequest  = new URLRequest("http://localhost/compress/compress.php
");
req.method = URLRequestMethod.POST;
req.data = buffer;

urlLoader.load(req);



PHP FILE:





2008/10/3, Benny <[EMAIL PROTECTED]>:
>
> (please ignore previous reply - contained mistakes regarding gzuncompress
> while I meant gzinflate)
>
> Thanks Juan Pablo Califano.
>
> I'm already sending the data in binary form so that's is probably not the
> problem.
> You are right about that the php decompression should be done with
> gzinflate
> (I did test with it before but that didn't work either so I thought I
> should
> try gzuncompress ;)
>
> When I compare the deflating results of the same string in PHP and Flash I
> noticed that flash started with two extra bytes while the rest of the
> output
> was the same.
>
> Next I added this code to the PHP test script to remove the two extra
> leading bytes:
>
> mb_internal_encoding("UTF-8");
> $encoded = mb_substr($_POST["XMLString"], 2);
>
> Now when I compare the string in $encoded with the encoded string produced
> by PHP deflate() then both seem to be exactly the same. So I expected that
>
> $decoded = gzinflate($encoded);
>
> would now return the original string but no, an empty string is returned!?
>
> If however I copy (i.e. hardcode) the compressed string into $encoded and
> then execute
>
> $decoded = gzinflate($encoded);
>
> Then it Works; I get the original text!?
>
> Big question now why does it work with the hardcoded compressed string and
> not with runtime compressed string, they seem to be exactly the same??
>
> Any ideas?
>
> BTW: thanks for your time :)
>
>
> ___
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


RE: [Flashcoders] how to uncompress a compressed bytearray with PHP

2008-10-03 Thread Benny
(please ignore previous reply - contained mistakes regarding gzuncompress
while I meant gzinflate)

Thanks Juan Pablo Califano.

I'm already sending the data in binary form so that's is probably not the
problem.
You are right about that the php decompression should be done with gzinflate
(I did test with it before but that didn't work either so I thought I should
try gzuncompress ;)

When I compare the deflating results of the same string in PHP and Flash I
noticed that flash started with two extra bytes while the rest of the output
was the same.

Next I added this code to the PHP test script to remove the two extra
leading bytes:

  mb_internal_encoding("UTF-8");
  $encoded = mb_substr($_POST["XMLString"], 2);

Now when I compare the string in $encoded with the encoded string produced
by PHP deflate() then both seem to be exactly the same. So I expected that 

  $decoded = gzinflate($encoded);
 
would now return the original string but no, an empty string is returned!?

If however I copy (i.e. hardcode) the compressed string into $encoded and
then execute 
  
  $decoded = gzinflate($encoded);

Then it Works; I get the original text!?

Big question now why does it work with the hardcoded compressed string and
not with runtime compressed string, they seem to be exactly the same??

Any ideas?

BTW: thanks for your time :)


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


RE: [Flashcoders] how to uncompress a compressed bytearray with PHP

2008-10-03 Thread Benny
Thanks Juan Pablo Califano.

I'm already sending the data in binary form so that's is probably not the
problem.
You are right about that the php decompression should be done with gzinflate
(I did test with it before but that didn't work either so I thought I should
try gzuncompress ;)

When I compare the deflating results of the same string in PHP and Flash I
noticed that flash started with two extra bytes while the rest of the output
was the same.

Next I added this code to the PHP test script to remove the two extra
leading bytes:

  mb_internal_encoding("UTF-8");
  $encoded = mb_substr($_POST["XMLString"], 2);

Now when I compare the string in $encoded with the encoded string produced
by PHP deflate() then both seem to be exactly the same. So I expected that 

  $decoded = gzuncompress($encoded);
 
would now return the original string but no, an empty string is returned!?

If however I copy (i.e. hardcode) the compressed string into $encoded and
then execute 
  
  $decoded = gzuncompress($encoded);

Then it Works; I get the original text!?

Big question now why does it work with the hardcoded compressed string and
not with runtime compressed string, they seem to be exactly the same??

Any ideas?

BTW: thanks for your time :)


___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] how to uncompress a compressed bytearray with PHP

2008-10-02 Thread Juan Pablo Califano
I see a couple of spots for problems in what you are doing.

When you're compressing the data, you are basically transforming text
strings to binary. I mean, text is binary data, but generally strings cannot
contain NUL characters (that is a 0 value), because that's traditionally
reserved to mark the end of the string. On the other hand, 0 is a perfectly
legal value in a "binary context" (so to speak) and I think you'll probably
get some 0's in the compressed data (or at least, I'm not sure if you could
be sure you won't, under any circumstance). So, when you're posting data
(except you post as binary), NULs will be a problem, because the data (the
string) will be truncated.

>From the php side, I think you should use the gzinflate function, as the
ByteArray class uses inflate / deflate (from zlib). I haven't used it in
php, just found it in the manual:
http://ar.php.net/manual/en/function.gzinflate.php

The signature of the function says it takes a string, so should probably
check what happens if you have embbeded NULs here too... (my guess is that
php will also truncate the data).

You could try to post the data as binary and see if it works, but I'd first
check that the uncompressing is working on the php side. A quick and dirty
idea: you could check this by dumping the compressed ByteArray in the Flash
IDE, for instance, to the trace output, in the form of a php assignment.
I.e, printing something like $data = chr(someNumber) . chr(someOtherNumber)
. chr(someOtherNumber); Then paste that in your php file and try to get the
uncompressed data hardcoding the generated php assignment. If you get that
right, then you know the only remaining problem is passing the data itself
to php in a "safe" way. Or you could even encode it as base64 (which is used
precisely to avoid the embedded NULs problem); decoding from the php side is
just a matter of calling base64_decode(); from the AS side, you don't have
anything builtin, but there are classes for encoding base64 around (I wrote
one some time ago). Of course, using base64 would add more overhead and will
add to the size of the data you're posting (1/3 of the unencoded size), but
maybe it turns out to be simpler than grabbing the binary data from php.

Cheers
Juan Pablo Califano


2008/10/2, Benny <[EMAIL PROTECTED]>:
>
> I am sending a compressed bytearray to PHP:
>
>
>
> var XMLOut:ByteArray = new ByteArray();
>
> XMLOut.writeUTFBytes("just an example");
>
> XMLOut.compress();
>
> . cut .
>
>
>
> The data is POSTed (with URLLoader in var XMLString) to
> mydomain.com/save.php:
>
>
>
> 
> $XMLString = gzuncompress($_POST["XMLString "]);
>
> $handle = fopen("content/test.xml", "w+b");
>
> fwrite($handle, $ XMLString);
>
> fclose($handle);
>
> ?>
>
>
>
> The result of this is that test.xml is being created but without any
> content.
>
> (BTW: If I leave out the compression/decompression part then the file is
> created with the expected content)
>
>
>
> Should this actually work with gzuncompress and if so why might it be
> failing here?
>
> Or should the uncompressioning be handled differently?
>
>
>
>
>
> ___
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


[Flashcoders] how to uncompress a compressed bytearray with PHP

2008-10-02 Thread Benny
I am sending a compressed bytearray to PHP:

 

var XMLOut:ByteArray = new ByteArray();

XMLOut.writeUTFBytes("just an example");

XMLOut.compress();

. cut .

 

The data is POSTed (with URLLoader in var XMLString) to
mydomain.com/save.php:

 



 

The result of this is that test.xml is being created but without any
content.

(BTW: If I leave out the compression/decompression part then the file is
created with the expected content)

 

Should this actually work with gzuncompress and if so why might it be
failing here?

Or should the uncompressioning be handled differently?

 

 

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders