Ver Fonte

Merge pull request #4065 from HaxeFoundation/php-bytesdata-refactoring

[php] refactor bytesdata
Simon Krajewski há 10 anos atrás
pai
commit
f025cb5b50

+ 1 - 1
std/haxe/crypto/Md5.hx

@@ -42,7 +42,7 @@ class Md5 {
 		#if neko
 			return haxe.io.Bytes.ofData(make_md5(b.getData()));
 		#elseif php
-			return haxe.io.Bytes.ofData(untyped __call__("md5", b.getData(), true));
+			return haxe.io.Bytes.ofData( haxe.io.BytesData.ofString(untyped __call__("md5", b.getData().toString(), true)));
 		#else
 			var h = new Md5().doEncode(bytes2blks(b));
 			var out = haxe.io.Bytes.alloc(16);

+ 1 - 1
std/haxe/crypto/Sha1.hx

@@ -38,7 +38,7 @@ class Sha1 {
 
 	public static function make( b : haxe.io.Bytes ) : haxe.io.Bytes {
 		#if php
-		return haxe.io.Bytes.ofData(untyped __call__("sha1", b.getData(), true));
+		return haxe.io.Bytes.ofData(haxe.io.BytesData.ofString(untyped __call__("sha1", b.getData().toString(), true)));
 		#else
 		var h = new Sha1().doEncode(bytes2blks(b));
 		var out = haxe.io.Bytes.alloc(20);

+ 20 - 20
std/haxe/crypto/Sha224.hx

@@ -25,7 +25,7 @@ package haxe.crypto;
     Creates a Sha224 of a String.
 */
 class Sha224 {
-    
+
     public static function encode( s:String ) : String {
         #if php
         return untyped __call__("hash", "sha224", s);
@@ -38,7 +38,7 @@ class Sha224 {
 
     public static function make( b : haxe.io.Bytes ) : haxe.io.Bytes {
         #if php
-        return haxe.io.Bytes.ofData(untyped __call__("hash", "sha224", b.getData(), true));
+        return haxe.io.Bytes.ofData(haxe.io.BytesData.ofString(untyped __call__("hash", "sha224", b.getData().toString(), true)));
         #else
         var h = new Sha224().doEncode(b.toString(), b.length*8);
         var out = haxe.io.Bytes.alloc(28);
@@ -52,26 +52,26 @@ class Sha224 {
         return out;
         #end
     }
-    
+
     public function new() {
     }
-    
+
     function doEncode( str : String, strlen : Int ) : Array<Int> {
         var K : Array<Int> = [
             0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
-            0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 
+            0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
             0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
-            0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 
-            0xE49B69C1, 0xEFBE4786, 0xFC19DC6 , 0x240CA1CC, 
-            0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 
+            0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
+            0xE49B69C1, 0xEFBE4786, 0xFC19DC6 , 0x240CA1CC,
+            0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
             0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
-            0xC6E00BF3, 0xD5A79147, 0x6CA6351 , 0x14292967, 
+            0xC6E00BF3, 0xD5A79147, 0x6CA6351 , 0x14292967,
             0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
-            0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 
+            0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
             0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
-            0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 
+            0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
             0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
-            0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 
+            0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
             0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
             0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
         ];
@@ -158,38 +158,38 @@ class Sha224 {
     function ROTR(X, n) {
         return ( X >>> n ) | (X << (32 - n));
     }
-    
+
     // ++
     function SHR(X, n) {
         return ( X >>> n );
     }
-    
+
     // ++
     function Ch(x, y, z) {
         return ((x & y) ^ ((~x) & z));
     }
-    
+
     // ++
     function Maj(x, y, z) {
         return ((x & y) ^ (x & z) ^ (y & z));
     }
-    
+
     function Sigma0(x) {
         return ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22);
     }
-    
+
     function Sigma1(x) {
         return ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25);
     }
-    
+
     function Gamma0(x) {
         return ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3);
     }
-    
+
     function Gamma1(x) {
         return ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10);
     }
-    
+
     function hex( a : Array<Int> ){
         var str = "";
         for( num in a ) {

+ 14 - 14
std/haxe/crypto/Sha256.hx

@@ -25,7 +25,7 @@ package haxe.crypto;
     Creates a Sha256 of a String.
 */
 class Sha256 {
-	
+
 	public static function encode( s:String ) : String {
 		#if php
 		return untyped __call__("hash", "sha256", s);
@@ -38,7 +38,7 @@ class Sha256 {
 
 	public static function make( b : haxe.io.Bytes ) : haxe.io.Bytes {
 		#if php
-		return haxe.io.Bytes.ofData(untyped __call__("hash", "sha256", b.getData(), true));
+		return haxe.io.Bytes.ofData(haxe.io.BytesData.ofString(untyped __call__("hash", "sha256", b.getData().toString(), true)));
 		#else
 		var h = new Sha256().doEncode(bytes2blks(b), b.length*8);
 		var out = haxe.io.Bytes.alloc(32);
@@ -52,10 +52,10 @@ class Sha256 {
 		return out;
 		#end
 	}
-	
+
 	public function new() {
 	}
-	
+
 	function doEncode( m : Array<Int>, l : Int ) : Array<Int> {
 		var K : Array<Int> = [
 			0x428A2F98,0x71374491,0xB5C0FBCF,0xE9B5DBA5,0x3956C25B,
@@ -107,7 +107,7 @@ class Sha256 {
 		}
 		return HASH;
 	}
-	
+
 	/*
 		Convert a string to a sequence of 16-word blocks, stored as an array.
 		Append padding bits and the length, as described in the SHA1 standard.
@@ -149,41 +149,41 @@ class Sha256 {
 	function S(X, n) {
 		return ( X >>> n ) | (X << (32 - n));
 	}
-	
+
 	function R(X, n) {
 		return ( X >>> n );
 	}
-	
+
 	function Ch(x, y, z) {
 		return ((x & y) ^ ((~x) & z));
 	}
-	
+
 	function Maj(x, y, z) {
 		return ((x & y) ^ (x & z) ^ (y & z));
 	}
-	
+
 	function Sigma0256(x) {
 		return (S(x, 2) ^ S(x, 13) ^ S(x, 22));
 	}
-	
+
 	function Sigma1256(x) {
 		return (S(x, 6) ^ S(x, 11) ^ S(x, 25));
 	}
-	
+
 	function Gamma0256(x) {
 		return (S(x, 7) ^ S(x, 18) ^ R(x, 3));
 	}
-	
+
 	function Gamma1256(x) {
 		return (S(x, 17) ^ S(x, 19) ^ R(x, 10));
 	}
-	
+
 	function safeAdd(x, y) {
 		var lsw = (x & 0xFFFF) + (y & 0xFFFF);
 		var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
 		return (msw << 16) | (lsw & 0xFFFF);
 	}
-	
+
 	function hex( a : Array<Int> ){
 		var str = "";
 		for( num in a ) {

+ 12 - 12
std/haxe/io/Bytes.hx

@@ -44,7 +44,7 @@ class Bytes {
 		#elseif flash
 		return b[pos];
 		#elseif php
-		return untyped __call__("ord", b[pos]);
+		return b.get(pos);
 		#elseif cpp
 		return untyped b[pos];
 		#elseif java
@@ -62,7 +62,7 @@ class Bytes {
 		#elseif flash
 		b[pos] = v;
 		#elseif php
-		b[pos] = untyped __call__("chr", v);
+		b.set(pos, v);
 		#elseif cpp
 		untyped b[pos] = v;
 		#elseif java
@@ -83,7 +83,7 @@ class Bytes {
 		#if neko
 		try untyped $sblit(b,pos,src.b,srcpos,len) catch( e : Dynamic ) throw Error.OutsideBounds;
 		#elseif php
-		b = untyped __php__("substr($this->b, 0, $pos) . substr($src->b, $srcpos, $len) . substr($this->b, $pos+$len)"); //__call__("substr", b, 0, pos)+__call__("substr", src.b, srcpos, len)+__call__("substr", b, pos+len);
+		b.blit(pos, src.b, srcpos, len);
 		#elseif flash
 		b.position = pos;
 		if( len > 0 ) b.writeBytes(src.b,srcpos,len);
@@ -142,7 +142,7 @@ class Bytes {
 		b.readBytes(b2,0,len);
 		return new Bytes(len,b2);
 		#elseif php
-		return new Bytes(len, untyped __call__("substr", b, pos, len));
+		return new Bytes(len, b.sub(pos, len));
 		#elseif java
 		var newarr = new java.NativeArray(len);
 		java.lang.System.arraycopy(b, pos, newarr, 0, len);
@@ -188,7 +188,7 @@ class Bytes {
 		b2.endian = flash.utils.Endian.LITTLE_ENDIAN;
 		return length - other.length;
 		#elseif php
-		return untyped __php__("$this->b < $other->b ? -1 : ($this->b == $other->b ? 0 : 1)");
+		return b.compare(other.b);
 		//#elseif cs
 		//TODO: memcmp if unsafe flag is on
 		#elseif cpp
@@ -364,7 +364,7 @@ class Bytes {
 		b.position = pos;
 		return b.readUTFBytes(len);
 		#elseif php
-		return untyped __call__("substr", b, pos, len);
+		return b.getString(pos, len);
 		#elseif cpp
 		var result:String="";
 		untyped __global__.__hxcpp_string_of_bytes(b,result,pos,len);
@@ -420,7 +420,7 @@ class Bytes {
 		b.position = 0;
 		return b.readUTFBytes(length);
 		#elseif php
-		return cast b;
+		return b.toString();
 		#elseif cs
 		return cs.system.text.Encoding.UTF8.GetString(b, 0, length);
 		#elseif java
@@ -460,7 +460,7 @@ class Bytes {
 		b.length = length;
 		return new Bytes(length,b);
 		#elseif php
-		return new Bytes(length, untyped __call__("str_repeat", __call__("chr", 0), length));
+		return new Bytes(length, BytesData.alloc(length));
 		#elseif cpp
 		var a = new BytesData();
 		if (length>0) a[length-1] = untyped 0;
@@ -487,8 +487,8 @@ class Bytes {
 		b.writeUTFBytes(s);
 		return new Bytes(b.length,b);
 		#elseif php
-		return new Bytes(untyped __call__("strlen", s), cast s);
-//		return ofData(untyped __call__("new _hx_array", __call__("array_values", __call__("unpack", "C*",  s))));
+		var x = BytesData.ofString(s);
+		return new Bytes(x.length, x);
 		#elseif cpp
 		var a = new BytesData();
 		untyped __global__.__hxcpp_bytes_of_string(a,s);
@@ -543,7 +543,7 @@ class Bytes {
 		#elseif neko
 		return new Bytes(untyped __dollar__ssize(b),b);
 		#elseif php
-		return new Bytes(untyped __call__("strlen", b), b);
+		return new Bytes(b.length, b);
 		#elseif cs
 		return new Bytes(b.Length,b);
 		#else
@@ -561,7 +561,7 @@ class Bytes {
 		#elseif flash
 		return b[pos];
 		#elseif php
-		return untyped __call__("ord", b[pos]);
+		return b.get(pos);
 		#elseif cpp
 		return untyped b.unsafeGet(pos);
 		#elseif java

+ 3 - 3
std/haxe/io/BytesBuffer.hx

@@ -97,7 +97,7 @@ class BytesBuffer {
 		#elseif flash
 		b.writeBytes(src.getData());
 		#elseif php
-		b += cast src.getData();
+		b += src.getData().toString();
 		#elseif cs
 		b.Write(src.getData(), 0, src.length);
 		#elseif java
@@ -166,7 +166,7 @@ class BytesBuffer {
 		#elseif flash
 		if( len > 0 ) b.writeBytes(src.getData(),pos,len);
 		#elseif php
-		b += untyped __call__("substr", src.b, pos, len);
+		b += src.getData().sub(pos, len).toString() ;
 		#elseif cs
 		b.Write(src.getData(), pos, len);
 		#elseif java
@@ -196,7 +196,7 @@ class BytesBuffer {
 		var bytes = new Bytes(b.length,b);
 		b.position = 0;
 		#elseif php
-		var bytes = new Bytes(b.length, cast b);
+		var bytes = new Bytes(b.length, BytesData.ofString(b));
 		#elseif cs
 		var buf = b.GetBuffer();
 		var bytes = new Bytes(cast b.Length, buf);

+ 1 - 1
std/haxe/io/BytesData.hx

@@ -26,7 +26,7 @@ package haxe.io;
 #elseif flash
 	typedef BytesData =	flash.utils.ByteArray;
 #elseif php
-	typedef BytesData =	php.NativeString;
+	typedef BytesData = php.BytesData;
 #elseif cpp
 	extern class Unsigned_char__ { }
 	typedef BytesData = Array<Unsigned_char__>;

+ 2 - 2
std/haxe/io/BytesInput.hx

@@ -97,7 +97,7 @@ class BytesInput extends Input {
 			#if neko
 			return untyped __dollar__sget(b,pos++);
 			#elseif php
-			return untyped __call__("ord", b[pos++]);
+			return b.get(pos++);
 			#elseif cpp
 			return untyped b[pos++];
 			#elseif java
@@ -141,7 +141,7 @@ class BytesInput extends Input {
 			#if neko
 			try untyped __dollar__sblit(buf.getData(),pos,b,this.pos,len) catch( e : Dynamic ) throw Error.OutsideBounds;
 			#elseif php
-			untyped __php__("$buf->b = substr($buf->b, 0, $pos) . substr($this->b, $this->pos, $len) . substr($buf->b, $pos+$len)");
+			buf.getData().blit(pos, b, this.pos, len);
 			#else
 			var b1 = b;
 			var b2 = #if js @:privateAccess buf.b #else buf.getData() #end;

+ 1 - 1
std/haxe/io/Input.hx

@@ -67,7 +67,7 @@ class Input {
 			#if neko
 				untyped __dollar__sset(b,pos,readByte());
 			#elseif php
-				b[pos] = untyped __call__("chr", readByte());
+				b.set(pos, readByte());
 			#elseif cpp
 				b[pos] = untyped readByte();
 			#else

+ 73 - 0
std/php/BytesData.hx

@@ -0,0 +1,73 @@
+package php;
+
+
+
+private class Wrapper {
+	public var s : NativeString;
+	public inline function new (s:NativeString) {
+		this.s = s;
+	}
+}
+
+abstract BytesData(Wrapper) {
+
+	inline function new (x:Wrapper) this = x;
+
+	inline function str ():php.NativeString return this.s;
+
+	inline function setNativeString (val:NativeString):Void {
+		this.s = val;
+	}
+
+	inline function get_length ():Int {
+		return untyped __call__("strlen", str());
+	}
+
+	static inline function wrap (s:NativeString):Wrapper {
+		return new Wrapper(s);
+	}
+
+	static inline function ofNativeString (s:NativeString) {
+		return new BytesData( wrap(s));
+	}
+
+	public inline function set (index:Int, val:Int):Void {
+		untyped __php__("{0}->s[{1}] = chr({2})", this, index, val);
+	}
+
+	public var length(get, never):Int;
+
+	public inline function compare (other:BytesData):Int {
+		return untyped __php__("{0} < {1} ? -1 : ({0} == {1} ? 0 : 1)", str(), other.str());
+	}
+
+	public inline function get (pos:Int):Int {
+		return untyped __call__("ord", str()[pos]);
+	}
+
+	public inline function copy ():BytesData {
+		return ofNativeString(str());
+	}
+
+	public inline function getString (pos:Int, len:Int):String {
+		return untyped __call__("substr", str(), pos, len);
+	}
+
+	public inline function sub (pos:Int, len:Int):BytesData {
+		return ofString(untyped __call__("substr", str(), pos, len));
+	}
+
+	public inline function blit (pos : Int, src : BytesData, srcpos : Int, len : Int):Void {
+		setNativeString(untyped __php__("substr({0}, 0, {2}) . substr({1}, {3}, {4}) . substr({0}, {2}+{4})", str(), src.str(), pos, srcpos, len));
+	}
+
+	public inline function toString():String return cast str();
+
+	public static inline function ofString (s:String) {
+		return ofNativeString(cast s);
+	}
+
+	public static inline function alloc (length:Int) {
+		return ofNativeString(untyped __call__("str_repeat", __call__("chr", 0), length));
+	}
+}