• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef HAP_BYTEBUFFER_H
16 #define HAP_BYTEBUFFER_H
17 
18 #include <memory>
19 #include <string>
20 
21 #include "common/export_define.h"
22 
23 namespace OHOS {
24 namespace Security {
25 namespace Verify {
26 enum ReadFileErrorCode {
27     DEST_BUFFER_IS_NULL = -1,
28     FILE_IS_CLOSE = -2,
29     MMAP_COPY_FAILED = -3,
30     READ_OFFSET_OUT_OF_RANGE = -4,
31     MMAP_FAILED = -5,
32     MMAP_PARAM_INVALID = -6,
33 };
34 
35 class HapByteBuffer {
36 public:
37     DLL_EXPORT HapByteBuffer();
38     DLL_EXPORT explicit HapByteBuffer(int bufferCapacity);
39     DLL_EXPORT HapByteBuffer(const HapByteBuffer& other);
40     DLL_EXPORT ~HapByteBuffer();
41     DLL_EXPORT HapByteBuffer& operator=(const HapByteBuffer& other);
42     DLL_EXPORT bool GetInt64(long long& value);
43     DLL_EXPORT bool GetInt64(int index, long long& value);
44     DLL_EXPORT bool GetUInt32(int index, unsigned int& value);
45     DLL_EXPORT bool GetInt32(int& value);
46     DLL_EXPORT bool GetInt32(int index, int& value);
47     DLL_EXPORT bool GetUInt16(int index, unsigned short& value);
48     DLL_EXPORT void PutInt32(int offset, int value);
49     DLL_EXPORT void PutByte(int offset, char value);
50     DLL_EXPORT void PutData(int offset, const char data[], int len);
51     DLL_EXPORT int GetCapacity() const;
52     DLL_EXPORT int GetPosition() const;
53     DLL_EXPORT int GetLimit() const;
54     DLL_EXPORT const char* GetBufferPtr() const;
55     DLL_EXPORT void SetPosition(int pos);
56     DLL_EXPORT void SetLimit(int lim);
57     DLL_EXPORT void SetCapacity(int cap);
58     DLL_EXPORT void Slice();
59     DLL_EXPORT int Remaining() const;
60     DLL_EXPORT bool HasRemaining() const;
61     DLL_EXPORT bool CopyPartialBuffer(const HapByteBuffer& other, int len);
62     DLL_EXPORT void Clear();
63     DLL_EXPORT bool IsEqual(const HapByteBuffer& other);
64     DLL_EXPORT bool IsEqual(const std::string& other);
65 
66 private:
67     void Init(int bufferCapacity);
68     bool CheckInputForGettingData(int index, int dataLen);
69 
70 private:
71     static const int MAX_PRINT_LENGTH;
72     static const int HEX_PRINT_LENGTH;
73     std::unique_ptr<char[]> buffer;
74     int position;
75     int limit;
76     int capacity;
77 };
78 } // namespace Verify
79 } // namespace Security
80 } // namespace OHOS
81 #endif // HAP_BYTEBUFFER_H
82