-
Notifications
You must be signed in to change notification settings - Fork 1
/
DelphiByteStreamReader-ES6.js
331 lines (269 loc) · 10.3 KB
/
DelphiByteStreamReader-ES6.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
"use strict";
/*
* Class DelphiByteStreamReader
* Provides methods to read given byte stream buffer starting from given offset.
* Methods correspond data types in Delphi language.
*
* Minimum required JS version: JS 2015 (ES6)
*/
var DelphiByteStreamReader = (function () {
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
function DelphiByteStreamReader(buffer, initOffset, maxLen) {
// Input is type of ArrayBuffer. It cannot be directly manipulated but create one of the typed array objects or a DataView object.
// Note: Problem with Typed array is that e.g. offset for reading integer must be multiple of 4. So DataView it is.
this.offset = 0;
this.buffer = buffer;
// convert ArrayBuffer to DataView
if (maxLen > 0) {
this.data = new DataView(buffer, initOffset, maxLen);
}
else {
this.data = new DataView(buffer, initOffset);
}
}
// -------------------------------------------------------------------------
// Private methods
// -------------------------------------------------------------------------
// converts byte array to characters
function bin2String(arr, maxLen = -1) {
let result = "";
if ((maxLen < 0) || (maxLen > arr.length))
maxLen = arr.length;
for (let i = 0; i < maxLen; i++) {
if (arr[i] == 0)
break;
result += String.fromCharCode(arr[i]);
}
return result;
};
// -------------------------------------------------------------------------
// Public methods
// -------------------------------------------------------------------------
DelphiByteStreamReader.prototype.getOffset = function () {
return this.offset;
};
DelphiByteStreamReader.prototype.incOffset = function (val) {
this.offset = this.offset + val;
};
// ----------------
// Integer types
//-----------------
// Signed 8-bit int
DelphiByteStreamReader.prototype.readShortInt = function () {
let val = this.data.getInt8(this.offset);
this.offset += 1;
return val;
};
// Signed 16-bit int
DelphiByteStreamReader.prototype.readSmallInt = function () {
let val = this.data.getInt16(this.offset, true);
this.offset += 2;
return val;
};
// Signed 32-bit int
DelphiByteStreamReader.prototype.readInteger = function () {
let val = this.data.getInt32(this.offset, true);
this.offset += 4;
return val;
};
DelphiByteStreamReader.prototype.readFixedInt = function () {
return this.readInteger();
};
// Signed 64-bit int as BigInt
DelphiByteStreamReader.prototype.readInt64Precise = function () {
throw new Error("Not implemented");
};
// Signed 64-bit int
DelphiByteStreamReader.prototype.readInt64 = function () {
let valN = this.readInt64Precise();
return Number(valN);
};
// Unsigned 8-bit int
DelphiByteStreamReader.prototype.readByte = function () {
let val = this.data.getUint8(this.offset);
this.offset += 1;
return val;
};
// Unsigned 16-bit int
DelphiByteStreamReader.prototype.readWord = function () {
let val = this.data.getUint16(this.offset, true);
this.offset += 2;
return val;
};
// Unsigned 32-bit int
DelphiByteStreamReader.prototype.readCardinal = function () {
let val = this.data.getUint32(this.offset, true);
this.offset += 4;
return val;
};
// Unsigned 64-bit int as BigInt
DelphiByteStreamReader.prototype.readUInt64Precise = function () {
throw new Error("Not implemented");
};
// Unsigned 64-bit int
DelphiByteStreamReader.prototype.readUInt64 = function () {
let valN = this.readUInt64Precise();
return Number(valN);
};
// ----------------
// Integer types (platform-dependent)
//-----------------
DelphiByteStreamReader.prototype.readNativeIntOn32 = function () {
return this.readInteger();
};
DelphiByteStreamReader.prototype.readNativeIntOn64 = function () {
return this.readInt64();
};
DelphiByteStreamReader.prototype.readNativeUIntOn32 = function () {
return this.readCardinal();
};
DelphiByteStreamReader.prototype.readNativeUIntOn64 = function () {
return this.readUInt64();
};
DelphiByteStreamReader.prototype.readLongIntOn32 = function () {
return this.readInteger();
};
DelphiByteStreamReader.prototype.readLongIntOnWin64 = function () {
return this.readInteger();
};
DelphiByteStreamReader.prototype.readLongIntOnPOSIX64 = function () {
return this.readInt64();
};
DelphiByteStreamReader.prototype.readLongWordOn32 = function () {
return this.readCardinal();
};
DelphiByteStreamReader.prototype.readLongWordOnWin64 = function () {
return this.readCardinal();
};
DelphiByteStreamReader.prototype.readLongWordOnPOSIX64 = function () {
return this.readUInt64();
};
// ----------------
// Boolean types
//-----------------
DelphiByteStreamReader.prototype.readBoolean = function () {
let val = this.readByte();
return (val != 0);
};
DelphiByteStreamReader.prototype.readByteBool = function () {
let val = this.readShortInt();
return (val != 0);
};
DelphiByteStreamReader.prototype.readWordBool = function () {
let val = this.readSmallInt();
return (val != 0);
};
DelphiByteStreamReader.prototype.readLongBool = function () {
let val = this.readInteger();
return (val != 0);
};
// ----------------
// Real types
//-----------------
// 32-bit real
DelphiByteStreamReader.prototype.readSingle = function () {
let val = this.data.getFloat32(this.offset, true);
this.offset += 4;
return val;
};
// 64-bit real
DelphiByteStreamReader.prototype.readDouble = function () {
let val = this.data.getFloat64(this.offset, true);
this.offset += 8;
return val;
};
DelphiByteStreamReader.prototype.readReal = function () {
return this.readDouble();
};
// DelphiByteStreamReader.prototype.readReal48 = function () {
// // not implemented
// let val = [];
//
// val[0] = this.data.getUint32(this.offset, true);
// this.offset += 4;
// val[1] = this.data.getInt16(this.offset, true);
// this.offset += 2;
// return (val[1] << 32) | (val[0]); // doesn't work, shifting must be < 32
// };
// DelphiByteStreamReader.prototype.readExtended = function () {
// // not implemented
// };
DelphiByteStreamReader.prototype.readComp = function () {
return this.readInt64();
};
DelphiByteStreamReader.prototype.readCurrency = function () {
return this.readInt64() / 10000;
};
// ----------------
// Character types
//-----------------
// byte-sized (8-bit) characters
DelphiByteStreamReader.prototype.readAnsiChar = function () {
let charCode = this.readByte();
return String.fromCharCode(charCode);
};
// word-sized (16-bit) characters
DelphiByteStreamReader.prototype.readChar = function () {
return this.readWideChar();
};
DelphiByteStreamReader.prototype.readWideChar = function () {
let charCode = this.readWord();
return String.fromCharCode(charCode);
};
DelphiByteStreamReader.prototype.readUCS2Char = function () {
return this.readWideChar();
};
// ----------------
// String types
//-----------------
DelphiByteStreamReader.prototype.readShortString = function (savedLen = 255) {
let actualLen = this.readByte(); // the first byte contains the actual length of the string
let str = this.readAnsiCharArray(savedLen, actualLen);
return str;
};
// ----------------
// Helpers for Character arrays
//-----------------
DelphiByteStreamReader.prototype.readCharArray = function (savedLen, actualLen = -1) {
let arr = [];
for (let i = 0; i < savedLen; i++) {
arr[i] = this.readWord();
}
return bin2String(arr, actualLen);
};
DelphiByteStreamReader.prototype.readAnsiCharArray = function (savedLen, actualLen = -1) {
let arr = this.readByteArray(savedLen);
return bin2String(arr, actualLen);
};
// ----------------
// Date and Time types
//-----------------
DelphiByteStreamReader.prototype.readDateTime = function () {
// In Delphi the TDateTime type maps to a Double
// where the integral part is the number of days passed since 1899-12-30 00:00
// and the fractional part is fraction of a 24 hour day that has elapsed on that day.
// For example .25 always corresponds to 06:00 not depending on the sign of the the integral part.
// In JS, Date is internally stored as milliseconds since 1970-01-01 00:00
let val = this.readDouble();
let timeInMs = Date.UTC(1899, 11, 30, 0, 0, 0, 0); // note: in JS months start from 0
if (val >= 0)
return new Date(timeInMs + 24*60*60*1000*val); // note: debugger shows as local time
else { // use .toISOString() to show saved time
let integral = Math.trunc(val); // negative
let fraction = Math.abs(val % 1); // positive => val -2.75 becomes -2+0.75=-1.25 days backwards
return new Date(timeInMs + 24*60*60*1000*(integral + fraction));
}
};
// ----------------
// Misc helpers
//-----------------
// reads a memory block of given length
DelphiByteStreamReader.prototype.readByteArray = function (len) {
let block = new Uint8Array(this.buffer, this.offset, len);
this.offset += len;
return block;
};
return DelphiByteStreamReader;
})();