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