unicode.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. void operator =(const UTF16Cache &other)
  89. {
  90. delete [] mString;
  91. mLength = other.mLength;
  92. mString = new UTF16[mLength];
  93. dMemcpy(mString, other.mString, mLength * sizeof(UTF16));
  94. }
  95. ~UTF16Cache()
  96. {
  97. delete [] mString;
  98. }
  99. void copyToBuffer(UTF16 *outBuffer, U32 lenToCopy, bool nullTerminate = true) const
  100. {
  101. U32 copy = getMin(mLength, lenToCopy);
  102. if(mString && copy > 0)
  103. dMemcpy(outBuffer, mString, copy * sizeof(UTF16));
  104. if(nullTerminate)
  105. outBuffer[copy] = 0;
  106. }
  107. };
  108. /// Cache for UTF16 strings
  109. typedef HashTable<U32, UTF16Cache> UTF16CacheTable;
  110. static UTF16CacheTable sgUTF16Cache;
  111. #endif // TORQUE_ENABLE_UTF16_CACHE
  112. //-----------------------------------------------------------------------------
  113. inline bool isSurrogateRange(U32 codepoint)
  114. {
  115. return ( 0xd800 < codepoint && codepoint < 0xdfff );
  116. }
  117. inline bool isAboveBMP(U32 codepoint)
  118. {
  119. return ( codepoint > 0xFFFF );
  120. }
  121. //-----------------------------------------------------------------------------
  122. U32 convertUTF8toUTF16N(const UTF8 *unistring, UTF16 *outbuffer, U32 len)
  123. {
  124. AssertFatal(len >= 1, "Buffer for unicode conversion must be large enough to hold at least the null terminator.");
  125. PROFILE_SCOPE(convertUTF8toUTF16);
  126. #ifdef TORQUE_ENABLE_UTF16_CACHE
  127. // If we have cached this conversion already, don't do it again
  128. U32 hashKey = Torque::hash((const U8 *)unistring, dStrlen(unistring), 0);
  129. UTF16CacheTable::Iterator cacheItr = sgUTF16Cache.find(hashKey);
  130. if(cacheItr != sgUTF16Cache.end())
  131. {
  132. const UTF16Cache &cache = (*cacheItr).value;
  133. cache.copyToBuffer(outbuffer, len);
  134. return getMin(cache.mLength,len - 1);
  135. }
  136. #endif
  137. U32 walked, nCodepoints;
  138. UTF32 middleman;
  139. nCodepoints=0;
  140. while(*unistring != '\0' && nCodepoints < len)
  141. {
  142. walked = 1;
  143. middleman = oneUTF8toUTF32(unistring,&walked);
  144. outbuffer[nCodepoints] = oneUTF32toUTF16(middleman);
  145. unistring+=walked;
  146. nCodepoints++;
  147. }
  148. nCodepoints = getMin(nCodepoints,len - 1);
  149. outbuffer[nCodepoints] = '\0';
  150. #ifdef TORQUE_ENABLE_UTF16_CACHE
  151. // Cache the results.
  152. // FIXME As written, this will result in some unnecessary memory copying due to copy constructor calls.
  153. UTF16Cache cache(outbuffer, nCodepoints);
  154. sgUTF16Cache.insertUnique(hashKey, cache);
  155. #endif
  156. return nCodepoints;
  157. }
  158. //-----------------------------------------------------------------------------
  159. U32 convertUTF16toUTF8N( const UTF16 *unistring, UTF8 *outbuffer, U32 len)
  160. {
  161. AssertFatal(len >= 1, "Buffer for unicode conversion must be large enough to hold at least the null terminator.");
  162. PROFILE_START(convertUTF16toUTF8);
  163. U32 walked, nCodeunits, codeunitLen;
  164. UTF32 middleman;
  165. nCodeunits=0;
  166. while( *unistring != '\0' && nCodeunits + 3 < len )
  167. {
  168. walked = 1;
  169. middleman = oneUTF16toUTF32(unistring,&walked);
  170. codeunitLen = oneUTF32toUTF8(middleman, &outbuffer[nCodeunits]);
  171. unistring += walked;
  172. nCodeunits += codeunitLen;
  173. }
  174. nCodeunits = getMin(nCodeunits,len - 1);
  175. outbuffer[nCodeunits] = '\0';
  176. PROFILE_END();
  177. return nCodeunits;
  178. }
  179. U32 convertUTF16toUTF8DoubleNULL( const UTF16 *unistring, UTF8 *outbuffer, U32 len)
  180. {
  181. AssertFatal(len >= 1, "Buffer for unicode conversion must be large enough to hold at least the null terminator.");
  182. PROFILE_START(convertUTF16toUTF8DoubleNULL);
  183. U32 walked, nCodeunits, codeunitLen;
  184. UTF32 middleman;
  185. nCodeunits=0;
  186. while( ! (*unistring == '\0' && *(unistring + 1) == '\0') && nCodeunits + 3 < len )
  187. {
  188. walked = 1;
  189. middleman = oneUTF16toUTF32(unistring,&walked);
  190. codeunitLen = oneUTF32toUTF8(middleman, &outbuffer[nCodeunits]);
  191. unistring += walked;
  192. nCodeunits += codeunitLen;
  193. }
  194. nCodeunits = getMin(nCodeunits,len - 1);
  195. outbuffer[nCodeunits] = NULL;
  196. outbuffer[nCodeunits+1] = NULL;
  197. PROFILE_END();
  198. return nCodeunits;
  199. }
  200. //-----------------------------------------------------------------------------
  201. // Functions that convert buffers of unicode code points
  202. //-----------------------------------------------------------------------------
  203. UTF16* createUTF16string( const UTF8* unistring)
  204. {
  205. PROFILE_SCOPE(convertUTF8toUTF16_create);
  206. // allocate plenty of memory.
  207. U32 nCodepoints, len = dStrlen(unistring) + 1;
  208. FrameTemp<UTF16> buf(len);
  209. // perform conversion
  210. nCodepoints = convertUTF8toUTF16N( unistring, buf, len);
  211. // add 1 for the NULL terminator the converter promises it included.
  212. nCodepoints++;
  213. // allocate the return buffer, copy over, and return it.
  214. UTF16 *ret = new UTF16[nCodepoints];
  215. dMemcpy(ret, buf, nCodepoints * sizeof(UTF16));
  216. return ret;
  217. }
  218. //-----------------------------------------------------------------------------
  219. UTF8* createUTF8string( const UTF16* unistring)
  220. {
  221. PROFILE_SCOPE(convertUTF16toUTF8_create);
  222. // allocate plenty of memory.
  223. U32 nCodeunits, len = dStrlen(unistring) * 3 + 1;
  224. FrameTemp<UTF8> buf(len);
  225. // perform conversion
  226. nCodeunits = convertUTF16toUTF8N( unistring, buf, len);
  227. // add 1 for the NULL terminator the converter promises it included.
  228. nCodeunits++;
  229. // allocate the return buffer, copy over, and return it.
  230. UTF8 *ret = new UTF8[nCodeunits];
  231. dMemcpy(ret, buf, nCodeunits * sizeof(UTF8));
  232. return ret;
  233. }
  234. //-----------------------------------------------------------------------------
  235. //-----------------------------------------------------------------------------
  236. // Functions that converts one unicode codepoint at a time
  237. //-----------------------------------------------------------------------------
  238. UTF32 oneUTF8toUTF32( const UTF8* codepoint, U32 *unitsWalked)
  239. {
  240. PROFILE_SCOPE(oneUTF8toUTF32);
  241. // codepoints 6 codeunits long are read, but do not convert correctly,
  242. // and are filtered out anyway.
  243. // early out for ascii
  244. if(!(*codepoint & 0x0080))
  245. {
  246. if (unitsWalked != NULL)
  247. *unitsWalked = 1;
  248. return (UTF32)*codepoint;
  249. }
  250. U32 expectedByteCount;
  251. UTF32 ret = 0;
  252. U8 codeunit;
  253. // check the first byte ( a.k.a. codeunit ) .
  254. U8 c = codepoint[0];
  255. c = c >> 1;
  256. expectedByteCount = sgFirstByteLUT[c];
  257. if(expectedByteCount > 0) // 0 or negative is illegal to start with
  258. {
  259. // process 1st codeunit
  260. ret |= sgByteMask8LUT[expectedByteCount] & codepoint[0]; // bug?
  261. // process trailing codeunits
  262. for(U32 i=1;i<expectedByteCount; i++)
  263. {
  264. codeunit = codepoint[i];
  265. if( sgFirstByteLUT[codeunit>>1] == 0 )
  266. {
  267. ret <<= 6; // shift up 6
  268. ret |= (codeunit & 0x3f); // mask in the low 6 bits of this codeunit byte.
  269. }
  270. else
  271. {
  272. // found a bad codepoint - did not get a medial where we wanted one.
  273. // Dump the replacement, and claim to have parsed only 1 char,
  274. // so that we'll dump a slew of replacements, instead of eating the next char.
  275. ret = kReplacementChar;
  276. expectedByteCount = 1;
  277. break;
  278. }
  279. }
  280. }
  281. else
  282. {
  283. // found a bad codepoint - got a medial or an illegal codeunit.
  284. // Dump the replacement, and claim to have parsed only 1 char,
  285. // so that we'll dump a slew of replacements, instead of eating the next char.
  286. ret = kReplacementChar;
  287. expectedByteCount = 1;
  288. }
  289. if(unitsWalked != NULL)
  290. *unitsWalked = expectedByteCount;
  291. // codepoints in the surrogate range are illegal, and should be replaced.
  292. if(isSurrogateRange(ret))
  293. ret = kReplacementChar;
  294. // codepoints outside the Basic Multilingual Plane add complexity to our UTF16 string classes,
  295. // we've read them correctly so they won't foul the byte stream,
  296. // but we kill them here to make sure they wont foul anything else
  297. if(isAboveBMP(ret))
  298. ret = kReplacementChar;
  299. return ret;
  300. }
  301. //-----------------------------------------------------------------------------
  302. UTF32 oneUTF16toUTF32(const UTF16* codepoint, U32 *unitsWalked)
  303. {
  304. PROFILE_START(oneUTF16toUTF32);
  305. U8 expectedType;
  306. U32 unitCount;
  307. UTF32 ret = 0;
  308. UTF16 codeunit1,codeunit2;
  309. codeunit1 = codepoint[0];
  310. expectedType = sgSurrogateLUT[codeunit1 >> 10];
  311. switch(expectedType)
  312. {
  313. case 0: // simple
  314. ret = codeunit1;
  315. unitCount = 1;
  316. break;
  317. case 1: // 2 surrogates
  318. codeunit2 = codepoint[1];
  319. if( sgSurrogateLUT[codeunit2 >> 10] == 2)
  320. {
  321. ret = ((codeunit1 & sgByteMaskLow10 ) << 10) | (codeunit2 & sgByteMaskLow10);
  322. unitCount = 2;
  323. break;
  324. }
  325. // else, did not find a trailing surrogate where we expected one,
  326. // so fall through to the error
  327. case 2: // error
  328. // found a trailing surrogate where we expected a codepoint or leading surrogate.
  329. // Dump the replacement.
  330. ret = kReplacementChar;
  331. unitCount = 1;
  332. break;
  333. default:
  334. // unexpected return
  335. AssertFatal(false, "oneUTF16toUTF323: unexpected type");
  336. ret = kReplacementChar;
  337. unitCount = 1;
  338. break;
  339. }
  340. if(unitsWalked != NULL)
  341. *unitsWalked = unitCount;
  342. // codepoints in the surrogate range are illegal, and should be replaced.
  343. if(isSurrogateRange(ret))
  344. ret = kReplacementChar;
  345. // codepoints outside the Basic Multilingual Plane add complexity to our UTF16 string classes,
  346. // we've read them correctly so they wont foul the byte stream,
  347. // but we kill them here to make sure they wont foul anything else
  348. // NOTE: these are perfectly legal codepoints, we just dont want to deal with them.
  349. if(isAboveBMP(ret))
  350. ret = kReplacementChar;
  351. PROFILE_END();
  352. return ret;
  353. }
  354. //-----------------------------------------------------------------------------
  355. UTF16 oneUTF32toUTF16(const UTF32 codepoint)
  356. {
  357. // found a codepoint outside the encodable UTF-16 range!
  358. // or, found an illegal codepoint!
  359. if(codepoint >= 0x10FFFF || isSurrogateRange(codepoint))
  360. return kReplacementChar;
  361. // these are legal, we just don't want to deal with them.
  362. if(isAboveBMP(codepoint))
  363. return kReplacementChar;
  364. return (UTF16)codepoint;
  365. }
  366. //-----------------------------------------------------------------------------
  367. U32 oneUTF32toUTF8(const UTF32 codepoint, UTF8 *threeByteCodeunitBuf)
  368. {
  369. PROFILE_START(oneUTF32toUTF8);
  370. U32 bytecount = 0;
  371. UTF8 *buf;
  372. U32 working = codepoint;
  373. buf = threeByteCodeunitBuf;
  374. //-----------------
  375. if(isSurrogateRange(working)) // found an illegal codepoint!
  376. working = kReplacementChar;
  377. if(isAboveBMP(working)) // these are legal, we just dont want to deal with them.
  378. working = kReplacementChar;
  379. //-----------------
  380. if( working < (1 << 7)) // codeable in 7 bits
  381. bytecount = 1;
  382. else if( working < (1 << 11)) // codeable in 11 bits
  383. bytecount = 2;
  384. else if( working < (1 << 16)) // codeable in 16 bits
  385. bytecount = 3;
  386. AssertISV( bytecount > 0, "Error converting to UTF-8 in oneUTF32toUTF8(). isAboveBMP() should have caught this!");
  387. //-----------------
  388. U8 mask = sgByteMask8LUT[0]; // 0011 1111
  389. U8 marker = ( ~mask << 1); // 1000 0000
  390. // Process the low order bytes, shifting the codepoint down 6 each pass.
  391. for( S32 i = bytecount-1; i > 0; i--)
  392. {
  393. threeByteCodeunitBuf[i] = marker | (working & mask);
  394. working >>= 6;
  395. }
  396. // Process the 1st byte. filter based on the # of expected bytes.
  397. mask = sgByteMask8LUT[bytecount];
  398. marker = ( ~mask << 1 );
  399. threeByteCodeunitBuf[0] = marker | working & mask;
  400. PROFILE_END();
  401. return bytecount;
  402. }
  403. //-----------------------------------------------------------------------------
  404. U32 dStrlen(const UTF16 *unistring)
  405. {
  406. if(!unistring)
  407. return 0;
  408. U32 i = 0;
  409. while(unistring[i] != '\0')
  410. i++;
  411. // AssertFatal( wcslen(unistring) == i, "Incorrect length" );
  412. return i;
  413. }
  414. //-----------------------------------------------------------------------------
  415. U32 dStrlen(const UTF32 *unistring)
  416. {
  417. U32 i = 0;
  418. while(unistring[i] != '\0')
  419. i++;
  420. return i;
  421. }
  422. //-----------------------------------------------------------------------------
  423. const UTF16* dStrrchr(const UTF16* unistring, U32 c)
  424. {
  425. if(!unistring) return NULL;
  426. const UTF16* tmp = unistring + dStrlen(unistring);
  427. while( tmp >= unistring)
  428. {
  429. if(*tmp == c)
  430. return tmp;
  431. tmp--;
  432. }
  433. return NULL;
  434. }
  435. UTF16* dStrrchr(UTF16* unistring, U32 c)
  436. {
  437. const UTF16* str = unistring;
  438. return const_cast<UTF16*>(dStrrchr(str, c));
  439. }
  440. const UTF16* dStrchr(const UTF16* unistring, U32 c)
  441. {
  442. if(!unistring) return NULL;
  443. const UTF16* tmp = unistring;
  444. while ( *tmp && *tmp != c)
  445. tmp++;
  446. return (*tmp == c) ? tmp : NULL;
  447. }
  448. UTF16* dStrchr(UTF16* unistring, U32 c)
  449. {
  450. const UTF16* str = unistring;
  451. return const_cast<UTF16*>(dStrchr(str, c));
  452. }
  453. //-----------------------------------------------------------------------------
  454. const UTF8* getNthCodepoint(const UTF8 *unistring, const U32 n)
  455. {
  456. const UTF8* ret = unistring;
  457. U32 charsseen = 0;
  458. while( *ret && charsseen < n)
  459. {
  460. ret++;
  461. if((*ret & 0xC0) != 0x80)
  462. charsseen++;
  463. }
  464. return ret;
  465. }
  466. /* alternate utf-8 decode impl for speed, no error checking,
  467. left here for your amusement:
  468. U32 codeunit = codepoint + expectedByteCount - 1;
  469. U32 i = 0;
  470. switch(expectedByteCount)
  471. {
  472. case 6: ret |= ( *(codeunit--) & 0x3f ); i++;
  473. case 5: ret |= ( *(codeunit--) & 0x3f ) << (6 * i++);
  474. case 4: ret |= ( *(codeunit--) & 0x3f ) << (6 * i++);
  475. case 3: ret |= ( *(codeunit--) & 0x3f ) << (6 * i++);
  476. case 2: ret |= ( *(codeunit--) & 0x3f ) << (6 * i++);
  477. case 1: ret |= *(codeunit) & byteMask8LUT[expectedByteCount] << (6 * i);
  478. }
  479. */
  480. //------------------------------------------------------------------------------
  481. // Byte Order Mark functions
  482. bool chompUTF8BOM( const char *inString, char **outStringPtr )
  483. {
  484. *outStringPtr = const_cast<char *>( inString );
  485. bool valid = false;
  486. if (inString[0] && inString[1] && inString[2])
  487. {
  488. U8 bom[4];
  489. dMemcpy(bom, inString, 4);
  490. valid = isValidUTF8BOM(bom);
  491. }
  492. // This is hackey, but I am not sure the best way to do it at the present.
  493. // The only valid BOM is a UTF8 BOM, which is 3 bytes, even though we read
  494. // 4 bytes because it could possibly be a UTF32 BOM, and we want to provide
  495. // an accurate error message. Perhaps this could be re-worked when more UTF
  496. // formats are supported to have isValidBOM return the size of the BOM, in
  497. // bytes.
  498. if( valid )
  499. (*outStringPtr) += 3; // SEE ABOVE!! -pw
  500. return valid;
  501. }
  502. bool isValidUTF8BOM( U8 bom[4] )
  503. {
  504. // Is it a BOM?
  505. if( bom[0] == 0 )
  506. {
  507. // Could be UTF32BE
  508. if( bom[1] == 0 && bom[2] == 0xFE && bom[3] == 0xFF )
  509. {
  510. Con::warnf( "Encountered a UTF32 BE BOM in this file; Torque does NOT support this file encoding. Use UTF8!" );
  511. return false;
  512. }
  513. return false;
  514. }
  515. else if( bom[0] == 0xFF )
  516. {
  517. // It's little endian, either UTF16 or UTF32
  518. if( bom[1] == 0xFE )
  519. {
  520. if( bom[2] == 0 && bom[3] == 0 )
  521. Con::warnf( "Encountered a UTF32 LE BOM in this file; Torque does NOT support this file encoding. Use UTF8!" );
  522. else
  523. Con::warnf( "Encountered a UTF16 LE BOM in this file; Torque does NOT support this file encoding. Use UTF8!" );
  524. }
  525. return false;
  526. }
  527. else if( bom[0] == 0xFE && bom[1] == 0xFF )
  528. {
  529. Con::warnf( "Encountered a UTF16 BE BOM in this file; Torque does NOT support this file encoding. Use UTF8!" );
  530. return false;
  531. }
  532. else if( bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF )
  533. {
  534. // Can enable this if you want -pw
  535. //Con::printf("Encountered a UTF8 BOM. Torque supports this.");
  536. return true;
  537. }
  538. // Don't print out an error message here, because it will try this with
  539. // every script. -pw
  540. return false;
  541. }