Browse Source

base64-encode resource names when writing to file system (see #3760)

Simon Krajewski 10 years ago
parent
commit
3060ca7fbb

+ 3 - 2
genas3.ml

@@ -330,6 +330,7 @@ let generate_resources infos =
 		let dir = (infos.com.file :: ["__res"]) in
 		create_dir [] dir;
 		let add_resource name data =
+			let name = Base64.str_encode name in
 			let ch = open_out_bin (String.concat "/" (dir @ [name])) in
 			output_string ch data;
 			close_out ch
@@ -344,9 +345,9 @@ let generate_resources infos =
 		Hashtbl.iter (fun name _ ->
 			let varname = ("v" ^ (string_of_int !k)) in
 			k := !k + 1;
-			print ctx "\t\t[Embed(source = \"__res/%s\", mimeType = \"application/octet-stream\")]\n" name;
+			print ctx "\t\t[Embed(source = \"__res/%s\", mimeType = \"application/octet-stream\")]\n" (Base64.str_encode name);
 			print ctx "\t\tpublic static var %s:Class;\n" varname;
-			inits := ("list[\"" ^name^ "\"] = " ^ varname ^ ";") :: !inits;
+			inits := ("list[\"" ^ Ast.s_escape name ^ "\"] = " ^ varname ^ ";") :: !inits;
 		) infos.com.resources;
 		spr ctx "\t\tstatic public function __init__():void {\n";
 		spr ctx "\t\t\tlist = new Dictionary();\n";

+ 2 - 1
gencs.ml

@@ -40,7 +40,7 @@ let rec is_cs_basic_type t =
 		| TInst( { cl_path = (["haxe"], "Int32") }, [] )
 		| TInst( { cl_path = (["haxe"], "Int64") }, [] )
 		| TAbstract ({ a_path = (["cs"], "Int64") },[])
-		| TAbstract ({ a_path = (["cs"], "UInt64") },[]) 
+		| TAbstract ({ a_path = (["cs"], "UInt64") },[])
 		| TAbstract ({ a_path = ([], "Int") },[])
 		| TAbstract ({ a_path = ([], "Float") },[])
 		| TAbstract ({ a_path = ([], "Bool") },[]) ->
@@ -3018,6 +3018,7 @@ let configure gen =
 				gen.gcon.file ^ "/src/Resources"
 		in
 		Hashtbl.iter (fun name v ->
+			let name = Base64.str_encode name 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 full_path = gen.gcon.file ^ "/src/" ^ name in
 		mkdir_from_path full_path;
 

+ 3 - 2
genpy.ml

@@ -2137,8 +2137,9 @@ module Generator = struct
 				end else
 					","
 				in
-				print ctx "%s'%s': open('%%s.%%s'%%(__file__,'%s'),'rb').read()" prefix k k;
-				Std.output_file (ctx.com.file ^ "." ^ k) v
+				let k_enc = Base64.str_encode k in
+				print ctx "%s\"%s\": open('%%s.%%s'%%(__file__,'%s'),'rb').read()" prefix (Ast.s_escape k) k_enc;
+				Std.output_file (ctx.com.file ^ "." ^ k_enc) v
 			) ctx.com.resources;
 			spr ctx "}"
 		end

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

@@ -45,6 +45,7 @@ package haxe;
 	}
 
 	public static function getString( name : String ) : String {
+		name = haxe.crypto.Base64.encode(haxe.io.Bytes.ofString(name));
 		var path = getPaths().get(name);
 		var str = cs.Lib.toNativeType(haxe.Resource).Assembly.GetManifestResourceStream(path);
 		if (str != null)
@@ -53,6 +54,7 @@ package haxe;
 	}
 
 	public static function getBytes( name : String ) : haxe.io.Bytes {
+		name = haxe.crypto.Base64.encode(haxe.io.Bytes.ofString(name));
 		var path = getPaths().get(name);
 		var str = cs.Lib.toNativeType(haxe.Resource).Assembly.GetManifestResourceStream(path);
 		if (str != null)

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

@@ -30,6 +30,7 @@ package haxe;
 	}
 
 	public static function getString( name : String ) : String {
+		name = haxe.crypto.Base64.encode(haxe.io.Bytes.ofString(name));
 		var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
 		if (stream == null)
 			return null;
@@ -38,6 +39,7 @@ package haxe;
 	}
 
 	public static function getBytes( name : String ) : haxe.io.Bytes {
+		name = haxe.crypto.Base64.encode(haxe.io.Bytes.ofString(name));
 		var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
 		if (stream == null)
 			return null;

+ 2 - 2
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
--resource res2.bin
+-resource res1.txt@res?!%[]))("'£1.txt
+-resource res2.bin@res?!%[]))("'£1.bin
 -dce full
 -D analyzer

+ 11 - 11
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] == "res1.txt" )
-			eq( names[1], "res2.bin" );
+		if( names[0] == "res?!%[]))(\"'£1.txt" )
+			eq( names[1], "res?!%[]))(\"'£1.bin" );
 		else {
-			eq( names[0], "res2.bin" );
-			eq( names[1], "res1.txt" );
+			eq( names[0], "res?!%[]))(\"'£1.bin" );
+			eq( names[1], "res?!%[]))(\"'£1.txt" );
 		}
-		eq( haxe.Resource.getString("res1.txt"), STR );
+		eq( haxe.Resource.getString("res?!%[]))(\"'£1.txt"), STR );
 		#if (neko || php)
 		// allow binary strings
-		eq( haxe.Resource.getBytes("res2.bin").sub(0,9).toString(), "MZ\x90\x00\x03\x00\x00\x00\x04" );
+		eq( haxe.Resource.getBytes("res?!%[]))(\"'£1.bin").sub(0,9).toString(), "MZ\x90\x00\x03\x00\x00\x00\x04" );
 		#else
 		// cut until first \0
-		eq( haxe.Resource.getString("res2.bin").substr(0,2), "MZ" );
+		eq( haxe.Resource.getString("res?!%[]))(\"'£1.bin").substr(0,2), "MZ" );
 		#end
-		eq( haxe.Resource.getBytes("res1.txt").compare(haxe.io.Bytes.ofString(STR)), 0 );
-		var b = haxe.Resource.getBytes("res2.bin");
+		eq( haxe.Resource.getBytes("res?!%[]))(\"'£1.txt").compare(haxe.io.Bytes.ofString(STR)), 0 );
+		var b = haxe.Resource.getBytes("res?!%[]))(\"'£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("res1.txt",true);
+		var ch = sys.io.File.write("res?!%[]))(\"'£1.txt",true);
 		ch.writeString(STR);
 		ch.close();
-		var ch = sys.io.File.write("res2.bin",true);
+		var ch = sys.io.File.write("res?!%[]))(\"'£1.bin",true);
 		ch.writeString("Héllo");
 		ch.writeByte(0);
 		ch.writeString("World");