Line data Source code
1 : /**
2 : * The MIT License (MIT)
3 : *
4 : * Copyright (c) 2021 RSK Labs Ltd
5 : *
6 : * Permission is hereby granted, free of charge, to any person obtaining a copy
7 : * of this software and associated documentation files (the "Software"), to
8 : * deal in the Software without restriction, including without limitation the
9 : * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 : * sell copies of the Software, and to permit persons to whom the Software is
11 : * furnished to do so, subject to the following conditions:
12 : *
13 : * The above copyright notice and this permission notice shall be included in
14 : * all copies or substantial portions of the Software.
15 : *
16 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 : * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 : * IN THE SOFTWARE.
23 : */
24 :
25 : #ifndef __MEMUTIL_H
26 : #define __MEMUTIL_H
27 :
28 : #include <stdint.h>
29 :
30 : #include "common_requirements.h"
31 :
32 : #define MEMMOVE_ZERO_OFFSET 0
33 :
34 : #define SAFE_MEMMOVE( \
35 : dst, dst_size, dst_off, src, src_size, src_off, n, ERR_EXPR) \
36 : if (!safe_memmove(dst, dst_size, dst_off, src, src_size, src_off, n)) { \
37 : ERR_EXPR; \
38 : }
39 :
40 : __attribute__((always_inline)) static inline int safe_memmove(
41 : const void *dst,
42 : unsigned int dst_size,
43 : unsigned int dst_off,
44 : const void *src,
45 : unsigned int src_size,
46 : unsigned int src_off,
47 : unsigned int n) {
48 :
49 35453 : if (n + dst_off < n || n + src_off < n ||
50 35447 : (uintptr_t)dst + (uintptr_t)dst_off < (uintptr_t)dst ||
51 35446 : (uintptr_t)src + (uintptr_t)src_off < (uintptr_t)src ||
52 35445 : n + dst_off > dst_size || n + src_off > src_size) {
53 :
54 18 : return false;
55 : } else {
56 35435 : platform_memmove(
57 : (unsigned char *)dst + dst_off, (unsigned char *)src + src_off, n);
58 35435 : return true;
59 : }
60 : }
61 :
62 : #endif // __MEMUTIL_H
|