32 {
33 if (in == null)
34 throw new IOException("null stream");
35
36 byte[] content;
37 try
38 {
39 ByteArrayOutputStream buf = new ByteArrayOutputStream(4096);
40 byte[] chunk = new byte[4096];
41 int n;
42 while ((n = in.read(chunk)) != -1)
43 buf.write(chunk, 0, n);
44 content = buf.toByteArray();
45 }
46 finally
47 {
48 in.close();
49 }
50
51 String text;
52 int b0 = content.length > 0 ? (content[0] & 0xFF) : -1;
53 int b1 = content.length > 1 ? (content[1] & 0xFF) : -1;
54 if (b0 == 0xFF && b1 == 0xFE)
55 text = new String(content, 2, content.length - 2, StandardCharsets.UTF_16LE);
56 else if (b0 == 0xFE && b1 == 0xFF)
57 text = new String(content, 2, content.length - 2, StandardCharsets.UTF_16BE);
58 else
59 {
60 text = new String(content, StandardCharsets.UTF_8);
61 if (text.startsWith("\uFEFF"))
62 text = text.substring(1);
63 }
64
65 RDPFileParser p = new RDPFileParser();
66 p.parse(new StringReader(text));
67
68 String s = p.getString("full address");
69 if (s != null)
70 {
71 int colonIdx = s.lastIndexOf(":");
72 if (colonIdx > s.lastIndexOf("]"))
73 {
74 try
75 {
76 bookmark.setPort(Integer.parseInt(s.substring(colonIdx + 1)));
77 }
78 catch (NumberFormatException e)
79 {
80 Log.e(TAG, "Malformed address");
81 }
82 s = s.substring(0, colonIdx);
83 }
84 if (s.startsWith("[") && s.endsWith("]"))
85 s = s.substring(1, s.length() - 1);
86 bookmark.setHostname(s);
87 }
88
89 Integer i = p.getInteger("server port");
90 if (i != null)
91 bookmark.setPort(i);
92
93 s = p.getString("username");
94 if (s != null)
95 bookmark.setUsername(s);
96
97 s = p.getString("domain");
98 if (s != null)
99 bookmark.setDomain(s);
100
101 BookmarkBase.ScreenSettings screen = bookmark.getActiveScreenSettings();
102 Integer w = p.getInteger("desktopwidth");
103 Integer h = p.getInteger("desktopheight");
104 if (w != null && h != null && w > 0 && h > 0)
105 {
106 screen.setWidth(w);
107 screen.setHeight(h);
108 screen.setResolution(BookmarkBase.ScreenSettings.CUSTOM);
109 }
110 i = p.getInteger("session bpp");
111 if (i != null)
112 screen.setColors(i);
113
114 BookmarkBase.PerformanceFlags perf = bookmark.getPerformanceFlags();
115 i = p.getInteger("disable wallpaper");
116 if (i != null)
117 perf.setWallpaper(i == 0);
118 i = p.getInteger("allow font smoothing");
119 if (i != null)
120 perf.setFontSmoothing(i == 1);
121 i = p.getInteger("allow desktop composition");
122 if (i != null)
123 perf.setDesktopComposition(i == 1);
124 i = p.getInteger("disable menu anims");
125 if (i != null)
126 perf.setMenuAnimations(i == 0);
127 i = p.getInteger("disable themes");
128 if (i != null)
129 perf.setTheming(i == 0);
130 i = p.getInteger("disable full window drag");
131 if (i != null)
132 perf.setFullWindowDrag(i == 0);
133
134 BookmarkBase.AdvancedSettings advanced = bookmark.getAdvancedSettings();
135 i = p.getInteger("connect to console");
136 if (i != null)
137 advanced.setConsoleMode(i == 1);
138
139 i = p.getInteger("audiomode");
140 if (i != null && i >= 0 && i <= 2)
141 advanced.setRedirectSound(i);
142 i = p.getInteger("audiocapturemode");
143 if (i != null)
144 advanced.setRedirectMicrophone(i == 1);
145
146 s = p.getString("loadbalanceinfo");
147 if (s != null && !s.isEmpty())
148 advanced.setLoadBalanceInfo(s);
149
150 s = p.getString("remoteapplicationprogram");
151 if (s == null || s.isEmpty())
152 s = p.getString("alternate shell");
153 if (s != null && !s.isEmpty())
154 advanced.setRemoteProgram(s);
155
156 s = p.getString("shell working directory");
157 if (s != null && !s.isEmpty())
158 advanced.setWorkDir(s);
159
160 s = p.getString("gatewayhostname");
161 if (s != null && !s.isEmpty())
162 {
163 String gwHost = s;
164 int gwPort = 443;
165 int colonIdx = s.lastIndexOf(":");
166 if (colonIdx > 0)
167 {
168 try
169 {
170 gwPort = Integer.parseInt(s.substring(colonIdx + 1));
171 gwHost = s.substring(0, colonIdx);
172 }
173 catch (NumberFormatException e)
174 {
175 Log.e(TAG, "Malformed gateway port");
176 }
177 }
178 bookmark.getGatewaySettings().setHostname(gwHost);
179 bookmark.getGatewaySettings().setPort(gwPort);
180 bookmark.setEnableGatewaySettings(true);
181 }
182 i = p.getInteger("gatewayusagemethod");
183 if (i != null)
184 bookmark.setEnableGatewaySettings(i == 1 || i == 2);
185
186 i = p.getInteger("redirectprinters");
187 if (i != null)
188 advanced.setRedirectPrinter(i == 1);
189 i = p.getInteger("disableprinterredirection");
190 if (i != null)
191 advanced.setRedirectPrinter(i == 0);
192
193 s = p.getString("pcb");
194 if (s != null && !s.isEmpty())
195 {
196 advanced.setVmConnectMode(true);
197 advanced.setVmConnectGuid(s);
198 }
199 }