forked from jeffpar/pcjs.v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiledump.js
909 lines (853 loc) · 33.6 KB
/
filedump.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
/**
* @fileoverview Converts file contents to JSON
* @author <a href="mailto:[email protected]">Jeff Parsons</a> (@jeffpar)
* @copyright © 2012-2020 Jeff Parsons
*
* This file is part of PCjs, a computer emulation software project at <https://www.pcjs.org>.
*
* PCjs is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* PCjs is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCjs. If not,
* see <http://www.gnu.org/licenses/gpl.html>.
*
* You are required to include the above copyright notice in every modified copy of this work
* and to display that copyright notice when the software starts running; see COPYRIGHT in
* <https://www.pcjs.org/modules/shared/lib/defines.js>.
*
* Some PCjs files also attempt to load external resource files, such as character-image files,
* ROM files, and disk image files. Those external resource files are not considered part of PCjs
* for purposes of the GNU General Public License, and the author does not claim any copyright
* as to their contents.
*/
"use strict";
var fs = require("fs");
var path = require("path");
var mkdirp = require("mkdirp");
var defines = require("../../shared/lib/defines");
var net = require("../../shared/lib/netlib");
var proc = require("../../shared/lib/proclib");
var str = require("../../shared/lib/strlib");
var DumpAPI = require("../../shared/lib/dumpapi");
/**
* FileDump()
*
* TODO: Consider adding a "map" option that allows the user to supply a MAP filename (via a "map" API parameter
* or a "--map" command-line option), which in turn triggers a call to loadMap(). Note that loadMap() will need to
* be a bit more general and use a worker function that calls either net.getFile() or fs.readFile(), similar to
* what the loadFile() function already does.
*
* @constructor
* @param {string} [sFormat] should be one of "json"|"longs"|"hex"|"bytes"|"rom" (see the FORMAT constants)
* @param {boolean|string} [fComments] enables comments and other readability enhancements in the JSON output
* @param {boolean|string} [fDecimal] forces decimal output if not undefined
* @param {number|string} [offDump]
* @param {number|string} [lenDump]
* @param {number|string} [nWidthDump]
* @param {string} [symbolFormat]
* @param {string} [sServerRoot]
*/
function FileDump(sFormat, fComments, fDecimal, offDump, lenDump, nWidthDump, symbolFormat, sServerRoot)
{
this.fDebug = false;
this.sFormat = (sFormat || DumpAPI.FORMAT.JSON);
this.fJSONNative = (this.sFormat == DumpAPI.FORMAT.JSON && !fComments);
this.nJSONIndent = 0;
this.fJSONComments = fComments;
this.sJSONWhitespace = (this.fJSONComments? " " : "");
this.fDecimal = fDecimal;
this.offDump = +offDump || 0;
this.lenDump = +lenDump || 0;
this.nWidthDump = +nWidthDump || 16;
this.symbolFormat = symbolFormat || "";
this.sServerRoot = sServerRoot || "";
this.buf = null;
this.addrLoad = null;
this.addrExec = null;
/*
* TODO: Decide what to do with this usage info; we can't use it as a default, because setting this.json
* causes outputFile() to ignore this.buf indiscriminately (ie, it breaks non-JSON output modes).
*/
this.json = ""; // "[\n /**\n * " + FileDump.sAPIURL + " " + FileDump.sCopyright + "\n * " + FileDump.sUsage + "\n */\n]";
}
/*
* Class constants
*/
FileDump.sAPIURL = "http://www.pcjs.org" + DumpAPI.ENDPOINT;
FileDump.sCopyright = COPYRIGHT;
FileDump.sNotice = FileDump.sAPIURL + " " + FileDump.sCopyright;
FileDump.sUsage = "Usage: " + FileDump.sAPIURL + "?" + DumpAPI.QUERY.FILE + "=({path}|{URL})&" + DumpAPI.QUERY.FORMAT + "=(json|longs|hex|octal|bytes|words|rom)";
FileDump.asBadExts = [
"js", "log"
];
/*
* Class methods
*/
/**
* CLI()
*
* Provides the command-line interface for the FileDump module.
*
* Usage:
*
* filedump --file=({path}|{URL}) [--merge=({path}|{url})] [--format=(json|longs|hex|octal|bytes|words|rom)]
* [--comments] [--decimal] [--offset={number}] [--width={number}] [--load={number}] [--exec={number}]
* [--output={path}] [--overwrite]
*
* Arguments:
*
* The default format is "json", which generates an array of signed 32-bit decimal values; "hex" is an older
* text format that consists entirely of 2-character hex values (deprecated), and "bytes" is a JSON-like format
* that also uses hex values (but with "0x" prefixes) and is normally used only when comments are enabled (use
* --decimal to force decimal byte output). "words" are like "bytes" except that it outputs 16-bit values
* instead of 8-bit values; "octal" is an alias for "words", the only difference being that it includes a comment
* alongside each word with the octal form of the word.
*
* If you want actual octal output, then an --octal option should be added at some point; however, that begs the
* question of how to render octal numbers so that all versions of JavaScript interpret them correctly. Prefixing
* octal values with an extra zero was the old convention, but it was never formally adopted and it isn't allowed
* in "strict" mode. Prefixing them with "0o" is the new convention for ES6 and up, which is great, but we still
* have no octal format that works across the board. We can use the new octal format in code, because we always
* compile browser-based code back to ES5 (converting all numeric constants to decimal), and current versions of
* Node supported the new format for quite a while. But what we store in data files is more problematic.
*
* When a second file is "merged", the first file sets all even bytes and the second file sets all odd bytes.
* In fact, any number of files can be merged: if there are N files, file #1 sets bytes at "offset mod N == 0",
* file #2 sets all bytes at "offset mod N == 1", and file #N sets all bytes at "offset mod N == N - 1".
*
* Note that command-line arguments, if any, are not validated. For example, argv['comments'] may be any of
* boolean, string, or undefined, since the user may have typed "--comments" or "--comments=foo" or nothing at all.
*
* Examples
*
* filedump --file=devices/pc/video/ibm-ega.rom --format=bytes --decimal
*
* Notes
*
* Originally, we had to specify `--format=bytes` because the onLoadROM() code in rom.js assumed the data was
* always byte-sized, but it has since been updated to support dword arrays, so the default format ("json")
* works fine as well. Also, `--decimal` reduces the size of the output file significantly.
*
* If there's a ".map" file (eg, "ibm-ega.map"), it's automatically loaded and appended to the ROM data as a
* "symbols" property; we may want to consider an option to disable the processing of map files, but for now, the
* simple answer is: if you don't want one, don't create one.
*/
FileDump.CLI = function()
{
let args = proc.getArgs();
if (!args.argc) {
console.log("usage: filedump --file=({path}|{URL}) [--merge=({path}|{url})] [--format=(json|longs|hex|octal|bytes|words|rom)] [--comments] [--decimal] [--offset={number}] [--width={number}] [--output={path}] [--overwrite]");
return;
}
let argv = args.argv;
let sFile = argv['file'];
if (!sFile || FileDump.asBadExts.indexOf(str.getExtension(sFile)) >= 0) {
FileDump.logError(new Error("bad or missing input filename"));
return;
}
let sOutputFile = argv['output'];
let fOverwrite = argv['overwrite'];
let sServerRoot = process.cwd();
let i = sServerRoot.indexOf("/pcjs");
sServerRoot = i > 0? sServerRoot.substr(0, i+5) : undefined;
let sFormat = FileDump.validateFormat(argv['format']);
if (sFormat === false) {
FileDump.logError(new Error("unrecognized format"));
return;
}
let sMergeFile, asMergeFiles = [];
let file = new FileDump(sFormat, argv['comments'], argv['decimal'], argv['offset'], argv['length'], argv['width'], argv['symbols'], sServerRoot);
if (argv['merge']) {
if (typeof argv['merge'] == "string") {
asMergeFiles.push(argv['merge']);
} else {
for (sMergeFile in argv['merge']) asMergeFiles.push(sMergeFile);
}
}
file.addrLoad = str.parseInt(argv['load']);
file.addrExec = str.parseInt(argv['exec']);
let cMergesPending = asMergeFiles.length, iStart = 0, nSkip = cMergesPending;
file.loadFile(sFile, iStart++, nSkip, function(err) {
let cErrors = 0;
let convert = function(merge=0) {
cMergesPending -= merge;
if (!cMergesPending) {
if (argv['checksum']) console.log(str.sprintf("checksum: 0x%02X", file.getChecksum()));
if (!cErrors) file.convertToFile(sOutputFile, fOverwrite);
}
};
if (!err) {
while ((sMergeFile = asMergeFiles.shift())) {
file.loadFile(sMergeFile, iStart++, nSkip, function(err) {
if (err) cErrors++;
convert(1);
});
}
convert();
}
});
};
/**
* logError(err)
*
* Conditionally logs an error to the console.
*
* @param {Error} err
* @return {string} the error message that was logged (or that would have been logged had logging been enabled)
*/
FileDump.logError = function(err)
{
let sError = "";
if (err) {
sError = "FileDump error: " + err.message;
console.log(sError);
}
return sError;
};
/**
* validateFormat(sFormat)
*
* @param {string} sFormat
* @return {null|string|boolean} the validated format, null if unspecified, or false if invalid
*/
FileDump.validateFormat = function(sFormat)
{
if (!sFormat) {
return null;
}
for (let s in DumpAPI.FORMAT) {
if (sFormat == DumpAPI.FORMAT[s]) return sFormat;
}
return false;
};
/*
* Object methods
*/
/**
* loadFile(sFile, iStart, nSkip, done)
*
* This used to be part of the FileDump constructor, but I felt it would be safer to separate
* object creation from any I/O, ensuring that the callback will not be invoked until the caller
* has received the FileDump object.
*
* @this {FileDump}
* @param {string} sFile
* @param {number} iStart
* @param {number} nSkip
* @param {function(Error)} done
*/
FileDump.prototype.loadFile = function(sFile, iStart, nSkip, done)
{
let obj = this;
let encoding = null;
let sExt = str.getExtension(sFile);
if (sExt == DumpAPI.FORMAT.JSON || sExt == DumpAPI.FORMAT.HEX || sExt == "lst" || sExt == "txt") {
encoding = "utf8";
}
let options = {encoding: encoding};
let sFilePath = (!this.sServerRoot || net.isRemote(sFile))? sFile : path.join(this.sServerRoot, sFile);
if (!this.sFilePath) this.sFilePath = sFilePath;
if (this.fDebug) console.log("loadFile(" + sFilePath + "," + iStart + "," + nSkip + ")");
if (net.isRemote(sFilePath)) {
net.getFile(sFilePath, options.encoding, function(err, status, buf) {
if (err) {
FileDump.logError(err);
done(err);
return;
}
obj.setData(buf, iStart, nSkip, sExt);
done(null);
});
} else {
fs.readFile(sFilePath, options, function(err, buf) {
if (err) {
FileDump.logError(err);
done(err);
return;
}
obj.setData(buf, iStart, nSkip, sExt);
done(null);
});
}
};
/**
* parseListing(sListing)
*
* Parses the data within a MACRO11 listing file and produces an array of bytes.
*
* @this {FileDump}
* @param {string} sListing
* @return {Array.<number>}
*/
FileDump.prototype.parseListing = function(sListing)
{
let ab = [];
let matchLine;
let re = /^( [0-9 ]{8}|)([0-7]{6})[: ]+([0-7]+)[ ]*([0-7']+|)[ ]+([0-7']+|)[ ]+(.*)$/gm;
while ((matchLine = re.exec(sListing))) {
/*
* matchLine[1]: line # (optional)
* matchLine[2]: address
* matchLine[3]: 6-digit opcode or 3-digit data
* matchLine[4]: 6-digit operand (with optional apostrophe) or 3-digit data
* matchLine[5]: 6-digit operand (with optional apostrophe) or 3-digit data
*/
let addrLine = parseInt(matchLine[2], 8);
if (this.addrLoad == null) this.addrLoad = addrLine;
for (let i = 3, s; i <= 5 && (s = matchLine[i]); i++) {
let data = parseInt(s.substr(0, 6), 8);
if (s.length == 3) {
addrLine++;
ab.push(data);
}
else {
addrLine += 2;
if (s.length == 7) {
data -= addrLine;
}
ab.push(data & 0xff);
ab.push(data >> 8);
}
}
}
return ab;
};
/**
* setData(buf, iStart, nSkip, sExt)
*
* Records the given file data in the FileDump's buffer
*
* @this {FileDump}
* @param {Buffer|string} buf
* @param {number} iStart
* @param {number} nSkip
* @param {string} [sExt]
*/
FileDump.prototype.setData = function(buf, iStart, nSkip, sExt)
{
let b, i, j, s;
if (typeof buf == "string") {
let ab = [];
if (sExt == "lst" || sExt == "txt") {
ab = this.parseListing(buf);
}
else if (buf.indexOf('{') >= 0) {
/*
* Treat the incoming string data as JSON data.
*/
let json;
try {
// json = JSON.parse(buf);
json = /** @type {Object} */ (eval("(" + buf + ")"));
} catch (err) {
FileDump.logError(err);
json = null;
}
if (json) {
let values, bytes;
if ((values = json['data'])) {
bytes = 4;
} else if ((values = json['words'])) {
bytes = 2;
}
if (values) {
for (i = 0; i < values.length; i++) {
let v = values[i];
for (j = 0; j < bytes; j++) {
ab.push(v & 0xff);
v >>>= 8;
}
}
}
}
}
else {
/*
* Treat the incoming string data as HEX (ie, a series of byte values encoded in hex, separated by whitespace)
*/
let as = buf.split(/\s+/);
for (i = 0; i < as.length; i++) {
s = as[i];
if (!s.length) continue;
if (isNaN(b = parseInt(s, 16))) break;
ab.push(b);
}
}
/*
* If we didn't recognize the data, then simply return, leaving this.buf undefined.
*/
if (!ab.length) return;
buf = Buffer.from(ab);
}
if (!this.buf) {
if (!nSkip) {
this.buf = buf;
} else {
this.buf = Buffer.alloc(buf.length * (nSkip + 1));
}
}
if (nSkip) {
for (i = 0; i < buf.length; i++) {
this.buf.writeUInt8(b = buf.readUInt8(i), iStart);
iStart += nSkip + 1;
}
}
};
/**
* getData()
*
* @this {FileDump}
* @return {Buffer|null}
*/
FileDump.prototype.getData = function()
{
return this.buf;
};
/**
* getChecksum()
*
* @this {FileDump}
* @return {number|null}
*/
FileDump.prototype.getChecksum = function()
{
if (!this.buf) return null;
let b = 0;
for (let i = 0; i < this.buf.length; i++) {
b += this.buf.readUInt8(i);
}
return b & 0xff;
};
/**
* dumpLine(nIndent, sLine, sComment)
*
* @this {FileDump}
* @param {number} [nIndent] is the relative number of characters to indent the given line (0 if none)
* @param {string} [sLine] is the given line
* @param {string} [sComment] is an optional comment to append to the line, if comment output is enabled
* @return {string} the indented/commented line
*/
FileDump.prototype.dumpLine = function(nIndent, sLine, sComment)
{
if (nIndent < 0) {
this.nJSONIndent += nIndent;
}
if (this.fJSONComments) {
sLine = " ".substr(0, this.nJSONIndent) + (sLine? (sLine + (sComment? (" // " + sComment) : "")) : "");
}
if (sLine) sLine += "\n";
if (nIndent > 0) {
this.nJSONIndent += nIndent;
}
return sLine;
};
/**
* dumpBuffer(sKey, buf, len, cbItem, offDump, lenDump, nWidthDump)
*
* @this {FileDump}
* @param {String} sKey is name of buffer data element
* @param {Buffer} buf is a Buffer containing the bytes to dump
* @param {number} len is the number of bytes to dump
* @param {number} cbItem is either 1 or 4, to dump bytes or dwords respectively
* @param {number} [offDump] is a relative offset (default is 0; see constructor)
* @param {number} [lenDump] is a relative length (default is 0; see constructor)
* @param {number} [nWidthDump] is an alternate width (default is 16; see constructor)
* @return {string} hex (or decimal) representation of the data
*/
FileDump.prototype.dumpBuffer = function(sKey, buf, len, cbItem, offDump, lenDump, nWidthDump)
{
let chOpen = '', chClose = '', chSep = ' ', sHexPrefix = "";
let nBase = 16;
if (sKey == DumpAPI.FORMAT.OCTAL) {
sKey = DumpAPI.FORMAT.WORDS;
nBase = 8;
}
this.sKey = sKey;
if (this.sFormat != DumpAPI.FORMAT.HEX) {
chOpen = '['; chClose = ']'; chSep = ',';
}
offDump = offDump || this.offDump;
lenDump = lenDump || this.lenDump || len;
nWidthDump = nWidthDump || this.nWidthDump;
let sDump = "";
let addrs = {'load': this.addrLoad, 'exec': this.addrExec};
for (let prop in addrs) {
let addr = addrs[prop];
if (addr != null) {
sDump += this.dumpLine(2, '"' + prop + '":' + str.toHexWord(addr) + (nBase == 8? "/*" + str.toOct(addr, 6) + "*/" : "") + ',');
}
}
if (sKey == "bytes") {
sDump += this.dumpLine(2, '"width":8,');
sKey = "values";
}
else if (sKey == "longs") {
sDump += this.dumpLine(2, '"width":32,');
sKey = "values";
}
sDump += this.dumpLine(2, (sKey? '"' + sKey + '":' : "") + this.sJSONWhitespace + chOpen);
let sLine = "";
let sASCII = "";
for (let cb = 0, off = offDump; cb < lenDump; cb += cbItem, off += cbItem) {
/*
* WARNING: Whenever the following condition arises, you probably have a non-dword-granular
* file, and you should have requested a byte-granular dump instead.
*/
if (off + cbItem > buf.length) {
break;
}
let v = (cbItem == 1? buf.readUInt8(off) : (cbItem == 2? buf.readInt16LE(off) : buf.readUInt32LE(off)));
if (off > offDump) {
sLine += chSep;
if (!(cb % nWidthDump)) {
sDump += this.dumpLine(0, sLine, sASCII);
sLine = sASCII = "";
}
}
if (cbItem > 1) {
if (cbItem > 2) {
sLine += v;
} else {
sLine += str.toHexWord(v) + (nBase == 8? "/*" + str.toOct(v & 0xffff, 6) + "*/" : "");
}
}
else {
if (this.fDecimal) {
sLine += v;
} else {
sLine += str.toHexByte(v);
}
if (!sASCII) sASCII = str.toHex(off, 0, true) + " ";
sASCII += (v >= 0x20 && v < 0x7F && v != 0x3C && v != 0x3E? String.fromCharCode(v) : ".");
}
}
sDump += this.dumpLine(0, sLine + chClose, sASCII);
this.dumpLine(-2);
return sDump;
};
/**
* loadMap(sMapFile, done)
*
* NOTE: Since ".map" files are an internal construct, I support only local map files (for now)
*
* @this {FileDump}
* @param {string} sMapFile
* @param {function(Error,String)} done
*/
FileDump.prototype.loadMap = function(sMapFile, done)
{
/*
* The HEX format doesn't support MAP files, because old HEX clients expect an Array of bytes,
* not an Object. For all other (JSON) formats, we assume that the JSON is "unwrapped" at this point,
* and so even if loadMap() doesn't find a map file, it will still wrap the resulting JSON with braces.
*/
if (this.sFormat != DumpAPI.FORMAT.HEX) {
if (!this.sKey) {
this.json = '"bytes":' + this.json;
}
let obj = this;
sMapFile = sMapFile.replace(/\.(rom|json|bin)$/, ".map");
if (str.endsWith(sMapFile, ".map")) {
let sMapName = path.basename(sMapFile);
fs.readFile(sMapFile, {encoding: "utf8"}, function(err, str) {
let sMapData = null;
if (err) {
/*
* This isn't really an error (map files are optional), although it might be helpful to display
* a warning. In any case, this is also why the first done() callback below always passes null for
* the Error parameter.
*
FileDump.logError(err);
*/
}
else {
// console.log("add this to obj.json:\n" + str);
/*
* Parse MAP data into a set of properties; for example, if the .map file contains:
*
* 0320 = HF_PORT
* 0000:0034 4 HDISK_INT
* 0040:0042 1 CMD_BLOCK
* 0003 @ DISK_SETUP
* 0000:004C 4 ORG_VECTOR
* 0028 . MOV AX,WORD PTR ORG_VECTOR ;GET DISKETTE VECTOR
*
* where the symbols in the second column of the .map file indicate type/size information as follows:
*
* = unsized value
* 1 1-byte (DB) value
* 2 2-byte (DW) value
* 4 4-byte (DD) value
* @ label
* . reference
* + bias (ie, value to be added to all following offsets)
*
* then we should produce the following corresponding JSON:
*
* {
* "HF_PORT": {
* "v":800
* },
* "HDISK_INT": {
* "b":4, "s":0, "o":52
* },
* "ORG_VECTOR": {
* "b":4, "s":0, "o":76
* },
* "CMD_BLOCK": {
* "b":1, "s":64, "o":66
* },
* "DISK_SETUP": {
* "o":3
* },
* ".40": {
* "o":64, "a":"MOV AX,WORD PTR ORG_VECTOR ;GET DISKETTE VECTOR"
* }
* }
*
* where "v" is the value of an absolute (unsized) value; "b" is either 1, 2, 4 or undefined; "s" is either a hard-coded
* segment or undefined; and "o" is the offset of an symbol. Also, if the symbol is not entirely upper-case, then we
* store the original-case version of the symbol as an "l" property.
*
* If the same symbol appears more than once in a .map file, the value of the last occurrence will replace any previous
* occurrence(s).
*
* aSymbols will be an associative array containing an entry for every symbol, where the key is the symbol and the value
* is another associative array containing the other properties described above.
*
* An alternative output format, enabled with --symbols=array, is a simplified transformation of the map file,
* using string triplets:
*
* "0320","=","HF_PORT",
* "0000:0034","4","HDISK_INT",
* "0040:0042","1","CMD_BLOCK",
* "0003","@","DISK_SETUP",
* "0000:004C","4","ORG_VECTOR",
* "0028",";","MOV AX,WORD PTR ORG_VECTOR ;GET DISKETTE VECTOR"
*/
let aSymbols;
let nBias = 0;
let asLines = str.split('\n');
if (obj.symbolFormat == "array") {
aSymbols = [];
} else {
aSymbols = {};
}
for (let iLine = 0; iLine < asLines.length; iLine++){
let s = asLines[iLine].trim();
if (!s || s.charAt(0) == ';') continue;
let match = s.match(/^\s*([0-9A-Z:]+)\s+([=124@.+;])(?:\t| {3})(.*?)\s*$/i);
if (match) {
let sValue = match[1];
let sType = match[2];
let sSymbol = match[3].replace(/"/g, "''");
if (obj.symbolFormat == "array") {
if (sType == '.' && sSymbol[0] == ';') {
sType = ';';
sSymbol = sSymbol.substr(1).trim();
if (!sSymbol) continue;
}
aSymbols.push(sValue);
aSymbols.push(sType);
aSymbols.push(sSymbol);
continue;
}
let sSegment = null;
let i = sValue.indexOf(':');
if (i >= 0) {
sSegment = sValue.substr(0, i);
sValue = sValue.substr(i+1);
}
let sComment = null;
i = sSymbol.indexOf(';');
if (i >= 0) {
sComment = sSymbol.substr(i+1).trim();
sSymbol = sSymbol.substr(0, i).trim();
}
let sID = sSymbol.toUpperCase();
let aValue = {};
switch (sType) {
case '=':
aValue['v'] = parseInt(sValue, 16);
break;
case '1':
case '2':
case '4':
aValue['b'] = parseInt(sType, 10);
/* falls through */
case '@':
case '.':
aValue['o'] = parseInt(sValue, 16) + nBias;
if (sSegment) {
aValue['s'] = parseInt(sSegment, 16);
}
if (sType != '.') break;
match = sSymbol.match(/^([A-Z_][A-Z0-9_]*):\s*(.*)/i);
if (match) {
sSymbol = match[1];
sID = sSymbol.toUpperCase();
if (match[2]) aValue['a'] = match[2];
if (aSymbols[sID]) {
sID = '.' + parseInt(sValue, 16);
}
} else {
aValue['a'] = sSymbol;
sID = sSymbol = '.' + parseInt(sValue, 16);
}
break;
case '+':
nBias = parseInt(sValue, 16);
continue;
default:
done(new Error("unrecognized symbol type (" + sType + ") in MAP file: " + sMapName), null);
return;
}
if (sID != sSymbol) {
aValue['l'] = sSymbol;
}
if (sComment) {
aValue['c'] = sComment;
}
aSymbols[sID] = aValue;
continue;
}
done(new Error("unrecognized line (" + s + ") in MAP file: " + sMapName), null);
return;
}
if (obj.symbolFormat == "array") {
sMapData = "[";
for (let i = 0; i < aSymbols.length; i+=3) {
if (i) sMapData += ',';
sMapData += '\n"' + aSymbols[i] + '","' + aSymbols[i+1] + '","' + aSymbols[i+2] + '"';
}
sMapData += "]\n";
} else {
if (obj.fJSONComments) {
sMapData = JSON.stringify(aSymbols, null, 2);
} else {
sMapData = JSON.stringify(aSymbols);
}
}
if (sMapData) {
obj.json = '{' + obj.json + ',"symbols":' + sMapData + '}';
}
}
if (!sMapData) {
obj.json = '{' + obj.json + '}';
}
done(null, obj.json);
});
return;
}
this.json = '{' + this.json + '}';
}
done(null, this.json);
};
/**
* buildJSON()
*
* Common code between the API helper (convertToJSON()) and the command-line helper (convertToFile()).
*
* @this {FileDump}
*/
FileDump.prototype.buildJSON = function()
{
this.json = "";
if (!this.buf) {
// console.log("no data available in file");
this.json = "[ /* no data */ ]";
} else {
// console.log("length of buffer: " + this.buf.length);
if (this.fJSONComments || this.sFormat == DumpAPI.FORMAT.HEX || this.sFormat == DumpAPI.FORMAT.BYTES) {
this.json += this.dumpBuffer(DumpAPI.FORMAT.BYTES, this.buf, this.buf.length, 1);
}
else if (this.sFormat == DumpAPI.FORMAT.OCTAL || this.sFormat == DumpAPI.FORMAT.WORDS) {
this.json += this.dumpBuffer(this.sFormat, this.buf, this.buf.length, 2);
} else {
this.json += this.dumpBuffer(DumpAPI.FORMAT.LONGS, this.buf, this.buf.length, 4);
}
}
};
/**
* convertToJSON(done)
*
* Converts the data buffer to JSON.
*
* @this {FileDump}
* @param {function(Error,string)} done
*/
FileDump.prototype.convertToJSON = function(done)
{
this.buildJSON();
this.loadMap(this.sFilePath, done);
};
/**
* convertToFile(sOutputFile, fOverwrite)
*
* Converts the data buffer to JSON, as appropriate.
*
* @this {FileDump}
* @param {string} sOutputFile
* @param {boolean} fOverwrite
*/
FileDump.prototype.convertToFile = function(sOutputFile, fOverwrite)
{
if (this.sServerRoot && sOutputFile) {
sOutputFile = path.join(this.sServerRoot, sOutputFile);
}
if (this.sFormat != DumpAPI.FORMAT.ROM) {
let obj = this;
this.buildJSON();
this.loadMap(this.sFilePath || sOutputFile, function(err, str) {
if (err) {
FileDump.logError(err);
} else {
obj.outputFile(sOutputFile, fOverwrite);
}
});
return;
}
this.outputFile(sOutputFile, fOverwrite);
};
/**
* outputFile(sOutputFile, fOverwrite)
*
* @this {FileDump}
* @param {string} sOutputFile
* @param {boolean} fOverwrite
*/
FileDump.prototype.outputFile = function(sOutputFile, fOverwrite)
{
let data = this.json || this.buf;
let sFormat = this.sFormat.toUpperCase();
if (sOutputFile) {
try {
if (fs.existsSync(sOutputFile) && !fOverwrite) {
console.log(sOutputFile + " exists, use --overwrite to rewrite");
} else {
let sDirName = path.dirname(sOutputFile);
if (!fs.existsSync(sDirName)) mkdirp.sync(sDirName);
fs.writeFileSync(sOutputFile, data);
console.log(data.length + "-byte " + sFormat + " file saved as " + sOutputFile);
}
} catch(err) {
FileDump.logError(err);
}
} else {
/*
* We'll dump JSON to the console, but not a raw file buffer; we could add an option to
* "stringify" buffers, but if that's what the caller wants, they should use "--format=json".
*/
if (typeof data == "string") {
console.log(data);
} else {
console.log("specify --output={file} to save " + data.length + "-byte " + sFormat + " file");
}
}
};
module.exports = FileDump;