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