• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "raw_parser.h"
17 
18 #include <cstdio>
19 #include <fstream>
20 #include <gslogger.h>
21 #include <mutex>
22 #include <securec.h>
23 #include <sstream>
24 
25 #include <zlib.h>
26 
27 namespace OHOS {
28 namespace {
29 DEFINE_HILOG_LABEL("RawParser");
30 } // namespace
31 
Parse(const std::string & file)32 int32_t RawParser::Parse(const std::string &file)
33 {
34     int32_t ret = ReadFile(file, compressed);
35     if (ret) {
36         GSLOG2HI(ERROR) << "ReadFile failed with" << ret;
37         return ret;
38     }
39 
40     auto minfo = reinterpret_cast<struct RawHeaderInfo*>(&compressed[0]);
41     if (strncmp(minfo->magic, "RAW.dif2", 0x8) != 0) {
42         GSLOG2HI(ERROR) << "magic header mistake, is " << minfo->magic;
43         return -1;
44     }
45 
46     width = minfo->width;
47     height = minfo->height;
48     lastData = std::make_unique<uint8_t[]>(GetSize());
49 
50     struct RawFrameInfo *info = reinterpret_cast<struct RawFrameInfo *>(&compressed[magicHeaderLength]);
51     uint32_t ipos = reinterpret_cast<uint8_t *>(info) - reinterpret_cast<uint8_t *>(minfo);
52     while (ipos < clength) {
53         GSLOG2HI(DEBUG) << info->type << ", " << info->offset << ", " << info->length << ", " << info->clen;
54         if (info->clen < 0) {
55             GSLOG2HI(ERROR) << "clen < 0";
56             return -1;
57         }
58 
59         struct RawFrameInfoPtr zi = { info->type, info->offset, info->length, info->clen, info->mem };
60         infos.push_back(zi);
61 
62         // for BUS_ADRALN
63         constexpr uint32_t memalign = 4;
64         uint32_t align = info->clen - info->clen / memalign * memalign;
65         if (align) {
66             align = memalign - align;
67         }
68         info = reinterpret_cast<struct RawFrameInfo *>(info->mem + info->clen + align);
69         ipos = reinterpret_cast<uint8_t *>(info) - reinterpret_cast<uint8_t *>(minfo);
70     }
71 
72     if (infos.empty()) {
73         GSLOG2HI(ERROR) << "infos is empty";
74         return -1;
75     }
76 
77     return 0;
78 }
79 
GetNextData(uint32_t * addr)80 int32_t RawParser::GetNextData(uint32_t *addr)
81 {
82     const int32_t count = (lastID + 1) % static_cast<int32_t>(infos.size());
83     if (count < 0) {
84         GSLOG2HI(ERROR) << "current count < 0";
85         return -1;
86     }
87     auto type = infos[count].type;
88     if (type == RAW_HEADER_TYPE_NONE) {
89         lastID = count;
90         return GetNowData(addr);
91     }
92 
93     auto offset = infos[count].offset;
94     auto length = infos[count].length;
95     auto clen = infos[count].clen;
96     if (type == RAW_HEADER_TYPE_COMPRESSED) {
97         if (length == 0) {
98             GSLOG2HI(INFO) << "length == 0";
99             lastID = count;
100             return GetNowData(addr);
101         }
102 
103         uncompressed = std::make_unique<uint8_t[]>(length);
104         int32_t ret = Uncompress(uncompressed, length, infos[count].mem, clen);
105         if (ret) {
106             GSLOG2HI(ERROR) << "uncompress failed";
107             return -1;
108         }
109     } else if (type == RAW_HEADER_TYPE_RAW) {
110         uncompressed = std::make_unique<uint8_t[]>(length);
111         if (memcpy_s(uncompressed.get(), length, infos[count].mem, clen)) {
112             GSLOG2HI(ERROR) << "memcpy failed";
113             return -1;
114         }
115     }
116 
117     lastID = count;
118     if (length > 0 && memcpy_s(lastData.get() + offset, GetSize() - offset,
119                                uncompressed.get(), length) != EOK) {
120         GSLOG2HI(ERROR) << "memcpy failed";
121         return -1;
122     }
123 
124     return GetNowData(addr);
125 }
126 
GetNowData(uint32_t * addr)127 int32_t RawParser::GetNowData(uint32_t *addr)
128 {
129     if (memcpy_s(addr, GetSize(), lastData.get(), GetSize()) != EOK) {
130         GSLOG2HI(ERROR) << "memcpy failed";
131         return -1;
132     }
133     return 0;
134 }
135 
ReadFile(const std::string & file,std::unique_ptr<uint8_t[]> & ptr)136 int32_t RawParser::ReadFile(const std::string &file, std::unique_ptr<uint8_t[]> &ptr)
137 {
138     std::ifstream ifs(file, std::ifstream::in | std::ifstream::binary);
139     if (!ifs.good()) {
140         GSLOG2HI(ERROR) << "read file failed";
141         return 1;
142     }
143 
144     ifs.seekg(0, ifs.end);
145     clength = static_cast<uint32_t>(ifs.tellg());
146     ifs.seekg (0, ifs.beg);
147 
148     ptr = std::make_unique<uint8_t[]>(static_cast<unsigned int>(clength));
149     ifs.read(reinterpret_cast<char *>(ptr.get()), clength);
150     return 0;
151 }
152 
Uncompress(std::unique_ptr<uint8_t[]> & dst,uint32_t dstlen,uint8_t * cmem,uint32_t clen)153 int32_t RawParser::Uncompress(std::unique_ptr<uint8_t[]> &dst, uint32_t dstlen, uint8_t *cmem, uint32_t clen)
154 {
155     unsigned long ulength = dstlen;
156     auto ret = uncompress(dst.get(), &ulength, cmem, clen);
157     if (ret) {
158         GSLOG2HI(ERROR) << "uncompress failed";
159     }
160     return ret;
161 }
162 } // namespace OHOS
163