UI for Zipcoin Blue

mbcs.js 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. var util = require('util'),
  2. Match = require ('../match');
  3. /**
  4. * Binary search implementation (recursive)
  5. */
  6. function binarySearch(arr, searchValue) {
  7. function find(arr, searchValue, left, right) {
  8. if (right < left)
  9. return -1;
  10. /*
  11. int mid = mid = (left + right) / 2;
  12. There is a bug in the above line;
  13. Joshua Bloch suggests the following replacement:
  14. */
  15. var mid = Math.floor((left + right) >>> 1);
  16. if (searchValue > arr[mid])
  17. return find(arr, searchValue, mid + 1, right);
  18. if (searchValue < arr[mid])
  19. return find(arr, searchValue, left, mid - 1);
  20. return mid;
  21. };
  22. return find(arr, searchValue, 0, arr.length - 1);
  23. };
  24. // 'Character' iterated character class.
  25. // Recognizers for specific mbcs encodings make their 'characters' available
  26. // by providing a nextChar() function that fills in an instance of iteratedChar
  27. // with the next char from the input.
  28. // The returned characters are not converted to Unicode, but remain as the raw
  29. // bytes (concatenated into an int) from the codepage data.
  30. //
  31. // For Asian charsets, use the raw input rather than the input that has been
  32. // stripped of markup. Detection only considers multi-byte chars, effectively
  33. // stripping markup anyway, and double byte chars do occur in markup too.
  34. //
  35. function IteratedChar() {
  36. this.charValue = 0; // 1-4 bytes from the raw input data
  37. this.index = 0;
  38. this.nextIndex = 0;
  39. this.error = false;
  40. this.done = false;
  41. this.reset = function() {
  42. this.charValue = 0;
  43. this.index = -1;
  44. this.nextIndex = 0;
  45. this.error = false;
  46. this.done = false;
  47. };
  48. this.nextByte = function(det) {
  49. if (this.nextIndex >= det.fRawLength) {
  50. this.done = true;
  51. return -1;
  52. }
  53. var byteValue = det.fRawInput[this.nextIndex++] & 0x00ff;
  54. return byteValue;
  55. };
  56. };
  57. /**
  58. * Asian double or multi-byte - charsets.
  59. * Match is determined mostly by the input data adhering to the
  60. * encoding scheme for the charset, and, optionally,
  61. * frequency-of-occurence of characters.
  62. */
  63. function mbcs() {};
  64. /**
  65. * Test the match of this charset with the input text data
  66. * which is obtained via the CharsetDetector object.
  67. *
  68. * @param det The CharsetDetector, which contains the input text
  69. * to be checked for being in this charset.
  70. * @return Two values packed into one int (Damn java, anyhow)
  71. * bits 0-7: the match confidence, ranging from 0-100
  72. * bits 8-15: The match reason, an enum-like value.
  73. */
  74. mbcs.prototype.match = function(det) {
  75. var singleByteCharCount = 0, //TODO Do we really need this?
  76. doubleByteCharCount = 0,
  77. commonCharCount = 0,
  78. badCharCount = 0,
  79. totalCharCount = 0,
  80. confidence = 0;
  81. var iter = new IteratedChar();
  82. detectBlock: {
  83. for (iter.reset(); this.nextChar(iter, det);) {
  84. totalCharCount++;
  85. if (iter.error) {
  86. badCharCount++;
  87. } else {
  88. var cv = iter.charValue & 0xFFFFFFFF;
  89. if (cv <= 0xff) {
  90. singleByteCharCount++;
  91. } else {
  92. doubleByteCharCount++;
  93. if (this.commonChars != null) {
  94. // NOTE: This assumes that there are no 4-byte common chars.
  95. if (binarySearch(this.commonChars, cv) >= 0) {
  96. commonCharCount++;
  97. }
  98. }
  99. }
  100. }
  101. if (badCharCount >= 2 && badCharCount * 5 >= doubleByteCharCount) {
  102. // console.log('its here!')
  103. // Bail out early if the byte data is not matching the encoding scheme.
  104. break detectBlock;
  105. }
  106. }
  107. if (doubleByteCharCount <= 10 && badCharCount== 0) {
  108. // Not many multi-byte chars.
  109. if (doubleByteCharCount == 0 && totalCharCount < 10) {
  110. // There weren't any multibyte sequences, and there was a low density of non-ASCII single bytes.
  111. // We don't have enough data to have any confidence.
  112. // Statistical analysis of single byte non-ASCII charcters would probably help here.
  113. confidence = 0;
  114. }
  115. else {
  116. // ASCII or ISO file? It's probably not our encoding,
  117. // but is not incompatible with our encoding, so don't give it a zero.
  118. confidence = 10;
  119. }
  120. break detectBlock;
  121. }
  122. //
  123. // No match if there are too many characters that don't fit the encoding scheme.
  124. // (should we have zero tolerance for these?)
  125. //
  126. if (doubleByteCharCount < 20 * badCharCount) {
  127. confidence = 0;
  128. break detectBlock;
  129. }
  130. if (this.commonChars == null) {
  131. // We have no statistics on frequently occuring characters.
  132. // Assess confidence purely on having a reasonable number of
  133. // multi-byte characters (the more the better
  134. confidence = 30 + doubleByteCharCount - 20 * badCharCount;
  135. if (confidence > 100) {
  136. confidence = 100;
  137. }
  138. } else {
  139. //
  140. // Frequency of occurence statistics exist.
  141. //
  142. var maxVal = Math.log(parseFloat(doubleByteCharCount) / 4);
  143. var scaleFactor = 90.0 / maxVal;
  144. confidence = Math.floor(Math.log(commonCharCount + 1) * scaleFactor + 10);
  145. confidence = Math.min(confidence, 100);
  146. }
  147. } // end of detectBlock:
  148. return confidence == 0 ? null : new Match(det, this, confidence);
  149. };
  150. /**
  151. * Get the next character (however many bytes it is) from the input data
  152. * Subclasses for specific charset encodings must implement this function
  153. * to get characters according to the rules of their encoding scheme.
  154. *
  155. * This function is not a method of class iteratedChar only because
  156. * that would require a lot of extra derived classes, which is awkward.
  157. * @param it The iteratedChar 'struct' into which the returned char is placed.
  158. * @param det The charset detector, which is needed to get at the input byte data
  159. * being iterated over.
  160. * @return True if a character was returned, false at end of input.
  161. */
  162. mbcs.prototype.nextChar = function(iter, det) {};
  163. /**
  164. * Shift-JIS charset recognizer.
  165. */
  166. module.exports.sjis = function() {
  167. this.name = function() {
  168. return 'Shift-JIS';
  169. };
  170. this.language = function() {
  171. return 'ja';
  172. };
  173. // TODO: This set of data comes from the character frequency-
  174. // of-occurence analysis tool. The data needs to be moved
  175. // into a resource and loaded from there.
  176. this.commonChars = [
  177. 0x8140, 0x8141, 0x8142, 0x8145, 0x815b, 0x8169, 0x816a, 0x8175, 0x8176, 0x82a0,
  178. 0x82a2, 0x82a4, 0x82a9, 0x82aa, 0x82ab, 0x82ad, 0x82af, 0x82b1, 0x82b3, 0x82b5,
  179. 0x82b7, 0x82bd, 0x82be, 0x82c1, 0x82c4, 0x82c5, 0x82c6, 0x82c8, 0x82c9, 0x82cc,
  180. 0x82cd, 0x82dc, 0x82e0, 0x82e7, 0x82e8, 0x82e9, 0x82ea, 0x82f0, 0x82f1, 0x8341,
  181. 0x8343, 0x834e, 0x834f, 0x8358, 0x835e, 0x8362, 0x8367, 0x8375, 0x8376, 0x8389,
  182. 0x838a, 0x838b, 0x838d, 0x8393, 0x8e96, 0x93fa, 0x95aa
  183. ];
  184. this.nextChar = function(iter, det) {
  185. iter.index = iter.nextIndex;
  186. iter.error = false;
  187. var firstByte;
  188. firstByte = iter.charValue = iter.nextByte(det);
  189. if (firstByte < 0)
  190. return false;
  191. if (firstByte <= 0x7f || (firstByte > 0xa0 && firstByte <= 0xdf))
  192. return true;
  193. var secondByte = iter.nextByte(det);
  194. if (secondByte < 0)
  195. return false;
  196. iter.charValue = (firstByte << 8) | secondByte;
  197. if (! ((secondByte >= 0x40 && secondByte <= 0x7f) || (secondByte >= 0x80 && secondByte <= 0xff))) {
  198. // Illegal second byte value.
  199. iter.error = true;
  200. }
  201. return true;
  202. };
  203. };
  204. util.inherits(module.exports.sjis, mbcs);
  205. /**
  206. * Big5 charset recognizer.
  207. */
  208. module.exports.big5 = function() {
  209. this.name = function() {
  210. return 'Big5';
  211. };
  212. this.language = function() {
  213. return 'zh';
  214. };
  215. // TODO: This set of data comes from the character frequency-
  216. // of-occurence analysis tool. The data needs to be moved
  217. // into a resource and loaded from there.
  218. this.commonChars = [
  219. 0xa140, 0xa141, 0xa142, 0xa143, 0xa147, 0xa149, 0xa175, 0xa176, 0xa440, 0xa446,
  220. 0xa447, 0xa448, 0xa451, 0xa454, 0xa457, 0xa464, 0xa46a, 0xa46c, 0xa477, 0xa4a3,
  221. 0xa4a4, 0xa4a7, 0xa4c1, 0xa4ce, 0xa4d1, 0xa4df, 0xa4e8, 0xa4fd, 0xa540, 0xa548,
  222. 0xa558, 0xa569, 0xa5cd, 0xa5e7, 0xa657, 0xa661, 0xa662, 0xa668, 0xa670, 0xa6a8,
  223. 0xa6b3, 0xa6b9, 0xa6d3, 0xa6db, 0xa6e6, 0xa6f2, 0xa740, 0xa751, 0xa759, 0xa7da,
  224. 0xa8a3, 0xa8a5, 0xa8ad, 0xa8d1, 0xa8d3, 0xa8e4, 0xa8fc, 0xa9c0, 0xa9d2, 0xa9f3,
  225. 0xaa6b, 0xaaba, 0xaabe, 0xaacc, 0xaafc, 0xac47, 0xac4f, 0xacb0, 0xacd2, 0xad59,
  226. 0xaec9, 0xafe0, 0xb0ea, 0xb16f, 0xb2b3, 0xb2c4, 0xb36f, 0xb44c, 0xb44e, 0xb54c,
  227. 0xb5a5, 0xb5bd, 0xb5d0, 0xb5d8, 0xb671, 0xb7ed, 0xb867, 0xb944, 0xbad8, 0xbb44,
  228. 0xbba1, 0xbdd1, 0xc2c4, 0xc3b9, 0xc440, 0xc45f
  229. ];
  230. this.nextChar = function(iter, det) {
  231. iter.index = iter.nextIndex;
  232. iter.error = false;
  233. var firstByte = iter.charValue = iter.nextByte(det);
  234. if (firstByte < 0)
  235. return false;
  236. // single byte character.
  237. if (firstByte <= 0x7f || firstByte == 0xff)
  238. return true;
  239. var secondByte = iter.nextByte(det);
  240. if (secondByte < 0)
  241. return false;
  242. iter.charValue = (iter.charValue << 8) | secondByte;
  243. if (secondByte < 0x40 || secondByte == 0x7f || secondByte == 0xff)
  244. iter.error = true;
  245. return true;
  246. };
  247. };
  248. util.inherits(module.exports.big5, mbcs);
  249. /**
  250. * EUC charset recognizers. One abstract class that provides the common function
  251. * for getting the next character according to the EUC encoding scheme,
  252. * and nested derived classes for EUC_KR, EUC_JP, EUC_CN.
  253. *
  254. * Get the next character value for EUC based encodings.
  255. * Character 'value' is simply the raw bytes that make up the character
  256. * packed into an int.
  257. */
  258. function eucNextChar(iter, det) {
  259. iter.index = iter.nextIndex;
  260. iter.error = false;
  261. var firstByte = 0;
  262. var secondByte = 0;
  263. var thirdByte = 0;
  264. //int fourthByte = 0;
  265. buildChar: {
  266. firstByte = iter.charValue = iter.nextByte(det);
  267. if (firstByte < 0) {
  268. // Ran off the end of the input data
  269. iter.done = true;
  270. break buildChar;
  271. }
  272. if (firstByte <= 0x8d) {
  273. // single byte char
  274. break buildChar;
  275. }
  276. secondByte = iter.nextByte(det);
  277. iter.charValue = (iter.charValue << 8) | secondByte;
  278. if (firstByte >= 0xA1 && firstByte <= 0xfe) {
  279. // Two byte Char
  280. if (secondByte < 0xa1) {
  281. iter.error = true;
  282. }
  283. break buildChar;
  284. }
  285. if (firstByte == 0x8e) {
  286. // Code Set 2.
  287. // In EUC-JP, total char size is 2 bytes, only one byte of actual char value.
  288. // In EUC-TW, total char size is 4 bytes, three bytes contribute to char value.
  289. // We don't know which we've got.
  290. // Treat it like EUC-JP. If the data really was EUC-TW, the following two
  291. // bytes will look like a well formed 2 byte char.
  292. if (secondByte < 0xa1) {
  293. iter.error = true;
  294. }
  295. break buildChar;
  296. }
  297. if (firstByte == 0x8f) {
  298. // Code set 3.
  299. // Three byte total char size, two bytes of actual char value.
  300. thirdByte = iter.nextByte(det);
  301. iter.charValue = (iter.charValue << 8) | thirdByte;
  302. if (thirdByte < 0xa1) {
  303. iter.error = true;
  304. }
  305. }
  306. }
  307. return iter.done == false;
  308. };
  309. /**
  310. * The charset recognize for EUC-JP. A singleton instance of this class
  311. * is created and kept by the public CharsetDetector class
  312. */
  313. module.exports.euc_jp = function() {
  314. this.name = function() {
  315. return 'EUC-JP';
  316. };
  317. this.language = function() {
  318. return 'ja';
  319. };
  320. // TODO: This set of data comes from the character frequency-
  321. // of-occurence analysis tool. The data needs to be moved
  322. // into a resource and loaded from there.
  323. this.commonChars = [
  324. 0xa1a1, 0xa1a2, 0xa1a3, 0xa1a6, 0xa1bc, 0xa1ca, 0xa1cb, 0xa1d6, 0xa1d7, 0xa4a2,
  325. 0xa4a4, 0xa4a6, 0xa4a8, 0xa4aa, 0xa4ab, 0xa4ac, 0xa4ad, 0xa4af, 0xa4b1, 0xa4b3,
  326. 0xa4b5, 0xa4b7, 0xa4b9, 0xa4bb, 0xa4bd, 0xa4bf, 0xa4c0, 0xa4c1, 0xa4c3, 0xa4c4,
  327. 0xa4c6, 0xa4c7, 0xa4c8, 0xa4c9, 0xa4ca, 0xa4cb, 0xa4ce, 0xa4cf, 0xa4d0, 0xa4de,
  328. 0xa4df, 0xa4e1, 0xa4e2, 0xa4e4, 0xa4e8, 0xa4e9, 0xa4ea, 0xa4eb, 0xa4ec, 0xa4ef,
  329. 0xa4f2, 0xa4f3, 0xa5a2, 0xa5a3, 0xa5a4, 0xa5a6, 0xa5a7, 0xa5aa, 0xa5ad, 0xa5af,
  330. 0xa5b0, 0xa5b3, 0xa5b5, 0xa5b7, 0xa5b8, 0xa5b9, 0xa5bf, 0xa5c3, 0xa5c6, 0xa5c7,
  331. 0xa5c8, 0xa5c9, 0xa5cb, 0xa5d0, 0xa5d5, 0xa5d6, 0xa5d7, 0xa5de, 0xa5e0, 0xa5e1,
  332. 0xa5e5, 0xa5e9, 0xa5ea, 0xa5eb, 0xa5ec, 0xa5ed, 0xa5f3, 0xb8a9, 0xb9d4, 0xbaee,
  333. 0xbbc8, 0xbef0, 0xbfb7, 0xc4ea, 0xc6fc, 0xc7bd, 0xcab8, 0xcaf3, 0xcbdc, 0xcdd1
  334. ];
  335. this.nextChar = eucNextChar;
  336. };
  337. util.inherits(module.exports.euc_jp, mbcs);
  338. /**
  339. * The charset recognize for EUC-KR. A singleton instance of this class
  340. * is created and kept by the public CharsetDetector class
  341. */
  342. module.exports.euc_kr = function() {
  343. this.name = function() {
  344. return 'EUC-KR';
  345. };
  346. this.language = function() {
  347. return 'ko';
  348. };
  349. // TODO: This set of data comes from the character frequency-
  350. // of-occurence analysis tool. The data needs to be moved
  351. // into a resource and loaded from there.
  352. this.commonChars = [
  353. 0xb0a1, 0xb0b3, 0xb0c5, 0xb0cd, 0xb0d4, 0xb0e6, 0xb0ed, 0xb0f8, 0xb0fa, 0xb0fc,
  354. 0xb1b8, 0xb1b9, 0xb1c7, 0xb1d7, 0xb1e2, 0xb3aa, 0xb3bb, 0xb4c2, 0xb4cf, 0xb4d9,
  355. 0xb4eb, 0xb5a5, 0xb5b5, 0xb5bf, 0xb5c7, 0xb5e9, 0xb6f3, 0xb7af, 0xb7c2, 0xb7ce,
  356. 0xb8a6, 0xb8ae, 0xb8b6, 0xb8b8, 0xb8bb, 0xb8e9, 0xb9ab, 0xb9ae, 0xb9cc, 0xb9ce,
  357. 0xb9fd, 0xbab8, 0xbace, 0xbad0, 0xbaf1, 0xbbe7, 0xbbf3, 0xbbfd, 0xbcad, 0xbcba,
  358. 0xbcd2, 0xbcf6, 0xbdba, 0xbdc0, 0xbdc3, 0xbdc5, 0xbec6, 0xbec8, 0xbedf, 0xbeee,
  359. 0xbef8, 0xbefa, 0xbfa1, 0xbfa9, 0xbfc0, 0xbfe4, 0xbfeb, 0xbfec, 0xbff8, 0xc0a7,
  360. 0xc0af, 0xc0b8, 0xc0ba, 0xc0bb, 0xc0bd, 0xc0c7, 0xc0cc, 0xc0ce, 0xc0cf, 0xc0d6,
  361. 0xc0da, 0xc0e5, 0xc0fb, 0xc0fc, 0xc1a4, 0xc1a6, 0xc1b6, 0xc1d6, 0xc1df, 0xc1f6,
  362. 0xc1f8, 0xc4a1, 0xc5cd, 0xc6ae, 0xc7cf, 0xc7d1, 0xc7d2, 0xc7d8, 0xc7e5, 0xc8ad
  363. ];
  364. this.nextChar = eucNextChar;
  365. };
  366. util.inherits(module.exports.euc_kr, mbcs);
  367. /**
  368. * GB-18030 recognizer. Uses simplified Chinese statistics.
  369. */
  370. module.exports.gb_18030 = function() {
  371. this.name = function() {
  372. return 'GB18030';
  373. };
  374. this.language = function() {
  375. return 'zh';
  376. };
  377. /*
  378. * Get the next character value for EUC based encodings.
  379. * Character 'value' is simply the raw bytes that make up the character
  380. * packed into an int.
  381. */
  382. this.nextChar = function(iter, det) {
  383. iter.index = iter.nextIndex;
  384. iter.error = false;
  385. var firstByte = 0;
  386. var secondByte = 0;
  387. var thirdByte = 0;
  388. var fourthByte = 0;
  389. buildChar: {
  390. firstByte = iter.charValue = iter.nextByte(det);
  391. if (firstByte < 0) {
  392. // Ran off the end of the input data
  393. iter.done = true;
  394. break buildChar;
  395. }
  396. if (firstByte <= 0x80) {
  397. // single byte char
  398. break buildChar;
  399. }
  400. secondByte = iter.nextByte(det);
  401. iter.charValue = (iter.charValue << 8) | secondByte;
  402. if (firstByte >= 0x81 && firstByte <= 0xFE) {
  403. // Two byte Char
  404. if ((secondByte >= 0x40 && secondByte <= 0x7E) || (secondByte >=80 && secondByte <= 0xFE)) {
  405. break buildChar;
  406. }
  407. // Four byte char
  408. if (secondByte >= 0x30 && secondByte <= 0x39) {
  409. thirdByte = iter.nextByte(det);
  410. if (thirdByte >= 0x81 && thirdByte <= 0xFE) {
  411. fourthByte = iter.nextByte(det);
  412. if (fourthByte >= 0x30 && fourthByte <= 0x39) {
  413. iter.charValue = (iter.charValue << 16) | (thirdByte << 8) | fourthByte;
  414. break buildChar;
  415. }
  416. }
  417. }
  418. iter.error = true;
  419. break buildChar;
  420. }
  421. }
  422. return iter.done == false;
  423. };
  424. // TODO: This set of data comes from the character frequency-
  425. // of-occurence analysis tool. The data needs to be moved
  426. // into a resource and loaded from there.
  427. this.commonChars = [
  428. 0xa1a1, 0xa1a2, 0xa1a3, 0xa1a4, 0xa1b0, 0xa1b1, 0xa1f1, 0xa1f3, 0xa3a1, 0xa3ac,
  429. 0xa3ba, 0xb1a8, 0xb1b8, 0xb1be, 0xb2bb, 0xb3c9, 0xb3f6, 0xb4f3, 0xb5bd, 0xb5c4,
  430. 0xb5e3, 0xb6af, 0xb6d4, 0xb6e0, 0xb7a2, 0xb7a8, 0xb7bd, 0xb7d6, 0xb7dd, 0xb8b4,
  431. 0xb8df, 0xb8f6, 0xb9ab, 0xb9c9, 0xb9d8, 0xb9fa, 0xb9fd, 0xbacd, 0xbba7, 0xbbd6,
  432. 0xbbe1, 0xbbfa, 0xbcbc, 0xbcdb, 0xbcfe, 0xbdcc, 0xbecd, 0xbedd, 0xbfb4, 0xbfc6,
  433. 0xbfc9, 0xc0b4, 0xc0ed, 0xc1cb, 0xc2db, 0xc3c7, 0xc4dc, 0xc4ea, 0xc5cc, 0xc6f7,
  434. 0xc7f8, 0xc8ab, 0xc8cb, 0xc8d5, 0xc8e7, 0xc9cf, 0xc9fa, 0xcab1, 0xcab5, 0xcac7,
  435. 0xcad0, 0xcad6, 0xcaf5, 0xcafd, 0xccec, 0xcdf8, 0xceaa, 0xcec4, 0xced2, 0xcee5,
  436. 0xcfb5, 0xcfc2, 0xcfd6, 0xd0c2, 0xd0c5, 0xd0d0, 0xd0d4, 0xd1a7, 0xd2aa, 0xd2b2,
  437. 0xd2b5, 0xd2bb, 0xd2d4, 0xd3c3, 0xd3d0, 0xd3fd, 0xd4c2, 0xd4da, 0xd5e2, 0xd6d0
  438. ];
  439. };
  440. util.inherits(module.exports.gb_18030, mbcs);