• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_mem.h"
17 
18 #include "securec.h"
19 
20 
21 namespace panda {
22 namespace ecmascript {
init()23 bool ZipFileReaderMem::init()
24 {
25     if (!ZipFileReader::init()) {
26         return false;
27     }
28     posix_fadvise(fd_, 0, 0, POSIX_FADV_SEQUENTIAL);
29 
30     fileContent_.resize(fileLen_);
31     auto pos = std::string::size_type{};
32     while (true) {
33         auto const readCount = read(fd_, fileContent_.data() + pos, fileContent_.size() - pos);
34         if (readCount < 0) {
35             fileContent_.clear();
36             return false;
37         } else if (readCount == 0) {
38             break;
39         }
40 
41         pos += readCount;
42     }
43     return true;
44 }
45 
ReadBuffer(size_t startPos,size_t bufferSize)46 std::string ZipFileReaderMem::ReadBuffer(size_t startPos, size_t bufferSize)
47 {
48     if (startPos + bufferSize > fileContent_.length()) {
49         return "";
50     }
51 
52     return fileContent_.substr(startPos, bufferSize);
53 }
54 
ReadBuffer(uint8_t * dst,size_t startPos,size_t bufferSize)55 bool ZipFileReaderMem::ReadBuffer(uint8_t *dst, size_t startPos, size_t bufferSize)
56 {
57     if (!dst || startPos + bufferSize > fileContent_.length()) {
58         return false;
59     }
60 
61     if (memcpy_s(dst, bufferSize, fileContent_.data() + startPos, bufferSize) != EOK) {
62         return false;
63     }
64 
65     return true;
66 }
67 }
68 }