summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2015-05-26 22:57:56 -0400
committermidipix <writeonce@midipix.org>2015-09-26 10:17:32 -0400
commite2475c34dd2cd7f4bbbe4b7e6ad16eb495bf452a (patch)
tree318cd24f9c905c4f55fc1f5d9b9cab45f1717be2 /src
parent239ee42e5853e09d8e5bb4b9c704f05ce9181c91 (diff)
downloadpemagine-e2475c34dd2cd7f4bbbe4b7e6ad16eb495bf452a.tar.bz2
pemagine-e2475c34dd2cd7f4bbbe4b7e6ad16eb495bf452a.tar.xz
pe_get_image_section_tbl_addr() and pe_get_image_named_section_addr(): initial commit.
Diffstat (limited to 'src')
-rw-r--r--src/headers/pe_get_image_section_tbl_addr.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/headers/pe_get_image_section_tbl_addr.c b/src/headers/pe_get_image_section_tbl_addr.c
new file mode 100644
index 0000000..01dabb5
--- /dev/null
+++ b/src/headers/pe_get_image_section_tbl_addr.c
@@ -0,0 +1,58 @@
+/*****************************************************************************/
+/* pemagination: a (virtual) tour into portable bits and executable bytes */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.PEMAGINE. */
+/*****************************************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <pemagine/pe_consts.h>
+#include <pemagine/pe_structs.h>
+#include <pemagine/pemagine.h>
+#include "pe_impl.h"
+
+pe_api
+struct pe_sec_hdr * pe_get_image_section_tbl_addr (const void * base)
+{
+ struct pe_coff_file_hdr * coff;
+ union pe_opt_hdr * opt;
+
+ if (!(coff = pe_get_image_coff_hdr_addr(base)))
+ return 0;
+
+ if (!(opt = pe_get_image_opt_hdr_addr(base)))
+ return 0;
+
+ return (struct pe_sec_hdr *)((char *)opt + *(uint16_t *)coff->size_of_opt_hdr);
+}
+
+pe_api
+struct pe_sec_hdr * pe_get_image_named_section_addr (const void * base, const char * name)
+{
+ uint16_t count;
+ struct pe_sec_hdr * hdr;
+ struct pe_coff_file_hdr*coff;
+ char * ch;
+ uint32_t len;
+ uint32_t pos;
+ uint64_t sname = 0;
+
+ if (!(hdr = pe_get_image_section_tbl_addr(base)))
+ return 0;
+
+ coff = pe_get_image_coff_hdr_addr(base);
+ count = *(uint16_t *)coff->num_of_sections;
+
+ if ((len = pe_impl_strlen_ansi(name)) > 8) {
+ /* todo: long name support */
+ return 0;
+ } else {
+ for (pos=0, ch=(char *)&sname; pos<len; pos++)
+ ch[pos] = name[pos];
+
+ for (; count; hdr++,count--)
+ if (*(uint64_t *)hdr->name == sname)
+ return hdr;
+ }
+
+ return 0;
+}