2016-02-03 04:35:59 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-01-21 18:58:19 +00:00
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
|
|
|
|
// deps
|
|
|
|
const iconv = require('iconv-lite');
|
|
|
|
const { Parser } = require('binary-parser');
|
2016-02-03 04:35:59 +00:00
|
|
|
|
|
|
|
exports.readSAUCE = readSAUCE;
|
|
|
|
|
|
|
|
const SAUCE_SIZE = 128;
|
2018-04-28 12:59:07 +00:00
|
|
|
const SAUCE_ID = Buffer.from([0x53, 0x41, 0x55, 0x43, 0x45]); // 'SAUCE'
|
2018-01-15 19:22:11 +00:00
|
|
|
|
|
|
|
// :TODO read comments
|
2018-04-28 12:59:07 +00:00
|
|
|
//const COMNT_ID = Buffer.from([0x43, 0x4f, 0x4d, 0x4e, 0x54]); // 'COMNT'
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2016-02-10 05:30:59 +00:00
|
|
|
exports.SAUCE_SIZE = SAUCE_SIZE;
|
2016-02-03 04:35:59 +00:00
|
|
|
// :TODO: SAUCE should be a class
|
|
|
|
// - with getFontName()
|
|
|
|
// - ...other methods
|
|
|
|
|
|
|
|
//
|
|
|
|
// See
|
|
|
|
// http://www.acid.org/info/sauce/sauce.htm
|
|
|
|
//
|
2016-02-10 05:30:59 +00:00
|
|
|
const SAUCE_VALID_DATA_TYPES = [0, 1, 2, 3, 4, 5, 6, 7, 8 ];
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function readSAUCE(data, cb) {
|
|
|
|
if(data.length < SAUCE_SIZE) {
|
2018-01-21 18:58:19 +00:00
|
|
|
return cb(Errors.DoesNotExist('No SAUCE record present'));
|
|
|
|
}
|
|
|
|
|
|
|
|
let sauceRec;
|
|
|
|
try {
|
|
|
|
sauceRec = new Parser()
|
|
|
|
.buffer('id', { length : 5 } )
|
|
|
|
.buffer('version', { length : 2 } )
|
|
|
|
.buffer('title', { length: 35 } )
|
|
|
|
.buffer('author', { length : 20 } )
|
|
|
|
.buffer('group', { length: 20 } )
|
|
|
|
.buffer('date', { length: 8 } )
|
|
|
|
.uint32le('fileSize')
|
|
|
|
.int8('dataType')
|
|
|
|
.int8('fileType')
|
|
|
|
.uint16le('tinfo1')
|
|
|
|
.uint16le('tinfo2')
|
|
|
|
.uint16le('tinfo3')
|
|
|
|
.uint16le('tinfo4')
|
|
|
|
.int8('numComments')
|
|
|
|
.int8('flags')
|
|
|
|
// :TODO: does this need to be optional?
|
|
|
|
.buffer('tinfos', { length: 22 } ) // SAUCE 00.5
|
|
|
|
.parse(data.slice(data.length - SAUCE_SIZE));
|
|
|
|
} catch(e) {
|
|
|
|
return cb(Errors.Invalid('Invalid SAUCE record'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!SAUCE_ID.equals(sauceRec.id)) {
|
|
|
|
return cb(Errors.DoesNotExist('No SAUCE record present'));
|
|
|
|
}
|
|
|
|
|
|
|
|
const ver = iconv.decode(sauceRec.version, 'cp437');
|
|
|
|
|
|
|
|
if('00' !== ver) {
|
|
|
|
return cb(Errors.Invalid(`Unsupported SAUCE version: ${ver}`));
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 18:58:19 +00:00
|
|
|
if(-1 === SAUCE_VALID_DATA_TYPES.indexOf(sauceRec.dataType)) {
|
|
|
|
return cb(Errors.Invalid(`Unsupported SAUCE DataType: ${sauceRec.dataType}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
const sauce = {
|
|
|
|
id : iconv.decode(sauceRec.id, 'cp437'),
|
|
|
|
version : iconv.decode(sauceRec.version, 'cp437').trim(),
|
|
|
|
title : iconv.decode(sauceRec.title, 'cp437').trim(),
|
|
|
|
author : iconv.decode(sauceRec.author, 'cp437').trim(),
|
|
|
|
group : iconv.decode(sauceRec.group, 'cp437').trim(),
|
|
|
|
date : iconv.decode(sauceRec.date, 'cp437').trim(),
|
|
|
|
fileSize : sauceRec.fileSize,
|
|
|
|
dataType : sauceRec.dataType,
|
|
|
|
fileType : sauceRec.fileType,
|
|
|
|
tinfo1 : sauceRec.tinfo1,
|
|
|
|
tinfo2 : sauceRec.tinfo2,
|
|
|
|
tinfo3 : sauceRec.tinfo3,
|
|
|
|
tinfo4 : sauceRec.tinfo4,
|
|
|
|
numComments : sauceRec.numComments,
|
|
|
|
flags : sauceRec.flags,
|
|
|
|
tinfos : sauceRec.tinfos,
|
|
|
|
};
|
|
|
|
|
|
|
|
const dt = SAUCE_DATA_TYPES[sauce.dataType];
|
|
|
|
if(dt && dt.parser) {
|
|
|
|
sauce[dt.name] = dt.parser(sauce);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, sauce);
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// :TODO: These need completed:
|
2018-01-21 18:58:19 +00:00
|
|
|
const SAUCE_DATA_TYPES = {
|
|
|
|
0 : { name : 'None' },
|
|
|
|
1 : { name : 'Character', parser : parseCharacterSAUCE },
|
|
|
|
2 : 'Bitmap',
|
|
|
|
3 : 'Vector',
|
|
|
|
4 : 'Audio',
|
|
|
|
5 : 'BinaryText',
|
|
|
|
6 : 'XBin',
|
|
|
|
7 : 'Archive',
|
|
|
|
8 : 'Executable',
|
|
|
|
};
|
|
|
|
|
|
|
|
const SAUCE_CHARACTER_FILE_TYPES = {
|
|
|
|
0 : 'ASCII',
|
|
|
|
1 : 'ANSi',
|
|
|
|
2 : 'ANSiMation',
|
|
|
|
3 : 'RIP script',
|
|
|
|
4 : 'PCBoard',
|
|
|
|
5 : 'Avatar',
|
|
|
|
6 : 'HTML',
|
|
|
|
7 : 'Source',
|
|
|
|
8 : 'TundraDraw',
|
|
|
|
};
|
2016-02-03 04:35:59 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Map of SAUCE font -> encoding hint
|
|
|
|
//
|
|
|
|
// Note that this is the same mapping that x84 uses. Be compatible!
|
|
|
|
//
|
2018-01-21 18:58:19 +00:00
|
|
|
const SAUCE_FONT_TO_ENCODING_HINT = {
|
2016-02-03 04:35:59 +00:00
|
|
|
'Amiga MicroKnight' : 'amiga',
|
|
|
|
'Amiga MicroKnight+' : 'amiga',
|
|
|
|
'Amiga mOsOul' : 'amiga',
|
|
|
|
'Amiga P0T-NOoDLE' : 'amiga',
|
|
|
|
'Amiga Topaz 1' : 'amiga',
|
|
|
|
'Amiga Topaz 1+' : 'amiga',
|
|
|
|
'Amiga Topaz 2' : 'amiga',
|
|
|
|
'Amiga Topaz 2+' : 'amiga',
|
|
|
|
'Atari ATASCII' : 'atari',
|
|
|
|
'IBM EGA43' : 'cp437',
|
|
|
|
'IBM EGA' : 'cp437',
|
|
|
|
'IBM VGA25G' : 'cp437',
|
|
|
|
'IBM VGA50' : 'cp437',
|
|
|
|
'IBM VGA' : 'cp437',
|
|
|
|
};
|
|
|
|
|
2018-01-21 18:58:19 +00:00
|
|
|
[
|
|
|
|
'437', '720', '737', '775', '819', '850', '852', '855', '857', '858',
|
|
|
|
'860', '861', '862', '863', '864', '865', '866', '869', '872'
|
|
|
|
].forEach( page => {
|
|
|
|
const codec = 'cp' + page;
|
2016-02-03 04:35:59 +00:00
|
|
|
SAUCE_FONT_TO_ENCODING_HINT['IBM EGA43 ' + page] = codec;
|
|
|
|
SAUCE_FONT_TO_ENCODING_HINT['IBM EGA ' + page] = codec;
|
|
|
|
SAUCE_FONT_TO_ENCODING_HINT['IBM VGA25g ' + page] = codec;
|
|
|
|
SAUCE_FONT_TO_ENCODING_HINT['IBM VGA50 ' + page] = codec;
|
|
|
|
SAUCE_FONT_TO_ENCODING_HINT['IBM VGA ' + page] = codec;
|
|
|
|
});
|
|
|
|
|
|
|
|
function parseCharacterSAUCE(sauce) {
|
2018-01-21 18:58:19 +00:00
|
|
|
const result = {};
|
2016-02-03 04:35:59 +00:00
|
|
|
|
|
|
|
result.fileType = SAUCE_CHARACTER_FILE_TYPES[sauce.fileType] || 'Unknown';
|
|
|
|
|
|
|
|
if(sauce.fileType === 0 || sauce.fileType === 1 || sauce.fileType === 2) {
|
|
|
|
// convience: create ansiFlags
|
|
|
|
sauce.ansiFlags = sauce.flags;
|
|
|
|
|
2018-01-21 18:58:19 +00:00
|
|
|
let i = 0;
|
2016-02-03 04:35:59 +00:00
|
|
|
while(i < sauce.tinfos.length && sauce.tinfos[i] !== 0x00) {
|
|
|
|
++i;
|
|
|
|
}
|
2018-01-21 18:58:19 +00:00
|
|
|
|
|
|
|
const fontName = iconv.decode(sauce.tinfos.slice(0, i), 'cp437');
|
2016-02-03 04:35:59 +00:00
|
|
|
if(fontName.length > 0) {
|
|
|
|
result.fontName = fontName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|