summaryrefslogtreecommitdiff
path: root/src/js/lib/str.js
blob: 31621bf8dd0e981234faabd4dae34051ae65a9b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
 *  currently unused (iirc) garbage
 *  basically just prints an address than the uint32_t there, and then +4, etc
 */
function prim_dump_u32(buf) {
	s = "";

	for (var i = 0; i < buf.length; i += 4) {
		tmp = [];

		tmp.push(buf[i + 0]);
		tmp.push(buf[i + 1]);
		tmp.push(buf[i + 2]);
		tmp.push(buf[i + 3]);

		s += "0x" + pad_left((0x422200 + i).toString(16), "0", 8);
		s += ": ";
		s += "0x" + pad_left(u8x4_to_u32(tmp).toString(16), "0", 8);
		if (u8x4_to_u32(tmp) >= 0x1800000 && u8x4_to_u32(tmp) < 0x1900000) {
			s += " -> 0x" + pad_left(read_u32(u8x4_to_u32(tmp)).toString(16), "0", 8);
			s += "\n";
			val = read_u32(u8x4_to_u32(tmp));
			if (val >= 0x1800000 && val < 0x1900000) {
				buf = read_buf(val, 0x100);
				s += (hexdump(buf, 8, 2, val, 8, "0x"));
			}
		}
		s += "\n";
	}

	return s;
}

/*
 *  pad str to n chars with c chars to the left
*/
function pad_left(s, c, n) {
	s_ = s;

	if (s_.length < n) {
		s_ = c.repeat(n - s_.length) + s_;
	}

	return s_;
}

/*
 *  convert ASCII str to uint8array (unicode be damned)
 */
function str_to_uint8_buf(s) {
	buf = new Uint8Array(s.length);

	for (i = 0; i < s.length; i++) {
		buf[i] = s.charCodeAt(i);
	}

	return buf;
}

/*
 *  HOLY UGLY BATMAN!
 */
function hexdump(buf, cols, col_split, base, pad_base, base_prefix) {
	s = "";
	if (buf.constructor != Uint8Array) {
		buf = str_to_uint8_buf(buf);
	}

	for (i = 0; i < buf.length; i += (cols * col_split)) {
		cur_base = base + i;
		s += base_prefix + pad_left(cur_base.toString(16), "0", pad_base) + ": ";
		for (j = i; j < (i + (cols * col_split)); j += col_split) {
			for (k = j; k < (j + col_split); k++) {
				val = buf[k];
				try {
					s += pad_left(val.toString(16), "0", 2);
				} catch (e) {
					s += "	";
				}
			}
			s += " ";
		}

		for (j = i; j < (i + (cols * col_split)); j++) {
			val = buf[j];

			if (val < 0x20 || val >= 0x80) {
				val = 0x2e; // period
			}

			chr = String.fromCharCode(val);
			s += chr;
		}

		s += "\n";
	}

	return s;
}

/*
 *  HEX SHIT
 */
function prim_hexdump(buf) {
	s = "";

	for (i = 0; i < buf.length; i++) {
		val = buf[i];
		s += pad_left(val.toString(16), "0", 2);
	}

	return s;
}