forked from bruderstein/nppPluginManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectLinkSearch.cpp
More file actions
190 lines (154 loc) · 4.78 KB
/
DirectLinkSearch.cpp
File metadata and controls
190 lines (154 loc) · 4.78 KB
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
/*
This file is part of Plugin Manager Plugin for Notepad++
Copyright (C)2009-2010 Dave Brotherstone <davegb@pobox.com>
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 "precompiled_headers.h"
#include "libinstall/DirectLinkSearch.h"
// Copied over from winnt.h, in order to avoid dependency to windows just for this.
#ifndef TEXT
#ifdef UNICODE
#define TEXT(quote) L##quote
#else
#define TEXT(quote) quote
#endif
#endif
using namespace std;
DirectLinkSearch::DirectLinkSearch(const TCHAR *filename)
{
_file.open(filename);
}
DirectLinkSearch::~DirectLinkSearch()
{
}
std::shared_ptr<TCHAR> DirectLinkSearch::search(const TCHAR *filename)
{
if (!filename || !(*filename))
{
std::shared_ptr<TCHAR> empty;
return empty;
}
size_t patternLength = _tcslen(filename);
size_t shiftTable[256];
for (int position = 0; position < 256; position++)
{
shiftTable[position] = patternLength;
}
for (size_t position = 0; position <= patternLength; position++)
{
shiftTable[filename[position]] = patternLength - position - 1;
}
size_t checkOffset = 0;
size_t shiftPosition;
size_t currentPosition = patternLength - 1;
TCHAR currentChar;
do
{
currentChar = _file.getCharAt(currentPosition - checkOffset);
shiftPosition = shiftTable[currentChar];
if (currentChar == filename[patternLength - checkOffset - 1])
{
checkOffset++;
if (checkOffset == patternLength)
{
// Found filename
size_t realPosition = validateDirectLink(currentPosition-checkOffset + 1);
if (realPosition != LINK_NOT_VALID)
{
std::shared_ptr<TCHAR> buffer(new TCHAR[currentPosition - realPosition + 2]);
int bufferPosition = 0;
while(realPosition <= currentPosition)
{
buffer.get()[bufferPosition++] = _file.getCharAt(realPosition++);
}
buffer.get()[bufferPosition] = '\0';
return buffer;
}
else
{
// Reset the check offset
checkOffset = 0;
// Advance to next posible position
currentPosition += patternLength;
}
}
}
else
{
currentPosition += shiftPosition - checkOffset;
checkOffset = 0;
}
} while (_file.getCharAt(currentPosition) != FILEBUFFER_EOF);
std::shared_ptr<TCHAR> empty;
return empty;
}
size_t DirectLinkSearch::validateDirectLink(size_t currentPosition)
{
TCHAR searchString[] = TEXT("href=\"http://");
TCHAR searchString2[] = TEXT("href=\"https://");
TCHAR validUriChars[] = TEXT(":@&=+$,;/?:");
size_t charListLen = _tcslen(validUriChars);
const int LINKOFFSET = 6;
size_t lastNonDomain = currentPosition;
size_t minPosition = currentPosition - MAX_RESULT_SIZE;
size_t startSearchPos = _tcslen(searchString) - 1;
size_t startSearchPos2 = _tcslen(searchString2) - 1;
size_t searchPos = startSearchPos;
size_t searchPos2 = startSearchPos2;
while(currentPosition >= minPosition)
{
TCHAR currentChar = _file.getCharAt(currentPosition);
if (currentChar != TEXT('.')
&& currentChar != TEXT('-')
&& !(currentChar >= TEXT('a') && currentChar <= TEXT('z'))
&& !(currentChar >= TEXT('A') && currentChar <= TEXT('Z'))
&& !(currentChar >= TEXT('0') && currentChar <= TEXT('9')))
{
lastNonDomain = currentPosition;
if (!findChar(currentChar, validUriChars, charListLen)
&& !(currentChar == TEXT('\"') && (searchString[searchPos] == TEXT('\"') || searchString2[searchPos2] == TEXT('\"'))))
break;
}
if (searchString[searchPos] == currentChar)
{
if (searchPos == 0)
{
return currentPosition + LINKOFFSET;
}
--searchPos;
}
else
searchPos = startSearchPos;
if (searchString2[searchPos2] == currentChar)
{
if (searchPos2 == 0)
{
return currentPosition + LINKOFFSET;
}
--searchPos2;
}
else
searchPos2 = startSearchPos2;
--currentPosition;
} /* while */
return LINK_NOT_VALID;
}
bool DirectLinkSearch::findChar(TCHAR ch, const TCHAR *charList, size_t charListLen)
{
for (size_t searchIndex = 0; searchIndex < charListLen; searchIndex++)
{
if (ch == charList[searchIndex])
return true;
}
return false;
}