<commit_before>/*
 * nt loader
 *
 * Copyright 2006-2008 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */


#include <stdarg.h>
#include <assert.h>

#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winternl.h"

#include "debug.h"
#include "mem.h"
#include "object.h"
#include "ntcall.h"
#include "section.h"
#include "object.inl"

class message_t;

typedef list_anchor<message_t, 0> message_list_t;
typedef list_element<message_t> message_entry_t;
typedef list_iter<message_t, 0> message_iter_t;

class message_t {
public:
	ULONG destination_id;
protected:
	friend class list_anchor<message_t, 0>;
	friend class list_iter<message_t, 0>;
	message_entry_t entry[1];
public:
	void *operator new(size_t n, size_t len);
	explicit message_t();
	bool is_linked() { return entry[0].is_linked(); }
	~message_t();
	void dump();
	const char* msg_type();
public:
	LPC_MESSAGE req;
};

struct listener_t;
struct port_queue_t;

typedef list_anchor<listener_t, 0> listener_list_t;
typedef list_element<listener_t> listener_entry_t;
typedef list_iter<listener_t, 0> listener_iter_t;

struct listener_t {
	listener_entry_t entry[1];
	port_t *port;
	thread_t *thread;
	BOOLEAN want_connect;
	ULONG message_id;
public:
	explicit listener_t(port_t *p, thread_t *t, BOOLEAN wc, ULONG id);
	~listener_t();
	bool is_linked() { return entry[0].is_linked(); }
};

struct port_queue_t : public object_t {
	ULONG refs;
	ULONG max_connect;
	ULONG max_data;
	message_list_t messages;
	listener_list_t listeners;
public:
	explicit port_queue_t( ULONG max_connect, ULONG max_data );
	~port_queue_t();
	message_t *find_connection_request();
};

struct port_t : public object_t {
	port_queue_t *queue;
	BOOLEAN server;
	thread_t *thread;
	port_t *other;
	section_t *section; // our section
	BYTE *other_section_base;	// mapped address of other port's section
	BYTE *our_section_base;	// mapped address of other port's section
	ULONG view_size;
	ULONG identifier;
	message_t *received_msg;
public:
	explicit port_t( BOOLEAN s, thread_t *t, port_queue_t *q );
	~port_t();
	void send_message( message_t *msg );
	void send_close_message( void );
	NTSTATUS send_reply( message_t *reply );
	void listen( message_t *&msg );
	NTSTATUS send_request( message_t *msg );
	void request_wait_reply( message_t *msg, message_t *&reply );
	NTSTATUS reply_wait_receive( message_t *reply, message_t *&received );
	NTSTATUS accept_connect( thread_t *t, message_t *reply, PLPC_SECTION_WRITE server_write_sec );
};

struct exception_msg_data_t {
	ULONG EventCode;
	ULONG Status;
	EXCEPTION_RECORD ExceptionRecord;
};

static int unique_message_id = 0x101;

void *message_t::operator new(size_t msg_size, size_t extra)
{
	return new unsigned char[msg_size + extra];
}

message_t::message_t() :
	destination_id(0)
{
	memset( &req, 0, sizeof req );
}

message_t::~message_t()
{
	assert( !is_linked() );
}

void unlink_and_free_message( message_list_t *list, message_t *msg )
{
	assert( msg->is_linked() );
	list->unlink( msg );<commit_msg>Use the correct delete operator<commit_after>/*
 * nt loader
 *
 * Copyright 2006-2008 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */


#include <stdarg.h>
#include <assert.h>

#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winternl.h"

#include "debug.h"
#include "mem.h"
#include "object.h"
#include "ntcall.h"
#include "section.h"
#include "object.inl"

class message_t;

typedef list_anchor<message_t, 0> message_list_t;
typedef list_element<message_t> message_entry_t;
typedef list_iter<message_t, 0> message_iter_t;

class message_t {
public:
	ULONG destination_id;
protected:
	friend class list_anchor<message_t, 0>;
	friend class list_iter<message_t, 0>;
	message_entry_t entry[1];
public:
	void *operator new(size_t n, size_t len);
	void operator delete(void* ptr);
	explicit message_t();
	bool is_linked() { return entry[0].is_linked(); }
	~message_t();
	void dump();
	const char* msg_type();
public:
	LPC_MESSAGE req;
};

struct listener_t;
struct port_queue_t;

typedef list_anchor<listener_t, 0> listener_list_t;
typedef list_element<listener_t> listener_entry_t;
typedef list_iter<listener_t, 0> listener_iter_t;

struct listener_t {
	listener_entry_t entry[1];
	port_t *port;
	thread_t *thread;
	BOOLEAN want_connect;
	ULONG message_id;
public:
	explicit listener_t(port_t *p, thread_t *t, BOOLEAN wc, ULONG id);
	~listener_t();
	bool is_linked() { return entry[0].is_linked(); }
};

struct port_queue_t : public object_t {
	ULONG refs;
	ULONG max_connect;
	ULONG max_data;
	message_list_t messages;
	listener_list_t listeners;
public:
	explicit port_queue_t( ULONG max_connect, ULONG max_data );
	~port_queue_t();
	message_t *find_connection_request();
};

struct port_t : public object_t {
	port_queue_t *queue;
	BOOLEAN server;
	thread_t *thread;
	port_t *other;
	section_t *section; // our section
	BYTE *other_section_base;	// mapped address of other port's section
	BYTE *our_section_base;	// mapped address of other port's section
	ULONG view_size;
	ULONG identifier;
	message_t *received_msg;
public:
	explicit port_t( BOOLEAN s, thread_t *t, port_queue_t *q );
	~port_t();
	void send_message( message_t *msg );
	void send_close_message( void );
	NTSTATUS send_reply( message_t *reply );
	void listen( message_t *&msg );
	NTSTATUS send_request( message_t *msg );
	void request_wait_reply( message_t *msg, message_t *&reply );
	NTSTATUS reply_wait_receive( message_t *reply, message_t *&received );
	NTSTATUS accept_connect( thread_t *t, message_t *reply, PLPC_SECTION_WRITE server_write_sec );
};

struct exception_msg_data_t {
	ULONG EventCode;
	ULONG Status;
	EXCEPTION_RECORD ExceptionRecord;
};

static int unique_message_id = 0x101;

void *message_t::operator new(size_t msg_size, size_t extra)
{
	return new unsigned char[msg_size + extra];
}

void message_t::operator delete(void* ptr)
{
	delete[] (unsigned char*) ptr;
}

message_t::message_t() :
	destination_id(0)
{
	memset( &req, 0, sizeof req );
}

message_t::~message_t()
{
	assert( !is_linked() );
}

void unlink_and_free_message( message_list_t *list, message_t *msg )
{
	assert( msg->is_linked() );
	list->unlink( msg );
{'fraction_non_alphanumeric': 0.10105960264900662, 'fraction_numerical': 0.01033112582781457, 'mean_word_length': 5.410016977928692, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1495556', 'n_tokens_mistral': 2701, 'n_tokens_neox': 2616, 'n_words': 802}