File: uefifind_main.cpp

package info (click to toggle)
uefitool 0.28.0%2BA73-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,728 kB
  • sloc: ansic: 55,322; cpp: 23,375; sh: 43; xml: 23; makefile: 5
file content (192 lines) | stat: -rw-r--r-- 5,995 bytes parent folder | download | duplicates (2)
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
/* uefifind_main.cpp

Copyright (c) 2018, LongSoft. All rights reserved.
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

#include "../version.h"
#include "../common/guiddatabase.h"
#include "uefifind.h"

void print_usage()
{
    std::cout << "UEFIFind " PROGRAM_VERSION << std::endl <<
        "Usage: UEFIFind {-h | --help | -v | -version}" << std::endl <<
        "       UEFIFind imagefile {header | body | all} {list | count} pattern" << std::endl <<
        "       UEFIFind imagefile file patternsfile" << std::endl;
}

int main(int argc, char *argv[])
{
    UEFIFind w;
    USTATUS result;

    if (argc == 1) {
        print_usage();
        return U_SUCCESS;
    }
    else if (argc == 2) {
        UString arg = argv[1];
        if (arg == UString("-h") || arg == UString("--help")) {
            print_usage();
            return U_SUCCESS;
        }
        else if (arg == UString("-v") || arg == UString("--version")) {
            std::cout << PROGRAM_VERSION << std::endl;
            return U_SUCCESS;
        }
    }
    else if (argc == 5) {
        UString inputArg = argv[1];
        UString modeArg = argv[2];
        UString subModeArg = argv[3];
        UString patternArg = argv[4];

        // Get search mode
        UINT8 mode;
        if (modeArg == UString("header"))
            mode = SEARCH_MODE_HEADER;
        else if (modeArg == UString("body"))
            mode = SEARCH_MODE_BODY;
        else if (modeArg == UString("all"))
            mode = SEARCH_MODE_ALL;
        else
            return U_INVALID_PARAMETER;

        // Get result type
        bool count;
        if (subModeArg == UString("list"))
            count = false;
        else if (subModeArg == UString("count"))
            count = true;
        else
            return U_INVALID_PARAMETER;

        // Parse input file
        result = w.init(inputArg);
        if (result)
            return result;

        // Go find the supplied pattern
        UString found;
        result = w.find(mode, count, patternArg, found);
        if (result)
            return result;

        // Nothing is found
        if (found.isEmpty())
            return U_ITEM_NOT_FOUND;

        // Print result
        std::cout << found.toLocal8Bit();
        return U_SUCCESS;
    }
    else if (argc == 4) {
        UString inputArg = argv[1];
        UString modeArg = argv[2];
        UString patternArg = argv[3];

        // Get search mode
        if (modeArg != UString("file"))
            return U_INVALID_PARAMETER;

        // Open patterns file
        if (!isExistOnFs(patternArg))
            return U_FILE_OPEN;

        std::ifstream patternsFile(patternArg.toLocal8Bit());
        if (!patternsFile)
            return U_FILE_OPEN;

        // Parse input file
        result = w.init(inputArg);
        if (result)
            return result;

        // Perform searches
        bool somethingFound = false;
        while (!patternsFile.eof()) {
            std::string line;
            std::getline(patternsFile, line);
            // Use sharp symbol as commentary
            if (line.size() == 0 || line[0] == '#')
                continue;

            // Split the read line
            std::vector<UString> list;
            std::string::size_type prev = 0, curr = 0;
            while ((curr = line.find(' ', curr)) != std::string::npos) {
                std::string substring( line.substr(prev, curr-prev) );
                list.push_back(UString(substring.c_str()));
                prev = ++curr;
            }
            list.push_back(UString(line.substr(prev, curr-prev).c_str()));

            if (list.size() < 3) {
                std::cout << line << std::endl << "skipped, too few arguments" << std::endl << std::endl;
                continue;
            }
            // Get search mode
            UINT8 mode;
            if (list.at(0) == UString("header"))
                mode = SEARCH_MODE_HEADER;
            else if (list.at(0) == UString("body"))
                mode = SEARCH_MODE_BODY;
            else if (list.at(0) == UString("all"))
                mode = SEARCH_MODE_ALL;
            else {
                std::cout << line << std::endl << "skipped, invalid search mode" << std::endl << std::endl;
                continue;
            }

            // Get result type
            bool count;
            if (list.at(1) == UString("list"))
                count = false;
            else if (list.at(1) == UString("count"))
                count = true;
            else {
                std::cout << line << std::endl << "skipped, invalid result type" << std::endl << std::endl;
                continue;
            }

            // Go find the supplied pattern
            UString found;
            result = w.find(mode, count, list.at(2), found);
            if (result) {
                std::cout << line << std::endl << "skipped, find failed with error " << (UINT32)result << std::endl << std::endl;
                continue;
            }

            if (found.isEmpty()) {
                // Nothing is found
                std::cout << line << std::endl << "nothing found" << std::endl << std::endl;
            }
            else {
                // Print result
                std::cout << line << std::endl << found.toLocal8Bit() << std::endl;
                somethingFound = true;
            }
        }

        // Nothing is found
        if (!somethingFound)
            return U_ITEM_NOT_FOUND;

        return U_SUCCESS;
    }

    print_usage();
    return U_INVALID_PARAMETER;
}