• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions
5  * are met:
6  * 1. Redistributions of source code must retain the above copyright
7  *    notice, this list of conditions and the following disclaimer.
8  * 2. Redistributions in binary form must reproduce the above copyright
9  *    notice, this list of conditions and the following disclaimer in the
10  *    documentation and/or other materials provided with the distribution.
11  *
12  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
13  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
14  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
15  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
16  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
18  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
19  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22  */
23 
24 #ifndef _FS_UTIL_H
25 #define _FS_UTIL_H
26 
27 #include <stdint.h>
28 
29 #define pwarn  printf
30 #define pfatal printf
31 #define perr   printf
32 
33 
34 /*
35  * le16dec(),le32dec(),le16enc(),le32enc()
36  * Link: https://github.com/freebsd/freebsd-src/
37  * Path:  freebsd-src/contrib/elftoolchain/libpe/_libpe.h
38  */
39 /* Encode/Decode macros */
40 #if defined(ELFTC_NEED_BYTEORDER_EXTENSIONS)
41 static  __inline uint16_t
le16dec(const void * pp)42 le16dec(const void *pp)
43 {
44 	unsigned char const *p = (unsigned char const *)pp;
45 
46 	return ((p[1] << 8) | p[0]);
47 }
48 
49 static __inline uint32_t
le32dec(const void * pp)50 le32dec(const void *pp)
51 {
52 	unsigned char const *p = (unsigned char const *)pp;
53 
54 	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
55 }
56 
57 static __inline void
le16enc(void * pp,uint16_t u)58 le16enc(void *pp, uint16_t u)
59 {
60 	unsigned char *p = (unsigned char *)pp;
61 
62 	p[0] = u & 0xff;
63 	p[1] = (u >> 8) & 0xff;
64 }
65 
66 static __inline void
le32enc(void * pp,uint32_t u)67 le32enc(void *pp, uint32_t u)
68 {
69 	unsigned char *p = (unsigned char *)pp;
70 
71 	p[0] = u & 0xff;
72 	p[1] = (u >> 8) & 0xff;
73 	p[2] = (u >> 16) & 0xff;
74 	p[3] = (u >> 24) & 0xff;
75 }
76 #endif	/* ELFTC_NEED_BYTEORDER_EXTENSIONS */
77 
78 /*
79  * roundup2
80  * Link: https://github.com/freebsd/freebsd-src/
81  * Path:  freebsd-src/tools/build/cross-build/include/common/sys/cdefs.h
82  */
83 #ifndef roundup2
84 #define roundup2(x, y) \
85 	(((x) + ((y)-1)) & (~((y)-1))) /* if y is powers of two */
86 #endif
87 #endif // _FS_UTIL_H