UNPKG

iso_8583_exos

Version:

A javascript library for messaging in iso 8583 messaging format by Roy Salgado.

146 lines (125 loc) 4.81 kB
const T = require('../tools'); const formats = require('../formats'); const cptable = require('exos_codepage'); /** * Unpack fields 0-127 from an ISO 8583 encoded string into a JSON * @method unpack_0_127 * @memberof module:Message-UnPackage */ module.exports = function (incoming, isoJSON, config, encoding) { if (Buffer.isBuffer(incoming)) { let ASCII = "ASCII"; let mti = encoding === ASCII ? new Buffer.from(incoming.slice(0, 4), 'utf8') : new Buffer.from(cptable.utils.decode(500, incoming.slice(0, 4)), 'utf8'); mti = mti.toString('utf8'); isoJSON['0'] = mti; if (!this._checkMTI(mti)) { return T.toErrorObject('failed to unpack at get mti'); } let bitmapEnd; // Does data contain a secondary bitmap? const secondaryBitmap = this.hasSecondaryBitmap( incoming.slice(4, 8), config ); if (secondaryBitmap === false) bitmapEnd = 12; else bitmapEnd = 20; if (config.bitmapEncoding === 'utf8') bitmapEnd = 36; const slice = incoming .slice(4, bitmapEnd) .toString(config.bitmapEncoding || 'hex'); const bitmap = T.getHex(slice) .split('') .map(Number); let thisBuff = incoming.slice(bitmapEnd, incoming.byteLength); for (let i = 1; i < bitmap.length; i++) { if (bitmap[i] === 1) { //format defined let field = i + 1; let this_format = this.formats[field] || formats[field]; if (field === 127) { let get127Exts = this.unpack_127_1_63(thisBuff, isoJSON); if (get127Exts.error) { return get127Exts; } else { isoJSON = get127Exts.json; thisBuff = get127Exts.remSlice; continue; } } if (this_format) { if (this_format.LenType === 'fixed') { if (this_format.ContentType === 'b') { isoJSON[field] = thisBuff .slice(0, this_format.MaxLen / 2) .toString('hex'); thisBuff = thisBuff.slice( this_format.MaxLen / 2, thisBuff.byteLength ); } else { let dataOfField = encoding === ASCII ? new Buffer.from(thisBuff.slice(0, this_format.MaxLen), 'utf8') : new Buffer.from(cptable.utils.decode(500, thisBuff.slice(0, this_format.MaxLen)), 'utf8'); isoJSON[field] = dataOfField.toString('utf8'); thisBuff = thisBuff.slice( this_format.MaxLen, thisBuff.byteLength ); } } else { let thisLen = T.getLenType(this_format.LenType); if (!this_format.MaxLen) return T.toErrorObject([ 'max length not implemented for ', this_format.LenType, field ]); if ( this.Msg[field] && this.Msg[field].length > this_format.MaxLen ) { return T.toErrorObject([ 'invalid length of data on field ', field ]); } if (thisLen === 0) { return T.toErrorObject([ 'field ', field, ' format not implemented' ]); } else { let len = encoding === ASCII ? new Buffer.from(thisBuff.slice(0, thisLen), 'utf8') : new Buffer.from(cptable.utils.decode(500, thisBuff.slice(0, thisLen)), 'utf8'); len = len.toString(); thisBuff = thisBuff.slice(thisLen, thisBuff.byteLength); if (this_format.ContentType === 'b') { let binaryData = thisBuff.slice(0, len); isoJSON[field] = binaryData.toString('hex'); thisBuff = thisBuff.slice( len, thisBuff.byteLength ); } else { let dataOfField = encoding === ASCII ? new Buffer.from(thisBuff.slice(0, Number(len)), 'utf8') : new Buffer.from(cptable.utils.decode(500, thisBuff.slice(0, Number(len))), 'utf8'); isoJSON[field] = dataOfField.toString(); thisBuff = thisBuff.slice(Number(len), thisBuff.byteLength); } } } } else return T.toErrorObject(['field ', field, ' format not implemented']); } } return { json: isoJSON, remSlice: thisBuff }; } else return T.toErrorObject(['expecting buffer but got ', typeof incoming]); };