fhq-server  v0.2.33
Documentation for fhq-server. FreeHackQuest is an open source platform for competitions of computer security.
wsjcpp_core.h
Go to the documentation of this file.
1 #ifndef WSJCPP_CORE_H
2 #define WSJCPP_CORE_H
3 
4 #include <string>
5 #include <vector>
6 #include <map>
7 #include <mutex>
8 #include <deque>
9 #include <iostream>
10 
12  public:
15  bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag,
16  bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag,
17  bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag
18  );
19  WsjcppFilePermissions(uint16_t nFilePermission);
20 
21  // owner flags
22  void setOwnerReadFlag(bool bOwnerReadFlag);
23  bool getOwnerReadFlag() const;
24  void setOwnerWriteFlag(bool bOwnerWriteFlag);
25  bool getOwnerWriteFlag() const;
26  void setOwnerExecuteFlag(bool bOwnerExecuteFlag);
27  bool getOwnerExecuteFlag() const;
28  void setOwnerFlags(bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag);
29 
30  // group flags
31  void setGroupReadFlag(bool bGroupReadFlag);
32  bool getGroupReadFlag() const;
33  void setGroupWriteFlag(bool bGroupWriteFlag);
34  bool getGroupWriteFlag() const;
35  void setGroupExecuteFlag(bool bGroupExecuteFlag);
36  bool getGroupExecuteFlag() const;
37  void setGroupFlags(bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag);
38 
39  // for other flags
40  void setOtherReadFlag(bool bOtherReadFlag);
41  bool getOtherReadFlag() const;
42  void setOtherWriteFlag(bool bOtherWriteFlag);
43  bool getOtherWriteFlag() const;
44  void setOtherExecuteFlag(bool bOtherExecuteFlag);
45  bool getOtherExecuteFlag() const;
46  void setOtherFlags(bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag);
47 
48  std::string toString() const;
49  uint16_t toUInt16() const;
50 
51  private:
61 };
62 
63 class WsjcppCore {
64  public:
65  static bool init(
66  int argc, char** argv,
67  const std::string &sApplicationName,
68  const std::string &sApplicationVersion,
69  const std::string &sApplicationAuthor,
70  const std::string &sLibraryNameForExports
71  );
72 
73  static std::string doNormalizePath(const std::string &sPath);
74  static std::string extractFilename(const std::string &sPath);
75  static std::string getCurrentDirectory();
76 
77  static long getCurrentTimeInMilliseconds();
78  static long getCurrentTimeInSeconds();
79  static std::string getCurrentTimeForFilename();
80  static std::string getCurrentTimeForLogFormat();
81  static std::string getThreadId();
82  static std::string formatTimeForWeb(long nTimeInSec);
83  static std::string formatTimeForFilename(long nTimeInSec);
84  static std::string formatTimeUTC(int nTimeInSec);
85 
86  static bool dirExists(const std::string &sFilename);
87  static bool fileExists(const std::string &sFilename);
88  static std::vector<std::string> listOfDirs(const std::string &sDirname);
89  static std::vector<std::string> getListOfDirs(const std::string &sDirname);
90  static std::vector<std::string> listOfFiles(const std::string &sDirname);
91  static std::vector<std::string> getListOfFiles(const std::string &sDirname);
92  static bool makeDir(const std::string &sDirname);
93  static bool writeFile(const std::string &sFilename, const std::string &sContent);
94  static bool readTextFile(const std::string &sFilename, std::string &sOutputContent);
95  static bool readFileToBuffer(const std::string &sFilename, char *pBuffer[], int &nBufferSize);
96  static bool writeFile(const std::string &sFilename, const char *pBuffer, const int nBufferSize);
97  static bool removeFile(const std::string &sFilename);
98  static bool copyFile(const std::string &sSourceFilename, const std::string &sTargetFilename);
99 
100  static bool createEmptyFile(const std::string &sFilename);
101 
102  static std::string& ltrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
103  static std::string& rtrim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
104  static std::string& trim(std::string& str, const std::string& chars = "\t\n\v\f\r ");
105  static std::string toLower(const std::string &str);
106  static std::string toUpper(const std::string& str);
107  static void replaceAll(std::string& str, const std::string& from, const std::string& to);
108  static std::vector<std::string> split(const std::string& sWhat, const std::string& sDelim);
109  static std::string join(const std::vector<std::string> &vWhat, const std::string& sDelim);
110 
111  static void initRandom();
112  static std::string createUuid();
113  static std::string uint2hexString(unsigned int n);
114  static unsigned long convertVoidToULong(void *p);
115  static std::string getPointerAsHex(void *p);
116  static std::string extractURLProtocol(const std::string& sValue);
117  static bool getEnv(const std::string& sName, std::string& sValue);
118 
119  static std::string encodeUriComponent(const std::string& sValue);
120  static std::string decodeUriComponent(const std::string& sValue);
121 
122  static std::string getHumanSizeBytes(long nBytes);
123 
124  static bool recoursiveCopyFiles(const std::string& sSourceDir, const std::string& sTargetDir);
125  static bool recoursiveRemoveDir(const std::string& sDir);
126 
127  static bool setFilePermissions(const std::string& sFilePath, const WsjcppFilePermissions &filePermissions, std::string& sError);
128  static bool getFilePermissions(const std::string& sFilePath, WsjcppFilePermissions &filePermissions, std::string& sError);
129 
130  static std::string doPadLeft(const std::string& sIn, char cWhat, int nLength);
131  static std::string doPadRight(const std::string& sIn, char cWhat, int nLength);
132 
133 };
134 
135 
136 // ---------------------------------------------------------------------
137 
139  FG_RED = 31,
140  FG_GREEN = 32,
141  FG_YELLOW = 93,
142  FG_BLUE = 34,
144  BG_RED = 41,
145  BG_GREEN = 42,
146  BG_BLUE = 44,
148 };
149 
150 // ---------------------------------------------------------------------
151 
154  public:
155  WsjcppColorModifier(WsjcppColorCode pCode) : code(pCode) {}
156  friend std::ostream&
157  operator<<(std::ostream& os, const WsjcppColorModifier& mod) {
158  return os << "\033[" << mod.code << "m";
159  }
160 };
161 
162 // ---------------------------------------------------------------------
163 
165  public:
167  void doLogRotateUpdateFilename(bool bForce = false);
168  std::mutex logMutex;
169  std::string logDir;
170  std::string logPrefixFile;
171  std::string logFile;
175  std::deque<std::string> logLastMessages;
176 };
177 
178 class WsjcppLog {
179  public:
181 
182  static void info(const std::string &sTag, const std::string &sMessage);
183  static void err(const std::string &sTag, const std::string &sMessage);
184  static void throw_err(const std::string &sTag, const std::string &sMessage);
185  static void warn(const std::string &sTag, const std::string &sMessage);
186  static void ok(const std::string &sTag, const std::string &sMessage);
187  static std::vector<std::string> getLastLogMessages();
188  static void setLogDirectory(const std::string &sDirectoryPath);
189  static void setPrefixLogFile(const std::string &sPrefixLogFile);
190  static void setEnableLogFile(bool bEnable);
191  static void setRotationPeriodInSec(long nRotationPeriodInSec);
192 
193  private:
194  static void add(WsjcppColorModifier &clr, const std::string &sType, const std::string &sTag, const std::string &sMessage);
195 };
196 
197 // ---------------------------------------------------------------------
198 // WsjcppResourceFile
199 
201  public:
203  virtual const std::string &getFilename() const = 0;
204  virtual const std::string &getPackAs() const = 0;
205  virtual int getBufferSize() const = 0;
206  virtual const char *getBuffer() const = 0;
207 };
208 
209 
210 // ---------------------------------------------------------------------
211 // WsjcppResourcesManager
212 
213 extern std::vector<WsjcppResourceFile*> *g_pWsjcppResourceFiles;
214 
216  public:
217  static void initGlobalVariables();
218  static void add(WsjcppResourceFile*);
219  static const std::vector<WsjcppResourceFile*> &list();
220  static bool has(const std::string &sFilename);
221  static WsjcppResourceFile* get(const std::string &sFilename);
222  static bool make(const std::string &sWorkspace);
223  // static bool createFolders(const std::string &sWorkspace);
224  // static bool extractFiles(const std::string &sWorkspace);
225 };
226 
227 // ---------------------------------------------------------------------
228 // Registry WsjcppResourceFile
229 #define REGISTRY_WSJCPP_RESOURCE_FILE( classname ) \
230  static classname * pRegistryWsjcppResourceFile ## classname = new classname(); \
231 
232 
233 #endif // WSJCPP_CORE_H
234 
235 
void setGroupReadFlag(bool bGroupReadFlag)
Definition: wsjcpp_core.cpp:121
WsjcppColorModifier(WsjcppColorCode pCode)
Definition: wsjcpp_core.h:155
void setGroupWriteFlag(bool bGroupWriteFlag)
Definition: wsjcpp_core.cpp:133
bool m_bOtherWriteFlag
Definition: wsjcpp_core.h:59
bool getOtherExecuteFlag() const
Definition: wsjcpp_core.cpp:195
uint16_t toUInt16() const
Definition: wsjcpp_core.cpp:232
void setGroupFlags(bool bGroupReadFlag, bool bGroupWriteFlag, bool bGroupExecuteFlag)
Definition: wsjcpp_core.cpp:157
std::string logDir
Definition: wsjcpp_core.h:169
std::string logPrefixFile
Definition: wsjcpp_core.h:170
bool m_bOwnerWriteFlag
Definition: wsjcpp_core.h:53
Definition: wsjcpp_core.h:178
bool m_bGroupWriteFlag
Definition: wsjcpp_core.h:56
void setOwnerFlags(bool bOwnerReadFlag, bool bOwnerWriteFlag, bool bOwnerExecuteFlag)
Definition: wsjcpp_core.cpp:113
bool getOtherReadFlag() const
Definition: wsjcpp_core.cpp:171
WsjcppColorCode code
Definition: wsjcpp_core.h:153
bool m_bGroupReadFlag
Definition: wsjcpp_core.h:55
bool m_bOwnerExecuteFlag
Definition: wsjcpp_core.h:54
std::string logFile
Definition: wsjcpp_core.h:171
void setOtherWriteFlag(bool bOtherWriteFlag)
Definition: wsjcpp_core.cpp:177
long logStartTime
Definition: wsjcpp_core.h:173
Definition: wsjcpp_core.h:139
bool m_bOtherReadFlag
Definition: wsjcpp_core.h:58
Definition: wsjcpp_core.h:63
std::deque< std::string > logLastMessages
Definition: wsjcpp_core.h:175
bool getGroupReadFlag() const
Definition: wsjcpp_core.cpp:127
std::string toString() const
Definition: wsjcpp_core.cpp:209
Definition: wsjcpp_core.h:140
static WsjcppLogGlobalConf g_WSJCPP_LOG_GLOBAL_CONF
Definition: wsjcpp_core.h:180
bool enableLogFile
Definition: wsjcpp_core.h:172
bool getOwnerReadFlag() const
Definition: wsjcpp_core.cpp:83
Definition: wsjcpp_core.h:141
void setGroupExecuteFlag(bool bGroupExecuteFlag)
Definition: wsjcpp_core.cpp:145
WsjcppColorCode
Definition: wsjcpp_core.h:138
bool getOtherWriteFlag() const
Definition: wsjcpp_core.cpp:183
Definition: wsjcpp_core.h:146
std::ostream & operator<<(std::ostream &out, MD5 md5)
Definition: md5.cpp:350
std::mutex logMutex
Definition: wsjcpp_core.h:168
bool m_bOtherExecuteFlag
Definition: wsjcpp_core.h:60
bool getOwnerExecuteFlag() const
Definition: wsjcpp_core.cpp:107
bool m_bGroupExecuteFlag
Definition: wsjcpp_core.h:57
void setOwnerReadFlag(bool bOwnerReadFlag)
Definition: wsjcpp_core.cpp:77
Definition: wsjcpp_core.h:145
static void trim(std::string &s)
Definition: mail_send_task.cpp:106
bool getGroupWriteFlag() const
Definition: wsjcpp_core.cpp:139
Definition: wsjcpp_core.h:142
Definition: wsjcpp_core.h:215
static void rtrim(std::string &s)
Definition: mail_send_task.cpp:97
void setOtherReadFlag(bool bOtherReadFlag)
Definition: wsjcpp_core.cpp:165
Definition: wsjcpp_core.h:143
std::vector< WsjcppResourceFile * > * g_pWsjcppResourceFiles
Definition: wsjcpp_core.cpp:1229
static void ltrim(std::string &s)
Definition: mail_send_task.cpp:89
Definition: wsjcpp_core.h:200
Definition: wsjcpp_core.h:144
Definition: wsjcpp_core.h:11
WsjcppFilePermissions()
Definition: wsjcpp_core.cpp:28
Definition: wsjcpp_core.h:164
bool m_bOwnerReadFlag
Definition: wsjcpp_core.h:52
void setOtherFlags(bool bOtherReadFlag, bool bOtherWriteFlag, bool bOtherExecuteFlag)
Definition: wsjcpp_core.cpp:201
bool getGroupExecuteFlag() const
Definition: wsjcpp_core.cpp:151
bool getOwnerWriteFlag() const
Definition: wsjcpp_core.cpp:95
long logRotationPeriodInSeconds
Definition: wsjcpp_core.h:174
Definition: wsjcpp_core.h:147
void setOtherExecuteFlag(bool bOtherExecuteFlag)
Definition: wsjcpp_core.cpp:189
void setOwnerWriteFlag(bool bOwnerWriteFlag)
Definition: wsjcpp_core.cpp:89
void setOwnerExecuteFlag(bool bOwnerExecuteFlag)
Definition: wsjcpp_core.cpp:101
Definition: wsjcpp_core.h:152