function_name
stringlengths
1
80
function
stringlengths
14
10.6k
comment
stringlengths
12
8.02k
normalized_comment
stringlengths
6
6.55k
CleanupFifo
void CleanupFifo(FIFO *f) { // Validate arguments if (f == NULL) { return; } DeleteLock(f->lock); Free(f->p); Free(f); // KS KS_INC(KS_FREEFIFO_COUNT); }
/* Clean-up the FIFO*/
Clean-up the FIFO
InitFifo
void InitFifo() { fifo_current_realloc_mem_size = FIFO_REALLOC_MEM_SIZE; }
/* Initialize the FIFO system*/
Initialize the FIFO system
NewFifo
FIFO *NewFifo() { return NewFifoEx(false); }
/* Create a FIFO*/
Create a FIFO
SetFifoCurrentReallocMemSize
void SetFifoCurrentReallocMemSize(UINT size) { if (size == 0) { size = FIFO_REALLOC_MEM_SIZE; } fifo_current_realloc_mem_size = size; }
/* Set the default memory reclaiming size of the FIFO*/
Set the default memory reclaiming size of the FIFO
ReadDump
BUF *ReadDump(char *filename) { return ReadDumpWithMaxSize(filename, 0); }
/* Read a dump file into a buffer*/
Read a dump file into a buffer
DumpDataW
bool DumpDataW(void *data, UINT size, wchar_t *filename) { IO *o; // Validate arguments if (filename == NULL || (size != 0 && data == NULL)) { return false; } o = FileCreateW(filename); if (o == NULL) { return false; } FileWrite(o, data, size); FileClose(o); return true; }
/* Write down the data*/
Write down the data
DumpBuf
bool DumpBuf(BUF *b, char *filename) { IO *o; // Validate arguments if (b == NULL || filename == NULL) { return false; } o = FileCreate(filename); if (o == NULL) { return false; } FileWrite(o, b->Buf, b->Size); FileClose(o); return true; }
/* Dump the contents of the buffer to the file*/
Dump the contents of the buffer to the file
DumpBufWIfNecessary
bool DumpBufWIfNecessary(BUF *b, wchar_t *filename) { BUF *now; bool need = true; // Validate arguments if (b == NULL || filename == NULL) { return false; } now = ReadDumpW(filename); if (now != NULL) { if (CompareBuf(now, b)) { need = false; } FreeBuf(now); } if (need == false) { return true; } else { return DumpBufW(b, filename); } }
/* Write to the file only if the contents of the file is different*/
Write to the file only if the contents of the file is different
BufToFile
bool BufToFile(IO *o, BUF *b) { UCHAR hash[MD5_SIZE]; UINT size; // Validate arguments if (o == NULL || b == NULL) { return false; } // Hash the data Md5(hash, b->Buf, b->Size); size = Endian32(b->Size); // Write the size if (FileWrite(o, &size, sizeof(size)) == false) { return false; } // Write a hash if (FileWrite(o, hash, sizeof(hash)) == false) { return false; } // Write the data if (FileWrite(o, b->Buf, b->Size) == false) { return false; } return true; }
/* Write the buffer to a file*/
Write the buffer to a file
NewBufFromMemory
BUF *NewBufFromMemory(void *buf, UINT size) { BUF *b; // Validate arguments if (buf == NULL && size != 0) { return NULL; } b = NewBuf(); WriteBuf(b, buf, size); SeekBufToBegin(b); return b; }
/* Create a buffer from memory*/
Create a buffer from memory
NewBuf
BUF *NewBuf() { BUF *b; // Memory allocation b = Malloc(sizeof(BUF)); b->Buf = Malloc(INIT_BUF_SIZE); b->Size = 0; b->Current = 0; b->SizeReserved = INIT_BUF_SIZE; // KS KS_INC(KS_NEWBUF_COUNT); KS_INC(KS_CURRENT_BUF_COUNT); return b; }
/* Creating a buffer*/
Creating a buffer
ClearBuf
void ClearBuf(BUF *b) { // Validate arguments if (b == NULL) { return; } b->Size = 0; b->Current = 0; }
/* Clearing the buffer*/
Clearing the buffer
WriteBuf
void WriteBuf(BUF *b, void *buf, UINT size) { UINT new_size; // Validate arguments if (b == NULL || buf == NULL || size == 0) { return; } new_size = b->Current + size; if (new_size > b->Size) { // Adjust the size AdjustBufSize(b, new_size); } if (b->Buf != NULL) { Copy((UCHAR *)b->Buf + b->Current, buf, size); } b->Current += size; b->Size = new_size; // KS KS_INC(KS_WRITE_BUF_COUNT); }
/* Write to the buffer*/
Write to the buffer
AddBufStr
void AddBufStr(BUF *b, char *str) { // Validate arguments if (b == NULL || str == NULL) { return; } WriteBuf(b, str, StrLen(str)); }
/* Append a string to the buffer*/
Append a string to the buffer
WriteBufLine
void WriteBufLine(BUF *b, char *str) { char *crlf = "\r\n"; // Validate arguments if (b == NULL || str == NULL) { return; } WriteBuf(b, str, StrLen(str)); WriteBuf(b, crlf, StrLen(crlf)); }
/* Write a line to the buffer*/
Write a line to the buffer
WriteBufStr
bool WriteBufStr(BUF *b, char *str) { UINT len; // Validate arguments if (b == NULL || str == NULL) { return false; } // String length len = StrLen(str); if (WriteBufInt(b, len + 1) == false) { return false; } // String body WriteBuf(b, str, len); return true; }
/* Write a string to a buffer*/
Write a string to a buffer
WriteBufInt64
bool WriteBufInt64(BUF *b, UINT64 value) { // Validate arguments if (b == NULL) { return false; } value = Endian64(value); WriteBuf(b, &value, sizeof(UINT64)); return true; }
/* Write a 64 bit integer to the buffer*/
Write a 64 bit integer to the buffer
WriteBufInt
bool WriteBufInt(BUF *b, UINT value) { // Validate arguments if (b == NULL) { return false; } value = Endian32(value); WriteBuf(b, &value, sizeof(UINT)); return true; }
/* Write an integer in the the buffer*/
Write an integer in the the buffer
WriteBufShort
bool WriteBufShort(BUF *b, USHORT value) { // Validate arguments if (b == NULL) { return false; } value = Endian16(value); WriteBuf(b, &value, sizeof(USHORT)); return true; }
/* Write a short integer in the the buffer*/
Write a short integer in the the buffer
WriteBufChar
bool WriteBufChar(BUF *b, UCHAR uc) { // Validate arguments if (b == NULL) { return false; } WriteBuf(b, &uc, 1); return true; }
/* Write a UCHAR to the buffer*/
Write a UCHAR to the buffer
ReadBufChar
UCHAR ReadBufChar(BUF *b) { UCHAR uc; // Validate arguments if (b == NULL) { return 0; } if (ReadBuf(b, &uc, 1) != 1) { return 0; } return uc; }
/* Read a UCHAR from the buffer*/
Read a UCHAR from the buffer
ReadBufInt64
UINT64 ReadBufInt64(BUF *b) { UINT64 value; // Validate arguments if (b == NULL) { return 0; } if (ReadBuf(b, &value, sizeof(UINT64)) != sizeof(UINT64)) { return 0; } return Endian64(value); }
/* Read a 64bit integer from the buffer*/
Read a 64bit integer from the buffer
ReadBufInt
UINT ReadBufInt(BUF *b) { UINT value; // Validate arguments if (b == NULL) { return 0; } if (ReadBuf(b, &value, sizeof(UINT)) != sizeof(UINT)) { return 0; } return Endian32(value); }
/* Read an integer from the buffer*/
Read an integer from the buffer
ReadBufShort
USHORT ReadBufShort(BUF *b) { USHORT value; // Validate arguments if (b == NULL) { return 0; } if (ReadBuf(b, &value, sizeof(USHORT)) != sizeof(USHORT)) { return 0; } return Endian16(value); }
/* Read a short integer from the buffer*/
Read a short integer from the buffer
WriteBufBuf
void WriteBufBuf(BUF *b, BUF *bb) { // Validate arguments if (b == NULL || bb == NULL) { return; } WriteBuf(b, bb->Buf, bb->Size); }
/* Write the buffer to a buffer*/
Write the buffer to a buffer
WriteBufBufWithOffset
void WriteBufBufWithOffset(BUF *b, BUF *bb) { // Validate arguments if (b == NULL || bb == NULL) { return; } WriteBuf(b, ((UCHAR *)bb->Buf) + bb->Current, bb->Size - bb->Current); }
/* Write the buffer (from the offset) to a buffer*/
Write the buffer (from the offset) to a buffer
BufSkipUtf8Bom
bool BufSkipUtf8Bom(BUF *b) { if (b == NULL) { return false; } SeekBufToBegin(b); if (b->Size >= 3) { UCHAR *data = b->Buf; if (data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF) { SeekBuf(b, 3, 1); return true; } } return false; }
/* Skip UTF-8 BOM*/
Skip UTF-8 BOM
ReadBufFromBuf
BUF *ReadBufFromBuf(BUF *b, UINT size) { BUF *ret; UCHAR *data; // Validate arguments if (b == NULL) { return NULL; } data = Malloc(size); if (ReadBuf(b, data, size) != size) { Free(data); return NULL; } ret = NewBuf(); WriteBuf(ret, data, size); SeekBuf(ret, 0, 0); Free(data); return ret; }
/* Read into a buffer from the buffer*/
Read into a buffer from the buffer
AdjustBufSize
void AdjustBufSize(BUF *b, UINT new_size) { // Validate arguments if (b == NULL) { return; } if (b->SizeReserved >= new_size) { return; } while (b->SizeReserved < new_size) { b->SizeReserved = b->SizeReserved * 2; } b->Buf = ReAlloc(b->Buf, b->SizeReserved); // KS KS_INC(KS_ADJUST_BUFSIZE_COUNT); }
/* Adjusting the buffer size*/
Adjusting the buffer size
SeekBufToBegin
void SeekBufToBegin(BUF *b) { // Validate arguments if (b == NULL) { return; } SeekBuf(b, 0, 0); }
/* Seek to the beginning of the buffer*/
Seek to the beginning of the buffer
SeekBufToEnd
void SeekBufToEnd(BUF *b) { // Validate arguments if (b == NULL) { return; } SeekBuf(b, b->Size, 0); }
/* Seek to end of the buffer*/
Seek to end of the buffer
SeekBuf
void SeekBuf(BUF *b, UINT offset, int mode) { UINT new_pos; // Validate arguments if (b == NULL) { return; } if (mode == 0) { // Absolute position new_pos = offset; } else { if (mode > 0) { // Move Right new_pos = b->Current + offset; } else { // Move Left if (b->Current >= offset) { new_pos = b->Current - offset; } else { new_pos = 0; } } } b->Current = MAKESURE(new_pos, 0, b->Size); KS_INC(KS_SEEK_BUF_COUNT); }
/* Seek of the buffer*/
Seek of the buffer
FreeBuf
void FreeBuf(BUF *b) { // Validate arguments if (b == NULL) { return; } // Memory release Free(b->Buf); Free(b); // KS KS_INC(KS_FREEBUF_COUNT); KS_DEC(KS_CURRENT_BUF_COUNT); }
/* Free the buffer*/
Free the buffer
CompareBuf
bool CompareBuf(BUF *b1, BUF *b2) { // Validate arguments if (b1 == NULL && b2 == NULL) { return true; } if (b1 == NULL || b2 == NULL) { return false; } if (b1->Size != b2->Size) { return false; } if (Cmp(b1->Buf, b2->Buf, b1->Size) != 0) { return false; } return true; }
/* Compare BUFs whether two are identical*/
Compare BUFs whether two are identical
MemToBuf
BUF *MemToBuf(void *data, UINT size) { BUF *b; // Validate arguments if (data == NULL && size != 0) { return NULL; } b = NewBuf(); WriteBuf(b, data, size); SeekBuf(b, 0, 0); return b; }
/* Create a buffer from the memory area*/
Create a buffer from the memory area
RandBuf
BUF *RandBuf(UINT size) { void *data = Malloc(size); BUF *ret; Rand(data, size); ret = MemToBuf(data, size); Free(data); return ret; }
/* Creating a random number buffer*/
Creating a random number buffer
ReadRemainBuf
BUF *ReadRemainBuf(BUF *b) { UINT size; // Validate arguments if (b == NULL) { return NULL; } if (b->Size < b->Current) { return NULL; } size = b->Size - b->Current; return ReadBufFromBuf(b, size); }
/* Read the rest part of the buffer*/
Read the rest part of the buffer
ReadBufRemainSize
UINT ReadBufRemainSize(BUF *b) { // Validate arguments if (b == NULL) { return 0; } if (b->Size < b->Current) { return 0; } return b->Size - b->Current; }
/* Get the length of the rest*/
Get the length of the rest
CloneBuf
BUF *CloneBuf(BUF *b) { BUF *bb; // Validate arguments if (b == NULL) { return NULL; } bb = MemToBuf(b->Buf, b->Size); return bb; }
/* Clone the buffer*/
Clone the buffer
EndianUnicode
void EndianUnicode(wchar_t *str) { UINT i, len; // Validate arguments if (str == NULL) { return; } len = UniStrLen(str); for (i = 0;i < len;i++) { str[i] = Endian16(str[i]); } }
/* Endian conversion of Unicode string*/
Endian conversion of Unicode string
B64_CodeToChar
char B64_CodeToChar(BYTE c) { BYTE r; r = '='; if (c <= 0x19) { r = c + 'A'; } if (c >= 0x1a && c <= 0x33) { r = c - 0x1a + 'a'; } if (c >= 0x34 && c <= 0x3d) { r = c - 0x34 + '0'; } if (c == 0x3e) { r = '+'; } if (c == 0x3f) { r = '/'; } return r; }
/* Base64 : Convert a code to a character*/
Base64 : Convert a code to a character
B64_CharToCode
char B64_CharToCode(char c) { if (c >= 'A' && c <= 'Z') { return c - 'A'; } if (c >= 'a' && c <= 'z') { return c - 'a' + 0x1a; } if (c >= '0' && c <= '9') { return c - '0' + 0x34; } if (c == '+') { return 0x3e; } if (c == '/') { return 0x3f; } if (c == '=') { return -1; } return 0; }
/* Base64 : Convert a character to a code*/
Base64 : Convert a character to a code
GetMemSize
UINT GetMemSize(void *addr) { MEMTAG *tag; // Validate arguments if (IS_NULL_POINTER(addr)) { return 0; } tag = POINTER_TO_MEMTAG(addr); CheckMemTag(tag); return tag->Size; }
/* Get memory size*/
Get memory size
FreeSafe
void FreeSafe(void **addr) { Free(*addr); *addr = NULL; }
/* Free and set pointer's value to NULL*/
Free and set pointer's value to NULL
CheckMemTag
void CheckMemTag(MEMTAG *tag) { if (IsTrackingEnabled() == false) { return; } // Validate arguments if (tag == NULL) { AbortExitEx("CheckMemTag: tag == NULL"); return; } if (tag->Magic != MEMTAG_MAGIC) { AbortExitEx("CheckMemTag: tag->Magic != MEMTAG_MAGIC"); return; } }
/* Check the memtag*/
Check the memtag
AddHead
void *AddHead(void *src, UINT src_size, void *head, UINT head_size) { void *ret; UINT ret_size; // Validate arguments if ((src == NULL && src_size != 0) || (head == NULL && head_size != 0)) { return NULL; } ret_size = src_size + head_size; ret = Malloc(ret_size); Copy(ret, head, head_size); Copy(((UCHAR *)ret) + head_size, src, src_size); return ret; }
/* Add the heading space to the memory area*/
Add the heading space to the memory area
Clone
void *Clone(void *addr, UINT size) { void *ret; // Validate arguments if (addr == NULL) { return NULL; } ret = Malloc(size); Copy(ret, addr, size); return ret; }
/* Clone the memory area*/
Clone the memory area
CmpCaseIgnore
int CmpCaseIgnore(void *p1, void *p2, UINT size) { UINT i; // Validate arguments if (p1 == NULL || p2 == NULL || size == 0) { return 0; } for (i = 0;i < size;i++) { char c1 = (char)(*(((UCHAR *)p1) + i)); char c2 = (char)(*(((UCHAR *)p2) + i)); c1 = ToUpper(c1); c2 = ToUpper(c2); if (c1 != c2) { return COMPARE_RET(c1, c2); } } return 0; }
/* Memory comparison (case-insensitive)*/
Memory comparison (case-insensitive)
Zero
void Zero(void *addr, UINT size) { // Validate arguments if (addr == NULL || size == 0) { return; } // KS KS_INC(KS_ZERO_COUNT); memset(addr, 0, size); }
/* Zero-clear of memory*/
Zero-clear of memory
StrMapCmp
int StrMapCmp(void *p1, void *p2) { STRMAP_ENTRY *s1, *s2; if (p1 == NULL || p2 == NULL) { return 0; } s1 = *(STRMAP_ENTRY **)p1; s2 = *(STRMAP_ENTRY **)p2; if (s1 == NULL || s2 == NULL) { return 0; } return StrCmpi(s1->Name, s2->Name); }
/* Compare the string map entries*/
Compare the string map entries
NewStrMap
LIST *NewStrMap() { return NewList(StrMapCmp); }
/* Create a string map (the data that can be searched by the string)*/
Create a string map (the data that can be searched by the string)
StrMapSearch
void *StrMapSearch(LIST *map, char *key) { STRMAP_ENTRY tmp, *result; tmp.Name = key; result = (STRMAP_ENTRY*)Search(map, &tmp); if(result != NULL) { return result->Value; } return NULL; }
/* Search in string map*/
Search in string map
XorData
void XorData(void *dst, void *src1, void *src2, UINT size) { UINT i; UCHAR *d, *c1, *c2; // Validate arguments if (dst == NULL || src1 == NULL || src2 == NULL || size == 0) { return; } d = (UCHAR *)dst; c1 = (UCHAR *)src1; c2 = (UCHAR *)src2; for (i = 0;i < size;i++) { *d = (*c1) ^ (*c2); d++; c1++; c2++; } }
/* XOR the data*/
XOR the data
IsTrackingEnabled
bool IsTrackingEnabled() { return (IsDebug() || IsMemCheck()) && kernel_status_inited; }
/* Get whether the tracking is enabled*/
Get whether the tracking is enabled
SortObjectView
int SortObjectView(void *p1, void *p2) { TRACKING_OBJECT *o1, *o2; if (p1 == NULL || p2 == NULL) { return 0; } o1 = *(TRACKING_OBJECT **)p1; o2 = *(TRACKING_OBJECT **)p2; if (o1 == NULL || o2 == NULL) { return 0; } if (o1->Id > o2->Id) { return 1; } else if (o1->Id == o2->Id) { return 0; } return -1; }
/* Sort the objects by chronological order*/
Sort the objects by chronological order
PrintObjectInfo
void PrintObjectInfo(TRACKING_OBJECT *o) { SYSTEMTIME t; char tmp[MAX_SIZE]; // Validate arguments if (o == NULL) { return; } UINT64ToSystem(&t, o->CreatedDate); GetDateTimeStrMilli(tmp, sizeof(tmp), &t); Print(" TRACKING_OBJECT ID: %u\n" " TRACKING_OBJECT TYPE: %s\n" " ADDRESS: 0x%p\n" " TRACKING_OBJECT SIZE: %u bytes\n" " CREATED DATE: %s\n", o->Id, o->Name, UINT64_TO_POINTER(o->Address), o->Size, tmp); PrintCallStack(o->CallStack); }
/* Display the information of the object */
Display the information of the object
PrintObjectList
void PrintObjectList(TRACKING_OBJECT *o) { char tmp[MAX_SIZE]; SYSTEMTIME t; UINT64ToSystem(&t, o->CreatedDate); GetTimeStrMilli(tmp, sizeof(tmp), &t); TrackGetObjSymbolInfo(o); Print("%-4u - [%-6s] %s 0x%p size=%-5u %11s %u\n", o->Id, o->Name, tmp, UINT64_TO_POINTER(o->Address), o->Size, o->FileName, o->LineNumber); }
/* Show a Summary of the object*/
Show a Summary of the object
DebugPrintCommandList
void DebugPrintCommandList() { Print( "a - All Objects\n" "i - Object Information\n" "? - Help\n" "q - Quit\n\n" ); }
/* List of the commands*/
List of the commands
PrintMemoryStatus
void PrintMemoryStatus() { MEMORY_STATUS s; GetMemoryStatus(&s); Print("MEMORY STATUS:\n" " NUM_OF_MEMORY_BLOCKS: %u\n" " SIZE_OF_TOTAL_MEMORY: %u bytes\n", s.MemoryBlocksNum, s.MemorySize); }
/* Display the usage of the memory*/
Display the usage of the memory
TrackGetObjSymbolInfo
void TrackGetObjSymbolInfo(TRACKING_OBJECT *o) { // Validate arguments if (o == NULL) { return; } if (!(o->LineNumber == 0 && o->FileName[0] == 0)) { return; } if (o->CallStack != NULL) { GetCallStackSymbolInfo(o->CallStack); if (StrLen(o->CallStack->filename) != 0 && o->CallStack->line != 0) { StrCpy(o->FileName, sizeof(o->FileName), o->CallStack->filename); o->LineNumber = o->CallStack->line; } } }
/* Get the symbol information by the object*/
Get the symbol information by the object
TrackDeleteObj
void TrackDeleteObj(UINT64 addr) { TRACKING_OBJECT *o; // Validate arguments if (addr == 0) { return; } if ((IsTrackingEnabled() && IsMemCheck()) == false) { // Don't track in detail if the memory check option is not specified return; } LockTrackingList(); { o = SearchTrackingList(addr); if (o == NULL) { UnlockTrackingList(); Debug("TrackDeleteObj(): 0x%x not found in tracking list!\n", addr); return; } DeleteTrackingList(o, true); } UnlockTrackingList(); }
/* Remove the object from the tracking list*/
Remove the object from the tracking list
TrackChangeObjSize
void TrackChangeObjSize(UINT64 addr, UINT size, UINT64 new_addr) { TRACKING_OBJECT *o; // Validate arguments if (addr == 0) { return; } if ((IsTrackingEnabled() && IsMemCheck()) == false) { // Don't track in detail if the memory check option is not specified return; } LockTrackingList(); { o = SearchTrackingList(addr); if (o == NULL) { UnlockTrackingList(); return; } DeleteTrackingList(o, false); o->Size = size; o->Address = new_addr; InsertTrackingList(o); } UnlockTrackingList(); }
/* Change the size of the object being tracked*/
Change the size of the object being tracked
CompareTrackingObject
int CompareTrackingObject(const void *p1, const void *p2) { TRACKING_OBJECT *o1, *o2; // Validate arguments if (p1 == NULL || p2 == NULL) { return 0; } o1 = *(TRACKING_OBJECT **)p1; o2 = *(TRACKING_OBJECT **)p2; if (o1 == NULL || o2 == NULL) { return 0; } if (o1->Address > o2->Address) { return 1; } if (o1->Address == o2->Address) { return 0; } return -1; }
/* Memory address comparison function*/
Memory address comparison function
SearchTrackingList
TRACKING_OBJECT *SearchTrackingList(UINT64 Address) { UINT i; // Validate arguments if (Address == 0) { return NULL; } i = TRACKING_HASH(Address); if (hashlist[i] != NULL) { TRACKING_LIST *tt = hashlist[i]; while (true) { if (tt->Object->Address == Address) { return tt->Object; } tt = tt->Next; if (tt == NULL) { break; } } } return NULL; }
/* Search an object in the tracking list*/
Search an object in the tracking list
InsertTrackingList
void InsertTrackingList(TRACKING_OBJECT *o) { UINT i; TRACKING_LIST *t; // Validate arguments if (o == NULL) { return; } t = OSMemoryAlloc(sizeof(TRACKING_LIST)); t->Object = o; t->Next = NULL; i = TRACKING_HASH(o->Address); if (hashlist[i] == NULL) { hashlist[i] = t; } else { TRACKING_LIST *tt = hashlist[i]; while (true) { if (tt->Next == NULL) { tt->Next = t; break; } tt = tt->Next; } } }
/* Insert an object into the tracking list*/
Insert an object into the tracking list
LockTrackingList
void LockTrackingList() { OSLock(obj_lock); }
/* Lock the tracking list*/
Lock the tracking list
UnlockTrackingList
void UnlockTrackingList() { OSUnlock(obj_lock); }
/* Unlock the tracking list*/
Unlock the tracking list
InitTracking
void InitTracking() { UINT i; CALLSTACK_DATA *s; // Hash list initialization hashlist = (TRACKING_LIST **)OSMemoryAlloc(sizeof(TRACKING_LIST *) * TRACKING_NUM_ARRAY); for (i = 0;i < TRACKING_NUM_ARRAY;i++) { hashlist[i] = NULL; } obj_id = 0; // Create a lock obj_lock = OSNewLock(); obj_id_lock = OSNewLock(); cs_lock = OSNewLock(); s = GetCallStack(); if (s == NULL) { do_not_get_callstack = true; } else { do_not_get_callstack = false; FreeCallStack(s); } }
/* Initialize the tracking*/
Initialize the tracking
FreeTracking
void FreeTracking() { UINT i; // Delete the lock OSDeleteLock(obj_lock); OSDeleteLock(obj_id_lock); OSDeleteLock(cs_lock); cs_lock = NULL; obj_id_lock = NULL; obj_lock = NULL; // Release all of the elements for (i = 0;i < TRACKING_NUM_ARRAY;i++) { if (hashlist[i] != NULL) { TRACKING_LIST *t = hashlist[i]; while (true) { TRACKING_LIST *t2 = t; TRACKING_OBJECT *o = t->Object; FreeCallStack(o->CallStack); OSMemoryFree(o); t = t->Next; OSMemoryFree(t2); if (t == NULL) { break; } } } } // Release the list OSMemoryFree(hashlist); }
/* Release the tracking*/
Release the tracking
PrintCallStack
void PrintCallStack(CALLSTACK_DATA *s) { char tmp[MAX_SIZE * 2]; GetCallStackStr(tmp, sizeof(tmp), s); Print("%s", tmp); }
/* Show the call stack*/
Show the call stack
GetCallStack
CALLSTACK_DATA *GetCallStack() { CALLSTACK_DATA *s; if (do_not_get_callstack) { // Not to get the call stack return NULL; } OSLock(cs_lock); { // Get the call stack s = OSGetCallStack(); } OSUnlock(cs_lock); if (s == NULL) { return NULL; } // Descend in the call stack for 3 steps s = WalkDownCallStack(s, 3); return s; }
/* Get the current call stack*/
Get the current call stack
GetCallStackSymbolInfo
bool GetCallStackSymbolInfo(CALLSTACK_DATA *s) { bool ret; // Validate arguments if (s == NULL) { return false; } OSLock(cs_lock); { ret = OSGetCallStackSymbolInfo(s); } OSUnlock(cs_lock); return ret; }
/* Get the symbol information of the call stack*/
Get the symbol information of the call stack
WalkDownCallStack
CALLSTACK_DATA *WalkDownCallStack(CALLSTACK_DATA *s, UINT num) { CALLSTACK_DATA *cs, *tmp; UINT i; // Validate arguments if (s == NULL) { return NULL; } cs = s; i = 0; while (true) { if (i >= num) { return cs; } i++; tmp = cs; cs = tmp->next; OSMemoryFree(tmp->name); OSMemoryFree(tmp); if (cs == NULL) { return NULL; } } }
/* Descend in the call stack by a specified number*/
Descend in the call stack by a specified number
FreeCallStack
void FreeCallStack(CALLSTACK_DATA *s) { // Validate arguments if (s == NULL) { return; } while (true) { CALLSTACK_DATA *next = s->next; OSMemoryFree(s->name); OSMemoryFree(s); if (next == NULL) { break; } s = next; } }
/* Release the call stack*/
Release the call stack
InitProcessCallOnce
void InitProcessCallOnce() { if (init_proc_once_flag == false) { init_proc_once_flag = true; #ifdef OS_WIN32 MsInitProcessCallOnce(); #endif // OS_WIN32 } }
/* The function which should be called once as soon as possible after the process is started*/
The function which should be called once as soon as possible after the process is started
WriteProbeData
void WriteProbeData(char *filename, UINT line, char *str, void *data, UINT size) { char tmp[MAX_SIZE]; USHORT cs; if (IsProbeEnabled() == false) { return; } // Take a checksum of the data if (size != 0) { cs = CalcChecksum16(data, size); } else { cs = 0; } // Generating a String snprintf(tmp, sizeof(tmp), "\"%s\" (Size=%5u, Crc=0x%04X)", str, size, cs); WriteProbe(filename, line, tmp); }
/* Writing a probe with the data*/
Writing a probe with the data
InitProbe
void InitProbe() { probe_buf = NewBuf(); probe_lock = NewLock(); probe_enabled = false; probe_start = 0; #ifdef OS_WIN32 probe_start = MsGetHiResCounter(); #endif // OS_WIN32 }
/* Initialization of Probe*/
Initialization of Probe
FreeProbe
void FreeProbe() { if (probe_buf->Size >= 1) { SYSTEMTIME st; char filename[MAX_SIZE]; // Write all to the file MakeDirEx("@probe_log"); LocalTime(&st); snprintf(filename, sizeof(filename), "@probe_log/%04u%02u%02u_%02u%02u%02u.log", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); DumpBuf(probe_buf, filename); } FreeBuf(probe_buf); DeleteLock(probe_lock); }
/* Release of Probe*/
Release of Probe
EnableProbe
void EnableProbe(bool enable) { probe_enabled = enable; }
/* Set enable / disable the Probe*/
Set enable / disable the Probe
IsProbeEnabled
bool IsProbeEnabled() { #ifndef USE_PROBE return false; #else // USE_PROBE return probe_enabled; #endif // USE_PROBE }
/* Get whether the Probe is enabled?*/
Get whether the Probe is enabled?
SetHamMode
void SetHamMode() { is_ham_mode = true; }
/* Set the Ham mode*/
Set the Ham mode
IsHamMode
bool IsHamMode() { return is_ham_mode; }
/* Get whether in Ham mode*/
Get whether in Ham mode
TimeCheck
void TimeCheck() { #ifdef OS_WIN32 UINT now, ret, total; now = Win32GetTick(); if (last_time_check == 0) { ret = 0; } else { ret = now - last_time_check; } last_time_check = now; if (first_time_check == 0) { first_time_check = now; } total = now - first_time_check; printf(" -- %3.3f / %3.3f\n", (double)ret / 1000.0f, (double)total / 1000.0f); #endif // OS_WIN32 }
/* Display the time from the previous call to now*/
Display the time from the previous call to now
IsIA64
bool IsIA64() { if (Is64() == false) { return false; } #ifndef MAYAQUA_IA_64 return false; #else // MAYAQUA_IA_64 return true; #endif // MAYAQUA_IA_64 }
/* Whether this system is IA64*/
Whether this system is IA64
IsX64
bool IsX64() { if (Is64() == false) { return false; } #ifndef MAYAQUA_IA_64 return true; #else // MAYAQUA_IA_64 return false; #endif // MAYAQUA_IA_64 }
/* Whether in x64*/
Whether in x64
MayaquaIsDotNetMode
bool MayaquaIsDotNetMode() { return dot_net_mode; }
/* Acquisition whether in .NET mode*/
Acquisition whether in .NET mode
CheckEndian
void CheckEndian() { unsigned short test; UCHAR *buf; test = 0x1234; buf = (UCHAR *)&test; if (buf[0] == 0x12) { g_little_endian = false; } else { g_little_endian = true; } }
/* Check the endian*/
Check the endian
IsNt
bool IsNt() { return is_nt; }
/* Whether in NT*/
Whether in NT
FreeMayaqua
void FreeMayaqua() { if ((--init_mayaqua_counter) > 0) { return; } // Release of Private IP File FreePrivateIPFile(); // Release of Probe FreeProbe(); // Delete the table FreeTable(); // Release of security token module FreeSecure(); // Release of the operating system specific module #ifdef OS_WIN32 MsFree(); #endif // OS_WIN32 // Release of OS information FreeOsInfo(); // Release of HamCore file system FreeHamcore(); // Release of the command line string FreeCommandLineStr(); // Release of the command line token FreeCommandLineTokens(); // Release of network communication module FreeNetwork(); // Release of real-time clock FreeTick64(); // Release of the string library FreeStringLibrary(); // Release of thread pool FreeThreading(); // Release of crypt library FreeCryptLibrary(); if (IsTrackingEnabled()) { // Show the kernel status if (g_debug) { PrintKernelStatus(); } // Display the debug information if (g_memcheck) { PrintDebugInformation(); } // Release the tracking FreeTracking(); } // Release of the kernel status FreeKernelStatus(); DeleteLock(tick_manual_lock); tick_manual_lock = NULL; // Release of OS OSFree(); }
/* Release of Mayaqua library*/
Release of Mayaqua library
CheckUnixTempDir
void CheckUnixTempDir() { if (OS_IS_UNIX(GetOsInfo()->OsType)) { char tmp[128], tmp2[64]; UINT64 now = SystemTime64(); IO *o; MakeDir("/tmp"); Format(tmp2, sizeof(tmp2), "%I64u", now); Format(tmp, sizeof(tmp), "/tmp/.%s", tmp2); o = FileCreate(tmp); if (o == NULL) { o = FileOpen(tmp, false); if (o == NULL) { Print("Unable to use /tmp.\n\n"); exit(0); } } FileClose(o); FileDelete(tmp); } }
/* Check whether /tmp is available in the UNIX*/
Check whether /tmp is available in the UNIX
Alert
void Alert(char *msg, char *caption) { OSAlert(msg, caption); }
/* Show an alert*/
Show an alert
GetOsType
UINT GetOsType() { OS_INFO *i = GetOsInfo(); if (i == NULL) { return 0; } return i->OsType; }
/* Get the OS type*/
Get the OS type
GetOsInfo
OS_INFO *GetOsInfo() { return os_info; }
/* Getting OS information*/
Getting OS information
InitOsInfo
void InitOsInfo() { if (os_info != NULL) { return; } os_info = ZeroMalloc(sizeof(OS_INFO)); OSGetOsInfo(os_info); }
/* Initialization of OS information*/
Initialization of OS information
FreeOsInfo
void FreeOsInfo() { if (os_info == NULL) { return; } Free(os_info->OsSystemName); Free(os_info->OsProductName); Free(os_info->OsVendorName); Free(os_info->OsVersion); Free(os_info->KernelName); Free(os_info->KernelVersion); Free(os_info); os_info = NULL; }
/* Release of OS information*/
Release of OS information
GetCommandLineUniToken
UNI_TOKEN_LIST *GetCommandLineUniToken() { if (cmdline_uni_token == NULL) { return UniNullToken(); } else { return UniCopyToken(cmdline_uni_token); } }
/* Get the Unicode command line tokens*/
Get the Unicode command line tokens
GetCommandLineToken
TOKEN_LIST *GetCommandLineToken() { if (cmdline_token == NULL) { return NullToken(); } else { return CopyToken(cmdline_token); } }
/* Getting the command line tokens*/
Getting the command line tokens
ParseCommandLineTokens
void ParseCommandLineTokens() { if (cmdline_token != NULL) { FreeToken(cmdline_token); } cmdline_token = ParseCmdLine(cmdline); if (cmdline_uni_token != NULL) { UniFreeToken(cmdline_uni_token); } cmdline_uni_token = UniParseCmdLine(uni_cmdline); }
/* Convert the command line string into tokens*/
Convert the command line string into tokens
FreeCommandLineTokens
void FreeCommandLineTokens() { if (cmdline_token != NULL) { FreeToken(cmdline_token); } cmdline_token = NULL; if (cmdline_uni_token != NULL) { UniFreeToken(cmdline_uni_token); } cmdline_uni_token = NULL; }
/* Release command line tokens*/
Release command line tokens
FreeCommandLineStr
void FreeCommandLineStr() { SetCommandLineStr(NULL); if (exename != NULL) { Free(exename); exename = NULL; } if (exename_w != NULL) { Free(exename_w); exename_w = NULL; } }
/* Release of the command line string*/
Release of the command line string