• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 UTIL_JS_TEXTDECODER_H_
17 #define UTIL_JS_TEXTDECODER_H_
18 
19 #include <memory.h>
20 #include <string>
21 #include <vector>
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "unicode/ucnv.h"
25 
26 using TransformToolPointer = std::unique_ptr<UConverter, void(*)(UConverter*)>;
27 namespace OHOS::Util {
28     struct DecodeArr {
DecodeArrDecodeArr29         DecodeArr(UChar *tarPos, size_t tarStaPos, size_t limLen)
30         {
31             this->target = tarPos;
32             this->tarStartPos = tarStaPos;
33             this->limitLen = limLen;
34         }
35         UChar *target = 0;
36         size_t tarStartPos = 0;
37         size_t limitLen = 0;
38     };
39 
40     class TextDecoder {
41     public:
42         enum class ConverterFlags {
43             FLUSH_FLG = 0x1,
44             FATAL_FLG = 0x2,
45             IGNORE_BOM_FLG = 0x4,
46             UNICODE_FLG = 0x8,
47             BOM_SEEN_FLG = 0x10,
48         };
49 
50     public:
51         /**
52          * Constructor of textdecoder
53          *
54          * @param buff Encoding format.
55          * @param optionVec There are two attributes of code related option parameters: fatal and ignorebom.
56          */
57         TextDecoder(std::string buff, std::vector<int> optionVec);
58 
59         /**
60          * Destructor of textencoder.
61          */
~TextDecoder()62         virtual ~TextDecoder() {}
63 
64         /**
65          * Destructor of textencoder.
66          *
67          * @param env NAPI environment parameters.
68          * @param src An array that matches the format and needs to be decoded.
69          * @param iflag Decoding related option parameters.
70          */
71         napi_value Decode(napi_env env, napi_value src, bool iflag);
72 
73         /**
74          * Get encoding format.
75          *
76          * @param env NAPI environment parameters.
77          */
78         napi_value GetEncoding(napi_env env) const;
79 
80         /**
81          * Gets the setting of the exception thrown.
82          *
83          * @param env NAPI environment parameters.
84          */
85         napi_value GetFatal(napi_env env) const;
86 
87         /**
88          * Gets whether to ignore the setting of BOM flag.
89          *
90          * @param env NAPI environment parameters.
91          */
92         napi_value GetIgnoreBOM(napi_env env) const;
93 
94         /**
95          * Gets the size of minimum byte.
96          */
97         size_t GetMinByteSize() const;
98 
99         /**
100          * Reset function.
101          */
102         void Reset() const;
103 
104         /**
105          * Gets the pointer to the converter.
106          */
GetConverterPtr()107         UConverter *GetConverterPtr() const
108         {
109             return tranTool_.get();
110         }
111 
112         /**
113          * Determine whether it is the flag of BOM.
114          */
IsBomFlag()115         bool IsBomFlag() const
116         {
117             uint32_t temp = label_ & static_cast<uint32_t>(ConverterFlags::BOM_SEEN_FLG);
118             if (temp == static_cast<uint32_t>(ConverterFlags::BOM_SEEN_FLG)) {
119                 return true;
120             } else {
121                 return false;
122             }
123         }
124 
125         /**
126          * Determine whether it is Unicode.
127          */
IsUnicode()128         bool IsUnicode() const
129         {
130             uint32_t temp = label_ & static_cast<uint32_t>(ConverterFlags::UNICODE_FLG);
131             if (temp == static_cast<uint32_t>(ConverterFlags::UNICODE_FLG)) {
132                 return true;
133             } else {
134                 return false;
135             }
136         }
137 
138         /**
139          * Determine whether it is an ignored BOM.
140          */
IsIgnoreBom()141         bool IsIgnoreBom() const
142         {
143             uint32_t temp = label_ & static_cast<uint32_t>(ConverterFlags::IGNORE_BOM_FLG);
144             if (temp == static_cast<uint32_t>(ConverterFlags::IGNORE_BOM_FLG)) {
145                 return true;
146             } else {
147                 return false;
148             }
149         }
150 
151         /**
152          * Close the pointer of converter.
153          */
ConverterClose(UConverter * pointer)154         static void ConverterClose(UConverter *pointer)
155         {
156             ucnv_close(pointer);
157         }
158 
159     private:
160         void SetBomFlag(const UChar *arr, const UErrorCode codeFlag, const DecodeArr decArr,
161                         size_t& rstLen, bool& bomFlag);
162         void FreedMemory(UChar *pData);
163         uint32_t label_;
164         std::string encStr_;
165         TransformToolPointer tranTool_;
166     };
167 }
168 #endif // UTIL_JS_TEXTDECODER_H_
169