From 3996e391054a1c02ab62e1541ae21a8204bd5d0a Mon Sep 17 00:00:00 2001 From: AmitKumar Date: Aug 03 2017 09:08:15 +0000 Subject: Moving headers used by both server and client to special folder These are the header files which are used by both client and server: src/util/io.h src/util/murmurhash3.h src/util/util_safealign.h This patch is about moving these header files to special folder (src/shared). It will be easier to identify these headers when looking for them in the src tree. util_safalign.h is renamed as safalign.h because util_ namespace is appropriate when this file belonged to the util's folder which is no longer the case. Resolves: https://pagure.io/SSSD/sssd/issue/1898 Reviewed-by: Fabiano FidĂȘncio --- diff --git a/Makefile.am b/Makefile.am index 62e0baf..faa2fba 100644 --- a/Makefile.am +++ b/Makefile.am @@ -652,7 +652,6 @@ dist_noinst_HEADERS = \ src/util/dlinklist.h \ src/util/debug.h \ src/util/util.h \ - src/util/io.h \ src/util/util_errors.h \ src/util/safe-format-string.h \ src/util/session_recording.h \ @@ -673,13 +672,11 @@ dist_noinst_HEADERS = \ src/util/refcount.h \ src/util/find_uid.h \ src/util/user_info_msg.h \ - src/util/murmurhash3.h \ src/util/mmap_cache.h \ src/util/atomic_io.h \ src/util/auth_utils.h \ src/util/authtok.h \ src/util/authtok-utils.h \ - src/util/util_safealign.h \ src/util/util_sss_idmap.h \ src/util/util_creds.h \ src/util/inotify.h \ @@ -849,6 +846,9 @@ dist_noinst_HEADERS = \ src/tools/common/sss_colondb.h \ src/tools/sssctl/sssctl.h \ src/util/probes.h \ + src/shared/io.h \ + src/shared/murmurhash3.h \ + src/shared/safealign.h \ $(NULL) diff --git a/src/lib/idmap/sss_idmap.c b/src/lib/idmap/sss_idmap.c index 51338c5..56ba904 100644 --- a/src/lib/idmap/sss_idmap.c +++ b/src/lib/idmap/sss_idmap.c @@ -29,7 +29,7 @@ #include "lib/idmap/sss_idmap.h" #include "lib/idmap/sss_idmap_private.h" -#include "util/murmurhash3.h" +#include "shared/murmurhash3.h" #define SID_FMT "%s-%d" #define SID_STR_MAX_LEN 1024 diff --git a/src/providers/ldap/sdap_idmap.c b/src/providers/ldap/sdap_idmap.c index 0fda815..f5ac511 100644 --- a/src/providers/ldap/sdap_idmap.c +++ b/src/providers/ldap/sdap_idmap.c @@ -20,9 +20,9 @@ along with this program. If not, see . */ +#include "shared/murmurhash3.h" #include "util/util.h" #include "util/dlinklist.h" -#include "util/murmurhash3.h" #include "providers/ldap/sdap_idmap.h" #include "util/util_sss_idmap.h" diff --git a/src/python/pysss_murmur.c b/src/python/pysss_murmur.c index a7519e8..060d29d 100644 --- a/src/python/pysss_murmur.c +++ b/src/python/pysss_murmur.c @@ -23,7 +23,7 @@ #include #include "util/sss_python.h" -#include "util/murmurhash3.h" +#include "shared/murmurhash3.h" PyDoc_STRVAR(murmurhash3_doc, "murmurhash3(key, key_len, seed) -> 32bit integer hash\n\ diff --git a/src/shared/io.h b/src/shared/io.h new file mode 100644 index 0000000..5a545b6 --- /dev/null +++ b/src/shared/io.h @@ -0,0 +1,28 @@ +/* + SSSD + + SSSD Utility functions + + Copyright (C) Lukas Slebodnik 2013 + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef _UTIL_IO_H_ +#define _UTIL_IO_H_ + +int sss_open_cloexec(const char *pathname, int flags, int *ret); +int sss_openat_cloexec(int dir_fd, const char *pathname, int flags, int *ret); + +#endif /* _UTIL_IO_H_ */ diff --git a/src/shared/murmurhash3.h b/src/shared/murmurhash3.h new file mode 100644 index 0000000..3cea68e --- /dev/null +++ b/src/shared/murmurhash3.h @@ -0,0 +1,16 @@ +/* This file is based on the public domain MurmurHash3 from Austin Appleby: + * http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp + * + * We use only the 32 bit variant because the 2 produce different result while + * we need to produce the same result regardless of the architecture as + * clients can be both 64 or 32 bit at the same time. + */ + +#ifndef _UTIL_MURMURHASH3_H_ +#define _UTIL_MURMURHASH3_H_ + +#include + +uint32_t murmurhash3(const char *key, int len, uint32_t seed); + +#endif /* _UTIL_MURMURHASH3_H_ */ diff --git a/src/shared/safealign.h b/src/shared/safealign.h new file mode 100644 index 0000000..cffc1c5 --- /dev/null +++ b/src/shared/safealign.h @@ -0,0 +1,141 @@ +/* + SSSD + + Authors: + Simo Sorce + + Copyright (C) Red Hat, Inc 2007 + + 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 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . +*/ + +#ifndef _SAFEALIGN_H +#define _SAFEALIGN_H + +#include +#include + +/* Use this macro to suppress alignment warnings (use it + * only to suppress false-positives) */ +#define DISCARD_ALIGN(ptr, type) ((type)(void *)(ptr)) + +#define IS_ALIGNED(ptr, type) \ + ((uintptr_t)(ptr) % sizeof(type) == 0) + +#define PADDING_SIZE(base, type) \ + ((sizeof(type) - ((base) % sizeof(type))) % sizeof(type)) + +#define SIZE_T_OVERFLOW(current, add) \ + (((size_t)(add)) > (SIZE_MAX - ((size_t)(current)))) + +static inline void +safealign_memcpy(void *dest, const void *src, size_t n, size_t *counter) +{ + memcpy(dest, src, n); + if (counter) { + *counter += n; + } +} + +#define SAFEALIGN_SETMEM_VALUE(dest, value, type, pctr) do { \ + type CV_MACRO_val = (type)(value); \ + safealign_memcpy(dest, &CV_MACRO_val, sizeof(type), pctr); \ +} while(0) + +/* SAFEALIGN_COPY_INT64(void *dest, void *src, size_t *pctr) + * This macro will safely copy sizeof(int64_t) bytes from memory + * location pointed by 'src' to memory location pointed by 'dest'. + * If the 'pctr' pointer is not NULL, the value it points to will + * be incremented by sizeof(int64_t). */ +#define SAFEALIGN_COPY_INT64(dest, src, pctr) \ + safealign_memcpy(dest, src, sizeof(int64_t), pctr) + +/* SAFEALIGN_SETMEM_INT64(void *dest, int64_t value, size_t *pctr) + * This macro will safely assign an int64_t value to the memory + * location pointed by 'dest'. If the 'pctr' pointer is not NULL, + * the value it points to will be incremented by sizeof(int64_t). */ +#define SAFEALIGN_SETMEM_INT64(dest, value, pctr) \ + SAFEALIGN_SETMEM_VALUE(dest, value, int64_t, pctr) + +/* SAFEALIGN_COPY_UINT32(void *dest, void *src, size_t *pctr) */ +#define SAFEALIGN_COPY_UINT32(dest, src, pctr) \ + safealign_memcpy(dest, src, sizeof(uint32_t), pctr) + +/* SAFEALIGN_SETMEM_UINT32(void *dest, uint32_t value, size_t *pctr) */ +#define SAFEALIGN_SETMEM_UINT32(dest, value, pctr) \ + SAFEALIGN_SETMEM_VALUE(dest, value, uint32_t, pctr) + +/* SAFEALIGN_COPY_INT32(void *dest, void *src, size_t *pctr) */ +#define SAFEALIGN_COPY_INT32(dest, src, pctr) \ + safealign_memcpy(dest, src, sizeof(int32_t), pctr) + +/* SAFEALIGN_SETMEM_INT32(void *dest, int32_t value, size_t *pctr) */ +#define SAFEALIGN_SETMEM_INT32(dest, value, pctr) \ + SAFEALIGN_SETMEM_VALUE(dest, value, int32_t, pctr) + +/* SAFEALIGN_COPY_UINT16(void *dest, void *src, size_t *pctr) */ +#define SAFEALIGN_COPY_UINT16(dest, src, pctr) \ + safealign_memcpy(dest, src, sizeof(uint16_t), pctr) + +/* SAFEALIGN_SETMEM_UINT16(void *dest, uint16_t value, size_t *pctr) */ +#define SAFEALIGN_SETMEM_UINT16(dest, value, pctr) \ + SAFEALIGN_SETMEM_VALUE(dest, value, uint16_t, pctr) + +/* These macros are the same as their equivalents without _CHECK suffix, + * but additionally make the caller return EINVAL immediatelly if *pctr + * would excceed len. */ +#define SAFEALIGN_COPY_UINT32_CHECK(dest, src, len, pctr) do { \ + if ((*(pctr) + sizeof(uint32_t)) > (len) || \ + SIZE_T_OVERFLOW(*(pctr), sizeof(uint32_t))) { return EINVAL; } \ + safealign_memcpy(dest, src, sizeof(uint32_t), pctr); \ +} while(0) + +#define SAFEALIGN_COPY_INT32_CHECK(dest, src, len, pctr) do { \ + if ((*(pctr) + sizeof(int32_t)) > (len) || \ + SIZE_T_OVERFLOW(*(pctr), sizeof(int32_t))) { return EINVAL; } \ + safealign_memcpy(dest, src, sizeof(int32_t), pctr); \ +} while(0) + +#define SAFEALIGN_COPY_UINT16_CHECK(dest, src, len, pctr) do { \ + if ((*(pctr) + sizeof(uint16_t)) > (len) || \ + SIZE_T_OVERFLOW(*(pctr), sizeof(uint16_t))) { return EINVAL; } \ + safealign_memcpy(dest, src, sizeof(uint16_t), pctr); \ +} while(0) + +#define SAFEALIGN_SETMEM_STRING(dest, value, length, pctr) do { \ + const char *CV_MACRO_val = (const char *)(value); \ + safealign_memcpy(dest, CV_MACRO_val, sizeof(char) * length, pctr); \ +} while(0) + +#define SAFEALIGN_MEMCPY_CHECK(dest, src, srclen, len, pctr) do { \ + if ((*(pctr) + srclen) > (len) || \ + SIZE_T_OVERFLOW(*(pctr), srclen)) { return EINVAL; } \ + safealign_memcpy(dest, src, srclen, pctr); \ +} while(0) + +#define SAFEALIGN_COPY_UINT8_CHECK(dest, src, len, pctr) do { \ + if ((*(pctr) + sizeof(uint8_t)) > (len) || \ + SIZE_T_OVERFLOW(*(pctr), sizeof(uint8_t))) { return EINVAL; } \ + safealign_memcpy(dest, src, sizeof(uint8_t), pctr); \ +} while(0) + +/* Aliases for backward compatibility. */ +#define SAFEALIGN_SET_VALUE SAFEALIGN_SETMEM_VALUE +#define SAFEALIGN_SET_INT64 SAFEALIGN_SETMEM_INT64 +#define SAFEALIGN_SET_UINT32 SAFEALIGN_SETMEM_UINT32 +#define SAFEALIGN_SET_INT32 SAFEALIGN_SETMEM_INT32 +#define SAFEALIGN_SET_UINT16 SAFEALIGN_SETMEM_UINT16 +#define SAFEALIGN_SET_STRING SAFEALIGN_SETMEM_STRING + +#endif /* _SAFEALIGN_H */ diff --git a/src/sss_client/nss_mc_common.c b/src/sss_client/nss_mc_common.c index 6cff2e1..696d672 100644 --- a/src/sss_client/nss_mc_common.c +++ b/src/sss_client/nss_mc_common.c @@ -32,7 +32,7 @@ #include #include "nss_mc.h" #include "sss_cli.h" -#include "util/io.h" +#include "shared/io.h" /* FIXME: hook up to library destructor to avoid leaks */ /* FIXME: temporarily open passwd file on our own, later we will probably diff --git a/src/sss_client/nss_mc_group.c b/src/sss_client/nss_mc_group.c index aacf59d..ce88d42 100644 --- a/src/sss_client/nss_mc_group.c +++ b/src/sss_client/nss_mc_group.c @@ -27,7 +27,7 @@ #include #include #include "nss_mc.h" -#include "util/util_safealign.h" +#include "shared/safealign.h" struct sss_cli_mc_ctx gr_mc_ctx = { UNINITIALIZED, -1, 0, NULL, 0, NULL, 0, NULL, 0, 0 }; diff --git a/src/sss_client/nss_mc_initgr.c b/src/sss_client/nss_mc_initgr.c index 74143d9..a77088d 100644 --- a/src/sss_client/nss_mc_initgr.c +++ b/src/sss_client/nss_mc_initgr.c @@ -30,7 +30,7 @@ #include #include #include "nss_mc.h" -#include "util/util_safealign.h" +#include "shared/safealign.h" struct sss_cli_mc_ctx initgr_mc_ctx = { UNINITIALIZED, -1, 0, NULL, 0, NULL, 0, NULL, 0, 0 }; diff --git a/src/sss_client/sss_cli.h b/src/sss_client/sss_cli.h index d419840..038406d 100644 --- a/src/sss_client/sss_cli.h +++ b/src/sss_client/sss_cli.h @@ -32,7 +32,7 @@ #include #include -#include "util/util_safealign.h" +#include "shared/safealign.h" #ifndef HAVE_ERRNO_T #define HAVE_ERRNO_T diff --git a/src/tests/cmocka/test_inotify.c b/src/tests/cmocka/test_inotify.c index 1f8561d..43e36e8 100644 --- a/src/tests/cmocka/test_inotify.c +++ b/src/tests/cmocka/test_inotify.c @@ -26,7 +26,7 @@ #include #include "limits.h" -#include "util/io.h" +#include "shared/io.h" #include "util/inotify.h" #include "util/util.h" #include "tests/common.h" diff --git a/src/tests/cmocka/test_io.c b/src/tests/cmocka/test_io.c index 8d29a05..20475a0 100644 --- a/src/tests/cmocka/test_io.c +++ b/src/tests/cmocka/test_io.c @@ -36,7 +36,7 @@ #include #include "limits.h" -#include "util/io.h" +#include "shared/io.h" #include "util/util.h" #include "tests/common.h" diff --git a/src/tests/util-tests.c b/src/tests/util-tests.c index a1b3fdd..d9f2b91 100644 --- a/src/tests/util-tests.c +++ b/src/tests/util-tests.c @@ -34,7 +34,7 @@ #include "util/util.h" #include "util/sss_utf8.h" -#include "util/murmurhash3.h" +#include "shared/murmurhash3.h" #include "tests/common_check.h" #define FILENAME_TEMPLATE "tests-atomicio-XXXXXX" diff --git a/src/util/io.c b/src/util/io.c index 42e7563..4d442b4 100644 --- a/src/util/io.c +++ b/src/util/io.c @@ -28,7 +28,7 @@ #include #include -#include "util/io.h" +#include "shared/io.h" /* CAUTION: * This file have to be minimalist and cannot include DEBUG macros diff --git a/src/util/io.h b/src/util/io.h deleted file mode 100644 index 8d10ed9..0000000 --- a/src/util/io.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - SSSD - - SSSD Utility functions - - Copyright (C) Lukas Slebodnik 2013 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#ifndef _UTIL_IO_H_ -#define _UTIL_IO_H_ - -/* CAUTION: - * This file is also used in sss_client (pam, nss). Therefore it have to be - * minimalist and cannot include DEBUG macros or header file util.h. - */ - -int sss_open_cloexec(const char *pathname, int flags, int *ret); -int sss_openat_cloexec(int dir_fd, const char *pathname, int flags, int *ret); - -#endif /* _UTIL_IO_H_ */ diff --git a/src/util/mmap_cache.h b/src/util/mmap_cache.h index 22c1ae6..63e0960 100644 --- a/src/util/mmap_cache.h +++ b/src/util/mmap_cache.h @@ -22,7 +22,7 @@ #ifndef _MMAP_CACHE_H_ #define _MMAP_CACHE_H_ -#include "util/murmurhash3.h" +#include "shared/murmurhash3.h" /* NOTE: all the code here assumes that writing a uint32_t nto mmapped diff --git a/src/util/murmurhash3.c b/src/util/murmurhash3.c index 061e64e..f8db9d2 100644 --- a/src/util/murmurhash3.c +++ b/src/util/murmurhash3.c @@ -11,7 +11,7 @@ #include #include "config.h" -#include "util/murmurhash3.h" +#include "shared/murmurhash3.h" #include "util/sss_endian.h" static uint32_t rotl(uint32_t x, int8_t r) diff --git a/src/util/murmurhash3.h b/src/util/murmurhash3.h deleted file mode 100644 index 6910e59..0000000 --- a/src/util/murmurhash3.h +++ /dev/null @@ -1,20 +0,0 @@ -/* This file is based on the public domain MurmurHash3 from Austin Appleby: - * http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp - * - * We use only the 32 bit variant because the 2 produce different result while - * we need to produce the same result regardless of the architecture as - * clients can be both 64 or 32 bit at the same time. - */ - -#ifndef _UTIL_MURMURHASH3_H_ -#define _UTIL_MURMURHASH3_H_ - -#include - -/* CAUTION: - * This file is also used in sss_client (pam, nss). Therefore it have to be - * minimalist and cannot include DEBUG macros or header file util.h. - */ -uint32_t murmurhash3(const char *key, int len, uint32_t seed); - -#endif /* _UTIL_MURMURHASH3_H_ */ diff --git a/src/util/util.h b/src/util/util.h index 934fae3..81d5e9b 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -39,9 +39,10 @@ #include #include "confdb/confdb.h" +#include "shared/io.h" +#include "shared/safealign.h" #include "util/atomic_io.h" #include "util/util_errors.h" -#include "util/util_safealign.h" #include "util/sss_format.h" #include "util/debug.h" @@ -591,7 +592,6 @@ errno_t get_dom_names(TALLOC_CTX *mem_ctx, /* from util_lock.c */ errno_t sss_br_lock_file(int fd, size_t start, size_t len, int num_tries, useconds_t wait); -#include "io.h" #ifdef HAVE_PAC_RESPONDER #define BUILD_WITH_PAC_RESPONDER true diff --git a/src/util/util_safealign.h b/src/util/util_safealign.h deleted file mode 100644 index 57f04a1..0000000 --- a/src/util/util_safealign.h +++ /dev/null @@ -1,147 +0,0 @@ -/* - SSSD - - Authors: - Simo Sorce - - Copyright (C) Red Hat, Inc 2007 - - 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 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with this program. If not, see . -*/ - -/* CAUTION: - * This file is also used in sss_client (pam, nss). Therefore it has to be - * minimalist and cannot include DEBUG macros or header file util.h. - */ - - -#ifndef _UTIL_SAFEALIGN_H -#define _UTIL_SAFEALIGN_H - -#include -#include - -/* Use this macro to suppress alignment warnings (use it - * only to suppress false-positives) */ -#define DISCARD_ALIGN(ptr, type) ((type)(void *)(ptr)) - -#define IS_ALIGNED(ptr, type) \ - ((uintptr_t)(ptr) % sizeof(type) == 0) - -#define PADDING_SIZE(base, type) \ - ((sizeof(type) - ((base) % sizeof(type))) % sizeof(type)) - -#define SIZE_T_OVERFLOW(current, add) \ - (((size_t)(add)) > (SIZE_MAX - ((size_t)(current)))) - -static inline void -safealign_memcpy(void *dest, const void *src, size_t n, size_t *counter) -{ - memcpy(dest, src, n); - if (counter) { - *counter += n; - } -} - -#define SAFEALIGN_SETMEM_VALUE(dest, value, type, pctr) do { \ - type CV_MACRO_val = (type)(value); \ - safealign_memcpy(dest, &CV_MACRO_val, sizeof(type), pctr); \ -} while(0) - -/* SAFEALIGN_COPY_INT64(void *dest, void *src, size_t *pctr) - * This macro will safely copy sizeof(int64_t) bytes from memory - * location pointed by 'src' to memory location pointed by 'dest'. - * If the 'pctr' pointer is not NULL, the value it points to will - * be incremented by sizeof(int64_t). */ -#define SAFEALIGN_COPY_INT64(dest, src, pctr) \ - safealign_memcpy(dest, src, sizeof(int64_t), pctr) - -/* SAFEALIGN_SETMEM_INT64(void *dest, int64_t value, size_t *pctr) - * This macro will safely assign an int64_t value to the memory - * location pointed by 'dest'. If the 'pctr' pointer is not NULL, - * the value it points to will be incremented by sizeof(int64_t). */ -#define SAFEALIGN_SETMEM_INT64(dest, value, pctr) \ - SAFEALIGN_SETMEM_VALUE(dest, value, int64_t, pctr) - -/* SAFEALIGN_COPY_UINT32(void *dest, void *src, size_t *pctr) */ -#define SAFEALIGN_COPY_UINT32(dest, src, pctr) \ - safealign_memcpy(dest, src, sizeof(uint32_t), pctr) - -/* SAFEALIGN_SETMEM_UINT32(void *dest, uint32_t value, size_t *pctr) */ -#define SAFEALIGN_SETMEM_UINT32(dest, value, pctr) \ - SAFEALIGN_SETMEM_VALUE(dest, value, uint32_t, pctr) - -/* SAFEALIGN_COPY_INT32(void *dest, void *src, size_t *pctr) */ -#define SAFEALIGN_COPY_INT32(dest, src, pctr) \ - safealign_memcpy(dest, src, sizeof(int32_t), pctr) - -/* SAFEALIGN_SETMEM_INT32(void *dest, int32_t value, size_t *pctr) */ -#define SAFEALIGN_SETMEM_INT32(dest, value, pctr) \ - SAFEALIGN_SETMEM_VALUE(dest, value, int32_t, pctr) - -/* SAFEALIGN_COPY_UINT16(void *dest, void *src, size_t *pctr) */ -#define SAFEALIGN_COPY_UINT16(dest, src, pctr) \ - safealign_memcpy(dest, src, sizeof(uint16_t), pctr) - -/* SAFEALIGN_SETMEM_UINT16(void *dest, uint16_t value, size_t *pctr) */ -#define SAFEALIGN_SETMEM_UINT16(dest, value, pctr) \ - SAFEALIGN_SETMEM_VALUE(dest, value, uint16_t, pctr) - -/* These macros are the same as their equivalents without _CHECK suffix, - * but additionally make the caller return EINVAL immediatelly if *pctr - * would excceed len. */ -#define SAFEALIGN_COPY_UINT32_CHECK(dest, src, len, pctr) do { \ - if ((*(pctr) + sizeof(uint32_t)) > (len) || \ - SIZE_T_OVERFLOW(*(pctr), sizeof(uint32_t))) { return EINVAL; } \ - safealign_memcpy(dest, src, sizeof(uint32_t), pctr); \ -} while(0) - -#define SAFEALIGN_COPY_INT32_CHECK(dest, src, len, pctr) do { \ - if ((*(pctr) + sizeof(int32_t)) > (len) || \ - SIZE_T_OVERFLOW(*(pctr), sizeof(int32_t))) { return EINVAL; } \ - safealign_memcpy(dest, src, sizeof(int32_t), pctr); \ -} while(0) - -#define SAFEALIGN_COPY_UINT16_CHECK(dest, src, len, pctr) do { \ - if ((*(pctr) + sizeof(uint16_t)) > (len) || \ - SIZE_T_OVERFLOW(*(pctr), sizeof(uint16_t))) { return EINVAL; } \ - safealign_memcpy(dest, src, sizeof(uint16_t), pctr); \ -} while(0) - -#define SAFEALIGN_SETMEM_STRING(dest, value, length, pctr) do { \ - const char *CV_MACRO_val = (const char *)(value); \ - safealign_memcpy(dest, CV_MACRO_val, sizeof(char) * length, pctr); \ -} while(0) - -#define SAFEALIGN_MEMCPY_CHECK(dest, src, srclen, len, pctr) do { \ - if ((*(pctr) + srclen) > (len) || \ - SIZE_T_OVERFLOW(*(pctr), srclen)) { return EINVAL; } \ - safealign_memcpy(dest, src, srclen, pctr); \ -} while(0) - -#define SAFEALIGN_COPY_UINT8_CHECK(dest, src, len, pctr) do { \ - if ((*(pctr) + sizeof(uint8_t)) > (len) || \ - SIZE_T_OVERFLOW(*(pctr), sizeof(uint8_t))) { return EINVAL; } \ - safealign_memcpy(dest, src, sizeof(uint8_t), pctr); \ -} while(0) - -/* Aliases for backward compatibility. */ -#define SAFEALIGN_SET_VALUE SAFEALIGN_SETMEM_VALUE -#define SAFEALIGN_SET_INT64 SAFEALIGN_SETMEM_INT64 -#define SAFEALIGN_SET_UINT32 SAFEALIGN_SETMEM_UINT32 -#define SAFEALIGN_SET_INT32 SAFEALIGN_SETMEM_INT32 -#define SAFEALIGN_SET_UINT16 SAFEALIGN_SETMEM_UINT16 -#define SAFEALIGN_SET_STRING SAFEALIGN_SETMEM_STRING - -#endif /* _UTIL_SAFEALIGN_H */