BearParser
Portable Executable parsing library (from PE-bear)
Loading...
Searching...
No Matches
Util.h
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <string.h>
5#include <stdlib.h>
6#include <stdio.h>
7#include <QtCore>
8#include "win_hdrs/win_types.h"
9
10#define DEFAULT_BUFSIZE 0xFF
11#define IS_PRINTABLE(c) (c >= 0x20 && c < 0x7f)
12#define IS_ENDLINE(c) (c == 0x0A || c == 0xD)
13
14//----
15#ifdef _DEBUG
16#define DBG_LVL 2
17#else
18#define DBG_LVL 0
19#endif
20
21#define TRACE() if (DBG_LVL) printf(">%s line: %d [%s]\n", __FUNCTION__, __LINE__, __FILE__);
22#define LOG(msg) if (DBG_LVL) printf("%s: %s\n", __FUNCTION__,msg);
23
24namespace Logger {
28 bool append(dbg_level lvl, const char* format, ...);
29};
30//----
31namespace pe_util {
32 inline bool isPrintable(char c) { return IS_PRINTABLE(c); }
33
34 bool isStrLonger(const char *inp, size_t maxLen);
35 //QString getString(const char *ptr, size_t maxInp, size_t maxBuf = DEFAULT_BUFSIZE);
36 bool hasNonPrintable(const char *ptr, size_t maxInp);
37 size_t getAsciiLen(const char *ptr, size_t maxCount, bool acceptNotTerminated = false);
38 size_t getAsciiLenW(const WORD *ptr, size_t maxCount, bool acceptNotTerminated = false);
39
40 size_t noWhiteCount(char *buf, size_t bufSize);
41 size_t noWhiteCount(std::string);
42 bool validateFuncName(const char* fPtr, size_t bufSize);
43 size_t forwarderNameLen(const char *ptr, size_t max_len);
44
45 void hexdump(BYTE *buf, size_t bufSize, size_t pad);
46
47 inline size_t unitsCount(uint64_t value, uint64_t unit, bool roundup = true)
48 {
49 if (unit == 0) return 0;
50 size_t units = value / unit;
51 if (roundup) {
52 if (value % unit) units++;
53 }
54 return units;
55 }
56
57 inline uint64_t roundup(uint64_t value, uint64_t unit)
58 {
59 const size_t units = unitsCount(value, unit);
60 return units * unit;
61 }
62
63 bool isSpaceClear(void* ptr, uint64_t size);
64 bool isHexChar(char c);
65
66 bool endsWith(std::string string, std::string endStr);
67};
68
#define IS_PRINTABLE(c)
Definition Util.h:11
Definition Util.h:24
bool append(dbg_level lvl, const char *format,...)
Definition Util.cpp:8
dbg_level
Definition Util.h:25
@ D_LVL_COUNT
Definition Util.h:26
@ D_ERROR
Definition Util.h:26
@ D_WARNING
Definition Util.h:26
@ D_INFO
Definition Util.h:26
Definition Util.h:31
bool hasNonPrintable(const char *ptr, size_t maxInp)
Definition Util.cpp:70
size_t forwarderNameLen(const char *ptr, size_t max_len)
Definition Util.cpp:116
size_t getAsciiLen(const char *ptr, size_t maxCount, bool acceptNotTerminated=false)
Definition Util.cpp:46
size_t unitsCount(uint64_t value, uint64_t unit, bool roundup=true)
Definition Util.h:47
bool isStrLonger(const char *inp, size_t maxLen)
Definition Util.cpp:38
size_t noWhiteCount(char *buf, size_t bufSize)
Definition Util.cpp:139
bool validateFuncName(const char *fPtr, size_t bufSize)
Definition Util.cpp:102
void hexdump(BYTE *buf, size_t bufSize, size_t pad)
Definition Util.cpp:180
bool isSpaceClear(void *ptr, uint64_t size)
Definition Util.cpp:161
uint64_t roundup(uint64_t value, uint64_t unit)
Definition Util.h:57
bool isPrintable(char c)
Definition Util.h:32
size_t getAsciiLenW(const WORD *ptr, size_t maxCount, bool acceptNotTerminated=false)
Definition Util.cpp:58
bool isHexChar(char c)
Definition Util.cpp:172
bool endsWith(std::string string, std::string endStr)
Definition Util.cpp:191