1
+ #include < iostream>
2
+ #include < fstream>
3
+ #include < string>
4
+ #include < dirent.h>
5
+ #include < cstring>
6
+ #include < vector>
7
+ #include < getopt.h>
8
+ #include < sys/stat.h>
9
+ #include < csignal>
10
+ #include < filesystem>
11
+ #include " search.h"
12
+ #include " pattern.h"
13
+
14
+ using namespace std ;
15
+
16
+ void getFiles (const string &root, vector<string> &files);
17
+ int32_t createDirectory (const std::string &directoryPath);
18
+
19
+
20
+ #define ACCESS (fileName,accessMode ) access(fileName,accessMode)
21
+ #define MKDIR (path ) mkdir(path,S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
22
+
23
+ /* *
24
+ * Print logo
25
+ *
26
+ */
27
+ void PrintLogo (void )
28
+ {
29
+ cout<< R"(
30
+ ________ _ __ __ _____ __
31
+ / ____/ /__ _ __/ | / /__ / /_ / ___/___ ____ ___________/ /_
32
+ / /_ / / _ \| |/_/ |/ / _ \/ __/ \__ \/ _ \/ __ `/ ___/ ___/ __ \
33
+ / __/ / / __/> </ /| / __/ /_ ___/ / __/ /_/ / / / /__/ / / /
34
+ /_/ /_/\___/_/|_/_/ |_/\___/\__/ /____/\___/\__,_/_/ \___/_/ /_/
35
+ by WillyTerra
36
+ )" <<endl;
37
+ }
38
+
39
+ /* *
40
+ * Print help
41
+ *
42
+ */
43
+ void PrintUsage (void )
44
+ {
45
+ cout <<R"(
46
+ **********************************************************************************
47
+ * -s Search flexnet licensing file
48
+ * -c Copy flexnet licensing file to new folder
49
+ * -h Display this info
50
+ **********************************************************************************
51
+ )" <<endl;
52
+ }
53
+
54
+ /* *
55
+ * Main function
56
+ * @param argc
57
+ * @param argv
58
+ * @return
59
+ */
60
+ int main (int argc, char * argv[]) {
61
+
62
+ int opt;
63
+ bool flgCopy = false ;
64
+ bool flgSearch = false ;
65
+
66
+ uint16_t PatchSig = 0 ;
67
+
68
+ string pCopyDirectory = " " ;
69
+
70
+ PrintLogo ();
71
+ SearchPatternInit ();
72
+
73
+ while ((opt = getopt (argc, argv, " sc:h" )) != -1 ) {
74
+ switch (opt) {
75
+ case ' c' : // Search Copy
76
+ flgCopy = true ;
77
+ pCopyDirectory = optarg;
78
+ break ;
79
+ case ' s' : // Search signature
80
+ flgSearch = true ;
81
+ break ;
82
+ case ' h' : // Help
83
+ PrintUsage ();
84
+ exit (EXIT_SUCCESS);
85
+ break ;
86
+ default : break ;
87
+ }
88
+ }
89
+
90
+ string root = " ." ; // Current Directory
91
+ vector<string> files;
92
+ getFiles (root, files);
93
+
94
+ for (auto file : files) {
95
+
96
+ ifstream binRead (file,ios::binary);
97
+ binRead.seekg (0 ,ios::end);
98
+
99
+ uint32_t dataFileSize = binRead.tellg ();
100
+ uint8_t *dataBuffer = new uint8_t [dataFileSize + 10 ];
101
+
102
+ // Read ELF file
103
+ binRead.seekg (0 ,ios::beg);
104
+ binRead.read ((char *)dataBuffer,dataFileSize);
105
+ binRead.close ();
106
+
107
+ int resData = 0 ;
108
+
109
+ if (flgSearch)
110
+ {
111
+ // Find ECC Pattern
112
+ resData = PatchPatternSearchInfo (dataBuffer,dataFileSize,fnSigPattern);
113
+
114
+ if (resData != -1 )
115
+ {
116
+ PatchSig++;
117
+ cout<<file<<endl;
118
+ while (dataBuffer[resData + 5 ] != 0x00 )
119
+ {
120
+ cout<<(char )dataBuffer[resData+5 ];
121
+ resData++;
122
+ }
123
+ cout<<endl;
124
+
125
+ if (flgCopy) // Copy file to another
126
+ {
127
+ vector <string> fields;
128
+
129
+ string origFolder = get_current_dir_name () + file.erase (0 ,1 );
130
+ string copyFolder = pCopyDirectory + file.erase (0 ,1 );
131
+
132
+ createDirectory (copyFolder);
133
+
134
+ std::ifstream src (origFolder, std::ios::binary);
135
+ std::ofstream dst (copyFolder, std::ios::binary);
136
+ dst << src.rdbuf ();
137
+ }
138
+ }
139
+ }
140
+ delete [] dataBuffer;
141
+ }
142
+
143
+ if (flgSearch)
144
+ cout<<" Total " <<PatchSig<<" files with FlexNet license." << endl;
145
+
146
+ return 0 ;
147
+ }
148
+
149
+ /* *
150
+ * Search binary file
151
+ * @param root
152
+ * @param files
153
+ */
154
+ void getFiles (const string &root, vector<string> &files) {
155
+
156
+ DIR *pDir;
157
+ struct dirent *ptr;
158
+
159
+ if (!(pDir = opendir (root.c_str ()))) {
160
+ return ;
161
+ }
162
+
163
+ while ((ptr = readdir (pDir)) != nullptr ) {
164
+
165
+ string sub_file = root + " /" + ptr->d_name ;
166
+
167
+ if (ptr->d_type != DT_REG && ptr->d_type != DT_DIR && ptr->d_type != DT_LNK) {
168
+ return ;
169
+ }
170
+
171
+ if (ptr->d_type == DT_REG) {
172
+ if (strcmp (ptr->d_name , " ." ) != 0 && strcmp (ptr->d_name , " .." ) != 0 ) {
173
+
174
+ uint8_t *dataBuffer = new uint8_t [4 ];
175
+
176
+ ifstream binRead (sub_file,ios::binary);
177
+ binRead.seekg (0 ,ios::beg);
178
+ binRead.read ((char *)dataBuffer,4 );
179
+ binRead.close ();
180
+
181
+ // Find binary file
182
+ if (strstr ((char *)(dataBuffer), " ELF" ))
183
+ {
184
+ files.push_back (sub_file);
185
+ }
186
+ }
187
+ }
188
+ else if (ptr->d_type == DT_DIR) {
189
+ if (strcmp (ptr->d_name , " ." ) != 0 && strcmp (ptr->d_name , " .." ) != 0 ) {
190
+ getFiles (sub_file, files);
191
+ }
192
+ }
193
+ }
194
+ closedir (pDir);
195
+ }
196
+ /* *
197
+ * Creat multiple directory
198
+ * @param directoryPath
199
+ * @return
200
+ */
201
+ int32_t createDirectory (const std::string &directoryPath)
202
+ {
203
+ uint32_t dirPathLen = directoryPath.length ();
204
+
205
+ if (dirPathLen > 256 )
206
+ {
207
+ return -1 ;
208
+ }
209
+ char tmpDirPath[256 ] = { 0 };
210
+
211
+ for (uint32_t i = 0 ; i < dirPathLen; ++i)
212
+ {
213
+ tmpDirPath[i] = directoryPath[i];
214
+ if (tmpDirPath[i] == ' \\ ' || tmpDirPath[i] == ' /' )
215
+ {
216
+ if (ACCESS (tmpDirPath, 0 ) != 0 )
217
+ {
218
+ int32_t ret = MKDIR (tmpDirPath);
219
+ if (ret != 0 )
220
+ {
221
+ return ret;
222
+ }
223
+ }
224
+ }
225
+ }
226
+ return 0 ;
227
+ }
0 commit comments