• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var euckrCPs = []; // index is unicode cp, value is pointer
2for (p = 0; p < euckr.length; p++) {
3	if (euckr[p] != null && euckrCPs[euckr[p]] == null) {
4		euckrCPs[euckr[p]] = p;
5	}
6}
7
8function chars2cps(chars) {
9	// this is needed because of javascript's handling of supplementary characters
10	// char: a string of unicode characters
11	// returns an array of decimal code point values
12	var haut = 0;
13	var out = [];
14	for (var i = 0; i < chars.length; i++) {
15		var b = chars.charCodeAt(i);
16		if (b < 0 || b > 0xffff) {
17			alert("Error in chars2cps: byte out of range " + b.toString(16) + "!");
18		}
19		if (haut != 0) {
20			if (0xdc00 <= b && b <= 0xdfff) {
21				out.push(0x10000 + ((haut - 0xd800) << 10) + (b - 0xdc00));
22				haut = 0;
23				continue;
24			} else {
25				alert(
26					"Error in chars2cps: surrogate out of range " +
27						haut.toString(16) +
28						"!"
29				);
30				haut = 0;
31			}
32		}
33		if (0xd800 <= b && b <= 0xdbff) {
34			haut = b;
35		} else {
36			out.push(b);
37		}
38	}
39	return out;
40}
41
42function euckrEncoder(stream) {
43	cps = chars2cps(stream);
44	var out = "";
45	var cp;
46	var finished = false;
47	var endofstream = 2000000;
48
49	while (!finished) {
50		if (cps.length == 0) cp = endofstream;
51		else cp = cps.shift();
52
53		if (cp == endofstream) {
54			finished = true;
55			continue;
56		}
57		if (cp >= 0x00 && cp <= 0x7f) {
58			// ASCII
59			out += " " + cp.toString(16).toUpperCase();
60			continue;
61		}
62		var ptr = euckrCPs[cp];
63		if (ptr == null) {
64			return null;
65			//			out += ' &#'+cp+';'
66			//			continue
67		}
68		var lead = Math.floor(ptr / 190) + 0x81;
69		var trail = ptr % 190 + 0x41;
70		out +=
71			" " +
72			lead.toString(16).toUpperCase() +
73			" " +
74			trail.toString(16).toUpperCase();
75	}
76	return out.trim();
77}
78
79function convertToHex(str) {
80	// converts a string of ASCII characters to hex byte codes
81	var out = "";
82	var result;
83	for (var c = 0; c < str.length; c++) {
84		result = str.charCodeAt(c).toString(16).toUpperCase() + " ";
85		out += result;
86	}
87	return out;
88}
89
90function normalizeStr(str) {
91	var out = "";
92	for (var c = 0; c < str.length; c++) {
93		if (str.charAt(c) == "%") {
94			out += String.fromCodePoint(
95				parseInt(str.charAt(c + 1) + str.charAt(c + 2), 16)
96			);
97			c += 2;
98		} else out += str.charAt(c);
99	}
100	var result = "";
101	for (var o = 0; o < out.length; o++) {
102		result += "%" + out.charCodeAt(o).toString(16).toUpperCase();
103	}
104	return result.replace(/%1B%28%42$/, "");
105}
106