summaryrefslogtreecommitdiffhomepage
path: root/dev/sci/sci_base.h
blob: c225c966a72ef6d0c06a5ca7152ea0a2f5406e8c (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* -------------------------------------------

Copyright ZKA Web Services Co.

File: sci_base.h
Purpose: SCI core header file (C++ only).

------------------------------------------- */

#ifndef __SCI_BASE_HXX__
#define __SCI_BASE_HXX__

#include <sci/sci_hint.h>

#define ATTRIBUTE(X) __attribute__((X))
#define IMPORT_XPCOM extern "XPCOM"
#define IMPORT_CXX	 extern "C++"
#define IMPORT_C	 extern "C"
typedef bool Bool;
typedef void Void;

typedef __UINT64_TYPE__ UInt64;
typedef __UINT32_TYPE__ UInt32;
typedef __UINT16_TYPE__ UInt16;
typedef __UINT8_TYPE__	UInt8;

typedef __SIZE_TYPE__ SizeT;

typedef __INT64_TYPE__ SInt64;
typedef __INT32_TYPE__ SInt32;
typedef __INT16_TYPE__ SInt16;
typedef __INT8_TYPE__  SInt8;

typedef void*			 VoidPtr;
typedef __UINTPTR_TYPE__ UIntPtr;
typedef char			 Char;
#include <sci/sci_lpc.h>

#ifdef __XPCOM_IMPL__
#include <sci/xpcom_core.h>
#else
class IUnknown; // Refrenced from an IDB entry.
class ICLSID;	// From the IDB, the constructor of the object, e.g: IAppCLSID.
class UUID;
class ATTRIBUTE(uuid("d7c144b6-0792-44b8-b06b-02b227b547df")) IUnknown
{
public:
	explicit IUnknown() = default;
	virtual ~IUnknown() = default;

	IUnknown& operator=(const IUnknown&) = default;
	IUnknown(const IUnknown&)			 = default;

	virtual SInt32	  Release()				   = 0;
	virtual void	  RemoveRef()			   = 0;
	virtual IUnknown* AddRef()				   = 0;
	virtual VoidPtr	  QueryClass(UUID* p_uuid) = 0;
};
template <typename FnSign, typename ClsID>
class IEventListener : public ClsID
{
	friend ClsID;

	explicit IEventListener() = default;
	virtual ~IEventListener() = default;

	IEventListener& operator=(const IEventListener&) = default;
	IEventListener(const IEventListener&)			 = default;

	virtual IEventListener& operator-=(const Char* event_name);
	virtual IEventListener& operator+=(FnSign arg) = 0;
};
#endif

// ------------------------------------------------------------------------------------------ //
/// @note Handle typedefs.
// ------------------------------------------------------------------------------------------ //

typedef VoidPtr Object;

typedef Object DLLObject;
typedef Object IOObject;
typedef Object SCMObject;
typedef Object ThreadObject;
typedef Object SocketObject;
typedef Object ShellObject;
typedef Object UIObject;

// ------------------------------------------------------------------------------------------ //

// ------------------------------------------------------------------------------------------ //
/// @note Dynamic Loader API.
// ------------------------------------------------------------------------------------------ //

/// @brief Get function which is part of the DLL.
/// @param symbol the symbol to look for
/// @param dll_handle the DLL handle.
/// @return the proc pointer.
IMPORT_C Object LdrGetDLLSymbolFromHandle(_Input const Char* symbol, _Input Object dll_handle);

/// @brief Open DLL handle.
/// @param path
/// @param drv
/// @return
IMPORT_C Object LdrOpenDLLHandle(_Input const Char* path, _Input const Char* drive_letter);

/// @brief Close DLL handle
/// @param dll_handle
/// @return
IMPORT_C Void LdrCloseDLLHandle(_Input Object* dll_handle);

// ------------------------------------------------------------------------------------------ //
// File API.
// ------------------------------------------------------------------------------------------ //

/// @brief Opens a file from a drive.
/// @param fs_path the filesystem path.
/// @param drive_letter drive name, use NULL to use default one.
/// @return the file descriptor of the file.
IMPORT_C Object IoOpenFile(const Char* fs_path, const Char* drive_letter);

/// @brief Closes a file and flushes its content.
/// @param file_desc the file descriptor.
/// @return void.
IMPORT_C Void IoCloseFile(_Input Object file_desc);

/// @brief Write data to a file.
/// @param file_desc the file descriptor.
/// @param out_data the data to write.
/// @param sz_data the size of the data to write.
/// @return the number of bytes written.
IMPORT_C UInt32 IoWriteFile(_Input Object file_desc, _Output VoidPtr out_data, SizeT sz_data);

/// @brief Read data from a file.
/// @param file_desc the file descriptor.
/// @param out_data the data to read.
/// @param sz_data the size of the data to read.
IMPORT_C UInt32 IoReadFile(_Input Object file_desc, _Output VoidPtr* out_data, SizeT sz_data);

/// @brief Rewind the file pointer to the beginning of the file.
/// @param file_desc the file descriptor.
/// @return the number of bytes read.
IMPORT_C UInt64 IoRewindFile(_Input Object file_desc);

/// @brief Tell the current position of the file pointer.
/// @param file_desc the file descriptor.
/// @return the current position of the file pointer.
IMPORT_C UInt64 IoTellFile(_Input Object file_desc);

IMPORT_C UInt64 IoSeekFile(_Input Object file_desc, UInt64 file_offset);

// ------------------------------------------------------------------------
// TLS API.
// ------------------------------------------------------------------------

/// @brief Installs the Thread Information Block and Global Information Block inside the current process.
/// @param void.
/// @return > 0 error ocurred or already present, = 0 success.
IMPORT_C UInt32 RtlTlsInstall(Void);

#ifndef __XPCOM_IMPL__

// ------------------------------------------------------------------------
// XPCOM API.
// ------------------------------------------------------------------------

/// @brief Allocate new XPCOM object.
/// @tparam TCLS the class type.
/// @tparam UCLSID UCLS factory class type.
/// @param uclsidOfCls UCLS factory class
/// @return TCLS interface
template <typename TCLS, typename UCLSID, typename... Args>
TCLS* XPCOMQueryClass(_Input UCLSID* uclsidOfCls, _Input Args&&... args);

/// @brief Release XPCOM object.
/// @tparam TCLS the class type.
/// @param cls the class to release.
/// @return status code.
template <typename TCLS>
SInt32 XPCOMReleaseClass(_Input TCLS* cls);

/// @brief Creates an XPCOM instance in the process.
/// @param handle_instance the XPCOM handle.
/// @param flags the XPCOM flags.
IMPORT_C SInt32 XPCOMCreateInstance(_Input UInt32 flags, _Output Object* handle_instance);

/// @brief Destroys an XPCOM instance of the process.
/// @param handle_instance the XPCOM handle.
IMPORT_C Void XPCOMDestroyInstance(_Input Object handle_instance);

#endif // !__XPCOM_IMPL__

// ------------------------------------------------------------------------
// Memory Management API.
// ------------------------------------------------------------------------

/// @brief Creates a new heap from the process's address space.
/// @param len the length of it.
/// @param flags the flags of it.
/// @return heap pointer.
IMPORT_C VoidPtr MmCreateHeap(_Input SizeT len, _Input UInt32 flags);

/// @brief Destroys the pointer
/// @param heap the heap itself.
/// @return void.
IMPORT_C Void MmDestroyHeap(_Input VoidPtr heap);

/// @brief Change protection flags of a memory region.
IMPORT_C Void MmSetHeapFlags(_Input VoidPtr heap, _Input UInt32 flags);

/// @brief Change protection flags of a memory region.
IMPORT_C UInt32 MmGetHeapFlags(_Input VoidPtr heap);

/// @brief Fill memory region with CRC32.
IMPORT_C UInt32 MmFillCRC32Heap(_Input VoidPtr heap);

/// @brief Copy memory region.
IMPORT_C VoidPtr MmCopyMemory(_Input VoidPtr dest, _Input VoidPtr src, _Input SizeT len);

/// @brief Fill memory region.
IMPORT_C VoidPtr MmFillMemory(_Input VoidPtr dest, _Input SizeT len, _Input UInt8 value);

// ------------------------------------------------------------------------
// Error handling API.
// ------------------------------------------------------------------------

IMPORT_C SInt32 ErrGetLastError(Void);

// ------------------------------------------------------------------------
// Threading API.
// ------------------------------------------------------------------------

/// @brief Exit the current thread.
/// @param exit_code the exit code.
IMPORT_C Void ThrExitCurrentThread(_Input SInt32 exit_code);

/// @brief Exit the main thread.
/// @param exit_code the exit code.
IMPORT_C Void ThrExitMainThread(_Input SInt32 exit_code);

/// @brief Exit a thread.
/// @param thread the thread to exit.
/// @param exit_code the exit code.
IMPORT_C Void ThrExitThread(_Input ThreadObject thread, _Input SInt32 exit_code);

typedef Void (*ThreadProc)(Void);

/// @brief Create a thread.
/// @param procedure the thread procedure.
/// @param argument_count number of arguments inside that thread.
/// @param flags Thread flags.
/// @return the thread object.
IMPORT_C ThreadObject ThrCreateThread(ThreadProc procedure, SInt32 argument_count, SInt32 flags);

/// @brief Yield the current thread.
/// @param thread the thread to yield.
IMPORT_C Void ThrExitYieldThread(Void);

/// @brief Join a thread.
/// @param thread the thread to join.
IMPORT_C Void ThrExitJoinThread(Void);

/// @brief Detach a thread.
/// @param thread the thread to detach.
IMPORT_C Void ThrExitDetachThread(Void);

// ------------------------------------------------------------------------
// Drive Management API.
// ------------------------------------------------------------------------

/// @brief Get the default drive letter.
/// @param void.
/// @return the drive letter.
IMPORT_C Char* DrvGetDefaultDriveLetter(Void);

/// @brief Get the drive letter from a path.
/// @param path the path.
/// @return the drive letter.
IMPORT_C Char* DrvGetDriveLetterFromPath(_Input const Char* path);

/// @brief Get a mounted drive from a letter.
/// @param letter the letter (A..Z).
/// @return the drive object.
IMPORT_C Object DrvGetMountedDrive(_Input const Char letter);

/// @brief Mount a drive.
/// @param path the path to mount.
/// @param letter the letter to mount.
IMPORT_C Void DrvMountDrive(_Input const Char* path, _Input const Char* letter);

/// @brief Unmount a drive.
/// @param letter the letter to unmount.
IMPORT_C Void DrvUnmountDrive(_Input const Char letter);

// ------------------------------------------------------------------------
// Event handling API, use to listen to OS specific events.
// ------------------------------------------------------------------------

/// @brief Add an event listener.
/// @param event_name the event name.
/// @param listener the listener to add.
/// @return the event listener.
IMPORT_C Void EvtAddListener(_Input const Char* event_name, _Input Object listener);

/// @brief Remove an event listener.
/// @param event_name the event name.
/// @param listener the listener to remove.
/// @return the event listener.
IMPORT_C Void EvtRemoveListener(_Input const Char* event_name, _Input Object listener);

/// @brief Dispatch an event.
/// @param event_name the event name.
/// @param event_data the event data.
/// @return the event data.
IMPORT_C VoidPtr EvtDispatchEvent(_Input const Char* event_name, _Input VoidPtr event_data);

#endif // ifndef __SCI_BASE_HXX__