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
16 #include "zip_file_reader_io.h"
17
18 namespace panda {
19 namespace ecmascript {
ReadBuffer(size_t startPos,size_t bufferSize)20 std::string ZipFileReaderIo::ReadBuffer(size_t startPos, size_t bufferSize)
21 {
22 std::string result;
23 if (fd_ < 0 || startPos + bufferSize > fileLen_) {
24 return result;
25 }
26
27 result.resize(bufferSize);
28 if (!ReadBuffer(reinterpret_cast<uint8_t*>(result.data()), startPos, bufferSize)) {
29 result.clear();
30 }
31
32 return result;
33 }
34
ReadBuffer(uint8_t * dst,size_t startPos,size_t bufferSize)35 bool ZipFileReaderIo::ReadBuffer(uint8_t *dst, size_t startPos, size_t bufferSize)
36 {
37 if (dst == nullptr || fd_ < 0 || startPos + bufferSize > fileLen_) {
38 return false;
39 }
40
41 auto const readCount = pread(fd_, dst, bufferSize, startPos);
42 if (readCount < 0 || static_cast<size_t>(readCount) < bufferSize) {
43 return false;
44 }
45
46 return true;
47 }
48 }
49 }