Blame tools/util.c

Packit 56e23f
/**
Packit 56e23f
 * Tool utility functions
Packit 56e23f
 *
Packit 56e23f
 * Copyright (c) 2014 Red Hat <pmoore@redhat.com>
Packit 56e23f
 * Author: Paul Moore <paul@paul-moore.com>
Packit 56e23f
 */
Packit 56e23f
Packit 56e23f
/*
Packit 56e23f
 * This library is free software; you can redistribute it and/or modify it
Packit 56e23f
 * under the terms of version 2.1 of the GNU Lesser General Public License as
Packit 56e23f
 * published by the Free Software Foundation.
Packit 56e23f
 *
Packit 56e23f
 * This library is distributed in the hope that it will be useful, but WITHOUT
Packit 56e23f
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit 56e23f
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
Packit 56e23f
 * for more details.
Packit 56e23f
 *
Packit 56e23f
 * You should have received a copy of the GNU Lesser General Public License
Packit 56e23f
 * along with this library; if not, see <http://www.gnu.org/licenses>.
Packit 56e23f
 */
Packit 56e23f
Packit 56e23f
#include <errno.h>
Packit 56e23f
#include <stdlib.h>
Packit 56e23f
#include <stdio.h>
Packit 56e23f
#include <linux/audit.h>
Packit 56e23f
Packit 56e23f
#ifndef _BSD_SOURCE
Packit 56e23f
#define _BSD_SOURCE
Packit 56e23f
#endif
Packit 56e23f
#include <endian.h>
Packit 56e23f
Packit 56e23f
#include "util.h"
Packit 56e23f
Packit 56e23f
/* determine the native architecture */
Packit 56e23f
#if __i386__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_I386
Packit 56e23f
#elif __x86_64__
Packit 56e23f
#ifdef __ILP32__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_X86_64
Packit 56e23f
#else
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_X86_64
Packit 56e23f
#endif /* __ILP32__ */
Packit 56e23f
#elif __arm__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_ARM
Packit 56e23f
#elif __aarch64__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_AARCH64
Packit 56e23f
#elif __mips__ && _MIPS_SIM == _MIPS_SIM_ABI32
Packit 56e23f
#if __MIPSEB__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_MIPS
Packit 56e23f
#elif __MIPSEL__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_MIPSEL
Packit 56e23f
#endif /* _MIPS_SIM_ABI32 */
Packit 56e23f
#elif __mips__ && _MIPS_SIM == _MIPS_SIM_ABI64
Packit 56e23f
#if __MIPSEB__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_MIPS64
Packit 56e23f
#elif __MIPSEL__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_MIPSEL64
Packit 56e23f
#endif /* _MIPS_SIM_ABI64 */
Packit 56e23f
#elif __mips__ && _MIPS_SIM == _MIPS_SIM_NABI32
Packit 56e23f
#if __MIPSEB__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_MIPS64N32
Packit 56e23f
#elif __MIPSEL__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_MIPSEL64N32
Packit 56e23f
#endif /* _MIPS_SIM_NABI32 */
Packit 56e23f
#elif __hppa64__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_PARISC64
Packit 56e23f
#elif __hppa__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_PARISC
Packit 56e23f
#elif __PPC64__
Packit 56e23f
#ifdef __BIG_ENDIAN__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_PPC64
Packit 56e23f
#else
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_PPC64LE
Packit 56e23f
#endif
Packit 56e23f
#elif __PPC__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_PPC
Packit 56e23f
#elif __s390x__ /* s390x must be checked before s390 */
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_S390X
Packit 56e23f
#elif __s390__
Packit 56e23f
#define ARCH_NATIVE		AUDIT_ARCH_S390
Packit Service 10c312
#elif __riscv && __riscv_xlen == 64
Packit Service 10c312
#define ARCH_NATIVE		AUDIT_ARCH_RISCV64
Packit 56e23f
#else
Packit 56e23f
#error the simulator code needs to know about your machine type
Packit 56e23f
#endif
Packit 56e23f
Packit 56e23f
/* default to the native arch */
Packit 56e23f
uint32_t arch = ARCH_NATIVE;
Packit 56e23f
Packit 56e23f
/**
Packit 56e23f
 * Convert a 16-bit target integer into the host's endianess
Packit Service 10c312
 * @param arch_token the architecture token
Packit 56e23f
 * @param val the 16-bit integer
Packit 56e23f
 *
Packit 56e23f
 * Convert the endianess of the supplied value and return it to the caller.
Packit 56e23f
 *
Packit 56e23f
 */
Packit Service 10c312
uint16_t ttoh16(uint32_t arch_token, uint16_t val)
Packit 56e23f
{
Packit Service 10c312
	if (arch_token & __AUDIT_ARCH_LE)
Packit 56e23f
		return le16toh(val);
Packit 56e23f
	else
Packit 56e23f
		return be16toh(val);
Packit 56e23f
}
Packit 56e23f
Packit 56e23f
/**
Packit 56e23f
 * Convert a 32-bit target integer into the host's endianess
Packit Service 10c312
 * @param arch_token the architecture token
Packit 56e23f
 * @param val the 32-bit integer
Packit 56e23f
 *
Packit 56e23f
 * Convert the endianess of the supplied value and return it to the caller.
Packit 56e23f
 *
Packit 56e23f
 */
Packit Service 10c312
uint32_t ttoh32(uint32_t arch_token, uint32_t val)
Packit 56e23f
{
Packit Service 10c312
	if (arch_token & __AUDIT_ARCH_LE)
Packit 56e23f
		return le32toh(val);
Packit 56e23f
	else
Packit 56e23f
		return be32toh(val);
Packit 56e23f
}
Packit 56e23f
Packit 56e23f
/**
Packit 56e23f
 * Convert a 32-bit host integer into the target's endianess
Packit Service 10c312
 * @param arch_token the architecture token
Packit 56e23f
 * @param val the 32-bit integer
Packit 56e23f
 *
Packit 56e23f
 * Convert the endianess of the supplied value and return it to the caller.
Packit 56e23f
 *
Packit 56e23f
 */
Packit Service 10c312
uint32_t htot32(uint32_t arch_token, uint32_t val)
Packit 56e23f
{
Packit Service 10c312
	if (arch_token & __AUDIT_ARCH_LE)
Packit 56e23f
		return htole32(val);
Packit 56e23f
	else
Packit 56e23f
		return htobe32(val);
Packit 56e23f
}
Packit 56e23f
Packit 56e23f
/**
Packit 56e23f
 * Convert a 64-bit host integer into the target's endianess
Packit Service 10c312
 * @param arch_token the architecture token
Packit 56e23f
 * @param val the 64-bit integer
Packit 56e23f
 *
Packit 56e23f
 * Convert the endianess of the supplied value and return it to the caller.
Packit 56e23f
 *
Packit 56e23f
 */
Packit Service 10c312
uint64_t htot64(uint32_t arch_token, uint64_t val)
Packit 56e23f
{
Packit Service 10c312
	if (arch_token & __AUDIT_ARCH_LE)
Packit 56e23f
		return htole64(val);
Packit 56e23f
	else
Packit 56e23f
		return htobe64(val);
Packit 56e23f
}