forked from smarttechnologies/peparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resourcepath.cpp
77 lines (66 loc) · 1.93 KB
/
resourcepath.cpp
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
// Copyright (c) 2016 SMART Technologies. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
#pragma warning(push)
#pragma warning(disable : 4996)
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#pragma warning(pop)
#include "resourcepath.h"
#include "peparser.h"
#include <iostream>
namespace peparser
{
bool ParsePath(const std::wstring& binaryPath, const std::wstring& resourcePath, ResourcePath& path, std::vector<WORD>& lang_ids, bool& allLanguages)
{
std::vector<std::wstring> pathElements;
boost::split(pathElements, resourcePath, boost::is_any_of(L"/"));
try
{
if (pathElements[0][0] != L'@')
path.SetType(std::stoi(pathElements[0]));
else
{
pathElements[0] = pathElements[0].substr(1, pathElements[0].size() - 1);
path.SetType(pathElements[0]);
}
if (pathElements[1][0] != L'@')
path.SetName(std::stoi(pathElements[1]));
else
{
pathElements[1] = pathElements[1].substr(1, pathElements[1].size() - 1);
path.SetName(pathElements[1]);
}
if (pathElements.size() > 2)
{
allLanguages = false;
lang_ids.push_back(std::stoi(pathElements[2]));
}
if (allLanguages)
{
PEParser pe(binaryPath);
pe.Open();
if (!pe.IsValidPE())
{
std::wcerr << L"Can't open file for reading or invalid format." << std::endl;
return false;
}
ResourceEntryPtr node;
if (pe.ResourceDirectory())
node = pe.ResourceDirectory()->AtPath(resourcePath);
if (node)
for (auto& entry : node->Entries())
{
if (!entry.second) continue;
if (entry.second->Name().size() > 0 && entry.second->Name()[0] != L'@')
lang_ids.push_back(std::stoi(entry.second->Name()));
}
}
}
catch (...)
{
std::wcerr << L"Error parsing options: must have valid resource path (type/name/lang)" << std::endl;
return false;
}
return true;
}
}