1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 // Original code is licensed as follows:
7 /*
8 * Copyright 2007 ZXing authors
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 */
22
23 #include "fxbarcode/qrcode/BC_QRCoderMode.h"
24
25 #include <utility>
26
27 #include "fxbarcode/utils.h"
28
29 CBC_QRCoderMode* CBC_QRCoderMode::sBYTE = nullptr;
30 CBC_QRCoderMode* CBC_QRCoderMode::sNUMERIC = nullptr;
31 CBC_QRCoderMode* CBC_QRCoderMode::sALPHANUMERIC = nullptr;
32 CBC_QRCoderMode* CBC_QRCoderMode::sKANJI = nullptr;
33 CBC_QRCoderMode* CBC_QRCoderMode::sECI = nullptr;
34 CBC_QRCoderMode* CBC_QRCoderMode::sGBK = nullptr;
35 CBC_QRCoderMode* CBC_QRCoderMode::sTERMINATOR = nullptr;
36 CBC_QRCoderMode* CBC_QRCoderMode::sFNC1_FIRST_POSITION = nullptr;
37 CBC_QRCoderMode* CBC_QRCoderMode::sFNC1_SECOND_POSITION = nullptr;
38 CBC_QRCoderMode* CBC_QRCoderMode::sSTRUCTURED_APPEND = nullptr;
39
CBC_QRCoderMode(std::vector<int32_t> charCountBits,int32_t bits,ByteString name)40 CBC_QRCoderMode::CBC_QRCoderMode(std::vector<int32_t> charCountBits,
41 int32_t bits,
42 ByteString name)
43 : m_characterCountBitsForVersions(std::move(charCountBits)),
44 m_bits(bits),
45 m_name(name) {}
46
~CBC_QRCoderMode()47 CBC_QRCoderMode::~CBC_QRCoderMode() {}
48
Initialize()49 void CBC_QRCoderMode::Initialize() {
50 sBYTE = new CBC_QRCoderMode({8, 16, 16}, 0x4, "BYTE");
51 sALPHANUMERIC = new CBC_QRCoderMode({9, 11, 13}, 0x2, "ALPHANUMERIC");
52 sECI = new CBC_QRCoderMode(std::vector<int32_t>(), 0x7, "ECI");
53 sKANJI = new CBC_QRCoderMode({8, 10, 12}, 0x8, "KANJI");
54 sNUMERIC = new CBC_QRCoderMode({10, 12, 14}, 0x1, "NUMERIC");
55 sGBK = new CBC_QRCoderMode({8, 10, 12}, 0x0D, "GBK");
56 sTERMINATOR = new CBC_QRCoderMode(std::vector<int32_t>(), 0x00, "TERMINATOR");
57 sFNC1_FIRST_POSITION =
58 new CBC_QRCoderMode(std::vector<int32_t>(), 0x05, "FNC1_FIRST_POSITION");
59 sFNC1_SECOND_POSITION =
60 new CBC_QRCoderMode(std::vector<int32_t>(), 0x09, "FNC1_SECOND_POSITION");
61 sSTRUCTURED_APPEND =
62 new CBC_QRCoderMode(std::vector<int32_t>(), 0x03, "STRUCTURED_APPEND");
63 }
64
Finalize()65 void CBC_QRCoderMode::Finalize() {
66 delete sBYTE;
67 delete sALPHANUMERIC;
68 delete sECI;
69 delete sKANJI;
70 delete sNUMERIC;
71 delete sGBK;
72 delete sTERMINATOR;
73 delete sFNC1_FIRST_POSITION;
74 delete sFNC1_SECOND_POSITION;
75 delete sSTRUCTURED_APPEND;
76 }
77
ForBits(int32_t bits,int32_t & e)78 CBC_QRCoderMode* CBC_QRCoderMode::ForBits(int32_t bits, int32_t& e) {
79 switch (bits) {
80 case 0x0:
81 return sTERMINATOR;
82 case 0x1:
83 return sNUMERIC;
84 case 0x2:
85 return sALPHANUMERIC;
86 case 0x3:
87 return sSTRUCTURED_APPEND;
88 case 0x4:
89 return sBYTE;
90 case 0x5:
91 return sFNC1_FIRST_POSITION;
92 case 0x7:
93 return sECI;
94 case 0x8:
95 return sKANJI;
96 case 0x9:
97 return sFNC1_SECOND_POSITION;
98 case 0x0D:
99 return sGBK;
100 default:
101 e = BCExceptionUnsupportedMode;
102 return nullptr;
103 }
104 }
105
GetBits() const106 int32_t CBC_QRCoderMode::GetBits() const {
107 return m_bits;
108 }
109
GetName() const110 ByteString CBC_QRCoderMode::GetName() const {
111 return m_name;
112 }
113
GetCharacterCountBits(int32_t number,int32_t & e) const114 int32_t CBC_QRCoderMode::GetCharacterCountBits(int32_t number,
115 int32_t& e) const {
116 if (m_characterCountBitsForVersions.empty()) {
117 e = BCExceptionCharacterNotThisMode;
118 return 0;
119 }
120 int32_t offset;
121 if (number <= 9) {
122 offset = 0;
123 } else if (number <= 26) {
124 offset = 1;
125 } else {
126 offset = 2;
127 }
128 return m_characterCountBitsForVersions[offset];
129 }
130
Destroy()131 void CBC_QRCoderMode::Destroy() {
132 if (sBYTE) {
133 delete CBC_QRCoderMode::sBYTE;
134 sBYTE = nullptr;
135 }
136 if (sNUMERIC) {
137 delete CBC_QRCoderMode::sNUMERIC;
138 sNUMERIC = nullptr;
139 }
140 if (sALPHANUMERIC) {
141 delete CBC_QRCoderMode::sALPHANUMERIC;
142 sALPHANUMERIC = nullptr;
143 }
144 if (sKANJI) {
145 delete CBC_QRCoderMode::sKANJI;
146 sKANJI = nullptr;
147 }
148 if (sECI) {
149 delete CBC_QRCoderMode::sECI;
150 sECI = nullptr;
151 }
152 if (sGBK) {
153 delete CBC_QRCoderMode::sGBK;
154 sGBK = nullptr;
155 }
156 if (sTERMINATOR) {
157 delete CBC_QRCoderMode::sTERMINATOR;
158 sTERMINATOR = nullptr;
159 }
160 if (sFNC1_FIRST_POSITION) {
161 delete CBC_QRCoderMode::sFNC1_FIRST_POSITION;
162 sFNC1_FIRST_POSITION = nullptr;
163 }
164 if (sFNC1_SECOND_POSITION) {
165 delete CBC_QRCoderMode::sFNC1_SECOND_POSITION;
166 sFNC1_SECOND_POSITION = nullptr;
167 }
168 if (sSTRUCTURED_APPEND) {
169 delete CBC_QRCoderMode::sSTRUCTURED_APPEND;
170 sSTRUCTURED_APPEND = nullptr;
171 }
172 }
173