OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 // Original code is licensed as follows: | 6 // Original code is licensed as follows: |
7 /* | 7 /* |
8 * Copyright 2006-2007 Jeremias Maerki. | 8 * Copyright 2006-2007 Jeremias Maerki. |
9 * | 9 * |
10 * Licensed under the Apache License, Version 2.0 (the "License"); | 10 * Licensed under the Apache License, Version 2.0 (the "License"); |
(...skipping 17 matching lines...) Expand all Loading... |
28 #include "BC_SymbolInfo.h" | 28 #include "BC_SymbolInfo.h" |
29 #include "BC_EncoderContext.h" | 29 #include "BC_EncoderContext.h" |
30 #include "BC_HighLevelEncoder.h" | 30 #include "BC_HighLevelEncoder.h" |
31 #include "BC_C40Encoder.h" | 31 #include "BC_C40Encoder.h" |
32 CBC_C40Encoder::CBC_C40Encoder() | 32 CBC_C40Encoder::CBC_C40Encoder() |
33 { | 33 { |
34 } | 34 } |
35 CBC_C40Encoder::~CBC_C40Encoder() | 35 CBC_C40Encoder::~CBC_C40Encoder() |
36 { | 36 { |
37 } | 37 } |
38 FX_INT32 CBC_C40Encoder::getEncodingMode() | 38 int32_t CBC_C40Encoder::getEncodingMode() |
39 { | 39 { |
40 return C40_ENCODATION; | 40 return C40_ENCODATION; |
41 } | 41 } |
42 void CBC_C40Encoder::Encode(CBC_EncoderContext &context, FX_INT32 &e) | 42 void CBC_C40Encoder::Encode(CBC_EncoderContext &context, int32_t &e) |
43 { | 43 { |
44 CFX_WideString buffer; | 44 CFX_WideString buffer; |
45 while (context.hasMoreCharacters()) { | 45 while (context.hasMoreCharacters()) { |
46 FX_WCHAR c = context.getCurrentChar(); | 46 FX_WCHAR c = context.getCurrentChar(); |
47 context.m_pos++; | 47 context.m_pos++; |
48 FX_INT32 lastCharSize = encodeChar(c, buffer, e); | 48 int32_t lastCharSize = encodeChar(c, buffer, e); |
49 if (e != BCExceptionNO) { | 49 if (e != BCExceptionNO) { |
50 return; | 50 return; |
51 } | 51 } |
52 FX_INT32 unwritten = (buffer.GetLength() / 3) * 2; | 52 int32_t unwritten = (buffer.GetLength() / 3) * 2; |
53 FX_INT32 curCodewordCount = context.getCodewordCount() + unwritten; | 53 int32_t curCodewordCount = context.getCodewordCount() + unwritten; |
54 context.updateSymbolInfo(curCodewordCount, e); | 54 context.updateSymbolInfo(curCodewordCount, e); |
55 if (e != BCExceptionNO) { | 55 if (e != BCExceptionNO) { |
56 return; | 56 return; |
57 } | 57 } |
58 FX_INT32 available = context.m_symbolInfo->m_dataCapacity - curCodewordC
ount; | 58 int32_t available = context.m_symbolInfo->m_dataCapacity - curCodewordCo
unt; |
59 if (!context.hasMoreCharacters()) { | 59 if (!context.hasMoreCharacters()) { |
60 CFX_WideString removed; | 60 CFX_WideString removed; |
61 if ((buffer.GetLength() % 3) == 2) { | 61 if ((buffer.GetLength() % 3) == 2) { |
62 if (available < 2 || available > 2) { | 62 if (available < 2 || available > 2) { |
63 lastCharSize = backtrackOneCharacter(context, buffer, remove
d, lastCharSize, e); | 63 lastCharSize = backtrackOneCharacter(context, buffer, remove
d, lastCharSize, e); |
64 if (e != BCExceptionNO) { | 64 if (e != BCExceptionNO) { |
65 return; | 65 return; |
66 } | 66 } |
67 } | 67 } |
68 } | 68 } |
69 while ((buffer.GetLength() % 3) == 1 | 69 while ((buffer.GetLength() % 3) == 1 |
70 && ((lastCharSize <= 3 && available != 1) || lastCharSize >
3)) { | 70 && ((lastCharSize <= 3 && available != 1) || lastCharSize >
3)) { |
71 lastCharSize = backtrackOneCharacter(context, buffer, removed, l
astCharSize, e); | 71 lastCharSize = backtrackOneCharacter(context, buffer, removed, l
astCharSize, e); |
72 if (e != BCExceptionNO) { | 72 if (e != BCExceptionNO) { |
73 return; | 73 return; |
74 } | 74 } |
75 } | 75 } |
76 break; | 76 break; |
77 } | 77 } |
78 FX_INT32 count = buffer.GetLength(); | 78 int32_t count = buffer.GetLength(); |
79 if ((count % 3) == 0) { | 79 if ((count % 3) == 0) { |
80 FX_INT32 newMode = CBC_HighLevelEncoder::lookAheadTest(context.m_msg
, context.m_pos, getEncodingMode()); | 80 int32_t newMode = CBC_HighLevelEncoder::lookAheadTest(context.m_msg,
context.m_pos, getEncodingMode()); |
81 if (newMode != getEncodingMode()) { | 81 if (newMode != getEncodingMode()) { |
82 context.signalEncoderChange(newMode); | 82 context.signalEncoderChange(newMode); |
83 break; | 83 break; |
84 } | 84 } |
85 } | 85 } |
86 } | 86 } |
87 handleEOD(context, buffer, e); | 87 handleEOD(context, buffer, e); |
88 } | 88 } |
89 void CBC_C40Encoder::writeNextTriplet(CBC_EncoderContext &context, CFX_WideStrin
g &buffer) | 89 void CBC_C40Encoder::writeNextTriplet(CBC_EncoderContext &context, CFX_WideStrin
g &buffer) |
90 { | 90 { |
91 context.writeCodewords(encodeToCodewords(buffer, 0)); | 91 context.writeCodewords(encodeToCodewords(buffer, 0)); |
92 buffer.Delete(0, 3); | 92 buffer.Delete(0, 3); |
93 } | 93 } |
94 void CBC_C40Encoder::handleEOD(CBC_EncoderContext &context, CFX_WideString &buff
er, FX_INT32 &e) | 94 void CBC_C40Encoder::handleEOD(CBC_EncoderContext &context, CFX_WideString &buff
er, int32_t &e) |
95 { | 95 { |
96 FX_INT32 unwritten = (buffer.GetLength() / 3) * 2; | 96 int32_t unwritten = (buffer.GetLength() / 3) * 2; |
97 FX_INT32 rest = buffer.GetLength() % 3; | 97 int32_t rest = buffer.GetLength() % 3; |
98 FX_INT32 curCodewordCount = context.getCodewordCount() + unwritten; | 98 int32_t curCodewordCount = context.getCodewordCount() + unwritten; |
99 context.updateSymbolInfo(curCodewordCount, e); | 99 context.updateSymbolInfo(curCodewordCount, e); |
100 if (e != BCExceptionNO) { | 100 if (e != BCExceptionNO) { |
101 return; | 101 return; |
102 } | 102 } |
103 FX_INT32 available = context.m_symbolInfo->m_dataCapacity - curCodewordCount
; | 103 int32_t available = context.m_symbolInfo->m_dataCapacity - curCodewordCount; |
104 if (rest == 2) { | 104 if (rest == 2) { |
105 buffer += (FX_WCHAR)'\0'; | 105 buffer += (FX_WCHAR)'\0'; |
106 while (buffer.GetLength() >= 3) { | 106 while (buffer.GetLength() >= 3) { |
107 writeNextTriplet(context, buffer); | 107 writeNextTriplet(context, buffer); |
108 } | 108 } |
109 if (context.hasMoreCharacters()) { | 109 if (context.hasMoreCharacters()) { |
110 context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); | 110 context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); |
111 } | 111 } |
112 } else if (available == 1 && rest == 1) { | 112 } else if (available == 1 && rest == 1) { |
113 while (buffer.GetLength() >= 3) { | 113 while (buffer.GetLength() >= 3) { |
114 writeNextTriplet(context, buffer); | 114 writeNextTriplet(context, buffer); |
115 } | 115 } |
116 if (context.hasMoreCharacters()) { | 116 if (context.hasMoreCharacters()) { |
117 context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); | 117 context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); |
118 } | 118 } |
119 context.m_pos--; | 119 context.m_pos--; |
120 } else if (rest == 0) { | 120 } else if (rest == 0) { |
121 while (buffer.GetLength() >= 3) { | 121 while (buffer.GetLength() >= 3) { |
122 writeNextTriplet(context, buffer); | 122 writeNextTriplet(context, buffer); |
123 } | 123 } |
124 if (available > 0 || context.hasMoreCharacters()) { | 124 if (available > 0 || context.hasMoreCharacters()) { |
125 context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); | 125 context.writeCodeword(CBC_HighLevelEncoder::C40_UNLATCH); |
126 } | 126 } |
127 } else { | 127 } else { |
128 e = BCExceptionIllegalStateUnexpectedCase; | 128 e = BCExceptionIllegalStateUnexpectedCase; |
129 return; | 129 return; |
130 } | 130 } |
131 context.signalEncoderChange(ASCII_ENCODATION); | 131 context.signalEncoderChange(ASCII_ENCODATION); |
132 } | 132 } |
133 FX_INT32 CBC_C40Encoder::encodeChar(FX_WCHAR c, CFX_WideString &sb, FX_INT32 &e) | 133 int32_t CBC_C40Encoder::encodeChar(FX_WCHAR c, CFX_WideString &sb, int32_t &e) |
134 { | 134 { |
135 if (c == ' ') { | 135 if (c == ' ') { |
136 sb += (FX_WCHAR)'\3'; | 136 sb += (FX_WCHAR)'\3'; |
137 return 1; | 137 return 1; |
138 } else if ((c >= '0') && (c <= '9')) { | 138 } else if ((c >= '0') && (c <= '9')) { |
139 sb += (FX_WCHAR)(c - 48 + 4); | 139 sb += (FX_WCHAR)(c - 48 + 4); |
140 return 1; | 140 return 1; |
141 } else if ((c >= 'A') && (c <= 'Z')) { | 141 } else if ((c >= 'A') && (c <= 'Z')) { |
142 sb += (FX_WCHAR)(c - 65 + 14); | 142 sb += (FX_WCHAR)(c - 65 + 14); |
143 return 1; | 143 return 1; |
(...skipping 13 matching lines...) Expand all Loading... |
157 sb += (FX_WCHAR)'\1'; | 157 sb += (FX_WCHAR)'\1'; |
158 sb += (FX_WCHAR)(c - 91 + 22); | 158 sb += (FX_WCHAR)(c - 91 + 22); |
159 return 2; | 159 return 2; |
160 } else if ((c >= 60) && (c <= 0x7f)) { | 160 } else if ((c >= 60) && (c <= 0x7f)) { |
161 sb += (FX_WCHAR)'\2'; | 161 sb += (FX_WCHAR)'\2'; |
162 sb += (FX_WCHAR)(c - 96); | 162 sb += (FX_WCHAR)(c - 96); |
163 return 2; | 163 return 2; |
164 } else if (c >= 80) { | 164 } else if (c >= 80) { |
165 sb += (FX_WCHAR)'\1'; | 165 sb += (FX_WCHAR)'\1'; |
166 sb += (FX_WCHAR)0x001e; | 166 sb += (FX_WCHAR)0x001e; |
167 FX_INT32 len = 2; | 167 int32_t len = 2; |
168 len += encodeChar((c - 128), sb, e); | 168 len += encodeChar((c - 128), sb, e); |
169 BC_EXCEPTION_CHECK_ReturnValue(e, 0); | 169 BC_EXCEPTION_CHECK_ReturnValue(e, 0); |
170 return len; | 170 return len; |
171 } else { | 171 } else { |
172 e = BCExceptionIllegalArgument; | 172 e = BCExceptionIllegalArgument; |
173 return 0; | 173 return 0; |
174 } | 174 } |
175 } | 175 } |
176 FX_INT32 CBC_C40Encoder::backtrackOneCharacter(CBC_EncoderContext &context, CFX_
WideString &buffer, CFX_WideString &removed, FX_INT32 lastCharSize, FX_INT32 &e) | 176 int32_t CBC_C40Encoder::backtrackOneCharacter(CBC_EncoderContext &context, CFX_W
ideString &buffer, CFX_WideString &removed, int32_t lastCharSize, int32_t &e) |
177 { | 177 { |
178 FX_INT32 count = buffer.GetLength(); | 178 int32_t count = buffer.GetLength(); |
179 buffer.Delete(count - lastCharSize, count); | 179 buffer.Delete(count - lastCharSize, count); |
180 context.m_pos--; | 180 context.m_pos--; |
181 FX_WCHAR c = context.getCurrentChar(); | 181 FX_WCHAR c = context.getCurrentChar(); |
182 lastCharSize = encodeChar(c, removed, e); | 182 lastCharSize = encodeChar(c, removed, e); |
183 BC_EXCEPTION_CHECK_ReturnValue(e, -1); | 183 BC_EXCEPTION_CHECK_ReturnValue(e, -1); |
184 context.resetSymbolInfo(); | 184 context.resetSymbolInfo(); |
185 return lastCharSize; | 185 return lastCharSize; |
186 } | 186 } |
187 CFX_WideString CBC_C40Encoder::encodeToCodewords(CFX_WideString sb, FX_INT32 sta
rtPos) | 187 CFX_WideString CBC_C40Encoder::encodeToCodewords(CFX_WideString sb, int32_t star
tPos) |
188 { | 188 { |
189 FX_WCHAR c1 = sb.GetAt(startPos); | 189 FX_WCHAR c1 = sb.GetAt(startPos); |
190 FX_WCHAR c2 = sb.GetAt(startPos + 1); | 190 FX_WCHAR c2 = sb.GetAt(startPos + 1); |
191 FX_WCHAR c3 = sb.GetAt(startPos + 2); | 191 FX_WCHAR c3 = sb.GetAt(startPos + 2); |
192 FX_INT32 v = (1600 * c1) + (40 * c2) + c3 + 1; | 192 int32_t v = (1600 * c1) + (40 * c2) + c3 + 1; |
193 FX_WCHAR cw1 = (FX_WCHAR) (v / 256); | 193 FX_WCHAR cw1 = (FX_WCHAR) (v / 256); |
194 FX_WCHAR cw2 = (FX_WCHAR) (v % 256); | 194 FX_WCHAR cw2 = (FX_WCHAR) (v % 256); |
195 CFX_WideString b1(cw1); | 195 CFX_WideString b1(cw1); |
196 CFX_WideString b2(cw2); | 196 CFX_WideString b2(cw2); |
197 return b1 + b2; | 197 return b1 + b2; |
198 } | 198 } |
OLD | NEW |