summaryrefslogtreecommitdiff
path: root/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.cpp
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2008-07-11 15:50:46 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2008-07-11 15:50:46 +0000
commit14c7f45cdae826f88dc539c8c38dd95caf305731 (patch)
tree832da054b7cfb2dc6fd63339af736625f31d21aa /utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.cpp
parent7c84ede3781c27db73403bd6302f320c76a58c8c (diff)
downloadrockbox-14c7f45cdae826f88dc539c8c38dd95caf305731.tar.gz
rockbox-14c7f45cdae826f88dc539c8c38dd95caf305731.zip
Add zook's ZenUtils to SVN
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@18010 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.cpp')
-rwxr-xr-xutils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.cpp1497
1 files changed, 1497 insertions, 0 deletions
diff --git a/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.cpp b/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.cpp
new file mode 100755
index 0000000000..5e4d3010bf
--- /dev/null
+++ b/utils/zenutils/libraries/pelib-0.9/pelib/ResourceDirectory.cpp
@@ -0,0 +1,1497 @@
1/*
2* ResourceDirectory.h - Part of the PeLib library.
3*
4* Copyright (c) 2004 - 2005 Sebastian Porst (webmaster@the-interweb.com)
5* All rights reserved.
6*
7* This software is licensed under the zlib/libpng License.
8* For more details see http://www.opensource.org/licenses/zlib-license.php
9* or the license information file (license.htm) in the root directory
10* of PeLib.
11*/
12
13#include "ResourceDirectory.h"
14
15namespace PeLib
16{
17// -------------------------------------------------- ResourceChild -------------------------------------------
18
19 ResourceChild::ResourceChild()
20 {
21 }
22
23 ResourceChild::ResourceChild(const ResourceChild& rhs)
24 {
25 if (dynamic_cast<ResourceNode*>(rhs.child))
26 {
27 ResourceNode* oldnode = static_cast<ResourceNode*>(rhs.child);
28
29 entry = rhs.entry;
30
31 child = new ResourceNode;
32 child->uiElementRva = rhs.child->getElementRva();
33 static_cast<ResourceNode*>(child)->header = oldnode->header;
34 static_cast<ResourceNode*>(child)->children = oldnode->children;
35 }
36 else
37 {
38 ResourceLeaf* oldnode = static_cast<ResourceLeaf*>(rhs.child);
39
40 child = new ResourceLeaf;
41 child->uiElementRva = rhs.child->getElementRva();
42 static_cast<ResourceLeaf*>(child)->m_data = oldnode->m_data;
43 static_cast<ResourceLeaf*>(child)->entry = oldnode->entry;
44 }
45 }
46
47 ResourceChild& ResourceChild::operator=(const ResourceChild& rhs)
48 {
49 if (this != &rhs)
50 {
51 if (dynamic_cast<ResourceNode*>(rhs.child))
52 {
53 ResourceNode* oldnode = static_cast<ResourceNode*>(rhs.child);
54
55 entry = rhs.entry;
56
57 child = new ResourceNode;
58 child->uiElementRva = rhs.child->getElementRva();
59 static_cast<ResourceNode*>(child)->header = oldnode->header;
60 static_cast<ResourceNode*>(child)->children = oldnode->children;
61 }
62 else
63 {
64 ResourceLeaf* oldnode = static_cast<ResourceLeaf*>(rhs.child);
65
66 child = new ResourceLeaf;
67 child->uiElementRva = rhs.child->getElementRva();
68 static_cast<ResourceLeaf*>(child)->m_data = oldnode->m_data;
69 static_cast<ResourceLeaf*>(child)->entry = oldnode->entry;
70 }
71 }
72
73 return *this;
74 }
75
76 ResourceChild::~ResourceChild()
77 {
78 delete child;
79 }
80
81 /**
82 * Compares the resource child's id to the parameter dwId.
83 * @param dwId ID of a resource.
84 * @return True, if the resource child's id equals the parameter.
85 **/
86 bool ResourceChild::equalId(dword dwId) const
87 {
88 return entry.irde.Name == dwId;
89 }
90
91 /**
92 * Compares the resource child's name to the parameter strName.
93 * @param strName ID of a resource.
94 * @return True, if the resource child's name equals the parameter.
95 **/
96 bool ResourceChild::equalName(std::string strName) const
97 {
98 return entry.wstrName == strName;
99 }
100
101 /**
102 * Returns true if the resource was given a name.
103 **/
104 bool ResourceChild::isNamedResource() const
105 {
106 return entry.wstrName.size() != 0;
107 }
108
109 /**
110 * The children of a resource must be ordered in a certain way. First come the named resources
111 * in sorted order, afterwards followed the unnamed resources in sorted order.
112 **/
113 bool ResourceChild::operator<(const ResourceChild& rc) const
114 {
115 if (this->isNamedResource() && !rc.isNamedResource())
116 {
117 return true;
118 }
119 else if (!this->isNamedResource() && rc.isNamedResource())
120 {
121 return false;
122 }
123 else if (this->isNamedResource() && rc.isNamedResource())
124 {
125 return this->entry.wstrName < rc.entry.wstrName;
126 }
127 else
128 {
129 return this->entry.irde.Name < rc.entry.irde.Name;
130 }
131 }
132
133/* unsigned int ResourceChild::size() const
134 {
135 return PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY::size()
136 + child->size()
137 + (entry.wstrName.size() ? entry.wstrName.size() + 2 : 0);
138 }
139*/
140// -------------------------------------------------- ResourceElement -------------------------------------------
141
142 /**
143 * Returns the RVA of a ResourceElement. This is the RVA where the ResourceElement can be
144 * found in the file.
145 * @return RVA of the ResourceElement.
146 **/
147 unsigned int ResourceElement::getElementRva() const
148 {
149 return uiElementRva;
150 }
151
152// -------------------------------------------------- ResourceLeaf -------------------------------------------
153
154 /**
155 * Checks if a ResourceElement is a leaf or not.
156 * @return Always returns true.
157 **/
158 bool ResourceLeaf::isLeaf() const
159 {
160 return true;
161 }
162
163 /**
164 * Reads the next resource leaf from the InputBuffer.
165 * @param inpBuffer An InputBuffer that holds the complete resource directory.
166 * @param uiOffset Offset of the resource leaf that's to be read.
167 * @param uiRva RVA of the beginning of the resource directory.
168 * @param pad Used for debugging purposes.
169 **/
170 int ResourceLeaf::read(InputBuffer& inpBuffer, unsigned int uiOffset, unsigned int uiRva/*, const std::string& pad*/)
171 {
172// std::cout << pad << "Leaf:" << std::endl;
173
174 // Invalid leaf.
175 if (uiOffset + PELIB_IMAGE_RESOURCE_DATA_ENTRY::size() > inpBuffer.size())
176 {
177 return 1;
178 }
179
180 uiElementRva = uiOffset + uiRva;
181
182 inpBuffer.set(uiOffset);
183
184 inpBuffer >> entry.OffsetToData;
185 inpBuffer >> entry.Size;
186 inpBuffer >> entry.CodePage;
187 inpBuffer >> entry.Reserved;
188
189/* std::cout << pad << std::hex << "Offset: " << entry.OffsetToData << std::endl;
190 std::cout << pad << std::hex << "Size: " << entry.Size << std::endl;
191 std::cout << pad << std::hex << "CodePage: " << entry.CodePage << std::endl;
192 std::cout << pad << std::hex << "Reserved: " << entry.Reserved << std::endl;
193*/
194 // Invalid leaf.
195 if (entry.OffsetToData - uiRva + entry.Size > inpBuffer.size())
196 {
197// std::cout << entry.OffsetToData << " XXX " << uiRva << " - " << entry.Size << " - " << inpBuffer.size() << std::endl;
198 return 1;
199 }
200
201// std::cout << entry.OffsetToData << " - " << uiRva << " - " << entry.Size << " - " << inpBuffer.size() << std::endl;
202 inpBuffer.set(entry.OffsetToData - uiRva);
203 m_data.assign(inpBuffer.data() + inpBuffer.get(), inpBuffer.data() + inpBuffer.get() + entry.Size);
204// std::cout << pad << std::hex << "Vector: " << m_data.size() << std::endl;
205// std::copy(m_data.begin(), m_data.end(), std::ostream_iterator<int>(std::cout << std::hex, " "));
206 return 0;
207 }
208
209 /**
210 * Rebuilds the current resource leaf.
211 * @param obBuffer OutputBuffer where the rebuilt resource leaf is stored.
212 * @param uiOffset Offset of the resource leaf inside the resource directory.
213 * @param uiRva RVA of the resource directory.
214 **/
215 void ResourceLeaf::rebuild(OutputBuffer& obBuffer, unsigned int& uiOffset, unsigned int uiRva, const std::string&) const
216 {
217// std::cout << std::hex << pad << "Leaf: " << uiOffset << std::endl;
218 uiOffset += PELIB_IMAGE_RESOURCE_DATA_ENTRY::size();
219
220// obBuffer << entry.OffsetToData;
221// obBuffer << uiOffset;
222 obBuffer << uiRva + uiOffset + (uiOffset % 4);
223 obBuffer << entry.Size;
224 obBuffer << entry.CodePage;
225 obBuffer << entry.Reserved;
226
227 while (uiOffset % 4)
228 {
229 uiOffset++;
230 obBuffer << (byte)0;
231 }
232
233 uiOffset += static_cast<unsigned int>(m_data.size());
234
235 for (unsigned int i=0;i<m_data.size();i++)
236 {
237 obBuffer << m_data[i];
238 }
239// std::cout << "LeafChild: " << std::endl;
240 }
241
242 void ResourceLeaf::makeValid()
243 {
244 entry.Size = static_cast<unsigned int>(m_data.size());
245 }
246
247/* /// Returns the size of a resource leaf.
248 unsigned int ResourceLeaf::size() const
249 {
250 return PELIB_IMAGE_RESOURCE_DATA_ENTRY::size() + m_data.size();
251 }
252*/
253
254 /**
255 * Returns a vector that contains the raw data of a resource leaf.
256 * @return Raw data of the resource.
257 **/
258 std::vector<byte> ResourceLeaf::getData() const
259 {
260 return m_data;
261 }
262
263 /**
264 * Overwrites the raw data of a resource.
265 * @param vData New data of the resource.
266 **/
267 void ResourceLeaf::setData(const std::vector<byte>& vData)
268 {
269 m_data = vData;
270 }
271
272 /**
273 * Returns the leaf's OffsetToData value. That's the RVA where the raw data of the resource
274 * can be found.
275 * @return The leaf's OffsetToData value.
276 **/
277 dword ResourceLeaf::getOffsetToData() const
278 {
279 return entry.OffsetToData;
280 }
281
282 /**
283 * Returns the leaf's Size value. That's the size of the raw data of the resource.
284 * @return The leaf's Size value.
285 **/
286 dword ResourceLeaf::getSize() const
287 {
288 return entry.Size;
289 }
290
291 /**
292 * Returns the leaf's CodePage value.
293 * @return The leaf's CodePage value.
294 **/
295 dword ResourceLeaf::getCodePage() const
296 {
297 return entry.CodePage;
298 }
299
300 /**
301 * Returns the leaf's Reserved value.
302 * @return The leaf's Reserved value.
303 **/
304 dword ResourceLeaf::getReserved() const
305 {
306 return entry.Reserved;
307 }
308
309 /**
310 * Sets the leaf's OffsetToData value.
311 * @param dwValue The leaf's new OffsetToData value.
312 **/
313 void ResourceLeaf::setOffsetToData(dword dwValue)
314 {
315 entry.OffsetToData = dwValue;
316 }
317
318 /**
319 * Sets the leaf's Size value.
320 * @param dwValue The leaf's new Size value.
321 **/
322 void ResourceLeaf::setSize(dword dwValue)
323 {
324 entry.Size = dwValue;
325 }
326
327 /**
328 * Sets the leaf's CodePage value.
329 * @param dwValue The leaf's new CodePage value.
330 **/
331 void ResourceLeaf::setCodePage(dword dwValue)
332 {
333 entry.CodePage = dwValue;
334 }
335
336 /**
337 * Sets the leaf's Reserved value.
338 * @param dwValue The leaf's new Reserved value.
339 **/
340 void ResourceLeaf::setReserved(dword dwValue)
341 {
342 entry.Reserved = dwValue;
343 }
344
345
346// -------------------------------------------------- ResourceNode -------------------------------------------
347
348 /**
349 * Checks if a ResourceElement is a leaf or not.
350 * @return Always returns false.
351 **/
352 bool ResourceNode::isLeaf() const
353 {
354 return false;
355 }
356
357 /**
358 * Sorts the node's children and corrects the node's header.
359 **/
360 void ResourceNode::makeValid()
361 {
362 std::sort(children.begin(), children.end());
363 header.NumberOfNamedEntries = static_cast<PeLib::word>(std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)));
364 header.NumberOfIdEntries = static_cast<unsigned int>(children.size()) - header.NumberOfNamedEntries;
365 }
366
367 /**
368 * Rebuilds the current resource node.
369 * @param obBuffer OutputBuffer where the rebuilt resource node is stored.
370 * @param uiOffset Offset of the resource node inside the resource directory.
371 * @param uiRva RVA of the resource directory.
372 * @param pad Used for debugging.
373 **/
374 void ResourceNode::rebuild(OutputBuffer& obBuffer, unsigned int& uiOffset, unsigned int uiRva, const std::string& pad) const
375 {
376/* std::cout << std::hex << pad << uiOffset << std::endl;
377
378 std::cout << std::hex << pad << "header.Characteristics: " << header.Characteristics << std::endl;
379 std::cout << std::hex << pad << "header.TimeDateStamp: " << header.TimeDateStamp << std::endl;
380 std::cout << std::hex << pad << "header.MajorVersion: " << header.MajorVersion << std::endl;
381 std::cout << std::hex << pad << "header.MinorVersion: " << header.MinorVersion << std::endl;
382 std::cout << std::hex << pad << "header.NumberOfNamedEntries: " << header.NumberOfNamedEntries << std::endl;
383 std::cout << std::hex << pad << "header.NumberOfIdEntries: " << header.NumberOfIdEntries << std::endl;
384*/
385 obBuffer << header.Characteristics;
386 obBuffer << header.TimeDateStamp;
387 obBuffer << header.MajorVersion;
388 obBuffer << header.MinorVersion;
389 //std::cout << pad << "Children: " << children.size() << std::endl;
390 //std::cout << pad << std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)) << std::endl;
391 //std::cout << pad << children.size() - std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource)) << std::endl;
392// obBuffer << std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource));
393// obBuffer << children.size() - std::count_if(children.begin(), children.end(), std::mem_fun_ref(&ResourceChild::isNamedResource));
394 obBuffer << header.NumberOfNamedEntries;
395 obBuffer << header.NumberOfIdEntries;
396
397 uiOffset += PELIB_IMAGE_RESOURCE_DIRECTORY::size();
398
399 for (unsigned int i=0;i<children.size();i++)
400 {
401 obBuffer << (dword)0;
402 obBuffer << (dword)0;
403 }
404
405 unsigned int newoffs = (uiOffset + children.size() * 8);
406
407 for (unsigned int i=0;i<children.size();i++)
408 {
409 if (!children[i].entry.wstrName.size())
410 {
411 obBuffer.update(uiOffset, children[i].entry.irde.Name);
412 }
413 else
414 {
415 obBuffer.update(uiOffset, newoffs | PELIB_IMAGE_RESOURCE_NAME_IS_STRING);
416 obBuffer << (word)children[i].entry.wstrName.size();
417 newoffs += 2;
418 for (unsigned int j=0;j<children[i].entry.wstrName.size();j++)
419 {
420// std::cout << children[i].entry.wstrName[j];
421 obBuffer << (word)children[i].entry.wstrName[j];
422 newoffs += 2;
423 }
424 }
425 uiOffset += 4;
426// obBuffer << children[i].entry.OffsetToData;
427 obBuffer.update(uiOffset, newoffs | (children[i].entry.irde.OffsetToData & PELIB_IMAGE_RESOURCE_DATA_IS_DIRECTORY));
428 uiOffset += 4;
429 children[i].child->rebuild(obBuffer, newoffs, uiRva, pad + " ");
430 }
431 uiOffset = newoffs;
432 }
433
434 /**
435 * Reads the next resource node from the InputBuffer.
436 * @param inpBuffer An InputBuffer that holds the complete resource directory.
437 * @param uiOffset Offset of the resource node that's to be read.
438 * @param uiRva RVA of the beginning of the resource directory.
439 * @param pad Something I need for debugging. Will be removed soon.
440 **/
441 int ResourceNode::read(InputBuffer& inpBuffer, unsigned int uiOffset, unsigned int uiRva/*, const std::string& pad*/)
442 {
443 // Not enough space to be a valid node.
444 if (uiOffset + PELIB_IMAGE_RESOURCE_DIRECTORY::size() > inpBuffer.size())
445 {
446 return 1;
447 }
448
449 uiElementRva = uiOffset + uiRva;
450
451 inpBuffer.set(uiOffset);
452
453 inpBuffer >> header.Characteristics;
454 inpBuffer >> header.TimeDateStamp;
455 inpBuffer >> header.MajorVersion;
456 inpBuffer >> header.MinorVersion;
457 inpBuffer >> header.NumberOfNamedEntries;
458 inpBuffer >> header.NumberOfIdEntries;
459
460 // Not enough space to be a valid node.
461 if (uiOffset + PELIB_IMAGE_RESOURCE_DIRECTORY::size()
462 + (header.NumberOfNamedEntries + header.NumberOfIdEntries) * PELIB_IMAGE_RESOURCE_DIRECTORY_ENTRY::size()
463 > inpBuffer.size())
464 {
465 return 1;
466 }
467
468// std::cout << std::hex << pad << "Characteristics: " << header.Characteristics << std::endl;
469// std::cout << std::hex << pad << "TimeDateStamp: " << header.TimeDateStamp << std::endl;
470// std::cout << std::hex << pad << "MajorVersion: " << header.MajorVersion << std::endl;
471// std::cout << std::hex << pad << "MinorVersion: " << header.MinorVersion << std::endl;
472// std::cout << std::hex << pad << "NumberOfNamedEntries: " << header.NumberOfNamedEntries << std::endl;
473// std::cout << std::hex << pad << "NumberOfIdEntries: " << header.NumberOfIdEntries << std::endl;
474
475 for (int i=0;i<header.NumberOfNamedEntries + header.NumberOfIdEntries;i++)
476 {
477 ResourceChild rc;
478 inpBuffer >> rc.entry.irde.Name;
479 inpBuffer >> rc.entry.irde.OffsetToData;
480
481 unsigned int lastPos = inpBuffer.get();
482
483 if (rc.entry.irde.Name & PELIB_IMAGE_RESOURCE_NAME_IS_STRING)
484 {
485 // Enough space to read string length?
486 if ((rc.entry.irde.Name & ~PELIB_IMAGE_RESOURCE_NAME_IS_STRING) + 2 < inpBuffer.size())
487 {
488 inpBuffer.set(rc.entry.irde.Name & ~PELIB_IMAGE_RESOURCE_NAME_IS_STRING);
489 word strlen;
490 inpBuffer >> strlen;
491
492 // Enough space to read string?
493 if ((rc.entry.irde.Name & ~PELIB_IMAGE_RESOURCE_NAME_IS_STRING) + 2 * strlen < inpBuffer.size())
494 {
495 wchar_t c;
496 for (word i=0;i<strlen;i++)
497 {
498 inpBuffer >> c;
499 rc.entry.wstrName += c;
500 }
501 }
502 }
503
504// std::wcout << rc.entry.wstrName << std::endl;
505
506// std::cout << strlen << std::endl;
507 inpBuffer.set(lastPos);
508 }
509
510 if (rc.entry.irde.OffsetToData & PELIB_IMAGE_RESOURCE_DATA_IS_DIRECTORY)
511 {
512 rc.child = new ResourceNode;
513 rc.child->read(inpBuffer, rc.entry.irde.OffsetToData & ~PELIB_IMAGE_RESOURCE_DATA_IS_DIRECTORY, uiRva/*, pad + " "*/);
514 }
515 else
516 {
517 rc.child = new ResourceLeaf;
518 rc.child->read(inpBuffer, rc.entry.irde.OffsetToData, uiRva/*, pad + " "*/);
519 }
520// std::cout << std::hex << pad << "Entry " << i << "(Name): " << rc.entry.irde.Name << std::endl;
521// std::cout << std::hex << pad << "Entry " << i << "(Offset): " << rc.entry.irde.OffsetToData << std::endl;
522
523 children.push_back(rc);
524 inpBuffer.set(lastPos);
525 }
526
527 return 0;
528 }
529
530 /**
531 * Returns the number of children of the current node. Note that this number is the number
532 * of defined children, not the value from the header.
533 * @return Number of node's children.
534 **/
535 unsigned int ResourceNode::getNumberOfChildren() const
536 {
537 return static_cast<unsigned int>(children.size());
538 }
539
540 /**
541 * Adds another child to the current node.
542 **/
543 void ResourceNode::addChild()
544 {
545 ResourceChild c;
546 c.child = 0;
547 children.push_back(c);
548 }
549
550 /**
551 * Returns a node's child.
552 * @param uiIndex Index of the child.
553 * @return The child identified by uiIndex. This child can be either a ResourceNode or a ResourceLeaf.
554 **/
555 ResourceElement* ResourceNode::getChild(unsigned int uiIndex)
556 {
557 return children[uiIndex].child;
558 }
559
560 /**
561 * Removes a child from the current node.
562 * @param uiIndex Index of the child.
563 **/
564 void ResourceNode::removeChild(unsigned int uiIndex)
565 {
566 children.erase(children.begin() + uiIndex);
567 }
568
569 /**
570 * Returns the name of a child.
571 * @param uiIndex Index of the child.
572 * @return Either the name of the specified child or an empty string.
573 **/
574 std::string ResourceNode::getChildName(unsigned int uiIndex) const
575 {
576 return children[uiIndex].entry.wstrName;
577 }
578
579 /**
580 * Returns the Name value of a child.
581 * @param uiIndex Index of the child.
582 * @return Name value of a child.
583 **/
584 dword ResourceNode::getOffsetToChildName(unsigned int uiIndex) const
585 {
586 return children[uiIndex].entry.irde.Name;
587 }
588
589 /**
590 * Returns the OffsetToData value of a child.
591 * @param uiIndex Index of the child.
592 * @return OffsetToData value of a child.
593 **/
594 dword ResourceNode::getOffsetToChildData(unsigned int uiIndex) const
595 {
596 return children[uiIndex].entry.irde.OffsetToData;
597 }
598
599
600 /**
601 * Sets the name of a child.
602 * @param uiIndex Index of the child.
603 * @param strNewName New name of the resource.
604 **/
605 void ResourceNode::setChildName(unsigned int uiIndex, const std::string& strNewName)
606 {
607 children[uiIndex].entry.wstrName = strNewName;
608 }
609
610 /**
611 * Sets the Name value of a child.
612 * @param uiIndex Index of the child.
613 * @param dwNewOffset New Name value of the resource.
614 **/
615 void ResourceNode::setOffsetToChildName(unsigned int uiIndex, dword dwNewOffset)
616 {
617 children[uiIndex].entry.irde.Name = dwNewOffset;
618 }
619
620 /**
621 * Sets the OffsetToData value of a child.
622 * @param uiIndex Index of the child.
623 * @param dwNewOffset New OffsetToData value of the resource.
624 **/
625 void ResourceNode::setOffsetToChildData(unsigned int uiIndex, dword dwNewOffset)
626 {
627 children[uiIndex].entry.irde.OffsetToData = dwNewOffset;
628 }
629
630 /**
631 * Returns the Characteristics value of the node.
632 * @return Characteristics value of the node.
633 **/
634 dword ResourceNode::getCharacteristics() const
635 {
636 return header.Characteristics;
637 }
638
639 /**
640 * Returns the TimeDateStamp value of the node.
641 * @return TimeDateStamp value of the node.
642 **/
643 dword ResourceNode::getTimeDateStamp() const
644 {
645 return header.TimeDateStamp;
646 }
647
648 /**
649 * Returns the MajorVersion value of the node.
650 * @return MajorVersion value of the node.
651 **/
652 word ResourceNode::getMajorVersion() const
653 {
654 return header.MajorVersion;
655 }
656
657 /**
658 * Returns the MinorVersion value of the node.
659 * @return MinorVersion value of the node.
660 **/
661 word ResourceNode::getMinorVersion() const
662 {
663 return header.MinorVersion;
664 }
665
666 /**
667 * Returns the NumberOfNamedEntries value of the node.
668 * @return NumberOfNamedEntries value of the node.
669 **/
670 word ResourceNode::getNumberOfNamedEntries() const
671 {
672 return header.NumberOfNamedEntries;
673 }
674
675 /**
676 * Returns the NumberOfIdEntries value of the node.
677 * @return NumberOfIdEntries value of the node.
678 **/
679 word ResourceNode::getNumberOfIdEntries() const
680 {
681 return header.NumberOfIdEntries;
682 }
683
684 /**
685 * Sets the Characteristics value of the node.
686 * @param value New Characteristics value of the node.
687 **/
688 void ResourceNode::setCharacteristics(dword value)
689 {
690 header.Characteristics = value;
691 }
692
693 /**
694 * Sets the TimeDateStamp value of the node.
695 * @param value New TimeDateStamp value of the node.
696 **/
697 void ResourceNode::setTimeDateStamp(dword value)
698 {
699 header.TimeDateStamp = value;
700 }
701
702 /**
703 * Sets the MajorVersion value of the node.
704 * @param value New MajorVersion value of the node.
705 **/
706 void ResourceNode::setMajorVersion(word value)
707 {
708 header.MajorVersion = value;
709 }
710
711 /**
712 * Sets the MinorVersion value of the node.
713 * @param value New MinorVersion value of the node.
714 **/
715 void ResourceNode::setMinorVersion(word value)
716 {
717 header.MinorVersion = value;
718 }
719
720 /**
721 * Sets the NumberOfNamedEntries value of the node.
722 * @param value New NumberOfNamedEntries value of the node.
723 **/
724 void ResourceNode::setNumberOfNamedEntries(word value)
725 {
726 header.NumberOfNamedEntries = value;
727 }
728
729 /**
730 * Sets the NumberOfIdEntries value of the node.
731 * @param value New NumberOfIdEntries value of the node.
732 **/
733 void ResourceNode::setNumberOfIdEntries(word value)
734 {
735 header.NumberOfIdEntries = value;
736 }
737
738
739/* /// Returns the size of a resource node.
740 unsigned int ResourceNode::size() const
741 {
742 if (children.size())
743 {
744 std::cout << std::accumulate(children.begin(), children.end(), 0, accumulate<ResourceChild>) << std::endl;
745 return PELIB_IMAGE_RESOURCE_DIRECTORY::size()
746 + std::accumulate(children.begin(), children.end(), 0, accumulate<ResourceChild>);
747 }
748 else
749 {
750 return 0;
751 }
752 }
753*/
754// -------------------------------------------------- ResourceDirectory -------------------------------------------
755
756 /**
757 * Returns the root node of the resource directory.
758 * @return Root node of the resource directory.
759 **/
760 ResourceNode* ResourceDirectory::getRoot()
761 {
762 return &m_rnRoot;
763 }
764
765 /**
766 * Correctly sorts the resource nodes of the resource tree. This function should be called
767 * before calling rebuild.
768 **/
769 void ResourceDirectory::makeValid()
770 {
771 m_rnRoot.makeValid();
772 }
773
774 /**
775 * Reads the resource directory from a file.
776 * @param strFilename Name of the file.
777 * @param uiOffset File offset of the resource directory.
778 * @param uiSize Raw size of the resource directory.
779 * @param uiResDirRva RVA of the beginning of the resource directory.
780 **/
781 int ResourceDirectory::read(const std::string& strFilename, unsigned int uiOffset, unsigned int uiSize, unsigned int uiResDirRva)
782 {
783 if (!uiSize || !uiOffset)
784 {
785 return 1;
786 }
787
788 std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
789
790 if (!ifFile)
791 {
792// throw Exceptions::CannotOpenFile(ResourceDirectoryId, __LINE__);
793 return 1;
794 }
795
796 if (fileSize(ifFile) < uiOffset + uiSize)
797 {
798// throw Exceptions::InvalidFormat(ResourceDirectoryId, __LINE__);
799 return 1;
800 }
801
802 ifFile.seekg(uiOffset, std::ios::beg);
803
804 PELIB_IMAGE_RESOURCE_DIRECTORY irdCurrRoot;
805
806 std::vector<byte> vResourceDirectory(uiSize);
807 ifFile.read(reinterpret_cast<char*>(&vResourceDirectory[0]), uiSize);
808
809 InputBuffer inpBuffer(vResourceDirectory);
810
811// ResourceNode currNode;
812 return m_rnRoot.read(inpBuffer, 0, uiResDirRva/*, ""*/);
813// std::swap(currNode, m_rnRoot);
814 }
815
816 /**
817 * Rebuilds the resource directory.
818 * @param vBuffer Buffer the source directory will be written to.
819 * @param uiRva RVA of the resource directory.
820 **/
821 void ResourceDirectory::rebuild(std::vector<byte>& vBuffer, unsigned int uiRva) const
822 {
823 OutputBuffer obBuffer(vBuffer);
824 unsigned int offs = 0;
825// std::cout << "Root: " << m_rnRoot.children.size() << std::endl;
826 m_rnRoot.rebuild(obBuffer, offs, uiRva, "");
827 }
828
829 /**
830 * Returns the size of the entire rebuilt resource directory. That's the size of the entire
831 * structure as it's written back to a file.
832 **/
833/* unsigned int ResourceDirectory::size() const
834 {
835 return m_rnRoot.size();
836 }
837*/
838 /**
839 * Writes the current resource directory back into a file.
840 * @param strFilename Name of the output file.
841 * @param uiOffset File offset where the resource directory will be written to.
842 * @param uiRva RVA of the file offset.
843 **/
844 int ResourceDirectory::write(const std::string& strFilename, unsigned int uiOffset, unsigned int uiRva) const
845 {
846 std::fstream ofFile(strFilename.c_str(), std::ios_base::in);
847
848 if (!ofFile)
849 {
850 ofFile.clear();
851 ofFile.open(strFilename.c_str(), std::ios_base::out | std::ios_base::binary);
852 }
853 else
854 {
855 ofFile.close();
856 ofFile.open(strFilename.c_str(), std::ios_base::in | std::ios_base::out | std::ios_base::binary);
857 }
858
859 if (!ofFile)
860 {
861 return ERROR_OPENING_FILE;
862 }
863
864 ofFile.seekp(uiOffset, std::ios::beg);
865
866 std::vector<unsigned char> vBuffer;
867 rebuild(vBuffer, uiRva);
868
869 ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), static_cast<unsigned int>(vBuffer.size()));
870
871 ofFile.close();
872
873 return 0;
874 }
875
876 /**
877 * Adds another resource type. The new resource type is identified by the ID dwResTypeId.
878 * @param dwResTypeId ID which identifies the resource type.
879 **/
880 int ResourceDirectory::addResourceType(dword dwResTypeId)
881 {
882 std::vector<ResourceChild>::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalId), dwResTypeId));
883 if (Iter != m_rnRoot.children.end())
884 {
885 return 1;
886 // throw Exceptions::EntryAlreadyExists(ResourceDirectoryId, __LINE__);
887 }
888
889 ResourceChild rcCurr;
890 rcCurr.child = new ResourceNode;
891 rcCurr.entry.irde.Name = dwResTypeId;
892 m_rnRoot.children.push_back(rcCurr);
893
894 return 0;
895 }
896
897 /**
898 * Adds another resource type. The new resource type is identified by the name strResTypeName.
899 * @param strResTypeName Name which identifies the resource type.
900 **/
901 int ResourceDirectory::addResourceType(const std::string& strResTypeName)
902 {
903 std::vector<ResourceChild>::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalName), strResTypeName));
904 if (Iter != m_rnRoot.children.end())
905 {
906 return 1;
907// throw Exceptions::EntryAlreadyExists(ResourceDirectoryId, __LINE__);
908 }
909
910 ResourceChild rcCurr;
911 rcCurr.entry.wstrName = strResTypeName;
912 rcCurr.child = new ResourceNode;
913 m_rnRoot.children.push_back(rcCurr);
914
915 return 0;
916 }
917
918 /**
919 * Removes the resource type identified by the ID dwResTypeId.
920 * @param dwResTypeId ID which identifies the resource type.
921 **/
922 int ResourceDirectory::removeResourceType(dword dwResTypeId)
923 {
924 std::vector<ResourceChild>::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalId), dwResTypeId));
925 if (Iter == m_rnRoot.children.end())
926 {
927 return 1;
928// throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__);
929 }
930
931 bool isNamed = false;
932 if (Iter->isNamedResource()) isNamed = true;
933
934 m_rnRoot.children.erase(Iter);
935
936 if (isNamed) m_rnRoot.header.NumberOfNamedEntries = static_cast<PeLib::word>(m_rnRoot.children.size());
937 else m_rnRoot.header.NumberOfIdEntries = static_cast<PeLib::word>(m_rnRoot.children.size());
938
939 return 0;
940 }
941
942 /**
943 * Removes the resource type identified by the name strResTypeName.
944 * @param strResTypeName Name which identifies the resource type.
945 **/
946 int ResourceDirectory::removeResourceType(const std::string& strResTypeName)
947 {
948 std::vector<ResourceChild>::iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalName), strResTypeName));
949 if (Iter == m_rnRoot.children.end())
950 {
951 return 1;
952 // throw Exceptions::ResourceTypeDoesNotExist(ResourceDirectoryId, __LINE__);
953 }
954
955 bool isNamed = false;
956 if (Iter->isNamedResource()) isNamed = true;
957
958 m_rnRoot.children.erase(Iter);
959
960 if (isNamed) m_rnRoot.header.NumberOfNamedEntries = static_cast<PeLib::word>(m_rnRoot.children.size());
961 else m_rnRoot.header.NumberOfIdEntries = static_cast<PeLib::word>(m_rnRoot.children.size());
962
963 return 0;
964 }
965
966 /**
967 * Removes the resource type identified by the index uiIndex.
968 * @param uiIndex Index which identifies the resource type.
969 **/
970 int ResourceDirectory::removeResourceTypeByIndex(unsigned int uiIndex)
971 {
972 bool isNamed = false;
973 if (m_rnRoot.children[uiIndex].isNamedResource()) isNamed = true;
974
975 m_rnRoot.children.erase(m_rnRoot.children.begin() + uiIndex);
976
977 if (isNamed) m_rnRoot.header.NumberOfNamedEntries = static_cast<PeLib::word>(m_rnRoot.children.size());
978 else m_rnRoot.header.NumberOfIdEntries = static_cast<PeLib::word>(m_rnRoot.children.size());
979
980 return 0;
981 }
982
983 /**
984 * Adds another resource to the resource tree. The first parameter identifies the resource type
985 * of the new resource, the second parameter identifies the resource itself.
986 * @param dwResTypeId ID of the resource type.
987 * @param dwResId ID of the resource.
988 **/
989 int ResourceDirectory::addResource(dword dwResTypeId, dword dwResId)
990 {
991 ResourceChild rcCurr;
992 rcCurr.entry.irde.Name = dwResId;
993 return addResourceT(dwResTypeId, dwResId, rcCurr);
994 }
995
996 /**
997 * Adds another resource to the resource tree. The first parameter identifies the resource type
998 * of the new resource, the second parameter identifies the resource itself.
999 * @param dwResTypeId ID of the resource type.
1000 * @param strResName Name of the resource.
1001 **/
1002 int ResourceDirectory::addResource(dword dwResTypeId, const std::string& strResName)
1003 {
1004 ResourceChild rcCurr;
1005 rcCurr.entry.wstrName = strResName;
1006 return addResourceT(dwResTypeId, strResName, rcCurr);
1007 }
1008
1009 /**
1010 * Adds another resource to the resource tree. The first parameter identifies the resource type
1011 * of the new resource, the second parameter identifies the resource itself.
1012 * @param strResTypeName Name of the resource type.
1013 * @param dwResId ID of the resource.
1014 **/
1015 int ResourceDirectory::addResource(const std::string& strResTypeName, dword dwResId)
1016 {
1017 ResourceChild rcCurr;
1018 rcCurr.entry.irde.Name = dwResId;
1019 return addResourceT(strResTypeName, dwResId, rcCurr);
1020 }
1021
1022 /**
1023 * Adds another resource to the resource tree. The first parameter identifies the resource type
1024 * of the new resource, the second parameter identifies the resource itself.
1025 * @param strResTypeName Name of the resource type.
1026 * @param strResName Name of the resource.
1027 **/
1028 int ResourceDirectory::addResource(const std::string& strResTypeName, const std::string& strResName)
1029 {
1030 ResourceChild rcCurr;
1031 rcCurr.entry.wstrName = strResName;
1032 return addResourceT(strResTypeName, strResName, rcCurr);
1033 }
1034
1035 /**
1036 * Removes a resource from the resource tree. The first parameter identifies the resource type
1037 * of the new resource, the second parameter identifies the resource itself.
1038 * @param dwResTypeIndex ID of the resource type.
1039 * @param dwResId ID of the resource.
1040 **/
1041 int ResourceDirectory::removeResource(dword dwResTypeIndex, dword dwResId)
1042 {
1043 return removeResourceT(dwResTypeIndex, dwResId);
1044 }
1045
1046 /**
1047 * Removes a resource from the resource tree. The first parameter identifies the resource type
1048 * of the new resource, the second parameter identifies the resource itself.
1049 * @param dwResTypeIndex ID of the resource type.
1050 * @param strResName Name of the resource.
1051 **/
1052 int ResourceDirectory::removeResource(dword dwResTypeIndex, const std::string& strResName)
1053 {
1054 return removeResourceT(dwResTypeIndex, strResName);
1055 }
1056
1057 /**
1058 * Removes a resource from the resource tree. The first parameter identifies the resource type
1059 * of the new resource, the second parameter identifies the resource itself.
1060 * @param strResTypeName Name of the resource type.
1061 * @param dwResId ID of the resource.
1062 **/
1063 int ResourceDirectory::removeResource(const std::string& strResTypeName, dword dwResId)
1064 {
1065 return removeResourceT(strResTypeName, dwResId);
1066 }
1067
1068 /**
1069 * Removes a resource from the resource tree. The first parameter identifies the resource type
1070 * of the new resource, the second parameter identifies the resource itself.
1071 * @param strResTypeName Name of the resource type.
1072 * @param strResName Name of the resource.
1073 **/
1074 int ResourceDirectory::removeResource(const std::string& strResTypeName, const std::string& strResName)
1075 {
1076 return removeResourceT(strResTypeName, strResName);
1077 }
1078
1079 /**
1080 * Returns the number of resource types.
1081 **/
1082 unsigned int ResourceDirectory::getNumberOfResourceTypes() const
1083 {
1084 return static_cast<unsigned int>(m_rnRoot.children.size());
1085 }
1086
1087 /**
1088 * Returns the ID of a resource type which was specified through an index.
1089 * The valid range of the parameter uiIndex is 0...getNumberOfResourceTypes() - 1.
1090 * Leaving the invalid range leads to undefined behaviour.
1091 * @param uiIndex Index which identifies a resource type.
1092 * @return The ID of the specified resource type.
1093 **/
1094 dword ResourceDirectory::getResourceTypeIdByIndex(unsigned int uiIndex) const
1095 {
1096 return m_rnRoot.children[uiIndex].entry.irde.Name;
1097 }
1098
1099 /**
1100 * Returns the name of a resource type which was specified through an index.
1101 * The valid range of the parameter uiIndex is 0...getNumberOfResourceTypes() - 1.
1102 * Leaving the invalid range leads to undefined behaviour.
1103 * @param uiIndex Index which identifies a resource type.
1104 * @return The name of the specified resource type.
1105 **/
1106 std::string ResourceDirectory::getResourceTypeNameByIndex(unsigned int uiIndex) const
1107 {
1108 return m_rnRoot.children[uiIndex].entry.wstrName;
1109 }
1110
1111 /**
1112 * Converts the ID of a resource type to an index.
1113 * @param dwResTypeId ID of the resource type.
1114 * @return Index of that resource type.
1115 **/
1116 int ResourceDirectory::resourceTypeIdToIndex(dword dwResTypeId) const
1117 {
1118 std::vector<ResourceChild>::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalId), dwResTypeId));
1119 if (Iter == m_rnRoot.children.end()) return -1;
1120 return static_cast<unsigned int>(std::distance(m_rnRoot.children.begin(), Iter));
1121 }
1122
1123 /**
1124 * Converts the name of a resource type to an index.
1125 * @param strResTypeName ID of the resource type.
1126 * @return Index of that resource type.
1127 **/
1128 int ResourceDirectory::resourceTypeNameToIndex(const std::string& strResTypeName) const
1129 {
1130 std::vector<ResourceChild>::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalName), strResTypeName));
1131 if (Iter == m_rnRoot.children.end()) return -1;
1132 return static_cast<unsigned int>(std::distance(m_rnRoot.children.begin(), Iter));
1133 }
1134
1135 /**
1136 * Returns the number of resources of a specific resource type.
1137 * @param dwId ID of the resource type.
1138 * @return Number of resources of resource type dwId.
1139 **/
1140 unsigned int ResourceDirectory::getNumberOfResources(dword dwId) const
1141 {
1142// std::vector<ResourceChild>::const_iterator IterD = m_rnRoot.children.begin();
1143// std::cout << dwId << std::endl;
1144// while (IterD != m_rnRoot.children.end())
1145// {
1146// std::cout << IterD->entry.irde.Name << std::endl;
1147// ++IterD;
1148// }
1149
1150 std::vector<ResourceChild>::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalId), dwId));
1151 if (Iter == m_rnRoot.children.end())
1152 {
1153 return 0xFFFFFFFF;
1154 }
1155 else
1156 {
1157 ResourceNode* currNode = static_cast<ResourceNode*>(Iter->child);
1158 return static_cast<unsigned int>(currNode->children.size());
1159 }
1160 }
1161
1162 /**
1163 * Returns the number of resources of a specific resource type.
1164 * @param strResTypeName Name of the resource type.
1165 * @return Number of resources of resource type strResTypeName.
1166 **/
1167 unsigned int ResourceDirectory::getNumberOfResources(const std::string& strResTypeName) const
1168 {
1169 std::vector<ResourceChild>::const_iterator Iter = std::find_if(m_rnRoot.children.begin(), m_rnRoot.children.end(), std::bind2nd(std::mem_fun_ref(&ResourceChild::equalName), strResTypeName));
1170 if (Iter == m_rnRoot.children.end())
1171 {
1172 return 0xFFFFFFFF;
1173 }
1174 else
1175 {
1176 ResourceNode* currNode = static_cast<ResourceNode*>(Iter->child);
1177 return static_cast<unsigned int>(currNode->children.size());
1178 }
1179 }
1180
1181 /**
1182 * Returns the number of resources of a resource type which was specified through an index.
1183 * The valid range of the parameter uiIndex is 0...getNumberOfResourceTypes() - 1.
1184 * Leaving the invalid range leads to undefined behaviour.
1185 * @param uiIndex Index which identifies a resource type.
1186 * @return The number of resources of the specified resource type.
1187 **/
1188 unsigned int ResourceDirectory::getNumberOfResourcesByIndex(unsigned int uiIndex) const
1189 {
1190 ResourceNode* currNode = static_cast<ResourceNode*>(m_rnRoot.children[uiIndex].child);
1191 return static_cast<unsigned int>(currNode->children.size());
1192 }
1193
1194 /**
1195 * Gets the resource data of a specific resource.
1196 * @param dwResTypeId Identifies the resource type of the resource.
1197 * @param dwResId Identifies the resource.
1198 * @param data Vector where the data is stored.
1199 **/
1200 void ResourceDirectory::getResourceData(dword dwResTypeId, dword dwResId, std::vector<byte>& data) const
1201 {
1202 getResourceDataT(dwResTypeId, dwResId, data);
1203 }
1204
1205 /**
1206 * Gets the resource data of a specific resource.
1207 * @param dwResTypeId Identifies the resource type of the resource.
1208 * @param strResName Identifies the resource.
1209 * @param data Vector where the data is stored.
1210 **/
1211 void ResourceDirectory::getResourceData(dword dwResTypeId, const std::string& strResName, std::vector<byte>& data) const
1212 {
1213 getResourceDataT(dwResTypeId, strResName, data);
1214 }
1215
1216 /**
1217 * Gets the resource data of a specific resource.
1218 * @param strResTypeName Identifies the resource type of the resource.
1219 * @param dwResId Identifies the resource.
1220 * @param data Vector where the data is stored.
1221 **/
1222 void ResourceDirectory::getResourceData(const std::string& strResTypeName, dword dwResId, std::vector<byte>& data) const
1223 {
1224 getResourceDataT(strResTypeName, dwResId, data);
1225 }
1226
1227 /**
1228 * Gets the resource data of a specific resource.
1229 * @param strResTypeName Identifies the resource type of the resource.
1230 * @param strResName Identifies the resource.
1231 * @param data Vector where the data is stored.
1232 **/
1233 void ResourceDirectory::getResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector<byte>& data) const
1234 {
1235 getResourceDataT(strResTypeName, strResName, data);
1236 }
1237
1238 /**
1239 * Gets the resource data of a specific resource by index.
1240 * The valid range of the parameter uiResTypeIndex is 0...getNumberOfResourceTypes() - 1.
1241 * The valid range of the parameter uiResIndex is 0...getNumberOfResources() - 1.
1242 * Leaving the invalid range leads to undefined behaviour.
1243 * @param uiResTypeIndex Identifies the resource type of the resource.
1244 * @param uiResIndex Identifies the resource.
1245 * @param data Vector where the data is stored.
1246 **/
1247 void ResourceDirectory::getResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector<byte>& data) const
1248 {
1249 ResourceNode* currNode = static_cast<ResourceNode*>(m_rnRoot.children[uiResTypeIndex].child);
1250 currNode = static_cast<ResourceNode*>(currNode->children[uiResIndex].child);
1251 ResourceLeaf* currLeaf = static_cast<ResourceLeaf*>(currNode->children[0].child);
1252
1253 data.assign(currLeaf->m_data.begin(), currLeaf->m_data.end());
1254 }
1255
1256 /**
1257 * Sets the resource data of a specific resource.
1258 * @param dwResTypeId Identifies the resource type of the resource.
1259 * @param dwResId Identifies the resource.
1260 * @param data The new resource data.
1261 **/
1262 void ResourceDirectory::setResourceData(dword dwResTypeId, dword dwResId, std::vector<byte>& data)
1263 {
1264 setResourceDataT(dwResTypeId, dwResId, data);
1265 }
1266
1267 /**
1268 * Sets the resource data of a specific resource.
1269 * @param dwResTypeId Identifies the resource type of the resource.
1270 * @param strResName Identifies the resource.
1271 * @param data The new resource data.
1272 **/
1273 void ResourceDirectory::setResourceData(dword dwResTypeId, const std::string& strResName, std::vector<byte>& data)
1274 {
1275 setResourceDataT(dwResTypeId, strResName, data);
1276 }
1277
1278 /**
1279 * Sets the resource data of a specific resource.
1280 * @param strResTypeName Identifies the resource type of the resource.
1281 * @param dwResId Identifies the resource.
1282 * @param data The new resource data.
1283 **/
1284 void ResourceDirectory::setResourceData(const std::string& strResTypeName, dword dwResId, std::vector<byte>& data)
1285 {
1286 setResourceDataT(strResTypeName, dwResId, data);
1287 }
1288
1289 /**
1290 * Sets the resource data of a specific resource.
1291 * @param strResTypeName Identifies the resource type of the resource.
1292 * @param strResName Identifies the resource.
1293 * @param data The new resource data.
1294 **/
1295 void ResourceDirectory::setResourceData(const std::string& strResTypeName, const std::string& strResName, std::vector<byte>& data)
1296 {
1297 setResourceDataT(strResTypeName, strResName, data);
1298 }
1299
1300 /**
1301 * Sets the resource data of a specific resource by index.
1302 * The valid range of the parameter uiResTypeIndex is 0...getNumberOfResourceTypes() - 1.
1303 * The valid range of the parameter uiResIndex is 0...getNumberOfResources() - 1.
1304 * Leaving the invalid range leads to undefined behaviour.
1305 * @param uiResTypeIndex Identifies the resource type of the resource.
1306 * @param uiResIndex Identifies the resource.
1307 * @param data The new resource data.
1308 **/
1309 void ResourceDirectory::setResourceDataByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, std::vector<byte>& data)
1310 {
1311 ResourceNode* currNode = static_cast<ResourceNode*>(m_rnRoot.children[uiResTypeIndex].child);
1312 currNode = static_cast<ResourceNode*>(currNode->children[uiResIndex].child);
1313 ResourceLeaf* currLeaf = static_cast<ResourceLeaf*>(currNode->children[0].child);
1314 currLeaf->m_data.assign(data.begin(), data.end());
1315 }
1316
1317 /**
1318 * Gets the ID of a specific resource.
1319 * @param dwResTypeId Identifies the resource type of the resource.
1320 * @param strResName Identifies the resource.
1321 * @return ID of the specified resource.
1322 **/
1323 dword ResourceDirectory::getResourceId(dword dwResTypeId, const std::string& strResName) const
1324 {
1325 return getResourceIdT(dwResTypeId, strResName);
1326 }
1327
1328 /**
1329 * Gets the ID of a specific resource.
1330 * @param strResTypeName Identifies the resource type of the resource.
1331 * @param strResName Identifies the resource.
1332 * @return ID of the specified resource.
1333 **/
1334 dword ResourceDirectory::getResourceId(const std::string& strResTypeName, const std::string& strResName) const
1335 {
1336 return getResourceIdT(strResTypeName, strResName);
1337 }
1338
1339 /**
1340 * Gets the ID of a specific resource by index.
1341 * @param uiResTypeIndex Identifies the resource type of the resource.
1342 * @param uiResIndex Identifies the resource.
1343 * @return ID of the specified resource.
1344 **/
1345 dword ResourceDirectory::getResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const
1346 {
1347 ResourceNode* currNode = static_cast<ResourceNode*>(m_rnRoot.children[uiResTypeIndex].child);
1348 return currNode->children[uiResIndex].entry.irde.Name;
1349 }
1350
1351 /**
1352 * Sets the ID of a specific resource.
1353 * @param dwResTypeId Identifies the resource type of the resource.
1354 * @param dwResId Identifies the resource.
1355 * @param dwNewResId New ID of the resource.
1356 **/
1357 void ResourceDirectory::setResourceId(dword dwResTypeId, dword dwResId, dword dwNewResId)
1358 {
1359 setResourceIdT(dwResTypeId, dwResId, dwNewResId);
1360 }
1361
1362 /**
1363 * Sets the ID of a specific resource.
1364 * @param dwResTypeId Identifies the resource type of the resource.
1365 * @param strResName Identifies the resource.
1366 * @param dwNewResId New ID of the resource.
1367 **/
1368 void ResourceDirectory::setResourceId(dword dwResTypeId, const std::string& strResName, dword dwNewResId)
1369 {
1370 setResourceIdT(dwResTypeId, strResName, dwNewResId);
1371 }
1372
1373 /**
1374 * Sets the ID of a specific resource.
1375 * @param strResTypeName Identifies the resource type of the resource.
1376 * @param dwResId Identifies the resource.
1377 * @param dwNewResId New ID of the resource.
1378 **/
1379 void ResourceDirectory::setResourceId(const std::string& strResTypeName, dword dwResId, dword dwNewResId)
1380 {
1381 setResourceIdT(strResTypeName, dwResId, dwNewResId);
1382 }
1383
1384 /**
1385 * Sets the ID of a specific resource.
1386 * @param strResTypeName Identifies the resource type of the resource.
1387 * @param strResName Identifies the resource.
1388 * @param dwNewResId New ID of the resource.
1389 **/
1390 void ResourceDirectory::setResourceId(const std::string& strResTypeName, const std::string& strResName, dword dwNewResId)
1391 {
1392 setResourceIdT(strResTypeName, strResName, dwNewResId);
1393 }
1394
1395 /**
1396 * Sets the ID of a specific resource by index.
1397 * @param uiResTypeIndex Identifies the resource type of the resource.
1398 * @param uiResIndex Identifies the resource.
1399 * @param dwNewResId New ID of the specified resource.
1400 **/
1401 void ResourceDirectory::setResourceIdByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, dword dwNewResId)
1402 {
1403 ResourceNode* currNode = static_cast<ResourceNode*>(m_rnRoot.children[uiResTypeIndex].child);
1404 currNode->children[uiResIndex].entry.irde.Name = dwNewResId;
1405 }
1406
1407 /**
1408 * Gets the Name of a specific resource.
1409 * @param dwResTypeId Identifies the resource type of the resource.
1410 * @param dwResId Identifies the resource.
1411 * @return Name of the specified resource.
1412 **/
1413 std::string ResourceDirectory::getResourceName(dword dwResTypeId, dword dwResId) const
1414 {
1415 return getResourceNameT(dwResTypeId, dwResId);
1416 }
1417
1418 /**
1419 * Gets the Name of a specific resource.
1420 * @param strResTypeName Identifies the resource type of the resource.
1421 * @param dwResId Identifies the resource.
1422 * @return Name of the specified resource.
1423 **/
1424 std::string ResourceDirectory::getResourceName(const std::string& strResTypeName, dword dwResId) const
1425 {
1426 return getResourceNameT(strResTypeName, dwResId);
1427 }
1428
1429 /**
1430 * Gets the name of a specific resource by index.
1431 * @param uiResTypeIndex Identifies the resource type of the resource.
1432 * @param uiResIndex Identifies the resource.
1433 * @return Name of the specified resource.
1434 **/
1435 std::string ResourceDirectory::getResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex) const
1436 {
1437 ResourceNode* currNode = static_cast<ResourceNode*>(m_rnRoot.children[uiResTypeIndex].child);
1438 return currNode->children[uiResIndex].entry.wstrName;
1439 }
1440
1441 /**
1442 * Sets the name of a specific resource.
1443 * @param dwResTypeId Identifies the resource type of the resource.
1444 * @param dwResId Identifies the resource.
1445 * @param strNewResName New name of the specified resource.
1446 **/
1447 void ResourceDirectory::setResourceName(dword dwResTypeId, dword dwResId, const std::string& strNewResName)
1448 {
1449 setResourceNameT(dwResTypeId, dwResId, strNewResName);
1450 }
1451
1452 /**
1453 * Sets the name of a specific resource.
1454 * @param dwResTypeId Identifies the resource type of the resource.
1455 * @param strResName Identifies the resource.
1456 * @param strNewResName New name of the specified resource.
1457 **/
1458 void ResourceDirectory::setResourceName(dword dwResTypeId, const std::string& strResName, const std::string& strNewResName)
1459 {
1460 setResourceNameT(dwResTypeId, strResName, strNewResName);
1461 }
1462
1463 /**
1464 * Sets the name of a specific resource.
1465 * @param strResTypeName Identifies the resource type of the resource.
1466 * @param dwResId Identifies the resource.
1467 * @param strNewResName New name of the specified resource.
1468 **/
1469 void ResourceDirectory::setResourceName(const std::string& strResTypeName, dword dwResId, const std::string& strNewResName)
1470 {
1471 setResourceNameT(strResTypeName, dwResId, strNewResName);
1472 }
1473
1474 /**
1475 * Sets the name of a specific resource.
1476 * @param strResTypeName Identifies the resource type of the resource.
1477 * @param strResName Identifies the resource.
1478 * @param strNewResName New name of the specified resource.
1479 **/
1480 void ResourceDirectory::setResourceName(const std::string& strResTypeName, const std::string& strResName, const std::string& strNewResName)
1481 {
1482 setResourceNameT(strResTypeName, strResName, strNewResName);
1483 }
1484
1485 /**
1486 * Sets the name of a specific resource by index.
1487 * @param uiResTypeIndex Identifies the resource type of the resource.
1488 * @param uiResIndex Identifies the resource.
1489 * @param strNewResName New name of the specified resource.
1490 **/
1491 void ResourceDirectory::setResourceNameByIndex(unsigned int uiResTypeIndex, unsigned int uiResIndex, const std::string& strNewResName)
1492 {
1493 ResourceNode* currNode = static_cast<ResourceNode*>(m_rnRoot.children[uiResTypeIndex].child);
1494 currNode->children[uiResIndex].entry.wstrName = strNewResName;
1495 }
1496
1497}