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