1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #ifndef HCS_BLOB_IF_H
10 #define HCS_BLOB_IF_H
11
12 #include "hdf_base.h"
13
14 #define CONFIG_NODE 0x1
15 #define CONFIG_ATTR 0x2
16 #define CONFIG_REFERENCE 0x3
17 #define CONFIG_ARRAY 0x4
18 #define CONFIG_BYTE 0x10
19 #define CONFIG_WORD 0x11
20 #define CONFIG_DWORD 0x12
21 #define CONFIG_QWORD 0x13
22 #define CONFIG_STRING 0x14
23
24 struct HbcHeader {
25 uint32_t magicNumber;
26 uint32_t versionMajor;
27 uint32_t versionMinor;
28 uint32_t checkSum;
29 int32_t totalSize;
30 };
31
32 #define HBC_MAGIC_NUMBER 0xA00AA00A
33 #define HBC_HEADER_LENGTH sizeof(struct HbcHeader)
34 #define HBC_BLOB_MAX_LENGTH (1024 * 1024 * 10) // The maximum length is 10 MB.
35 #define HBC_ROOT_NAME "root"
36
37 bool HcsIsByteAlign(void);
38 #define HCS_ALIGN_SIZE 4
HcsAlignSize(size_t size)39 static inline size_t HcsAlignSize(size_t size)
40 {
41 return (size + HCS_ALIGN_SIZE - 1) & (~(HCS_ALIGN_SIZE - 1));
42 }
43
HcsByteCodeToUint8(const char * start)44 static inline uint8_t HcsByteCodeToUint8(const char *start)
45 {
46 return *(uint8_t *)(start);
47 }
48
HcsByteCodeToUint16(const char * start)49 static inline uint16_t HcsByteCodeToUint16(const char *start)
50 {
51 return *(uint16_t *)(start);
52 }
53
HcsByteCodeToUint32(const char * start)54 static inline uint32_t HcsByteCodeToUint32(const char *start)
55 {
56 return *(uint32_t *)(start);
57 }
58
HcsByteCodeToUint64(const char * start)59 static inline uint64_t HcsByteCodeToUint64(const char *start)
60 {
61 return *(uint64_t *)(start);
62 }
63
HcsGetPrefix(const char * start)64 static inline uint32_t HcsGetPrefix(const char *start)
65 {
66 return HcsIsByteAlign() ? HcsByteCodeToUint32(start) : HcsByteCodeToUint8(start);
67 }
68
69 #define HCS_DWORD_LENGTH 4
70 #define HCS_QWORD_LENGTH 8
71 #define HCS_PREFIX_LENGTH (HcsIsByteAlign() ? HCS_DWORD_LENGTH : 1)
72 #define HCS_BYTE_LENGTH (HcsIsByteAlign() ? HCS_DWORD_LENGTH : 1)
73 #define HCS_WORD_LENGTH (HcsIsByteAlign() ? HCS_DWORD_LENGTH : 2)
74 #define HCS_STRING_LENGTH(str) (HcsIsByteAlign() ? HcsAlignSize(strlen(str) + 1) : (strlen(str) + 1))
75 int32_t HcsGetDataTypeOffset(const char *start);
76 int32_t HcsGetAttrLength(const char *start);
77 int32_t HcsGetNodeOrAttrLength(const char *start);
78 int32_t HcsGetNodeLength(const char *blob);
79 bool HcsCheckBlobFormat(const char *start, uint32_t length);
80 bool HcsSwapToUint8(uint8_t *value, const char *realValue, uint32_t type);
81 bool HcsSwapToUint16(uint16_t *value, const char *realValue, uint32_t type);
82 bool HcsSwapToUint32(uint32_t *value, const char *realValue, uint32_t type);
83 bool HcsSwapToUint64(uint64_t *value, const char *realValue, uint32_t type);
84
85 #endif /* HCS_BLOB_IF_H */