source: josm/trunk/src/org/openstreetmap/josm/tools/WindowsShortcut.java@ 13695

Last change on this file since 13695 was 13692, checked in by Don-vip, 6 years ago

see #16243 - do not rely on internal JDK classes, not available on Linux

  • Property svn:eol-style set to native
File size: 8.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.io.ByteArrayOutputStream;
5import java.io.File;
6import java.io.FileInputStream;
7import java.io.IOException;
8import java.io.InputStream;
9import java.text.ParseException;
10
11/**
12 * Represents a Windows shortcut (typically visible to Java only as a '.lnk' file).
13 *
14 * Retrieved 2011-09-23 from http://stackoverflow.com/questions/309495/windows-shortcut-lnk-parser-in-java/672775#672775
15 *
16 * Written by: (the stack overflow users, obviously!)
17 * Apache Commons VFS dependency removed by crysxd (why were we using that!?) https://github.com/crysxd
18 * Headerified, refactored and commented by Code Bling http://stackoverflow.com/users/675721/code-bling
19 * Network file support added by Stefan Cordes http://stackoverflow.com/users/81330/stefan-cordes
20 * Adapted by Sam Brightman http://stackoverflow.com/users/2492/sam-brightman
21 * Based on information in 'The Windows Shortcut File Format' by Jesse Hager <jessehager@iname.com>
22 * And somewhat based on code from the book 'Swing Hacks: Tips and Tools for Killer GUIs'
23 * by Joshua Marinacci and Chris Adamson
24 * ISBN: 0-596-00907-0
25 * http://www.oreilly.com/catalog/swinghks/
26 * @since 13692
27 */
28public class WindowsShortcut {
29 private boolean isDirectory;
30 private boolean isLocal;
31 private String realFile;
32
33 /**
34 * Provides a quick test to see if this could be a valid link !
35 * If you try to instantiate a new WindowShortcut and the link is not valid,
36 * Exceptions may be thrown and Exceptions are extremely slow to generate,
37 * therefore any code needing to loop through several files should first check this.
38 *
39 * @param file the potential link
40 * @return true if may be a link, false otherwise
41 * @throws IOException if an IOException is thrown while reading from the file
42 */
43 public static boolean isPotentialValidLink(File file) throws IOException {
44 final int minimum_length = 0x64;
45 boolean isPotentiallyValid = false;
46 try (InputStream fis = new FileInputStream(file)) {
47 isPotentiallyValid = file.isFile()
48 && file.getName().toLowerCase().endsWith(".lnk")
49 && fis.available() >= minimum_length
50 && isMagicPresent(getBytes(fis, 32));
51 }
52 return isPotentiallyValid;
53 }
54
55 /**
56 * Constructs a new {@code WindowsShortcut}
57 * @param file file
58 * @throws IOException if an I/O error occurs
59 * @throws ParseException if a parsing error occurs
60 */
61 public WindowsShortcut(File file) throws IOException, ParseException {
62 try (InputStream in = new FileInputStream(file)) {
63 parseLink(getBytes(in));
64 }
65 }
66
67 /**
68 * @return the name of the filesystem object pointed to by this shortcut
69 */
70 public String getRealFilename() {
71 return realFile;
72 }
73
74 /**
75 * Tests if the shortcut points to a local resource.
76 * @return true if the 'local' bit is set in this shortcut, false otherwise
77 */
78 public boolean isLocal() {
79 return isLocal;
80 }
81
82 /**
83 * Tests if the shortcut points to a directory.
84 * @return true if the 'directory' bit is set in this shortcut, false otherwise
85 */
86 public boolean isDirectory() {
87 return isDirectory;
88 }
89
90 /**
91 * Gets all the bytes from an InputStream
92 * @param in the InputStream from which to read bytes
93 * @return array of all the bytes contained in 'in'
94 * @throws IOException if an IOException is encountered while reading the data from the InputStream
95 */
96 private static byte[] getBytes(InputStream in) throws IOException {
97 return getBytes(in, null);
98 }
99
100 /**
101 * Gets up to max bytes from an InputStream
102 * @param in the InputStream from which to read bytes
103 * @param max maximum number of bytes to read
104 * @return array of all the bytes contained in 'in'
105 * @throws IOException if an IOException is encountered while reading the data from the InputStream
106 */
107 private static byte[] getBytes(InputStream in, Integer max) throws IOException {
108 // read the entire file into a byte buffer
109 ByteArrayOutputStream bout = new ByteArrayOutputStream();
110 byte[] buff = new byte[256];
111 while (max == null || max > 0) {
112 int n = in.read(buff);
113 if (n == -1) {
114 break;
115 }
116 bout.write(buff, 0, n);
117 if (max != null)
118 max -= n;
119 }
120 in.close();
121 return bout.toByteArray();
122 }
123
124 private static boolean isMagicPresent(byte[] link) {
125 final int magic = 0x0000004C;
126 final int magic_offset = 0x00;
127 return link.length >= 32 && bytesToDword(link, magic_offset) == magic;
128 }
129
130 /**
131 * Gobbles up link data by parsing it and storing info in member fields
132 * @param link all the bytes from the .lnk file
133 * @throws ParseException if a parsing error occurs
134 */
135 private void parseLink(byte[] link) throws ParseException {
136 try {
137 if (!isMagicPresent(link))
138 throw new ParseException("Invalid shortcut; magic is missing", 0);
139
140 // get the flags byte
141 byte flags = link[0x14];
142
143 // get the file attributes byte
144 final int file_atts_offset = 0x18;
145 byte file_atts = link[file_atts_offset];
146 byte is_dir_mask = (byte) 0x10;
147 if ((file_atts & is_dir_mask) > 0) {
148 isDirectory = true;
149 } else {
150 isDirectory = false;
151 }
152
153 // if the shell settings are present, skip them
154 final int shell_offset = 0x4c;
155 final byte has_shell_mask = (byte) 0x01;
156 int shell_len = 0;
157 if ((flags & has_shell_mask) > 0) {
158 // the plus 2 accounts for the length marker itself
159 shell_len = bytesToWord(link, shell_offset) + 2;
160 }
161
162 // get to the file settings
163 int file_start = 0x4c + shell_len;
164
165 final int file_location_info_flag_offset_offset = 0x08;
166 int file_location_info_flag = link[file_start + file_location_info_flag_offset_offset];
167 isLocal = (file_location_info_flag & 2) == 0;
168 // get the local volume and local system values
169 //final int localVolumeTable_offset_offset = 0x0C;
170 final int basename_offset_offset = 0x10;
171 final int networkVolumeTable_offset_offset = 0x14;
172 final int finalname_offset_offset = 0x18;
173 int finalname_offset = link[file_start + finalname_offset_offset] + file_start;
174 String finalname = getNullDelimitedString(link, finalname_offset);
175 if (isLocal) {
176 int basename_offset = link[file_start + basename_offset_offset] + file_start;
177 String basename = getNullDelimitedString(link, basename_offset);
178 realFile = basename + finalname;
179 } else {
180 int networkVolumeTable_offset = link[file_start + networkVolumeTable_offset_offset] + file_start;
181 int shareName_offset_offset = 0x08;
182 int shareName_offset = link[networkVolumeTable_offset + shareName_offset_offset]
183 + networkVolumeTable_offset;
184 String shareName = getNullDelimitedString(link, shareName_offset);
185 realFile = shareName + "\\" + finalname;
186 }
187 } catch (ArrayIndexOutOfBoundsException e) {
188 ParseException ex = new ParseException("Could not be parsed, probably not a valid WindowsShortcut", 0);
189 ex.initCause(e);
190 throw ex;
191 }
192 }
193
194 private static String getNullDelimitedString(byte[] bytes, int off) {
195 int len = 0;
196 // count bytes until the null character (0)
197 while (true) {
198 if (bytes[off + len] == 0) {
199 break;
200 }
201 len++;
202 }
203 return new String(bytes, off, len);
204 }
205
206 /*
207 * convert two bytes into a short note, this is little endian because it's for an Intel only OS.
208 */
209 private static int bytesToWord(byte[] bytes, int off) {
210 return ((bytes[off + 1] & 0xff) << 8) | (bytes[off] & 0xff);
211 }
212
213 private static int bytesToDword(byte[] bytes, int off) {
214 return (bytesToWord(bytes, off + 2) << 16) | bytesToWord(bytes, off);
215 }
216}
Note: See TracBrowser for help on using the repository browser.