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 #define HDF_SIZE_MAX 0xffffffffu
14 
15 #define CONFIG_NODE 0x1
16 #define CONFIG_ATTR 0x2
17 #define CONFIG_REFERENCE 0x3
18 #define CONFIG_ARRAY 0x4
19 #define CONFIG_BYTE 0x10
20 #define CONFIG_WORD 0x11
21 #define CONFIG_DWORD 0x12
22 #define CONFIG_QWORD 0x13
23 #define CONFIG_STRING 0x14
24 
25 struct HbcHeader {
26     uint32_t magicNumber;
27     uint32_t versionMajor;
28     uint32_t versionMinor;
29     uint32_t checkSum;
30     int32_t totalSize;
31 };
32 
33 #define HBC_MAGIC_NUMBER 0xA00AA00A
34 #define HBC_HEADER_LENGTH sizeof(struct HbcHeader)
35 #define HBC_BLOB_MAX_LENGTH (1024 * 1024 * 10) // The maximum length is 10 MB.
36 #define HBC_ROOT_NAME "root"
37 
38 bool HcsIsByteAlign(void);
39 #define HCS_ALIGN_SIZE 4
HcsAlignSize(size_t size)40 static inline size_t HcsAlignSize(size_t size)
41 {
42     if (size + HCS_ALIGN_SIZE == 0 || size > (HDF_SIZE_MAX - HCS_ALIGN_SIZE)) {
43         return 0;
44     }
45     return (size + HCS_ALIGN_SIZE - 1) & (~(HCS_ALIGN_SIZE - 1));
46 }
47 
HcsByteCodeToUint8(const char * start)48 static inline uint8_t HcsByteCodeToUint8(const char *start)
49 {
50     return *(uint8_t *)(start);
51 }
52 
HcsByteCodeToUint16(const char * start)53 static inline uint16_t HcsByteCodeToUint16(const char *start)
54 {
55     return *(uint16_t *)(start);
56 }
57 
HcsByteCodeToUint32(const char * start)58 static inline uint32_t HcsByteCodeToUint32(const char *start)
59 {
60     return *(uint32_t *)(start);
61 }
62 
HcsByteCodeToUint64(const char * start)63 static inline uint64_t HcsByteCodeToUint64(const char *start)
64 {
65     return *(uint64_t *)(start);
66 }
67 
HcsGetPrefix(const char * start)68 static inline uint32_t HcsGetPrefix(const char *start)
69 {
70     return HcsIsByteAlign() ? HcsByteCodeToUint32(start) : HcsByteCodeToUint8(start);
71 }
72 
73 #define HCS_DWORD_LENGTH 4
74 #define HCS_QWORD_LENGTH 8
75 #define HCS_PREFIX_LENGTH (HcsIsByteAlign() ? HCS_DWORD_LENGTH : 1)
76 #define HCS_BYTE_LENGTH (HcsIsByteAlign() ? HCS_DWORD_LENGTH : 1)
77 #define HCS_WORD_LENGTH (HcsIsByteAlign() ? HCS_DWORD_LENGTH : 2)
78 #define HCS_STRING_LENGTH(str) (HcsIsByteAlign() ? HcsAlignSize(strlen(str) + 1) : (strlen(str) + 1))
79 int32_t HcsGetDataTypeOffset(const char *start);
80 int32_t HcsGetAttrLength(const char *start);
81 int32_t HcsGetNodeOrAttrLength(const char *start);
82 int32_t HcsGetNodeLength(const char *blob);
83 bool HcsCheckBlobFormat(const char *start, uint32_t length);
84 bool HcsSwapToUint8(uint8_t *value, const char *realValue, uint32_t type);
85 bool HcsSwapToUint16(uint16_t *value, const char *realValue, uint32_t type);
86 bool HcsSwapToUint32(uint32_t *value, const char *realValue, uint32_t type);
87 bool HcsSwapToUint64(uint64_t *value, const char *realValue, uint32_t type);
88 
89 #endif /* HCS_BLOB_IF_H */