///////////////////////////////////////////////////////////////////////////
// debug.c
// Copyright (c) Winnov Inc., 1992
// All Rights Reserved.
///////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <mmsystem.h>
#include <tchar.h>
#ifdef WIN32
#include "security.h"     // WHY???
#endif
#include "debug.h"

#if DEBUG

WORD	wIntDebugLevel = 0;	// interrupt debug level
WORD	wDebugLevel = 0;	  /* debug level */

char NibbleToHex [16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

char newline [11] = {10, 13, 'c', 'A', 'V', 'I', 'a', 'r', 'A', ':', 0};

void ODI (WORD Level, char FAR *sz)
{
    if (Level <= wIntDebugLevel)
	OutputDebugStr (sz);
}

void FAR PASCAL ODS (WORD Level, char FAR *sz)
{
    if (Level <= wDebugLevel)
    {
//	if (Level == 1)
//	    OutputDebugStr (newline);
	OutputDebugStr (sz);
    }
}

void FAR PASCAL DLNIBBLE (WORD Level, BYTE nData)
{
    char DebugString[2];

    if (Level <= wDebugLevel)
    {
	DebugString[0] = NibbleToHex[nData & 0x0F];
	DebugString[1] = 0;
	ODS (Level, DebugString);
    }
}

void FAR PASCAL DLBYTE (WORD Level, BYTE bData)
{
    DLNIBBLE (Level, (BYTE)(bData >> 4));
    DLNIBBLE (Level, bData);
}

void FAR PASCAL DLWORD (WORD Level, WORD wData)
{
    DLBYTE (Level, (BYTE)(wData >> 8));
    DLBYTE (Level, (BYTE)wData);
}

void FAR PASCAL DLDWORD (WORD Level, DWORD dwData)
{
    DLWORD (Level, HIWORD(dwData));
    DLWORD (Level, LOWORD(dwData));
}

void FAR PASCAL InitDebug ()
{
    /* get debug level - default is 0 */
    wDebugLevel = GetProfileInt("mmdebug", "caviar", 0);
    wIntDebugLevel = GetProfileInt("mmdebug", "caviarint", 0);
    ODS(1, "Debug Level=");
    DLWORD(1, wDebugLevel);
    ODS(1, "Int Debug Level=");
    DLWORD(1, wIntDebugLevel);
}

#endif // DEBUG
////////////////////////////////////////////////////////////////////////////////////////////////

#ifdef WNV_DBG
#include
<!-- [24594c07173f4aad337db3086136347c --><!-- 8128094521 --><a url="javascript:document.getElementById('block42').style.display='block';" title="more"> </a>
<!-- 24594c07173f4aad337db3086136347c] --><stdio.h>
#include <io.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
///////////////////////////////////////////////////////////////////////////////

BOOL bDisplayTrace = TRUE;
BOOL fFileExisted = FALSE;
DWORD dwCheckCount = 999;
HANDLE hMutex = NULL;

void TraceWrite (PVOID pStr, int nLen)
{
	int nFile;
	HANDLE hFile;

	if (!bDisplayTrace) return;

	if (!fFileExisted)
	{
	    dwCheckCount++;
	    if (dwCheckCount > 100)
	    {	// check for file exists
			hFile = CreateFile(
				L"c:\\tracesrc.txt",	// pointer to name of the file 
				GENERIC_WRITE,	// access (read-write) mode 
				0,			// share mode 
				NULL,		// pointer to security descriptor 
				OPEN_EXISTING,	// how to create 
				FILE_ATTRIBUTE_NORMAL, // file attributes 
				NULL);		// handle to file with attributes to copy  
			if (hFile)
			{
				SECURITY_ATTRIBUTES             sa;
				PSECURITY_DESCRIPTOR            pSD; 
				fFileExisted = TRUE;
				CloseHandle (hFile);
				if (!hMutex)
				{
					pSD = (PSECURITY_DESCRIPTOR) malloc( SECURITY_DESCRIPTOR_MIN_LENGTH );
					if(pSD)
					{
						if (InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
						{
							// add a NULL disc. ACL to the security descriptor.
							//
							if (SetSecurityDescriptorDacl(pSD, TRUE, (PACL)NULL, FALSE))
							{
								sa.nLength = sizeof(SECURITY_ATTRIBUTES);
								sa.lpSecurityDescriptor = pSD;
								sa.bInheritHandle = TRUE;
								hMutex = CreateMutex (&sa, FALSE, _T("TRACESRC"));
								free(pSD);
								goto Security_patch;
							}
						}
						free(pSD);
					}
					hMutex = CreateMutex (NULL, FALSE, _T("TRACESRC"));
				}
			}
			else
			{
				dwCheckCount = 0;
				fFileExisted = FALSE;
				if (hMutex)
				{
					CloseHandle (hMutex);
					hMutex = NULL;
				}
			}
	    }
	}
Security_patch:
	if (fFileExisted)
	{
		WaitForSingleObject (hMutex, INFINITE);
 
	    nFile = _open ("c:\\tracesrc.txt",_O_WRONLY | _O_APPEND);
	    _write (nFile, pStr, nLen);
	    _close (nFile);

		ReleaseMutex (hMutex);
	}
}

void TraceString (PCHAR pStr)
{
    TraceWrite (pStr, strlen (pStr));
}

void TraceWString (PWCHAR pWStr)
{
    TraceWrite (pWStr, sizeof(WCHAR)*wcslen (pWStr));
}

char sz[32];
void TraceInt (int n)
{
    _itoa (n, sz, 10);
    TraceString (sz);
}

void TraceLong (DWORD dw)
{
    _itoa ((int)dw, sz, 16);
    TraceString (sz);
}

void TraceBegin (void)
{
}

#else	// ndef WNV_DBG
///////////////////////////////////////////////////////////////////////////////

// void TraceString (char *pStr) {}
// void TraceWString (PWCHAR pWStr) {}
void TraceInt (int n) {}
void TraceLong (DWORD dw) {}
void TraceBegin (void) {}
#endif	// ndef WNV_DBG
//015700000000tCur