25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

434 satır
8.8 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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 unescaping
  35. */
  36. #include "AssimpPCH.h"
  37. #include "STEPFileEncoding.h"
  38. #include "fast_atof.h"
  39. #include "../contrib/ConvertUTF/ConvertUTF.h"
  40. using namespace Assimp;
  41. // roman1 to utf16 table
  42. static const UTF16 mac_codetable[] = {
  43. // 0x20 unassig./nonprint. slots
  44. 0x0020 ,
  45. 0x0021 ,
  46. 0x0022 ,
  47. 0x0023 ,
  48. 0x0024 ,
  49. 0x0025 ,
  50. 0x0026 ,
  51. 0x0027 ,
  52. 0x0028 ,
  53. 0x0029 ,
  54. 0x002A ,
  55. 0x002B ,
  56. 0x002C ,
  57. 0x002D ,
  58. 0x002E ,
  59. 0x002F ,
  60. 0x0030 ,
  61. 0x0031 ,
  62. 0x0032 ,
  63. 0x0033 ,
  64. 0x0034 ,
  65. 0x0035 ,
  66. 0x0036 ,
  67. 0x0037 ,
  68. 0x0038 ,
  69. 0x0039 ,
  70. 0x003A ,
  71. 0x003B ,
  72. 0x003C ,
  73. 0x003D ,
  74. 0x003E ,
  75. 0x003F ,
  76. 0x0040 ,
  77. 0x0041 ,
  78. 0x0042 ,
  79. 0x0043 ,
  80. 0x0044 ,
  81. 0x0045 ,
  82. 0x0046 ,
  83. 0x0047 ,
  84. 0x0048 ,
  85. 0x0049 ,
  86. 0x004A ,
  87. 0x004B ,
  88. 0x004C ,
  89. 0x004D ,
  90. 0x004E ,
  91. 0x004F ,
  92. 0x0050 ,
  93. 0x0051 ,
  94. 0x0052 ,
  95. 0x0053 ,
  96. 0x0054 ,
  97. 0x0055 ,
  98. 0x0056 ,
  99. 0x0057 ,
  100. 0x0058 ,
  101. 0x0059 ,
  102. 0x005A ,
  103. 0x005B ,
  104. 0x005C ,
  105. 0x005D ,
  106. 0x005E ,
  107. 0x005F ,
  108. 0x0060 ,
  109. 0x0061 ,
  110. 0x0062 ,
  111. 0x0063 ,
  112. 0x0064 ,
  113. 0x0065 ,
  114. 0x0066 ,
  115. 0x0067 ,
  116. 0x0068 ,
  117. 0x0069 ,
  118. 0x006A ,
  119. 0x006B ,
  120. 0x006C ,
  121. 0x006D ,
  122. 0x006E ,
  123. 0x006F ,
  124. 0x0070 ,
  125. 0x0071 ,
  126. 0x0072 ,
  127. 0x0073 ,
  128. 0x0074 ,
  129. 0x0075 ,
  130. 0x0076 ,
  131. 0x0077 ,
  132. 0x0078 ,
  133. 0x0079 ,
  134. 0x007A ,
  135. 0x007B ,
  136. 0x007C ,
  137. 0x007D ,
  138. 0x007E ,
  139. 0x0000 , // unassig.
  140. 0x00C4 ,
  141. 0x00C5 ,
  142. 0x00C7 ,
  143. 0x00C9 ,
  144. 0x00D1 ,
  145. 0x00D6 ,
  146. 0x00DC ,
  147. 0x00E1 ,
  148. 0x00E0 ,
  149. 0x00E2 ,
  150. 0x00E4 ,
  151. 0x00E3 ,
  152. 0x00E5 ,
  153. 0x00E7 ,
  154. 0x00E9 ,
  155. 0x00E8 ,
  156. 0x00EA ,
  157. 0x00EB ,
  158. 0x00ED ,
  159. 0x00EC ,
  160. 0x00EE ,
  161. 0x00EF ,
  162. 0x00F1 ,
  163. 0x00F3 ,
  164. 0x00F2 ,
  165. 0x00F4 ,
  166. 0x00F6 ,
  167. 0x00F5 ,
  168. 0x00FA ,
  169. 0x00F9 ,
  170. 0x00FB ,
  171. 0x00FC ,
  172. 0x2020 ,
  173. 0x00B0 ,
  174. 0x00A2 ,
  175. 0x00A3 ,
  176. 0x00A7 ,
  177. 0x2022 ,
  178. 0x00B6 ,
  179. 0x00DF ,
  180. 0x00AE ,
  181. 0x00A9 ,
  182. 0x2122 ,
  183. 0x00B4 ,
  184. 0x00A8 ,
  185. 0x2260 ,
  186. 0x00C6 ,
  187. 0x00D8 ,
  188. 0x221E ,
  189. 0x00B1 ,
  190. 0x2264 ,
  191. 0x2265 ,
  192. 0x00A5 ,
  193. 0x00B5 ,
  194. 0x2202 ,
  195. 0x2211 ,
  196. 0x220F ,
  197. 0x03C0 ,
  198. 0x222B ,
  199. 0x00AA ,
  200. 0x00BA ,
  201. 0x03A9 ,
  202. 0x00E6 ,
  203. 0x00F8 ,
  204. 0x00BF ,
  205. 0x00A1 ,
  206. 0x00AC ,
  207. 0x221A ,
  208. 0x0192 ,
  209. 0x2248 ,
  210. 0x2206 ,
  211. 0x00AB ,
  212. 0x00BB ,
  213. 0x2026 ,
  214. 0x00A0 ,
  215. 0x00C0 ,
  216. 0x00C3 ,
  217. 0x00D5 ,
  218. 0x0152 ,
  219. 0x0153 ,
  220. 0x2013 ,
  221. 0x2014 ,
  222. 0x201C ,
  223. 0x201D ,
  224. 0x2018 ,
  225. 0x2019 ,
  226. 0x00F7 ,
  227. 0x25CA ,
  228. 0x00FF ,
  229. 0x0178 ,
  230. 0x2044 ,
  231. 0x20AC ,
  232. 0x2039 ,
  233. 0x203A ,
  234. 0xFB01 ,
  235. 0xFB02 ,
  236. 0x2021 ,
  237. 0x00B7 ,
  238. 0x201A ,
  239. 0x201E ,
  240. 0x2030 ,
  241. 0x00C2 ,
  242. 0x00CA ,
  243. 0x00C1 ,
  244. 0x00CB ,
  245. 0x00C8 ,
  246. 0x00CD ,
  247. 0x00CE ,
  248. 0x00CF ,
  249. 0x00CC ,
  250. 0x00D3 ,
  251. 0x00D4 ,
  252. 0xF8FF ,
  253. 0x00D2 ,
  254. 0x00DA ,
  255. 0x00DB ,
  256. 0x00D9 ,
  257. 0x0131 ,
  258. 0x02C6 ,
  259. 0x02DC ,
  260. 0x00AF ,
  261. 0x02D8 ,
  262. 0x02D9 ,
  263. 0x02DA ,
  264. 0x00B8 ,
  265. 0x02DD ,
  266. 0x02DB ,
  267. 0x02C7
  268. };
  269. // ------------------------------------------------------------------------------------------------
  270. bool STEP::StringToUTF8(std::string& s)
  271. {
  272. // very basic handling for escaped string sequences
  273. // http://doc.spatial.com/index.php?title=InterOp:Connect/STEP&redirect=no
  274. for (size_t i = 0; i < s.size(); ) {
  275. if (s[i] == '\\') {
  276. // \S\X - cp1252 (X is the character remapped to [0,127])
  277. if (i+3 < s.size() && s[i+1] == 'S' && s[i+2] == '\\') {
  278. // http://stackoverflow.com/questions/5586214/how-to-convert-char-from-iso-8859-1-to-utf-8-in-c-multiplatformly
  279. ai_assert((uint8_t)s[i+3] < 0x80);
  280. const uint8_t ch = s[i+3] + 0x80;
  281. s[i] = 0xc0 | (ch & 0xc0) >> 6;
  282. s[i+1] = 0x80 | (ch & 0x3f);
  283. s.erase(i + 2,2);
  284. ++i;
  285. }
  286. // \X\xx - mac/roman (xx is a hex sequence)
  287. else if (i+4 < s.size() && s[i+1] == 'X' && s[i+2] == '\\') {
  288. const uint8_t macval = HexOctetToDecimal(s.c_str() + i + 3);
  289. if(macval < 0x20) {
  290. return false;
  291. }
  292. ai_assert(sizeof(mac_codetable) / sizeof(mac_codetable[0]) == 0x100-0x20);
  293. const UTF32 unival = mac_codetable[macval - 0x20], *univalp = &unival;
  294. UTF8 temp[5], *tempp = temp;
  295. ai_assert(sizeof(UTF8) == 1);
  296. if(ConvertUTF32toUTF8(&univalp, univalp+1, &tempp, tempp+sizeof(temp), lenientConversion) != conversionOK) {
  297. return false;
  298. }
  299. const size_t outcount = static_cast<size_t>(tempp-temp);
  300. s.erase(i,5);
  301. s.insert(i, reinterpret_cast<char*>(temp), outcount);
  302. i += outcount;
  303. }
  304. // \Xn\ .. \X0\ - various unicode encodings (n=2: utf16; n=4: utf32)
  305. else if (i+3 < s.size() && s[i+1] == 'X' && s[i+2] >= '0' && s[i+2] <= '9') {
  306. switch(s[i+2]) {
  307. // utf16
  308. case '2':
  309. // utf32
  310. case '4':
  311. if (s[i+3] == '\\') {
  312. const size_t basei = i+4;
  313. size_t j = basei, jend = s.size()-4;
  314. for (; j < jend; ++j) {
  315. if (s[j] == '\\' && s[j] == 'X' && s[j] == '0' && s[j] == '\\') {
  316. break;
  317. }
  318. }
  319. if (j == jend) {
  320. return false;
  321. }
  322. if (j == basei) {
  323. s.erase(i,8);
  324. continue;
  325. }
  326. if (s[i+2] == '2') {
  327. if (((j - basei) % 4) != 0) {
  328. return false;
  329. }
  330. const size_t count = (j-basei)/4;
  331. boost::scoped_array<UTF16> src(new UTF16[count]);
  332. const char* cur = s.c_str() + basei;
  333. for (size_t k = 0; k < count; ++k, cur += 4) {
  334. src[k] = (static_cast<UTF16>(HexOctetToDecimal(cur)) << 8u) |
  335. static_cast<UTF16>(HexOctetToDecimal(cur+2));
  336. }
  337. const size_t dcount = count * 3; // this is enough to hold all possible outputs
  338. boost::scoped_array<UTF8> dest(new UTF8[dcount]);
  339. const UTF16* srct = src.get();
  340. UTF8* destt = dest.get();
  341. if(ConvertUTF16toUTF8(&srct, srct+count, &destt, destt+dcount, lenientConversion) != conversionOK) {
  342. return false;
  343. }
  344. const size_t outcount = static_cast<size_t>(destt-dest.get());
  345. s.erase(i,(j+4-i));
  346. ai_assert(sizeof(UTF8) == 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. boost::scoped_array<UTF32> src(new UTF32[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<UTF32>(HexOctetToDecimal(cur )) << 24u) |
  360. (static_cast<UTF32>(HexOctetToDecimal(cur+2)) << 16u) |
  361. (static_cast<UTF32>(HexOctetToDecimal(cur+4)) << 8u) |
  362. (static_cast<UTF32>(HexOctetToDecimal(cur+6)));
  363. }
  364. const size_t dcount = count * 5; // this is enough to hold all possible outputs
  365. boost::scoped_array<UTF8> dest(new UTF8[dcount]);
  366. const UTF32* srct = src.get();
  367. UTF8* destt = dest.get();
  368. if(ConvertUTF32toUTF8(&srct, srct+count, &destt, destt+dcount, lenientConversion) != conversionOK) {
  369. return false;
  370. }
  371. const size_t outcount = static_cast<size_t>(destt-dest.get());
  372. s.erase(i,(j+4-i));
  373. ai_assert(sizeof(UTF8) == 1);
  374. s.insert(i, reinterpret_cast<char*>(dest.get()), outcount);
  375. i += outcount;
  376. continue;
  377. }
  378. }
  379. break;
  380. // TODO: other encoding patterns?
  381. default:
  382. return false;
  383. }
  384. }
  385. }
  386. ++i;
  387. }
  388. return true;
  389. }