From 3060ca7fbb7a2b50b91cbf7e6a14e3798740f313 Mon Sep 17 00:00:00 2001 From: Simon Krajewski Date: Thu, 26 Feb 2015 09:40:49 +0100 Subject: [PATCH] base64-encode resource names when writing to file system (see #3760) --- genas3.ml | 5 +++-- gencs.ml | 3 ++- genjava.ml | 2 +- genpy.ml | 5 +++-- std/cs/_std/haxe/Resource.hx | 2 ++ std/java/_std/haxe/Resource.hx | 2 ++ tests/unit/compile-each.hxml | 4 ++-- tests/unit/src/unit/TestResource.hx | 22 +++++++++++----------- 8 files changed, 26 insertions(+), 19 deletions(-) diff --git a/genas3.ml b/genas3.ml index b8d09fa134a..5f999bd540c 100644 --- a/genas3.ml +++ b/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"; diff --git a/gencs.ml b/gencs.ml index ff7894b7a27..8c2455d908f 100644 --- a/gencs.ml +++ b/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; diff --git a/genjava.ml b/genjava.ml index 44136c33be3..14794d3891f 100644 --- a/genjava.ml +++ b/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; diff --git a/genpy.ml b/genpy.ml index 56653abef5b..0a9ba575ceb 100644 --- a/genpy.ml +++ b/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 diff --git a/std/cs/_std/haxe/Resource.hx b/std/cs/_std/haxe/Resource.hx index 421879df77c..d6383618144 100644 --- a/std/cs/_std/haxe/Resource.hx +++ b/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) diff --git a/std/java/_std/haxe/Resource.hx b/std/java/_std/haxe/Resource.hx index 084127173d4..dff4a13e342 100644 --- a/std/java/_std/haxe/Resource.hx +++ b/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).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).getResourceAsStream("/" + name); if (stream == null) return null; diff --git a/tests/unit/compile-each.hxml b/tests/unit/compile-each.hxml index 7c4c4bbf9da..756f78c15a4 100644 --- a/tests/unit/compile-each.hxml +++ b/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 \ No newline at end of file diff --git a/tests/unit/src/unit/TestResource.hx b/tests/unit/src/unit/TestResource.hx index 326b0adc261..3e2df4d768b 100644 --- a/tests/unit/src/unit/TestResource.hx +++ b/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");