Explorar el Código

[java/cs] Only escape special characters. See #3760

Cauê Waneck hace 10 años
padre
commit
bf0de81c9f

+ 9 - 0
codegen.ml

@@ -121,6 +121,15 @@ let is_removable_field ctx f =
 		| Method MethMacro -> not ctx.in_macro
 		| _ -> false)
 
+let escape_res_name name allow_dirs =
+	ExtString.String.replace_chars (fun chr ->
+		if (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9') || chr = '_' then
+			Char.escaped chr
+		else if chr = '/' && allow_dirs then
+			"/"
+		else
+			"-x" ^ (string_of_int (Char.code chr))) name
+
 (* -------------------------------------------------------------------------- *)
 (* REMOTING PROXYS *)
 

+ 1 - 1
gencs.ml

@@ -3018,7 +3018,7 @@ let configure gen =
 				gen.gcon.file ^ "/src/Resources"
 		in
 		Hashtbl.iter (fun name v ->
-			let name = Base64.str_encode name in
+			let name = Codegen.escape_res_name name true in
 			let full_path = src ^ "/" ^ name in
 			mkdir_from_path full_path;
 

+ 1 - 1
genjava.ml

@@ -2256,7 +2256,7 @@ let configure gen =
 	let res = ref [] in
 	Hashtbl.iter (fun name v ->
 		res := { eexpr = TConst(TString name); etype = gen.gcon.basic.tstring; epos = Ast.null_pos } :: !res;
-		let name = Base64.str_encode name in
+		let name = Codegen.escape_res_name name true in
 		let full_path = gen.gcon.file ^ "/src/" ^ name in
 		mkdir_from_path full_path;
 

+ 14 - 2
std/cs/_std/haxe/Resource.hx

@@ -44,8 +44,20 @@ package haxe;
 		return content.copy();
 	}
 
+	private static function unescapeName( name : String ) : String
+	{
+		var regex = ~/-x([0-9]+)/g;
+		return regex.map(name, function(regex) return String.fromCharCode(Std.parseInt(regex.matched(1))));
+	}
+
+	private static function escapeName( name : String ) : String
+	{
+		var regex = ~/[^A-Za-z0-9_\/]/g;
+		return regex.map(name, function(v) return '-x' + v.matched(0).charCodeAt(0));
+	}
+
 	public static function getString( name : String ) : String {
-		name = haxe.crypto.Base64.encode(haxe.io.Bytes.ofString(name));
+		name = escapeName(name);
 		var path = getPaths().get(name);
 		var str = cs.Lib.toNativeType(haxe.Resource).Assembly.GetManifestResourceStream(path);
 		if (str != null)
@@ -54,7 +66,7 @@ package haxe;
 	}
 
 	public static function getBytes( name : String ) : haxe.io.Bytes {
-		name = haxe.crypto.Base64.encode(haxe.io.Bytes.ofString(name));
+		name = escapeName(name);
 		var path = getPaths().get(name);
 		var str = cs.Lib.toNativeType(haxe.Resource).Assembly.GetManifestResourceStream(path);
 		if (str != null)

+ 14 - 2
std/java/_std/haxe/Resource.hx

@@ -29,8 +29,20 @@ package haxe;
 		return content.copy();
 	}
 
+	private static function unescapeName( name : String ) : String
+	{
+		var regex = ~/-x([0-9]+)/g;
+		return regex.map(name, function(regex) return String.fromCharCode(Std.parseInt(regex.matched(1))));
+	}
+
+	private static function escapeName( name : String ) : String
+	{
+		var regex = ~/[^A-Za-z0-9_\/]/g;
+		return regex.map(name, function(v) return '-x' + v.matched(0).charCodeAt(0));
+	}
+
 	public static function getString( name : String ) : String {
-		name = haxe.crypto.Base64.encode(haxe.io.Bytes.ofString(name));
+		name = escapeName(name);
 		var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
 		if (stream == null)
 			return null;
@@ -39,7 +51,7 @@ package haxe;
 	}
 
 	public static function getBytes( name : String ) : haxe.io.Bytes {
-		name = haxe.crypto.Base64.encode(haxe.io.Bytes.ofString(name));
+		name = escapeName(name);
 		var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
 		if (stream == null)
 			return null;

+ 3 - 3
tests/unit/compile-each.hxml

@@ -2,7 +2,7 @@
 -debug
 -cp src
 -cp "C:\Program Files\The Haxe Effect\src/dev/null"
--resource res1.txt@res?!%[]))("'1.txt
--resource res2.bin@res?!%[]))("'1.bin
+-resource res1.txt@re/s?!%[]))("'1.txt
+-resource res2.bin@re/s?!%[]))("'1.bin
 -dce full
--D analyzer
+-D analyzer

+ 12 - 12
tests/unit/src/unit/TestResource.hx

@@ -7,22 +7,22 @@ class TestResource extends Test {
 	function testResources() {
 		var names = haxe.Resource.listNames();
 		eq( names.length, 2 );
-		if( names[0] == "res?!%[]))(\"'1.txt" )
-			eq( names[1], "res?!%[]))(\"'1.bin" );
+		if( names[0] == "re/s?!%[]))(\"'1.txt" )
+			eq( names[1], "re/s?!%[]))(\"'1.bin" );
 		else {
-			eq( names[0], "res?!%[]))(\"'1.bin" );
-			eq( names[1], "res?!%[]))(\"'1.txt" );
+			eq( names[0], "re/s?!%[]))(\"'1.bin" );
+			eq( names[1], "re/s?!%[]))(\"'1.txt" );
 		}
-		eq( haxe.Resource.getString("res?!%[]))(\"'1.txt"), STR );
+		eq( haxe.Resource.getString("re/s?!%[]))(\"'1.txt"), STR );
 		#if (neko || php)
 		// allow binary strings
-		eq( haxe.Resource.getBytes("res?!%[]))(\"'1.bin").sub(0,9).toString(), "MZ\x90\x00\x03\x00\x00\x00\x04" );
+		eq( haxe.Resource.getBytes("re/s?!%[]))(\"'1.bin").sub(0,9).toString(), "MZ\x90\x00\x03\x00\x00\x00\x04" );
 		#else
 		// cut until first \0
-		eq( haxe.Resource.getString("res?!%[]))(\"'1.bin").substr(0,2), "MZ" );
+		eq( haxe.Resource.getString("re/s?!%[]))(\"'1.bin").substr(0,2), "MZ" );
 		#end
-		eq( haxe.Resource.getBytes("res?!%[]))(\"'1.txt").compare(haxe.io.Bytes.ofString(STR)), 0 );
-		var b = haxe.Resource.getBytes("res?!%[]))(\"'1.bin");
+		eq( haxe.Resource.getBytes("re/s?!%[]))(\"'1.txt").compare(haxe.io.Bytes.ofString(STR)), 0 );
+		var b = haxe.Resource.getBytes("re/s?!%[]))(\"'1.bin");
 		var firsts = [0x4D,0x5A,0x90,0x00,0x03,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0xB8];
 		var lasts = [0xD6,0x52,0x03,0x1A,0x2C,0x4E,0x45,0x4B,0x4F,0x00,0x1C,0x00,0x00];
 		for( i in 0...firsts.length )
@@ -33,10 +33,10 @@ class TestResource extends Test {
 
 	#if neko
 	static function main() {
-		var ch = sys.io.File.write("res?!%[]))(\"'1.txt",true);
+		var ch = sys.io.File.write("re/s?!%[]))(\"'1.txt",true);
 		ch.writeString(STR);
 		ch.close();
-		var ch = sys.io.File.write("res?!%[]))(\"'1.bin",true);
+		var ch = sys.io.File.write("re/s?!%[]))(\"'1.bin",true);
 		ch.writeString("Héllo");
 		ch.writeByte(0);
 		ch.writeString("World");
@@ -46,4 +46,4 @@ class TestResource extends Test {
 	}
 	#end
 
-}
+}