Bundle libseccomp 2.3.1
[linux-seccomp.git] / libseccomp / tools / util.c
CommitLineData
8befd5cc
MG
1/**
2 * Tool utility functions
3 *
4 * Copyright (c) 2014 Red Hat <pmoore@redhat.com>
5 * Author: Paul Moore <paul@paul-moore.com>
6 */
7
8/*
9 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of version 2.1 of the GNU Lesser General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This library is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, see <http://www.gnu.org/licenses>.
20 */
21
22#include <errno.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <linux/audit.h>
26
27#ifndef _BSD_SOURCE
28#define _BSD_SOURCE
29#endif
30#include <endian.h>
31
32#include "util.h"
33
34/* determine the native architecture */
35#if __i386__
36#define ARCH_NATIVE AUDIT_ARCH_I386
37#elif __x86_64__
38#ifdef __ILP32__
39#define ARCH_NATIVE AUDIT_ARCH_X86_64
40#else
41#define ARCH_NATIVE AUDIT_ARCH_X86_64
42#endif /* __ILP32__ */
43#elif __arm__
44#define ARCH_NATIVE AUDIT_ARCH_ARM
45#elif __aarch64__
46#define ARCH_NATIVE AUDIT_ARCH_AARCH64
47#elif __mips__ && _MIPS_SIM == _MIPS_SIM_ABI32
48#if __MIPSEB__
49#define ARCH_NATIVE AUDIT_ARCH_MIPS
50#elif __MIPSEL__
51#define ARCH_NATIVE AUDIT_ARCH_MIPSEL
52#endif /* _MIPS_SIM_ABI32 */
53#elif __mips__ && _MIPS_SIM == _MIPS_SIM_ABI64
54#if __MIPSEB__
55#define ARCH_NATIVE AUDIT_ARCH_MIPS64
56#elif __MIPSEL__
57#define ARCH_NATIVE AUDIT_ARCH_MIPSEL64
58#endif /* _MIPS_SIM_ABI64 */
59#elif __mips__ && _MIPS_SIM == _MIPS_SIM_NABI32
60#if __MIPSEB__
61#define ARCH_NATIVE AUDIT_ARCH_MIPS64N32
62#elif __MIPSEL__
63#define ARCH_NATIVE AUDIT_ARCH_MIPSEL64N32
64#endif /* _MIPS_SIM_NABI32 */
65#elif __PPC64__
66#ifdef __BIG_ENDIAN__
67#define ARCH_NATIVE AUDIT_ARCH_PPC64
68#else
69#define ARCH_NATIVE AUDIT_ARCH_PPC64LE
70#endif
71#elif __PPC__
72#define ARCH_NATIVE AUDIT_ARCH_PPC
73#elif __s390x__ /* s390x must be checked before s390 */
74#define ARCH_NATIVE AUDIT_ARCH_S390X
75#elif __s390__
76#define ARCH_NATIVE AUDIT_ARCH_S390
77#else
78#error the simulator code needs to know about your machine type
79#endif
80
81/* default to the native arch */
82uint32_t arch = ARCH_NATIVE;
83
84/**
85 * Convert a 16-bit target integer into the host's endianess
86 * @param arch the architecture token
87 * @param val the 16-bit integer
88 *
89 * Convert the endianess of the supplied value and return it to the caller.
90 *
91 */
92uint16_t ttoh16(uint32_t arch, uint16_t val)
93{
94 if (arch & __AUDIT_ARCH_LE)
95 return le16toh(val);
96 else
97 return be16toh(val);
98}
99
100/**
101 * Convert a 32-bit target integer into the host's endianess
102 * @param arch the architecture token
103 * @param val the 32-bit integer
104 *
105 * Convert the endianess of the supplied value and return it to the caller.
106 *
107 */
108uint32_t ttoh32(uint32_t arch, uint32_t val)
109{
110 if (arch & __AUDIT_ARCH_LE)
111 return le32toh(val);
112 else
113 return be32toh(val);
114}
115
116/**
117 * Convert a 32-bit host integer into the target's endianess
118 * @param arch the architecture token
119 * @param val the 32-bit integer
120 *
121 * Convert the endianess of the supplied value and return it to the caller.
122 *
123 */
124uint32_t htot32(uint32_t arch, uint32_t val)
125{
126 if (arch & __AUDIT_ARCH_LE)
127 return htole32(val);
128 else
129 return htobe32(val);
130}
131
132/**
133 * Convert a 64-bit host integer into the target's endianess
134 * @param arch the architecture token
135 * @param val the 64-bit integer
136 *
137 * Convert the endianess of the supplied value and return it to the caller.
138 *
139 */
140uint64_t htot64(uint32_t arch, uint64_t val)
141{
142 if (arch & __AUDIT_ARCH_LE)
143 return htole64(val);
144 else
145 return htobe64(val);
146}
This page took 0.016498 seconds and 4 git commands to generate.