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