summaryrefslogtreecommitdiffstats
path: root/libsyscalls_section_value.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2023-12-16 12:40:10 +0100
committerMattias Andrée <maandree@kth.se>2023-12-16 12:40:10 +0100
commit683f205402a99cfc8cea46c83ce9b46a42616d42 (patch)
treef6ee1619454a6ec8b9e31770bbbc2abf36aae2fd /libsyscalls_section_value.c
parentImprove portability (diff)
downloadlibsyscalls-683f205402a99cfc8cea46c83ce9b46a42616d42.tar.gz
libsyscalls-683f205402a99cfc8cea46c83ce9b46a42616d42.tar.bz2
libsyscalls-683f205402a99cfc8cea46c83ce9b46a42616d42.tar.xz
All kinds of stuff
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to '')
-rw-r--r--libsyscalls_section_value.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/libsyscalls_section_value.c b/libsyscalls_section_value.c
new file mode 100644
index 0000000..dc86cca
--- /dev/null
+++ b/libsyscalls_section_value.c
@@ -0,0 +1,74 @@
+/* See LICENSE file for copyright and license details. */
+#include "common.h"
+
+
+enum libsyscalls_error
+libsyscalls_section_value(unsigned long long int value_in, size_t bits,
+ enum libsyscalls_datatype_section section, unsigned long long int *value_out)
+{
+ unsigned long long int value = value_in, mask, shift;
+
+ if (bits > sizeof(value) / sizeof(char) * CHAR_BIT ||
+ bits & (size_t)(LIBSYSCALLS_GET_SECTION_FRACTION(section) - 1U))
+ return LIBSYSCALLS_E_INVAL;
+
+ bits /= (size_t)LIBSYSCALLS_GET_SECTION_FRACTION(section);
+ if (bits == sizeof(mask) / sizeof(char) * CHAR_BIT)
+ mask = ~0ULL;
+ else
+ mask = (1ULL << bits) - 1ULL;
+
+ switch (section) {
+ case LIBSYSCALLS_SECTION_UNDETERMINED:
+ case LIBSYSCALLS_SECTION_WHOLE: /* 0xFFFFFFFFFFFFFFFF */
+ case LIBSYSCALLS_SECTION_LOWER_HALF: /* 0x00000000FFFFFFFF */
+ case LIBSYSCALLS_SECTION_LOWER_QUARTER: /* 0x000000000000FFFF */
+ break;
+
+ case LIBSYSCALLS_SECTION_UPPER_HALF: /* 0xFFFFFFFF00000000 */
+ case LIBSYSCALLS_SECTION_LOWER_MID_QUARTER: /* 0x00000000FFFF0000 */
+ value >>= bits;
+ break;
+
+ case LIBSYSCALLS_SECTION_INNER_HALF: /* 0x0000FFFFFFFF0000 */
+ value >>= bits / 2;
+ break;
+
+ case LIBSYSCALLS_SECTION_UPPER_MID_QUARTER: /* 0x0000FFFF00000000 */
+ value >>= bits * 2;
+ break;
+
+ case LIBSYSCALLS_SECTION_UPPER_QUARTER: /* 0xFFFF000000000000 */
+ value >>= bits * 3;
+ break;
+
+ case LIBSYSCALLS_SECTION_OUTER_HALF: /* 0xFFFF00000000FFFF */
+ value = ((value & (mask << (bits + bits / 2))) >> bits) | (value & (mask >> (bits / 2)));
+ break;
+
+ case LIBSYSCALLS_SECTION_ODD_QUARTERS_AS_HALF: /* 0xFFFF0000FFFF0000 */
+ value >>= bits / 2;
+ /* fall through */
+ case LIBSYSCALLS_SECTION_EVEN_QUARTERS_AS_HALF: /* 0x0000FFFF0000FFFF */
+ value = ((value & (mask << (bits / 2)) & ~mask) >> (bits / 2)) | (value & (mask >> (bits / 2)));
+ break;
+
+ case LIBSYSCALLS_SECTION_ODD_BYTES_AS_HALF: /* 0xFF00FF00FF00FF00 */
+ value_in >>= 8;
+ /* fall through */
+ case LIBSYSCALLS_SECTION_EVEN_BYTES_AS_HALF: /* 0x00FF00FF00FF00FF */
+ value = 0;
+ for (shift = 0; value_in; value_in >>= 16, shift += 8)
+ value |= (value_in & 0x00FFU) << shift;
+ break;
+
+ default:
+ return LIBSYSCALLS_E_INVAL;
+ }
+
+ value &= mask;
+masked:
+ if (value_out)
+ *value_out = value;
+ return LIBSYSCALLS_E_OK;
+}