<commit_before>/*
 * (C) 2008-2012 by <NAME> <<EMAIL>>
 *
 * This program 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.
 */
#include <limits.h>	/* for INT_MAX */
#include <libmnl/libmnl.h>
#include <string.h>
#include <errno.h>
#include "internal.h"

/**
 * \defgroup attr Netlink attribute helpers
 *
 * Netlink Type-Length-Value (TLV) attribute:
 * \verbatim
	|<-- 2 bytes -->|<-- 2 bytes -->|<-- variable -->|
	-------------------------------------------------
	|     length    |      type     |      value     |
	-------------------------------------------------
	|<--------- header ------------>|<-- payload --->|
\endverbatim
 * The payload of the Netlink message contains sequences of attributes that are
 * expressed in TLV format.
 *
 * @{
 */

/**
 * mnl_attr_get_type - get type of netlink attribute
 * \param attr pointer to netlink attribute
 *
 * This function returns the attribute type.
 */
uint16_t mnl_attr_get_type(const struct nlattr *attr)
{
	return attr->nla_type & NLA_TYPE_MASK;
}
EXPORT_SYMBOL(mnl_attr_get_type);

/**
 * mnl_attr_get_len - get length of netlink attribute
 * \param attr pointer to netlink attribute
 *
 * This function returns the attribute length that is the attribute header
 * plus the attribute payload.
 */
uint16_t mnl_attr_get_len(const struct nlattr *attr)
{
	return attr->nla_len;
}
EXPORT_SYMBOL(mnl_attr_get_len);

/**
 * mnl_attr_get_payload_len - get the attribute payload-value length
 * \param attr pointer to netlink attribute
 *
 * This function returns the attribute payload-value length.
 */
uint16_t mnl_attr_get_payload_len(const struct nlattr *attr)
{
	return attr->nla_len - MNL_ATTR_HDRLEN;
}
EXPORT_SYMBOL(mnl_attr_get_payload_len);

/**
 * mnl_attr_get_payload - get pointer to the attribute payload
 * \param attr pointer to netlink attribute
 *
 * This function return a pointer to the attribute payload.
 */
void *mnl_attr_get_payload(const struct nlattr *attr)
{
	return (void *)attr + MNL_ATTR_HDRLEN;
}
EXPORT_SYMBOL(mnl_attr_get_payload);

/**
 * mnl_attr_ok - check if there is room for an attribute in a buffer
 * \param attr attribute that we want to check if there is room for
 * \param len remaining bytes in a buffer that contains the attribute
 *
 * This function is used to check that a buffer, which is supposed to contain
 * an attribute, has enough room for the attribute that it stores, i.e. this
 * function can be used to verify that an attribute is neither malformed nor
 * truncated.
 *
 * This function does not set errno in case of error since it is intended
 * for iterations. Thus, it returns true on success and false on error.
 *
 * The len parameter may be negative in the case of malformed messages during
 * attribute iteration, that is why we use a signed integer.
 */
bool mnl_attr_ok(const struct nlattr *attr, int len)
{
	return len >= (int)sizeof(struct nlattr) &&
	       attr->nla_len >= sizeof(struct nlattr) &&
	       (int)attr->nla_len <= len;
}
EXPORT_SYMBOL(mnl_attr_ok);

/**
 * mnl_attr_next - get the next attribute in the payload of a netlink message
 * \param attr pointer to the current attribute
 * \param len length of the remaining bytes in the buffer (passed by reference).
 *
 * This function returns a pointer to the next attribute after the one passed
 * as parameter. You have to use mnl_attr_ok() to ensure that the next
 * attribute is valid.
 */
struct nlattr *mnl_attr_next(const struct nlattr *attr)
{
	return (struct nlattr *)((void *)attr + MNL_ALIGN(attr->nla_len));
}
EXPORT_SYMBOL(mnl_attr_next);

/**
 * mnl_attr_type_valid - check if the attribute type is valid
 * \param attr pointer to attribute to be checked
 * \param max maximum attribute type
 *
 * This function allows to check if the attribute type is higher than the
 * maximum supported type. If the attribute type is invalid, this function
 * returns -1 and errno is explicitly set. On success, this function returns 1.
 *<commit_msg>attr: minor doc fix in mnl_attr_next()<commit_after>/*
 * (C) 2008-2012 by <NAME> <<EMAIL>>
 *
 * This program 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.
 */
