/* * NTFS Class common definitions * * Copyright(C) 2010 cyb70289 * * This program/include file is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as published * by the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program/include file is distributed in the hope that it will be * useful, but WITHOUT ANY WARRANTY; without even the implied warranty * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. */ #ifndef __NTFS_COMMON_H_CYB70289 #define __NTFS_COMMON_H_CYB70289 #include #include #include #include #include "NTFS_DataType.h" #define ATTR_NUMS 16 // Attribute Types count #define ATTR_INDEX(at) (((at)>>4)-1) // Attribute Type to Index, eg. 0x10->0, 0x30->2 #define ATTR_MASK(at) (((DWORD)1)< struct NTSLIST_ENTRY { NTSLIST_ENTRY *Next; ENTRY_TYPE *Entry; }; // List Entry Smart Pointer template class CEntrySmartPtr { public: CEntrySmartPtr(ENTRY_TYPE *ptr = NULL) { EntryPtr = ptr; } virtual ~CEntrySmartPtr() { if (EntryPtr) delete EntryPtr; } private: const ENTRY_TYPE *EntryPtr; public: __inline CEntrySmartPtr operator = (const ENTRY_TYPE* ptr) { // Delete previous pointer if allocated if (EntryPtr) delete EntryPtr; EntryPtr = ptr; return *this; } __inline const ENTRY_TYPE* operator->() const { _ASSERT(EntryPtr); return EntryPtr; } __inline BOOL IsValid() const { return EntryPtr != NULL; } }; ////////////////////////////////////// // Single list implementation ////////////////////////////////////// template class CSList { public: CSList() { ListHead = ListTail = NULL; ListCurrent = NULL; EntryCount = 0; } virtual ~CSList() { RemoveAll(); } private: int EntryCount; NTSLIST_ENTRY *ListHead; NTSLIST_ENTRY *ListTail; NTSLIST_ENTRY *ListCurrent; public: // Get entry count __inline int GetCount() const { return EntryCount; } // Insert to tail BOOL InsertEntry(ENTRY_TYPE *entry) { NTSLIST_ENTRY *le = new NTSLIST_ENTRY; if (!le) return FALSE; le->Entry = entry; le->Next = NULL; if (ListTail == NULL) ListHead = le; // Empty list else ListTail->Next = le; ListTail = le; EntryCount++; return TRUE; } // Remove all entries void RemoveAll() { while (ListHead) { ListCurrent = ListHead->Next; delete ListHead->Entry; delete ListHead; ListHead = ListCurrent; } ListHead = ListTail = NULL; ListCurrent = NULL; EntryCount = 0; } // Find first entry __inline ENTRY_TYPE *FindFirstEntry() const { ((CSList*)this)->ListCurrent = ListHead; if (ListCurrent) return ListCurrent->Entry; else return NULL; } // Find next entry __inline ENTRY_TYPE *FindNextEntry() const { if (ListCurrent) ((CSList*)this)->ListCurrent = ListCurrent->Next; if (ListCurrent) return ListCurrent->Entry; else return NULL; } // Throw all entries // Caution! All entries are just thrown without free __inline void ThrowAll() { ListHead = ListTail = NULL; ListCurrent = NULL; EntryCount = 0; } }; //CSList ////////////////////////////////////// // Stack implementation ////////////////////////////////////// template class CStack { public: CStack() { ListHead = ListTail = NULL; EntryCount = 0; } virtual ~CStack() { RemoveAll(); } private: int EntryCount; NTSLIST_ENTRY *ListHead; NTSLIST_ENTRY *ListTail; public: // Get entry count __inline int GetCount() const { return EntryCount; } // Insert to head BOOL Push(ENTRY_TYPE *entry) { NTSLIST_ENTRY *le = new NTSLIST_ENTRY; if (!le) return FALSE; le->Entry = entry; le->Next = ListHead; ListHead = le; if (ListTail == NULL) ListTail = le; // Empty list EntryCount ++; return TRUE; } // Remove from head ENTRY_TYPE* Pop() { if (ListHead == NULL) return NULL; NTSLIST_ENTRY *le = ListHead; ENTRY_TYPE *e = le->Entry; if (ListTail == ListHead) ListTail = ListHead->Next; ListHead = ListHead->Next; delete le; EntryCount --; return e; } // Remove all entries void RemoveAll() { NTSLIST_ENTRY *le; while (ListHead) { le = ListHead->Next; delete ListHead->Entry; delete ListHead; ListHead = le; } ListHead = ListTail = NULL; EntryCount = 0; } }; //CStack #endif