Debian Codegraph @

src/lib/libcdr_utils.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3 * This file is part of the libcdr project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include "libcdr_utils.h"
11
12 #include <cassert>
13 #include <cstdarg>
14 #include <cstdio>
15 #include <string.h>
16
17 #include <unicode/ucsdet.h>
18 #include <unicode/ucnv.h>
19 #include <unicode/utypes.h>
20 #include <unicode/utf8.h>
21
22 #define CDR_NUM_ELEMENTS(array) sizeof(array)/sizeof(array[0])
23
24 #define SURROGATE_VALUE(h,l) (((h) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000)
25
26 namespace
27 {
28
29 static unsigned short getEncodingFromICUName(const char *name)
30 {
31 // ANSI
32 if (strcmp(name, "ISO-8859-1") == 0)
33 return 0;
34 if (strcmp(name, "windows-1252") == 0)
35 return 0;
36 // CENTRAL EUROPE
37 if (strcmp(name, "ISO-8859-2") == 0)
38 return 0xee;
39 if (strcmp(name, "windows-1250") == 0)
40 return 0xee;
41 // RUSSIAN
42 if (strcmp(name, "ISO-8859-5") == 0)
43 return 0xcc;
44 if (strcmp(name, "windows-1251") == 0)
45 return 0xcc;
46 if (strcmp(name, "KOI8-R") == 0)
47 return 0xcc;
48 // ARABIC
49 if (strcmp(name, "ISO-8859-6") == 0)
50 return 0xb2;
51 if (strcmp(name, "windows-1256") == 0)
52 return 0xb2;
53 // TURKISH
54 if (strcmp(name, "ISO-8859-9") == 0)
55 return 0xa2;
56 if (strcmp(name, "windows-1254") == 0)
57 return 0xa2;
58 // GREEK
59 if (strcmp(name, "ISO-8859-7") == 0)
60 return 0xa1;
61 if (strcmp(name, "windows-1253") == 0)
62 return 0xa1;
63 // HEBREW
64 if (strcmp(name, "ISO-8859-8") == 0)
65 return 0xb1;
66 if (strcmp(name, "windows-1255") == 0)
67 return 0xb1;
68 // JAPANESE
69 if (strcmp(name, "Shift_JIS") == 0)
70 return 0x80;
71 if (strcmp(name, "ISO-2022-JP") == 0)
72 return 0x80;
73 if (strcmp(name, "EUC-JP") == 0)
74 return 0x80;
75 if (strcmp(name, "windows-932") == 0)
76 return 0x80;
77 // KOREAN
78 if (strcmp(name, "ISO-2022-KR") == 0)
79 return 0x81;
80 if (strcmp(name, "EUC-KR") == 0)
81 return 0x81;
82 if (strcmp(name, "windows-949") == 0)
83 return 0x81;
84 // CHINESE SIMPLIFIED
85 if (strcmp(name, "ISO-2022-CN") == 0)
86 return 0x86;
87 if (strcmp(name, "GB18030") == 0)
88 return 0x86;
89 if (strcmp(name, "windows-936") == 0)
90 return 0x86;
91 // CHINESE TRADITIONAL
92 if (strcmp(name, "Big5") == 0)
93 return 0x88;
94 if (strcmp(name, "windows-950") == 0)
95 return 0x88;
96
97 return 0;
98 }
99
100 static unsigned short getEncoding(const unsigned char *buffer, unsigned long bufferLength)
101 {
102 if (!buffer)
103 return 0;
104 UErrorCode status = U_ZERO_ERROR;
105 UCharsetDetector *csd = nullptr;
106 try
107 {
108 csd = ucsdet_open(&status);
109 if (U_FAILURE(status) || !csd)
110 return 0;
111 ucsdet_enableInputFilter(csd, true);
112 ucsdet_setText(csd, (const char *)buffer, (unsigned)bufferLength, &status);
113 if (U_FAILURE(status))
114 throw libcdr::EncodingException();
115 const UCharsetMatch *csm = ucsdet_detect(csd, &status);
116 if (U_FAILURE(status) || !csm)
117 throw libcdr::EncodingException();
118 const char *name = ucsdet_getName(csm, &status);
119 if (U_FAILURE(status) || !name)
120 throw libcdr::EncodingException();
121 int32_t confidence = ucsdet_getConfidence(csm, &status);
122 if (U_FAILURE(status))
123 throw libcdr::EncodingException();
124 CDR_DEBUG_MSG(("UCSDET: getEncoding name %s, confidence %i\n", name, confidence));
125 unsigned short encoding = getEncodingFromICUName(name);
126 ucsdet_close(csd);
127 /* From ICU documentation
128 * A confidence value of ten does have a general meaning - it is used
129 * for charsets that can represent the input data, but for which there
130 * is no other indication that suggests that the charset is the correct
131 * one. Pure 7 bit ASCII data, for example, is compatible with a great
132 * many charsets, most of which will appear as possible matches with
133 * a confidence of 10.
134 */
135 if (confidence == 10)
136 return 0;
137 return encoding;
138 }
139 catch (const libcdr::EncodingException &)
140 {
141 ucsdet_close(csd);
142 return 0;
143 }
144 }
145
146 static void _appendUCS4(librevenge::RVNGString &text, UChar32 ucs4Character)
147 {
148 // Convert carriage returns to new line characters
149 // Writerperfect/LibreOffice will replace them by <text:line-break>
150 if (ucs4Character == (UChar32) 0x0d)
151 ucs4Character = (UChar32) '\n';
152
153 unsigned char outbuf[U8_MAX_LENGTH+1];
154 int i = 0;
155 U8_APPEND_UNSAFE(&outbuf[0], i, ucs4Character);
156 outbuf[i] = 0;
157
158 text.append((char *)outbuf);
159 }
160
161 } // anonymous namespace
162
163 uint8_t libcdr::readU8(librevenge::RVNGInputStream *input, bool /* bigEndian */)
164 {
165 if (!input || input->isEnd())
166 {
167 CDR_DEBUG_MSG(("Throwing EndOfStreamException\n"));
168 throw EndOfStreamException();
169 }
170 unsigned long numBytesRead;
171 uint8_t const *p = input->read(sizeof(uint8_t), numBytesRead);
172
173 if (p && numBytesRead == sizeof(uint8_t))
174 return *(uint8_t const *)(p);
175 CDR_DEBUG_MSG(("Throwing EndOfStreamException\n"));
176 throw EndOfStreamException();
177 }
178
179 uint16_t libcdr::readU16(librevenge::RVNGInputStream *input, bool bigEndian)
180 {
181 if (!input || input->isEnd())
182 {
183 CDR_DEBUG_MSG(("Throwing EndOfStreamException\n"));
184 throw EndOfStreamException();
185 }
186 unsigned long numBytesRead;
187 uint8_t const *p = input->read(sizeof(uint16_t), numBytesRead);
188
189 if (p && numBytesRead == sizeof(uint16_t))
190 {
191 if (bigEndian)
192 return (uint16_t)(p[1]|((uint16_t)p[0]<<8));
193 return (uint16_t)(p[0]|((uint16_t)p[1]<<8));
194 }
195 CDR_DEBUG_MSG(("Throwing EndOfStreamException\n"));
196 throw EndOfStreamException();
197 }
198
199 int16_t libcdr::readS16(librevenge::RVNGInputStream *input, bool bigEndian)
200 {
201 return (int16_t)readU16(input, bigEndian);
202 }
203
204 uint32_t libcdr::readU32(librevenge::RVNGInputStream *input, bool bigEndian)
205 {
206 if (!input || input->isEnd())
207 {
208 CDR_DEBUG_MSG(("Throwing EndOfStreamException\n"));
209 throw EndOfStreamException();
210 }
211 unsigned long numBytesRead;
212 uint8_t const *p = input->read(sizeof(uint32_t), numBytesRead);
213
214 if (p && numBytesRead == sizeof(uint32_t))
215 {
216 if (bigEndian)
217 return (uint32_t)p[3]|((uint32_t)p[2]<<8)|((uint32_t)p[1]<<16)|((uint32_t)p[0]<<24);
218 return (uint32_t)p[0]|((uint32_t)p[1]<<8)|((uint32_t)p[2]<<16)|((uint32_t)p[3]<<24);
219 }
220 CDR_DEBUG_MSG(("Throwing EndOfStreamException\n"));
221 throw EndOfStreamException();
222 }
223
224 int32_t libcdr::readS32(librevenge::RVNGInputStream *input, bool bigEndian)
225 {
226 return (int32_t)readU32(input, bigEndian);
227 }
228
229 uint64_t libcdr::readU64(librevenge::RVNGInputStream *input, bool bigEndian)
230 {
231 if (!input || input->isEnd())
232 {
233 CDR_DEBUG_MSG(("Throwing EndOfStreamException\n"));
234 throw EndOfStreamException();
235 }
236 unsigned long numBytesRead;
237 uint8_t const *p = input->read(sizeof(uint64_t), numBytesRead);
238
239 if (p && numBytesRead == sizeof(uint64_t))
240 {
241 if (bigEndian)
242 return (uint64_t)p[7]|((uint64_t)p[6]<<8)|((uint64_t)p[5]<<16)|((uint64_t)p[4]<<24)|((uint64_t)p[3]<<32)|((uint64_t)p[2]<<40)|((uint64_t)p[1]<<48)|((uint64_t)p[0]<<56);
243 return (uint64_t)p[0]|((uint64_t)p[1]<<8)|((uint64_t)p[2]<<16)|((uint64_t)p[3]<<24)|((uint64_t)p[4]<<32)|((uint64_t)p[5]<<40)|((uint64_t)p[6]<<48)|((uint64_t)p[7]<<56);
244 }
245 CDR_DEBUG_MSG(("Throwing EndOfStreamException\n"));
246 throw EndOfStreamException();
247 }
248
249 double libcdr::readDouble(librevenge::RVNGInputStream *input, bool bigEndian)
250 {
251 union
252 {
253 uint64_t u;
254 double d;
255 } tmpUnion;
256
257 tmpUnion.u = readU64(input, bigEndian);
258
259 return tmpUnion.d;
260 }
261
262 double libcdr::readFixedPoint(librevenge::RVNGInputStream *input, bool bigEndian)
263 {
264 unsigned fixedPointNumber = readU32(input, bigEndian);
265 auto fixedPointNumberIntegerPart = (short)((fixedPointNumber & 0xFFFF0000) >> 16);
266 auto fixedPointNumberFractionalPart = (double)((double)(fixedPointNumber & 0x0000FFFF)/(double)0xFFFF);
267 return ((double)fixedPointNumberIntegerPart + fixedPointNumberFractionalPart);
268 }
269
270 unsigned long libcdr::getLength(librevenge::RVNGInputStream *const input)
271 {
272 if (!input)
273 throw EndOfStreamException();
274
275 const long orig = input->tell();
276 long end = 0;
277
278 if (input->seek(0, librevenge::RVNG_SEEK_END) == 0)
279 {
280 end = input->tell();
281 }
282 else
283 {
284 // RVNG_SEEK_END does not work. Use the harder way.
285 if (input->seek(0, librevenge::RVNG_SEEK_SET) != 0)
286 throw EndOfStreamException();
287 while (!input->isEnd())
288 {
289 readU8(input);
290 ++end;
291 }
292 }
293 assert(end >= 0);
294
295 if (input->seek(orig, librevenge::RVNG_SEEK_SET) != 0)
296 throw EndOfStreamException();
297
298 return static_cast<unsigned long>(end);
299 }
300
301 unsigned long libcdr::getRemainingLength(librevenge::RVNGInputStream *const input)
302 {
303 return getLength(input) - static_cast<unsigned long>(input->tell());
304 }
305
306 int libcdr::cdr_round(double d)
307 {
308 return (d>0) ? int(d+0.5) : int(d-0.5);
309 }
310
311 void libcdr::writeU16(librevenge::RVNGBinaryData &buffer, const int value)
312 {
313 buffer.append((unsigned char)(value & 0xFF));
314 buffer.append((unsigned char)((value >> 8) & 0xFF));
315 }
316
317 void libcdr::writeU32(librevenge::RVNGBinaryData &buffer, const int value)
318 {
319 buffer.append((unsigned char)(value & 0xFF));
320 buffer.append((unsigned char)((value >> 8) & 0xFF));
321 buffer.append((unsigned char)((value >> 16) & 0xFF));
322 buffer.append((unsigned char)((value >> 24) & 0xFF));
323 }
324
325 void libcdr::appendCharacters(librevenge::RVNGString &text, std::vector<unsigned char> characters, unsigned short charset)
326 {
327 if (characters.empty())
328 return;
329 static const UChar32 symbolmap [] =
330 {
331 0x0020, 0x0021, 0x2200, 0x0023, 0x2203, 0x0025, 0x0026, 0x220D, // 0x20 ..
332 0x0028, 0x0029, 0x2217, 0x002B, 0x002C, 0x2212, 0x002E, 0x002F,
333 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
334 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D, 0x003E, 0x003F,
335 0x2245, 0x0391, 0x0392, 0x03A7, 0x0394, 0x0395, 0x03A6, 0x0393,
336 0x0397, 0x0399, 0x03D1, 0x039A, 0x039B, 0x039C, 0x039D, 0x039F,
337 0x03A0, 0x0398, 0x03A1, 0x03A3, 0x03A4, 0x03A5, 0x03C2, 0x03A9,
338 0x039E, 0x03A8, 0x0396, 0x005B, 0x2234, 0x005D, 0x22A5, 0x005F,
339 0xF8E5, 0x03B1, 0x03B2, 0x03C7, 0x03B4, 0x03B5, 0x03C6, 0x03B3,
340 0x03B7, 0x03B9, 0x03D5, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BF,
341 0x03C0, 0x03B8, 0x03C1, 0x03C3, 0x03C4, 0x03C5, 0x03D6, 0x03C9,
342 0x03BE, 0x03C8, 0x03B6, 0x007B, 0x007C, 0x007D, 0x223C, 0x0020, // .. 0x7F
343 0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
344 0x0088, 0x0089, 0x008a, 0x008b, 0x008c, 0x008d, 0x008e, 0x008f,
345 0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
346 0x0098, 0x0099, 0x009a, 0x009b, 0x009c, 0x009d, 0x009E, 0x009f,
347 0x20AC, 0x03D2, 0x2032, 0x2264, 0x2044, 0x221E, 0x0192, 0x2663, // 0xA0 ..
348 0x2666, 0x2665, 0x2660, 0x2194, 0x2190, 0x2191, 0x2192, 0x2193,
349 0x00B0, 0x00B1, 0x2033, 0x2265, 0x00D7, 0x221D, 0x2202, 0x2022,
350 0x00F7, 0x2260, 0x2261, 0x2248, 0x2026, 0x23D0, 0x23AF, 0x21B5,
351 0x2135, 0x2111, 0x211C, 0x2118, 0x2297, 0x2295, 0x2205, 0x2229,
352 0x222A, 0x2283, 0x2287, 0x2284, 0x2282, 0x2286, 0x2208, 0x2209,
353 0x2220, 0x2207, 0x00AE, 0x00A9, 0x2122, 0x220F, 0x221A, 0x22C5,
354 0x00AC, 0x2227, 0x2228, 0x21D4, 0x21D0, 0x21D1, 0x21D2, 0x21D3,
355 0x25CA, 0x3008, 0x00AE, 0x00A9, 0x2122, 0x2211, 0x239B, 0x239C,
356 0x239D, 0x23A1, 0x23A2, 0x23A3, 0x23A7, 0x23A8, 0x23A9, 0x23AA,
357 0xF8FF, 0x3009, 0x222B, 0x2320, 0x23AE, 0x2321, 0x239E, 0x239F,
358 0x23A0, 0x23A4, 0x23A5, 0x23A6, 0x23AB, 0x23AC, 0x23AD, 0x0020 // .. 0xFE
359 };
360
361 if (!charset && !characters.empty())
362 charset = getEncoding(&characters[0], characters.size());
363
364 if (charset == 0x02) // SYMBOL
365 {
366 uint32_t ucs4Character = 0;
367 for (std::vector<unsigned char>::const_iterator iter = characters.begin();
368 iter != characters.end(); ++iter)
369 {
370 if (*iter < 0x20)
371 ucs4Character = 0x20;
372 else
373 ucs4Character = symbolmap[*iter - 0x20];
374 _appendUCS4(text, ucs4Character);
375 }
376 }
377 else
378 {
379 UErrorCode status = U_ZERO_ERROR;
380 UConverter *conv = nullptr;
381 switch (charset)
382 {
383 case 0x80: // SHIFTJIS
384 conv = ucnv_open("windows-932", &status);
385 break;
386 case 0x81: // HANGUL
387 conv = ucnv_open("windows-949", &status);
388 break;
389 case 0x86: // GB2312
390 conv = ucnv_open("windows-936", &status);
391 break;
392 case 0x88: // CHINESEBIG5
393 conv = ucnv_open("windows-950", &status);
394 break;
395 case 0xa1: // GREEEK
396 conv = ucnv_open("windows-1253", &status);
397 break;
398 case 0xa2: // TURKISH
399 conv = ucnv_open("windows-1254", &status);
400 break;
401 case 0xa3: // VIETNAMESE
402 conv = ucnv_open("windows-1258", &status);
403 break;
404 case 0xb1: // HEBREW
405 conv = ucnv_open("windows-1255", &status);
406 break;
407 case 0xb2: // ARABIC
408 conv = ucnv_open("windows-1256", &status);
409 break;
410 case 0xba: // BALTIC
411 conv = ucnv_open("windows-1257", &status);
412 break;
413 case 0xcc: // RUSSIAN
414 conv = ucnv_open("windows-1251", &status);
415 break;
416 case 0xde: // THAI
417 conv = ucnv_open("windows-874", &status);
418 break;
419 case 0xee: // CENTRAL EUROPE
420 conv = ucnv_open("windows-1250", &status);
421 break;
422 default:
423 conv = ucnv_open("windows-1252", &status);
424 break;
425 }
426 if (U_SUCCESS(status) && conv)
427 {
428 const auto *src = (const char *)&characters[0];
429 const char *srcLimit = (const char *)src + characters.size();
430 while (src < srcLimit)
431 {
432 UChar32 ucs4Character = ucnv_getNextUChar(conv, &src, srcLimit, &status);
433 if (U_SUCCESS(status) && U_IS_UNICODE_CHAR(ucs4Character))
434 _appendUCS4(text, ucs4Character);
435 }
436 }
437 if (conv)
438 ucnv_close(conv);
439 }
440 }
441
442 void libcdr::appendCharacters(librevenge::RVNGString &text, std::vector<unsigned char> characters)
443 {
444 if (characters.empty())
445 return;
446
447 UErrorCode status = U_ZERO_ERROR;
448 UConverter *conv = ucnv_open("UTF-16LE", &status);
449
450 if (U_SUCCESS(status) && conv)
451 {
452 const auto *src = (const char *)&characters[0];
453 const char *srcLimit = (const char *)src + characters.size();
454 while (src < srcLimit)
455 {
456 UChar32 ucs4Character = ucnv_getNextUChar(conv, &src, srcLimit, &status);
457 if (U_SUCCESS(status) && U_IS_UNICODE_CHAR(ucs4Character))
458 _appendUCS4(text, ucs4Character);
459 }
460 }
461 if (conv)
462 ucnv_close(conv);
463 }
464
465 void libcdr::appendUTF8Characters(librevenge::RVNGString &text, std::vector<unsigned char> characters)
466 {
467 if (characters.empty())
468 return;
469
470 for (std::vector<unsigned char>::const_iterator iter = characters.begin(); iter != characters.end(); ++iter)
471 text.append((char)*iter);
472 }
473
474 #ifdef DEBUG
475
476 void libcdr::debugPrint(const char *const format, ...)
477 {
478 va_list args;
479 va_start(args, format);
480 std::vfprintf(stderr, format, args);
481 va_end(args);
482 }
483
484 const char *libcdr::toFourCC(unsigned value, bool bigEndian)
485 {
486 static char sValue[5] = { 0, 0, 0, 0, 0 };
487 if (bigEndian)
488 {
489 sValue[3] = (char)(value & 0xff);
490 sValue[2] = (char)((value & 0xff00) >> 8);
491 sValue[1] = (char)((value & 0xff0000) >> 16);
492 sValue[0] = (char)((value & 0xff000000) >> 24);
493 }
494 else
495 {
496 sValue[0] = (char)(value & 0xff);
497 sValue[1] = (char)((value & 0xff00) >> 8);
498 sValue[2] = (char)((value & 0xff0000) >> 16);
499 sValue[3] = (char)((value & 0xff000000) >> 24);
500 }
501 return sValue;
502 }
503 #endif
504
505 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */