• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var big5CPs = []; // index is unicode cp, value is pointer
2for (var p = 5024; p < big5.length; p++) {
3	// "Let index be index jis0208 excluding all pointers in the range 8272 to 8835, inclusive."
4	if (big5[p] != null && big5CPs[big5[p]] == null) {
5		big5CPs[big5[p]] = p;
6	}
7}
8//  If code point is U+2550, U+255E, U+2561, U+256A, U+5341, or U+5345, return the last pointer corresponding to code point in index.
9big5CPs[0x2550] = 18991;
10big5CPs[0x255e] = 18975;
11big5CPs[0x2561] = 18977;
12big5CPs[0x256a] = 18976;
13big5CPs[0x5341] = 5512;
14big5CPs[0x5345] = 5599;
15
16function chars2cps(chars) {
17	// this is needed because of javascript's handling of supplementary characters
18	// char: a string of unicode characters
19	// returns an array of decimal code point values
20	var haut = 0;
21	var out = [];
22	for (var i = 0; i < chars.length; i++) {
23		var b = chars.charCodeAt(i);
24		if (b < 0 || b > 0xffff) {
25			alert("Error in chars2cps: byte out of range " + b.toString(16) + "!");
26		}
27		if (haut != 0) {
28			if (0xdc00 <= b && b <= 0xdfff) {
29				out.push(0x10000 + ((haut - 0xd800) << 10) + (b - 0xdc00));
30				haut = 0;
31				continue;
32			} else {
33				alert(
34					"Error in chars2cps: surrogate out of range " +
35						haut.toString(16) +
36						"!"
37				);
38				haut = 0;
39			}
40		}
41		if (0xd800 <= b && b <= 0xdbff) {
42			haut = b;
43		} else {
44			out.push(b);
45		}
46	}
47	return out;
48}
49
50function big5Encoder(stream) {
51	var cps = chars2cps(stream);
52	var out = "";
53	var cp;
54	var finished = false;
55	var endofstream = 2000000;
56
57	while (!finished) {
58		if (cps.length == 0) cp = endofstream;
59		else cp = cps.shift();
60
61		var cpx = 0;
62
63		if (cp == endofstream) {
64			finished = true;
65			continue;
66		}
67		if (cp >= 0x00 && cp <= 0x7f) {
68			// ASCII
69			out += " " + cp.toString(16).toUpperCase();
70			continue;
71		}
72		var ptr = big5CPs[cp];
73		if (ptr == null) {
74			return null;
75			//			out += ' &#'+cp+';'
76			//			continue
77		}
78		var lead = Math.floor(ptr / 157) + 0x81;
79		var trail = ptr % 157;
80		var offset;
81		if (trail < 0x3f) offset = 0x40;
82		else {
83			offset = 0x62;
84		}
85		var end = trail + offset;
86		out +=
87			" " +
88			lead.toString(16).toUpperCase() +
89			" " +
90			end.toString(16).toUpperCase();
91	}
92
93	return out.trim();
94}
95
96function convertToHex(str) {
97	// converts a string of ASCII characters to hex byte codes
98	var out = "";
99	var result;
100	for (var c = 0; c < str.length; c++) {
101		result = str.charCodeAt(c).toString(16).toUpperCase() + " ";
102		out += result;
103	}
104	return out;
105}
106
107function normalizeStr(str) {
108	var out = "";
109	for (var c = 0; c < str.length; c++) {
110		if (str.charAt(c) == "%") {
111			out += String.fromCodePoint(
112				parseInt(str.charAt(c + 1) + str.charAt(c + 2), 16)
113			);
114			c += 2;
115		} else out += str.charAt(c);
116	}
117	var result = "";
118	for (var o = 0; o < out.length; o++) {
119		result += "%" + out.charCodeAt(o).toString(16).toUpperCase();
120	}
121	return result.replace(/%1B%28%42$/, "");
122}
123