FreeRDP
Loading...
Searching...
No Matches
RDPFileHelper.java
1/*
2 RDP file import/export helper
3
4 Copyright 2026 Ibrahim Sevinc <ibrahim.sevinc.mail@gmail.com>
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9*/
10
11package com.freerdp.freerdpcore.utils;
12
13import android.util.Log;
14
15import com.freerdp.freerdpcore.domain.BookmarkBase;
16
17import java.io.ByteArrayOutputStream;
18import java.io.IOException;
19import java.io.InputStream;
20import java.io.StringReader;
21import java.nio.charset.StandardCharsets;
22
23public final class RDPFileHelper
24{
25 private static final String TAG = "RDPFileHelper";
26
27 private RDPFileHelper()
28 {
29 }
30
31 public static void importFrom(InputStream in, BookmarkBase bookmark) throws IOException
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
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 }
200
201 public static String toRdpString(BookmarkBase bookmark)
202 {
203 StringBuilder sb = new StringBuilder();
204 BookmarkBase.ScreenSettings screen = bookmark.getActiveScreenSettings();
205 BookmarkBase.PerformanceFlags perf = bookmark.getActivePerformanceFlags();
206 BookmarkBase.AdvancedSettings adv = bookmark.getAdvancedSettings();
207
208 String host = bookmark.getHostname();
209 int port = bookmark.getPort();
210 writeString(sb, "full address", port != 3389 ? host + ":" + port : host);
211
212 String username = bookmark.getUsername();
213 if (!username.isEmpty())
214 writeString(sb, "username", username);
215
216 String domain = bookmark.getDomain();
217 if (!domain.isEmpty())
218 writeString(sb, "domain", domain);
219
220 if (screen.getResolution() == BookmarkBase.ScreenSettings.CUSTOM)
221 {
222 writeInt(sb, "desktopwidth", screen.getWidth());
223 writeInt(sb, "desktopheight", screen.getHeight());
224 }
225 writeInt(sb, "session bpp", screen.getColors());
226
227 writeInt(sb, "disable wallpaper", perf.getWallpaper() ? 0 : 1);
228 writeInt(sb, "allow font smoothing", perf.getFontSmoothing() ? 1 : 0);
229 writeInt(sb, "allow desktop composition", perf.getDesktopComposition() ? 1 : 0);
230 writeInt(sb, "disable menu anims", perf.getMenuAnimations() ? 0 : 1);
231 writeInt(sb, "disable themes", perf.getTheming() ? 0 : 1);
232 writeInt(sb, "disable full window drag", perf.getFullWindowDrag() ? 0 : 1);
233
234 if (adv.getConsoleMode())
235 writeInt(sb, "connect to console", 1);
236
237 int audioMode = adv.getRedirectSound();
238 if (audioMode != 0)
239 writeInt(sb, "audiomode", audioMode);
240
241 if (adv.getRedirectMicrophone())
242 writeInt(sb, "audiocapturemode", 1);
243
244 String lbInfo = adv.getLoadBalanceInfo();
245 if (!lbInfo.isEmpty())
246 writeString(sb, "loadbalanceinfo", lbInfo);
247
248 String remoteApp = adv.getRemoteProgram();
249 if (!remoteApp.isEmpty())
250 writeString(sb, "remoteapplicationprogram", remoteApp);
251
252 String workDir = adv.getWorkDir();
253 if (!workDir.isEmpty())
254 writeString(sb, "shell working directory", workDir);
255
256 if (bookmark.getEnableGatewaySettings())
257 {
258 BookmarkBase.GatewaySettings gw = bookmark.getGatewaySettings();
259 String gwHost = gw.getHostname();
260 int gwPort = gw.getPort();
261 writeString(sb, "gatewayhostname", gwPort != 443 ? gwHost + ":" + gwPort : gwHost);
262 writeInt(sb, "gatewayusagemethod", 2);
263 }
264
265 if (adv.getVmConnectMode())
266 writeString(sb, "pcb", adv.getVmConnectGuid());
267
268 if (adv.getRedirectPrinter())
269 writeInt(sb, "redirectprinters", 1);
270
271 return sb.toString();
272 }
273
274 private static void writeString(StringBuilder sb, String key, String value)
275 {
276 sb.append(key).append(":s:").append(value).append("\r\n");
277 }
278
279 private static void writeInt(StringBuilder sb, String key, int value)
280 {
281 sb.append(key).append(":i:").append(value).append("\r\n");
282 }
283}