<commit_before>/*
 * host2wire.c
 *
 * conversion routines from the host to the wire format.
 * This will usually just a re-ordering of the
 * data (as we store it in network format)
 *
 * XXX not sure if we need to keep this around
 *
 * a Net::DNS like library for C
 *
 * (c) NLnet Labs, 2004
 *
 * See the file LICENSE for the license
 */

#include <config.h>
#include <ldns/host2wire.h>

/* TODO 
  add a pointer to a 'possiblecompression' structure
  to all the needed functions?
  something like an array of name, pointer values?
  every dname part could be added to it
*/

/* TODO dname must still be handled as an rdf? */
ldns_status
ldns_dname2buffer_wire(ldns_buffer *buffer, ldns_rdf *name)
{
	if (ldns_buffer_reserve(buffer, ldns_rdf_size(name))) {
		ldns_buffer_write(buffer,
		                  ldns_rdf_data(name),
		                  ldns_rdf_size(name));
	}
	return ldns_buffer_status(buffer);
}

ldns_status
ldns_rdf2buffer_wire(ldns_buffer *buffer, ldns_rdf *rdf)
{
	if (ldns_buffer_reserve(buffer, ldns_rdf_size(rdf))) {
		ldns_buffer_write(buffer,
		                  ldns_rdf_data(rdf),
		                  ldns_rdf_size(rdf));
	}
	return ldns_buffer_status(buffer);
}

ldns_status
ldns_rr2buffer_wire(ldns_buffer *buffer, ldns_rr *rr, int section)
{
	uint16_t i;
	uint16_t rdl_pos = 0;
	
	if (ldns_rr_owner(rr)) {
		(void) ldns_dname2buffer_wire(buffer, ldns_rr_owner(rr));
	}
	
	if (ldns_buffer_reserve(buffer, 4)) {
		(void) ldns_buffer_write_u16(buffer, ldns_rr_get_type(rr));
		(void) ldns_buffer_write_u16(buffer, ldns_rr_get_class(rr));
	}

	if (section != LDNS_SECTION_QUESTION) {
		if (ldns_buffer_reserve(buffer, 6)) {
			ldns_buffer_write_u32(buffer, ldns_rr_ttl(rr));
			/* remember pos for later */
			rdl_pos = ldns_buffer_position(buffer);
			ldns_buffer_write_u16(buffer, 0);
		}	

		for (i = 0; i < ldns_rr_rd_count(rr); i++) {
			(void) ldns_rdf2buffer_wire(buffer, ldns_rr_rdf(rr, i));
		}
		
		if (rdl_pos != 0) {
			ldns_buffer_write_u16_at(buffer,
			                         rdl_pos,
			                         ldns_buffer_position(buffer)
		        	                   - rdl_pos
		                	           - 2
		                	           );
		}
	}
	return ldns_buffer_status(buffer);
}

/**
 * Copy the packet header data to the buffer in wire format
 */
