aboutsummaryrefslogtreecommitdiff
path: root/src/utils/WindowsShortcut.cpp
blob: 1ba63250fbcd231b6b70387e28dd44c354c171ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 *      Copyright (C) 2005-2013 Team XBMC
 *      http://xbmc.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

// WindowsShortcut.cpp: implementation of the CWindowsShortcut class.
//
//////////////////////////////////////////////////////////////////////

#include "sc.h"
#include "WindowsShortcut.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif


#define FLAG_SHELLITEMIDLIST   1
#define FLAG_FILEORDIRECTORY   2
#define FLAG_DESCRIPTION     4
#define FLAG_RELATIVEPATH     8
#define FLAG_WORKINGDIRECTORY   0x10
#define FLAG_ARGUMENTS          0x20
#define FLAG_ICON               0x40

#define VOLUME_LOCAL        1
#define VOLUME_NETWORK      2

using namespace std;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWindowsShortcut::CWindowsShortcut()
{
}

CWindowsShortcut::~CWindowsShortcut()
{
}

bool CWindowsShortcut::GetShortcut(const string& strFileName, string& strFileOrDir)
{
  strFileOrDir = "";
  if (!IsShortcut(strFileName) ) return false;


  CFile file;
  if (!file.Open(strFileName.c_str(), CFile::typeBinary | CFile::modeRead)) return false;
  byte byHeader[2048];
  int iBytesRead = file.Read(byHeader, 2048);
  file.Close();

  DWORD dwFlags = *((DWORD*)(&byHeader[0x14]));

  int iPos = 0x4c;
  if (dwFlags & FLAG_SHELLITEMIDLIST)
  {
    WORD sLength = *((WORD*)(&byHeader[iPos]));
    iPos += sLength;
    iPos += 2;
  }

  // skip File Location Info
  DWORD dwLen = 0x1c;
  if (dwFlags & FLAG_SHELLITEMIDLIST)
  {
    dwLen = *((DWORD*)(&byHeader[iPos]));
  }


  DWORD dwVolumeFlags = *((DWORD*)(&byHeader[iPos + 0x8]));
  DWORD dwOffsetLocalVolumeInfo = *((DWORD*)(&byHeader[iPos + 0xc]));
  DWORD dwOffsetBasePathName = *((DWORD*)(&byHeader[iPos + 0x10]));
  DWORD dwOffsetNetworkVolumeInfo = *((DWORD*)(&byHeader[iPos + 0x14]));
  DWORD dwOffsetRemainingPathName = *((DWORD*)(&byHeader[iPos + 0x18]));


  if ((dwVolumeFlags & VOLUME_NETWORK) == 0) return false;

  strFileOrDir = "smb:";
  // share name
  iPos += dwOffsetNetworkVolumeInfo + 0x14;
  while (iPos < iBytesRead && byHeader[iPos] != 0)
  {
    if (byHeader[iPos] == '\\') byHeader[iPos] = '/';
    strFileOrDir += (char)byHeader[iPos];
    iPos++;
  }
  iPos++;
  // file/folder name
  strFileOrDir += '/';
  while (iPos < iBytesRead && byHeader[iPos] != 0)
  {
    if (byHeader[iPos] == '\\') byHeader[iPos] = '/';
    strFileOrDir += (char)byHeader[iPos];
    iPos++;
  }
  return true;
}

bool CWindowsShortcut::IsShortcut(const string& strFileName)
{
  CFile file;
  if (!file.Open(strFileName.c_str(), CFile::typeBinary | CFile::modeRead)) return false;
  byte byHeader[0x80];
  int iBytesRead = file.Read(byHeader, 0x80);
  file.Close();
  if (iBytesRead < 0x4c)
  {
    return false;
  }
  //long integer that is always set to 4Ch
  if (byHeader[0] != 0x4c) return false;
  if (byHeader[1] != 0x0 ) return false;
  if (byHeader[2] != 0x0 ) return false;
  if (byHeader[3] != 0x0 ) return false;

  //globally unique identifier GUID of the shell links
  if (byHeader[0x04] != 0x01) return false;
  if (byHeader[0x05] != 0x14) return false;
  if (byHeader[0x06] != 0x02) return false;
  if (byHeader[0x07] != 0x00) return false;
  if (byHeader[0x08] != 0x00) return false;
  if (byHeader[0x09] != 0x00) return false;
  if (byHeader[0x0a] != 0x00) return false;
  if (byHeader[0x0b] != 0x00) return false;
  if (byHeader[0x0c] != 0xc0) return false;
  if (byHeader[0x0d] != 0x00) return false;
  if (byHeader[0x0e] != 0x00) return false;
  if (byHeader[0x0f] != 0x00) return false;
  if (byHeader[0x10] != 0x00) return false;
  if (byHeader[0x11] != 0x00) return false;
  if (byHeader[0x12] != 0x00) return false;
  if (byHeader[0x13] != 0x46) return false;

  // 2dwords, always 0
  if (byHeader[0x44] != 0x0 ) return false;
  if (byHeader[0x45] != 0x0 ) return false;
  if (byHeader[0x46] != 0x0 ) return false;
  if (byHeader[0x47] != 0x0 ) return false;
  if (byHeader[0x48] != 0x0 ) return false;
  if (byHeader[0x49] != 0x0 ) return false;
  if (byHeader[0x4a] != 0x0 ) return false;
  if (byHeader[0x4b] != 0x0 ) return false;

  return true;
}