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 FRAMEWORKS_INNERKITSIMPL_ACCESSOR_INCLUDE_PNG_EXIF_METADATA_ACCESSOR_H
17 #define FRAMEWORKS_INNERKITSIMPL_ACCESSOR_INCLUDE_PNG_EXIF_METADATA_ACCESSOR_H
18 
19 #include "abstract_exif_metadata_accessor.h"
20 #include "buffer_metadata_stream.h"
21 
22 namespace OHOS {
23 namespace Media {
24 static unsigned char pngSignature[] = {
25     0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
26 };
27 
28 class PngExifMetadataAccessor : public AbstractExifMetadataAccessor {
29 public:
30     PngExifMetadataAccessor(std::shared_ptr<MetadataStream> &stream);
31     ~PngExifMetadataAccessor();
32 
33     virtual uint32_t Read() override;
34     virtual uint32_t Write() override;
35     bool ReadBlob(DataBuf &blob) override;
36     uint32_t WriteBlob(DataBuf &blob) override;
37     uint32_t GetFilterArea(const std::vector<std::string> &exifKeys,
38                            std::vector<std::pair<uint32_t, uint32_t>> &ranges) override;
39 
40 private:
41     bool IsPngType() const;
42     bool FindTiffFromText(const DataBuf &data, const std::string chunkType, DataBuf &tiffData);
43     bool ProcessExifData(DataBuf &blob, std::string chunkType, uint32_t chunkLength);
44     bool GetExifEncodedBlob(uint8_t **dataBlob, uint32_t &size);
45     bool UpdateExifMetadata(BufferMetadataStream &bufStream, uint8_t *dataBlob, uint32_t size);
46     bool WriteData(BufferMetadataStream &bufStream, uint8_t *data, uint32_t size);
47     bool WriteExifData(BufferMetadataStream &bufStream, uint8_t *dataBlob, uint32_t size,
48                        DataBuf &chunkBuf, std::string chunkType);
49     ssize_t ReadChunk(DataBuf &buffer) const;
50     uint32_t UpdateData(uint8_t *dataBlob, uint32_t size);
51     bool isCompressed_ = false;
52 };
53 } // namespace Media
54 } // namespace OHOS
55 
56 #endif // FRAMEWORKS_INNERKITSIMPL_ACCESSOR_INCLUDE_PNG_EXIF_METADATA_ACCESSOR_H
57