1 // SoftEther VPN Source Code - Developer Edition Master Branch
2 // Mayaqua Kernel
3 
4 
5 // Memory.h
6 // Header of Memory.c
7 
8 #ifndef	MEMORY_H
9 #define	MEMORY_H
10 
11 #include "MayaType.h"
12 
13 // MallocFast (not implemented)
14 #define	MallocFast		Malloc
15 #define	ZeroMallocFast	ZeroMalloc
16 
17 // Memory size that can be passed to the kernel at a time
18 #define	MAX_SEND_BUF_MEM_SIZE				(10 * 1024 * 1024)
19 
20 // The magic number for memory tag
21 #define	MEMTAG_MAGIC						0x49414449
22 
23 #define	CALC_MALLOCSIZE(size)				((MAX(size, 1)) + sizeof(MEMTAG))
24 #define	MEMTAG_TO_POINTER(p)				((void *)(((UCHAR *)(p)) + sizeof(MEMTAG)))
25 #define	POINTER_TO_MEMTAG(p)				((MEMTAG *)(((UCHAR *)(p)) - sizeof(MEMTAG)))
26 #define	IS_NULL_POINTER(p)					(((p) == NULL) || ((POINTER_TO_UINT64(p) == (UINT64)sizeof(MEMTAG))))
27 #define	PTR_TO_PTR(p)						((void **)(&p))
28 
29 // Fixed size of a block of memory pool
30 #define	MEMPOOL_MAX_SIZE					3000
31 
32 
33 // Memory tag
34 struct MEMTAG
35 {
36 	UINT Magic;
37 	UINT Size;
38 	bool ZeroFree;
39 	UINT Padding;
40 };
41 
42 // Buffer
43 struct BUF
44 {
45 	void *Buf;
46 	UINT Size;
47 	UINT SizeReserved;
48 	UINT Current;
49 };
50 
51 // FIFO
52 struct FIFO
53 {
54 	REF *ref;
55 	LOCK *lock;
56 	void *p;
57 	UINT pos, size, memsize;
58 	UINT64 total_read_size;
59 	UINT64 total_write_size;
60 	bool fixed;
61 };
62 
63 // List
64 struct LIST
65 {
66 	REF *ref;
67 	UINT num_item, num_reserved;
68 	void **p;
69 	LOCK *lock;
70 	COMPARE *cmp;
71 	bool sorted;
72 	UINT64 Param1;
73 };
74 
75 // Queue
76 struct QUEUE
77 {
78 	REF *ref;
79 	UINT num_item;
80 	FIFO *fifo;
81 	LOCK *lock;
82 };
83 
84 // Stack
85 struct SK
86 {
87 	REF *ref;
88 	UINT num_item, num_reserved;
89 	void **p;
90 	LOCK *lock;
91 	bool no_compact;
92 };
93 
94 // Candidate list
95 struct CANDIDATE
96 {
97 	wchar_t *Str;						// String
98 	UINT64 LastSelectedTime;			// Date and time last selected
99 };
100 
101 struct STRMAP_ENTRY
102 {
103 	char *Name;
104 	void *Value;
105 };
106 
107 // Shared buffer
108 struct SHARED_BUFFER
109 {
110 	REF *Ref;
111 	void *Data;
112 	UINT Size;
113 };
114 
115 // Macro
116 #define	LIST_DATA(o, i)		(((o) != NULL) ? ((o)->p[(i)]) : NULL)
117 #define	LIST_NUM(o)			(((o) != NULL) ? (o)->num_item : 0)
118 #define	HASH_LIST_NUM(o)	(((o) != NULL) ? (o)->NumItems : 0)
119 
120 // Function pointer type to get a hash function
121 typedef UINT (GET_HASH)(void *p);
122 
123 // Hash list
124 struct HASH_LIST
125 {
126 	UINT Bits;
127 	UINT Size;
128 	GET_HASH *GetHashProc;
129 	COMPARE *CompareProc;
130 	LOCK *Lock;
131 	REF *Ref;
132 	LIST **Entries;
133 	UINT NumItems;
134 	LIST *AllList;
135 };
136 
137 // PRAND
138 struct PRAND
139 {
140 	UCHAR Key[20];
141 	CRYPT *Rc4;
142 };
143 
144 // Function prototype
145 HASH_LIST *NewHashList(GET_HASH *get_hash_proc, COMPARE *compare_proc, UINT bits, bool make_list);
146 void ReleaseHashList(HASH_LIST *h);
147 void CleanupHashList(HASH_LIST *h);
148 void AddHash(HASH_LIST *h, void *p);
149 bool DeleteHash(HASH_LIST *h, void *p);
150 void *SearchHash(HASH_LIST *h, void *t);
151 UINT CalcHashForHashList(HASH_LIST *h, void *p);
152 void **HashListToArray(HASH_LIST *h, UINT *num);
153 void LockHashList(HASH_LIST *h);
154 void UnlockHashList(HASH_LIST *h);
155 bool IsInHashListKey(HASH_LIST *h, UINT key);
156 void *HashListKeyToPointer(HASH_LIST *h, UINT key);
157 
158 PRAND *NewPRand(void *key, UINT key_size);
159 void FreePRand(PRAND *r);
160 void PRand(PRAND *p, void *data, UINT size);
161 UINT PRandInt(PRAND *p);
162 
163 LIST *NewCandidateList();
164 void FreeCandidateList(LIST *o);
165 int CompareCandidate(void *p1, void *p2);
166 void AddCandidate(LIST *o, wchar_t *str, UINT num_max);
167 BUF *CandidateToBuf(LIST *o);
168 LIST *BufToCandidate(BUF *b);
169 
170 void *Malloc(UINT size);
171 void *MallocEx(UINT size, bool zero_clear_when_free);
172 void *ZeroMalloc(UINT size);
173 void *ZeroMallocEx(UINT size, bool zero_clear_when_free);
174 void *ReAlloc(void *addr, UINT size);
175 void Free(void *addr);
176 void FreeSafe(void **addr);
177 void CheckMemTag(MEMTAG *tag);
178 UINT GetMemSize(void *addr);
179 
180 void *InternalMalloc(UINT size);
181 void *InternalReAlloc(void *addr, UINT size);
182 void InternalFree(void *addr);
183 
184 void Copy(void *dst, void *src, UINT size);
185 void Move(void *dst, void *src, UINT size);
186 int Cmp(void *p1, void *p2, UINT size);
187 int CmpCaseIgnore(void *p1, void *p2, UINT size);
188 void ZeroMem(void *addr, UINT size);
189 void Zero(void *addr, UINT size);
190 void *Clone(void *addr, UINT size);
191 void *AddHead(void *src, UINT src_size, void *head, UINT head_size);
192 
193 char B64_CodeToChar(BYTE c);
194 char B64_CharToCode(char c);
195 int B64_Encode(char *set, char *source, int len);
196 int B64_Decode(char *set, char *source, int len);
197 UINT Encode64(char *dst, char *src);
198 UINT Decode64(char *dst, char *src);
199 
200 USHORT Swap16(USHORT value);
201 UINT Swap32(UINT value);
202 UINT64 Swap64(UINT64 value);
203 USHORT Endian16(USHORT src);
204 UINT Endian32(UINT src);
205 UINT64 Endian64(UINT64 src);
206 void EndianUnicode(wchar_t *str);
207 
208 BUF *NewBuf();
209 BUF *NewBufFromMemory(void *buf, UINT size);
210 void ClearBuf(BUF *b);
211 void WriteBuf(BUF *b, void *buf, UINT size);
212 void WriteBufBuf(BUF *b, BUF *bb);
213 void WriteBufBufWithOffset(BUF *b, BUF *bb);
214 UINT ReadBuf(BUF *b, void *buf, UINT size);
215 bool BufSkipUtf8Bom(BUF *b);
216 BUF *ReadBufFromBuf(BUF *b, UINT size);
217 void AdjustBufSize(BUF *b, UINT new_size);
218 void SeekBuf(BUF *b, UINT offset, int mode);
219 void SeekBufToEnd(BUF *b);
220 void SeekBufToBegin(BUF *b);
221 void FreeBuf(BUF *b);
222 bool BufToFile(IO *o, BUF *b);
223 BUF *FileToBuf(IO *o);
224 UINT ReadBufInt(BUF *b);
225 USHORT ReadBufShort(BUF *b);
226 UINT64 ReadBufInt64(BUF *b);
227 UCHAR ReadBufChar(BUF *b);
228 bool WriteBufInt(BUF *b, UINT value);
229 bool WriteBufInt64(BUF *b, UINT64 value);
230 bool WriteBufChar(BUF *b, UCHAR uc);
231 bool WriteBufShort(BUF *b, USHORT value);
232 bool ReadBufStr(BUF *b, char *str, UINT size);
233 bool WriteBufStr(BUF *b, char *str);
234 void WriteBufLine(BUF *b, char *str);
235 void AddBufStr(BUF *b, char *str);
236 bool DumpBuf(BUF *b, char *filename);
237 bool DumpBufW(BUF *b, wchar_t *filename);
238 bool DumpBufWIfNecessary(BUF *b, wchar_t *filename);
239 bool DumpDataW(void *data, UINT size, wchar_t *filename);
240 BUF *ReadDump(char *filename);
241 BUF *ReadDumpWithMaxSize(char *filename, UINT max_size);
242 BUF *ReadDumpW(wchar_t *filename);
243 BUF *ReadDumpExW(wchar_t *filename, bool read_lock);
244 BUF *CloneBuf(BUF *b);
245 BUF *MemToBuf(void *data, UINT size);
246 BUF *RandBuf(UINT size);
247 BUF *ReadRemainBuf(BUF *b);
248 UINT ReadBufRemainSize(BUF *b);
249 bool CompareBuf(BUF *b1, BUF *b2);
250 
251 UINT ReadFifo(FIFO *f, void *p, UINT size);
252 BUF *ReadFifoAll(FIFO *f);
253 void ShrinkFifoMemory(FIFO *f);
254 UCHAR *GetFifoPointer(FIFO *f);
255 UCHAR *FifoPtr(FIFO *f);
256 void WriteFifo(FIFO *f, void *p, UINT size);
257 UINT FifoSize(FIFO *f);
258 void ReleaseFifo(FIFO *f);
259 void CleanupFifo(FIFO *f);
260 FIFO *NewFifo();
261 FIFO *NewFifoFast();
262 FIFO *NewFifoEx(bool fast);
263 FIFO *NewFifoEx2(bool fast, bool fixed);
264 void InitFifo();
265 void SetFifoCurrentReallocMemSize(UINT size);
266 
267 void *Search(LIST *o, void *target);
268 void Sort(LIST *o);
269 void Add(LIST *o, void *p);
270 void AddDistinct(LIST *o, void *p);
271 void Insert(LIST *o, void *p);
272 bool Delete(LIST *o, void *p);
273 void DeleteAll(LIST *o);
274 void LockList(LIST *o);
275 void UnlockList(LIST *o);
276 void ReleaseList(LIST *o);
277 void CleanupList(LIST *o);
278 LIST *NewList(COMPARE *cmp);
279 LIST *NewListFast(COMPARE *cmp);
280 LIST *NewListEx(COMPARE *cmp, bool fast);
281 LIST *NewListEx2(COMPARE *cmp, bool fast, bool fast_malloc);
282 LIST *NewListSingle(void *p);
283 LIST *NewEntryList(char *src, char *key_separator, char *value_separator);
284 bool EntryListHasKey(LIST *o, char *key);
285 char *EntryListStrValue(LIST *o, char *key);
286 UINT EntryListIntValue(LIST *o, char *key);
287 void FreeEntryList(LIST *o);
288 LIST *CloneList(LIST *o);
289 void CopyToArray(LIST *o, void *p);
290 void *ToArray(LIST *o);
291 void *ToArrayEx(LIST *o, bool fast);
292 int CompareStr(void *p1, void *p2);
293 bool InsertStr(LIST *o, char *str);
294 int CompareUniStr(void *p1, void *p2);
295 bool IsInList(LIST *o, void *p);
296 bool IsInListKey(LIST *o, UINT key);
297 void *ListKeyToPointer(LIST *o, UINT key);
298 bool IsInListStr(LIST *o, char *str);
299 bool IsInListUniStr(LIST *o, wchar_t *str);
300 bool ReplaceListPointer(LIST *o, void *oldptr, void *newptr);
301 void AddInt(LIST *o, UINT i);
302 void AddInt64(LIST *o, UINT64 i);
303 void AddIntDistinct(LIST *o, UINT i);
304 void AddInt64Distinct(LIST *o, UINT64 i);
305 void DelInt(LIST *o, UINT i);
306 void ReleaseIntList(LIST *o);
307 void ReleaseInt64List(LIST *o);
308 bool IsIntInList(LIST *o, UINT i);
309 bool IsInt64InList(LIST *o, UINT64 i);
310 LIST *NewIntList(bool sorted);
311 LIST *NewInt64List(bool sorted);
312 int CompareInt(void *p1, void *p2);
313 int CompareInt64(void *p1, void *p2);
314 void InsertInt(LIST *o, UINT i);
315 void InsertIntDistinct(LIST *o, UINT i);
316 
317 void *GetNext(QUEUE *q);
318 void *GetNextWithLock(QUEUE *q);
319 void InsertQueue(QUEUE *q, void *p);
320 void InsertQueueWithLock(QUEUE *q, void *p);
321 void InsertQueueInt(QUEUE *q, UINT value);
322 void LockQueue(QUEUE *q);
323 void UnlockQueue(QUEUE *q);
324 void ReleaseQueue(QUEUE *q);
325 void CleanupQueue(QUEUE *q);
326 QUEUE *NewQueue();
327 QUEUE *NewQueueFast();
328 UINT GetQueueNum(QUEUE *q);
329 
330 SK *NewSk();
331 SK *NewSkEx(bool no_compact);
332 void ReleaseSk(SK *s);
333 void CleanupSk(SK *s);
334 void LockSk(SK *s);
335 void UnlockSk(SK *s);
336 void Push(SK *s, void *p);
337 void *Pop(SK *s);
338 
339 UINT Uncompress(void *dst, UINT dst_size, void *src, UINT src_size);
340 UINT Compress(void *dst, UINT dst_size, void *src, UINT src_size);
341 UINT CompressEx(void *dst, UINT dst_size, void *src, UINT src_size, UINT level);
342 UINT CalcCompress(UINT src_size);
343 BUF *CompressBuf(BUF *src_buf);
344 BUF *UncompressBuf(BUF *src_buf);
345 
346 bool IsZero(void *data, UINT size);
347 
348 LIST *NewStrMap();
349 void *StrMapSearch(LIST *map, char *key);
350 
351 UINT SearchBin(void *data, UINT data_start, UINT data_size, void *key, UINT key_size);
352 void CrashNow();
353 UINT Power(UINT a, UINT b);
354 
355 void XorData(void *dst, void *src1, void *src2, UINT size);
356 
357 SHARED_BUFFER *NewSharedBuffer(void *data, UINT size);
358 void ReleaseSharedBuffer(SHARED_BUFFER *b);
359 void CleanupSharedBuffer(SHARED_BUFFER *b);
360 
361 void AppendBufUtf8(BUF *b, wchar_t *str);
362 void AppendBufStr(BUF *b, char *str);
363 
364 LIST *NewStrList();
365 void ReleaseStrList(LIST *o);
366 bool AddStrToStrListDistinct(LIST *o, char *str);
367 
368 #endif	// MEMORY_H
369 
370