2
0

unicode.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <stdio.h>
  23. #include "core/frameAllocator.h"
  24. #include "core/strings/unicode.h"
  25. #include "core/strings/stringFunctions.h"
  26. #include "platform/profiler.h"
  27. #include "console/console.h"
  28. #define TORQUE_ENABLE_UTF16_CACHE
  29. #ifdef TORQUE_ENABLE_UTF16_CACHE
  30. #include "core/util/tDictionary.h"
  31. #include "core/util/hashFunction.h"
  32. #endif
  33. //-----------------------------------------------------------------------------
  34. /// replacement character. Standard correct value is 0xFFFD.
  35. #define kReplacementChar 0xFFFD
  36. /// Look up table. Shift a byte >> 1, then look up how many bytes to expect after it.
  37. /// Contains -1's for illegal values.
  38. static const U8 sgFirstByteLUT[128] =
  39. {
  40. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x0F // single byte ascii
  41. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x1F // single byte ascii
  42. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x2F // single byte ascii
  43. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x3F // single byte ascii
  44. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x4F // trailing utf8
  45. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x5F // trailing utf8
  46. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // 0x6F // first of 2
  47. 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 6, 0, // 0x7F // first of 3,4,5,illegal in utf-8
  48. };
  49. /// Look up table. Shift a 16-bit word >> 10, then look up whether it is a surrogate,
  50. /// and which part. 0 means non-surrogate, 1 means 1st in pair, 2 means 2nd in pair.
  51. static const U8 sgSurrogateLUT[64] =
  52. {
  53. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x0F
  54. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x1F
  55. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x2F
  56. 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, // 0x3F
  57. };
  58. /// Look up table. Feed value from firstByteLUT in, gives you
  59. /// the mask for the data bits of that UTF-8 code unit.
  60. static const U8 sgByteMask8LUT[] = { 0x3f, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 }; // last 0=6, 1=7, 2=5, 4, 3, 2, 1 bits
  61. /// Mask for the data bits of a UTF-16 surrogate.
  62. static const U16 sgByteMaskLow10 = 0x03ff;
  63. //-----------------------------------------------------------------------------
  64. #ifdef TORQUE_ENABLE_UTF16_CACHE
  65. /// Cache data for UTF16 strings. This is wrapped in a class so that data is
  66. /// automatically freed when the hash table is deleted.
  67. struct UTF16Cache
  68. {
  69. UTF16 *mString;
  70. U32 mLength;
  71. UTF16Cache()
  72. {
  73. mString = NULL;
  74. mLength = 0;
  75. }
  76. UTF16Cache(UTF16 *str, U32 len)
  77. {
  78. mLength = len;
  79. mString = new UTF16[mLength];
  80. dMemcpy(mString, str, mLength * sizeof(UTF16));
  81. }
  82. UTF16Cache(const UTF16Cache &other)
  83. {
  84. mLength = other.mLength;
  85. mString = new UTF16[mLength];
  86. dMemcpy(mString, other.mString, mLength * sizeof(UTF16));
  87. }
  88. UTF16Cache & operator =(const UTF16Cache &other)
  89. {
  90. if (&other != this)
  91. {
  92. delete [] mString;
  93. mLength = other.mLength;
  94. mString = new UTF16[mLength];
  95. dMemcpy(mString, other.mString, mLength * sizeof(UTF16));
  96. }
  97. return *this;
  98. }
  99. ~UTF16Cache()
  100. {
  101. delete [] mString;
  102. }
  103. void copyToBuffer(UTF16 *outBuffer, U32 lenToCopy, bool nullTerminate = true) const
  104. {
  105. U32 copy = getMin(mLength, lenToCopy);
  106. if(mString && copy > 0)
  107. dMemcpy(outBuffer, mString, copy * sizeof(UTF16));
  108. if(nullTerminate)
  109. outBuffer[copy] = 0;
  110. }
  111. };
  112. /// Cache for UTF16 strings
  113. typedef HashTable<U32, UTF16Cache> UTF16CacheTable;
  114. static UTF16CacheTable sgUTF16Cache;
  115. #endif // TORQUE_ENABLE_UTF16_CACHE
  116. //-----------------------------------------------------------------------------
  117. inline bool isSurrogateRange(U32 codepoint)
  118. {
  119. return ( 0xd800 < codepoint && codepoint < 0xdfff );
  120. }
  121. inline bool isAboveBMP(U32 codepoint)
  122. {
  123. return ( codepoint > 0xFFFF );
  124. }
  125. //-----------------------------------------------------------------------------
  126. U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len)
  127. {
  128. AssertFatal(len >= 1, "Buffer for unicode conversion must be large enough to hold at least the null terminator.");
  129. PROFILE_SCOPE(convertUTF8toUTF16);
  130. #ifdef TORQUE_ENABLE_UTF16_CACHE
  131. // If we have cached this conversion already, don't do it again
  132. U32 hashKey = Torque::hash((const U8 *)unistring, dStrlen(unistring), 0);
  133. UTF16CacheTable::Iterator cacheItr = sgUTF16Cache.find(hashKey);
  134. if(cacheItr != sgUTF16Cache.end())
  135. {
  136. const UTF16Cache &cache = (*cacheItr).value;
  137. cache.copyToBuffer(outbuffer, len);
  138. return getMin(cache.mLength,len - 1);
  139. }
  140. #endif
  141. U32 walked, nCodepoints;
  142. UTF32 middleman;
  143. nCodepoints=0;
  144. while(*unistring != '\0' && nCodepoints < len)
  145. {
  146. walked = 1;
  147. middleman = oneUTF8toUTF32(unistring,&walked);
  148. outbuffer[nCodepoints] = oneUTF32toUTF16(middleman);
  149. unistring+=walked;
  150. nCodepoints++;
  151. }
  152. nCodepoints = getMin(nCodepoints,len - 1);
  153. outbuffer[nCodepoints] = '\0';
  154. #ifdef TORQUE_ENABLE_UTF16_CACHE
  155. // Cache the results.
  156. // FIXME As written, this will result in some unnecessary memory copying due to copy constructor calls.
  157. UTF16Cache cache(outbuffer, nCodepoints);
  158. sgUTF16Cache.insertUnique(hashKey, cache);
  159. #endif
  160. return nCodepoints;
  161. }
  162. //-----------------------------------------------------------------------------
  163. U32 convertUTF16toUTF8N( const UTF16 *unistring, UTF8 *outbuffer, U32 len)
  164. {
  165. AssertFatal(len >= 1, "Buffer for unicode conversion must be large enough to hold at least the null terminator.");
  166. PROFILE_START(convertUTF16toUTF8);
  167. U32 walked, nCodeunits, codeunitLen;
  168. UTF32 middleman;
  169. nCodeunits=0;
  170. while( *unistring != '\0' && nCodeunits + 3 < len )
  171. {
  172. walked = 1;
  173. middleman = oneUTF16toUTF32(unistring,&walked);
  174. codeunitLen = oneUTF32toUTF8(middleman, &outbuffer[nCodeunits]);
  175. unistring += walked;
  176. nCodeunits += codeunitLen;
  177. }
  178. nCodeunits = getMin(nCodeunits,len - 1);
  179. outbuffer[nCodeunits] = '\0';
  180. PROFILE_END();
  181. return nCodeunits;
  182. }
  183. U32 convertUTF16toUTF8DoubleNULL( const UTF16 *unistring, UTF8 *outbuffer, U32 len)
  184. {
  185. AssertFatal(len >= 1, "Buffer for unicode conversion must be large enough to hold at least the null terminator.");
  186. PROFILE_START(convertUTF16toUTF8DoubleNULL);
  187. U32 walked, nCodeunits, codeunitLen;
  188. UTF32 middleman;
  189. nCodeunits=0;
  190. while( ! (*unistring == '\0' && *(unistring + 1) == '\0') && nCodeunits + 3 < len )
  191. {
  192. walked = 1;
  193. middleman = oneUTF16toUTF32(unistring,&walked);
  194. codeunitLen = oneUTF32toUTF8(middleman, &outbuffer[nCodeunits]);
  195. unistring += walked;
  196. nCodeunits += codeunitLen;
  197. }
  198. nCodeunits = getMin(nCodeunits,len - 1);
  199. outbuffer[nCodeunits] = '\0';
  200. outbuffer[nCodeunits+1] = '\0';
  201. PROFILE_END();
  202. return nCodeunits;
  203. }
  204. //-----------------------------------------------------------------------------
  205. // Functions that convert buffers of unicode code points
  206. //-----------------------------------------------------------------------------
  207. UTF16* createUTF16string( const UTF8* unistring)
  208. {
  209. PROFILE_SCOPE(createUTF16string);
  210. // allocate plenty of memory.
  211. U32 nCodepoints, len = dStrlen(unistring) + 1;
  212. FrameTemp<UTF16> buf(len);
  213. // perform conversion
  214. nCodepoints = convertUTF8toUTF16N( unistring, buf, len);
  215. // add 1 for the NULL terminator the converter promises it included.
  216. nCodepoints++;
  217. // allocate the return buffer, copy over, and return it.
  218. UTF16 *ret = new UTF16[nCodepoints];
  219. dMemcpy(ret, buf, nCodepoints * sizeof(UTF16));
  220. return ret;
  221. }
  222. //-----------------------------------------------------------------------------
  223. UTF8* createUTF8string( const UTF16* unistring)
  224. {
  225. PROFILE_SCOPE(createUTF8string);
  226. // allocate plenty of memory.
  227. U32 nCodeunits, len = dStrlen(unistring) * 3 + 1;
  228. FrameTemp<UTF8> buf(len);
  229. // perform conversion
  230. nCodeunits = convertUTF16toUTF8N( unistring, buf, len);
  231. // add 1 for the NULL terminator the converter promises it included.
  232. nCodeunits++;
  233. // allocate the return buffer, copy over, and return it.
  234. UTF8 *ret = new UTF8[nCodeunits];
  235. dMemcpy(ret, buf, nCodeunits * sizeof(UTF8));
  236. return ret;
  237. }
  238. //-----------------------------------------------------------------------------
  239. //-----------------------------------------------------------------------------
  240. // Functions that converts one unicode codepoint at a time
  241. //-----------------------------------------------------------------------------
  242. UTF32 oneUTF8toUTF32( const UTF8* codepoint, U32 *unitsWalked)
  243. {
  244. PROFILE_SCOPE(oneUTF8toUTF32);
  245. // codepoints 6 codeunits long are read, but do not convert correctly,
  246. // and are filtered out anyway.
  247. // early out for ascii
  248. if(!(*codepoint & 0x0080))
  249. {
  250. if (unitsWalked != NULL)
  251. *unitsWalked = 1;
  252. return (UTF32)*codepoint;
  253. }
  254. U32 expectedByteCount;
  255. UTF32 ret = 0;
  256. U8 codeunit;
  257. // check the first byte ( a.k.a. codeunit ) .
  258. U8 c = codepoint[0];
  259. c = c >> 1;
  260. expectedByteCount = sgFirstByteLUT[c];
  261. if(expectedByteCount > 0) // 0 or negative is illegal to start with
  262. {
  263. // process 1st codeunit
  264. ret |= sgByteMask8LUT[expectedByteCount] & codepoint[0]; // bug?
  265. // process trailing codeunits
  266. for(U32 i=1;i<expectedByteCount; i++)
  267. {
  268. codeunit = codepoint[i];
  269. if( sgFirstByteLUT[codeunit>>1] == 0 )
  270. {
  271. ret <<= 6; // shift up 6
  272. ret |= (codeunit & 0x3f); // mask in the low 6 bits of this codeunit byte.
  273. }
  274. else
  275. {
  276. // found a bad codepoint - did not get a medial where we wanted one.
  277. // Dump the replacement, and claim to have parsed only 1 char,
  278. // so that we'll dump a slew of replacements, instead of eating the next char.
  279. ret = kReplacementChar;
  280. expectedByteCount = 1;
  281. break;
  282. }
  283. }
  284. }
  285. else
  286. {
  287. // found a bad codepoint - got a medial or an illegal codeunit.
  288. // Dump the replacement, and claim to have parsed only 1 char,
  289. // so that we'll dump a slew of replacements, instead of eating the next char.
  290. ret = kReplacementChar;
  291. expectedByteCount = 1;
  292. }
  293. if(unitsWalked != NULL)
  294. *unitsWalked = expectedByteCount;
  295. // codepoints in the surrogate range are illegal, and should be replaced.
  296. if(isSurrogateRange(ret))
  297. ret = kReplacementChar;
  298. // codepoints outside the Basic Multilingual Plane add complexity to our UTF16 string classes,
  299. // we've read them correctly so they won't foul the byte stream,
  300. // but we kill them here to make sure they wont foul anything else
  301. if(isAboveBMP(ret))
  302. ret = kReplacementChar;
  303. return ret;
  304. }
  305. //-----------------------------------------------------------------------------
  306. UTF32 oneUTF16toUTF32(const UTF16* codepoint, U32 *unitsWalked)
  307. {
  308. PROFILE_START(oneUTF16toUTF32);
  309. U8 expectedType;
  310. U32 unitCount;
  311. UTF32 ret = 0;
  312. UTF16 codeunit1,codeunit2;
  313. codeunit1 = codepoint[0];
  314. expectedType = sgSurrogateLUT[codeunit1 >> 10];
  315. switch(expectedType)
  316. {
  317. case 0: // simple
  318. ret = codeunit1;
  319. unitCount = 1;
  320. break;
  321. case 1: // 2 surrogates
  322. codeunit2 = codepoint[1];
  323. if( sgSurrogateLUT[codeunit2 >> 10] == 2)
  324. {
  325. ret = ((codeunit1 & sgByteMaskLow10 ) << 10) | (codeunit2 & sgByteMaskLow10);
  326. unitCount = 2;
  327. break;
  328. }
  329. // else, did not find a trailing surrogate where we expected one,
  330. // so fall through to the error
  331. case 2: // error
  332. // found a trailing surrogate where we expected a codepoint or leading surrogate.
  333. // Dump the replacement.
  334. ret = kReplacementChar;
  335. unitCount = 1;
  336. break;
  337. default:
  338. // unexpected return
  339. AssertFatal(false, "oneUTF16toUTF323: unexpected type");
  340. ret = kReplacementChar;
  341. unitCount = 1;
  342. break;
  343. }
  344. if(unitsWalked != NULL)
  345. *unitsWalked = unitCount;
  346. // codepoints in the surrogate range are illegal, and should be replaced.
  347. if(isSurrogateRange(ret))
  348. ret = kReplacementChar;
  349. // codepoints outside the Basic Multilingual Plane add complexity to our UTF16 string classes,
  350. // we've read them correctly so they wont foul the byte stream,
  351. // but we kill them here to make sure they wont foul anything else
  352. // NOTE: these are perfectly legal codepoints, we just dont want to deal with them.
  353. if(isAboveBMP(ret))
  354. ret = kReplacementChar;
  355. PROFILE_END();
  356. return ret;
  357. }
  358. //-----------------------------------------------------------------------------
  359. UTF16 oneUTF32toUTF16(const UTF32 codepoint)
  360. {
  361. // found a codepoint outside the encodable UTF-16 range!
  362. // or, found an illegal codepoint!
  363. if(codepoint >= 0x10FFFF || isSurrogateRange(codepoint))
  364. return kReplacementChar;
  365. // these are legal, we just don't want to deal with them.
  366. if(isAboveBMP(codepoint))
  367. return kReplacementChar;
  368. return (UTF16)codepoint;
  369. }
  370. //-----------------------------------------------------------------------------
  371. U32 oneUTF32toUTF8(const UTF32 codepoint, UTF8 *threeByteCodeunitBuf)
  372. {
  373. PROFILE_START(oneUTF32toUTF8);
  374. U32 bytecount = 0;
  375. UTF8 *buf;
  376. U32 working = codepoint;
  377. buf = threeByteCodeunitBuf;
  378. //-----------------
  379. if(isSurrogateRange(working)) // found an illegal codepoint!
  380. working = kReplacementChar;
  381. if(isAboveBMP(working)) // these are legal, we just dont want to deal with them.
  382. working = kReplacementChar;
  383. //-----------------
  384. if( working < (1 << 7)) // codeable in 7 bits
  385. bytecount = 1;
  386. else if( working < (1 << 11)) // codeable in 11 bits
  387. bytecount = 2;
  388. else if( working < (1 << 16)) // codeable in 16 bits
  389. bytecount = 3;
  390. AssertISV( bytecount > 0, "Error converting to UTF-8 in oneUTF32toUTF8(). isAboveBMP() should have caught this!");
  391. //-----------------
  392. U8 mask = sgByteMask8LUT[0]; // 0011 1111
  393. U8 marker = ( ~static_cast<U32>(mask) << 1u); // 1000 0000
  394. // Process the low order bytes, shifting the codepoint down 6 each pass.
  395. for( S32 i = bytecount-1; i > 0; i--)
  396. {
  397. threeByteCodeunitBuf[i] = marker | (working & mask);
  398. working >>= 6;
  399. }
  400. // Process the 1st byte. filter based on the # of expected bytes.
  401. mask = sgByteMask8LUT[bytecount];
  402. marker = ( ~mask << 1 );
  403. threeByteCodeunitBuf[0] = marker | (working & mask);
  404. PROFILE_END();
  405. return bytecount;
  406. }
  407. //-----------------------------------------------------------------------------
  408. U32 dStrlen(const UTF16 *unistring)
  409. {
  410. if(!unistring)
  411. return 0;
  412. U32 i = 0;
  413. while(unistring[i] != '\0')
  414. i++;
  415. // AssertFatal( wcslen(unistring) == i, "Incorrect length" );
  416. return i;
  417. }
  418. //-----------------------------------------------------------------------------
  419. U32 dStrlen(const UTF32 *unistring)
  420. {
  421. U32 i = 0;
  422. while(unistring[i] != '\0')
  423. i++;
  424. return i;
  425. }
  426. //-----------------------------------------------------------------------------
  427. const UTF16* dStrrchr(const UTF16* unistring, U32 c)
  428. {
  429. if(!unistring) return NULL;
  430. const UTF16* tmp = unistring + dStrlen(unistring);
  431. while( tmp >= unistring)
  432. {
  433. if(*tmp == c)
  434. return tmp;
  435. tmp--;
  436. }
  437. return NULL;
  438. }
  439. UTF16* dStrrchr(UTF16* unistring, U32 c)
  440. {
  441. const UTF16* str = unistring;
  442. return const_cast<UTF16*>(dStrrchr(str, c));
  443. }
  444. const UTF16* dStrchr(const UTF16* unistring, U32 c)
  445. {
  446. if(!unistring) return NULL;
  447. const UTF16* tmp = unistring;
  448. while ( *tmp && *tmp != c)
  449. tmp++;
  450. return (*tmp == c) ? tmp : NULL;
  451. }
  452. UTF16* dStrchr(UTF16* unistring, U32 c)
  453. {
  454. const UTF16* str = unistring;
  455. return const_cast<UTF16*>(dStrchr(str, c));
  456. }
  457. //-----------------------------------------------------------------------------
  458. const UTF8* getNthCodepoint(const UTF8 *unistring, const U32 n)
  459. {
  460. const UTF8* ret = unistring;
  461. U32 charsseen = 0;
  462. while( *ret && charsseen < n)
  463. {
  464. ret++;
  465. if((*ret & 0xC0) != 0x80)
  466. charsseen++;
  467. }
  468. return ret;
  469. }
  470. /* alternate utf-8 decode impl for speed, no error checking,
  471. left here for your amusement:
  472. U32 codeunit = codepoint + expectedByteCount - 1;
  473. U32 i = 0;
  474. switch(expectedByteCount)
  475. {
  476. case 6: ret |= ( *(codeunit--) & 0x3f ); i++;
  477. case 5: ret |= ( *(codeunit--) & 0x3f ) << (6 * i++);
  478. case 4: ret |= ( *(codeunit--) & 0x3f ) << (6 * i++);
  479. case 3: ret |= ( *(codeunit--) & 0x3f ) << (6 * i++);
  480. case 2: ret |= ( *(codeunit--) & 0x3f ) << (6 * i++);
  481. case 1: ret |= *(codeunit) & byteMask8LUT[expectedByteCount] << (6 * i);
  482. }
  483. */
  484. //------------------------------------------------------------------------------
  485. // Byte Order Mark functions
  486. bool chompUTF8BOM( const char *inString, char **outStringPtr )
  487. {
  488. *outStringPtr = const_cast<char *>( inString );
  489. bool valid = false;
  490. if (inString[0] && inString[1] && inString[2])
  491. {
  492. U8 bom[4];
  493. dMemcpy(bom, inString, 4);
  494. valid = isValidUTF8BOM(bom);
  495. }
  496. // This is hackey, but I am not sure the best way to do it at the present.
  497. // The only valid BOM is a UTF8 BOM, which is 3 bytes, even though we read
  498. // 4 bytes because it could possibly be a UTF32 BOM, and we want to provide
  499. // an accurate error message. Perhaps this could be re-worked when more UTF
  500. // formats are supported to have isValidBOM return the size of the BOM, in
  501. // bytes.
  502. if( valid )
  503. (*outStringPtr) += 3; // SEE ABOVE!! -pw
  504. return valid;
  505. }
  506. bool isValidUTF8BOM( U8 bom[4] )
  507. {
  508. // Is it a BOM?
  509. if( bom[0] == 0 )
  510. {
  511. // Could be UTF32BE
  512. if( bom[1] == 0 && bom[2] == 0xFE && bom[3] == 0xFF )
  513. {
  514. Con::warnf( "Encountered a UTF32 BE BOM in this file; Torque does NOT support this file encoding. Use UTF8!" );
  515. return false;
  516. }
  517. return false;
  518. }
  519. else if( bom[0] == 0xFF )
  520. {
  521. // It's little endian, either UTF16 or UTF32
  522. if( bom[1] == 0xFE )
  523. {
  524. if( bom[2] == 0 && bom[3] == 0 )
  525. Con::warnf( "Encountered a UTF32 LE BOM in this file; Torque does NOT support this file encoding. Use UTF8!" );
  526. else
  527. Con::warnf( "Encountered a UTF16 LE BOM in this file; Torque does NOT support this file encoding. Use UTF8!" );
  528. }
  529. return false;
  530. }
  531. else if( bom[0] == 0xFE && bom[1] == 0xFF )
  532. {
  533. Con::warnf( "Encountered a UTF16 BE BOM in this file; Torque does NOT support this file encoding. Use UTF8!" );
  534. return false;
  535. }
  536. else if( bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF )
  537. {
  538. // Can enable this if you want -pw
  539. //Con::printf("Encountered a UTF8 BOM. Torque supports this.");
  540. return true;
  541. }
  542. // Don't print out an error message here, because it will try this with
  543. // every script. -pw
  544. return false;
  545. }