• 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     uint32_t count = (lastID + 1) % infos.size();
83     auto type = infos[count].type;
84     if (type == RAW_HEADER_TYPE_NONE) {
85         lastID = static_cast<int32_t>(count);
86         return GetNowData(addr);
87     }
88 
89     auto offset = infos[count].offset;
90     auto length = infos[count].length;
91     auto clen = infos[count].clen;
92     if (type == RAW_HEADER_TYPE_COMPRESSED) {
93         if (length == 0) {
94             GSLOG2HI(INFO) << "length == 0";
95             lastID = static_cast<int32_t>(count);
96             return GetNowData(addr);
97         }
98 
99         uncompressed = std::make_unique<uint8_t[]>(length);
100         int32_t ret = Uncompress(uncompressed, length, infos[count].mem, clen);
101         if (ret) {
102             GSLOG2HI(ERROR) << "uncompress failed";
103             return -1;
104         }
105     } else if (type == RAW_HEADER_TYPE_RAW) {
106         uncompressed = std::make_unique<uint8_t[]>(length);
107         if (memcpy_s(uncompressed.get(), length, infos[count].mem, clen)) {
108             GSLOG2HI(ERROR) << "memcpy failed";
109             return -1;
110         }
111     }
112 
113     lastID =  static_cast<int32_t>(count);
114     if (length > 0 && memcpy_s(lastData.get() + offset, GetSize() - offset,
115                                uncompressed.get(), length) != EOK) {
116         GSLOG2HI(ERROR) << "memcpy failed";
117         return -1;
118     }
119 
120     return GetNowData(addr);
121 }
122 
GetNowData(uint32_t * addr)123 int32_t RawParser::GetNowData(uint32_t *addr)
124 {
125     if (memcpy_s(addr, GetSize(), lastData.get(), GetSize()) != EOK) {
126         GSLOG2HI(ERROR) << "memcpy failed";
127         return -1;
128     }
129     return 0;
130 }
131 
ReadFile(const std::string & file,std::unique_ptr<uint8_t[]> & ptr)132 int32_t RawParser::ReadFile(const std::string &file, std::unique_ptr<uint8_t[]> &ptr)
133 {
134     std::ifstream ifs(file, std::ifstream::in | std::ifstream::binary);
135     if (!ifs.good()) {
136         GSLOG2HI(ERROR) << "read file failed";
137         return 1;
138     }
139 
140     ifs.seekg(0, ifs.end);
141     clength = static_cast<uint32_t>(ifs.tellg());
142     ifs.seekg (0, ifs.beg);
143 
144     ptr = std::make_unique<uint8_t[]>(static_cast<unsigned int>(clength));
145     ifs.read(reinterpret_cast<char *>(ptr.get()), clength);
146     return 0;
147 }
148 
Uncompress(std::unique_ptr<uint8_t[]> & dst,uint32_t dstlen,uint8_t * cmem,uint32_t clen)149 int32_t RawParser::Uncompress(std::unique_ptr<uint8_t[]> &dst, uint32_t dstlen, uint8_t *cmem, uint32_t clen)
150 {
151     unsigned long ulength = dstlen;
152     auto ret = uncompress(dst.get(), &ulength, cmem, clen);
153     if (ret) {
154         GSLOG2HI(ERROR) << "uncompress failed";
155     }
156     return ret;
157 }
158 } // namespace OHOS
159