1 /*
2 * Copyright (C) 2024 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
16 #include "file_manager_utils.h"
17
18 #include "securec.h"
19
20 #include "adaptor_log.h"
21 #include "adaptor_memory.h"
22
23 #define MAX_BUFFER_LEN 512000
24 #define DEFAULT_EXPANSION_RATIO 2
25
26 #ifdef IAM_TEST_ENABLE
27 #define IAM_STATIC
28 #else
29 #define IAM_STATIC static
30 #endif
31
GetRemainSpace(const Buffer *object)32 IAM_STATIC uint32_t GetRemainSpace(const Buffer *object)
33 {
34 return object->maxSize - object->contentSize;
35 }
36
GetStreamAddress(const Buffer *object)37 IAM_STATIC uint8_t *GetStreamAddress(const Buffer *object)
38 {
39 return object->buf + object->contentSize;
40 }
41
CapacityExpansion(Buffer *object, uint32_t targetCapacity)42 IAM_STATIC ResultCode CapacityExpansion(Buffer *object, uint32_t targetCapacity)
43 {
44 if (!IsBufferValid(object) || object->maxSize > MAX_BUFFER_LEN / DEFAULT_EXPANSION_RATIO) {
45 LOG_ERROR("invalid params");
46 return RESULT_BAD_PARAM;
47 }
48 uint32_t targetSize = object->maxSize;
49 while (targetSize < targetCapacity && targetSize <= MAX_BUFFER_LEN / DEFAULT_EXPANSION_RATIO) {
50 targetSize = targetSize * DEFAULT_EXPANSION_RATIO;
51 }
52 if (targetSize < targetCapacity) {
53 LOG_ERROR("target capacity can not reach");
54 return RESULT_BAD_PARAM;
55 }
56 uint8_t *buf = Malloc(targetSize);
57 if (buf == NULL) {
58 LOG_ERROR("malloc failed");
59 return RESULT_NO_MEMORY;
60 }
61 if (memcpy_s(buf, targetSize, object->buf, object->contentSize) != EOK) {
62 LOG_ERROR("copy failed");
63 Free(buf);
64 return RESULT_NO_MEMORY;
65 }
66 Free(object->buf);
67 object->buf = buf;
68 object->maxSize = targetSize;
69 return RESULT_SUCCESS;
70 }
71
StreamWrite(Buffer *parcel, void *from, uint32_t size)72 ResultCode StreamWrite(Buffer *parcel, void *from, uint32_t size)
73 {
74 if (!IsBufferValid(parcel) || from == NULL) {
75 LOG_ERROR("invalid params");
76 return RESULT_BAD_PARAM;
77 }
78 if (GetRemainSpace(parcel) < size) {
79 ResultCode result = CapacityExpansion(parcel, size);
80 if (result != RESULT_SUCCESS) {
81 LOG_ERROR("CapacityExpansion failed");
82 return result;
83 }
84 }
85 if (memcpy_s(GetStreamAddress(parcel), GetRemainSpace(parcel), from, size) != EOK) {
86 LOG_ERROR("copy failed");
87 return RESULT_NO_MEMORY;
88 }
89 parcel->contentSize += size;
90 return RESULT_SUCCESS;
91 }
92
StreamRead(Buffer *parcel, uint32_t *index, void *to, uint32_t size)93 ResultCode StreamRead(Buffer *parcel, uint32_t *index, void *to, uint32_t size)
94 {
95 if (!IsBufferValid(parcel) || index == NULL || to == NULL) {
96 LOG_ERROR("invalid params");
97 return RESULT_BAD_PARAM;
98 }
99 if (parcel->contentSize <= *index || parcel->contentSize - *index < size) {
100 LOG_ERROR("the buffer length is insufficient");
101 return RESULT_BAD_PARAM;
102 }
103 if (memcpy_s(to, size, parcel->buf + *index, size) != EOK) {
104 LOG_ERROR("copy failed");
105 return RESULT_NO_MEMORY;
106 }
107 *index += size;
108 return RESULT_SUCCESS;
109 }
110