#include <limits.h>	/* for INT_MAX */
#include <libmnl/libmnl.h>
#include <string.h>
#include <errno.h>
#include "internal.h"

/**
 * \defgroup attr Netlink attribute helpers
 *
 * Netlink Type-Length-Value (TLV) attribute:
 * \verbatim
	|<-- 2 bytes -->|<-- 2 bytes -->|<-- variable -->|
	-------------------------------------------------
	|     length    |      type     |      value     |
	-------------------------------------------------
	|<--------- header ------------>|<-- payload --->|
\endverbatim
 * The payload of the Netlink message contains sequences of attributes that are
 * expressed in TLV format.
 *
 * @{
 */

/**
 * mnl_attr_get_type - get type of netlink attribute
 * \param attr pointer to netlink attribute
 *
 * This function returns the attribute type.
 */
uint16_t mnl_attr_get_type(const struct nlattr *attr)
{
	return attr->nla_type & NLA_TYPE_MASK;
}
EXPORT_SYMBOL(mnl_attr_get_type);

/**
 * mnl_attr_get_len - get length of netlink attribute
 * \param attr pointer to netlink attribute
 *
 * This function returns the attribute length that is the attribute header
 * plus the attribute payload.
 */
uint16_t mnl_attr_get_len(const struct nlattr *attr)
{
	return attr->nla_len;
}
EXPORT_SYMBOL(mnl_attr_get_len);

/**
 * mnl_attr_get_payload_len - get the attribute payload-value length
 * \param attr pointer to netlink attribute
 *
 * This function returns the attribute payload-value length.
 */
uint16_t mnl_attr_get_payload_len(const struct nlattr *attr)
{
	return attr->nla_len - MNL_ATTR_HDRLEN;
}
EXPORT_SYMBOL(mnl_attr_get_payload_len);

/**
 * mnl_attr_get_payload - get pointer to the attribute payload
 * \param attr pointer to netlink attribute
 *
 * This function return a pointer to the attribute payload.
 */
void *mnl_attr_get_payload(const struct nlattr *attr)
{
	return (void *)attr + MNL_ATTR_HDRLEN;
}
EXPORT_SYMBOL(mnl_attr_get_payload);

/**
 * mnl_attr_ok - check if there is room for an attribute in a buffer
 * \param attr attribute that we want to check if there is room for
 * \param len remaining bytes in a buffer that contains the attribute
 *
 * This function is used to check that a buffer, which is supposed to contain
 * an attribute, has enough room for the attribute that it stores, i.e. this
 * function can be used to verify that an attribute is neither malformed nor
 * truncated.
 *
 * This function does not set errno in case of error since it is intended
 * for iterations. Thus, it returns true on success and false on error.
 *
 * The len parameter may be negative in the case of malformed messages during
 * attribute iteration, that is why we use a signed integer.
 */
bool mnl_attr_ok(const struct nlattr *attr, int len)
{
	return len >= (int)sizeof(struct nlattr) &&
	       attr->nla_len >= sizeof(struct nlattr) &&
	       (int)attr->nla_len <= len;
}
EXPORT_SYMBOL(mnl_attr_ok);

/**
 * mnl_attr_next - get the next attribute in the payload of a netlink message
 * \param attr pointer to the current attribute
 *
 * This function returns a pointer to the next attribute after the one passed
 * as parameter. You have to use mnl_attr_ok() to ensure that the next
 * attribute is valid.
 */
struct nlattr *mnl_attr_next(const struct nlattr *attr)
{
	return (struct nlattr *)((void *)attr + MNL_ALIGN(attr->nla_len));
}
EXPORT_SYMBOL(mnl_attr_next);

/**
 * mnl_attr_type_valid - check if the attribute type is valid
 * \param attr pointer to attribute to be checked
 * \param max maximum attribute type
 *
 * This function allows to check if the attribute type is higher than the
 * maximum supported type. If the attribute type is invalid, this function
 * returns -1 and errno is explicitly set. On success, this function returns 1.
 *
{'fraction_non_alphanumeric': 0.1132489040428641, 'fraction_numerical': 0.004870920603994155, 'mean_word_length': 3.3294675803900895, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 43, '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': '6555106', 'n_tokens_mistral': 2440, 'n_tokens_neox': 2396, 'n_words': 978}