-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhex.js
141 lines (125 loc) · 3.46 KB
/
hex.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Intel HEXファイルデコーダー コンストラクタ
*/
HexDecoder = function(buffer) {
this.size = null;
this.top = null;
this.buffer = buffer;
};
/*
Intel HEXファイルをメモリに書き込む
*/
HexDecoder.prototype._decode = function(hex, buf, off) {
var top = 0xffff;
var bottom = 0;
var r = 0;
var sum;
var len;
var address_h;
var address_l;
var address;
var i;
var val;
if(off === undefined)
off = 0;
for(;;) {
/* レコード先頭文字を得る */
if(hex.charAt(r) != ":")
throw new Error("ファイルの形式が不正です");
r++;
/* データ長を得る */
sum = len = parseInt(hex.substr(r, 2), 16); r += 2;
if(len == 0)
return;
/* オフセットレコードを得る */
address_h = parseInt(hex.substr(r, 2), 16); r += 2;
sum += address_h;
address_l = parseInt(hex.substr(r, 2), 16); r += 2;
sum += address_l;
address = address_h << 8 | address_l;
/* レコードタイプを得る */
switch(parseInt(hex.substr(r, 2), 16)) {
case 0x00: /* データレコード */
break;
case 0x01: /* エンドレコード */
return;
default: /* その他(未対応) */
throw new Error("未対応の形式です");
}
r += 2;
/* データを得る */
for(i = address + off; i < address + off + len; i++) {
val = parseInt(hex.substr(r, 2), 16); r += 2;
sum += val;
if(buf != null)
buf[i] = val;
}
/* サムをチェックする */
sum = (~sum + 1) & 0xff;
/*
if(sum != parseInt(hex.substr(r, 2), 16))
throw new Error("チェックサムが合いません");
*/
r += 2;
/* 先頭とサイズを得る */
if(top > address + off)
top = address + off;
if(bottom < address + off)
bottom = address + off + len ;
this.top = top;
this.size = bottom - top;
/* 次の行まで移動する */
while(r < hex.length && (hex.charAt(r) == "\r" || hex.charAt(r) == "\n"))
r++;
}
};
/*
Intel HEXファイルの先頭とファイルサイズを得る
*/
HexDecoder.prototype.check = function(hex) {
var tmp = this.buffer;
this._decode(hex,tmp);
this.buffer = tmp;
};
/*
Intel HEXファイルをメモリに書き込む
*/
HexDecoder.prototype.decode = function(hex, address) {
if(address === undefined)
this._decode(hex,this.buffer);
else {
this.check(hex,this.buffer);
this._decode(hex,this.buffer, address - this.top);
}
return [this.top , this.size];
};
/*
Intel HEXファイルをサーバから読み込む
*/
HexDecoder.prototype.read = function(url, address) {
this._hexDecoderHttp = new XMLHttpRequest();
this._hexDecoderHttp.open("GET", url, false);
this._hexDecoderHttp.send(null);
this.decode(this._hexDecoderHttp.responseText, address);
};
/*
ZIP 化された Intel HEXファイルをメモリに書き込む
*/
HexDecoder.prototype.decodezip = function(data, filelist) {
// Zip archive
var zip=new JSZip(data);
for (var i=0;i<filelist.length;i++){
var ihex=zip.file(filelist[i]).asText();
this._decode(ihex,this.buffer[i]);
}
};
/*
Intel HEX ZIP ファイルをサーバから読み込む
*/
HexDecoder.prototype.readzip = function(url, filelist) {
this._hexDecoderHttp = new XMLHttpRequest();
this._hexDecoderHttp.open("GET", url, false);
this._hexDecoderHttp.overrideMimeType('text\/plain; charset=x-user-defined');
this._hexDecoderHttp.send(null);
this.decodezip(this._hexDecoderHttp.response, filelist);
};