2
0

unicode.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. void UTF16ClearCache()
  239. {
  240. sgUTF16Cache.clear();
  241. }
  242. //-----------------------------------------------------------------------------
  243. //-----------------------------------------------------------------------------
  244. // Functions that converts one unicode codepoint at a time
  245. //-----------------------------------------------------------------------------
  246. UTF32 oneUTF8toUTF32( const UTF8* codepoint, U32 *unitsWalked)
  247. {
  248. PROFILE_SCOPE(oneUTF8toUTF32);
  249. // codepoints 6 codeunits long are read, but do not convert correctly,
  250. // and are filtered out anyway.
  251. // early out for ascii
  252. if(!(*codepoint & 0x0080))
  253. {
  254. if (unitsWalked != NULL)
  255. *unitsWalked = 1;
  256. return (UTF32)*codepoint;
  257. }
  258. U32 expectedByteCount;
  259. UTF32 ret = 0;
  260. U8 codeunit;
  261. // check the first byte ( a.k.a. codeunit ) .
  262. U8 c = codepoint[0];
  263. c = c >> 1;
  264. expectedByteCount = sgFirstByteLUT[c];
  265. if(expectedByteCount > 0) // 0 or negative is illegal to start with
  266. {
  267. // process 1st codeunit
  268. ret |= sgByteMask8LUT[expectedByteCount] & codepoint[0]; // bug?
  269. // process trailing codeunits
  270. for(U32 i=1;i<expectedByteCount; i++)
  271. {
  272. codeunit = codepoint[i];
  273. if( sgFirstByteLUT[codeunit>>1] == 0 )
  274. {
  275. ret <<= 6; // shift up 6
  276. ret |= (codeunit & 0x3f); // mask in the low 6 bits of this codeunit byte.
  277. }
  278. else
  279. {
  280. // found a bad codepoint - did not get a medial where we wanted one.
  281. // Dump the replacement, and claim to have parsed only 1 char,
  282. // so that we'll dump a slew of replacements, instead of eating the next char.
  283. ret = kReplacementChar;
  284. expectedByteCount = 1;
  285. break;
  286. }
  287. }
  288. }
  289. else
  290. {
  291. // found a bad codepoint - got a medial or an illegal codeunit.
  292. // Dump the replacement, and claim to have parsed only 1 char,
  293. // so that we'll dump a slew of replacements, instead of eating the next char.
  294. ret = kReplacementChar;
  295. expectedByteCount = 1;
  296. }
  297. if(unitsWalked != NULL)
  298. *unitsWalked = expectedByteCount;
  299. // codepoints in the surrogate range are illegal, and should be replaced.
  300. if(isSurrogateRange(ret))
  301. ret = kReplacementChar;
  302. // codepoints outside the Basic Multilingual Plane add complexity to our UTF16 string classes,
  303. // we've read them correctly so they won't foul the byte stream,
  304. // but we kill them here to make sure they wont foul anything else
  305. if(isAboveBMP(ret))
  306. ret = kReplacementChar;
  307. return ret;
  308. }
  309. //-----------------------------------------------------------------------------
  310. UTF32 oneUTF16toUTF32(const UTF16* codepoint, U32 *unitsWalked)
  311. {
  312. PROFILE_START(oneUTF16toUTF32);
  313. U8 expectedType;
  314. U32 unitCount;
  315. UTF32 ret = 0;
  316. UTF16 codeunit1,codeunit2;
  317. codeunit1 = codepoint[0];
  318. expectedType = sgSurrogateLUT[codeunit1 >> 10];
  319. switch(expectedType)
  320. {
  321. case 0: // simple
  322. ret = codeunit1;
  323. unitCount = 1;
  324. break;
  325. case 1: // 2 surrogates
  326. codeunit2 = codepoint[1];
  327. if( sgSurrogateLUT[codeunit2 >> 10] == 2)
  328. {
  329. ret = ((codeunit1 & sgByteMaskLow10 ) << 10) | (codeunit2 & sgByteMaskLow10);
  330. unitCount = 2;
  331. break;
  332. }
  333. // else, did not find a trailing surrogate where we expected one,
  334. // so fall through to the error
  335. case 2: // error
  336. // found a trailing surrogate where we expected a codepoint or leading surrogate.
  337. // Dump the replacement.
  338. ret = kReplacementChar;
  339. unitCount = 1;
  340. break;
  341. default:
  342. // unexpected return
  343. AssertFatal(false, "oneUTF16toUTF323: unexpected type");
  344. ret = kReplacementChar;
  345. unitCount = 1;
  346. break;
  347. }
  348. if(unitsWalked != NULL)
  349. *unitsWalked = unitCount;
  350. // codepoints in the surrogate range are illegal, and should be replaced.
  351. if(isSurrogateRange(ret))
  352. ret = kReplacementChar;
  353. // codepoints outside the Basic Multilingual Plane add complexity to our UTF16 string classes,
  354. // we've read them correctly so they wont foul the byte stream,
  355. // but we kill them here to make sure they wont foul anything else
  356. // NOTE: these are perfectly legal codepoints, we just dont want to deal with them.
  357. if(isAboveBMP(ret))
  358. ret = kReplacementChar;
  359. PROFILE_END();
  360. return ret;
  361. }
  362. //-----------------------------------------------------------------------------
  363. UTF16 oneUTF32toUTF16(const UTF32 codepoint)
  364. {
  365. // found a codepoint outside the encodable UTF-16 range!
  366. // or, found an illegal codepoint!
  367. if(codepoint >= 0x10FFFF || isSurrogateRange(codepoint))
  368. return kReplacementChar;
  369. // these are legal, we just don't want to deal with them.
  370. if(isAboveBMP(codepoint))
  371. return kReplacementChar;
  372. return (UTF16)codepoint;
  373. }
  374. //-----------------------------------------------------------------------------
  375. U32 oneUTF32toUTF8(const UTF32 codepoint, UTF8 *threeByteCodeunitBuf)
  376. {
  377. PROFILE_START(oneUTF32toUTF8);
  378. U32 bytecount = 0;
  379. UTF8 *buf;
  380. U32 working = codepoint;
  381. buf = threeByteCodeunitBuf;
  382. //-----------------
  383. if(isSurrogateRange(working)) // found an illegal codepoint!
  384. working = kReplacementChar;
  385. if(isAboveBMP(working)) // these are legal, we just dont want to deal with them.
  386. working = kReplacementChar;
  387. //-----------------
  388. if( working < (1 << 7)) // codeable in 7 bits
  389. bytecount = 1;
  390. else if( working < (1 << 11)) // codeable in 11 bits
  391. bytecount = 2;
  392. else if( working < (1 << 16)) // codeable in 16 bits
  393. bytecount = 3;
  394. AssertISV( bytecount > 0, "Error converting to UTF-8 in oneUTF32toUTF8(). isAboveBMP() should have caught this!");
  395. //-----------------
  396. U8 mask = sgByteMask8LUT[0]; // 0011 1111
  397. U8 marker = ( ~static_cast<U32>(mask) << 1u); // 1000 0000
  398. // Process the low order bytes, shifting the codepoint down 6 each pass.
  399. for( S32 i = bytecount-1; i > 0; i--)
  400. {
  401. threeByteCodeunitBuf[i] = marker | (working & mask);
  402. working >>= 6;
  403. }
  404. // Process the 1st byte. filter based on the # of expected bytes.
  405. mask = sgByteMask8LUT[bytecount];
  406. marker = ( ~mask << 1 );
  407. threeByteCodeunitBuf[0] = marker | (working & mask);
  408. PROFILE_END();
  409. return bytecount;
  410. }
  411. //-----------------------------------------------------------------------------
  412. U32 dStrlen(const UTF16 *unistring)
  413. {
  414. if(!unistring)
  415. return 0;
  416. U32 i = 0;
  417. while(unistring[i] != '\0')
  418. i++;
  419. // AssertFatal( wcslen(unistring) == i, "Incorrect length" );
  420. return i;
  421. }
  422. //-----------------------------------------------------------------------------
  423. U32 dStrlen(const UTF32 *unistring)
  424. {
  425. U32 i = 0;
  426. while(unistring[i] != '\0')
  427. i++;
  428. return i;
  429. }
  430. //-----------------------------------------------------------------------------
  431. const UTF16* dStrrchr(const UTF16* unistring, U32 c)
  432. {
  433. if(!unistring) return NULL;
  434. const UTF16* tmp = unistring + dStrlen(unistring);
  435. while( tmp >= unistring)
  436. {
  437. if(*tmp == c)
  438. return tmp;
  439. tmp--;
  440. }
  441. return NULL;
  442. }
  443. UTF16* dStrrchr(UTF16* unistring, U32 c)
  444. {
  445. const UTF16* str = unistring;
  446. return const_cast<UTF16*>(dStrrchr(str, c));
  447. }
  448. const UTF16* dStrchr(const UTF16* unistring, U32 c)
  449. {
  450. if(!unistring) return NULL;
  451. const UTF16* tmp = unistring;
  452. while ( *tmp && *tmp != c)
  453. tmp++;
  454. return (*tmp == c) ? tmp : NULL;
  455. }
  456. UTF16* dStrchr(UTF16* unistring, U32 c)
  457. {
  458. const UTF16* str = unistring;
  459. return const_cast<UTF16*>(dStrchr(str, c));
  460. }
  461. //-----------------------------------------------------------------------------
  462. const UTF8* getNthCodepoint(const UTF8 *unistring, const U32 n)
  463. {
  464. const UTF8* ret = unistring;
  465. U32 charsseen = 0;
  466. while( *ret && charsseen < n)
  467. {
  468. ret++;
  469. if((*ret & 0xC0) != 0x80)
  470. charsseen++;
  471. }
  472. return ret;
  473. }
  474. /* alternate utf-8 decode impl for speed, no error checking,
  475. left here for your amusement:
  476. U32 codeunit = codepoint + expectedByteCount - 1;
  477. U32 i = 0;
  478. switch(expectedByteCount)
  479. {
  480. case 6: ret |= ( *(codeunit--) & 0x3f ); i++;
  481. case 5: ret |= ( *(codeunit--) & 0x3f ) << (6 * i++);
  482. case 4: ret |= ( *(codeunit--) & 0x3f ) << (6 * i++);
  483. case 3: ret |= ( *(codeunit--) & 0x3f ) << (6 * i++);
  484. case 2: ret |= ( *(codeunit--) & 0x3f ) << (6 * i++);
  485. case 1: ret |= *(codeunit) & byteMask8LUT[expectedByteCount] << (6 * i);
  486. }
  487. */
  488. //------------------------------------------------------------------------------
  489. // Byte Order Mark functions
  490. bool chompUTF8BOM( const char *inString, char **outStringPtr )
  491. {
  492. *outStringPtr = const_cast<char *>( inString );
  493. bool valid = false;
  494. if (inString[0] && inString[1] && inString[2])
  495. {
  496. U8 bom[4];
  497. dMemcpy(bom, inString, 4);
  498. valid = isValidUTF8BOM(bom);
  499. }
  500. // This is hackey, but I am not sure the best way to do it at the present.
  501. // The only valid BOM is a UTF8 BOM, which is 3 bytes, even though we read
  502. // 4 bytes because it could possibly be a UTF32 BOM, and we want to provide
  503. // an accurate error message. Perhaps this could be re-worked when more UTF
  504. // formats are supported to have isValidBOM return the size of the BOM, in
  505. // bytes.
  506. if( valid )
  507. (*outStringPtr) += 3; // SEE ABOVE!! -pw
  508. return valid;
  509. }
  510. bool isValidUTF8BOM( U8 bom[4] )
  511. {
  512. // Is it a BOM?
  513. if( bom[0] == 0 )
  514. {
  515. // Could be UTF32BE
  516. if( bom[1] == 0 && bom[2] == 0xFE && bom[3] == 0xFF )
  517. {
  518. Con::warnf( "Encountered a UTF32 BE BOM in this file; Torque does NOT support this file encoding. Use UTF8!" );
  519. return false;
  520. }
  521. return false;
  522. }
  523. else if( bom[0] == 0xFF )
  524. {
  525. // It's little endian, either UTF16 or UTF32
  526. if( bom[1] == 0xFE )
  527. {
  528. if( bom[2] == 0 && bom[3] == 0 )
  529. Con::warnf( "Encountered a UTF32 LE BOM in this file; Torque does NOT support this file encoding. Use UTF8!" );
  530. else
  531. Con::warnf( "Encountered a UTF16 LE BOM in this file; Torque does NOT support this file encoding. Use UTF8!" );
  532. }
  533. return false;
  534. }
  535. else if( bom[0] == 0xFE && bom[1] == 0xFF )
  536. {
  537. Con::warnf( "Encountered a UTF16 BE BOM in this file; Torque does NOT support this file encoding. Use UTF8!" );
  538. return false;
  539. }
  540. else if( bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF )
  541. {
  542. // Can enable this if you want -pw
  543. //Con::printf("Encountered a UTF8 BOM. Torque supports this.");
  544. return true;
  545. }
  546. // Don't print out an error message here, because it will try this with
  547. // every script. -pw
  548. return false;
  549. }