1 /* 2 * Copyright (C) 2024 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 #ifndef PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_PARSER_HEIF_STREAM_H 17 #define PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_PARSER_HEIF_STREAM_H 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 #include "heif_error.h" 23 24 namespace OHOS { 25 namespace ImagePlugin { 26 class HeifInputStream { 27 public: 28 virtual ~HeifInputStream() = default; 29 30 [[nodiscard]] virtual int64_t Tell() const = 0; 31 32 virtual bool CheckSize(size_t size, int64_t end) = 0; 33 34 virtual bool Read(void *data, size_t size) = 0; 35 36 virtual bool Seek(int64_t position) = 0; 37 }; 38 39 class HeifBufferInputStream : public HeifInputStream { 40 public: 41 HeifBufferInputStream(const uint8_t *data, size_t size, bool needCopy); 42 43 ~HeifBufferInputStream() override; 44 45 [[nodiscard]] int64_t Tell() const override; 46 47 bool CheckSize(size_t target_size, int64_t end) override; 48 49 bool Read(void *data, size_t size) override; 50 51 bool Seek(int64_t position) override; 52 53 private: 54 const uint8_t *data_; 55 size_t length_; 56 int64_t pos_; 57 bool copied_; 58 }; 59 60 class HeifStreamReader { 61 public: 62 HeifStreamReader(std::shared_ptr<HeifInputStream> stream, 63 int64_t start, 64 size_t length); 65 66 uint8_t Read8(); 67 68 uint16_t Read16(); 69 70 uint32_t Read32(); 71 uint64_t Read64(); 72 73 bool ReadData(uint8_t *data, size_t size); 74 75 std::string ReadString(); 76 77 [[nodiscard]] bool CheckSize(size_t size); 78 SkipEnd()79 void SkipEnd() 80 { 81 inputStream_->Seek(end_); 82 } 83 IsAtEnd()84 [[nodiscard]] bool IsAtEnd() 85 { 86 return GetStream()->Tell() >= end_; 87 } 88 HasError()89 [[nodiscard]] bool HasError() const 90 { 91 return hasError_; 92 } 93 SetError(bool hasError)94 void SetError(bool hasError) 95 { 96 hasError_ = hasError; 97 } 98 GetError()99 [[nodiscard]] heif_error GetError() const 100 { 101 return hasError_ ? heif_error_eof : heif_error_ok; 102 } 103 GetStream()104 std::shared_ptr<HeifInputStream> GetStream() { return inputStream_; } 105 GetRemainSize()106 [[nodiscard]] size_t GetRemainSize() const { return end_ - inputStream_->Tell(); } 107 108 private: 109 std::shared_ptr<HeifInputStream> inputStream_; 110 int64_t start_; 111 int64_t end_; 112 113 bool hasError_ = false; 114 }; 115 116 class HeifStreamWriter { 117 public: 118 void CheckSize(size_t size); 119 120 void Write8(uint8_t); 121 122 void Write16(uint16_t); 123 124 void Write32(uint32_t); 125 126 void Write64(uint64_t); 127 128 void Write(int size, uint64_t value); 129 130 void Write(const std::string &); 131 132 void Write(const std::vector<uint8_t> &data); 133 134 void Skip(size_t skipSize); 135 136 void Insert(size_t insertSize); 137 GetDataSize()138 size_t GetDataSize() const { return data_.size(); } 139 GetPos()140 size_t GetPos() const { return position_; } 141 SetPos(size_t pos)142 void SetPos(size_t pos) { position_ = pos; } 143 SetPositionToEnd()144 void SetPositionToEnd() { position_ = data_.size(); } 145 GetData()146 const std::vector<uint8_t> &GetData() const { return data_; } 147 148 private: 149 std::vector<uint8_t> data_; 150 size_t position_ = 0; 151 }; 152 } // namespace ImagePlugin 153 } // namespace OHOS 154 155 #endif // PLUGINS_COMMON_LIBS_IMAGE_LIBEXTPLUGIN_INCLUDE_HEIF_PARSER_HEIF_STREAM_H 156