Thread: DC_VFS Change!
View Single Post
Old 12-15-2003, 06:35 AM  
darkone
Disabled
 
darkone's Avatar
 
Join Date: Dec 2001
Posts: 2,230
Default

Here's commands for context handling.. it uses really simple format: |type|length|data|TCHAR null|

BOOL CreateFileContext(LPFILECONTEXT lpContext, LPFILECONTEXT lpSourceContext)
{
// Copy context
if (lpSourceContext &&
lpSourceContext->dwData)
{
lpContext->dwData = lpSourceContext->dwData;
lpContext->lpData = Allocate("FileContext", lpContext->dwData);
if (! lpContext->lpData) return FALSE;
CopyMemory(lpContext->lpData, lpSourceContext->lpData, lpContext->dwData);
}
else ZeroMemory(lpContext, sizeof(FILECONTEXT));

return TRUE;
}




BOOL InsertFileContext(LPFILECONTEXT lpContext, BYTE Item, LPVOID lpData, DWORD dwData)
{
LPVOID lpMemory;
DWORD dwSlack, dwMemory;
TCHAR pBuffer[1];

pBuffer[0] = _TEXT('\0');
// Remove existing context
dwSlack = lpContext->dwData;
DeleteFileContext(lpContext, Item);
dwSlack = (lpContext->dwData ? dwSlack - lpContext->dwData : 0);

// Allocate more memory
if (dwSlack < (dwData + sizeof(BYTE) + sizeof(UINT32) + sizeof(TCHAR)))
{
lpMemory = ReAllocate(lpContext->lpData, "FileContext",
lpContext->dwData + dwData + sizeof(BYTE) + sizeof(UINT32) + sizeof(TCHAR));
if (! lpMemory) ERROR_RETURN(ERROR_NOT_ENOUGH_MEMORY, FALSE);
lpContext->lpData = lpMemory;
}

dwMemory = dwData + sizeof(BYTE) + sizeof(UINT32) + sizeof(TCHAR);
// Copy new data
CopyMemory((LPVOID)((ULONG)lpContext->lpData + lpContext->dwData),
&Item, sizeof(BYTE));
lpContext->dwData += sizeof(BYTE);
CopyMemory((LPVOID)((ULONG)lpContext->lpData + lpContext->dwData),
&dwMemory, sizeof(UINT32));
lpContext->dwData += sizeof(UINT32);
CopyMemory((LPVOID)((ULONG)lpContext->lpData + lpContext->dwData),
lpData, dwData);
lpContext->dwData += dwData;
CopyMemory((LPVOID)((ULONG)lpContext->lpData + lpContext->dwData), pBuffer, sizeof(TCHAR));
lpContext->dwData += sizeof(TCHAR);

return TRUE;
}


BOOL FreeFileContext(LPFILECONTEXT lpContext)
{
BOOL bReturn;

bReturn = Free(lpContext->lpData);
lpContext->lpData = NULL;
lpContext->dwData = 0;
return bReturn;
}


BOOL DeleteFileContext(LPFILECONTEXT lpContext, BYTE Item)
{
LPVOID lpMemory;
DWORD dwMemory;

// Remove existing context
lpMemory = FindFileContext(Item, lpContext);
if (lpMemory)
{
dwMemory = ((PUINT32)lpMemory)[-1];
lpMemory = (LPVOID)((ULONG)lpMemory - sizeof(UINT32) - sizeof(BYTE));
MoveMemory(lpMemory,
(LPVOID)((ULONG)lpMemory + dwMemory),
((ULONG)lpContext->lpData + lpContext->dwData) - ((ULONG)lpMemory + dwMemory));
lpContext->dwData -= dwMemory;
}
return TRUE;
}




LPVOID FindFileContext(BYTE Item, LPFILECONTEXT lpContext)
{
LPBYTE lpOffset;
UINT32 Size;

// Seek through buffer
lpOffset = (LPBYTE)lpContext->lpData;
for (;(ULONG)lpOffset - (ULONG)lpContext->lpData < lpContext->dwData;lpOffset += Size)
{
// Size of entry
Size = ((PUINT32)&lpOffset[1])[0] + sizeof(BYTE) + sizeof(UINT32);

if (lpOffset[0] == Item) return &lpOffset[sizeof(BYTE) + sizeof(UINT)];
}
return NULL;
}
darkone is offline