STEPFileEncoding.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2020, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** @file STEPFileEncoding.cpp
  34. * @brief STEP character handling, string un-escaping
  35. */
  36. #include "STEPFileEncoding.h"
  37. #include <assimp/fast_atof.h>
  38. #ifdef ASSIMP_USE_HUNTER
  39. # include <utf8.h>
  40. #else
  41. # include <contrib/utf8cpp/source/utf8.h>
  42. #endif
  43. #include <memory>
  44. using namespace Assimp;
  45. // roman1 to utf16 table
  46. static const uint16_t mac_codetable[] = {
  47. // 0x20 unassig./nonprint. slots
  48. 0x0020 ,
  49. 0x0021 ,
  50. 0x0022 ,
  51. 0x0023 ,
  52. 0x0024 ,
  53. 0x0025 ,
  54. 0x0026 ,
  55. 0x0027 ,
  56. 0x0028 ,
  57. 0x0029 ,
  58. 0x002A ,
  59. 0x002B ,
  60. 0x002C ,
  61. 0x002D ,
  62. 0x002E ,
  63. 0x002F ,
  64. 0x0030 ,
  65. 0x0031 ,
  66. 0x0032 ,
  67. 0x0033 ,
  68. 0x0034 ,
  69. 0x0035 ,
  70. 0x0036 ,
  71. 0x0037 ,
  72. 0x0038 ,
  73. 0x0039 ,
  74. 0x003A ,
  75. 0x003B ,
  76. 0x003C ,
  77. 0x003D ,
  78. 0x003E ,
  79. 0x003F ,
  80. 0x0040 ,
  81. 0x0041 ,
  82. 0x0042 ,
  83. 0x0043 ,
  84. 0x0044 ,
  85. 0x0045 ,
  86. 0x0046 ,
  87. 0x0047 ,
  88. 0x0048 ,
  89. 0x0049 ,
  90. 0x004A ,
  91. 0x004B ,
  92. 0x004C ,
  93. 0x004D ,
  94. 0x004E ,
  95. 0x004F ,
  96. 0x0050 ,
  97. 0x0051 ,
  98. 0x0052 ,
  99. 0x0053 ,
  100. 0x0054 ,
  101. 0x0055 ,
  102. 0x0056 ,
  103. 0x0057 ,
  104. 0x0058 ,
  105. 0x0059 ,
  106. 0x005A ,
  107. 0x005B ,
  108. 0x005C ,
  109. 0x005D ,
  110. 0x005E ,
  111. 0x005F ,
  112. 0x0060 ,
  113. 0x0061 ,
  114. 0x0062 ,
  115. 0x0063 ,
  116. 0x0064 ,
  117. 0x0065 ,
  118. 0x0066 ,
  119. 0x0067 ,
  120. 0x0068 ,
  121. 0x0069 ,
  122. 0x006A ,
  123. 0x006B ,
  124. 0x006C ,
  125. 0x006D ,
  126. 0x006E ,
  127. 0x006F ,
  128. 0x0070 ,
  129. 0x0071 ,
  130. 0x0072 ,
  131. 0x0073 ,
  132. 0x0074 ,
  133. 0x0075 ,
  134. 0x0076 ,
  135. 0x0077 ,
  136. 0x0078 ,
  137. 0x0079 ,
  138. 0x007A ,
  139. 0x007B ,
  140. 0x007C ,
  141. 0x007D ,
  142. 0x007E ,
  143. 0x0000 , // unassig.
  144. 0x00C4 ,
  145. 0x00C5 ,
  146. 0x00C7 ,
  147. 0x00C9 ,
  148. 0x00D1 ,
  149. 0x00D6 ,
  150. 0x00DC ,
  151. 0x00E1 ,
  152. 0x00E0 ,
  153. 0x00E2 ,
  154. 0x00E4 ,
  155. 0x00E3 ,
  156. 0x00E5 ,
  157. 0x00E7 ,
  158. 0x00E9 ,
  159. 0x00E8 ,
  160. 0x00EA ,
  161. 0x00EB ,
  162. 0x00ED ,
  163. 0x00EC ,
  164. 0x00EE ,
  165. 0x00EF ,
  166. 0x00F1 ,
  167. 0x00F3 ,
  168. 0x00F2 ,
  169. 0x00F4 ,
  170. 0x00F6 ,
  171. 0x00F5 ,
  172. 0x00FA ,
  173. 0x00F9 ,
  174. 0x00FB ,
  175. 0x00FC ,
  176. 0x2020 ,
  177. 0x00B0 ,
  178. 0x00A2 ,
  179. 0x00A3 ,
  180. 0x00A7 ,
  181. 0x2022 ,
  182. 0x00B6 ,
  183. 0x00DF ,
  184. 0x00AE ,
  185. 0x00A9 ,
  186. 0x2122 ,
  187. 0x00B4 ,
  188. 0x00A8 ,
  189. 0x2260 ,
  190. 0x00C6 ,
  191. 0x00D8 ,
  192. 0x221E ,
  193. 0x00B1 ,
  194. 0x2264 ,
  195. 0x2265 ,
  196. 0x00A5 ,
  197. 0x00B5 ,
  198. 0x2202 ,
  199. 0x2211 ,
  200. 0x220F ,
  201. 0x03C0 ,
  202. 0x222B ,
  203. 0x00AA ,
  204. 0x00BA ,
  205. 0x03A9 ,
  206. 0x00E6 ,
  207. 0x00F8 ,
  208. 0x00BF ,
  209. 0x00A1 ,
  210. 0x00AC ,
  211. 0x221A ,
  212. 0x0192 ,
  213. 0x2248 ,
  214. 0x2206 ,
  215. 0x00AB ,
  216. 0x00BB ,
  217. 0x2026 ,
  218. 0x00A0 ,
  219. 0x00C0 ,
  220. 0x00C3 ,
  221. 0x00D5 ,
  222. 0x0152 ,
  223. 0x0153 ,
  224. 0x2013 ,
  225. 0x2014 ,
  226. 0x201C ,
  227. 0x201D ,
  228. 0x2018 ,
  229. 0x2019 ,
  230. 0x00F7 ,
  231. 0x25CA ,
  232. 0x00FF ,
  233. 0x0178 ,
  234. 0x2044 ,
  235. 0x20AC ,
  236. 0x2039 ,
  237. 0x203A ,
  238. 0xFB01 ,
  239. 0xFB02 ,
  240. 0x2021 ,
  241. 0x00B7 ,
  242. 0x201A ,
  243. 0x201E ,
  244. 0x2030 ,
  245. 0x00C2 ,
  246. 0x00CA ,
  247. 0x00C1 ,
  248. 0x00CB ,
  249. 0x00C8 ,
  250. 0x00CD ,
  251. 0x00CE ,
  252. 0x00CF ,
  253. 0x00CC ,
  254. 0x00D3 ,
  255. 0x00D4 ,
  256. 0xF8FF ,
  257. 0x00D2 ,
  258. 0x00DA ,
  259. 0x00DB ,
  260. 0x00D9 ,
  261. 0x0131 ,
  262. 0x02C6 ,
  263. 0x02DC ,
  264. 0x00AF ,
  265. 0x02D8 ,
  266. 0x02D9 ,
  267. 0x02DA ,
  268. 0x00B8 ,
  269. 0x02DD ,
  270. 0x02DB ,
  271. 0x02C7
  272. };
  273. // ------------------------------------------------------------------------------------------------
  274. bool STEP::StringToUTF8(std::string& s)
  275. {
  276. // very basic handling for escaped string sequences
  277. // http://doc.spatial.com/index.php?title=InterOp:Connect/STEP&redirect=no
  278. for (size_t i = 0; i < s.size(); ) {
  279. if (s[i] == '\\') {
  280. // \S\X - cp1252 (X is the character remapped to [0,127])
  281. if (i+3 < s.size() && s[i+1] == 'S' && s[i+2] == '\\') {
  282. // http://stackoverflow.com/questions/5586214/how-to-convert-char-from-iso-8859-1-to-utf-8-in-c-multiplatformly
  283. ai_assert((uint8_t)s[i+3] < 0x80);
  284. const uint8_t ch = s[i+3] + 0x80;
  285. s[i] = 0xc0 | (ch & 0xc0) >> 6;
  286. s[i+1] = 0x80 | (ch & 0x3f);
  287. s.erase(i + 2,2);
  288. ++i;
  289. }
  290. // \X\xx - mac/roman (xx is a hex sequence)
  291. else if (i+4 < s.size() && s[i+1] == 'X' && s[i+2] == '\\') {
  292. const uint8_t macval = HexOctetToDecimal(s.c_str() + i + 3);
  293. if(macval < 0x20) {
  294. return false;
  295. }
  296. ai_assert(sizeof(mac_codetable) / sizeof(mac_codetable[0]) == 0x100-0x20);
  297. const uint32_t unival = mac_codetable[macval - 0x20], *univalp = &unival;
  298. unsigned char temp[5], *tempp = temp;
  299. ai_assert(sizeof( unsigned char ) == 1);
  300. utf8::utf32to8( univalp, univalp + 1, tempp );
  301. const size_t outcount = static_cast<size_t>(tempp-temp);
  302. s.erase(i,5);
  303. s.insert(i, reinterpret_cast<char*>(temp), outcount);
  304. i += outcount;
  305. }
  306. // \Xn\ .. \X0\ - various unicode encodings (n=2: utf16; n=4: utf32)
  307. else if (i+3 < s.size() && s[i+1] == 'X' && s[i+2] >= '0' && s[i+2] <= '9') {
  308. switch(s[i+2]) {
  309. // utf16
  310. case '2':
  311. // utf32
  312. case '4':
  313. if (s[i+3] == '\\') {
  314. const size_t basei = i+4;
  315. size_t j = basei, jend = s.size()-3;
  316. for (; j < jend; ++j) {
  317. if (s[j] == '\\' && s[j+1] == 'X' && s[j+2] == '0' && s[j+3] == '\\') {
  318. break;
  319. }
  320. }
  321. if (j == jend) {
  322. return false;
  323. }
  324. if (j == basei) {
  325. s.erase(i,8);
  326. continue;
  327. }
  328. if (s[i+2] == '2') {
  329. if (((j - basei) % 4) != 0) {
  330. return false;
  331. }
  332. const size_t count = (j-basei)/4;
  333. std::unique_ptr<uint16_t[]> src(new uint16_t[count]);
  334. const char* cur = s.c_str() + basei;
  335. for (size_t k = 0; k < count; ++k, cur += 4) {
  336. src[k] = (static_cast<uint16_t>(HexOctetToDecimal(cur)) << 8u) |
  337. static_cast<uint16_t>(HexOctetToDecimal(cur+2));
  338. }
  339. const size_t dcount = count * 3; // this is enough to hold all possible outputs
  340. std::unique_ptr<unsigned char[]> dest(new unsigned char[dcount]);
  341. const uint16_t* srct = src.get();
  342. unsigned char* destt = dest.get();
  343. utf8::utf16to8( srct, srct + count, destt );
  344. const size_t outcount = static_cast<size_t>(destt-dest.get());
  345. s.erase(i,(j+4-i));
  346. ai_assert(sizeof(unsigned char) == 1);
  347. s.insert(i, reinterpret_cast<char*>(dest.get()), outcount);
  348. i += outcount;
  349. continue;
  350. }
  351. else if (s[i+2] == '4') {
  352. if (((j - basei) % 8) != 0) {
  353. return false;
  354. }
  355. const size_t count = (j-basei)/8;
  356. std::unique_ptr<uint32_t[]> src(new uint32_t[count]);
  357. const char* cur = s.c_str() + basei;
  358. for (size_t k = 0; k < count; ++k, cur += 8) {
  359. src[k] = (static_cast<uint32_t>(HexOctetToDecimal(cur )) << 24u) |
  360. (static_cast<uint32_t>(HexOctetToDecimal(cur+2)) << 16u) |
  361. (static_cast<uint32_t>(HexOctetToDecimal(cur+4)) << 8u) |
  362. (static_cast<uint32_t>(HexOctetToDecimal(cur+6)));
  363. }
  364. const size_t dcount = count * 5; // this is enough to hold all possible outputs
  365. std::unique_ptr<unsigned char[]> dest(new unsigned char[dcount]);
  366. const uint32_t* srct = src.get();
  367. unsigned char* destt = dest.get();
  368. utf8::utf32to8( srct, srct + count, destt );
  369. const size_t outcount = static_cast<size_t>(destt-dest.get());
  370. s.erase(i,(j+4-i));
  371. ai_assert(sizeof(unsigned char) == 1);
  372. s.insert(i, reinterpret_cast<char*>(dest.get()), outcount);
  373. i += outcount;
  374. continue;
  375. }
  376. }
  377. break;
  378. // TODO: other encoding patterns?
  379. default:
  380. return false;
  381. }
  382. }
  383. }
  384. ++i;
  385. }
  386. return true;
  387. }