1 /*
2 * Copyright (c) 2023 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 #include "ecmascript/compiler/aot_file/binary_buffer_parser.h"
16 #include "ecmascript/compiler/compiler_log.h"
17 #include "securec.h"
18
19 namespace panda::ecmascript {
ParseBuffer(void * dst,uint32_t count)20 void BinaryBufferParser::ParseBuffer(void *dst, uint32_t count)
21 {
22 if (count > 0 && count + offset_ <= length_) {
23 if (memcpy_s(dst, count, buffer_ + offset_, count) != EOK) {
24 LOG_FULL(FATAL) << "memcpy_s failed";
25 }
26 offset_ = offset_ + count;
27 } else {
28 LOG_FULL(FATAL) << "parse buffer error, length is 0 or overflow";
29 }
30 }
31
ParseBuffer(void * dst,uint32_t count,uint32_t offset)32 void BinaryBufferParser::ParseBuffer(void *dst, uint32_t count, uint32_t offset)
33 {
34 offset_ = offset;
35 ParseBuffer(dst, count);
36 }
37
ParseBuffer(uint8_t * dst,uint32_t count,uint8_t * src)38 void BinaryBufferParser::ParseBuffer(uint8_t *dst, uint32_t count, uint8_t *src)
39 {
40 if (src >= buffer_ && src + count <= buffer_ + length_) {
41 if (memcpy_s(dst, count, src, count) != EOK) {
42 LOG_FULL(FATAL) << "memcpy_s failed";
43 }
44 } else {
45 LOG_FULL(FATAL) << "parse buffer error, length is 0 or overflow";
46 }
47 }
48 } // namespace panda::ecmascript
49