Browse Source

[php] fix cross-platform Bytes sreialization/deserialization (#8943)

Aleksandr Kuzmenko 5 years ago
parent
commit
1e77f62999
4 changed files with 28 additions and 1 deletions
  1. 1 0
      std/haxe/Serializer.hx
  2. 2 1
      std/haxe/Unserializer.hx
  3. 6 0
      std/php/Global.hx
  4. 19 0
      tests/unit/src/unit/TestSerialize.hx

+ 1 - 0
std/haxe/Serializer.hx

@@ -343,6 +343,7 @@ class Serializer {
 						buf.add(chars);
 						#elseif php
 						var chars = new String(php.Global.base64_encode(v.getData()));
+						chars = php.Global.strtr(chars, '+/', '%:');
 						buf.add("s");
 						buf.add(chars.length);
 						buf.add(":");

+ 2 - 1
std/haxe/Unserializer.hx

@@ -389,7 +389,8 @@ class Unserializer {
 				#if neko
 				var bytes = haxe.io.Bytes.ofData(base_decode(untyped buf.substr(pos, len).__s, untyped BASE64.__s));
 				#elseif php
-				var bytes = haxe.io.Bytes.ofData(php.Global.base64_decode(buf.substr(pos, len)));
+				var phpEncoded = php.Global.strtr(buf.substr(pos, len), '%:', '+/');
+				var bytes = haxe.io.Bytes.ofData(php.Global.base64_decode(phpEncoded));
 				#else
 				var codes = CODES;
 				if (codes == null) {

+ 6 - 0
std/php/Global.hx

@@ -380,6 +380,12 @@ extern class Global {
 	**/
 	static function strcmp(str1:String, str2:String):Int;
 
+	/**
+		@see http://php.net/manual/en/function.strtr.php
+	**/
+	@:overload(function(str:String, from:NativeAssocArray<String>):String {})
+	static function strtr(str:String, from:String, to:String):String;
+
 	/**
 		@see http://php.net/manual/en/function.str-repeat.php
 	**/

+ 19 - 0
tests/unit/src/unit/TestSerialize.hx

@@ -116,6 +116,7 @@ class TestSerialize extends Test {
 		for( i in 0...b.length )
 			b.set(i,i%10);
 		doTestBytes(b);
+		doTestBytesCrossPlatform();
 
 		// recursivity
 		c.ref = c;
@@ -175,4 +176,22 @@ class TestSerialize extends Test {
 			eq( b2.get(i), b.get(i) );
 	}
 
+	function doTestBytesCrossPlatform() {
+		var sample = 's340:AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0%P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn%AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq%wsbKztLW2t7i5uru8vb6:wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t:g4eLj5OXm5%jp6uvs7e7v8PHy8:T19vf4%fr7:P3%';
+
+		//serialization
+		var b = haxe.io.Bytes.alloc(255);
+		for(i in 0...255) b.set(i, i);
+		eq(sample, haxe.Serializer.run(b));
+
+		//de-serialization
+		var b:haxe.io.Bytes = haxe.Unserializer.run(sample);
+		eq(255, b.length);
+		for(i in 0...b.length) {
+			var byte = b.get(i);
+			eq(i, byte);
+			if(i != byte) break;
+		}
+	}
+
 }