• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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_writer.h"
17 #include "zlib.h"
18 
19 namespace ark::panda_file {
20 
MemoryWriter()21 MemoryWriter::MemoryWriter() : checksum_(adler32(0, nullptr, 0)) {}
22 
WriteBytes(const std::vector<uint8_t> & bytes)23 bool MemoryWriter::WriteBytes(const std::vector<uint8_t> &bytes)
24 {
25     if (bytes.empty()) {
26         return true;
27     }
28     if (countChecksum_) {
29         checksum_ = adler32(checksum_, bytes.data(), bytes.size());
30     }
31     data_.insert(data_.end(), bytes.cbegin(), bytes.cend());
32     return true;
33 }
34 
MemoryBufferWriter(uint8_t * buffer,size_t size)35 MemoryBufferWriter::MemoryBufferWriter(uint8_t *buffer, size_t size)
36     : sp_(buffer, size), checksum_(adler32(0, nullptr, 0))
37 {
38 }
39 
WriteBytes(const std::vector<uint8_t> & bytes)40 bool MemoryBufferWriter::WriteBytes(const std::vector<uint8_t> &bytes)
41 {
42     if (bytes.empty()) {
43         return true;
44     }
45     if (countChecksum_) {
46         checksum_ = adler32(checksum_, bytes.data(), bytes.size());
47     }
48     auto subSp = sp_.SubSpan(offset_, bytes.size());
49     if (memcpy_s(subSp.data(), subSp.size(), bytes.data(), bytes.size()) != 0) {
50         return false;
51     }
52     offset_ += bytes.size();
53     return true;
54 }
55 
FileWriter(const std::string & fileName)56 FileWriter::FileWriter(const std::string &fileName) : checksum_(adler32(0, nullptr, 0))
57 {
58 #ifdef PANDA_TARGET_WINDOWS
59     constexpr char const *MODE = "wb";
60 #else
61     constexpr char const *MODE = "wbe";
62 #endif
63 
64     file_ = fopen(fileName.c_str(), MODE);
65 }
66 
~FileWriter()67 FileWriter::~FileWriter()
68 {
69     if (file_ != nullptr) {
70         fclose(file_);
71     }
72     file_ = nullptr;
73 }
74 
WriteByte(uint8_t data)75 bool FileWriter::WriteByte(uint8_t data)
76 {
77     return WriteBytes({data});
78 }
79 
WriteBytes(const std::vector<uint8_t> & bytes)80 bool FileWriter::WriteBytes(const std::vector<uint8_t> &bytes)
81 {
82     if (file_ == nullptr) {
83         return false;
84     }
85 
86     if (bytes.empty()) {
87         return true;
88     }
89 
90     if (countChecksum_) {
91         checksum_ = adler32(checksum_, bytes.data(), bytes.size());
92     }
93 
94     if (fwrite(bytes.data(), sizeof(decltype(bytes.back())), bytes.size(), file_) != bytes.size()) {
95         return false;
96     }
97 
98     offset_ += bytes.size();
99     return true;
100 }
101 
102 }  // namespace ark::panda_file
103