summaryrefslogtreecommitdiffhomepage
path: root/src/internal/ntapi_log.h
blob: ae9cce99e336a898a23dbb21e87bfff90e272431 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2016  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <psxtypes/psxtypes.h>
#include <ntapi/nt_file.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"

static inline int __ntapi_uintptr_to_utf8(uintptr_t value,unsigned char * buf)
{
	int i,len;
	uintptr_t val;

	if (!value) {
		*buf = '0';
		len = 1;
	} else {
		for (len=0,val=value; val; val=val/10,len++);
		for (i=len,buf+=len-1; i; i--,buf--,value=value/10)
			*buf = '0' + (value % 10);
	}

	return len;
}

static inline ssize_t __ntapi_log_write(void * msg,uint32_t size)
{
	int32_t		status;
	void *		hlog;
	nt_iosb		iosb;
	uintptr_t	buffer[8] = {0};
	unsigned char *	ch = (unsigned char *)buffer;

	if (!(hlog = __ntapi_internals()->rtdata->hlog))
		return NT_STATUS_INVALID_HANDLE;

	*ch++ = '@';
	ch   += __ntapi_uintptr_to_utf8(
			pe_get_current_process_id(),
			ch);
	*ch++ = ':';
	ch   += __ntapi_uintptr_to_utf8(
			pe_get_current_thread_id(),
			ch);
	*ch++ = '@';
	*ch++ = ' ';

	__ntapi->zw_write_file(
		hlog,
		0,0,0,&iosb,
		buffer,
		(uint32_t)(ch-(unsigned char *)buffer),
		0,0);

	status = __ntapi->zw_write_file(
		hlog,
		0,0,0,&iosb,
		msg,
		(uint32_t)size,
		0,0);

	return status ? -1 : iosb.info;
}