summaryrefslogtreecommitdiffstats
path: root/kfile-plugins/lnk/read_lnk.cpp
blob: c3825e05fc3cde82cd4f64d7a479e181798f7e76 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/***************************************************************************
 *   Copyright (C) 2004 by Martin Koller                                   *
 *   [email protected]                                                    *
 *                                                                         *
 *   This function reads the content of a M$-Windoze .lnk file             *
 *   and returns data in the given structure.                              *
 *                                                                         *
 *   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 of the License, 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 this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/

#include "read_lnk.h"
#include <stdio.h>
#include <kdebug.h>
#include <kio/netaccess.h>

//--------------------------------------------------------------------------------

//TODO: little/big endian problem ?
struct LNKHeader
{
   char magic[4];
   char GUID[16];
   TQ_UINT32 flags;
   TQ_UINT32 attributes;
   char time1[8];
   char time2[8];
   char time3[8];
   TQ_UINT32 length;
   TQ_UINT32 iconNum;
   TQ_UINT32 showWnd;
   TQ_UINT32 hotKey;
   char filler[8];
};

struct LNKFileLocation
{
  TQ_UINT32 totalLen;
  TQ_UINT32 ptr;
  TQ_UINT32 flags;
  TQ_UINT32 localVolume;
  TQ_UINT32 basePath;
  TQ_UINT32 netVolume;
  TQ_UINT32 pathname;
};

//--------------------------------------------------------------------------------

bool readLNK(const KURL &url, LNKInfo &info)
{
  const char* lnkFile = 0;

  TQString tempFile;
  if ( KIO::NetAccess::download(url, tempFile, 0) )
    lnkFile = tempFile.latin1();
  else
    return false;

  kdDebug(7034) << "opening:" << lnkFile << endl;
  FILE *fd = fopen(lnkFile, "rb");
  if ( !fd )
  {
    kdWarning(7034) << "could not open file " << lnkFile << endl;
    KIO::NetAccess::removeTempFile(tempFile);
    return false;
  }

  LNKHeader header;

  if ( fread(&header, sizeof(header), 1, fd) != 1 )
  {
    kdWarning(7034) << "wrong header size" << endl;
    fclose(fd);
    KIO::NetAccess::removeTempFile(tempFile);
    return false;
  }

  if ( memcmp(header.magic, "L\0\0\0", 4) != 0 )
  {
    kdWarning(7034) << "wrong magic in header" << endl;
    fclose(fd);
    KIO::NetAccess::removeTempFile(tempFile);
    return false;
  }

  if ( header.flags & 0x1 )  // the shell item id list is present
  {
    TQ_UINT16 len;

    // skip that list
    if ( (fread(&len, sizeof(len), 1, fd) != 1) || (fseek(fd, len, SEEK_CUR) != 0) )
    {
      kdWarning(7034) << "could not read shell item id list" << endl;
      fclose(fd);
      KIO::NetAccess::removeTempFile(tempFile);
      return false;
    }
  }

  info.isDirectory = (header.attributes & 0x10);

  if ( ! info.isDirectory )  // not a directory
    info.fileSize = header.length;

  info.isFileOrDir = (header.flags & 0x2);  // points to file or directory

  if ( info.isFileOrDir )
  {
    LNKFileLocation loc;

    if ( fread(&loc, sizeof(loc), 1, fd) != 1 )
    {
      kdWarning(7034) << "could not read file localtion table" << endl;
      fclose(fd);
      KIO::NetAccess::removeTempFile(tempFile);
      return false;
    }

    // limit the following "new", because the size to allocate is in the file
    // which can easily be manipulted to contain a huge number and lead to a crash
    if ( (loc.totalLen <= sizeof(loc)) || (loc.totalLen > 4096) )  // 4096 is just an arbitrary number I think shall be enough
    {
      fclose(fd);
      KIO::NetAccess::removeTempFile(tempFile);
      return false;
    }

    size_t size = loc.totalLen - sizeof(loc);
    char *data = new char[size];
    char *start = data - sizeof(loc);

    if ( fread(data, size, 1, fd) != 1 )
    {
      kdWarning(7034) << "could not read pathes data" << endl;
      delete [] data;
      fclose(fd);
      KIO::NetAccess::removeTempFile(tempFile);
      return false;
    }

    info.isNetworkPath = !(loc.flags & 0x1);

    if ( !info.isNetworkPath )
    {
      info.volumeName = (start + loc.localVolume + 0x10);  // volume label

      info.path = TQString();

      if ( *(start + loc.basePath) )
      {
        // Don't put any more than "X:" into info.driveName.
        info.driveName = *(start + loc.basePath);
        info.driveName += ':';

        // If we in fact do have more than just "X:", store any additional
        // path information separately in info.path.
        if ( *(start + loc.basePath + 1) == ':' &&
             *(start + loc.basePath + 2) != 0)
          info.path = (start + loc.basePath + 2);
      }

      if ( *(start + loc.pathname) != 0 )
      {
        if ( info.path.isNull() )
          info.path = (start + loc.pathname);
        else
          info.path = info.path + "\\" + (start + loc.pathname);
      }
    }
    else  // network path
    {
      info.path = TQString("%1\\%2")
                          .arg(start + loc.netVolume + 0x14)  // network share name
                          .arg(start + loc.pathname);
    }

    delete [] data;
    data = 0;

    if ( header.flags & 0x4 )   // has description string
    {
      TQ_UINT16 len;

      if ( fread(&len, sizeof(len), 1, fd) != 1 )
      {
        kdWarning(7034) << "could not read description string length" << endl;
        fclose(fd);
        KIO::NetAccess::removeTempFile(tempFile);
        return false;
      }

      data = new char[len+1];  // this can never be > 65K, so its OK to not check the size

      if ( fread(data, len, 1, fd) != 1 )
      {
        kdWarning(7034) << "could not read description string" << endl;
        delete [] data;
        fclose(fd);
        KIO::NetAccess::removeTempFile(tempFile);
        return false;
      }

      data[len] = 0;  // nullbyte seems to miss

      info.description = data;

      delete [] data;
    }
  }

  fclose(fd);
  KIO::NetAccess::removeTempFile(tempFile);

  return true;
}