1 /*
2  * Copyright (C) 2021 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 
17 #include "wbmp_format_agent.h"
18 
19 #include "image_log.h"
20 #include "image_plugin_type.h"
21 #include "plugin_service.h"
22 #include "string"
23 
24 #undef LOG_DOMAIN
25 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_PLUGIN
26 
27 #undef LOG_TAG
28 #define LOG_TAG "WbmpFormatAgent"
29 
30 namespace OHOS {
31 namespace ImagePlugin {
32 using namespace ImagePlugin;
33 using namespace MultimediaPlugin;
34 
35 const std::string FORMAT_TYPE = "image/vnd.wap.wbmp";
36 constexpr uint32_t HEADER_SIZE = 32;
37 constexpr uint8_t SHIF_BIT_MASK = 7;
38 constexpr uint8_t LOW_BIT_MASK = 0x7F;
39 constexpr uint8_t HIGH_BIT_MASK = 0x80;
40 
read_byte(uint8_t * stream,uint8_t & value,uint32_t & offset,uint32_t dataSize)41 bool WbmpFormatAgent::read_byte(uint8_t *stream, uint8_t &value, uint32_t &offset, uint32_t dataSize)
42 {
43     if (stream == nullptr) {
44         IMAGE_LOGE("read_byte: stream is nullptr");
45         return false;
46     }
47     if (offset >= dataSize) {
48         IMAGE_LOGE("read_header data offset %{public}u. dataSize %{public}u", offset, dataSize);
49         return false;
50     }
51     value = *(stream + offset);
52     offset++;
53     return true;
54 }
55 
read_mbf(uint8_t * stream,uint64_t & value,uint32_t & offset,uint32_t dataSize)56 bool WbmpFormatAgent::read_mbf(uint8_t *stream, uint64_t &value, uint32_t &offset, uint32_t dataSize)
57 {
58     uint64_t n = 0;
59     uint8_t data;
60     const uint64_t kLimit = 0xFE00000000000000;
61     do {
62         if (n & kLimit) { // Will overflow on shift by 7.
63             return false;
64         }
65         if (!read_byte(stream, data, offset, dataSize)) {
66             return false;
67         }
68         n = (n << SHIF_BIT_MASK) | (data & LOW_BIT_MASK);
69     } while (data & HIGH_BIT_MASK);
70     value = n;
71     return true;
72 }
73 
read_header(const void * stream,uint32_t dataSize)74 bool WbmpFormatAgent::read_header(const void *stream, uint32_t dataSize)
75 {
76     uint8_t data;
77     uint8_t *pData = static_cast<uint8_t *>(const_cast<void *>(stream));
78     uint32_t offset = 0;
79 
80     if (!read_byte(pData, data, offset, dataSize) || data != 0) { // unknown type
81         return false;
82     }
83     IMAGE_LOGD("read_header data %{public}d.", data);
84 
85     if (!read_byte(pData, data, offset, dataSize) || (data & 0x9F)) { // skip fixed header
86         return false;
87     }
88     IMAGE_LOGD("read_header data %{public}d.", data);
89 
90     uint64_t width;
91     uint64_t height;
92     if (!read_mbf(pData, width, offset, dataSize) || width > 0xFFFF || !width) {
93         return false;
94     }
95     IMAGE_LOGD("read_header width %{public}lld.", static_cast<long long>(width));
96 
97     if (!read_mbf(pData, height, offset, dataSize) || height > 0xFFFF || !height) {
98         return false;
99     }
100     IMAGE_LOGD("read_header height %{public}lld.", static_cast<long long>(height));
101 
102     return true;
103 }
104 
105 
GetFormatType()106 std::string WbmpFormatAgent::GetFormatType()
107 {
108     return FORMAT_TYPE;
109 }
110 
GetHeaderSize()111 uint32_t WbmpFormatAgent::GetHeaderSize()
112 {
113     return HEADER_SIZE;
114 }
115 
CheckFormat(const void * headerData,uint32_t dataSize)116 bool WbmpFormatAgent::CheckFormat(const void *headerData, uint32_t dataSize)
117 {
118     if (headerData == nullptr) {
119         IMAGE_LOGE("check format failed: header data is null.");
120         return false;
121     }
122 
123     if (!read_header(headerData, dataSize)) {
124         IMAGE_LOGD("not wbmp image format.");
125         return false;
126     }
127 
128     IMAGE_LOGD("wbmp image format ok.");
129     return true;
130 }
131 } // namespace ImagePlugin
132 } // namespace OHOS
133