ldns_status
ldns_hdr2buffer_wire(ldns_buffer *buffer, ldns_pkt *packet)
{
	uint8_t flags;

	if (ldns_buffer_reserve(buffer, 12)) {
		ldns_buffer_write_u16(buffer, ldns_pkt_id(packet));
		
		flags = ldns_pkt_qr(packet) << 7
		        | ldns_pkt_opcode(packet) << 6
		        | ldns_pkt_aa(packet) << 2
		        | ldns_pkt_tc(packet) << 1
		        | ldns_pkt_rd(packet);
		ldns_buffer_write_u8(buffer, flags);
		
		flags = ldns_pkt_ra(packet) << 7<commit_msg>rrsig2buffer - basis function for dnssec<commit_after>/*
 * host2wire.c
 *
 * conversion routines from the host to the wire format.
 * This will usually just a re-ordering of the
 * data (as we store it in network format)
 *
 * XXX not sure if we need to keep this around
 *
 * a Net::DNS like library for C
 *
 * (c) NLnet Labs, 2004
 *
 * See the file LICENSE for the license
 */

#include <config.h>
#include <ldns/host2wire.h>

/* TODO 
  add a pointer to a 'possiblecompression' structure
  to all the needed functions?
  something like an array of name, pointer values?
  every dname part could be added to it
*/

/* TODO dname must still be handled as an rdf? */
ldns_status
ldns_dname2buffer_wire(ldns_buffer *buffer, ldns_rdf *name)
{
	if (ldns_buffer_reserve(buffer, ldns_rdf_size(name))) {
		ldns_buffer_write(buffer,
		                  ldns_rdf_data(name),
		                  ldns_rdf_size(name));
	}
	return ldns_buffer_status(buffer);
}

ldns_status
ldns_rdf2buffer_wire(ldns_buffer *buffer, ldns_rdf *rdf)
{
	if (ldns_buffer_reserve(buffer, ldns_rdf_size(rdf))) {
		ldns_buffer_write(buffer,
		                  ldns_rdf_data(rdf),
		                  ldns_rdf_size(rdf));
	}
	return ldns_buffer_status(buffer);
}

ldns_status
ldns_rr2buffer_wire(ldns_buffer *buffer, ldns_rr *rr, int section)
{
	uint16_t i;
	uint16_t rdl_pos = 0;
	
	if (ldns_rr_owner(rr)) {
		(void) ldns_dname2buffer_wire(buffer, ldns_rr_owner(rr));
	}
	
	if (ldns_buffer_reserve(buffer, 4)) {
		(void) ldns_buffer_write_u16(buffer, ldns_rr_get_type(rr));
		(void) ldns_buffer_write_u16(buffer, ldns_rr_get_class(rr));
	}

	if (section != LDNS_SECTION_QUESTION) {
		if (ldns_buffer_reserve(buffer, 6)) {
			ldns_buffer_write_u32(buffer, ldns_rr_ttl(rr));
			/* remember pos for later */
			rdl_pos = ldns_buffer_position(buffer);
			ldns_buffer_write_u16(buffer, 0);
		}	

		for (i = 0; i < ldns_rr_rd_count(rr); i++) {
			(void) ldns_rdf2buffer_wire(buffer, ldns_rr_rdf(rr, i));
		}
		
		if (rdl_pos != 0) {
			ldns_buffer_write_u16_at(buffer,
			                         rdl_pos,
			                         ldns_buffer_position(buffer)
		        	                   - rdl_pos
		                	           - 2
		                	           );
		}
	}
	return ldns_buffer_status(buffer);
}

/**
 * convert a rrsig to wireformat BUT EXCLUDE the rrsig rdata
 * This is needed in DNSSEC verification
 * \param[out] *buffer buffer where to put the result
 * \param[in] *rr sigrr to operate on
 */
ldns_status
ldns_rrsig2buffer_wire(ldns_buffer *buffer, ldns_rr *rr)
{
	uint16_t i;
	uint16_t rdl_pos = 0;

	/* it must be a sig RR */
	if (ldns_rr_get_type(rr) != LDNS_RR_TYPE_RRSIG) {
		return LDNS_STATUS_ERR;
	}
	
	if (ldns_rr_owner(rr)) {
		(void) ldns_dname2buffer_wire(buffer, ldns_rr_owner(rr));
	}
	
	if (ldns_buffer_reserve(buffer, 4)) {
		(void) ldns_buffer_write_u16(buffer, ldns_rr_get_type(rr));
		(void) ldns_buffer_write_u16(buffer, ldns_rr_get_class(rr));
	}

	if (ldns_buffer_reserve(buffer, 6)) {
		ldns_buffer_write_u32(buffer, ldns_rr_ttl(rr));
		/* remember pos for later */
		rdl_pos = ldns_buffer_position(buffer);
		ldns_buffer_write_u16(buffer, 0);
	}	

	/* now convert all the rdf, except the actual signature data
	 * rdf number 8 */
	for (i = 0; i < ldns_rr_rd_count(rr) - 1; i++) {
		(void) ldns_rdf2buffer_wire(buffer, ldns_rr_rdf(rr, i));
	}

	if (rdl_pos != 0) {
		ldns_buffer_write_u16_at(buffer,
				rdl_pos,
				ldns_buffer_position(buffer)
				- rdl_pos
				- 2
				);
	}
	return ldns_buffer_status(buffer);
}

/**
 * Copy the packet header data to the buffer in wire format
 */
ldns_status
ldns_hdr2buffer_wire(ldns_buffer *buffer, ldns_pkt *packet)
{
	uint8_t flags;

	if (ldns_buffer_reserve(buffer, 12)) {
		ldns_buffer_write_u16(buffer, ldns_pkt_id(packet));
		
		flags = ldns_pkt_qr(packet) << 7
		        | ldns_pkt_opcode(packet) << 6
		        | ldns_pkt_aa(packet) << 2
		        | ldns_pkt_tc(packet) << 1
		        | ldns_pkt_rd(packet);
		ldns_buffer_write_u8(buffer, flags);
		
		flags = ldns_pkt_ra(packet) << 7
{'fraction_non_alphanumeric': 0.10558002936857562, 'fraction_numerical': 0.016886930983847283, 'mean_word_length': 3.22256664600124, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 7, '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': '5120420', 'n_tokens_mistral': 2960, 'n_tokens_neox': 2624, 'n_words': 559}