IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
Resources.cs
Go to the documentation of this file.
1using System;
2using System.IO;
3
5{
6
7 static class Resources
8 {
9
15 public static string ToPackageName(string name)
16 {
17 int index = name.LastIndexOf('/');
18 if (index == -1 || index == name.Length - 1)
19 {
20 return "";
21 }
22 else
23 {
24 return name.Substring(0, index).Replace('/', '.');
25 }
26 }
27
33 public static string ToResourceName(string dir, string file)
34 {
35 // full dir path, with ending separator
36 dir = Path.GetFullPath(dir);
37 if (dir.EndsWith(Path.DirectorySeparatorChar) == false)
38 dir += Path.DirectorySeparatorChar;
39
40 // normalize relative path off of dir
41 file = Path.Combine(dir, file);
42 if (file.StartsWith(dir) == false)
43 throw new InvalidOperationException("Resource file does not exist in directory.");
44
45 // cut directory off of file path
46 file = file[dir.Length..];
47 if (Path.DirectorySeparatorChar != '/')
48 file = file.Replace(Path.DirectorySeparatorChar, '/');
49
50 // ensure directory path ends with separator
51 if (file != "" && Directory.Exists(file))
52 file += "/";
53
54 return file;
55 }
56
62 public static string? ToFilePath(string dir, string name)
63 {
64 var expectDirectory = name.EndsWith('/');
65 if (expectDirectory)
66 name = name.TrimEnd('/');
67
68 var path = ToSafeFilePath(name);
69 if (path is not null)
70 {
71 var file = Path.Combine(dir, path);
72 if (Directory.Exists(file) || (Directory.Exists(file) == false && expectDirectory == false))
73 return file;
74 }
75
76 return null;
77 }
78
88 static string? ToSafeFilePath(string name)
89 {
90 // scan elements of resource name
91 int next;
92 int off = 0;
93 while ((next = name.IndexOf('/', off)) != -1)
94 {
95 int len = next - off;
96 if (MayTranslate(name, off, len) == false)
97 return null;
98
99 off = next + 1;
100 }
101
102 int rem = name.Length - off;
103 if (MayTranslate(name, off, rem) == false)
104 return null;
105
106 // map resource name to a file path string
107 string path;
108 if (Path.DirectorySeparatorChar == '/')
109 {
110 path = name;
111 }
112 else
113 {
114 // not allowed to embed file separators
115 if (name.Contains(Path.DirectorySeparatorChar))
116 return null;
117
118 // convert to native separator
119 path = name.Replace('/', Path.DirectorySeparatorChar);
120 }
121
122 // file path not allowed to have root component
123 return Path.IsPathRooted(path) == false ? path : null;
124 }
125
134 static bool MayTranslate(string name, int off, int len)
135 {
136 if (len <= 2)
137 {
138 if (len == 0)
139 return false;
140
141 var starsWithDot = (name[off] == '.');
142 if (len == 1 && starsWithDot)
143 return false;
144 if (len == 2 && starsWithDot && (name[off + 1] == '.'))
145 return false;
146 }
147
148 return true;
149 }
150
151 }
152
153}
static ? string ToFilePath(string dir, string name)
Returns a file path to a resource in a file tree. If the resource name has a trailing "/" then the fi...
Definition Resources.cs:62
static string ToResourceName(string dir, string file)
Returns a resource name corresponding to the relative file path between dir and file ....
Definition Resources.cs:33
static string ToPackageName(string name)
Derive a package name for a resource. The package name returned by this method may not be a legal pac...
Definition Resources.cs:15